s3-samr: a level 18 password set needs to set the pwdlastset as well.
[Samba.git] / source / rpc_server / srv_samr_nt.c
blob9002743e173289cebb7e1b24447147f40568ee04
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Paul Ashton 1997,
7 * Copyright (C) Marc Jacobsen 1999,
8 * Copyright (C) Jeremy Allison 2001-2008,
9 * Copyright (C) Jean François Micouleau 1998-2001,
10 * Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002,
11 * Copyright (C) Gerald (Jerry) Carter 2003-2004,
12 * Copyright (C) Simo Sorce 2003.
13 * Copyright (C) Volker Lendecke 2005.
14 * Copyright (C) Guenther Deschner 2008.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 * This is the implementation of the SAMR code.
34 #include "includes.h"
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_RPC_SRV
39 #define SAMR_USR_RIGHTS_WRITE_PW \
40 ( READ_CONTROL_ACCESS | \
41 SAMR_USER_ACCESS_CHANGE_PASSWORD | \
42 SAMR_USER_ACCESS_SET_LOC_COM)
43 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
44 ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
46 #define DISP_INFO_CACHE_TIMEOUT 10
48 typedef struct disp_info {
49 DOM_SID sid; /* identify which domain this is. */
50 bool builtin_domain; /* Quick flag to check if this is the builtin domain. */
51 struct pdb_search *users; /* querydispinfo 1 and 4 */
52 struct pdb_search *machines; /* querydispinfo 2 */
53 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
54 struct pdb_search *aliases; /* enumaliases */
56 uint16 enum_acb_mask;
57 struct pdb_search *enum_users; /* enumusers with a mask */
59 struct timed_event *cache_timeout_event; /* cache idle timeout
60 * handler. */
61 } DISP_INFO;
63 /* We keep a static list of these by SID as modern clients close down
64 all resources between each request in a complete enumeration. */
66 struct samr_info {
67 /* for use by the \PIPE\samr policy */
68 DOM_SID sid;
69 bool builtin_domain; /* Quick flag to check if this is the builtin domain. */
70 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
71 uint32 acc_granted;
72 DISP_INFO *disp_info;
73 TALLOC_CTX *mem_ctx;
76 static const struct generic_mapping sam_generic_mapping = {
77 GENERIC_RIGHTS_SAM_READ,
78 GENERIC_RIGHTS_SAM_WRITE,
79 GENERIC_RIGHTS_SAM_EXECUTE,
80 GENERIC_RIGHTS_SAM_ALL_ACCESS};
81 static const struct generic_mapping dom_generic_mapping = {
82 GENERIC_RIGHTS_DOMAIN_READ,
83 GENERIC_RIGHTS_DOMAIN_WRITE,
84 GENERIC_RIGHTS_DOMAIN_EXECUTE,
85 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
86 static const struct generic_mapping usr_generic_mapping = {
87 GENERIC_RIGHTS_USER_READ,
88 GENERIC_RIGHTS_USER_WRITE,
89 GENERIC_RIGHTS_USER_EXECUTE,
90 GENERIC_RIGHTS_USER_ALL_ACCESS};
91 static const struct generic_mapping usr_nopwchange_generic_mapping = {
92 GENERIC_RIGHTS_USER_READ,
93 GENERIC_RIGHTS_USER_WRITE,
94 GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
95 GENERIC_RIGHTS_USER_ALL_ACCESS};
96 static const struct generic_mapping grp_generic_mapping = {
97 GENERIC_RIGHTS_GROUP_READ,
98 GENERIC_RIGHTS_GROUP_WRITE,
99 GENERIC_RIGHTS_GROUP_EXECUTE,
100 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
101 static const struct generic_mapping ali_generic_mapping = {
102 GENERIC_RIGHTS_ALIAS_READ,
103 GENERIC_RIGHTS_ALIAS_WRITE,
104 GENERIC_RIGHTS_ALIAS_EXECUTE,
105 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
107 /*******************************************************************
108 *******************************************************************/
110 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
111 const struct generic_mapping *map,
112 DOM_SID *sid, uint32 sid_access )
114 DOM_SID domadmin_sid;
115 SEC_ACE ace[5]; /* at most 5 entries */
116 size_t i = 0;
118 SEC_ACL *psa = NULL;
120 /* basic access for Everyone */
122 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
123 map->generic_execute | map->generic_read, 0);
125 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
127 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
128 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
129 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
130 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
132 /* Add Full Access for Domain Admins if we are a DC */
134 if ( IS_DC ) {
135 sid_copy( &domadmin_sid, get_global_sam_sid() );
136 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
137 init_sec_ace(&ace[i++], &domadmin_sid,
138 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
141 /* if we have a sid, give it some special access */
143 if ( sid ) {
144 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sid_access, 0);
147 /* create the security descriptor */
149 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
150 return NT_STATUS_NO_MEMORY;
152 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
153 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
154 psa, sd_size)) == NULL)
155 return NT_STATUS_NO_MEMORY;
157 return NT_STATUS_OK;
160 /*******************************************************************
161 Checks if access to an object should be granted, and returns that
162 level of access for further checks.
163 ********************************************************************/
165 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
166 SE_PRIV *rights, uint32 rights_mask,
167 uint32 des_access, uint32 *acc_granted,
168 const char *debug )
170 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
171 uint32 saved_mask = 0;
173 /* check privileges; certain SAM access bits should be overridden
174 by privileges (mostly having to do with creating/modifying/deleting
175 users and groups) */
177 if ( rights && user_has_any_privilege( token, rights ) ) {
179 saved_mask = (des_access & rights_mask);
180 des_access &= ~saved_mask;
182 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
183 rights_mask));
187 /* check the security descriptor first */
189 status = se_access_check(psd, token, des_access, acc_granted);
190 if (NT_STATUS_IS_OK(status)) {
191 goto done;
194 /* give root a free pass */
196 if ( geteuid() == sec_initial_uid() ) {
198 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
199 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
201 *acc_granted = des_access;
203 status = NT_STATUS_OK;
204 goto done;
208 done:
209 /* add in any bits saved during the privilege check (only
210 matters is status is ok) */
212 *acc_granted |= rights_mask;
214 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
215 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
216 des_access, *acc_granted));
218 return status;
221 /*******************************************************************
222 Checks if access to a function can be granted
223 ********************************************************************/
225 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
227 DEBUG(5,("%s: access check ((granted: %#010x; required: %#010x)\n",
228 debug, acc_granted, acc_required));
230 /* check the security descriptor first */
232 if ( (acc_granted&acc_required) == acc_required )
233 return NT_STATUS_OK;
235 /* give root a free pass */
237 if (geteuid() == sec_initial_uid()) {
239 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
240 debug, acc_granted, acc_required));
241 DEBUGADD(4,("but overwritten by euid == 0\n"));
243 return NT_STATUS_OK;
246 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: %#010x)\n",
247 debug, acc_granted, acc_required));
249 return NT_STATUS_ACCESS_DENIED;
252 /*******************************************************************
253 Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
254 ********************************************************************/
256 static void map_max_allowed_access(const NT_USER_TOKEN *token,
257 uint32_t *pacc_requested)
259 if (!((*pacc_requested) & MAXIMUM_ALLOWED_ACCESS)) {
260 return;
262 *pacc_requested &= ~MAXIMUM_ALLOWED_ACCESS;
264 /* At least try for generic read. */
265 *pacc_requested = GENERIC_READ_ACCESS;
267 /* root gets anything. */
268 if (geteuid() == sec_initial_uid()) {
269 *pacc_requested |= GENERIC_ALL_ACCESS;
270 return;
273 /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
275 if (is_sid_in_token(token, &global_sid_Builtin_Administrators) ||
276 is_sid_in_token(token, &global_sid_Builtin_Account_Operators)) {
277 *pacc_requested |= GENERIC_ALL_ACCESS;
278 return;
281 /* Full access for DOMAIN\Domain Admins. */
282 if ( IS_DC ) {
283 DOM_SID domadmin_sid;
284 sid_copy( &domadmin_sid, get_global_sam_sid() );
285 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
286 if (is_sid_in_token(token, &domadmin_sid)) {
287 *pacc_requested |= GENERIC_ALL_ACCESS;
288 return;
291 /* TODO ! Check privileges. */
294 /*******************************************************************
295 Fetch or create a dispinfo struct.
296 ********************************************************************/
298 static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid)
301 * We do a static cache for DISP_INFO's here. Explanation can be found
302 * in Jeremy's checkin message to r11793:
304 * Fix the SAMR cache so it works across completely insane
305 * client behaviour (ie.:
306 * open pipe/open SAMR handle/enumerate 0 - 1024
307 * close SAMR handle, close pipe.
308 * open pipe/open SAMR handle/enumerate 1024 - 2048...
309 * close SAMR handle, close pipe.
310 * And on ad-nausium. Amazing.... probably object-oriented
311 * client side programming in action yet again.
312 * This change should *massively* improve performance when
313 * enumerating users from an LDAP database.
314 * Jeremy.
316 * "Our" and the builtin domain are the only ones where we ever
317 * enumerate stuff, so just cache 2 entries.
320 static struct disp_info builtin_dispinfo;
321 static struct disp_info domain_dispinfo;
323 /* There are two cases to consider here:
324 1) The SID is a domain SID and we look for an equality match, or
325 2) This is an account SID and so we return the DISP_INFO* for our
326 domain */
328 if (psid == NULL) {
329 return NULL;
332 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
334 * Necessary only once, but it does not really hurt.
336 sid_copy(&builtin_dispinfo.sid, &global_sid_Builtin);
338 return &builtin_dispinfo;
341 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
343 * Necessary only once, but it does not really hurt.
345 sid_copy(&domain_dispinfo.sid, get_global_sam_sid());
347 return &domain_dispinfo;
350 return NULL;
353 /*******************************************************************
354 Create a samr_info struct.
355 ********************************************************************/
357 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
359 struct samr_info *info;
360 fstring sid_str;
361 TALLOC_CTX *mem_ctx;
363 if (psid) {
364 sid_to_fstring(sid_str, psid);
365 } else {
366 fstrcpy(sid_str,"(NULL)");
369 mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
371 if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
372 return NULL;
374 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
375 if (psid) {
376 sid_copy( &info->sid, psid);
377 info->builtin_domain = sid_check_is_builtin(psid);
378 } else {
379 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
380 info->builtin_domain = False;
382 info->mem_ctx = mem_ctx;
384 info->disp_info = get_samr_dispinfo_by_sid(psid);
386 return info;
389 /*******************************************************************
390 Function to free the per SID data.
391 ********************************************************************/
393 static void free_samr_cache(DISP_INFO *disp_info)
395 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
396 sid_string_dbg(&disp_info->sid)));
398 /* We need to become root here because the paged search might have to
399 * tell the LDAP server we're not interested in the rest anymore. */
401 become_root();
403 if (disp_info->users) {
404 DEBUG(10,("free_samr_cache: deleting users cache\n"));
405 pdb_search_destroy(disp_info->users);
406 disp_info->users = NULL;
408 if (disp_info->machines) {
409 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
410 pdb_search_destroy(disp_info->machines);
411 disp_info->machines = NULL;
413 if (disp_info->groups) {
414 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
415 pdb_search_destroy(disp_info->groups);
416 disp_info->groups = NULL;
418 if (disp_info->aliases) {
419 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
420 pdb_search_destroy(disp_info->aliases);
421 disp_info->aliases = NULL;
423 if (disp_info->enum_users) {
424 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
425 pdb_search_destroy(disp_info->enum_users);
426 disp_info->enum_users = NULL;
428 disp_info->enum_acb_mask = 0;
430 unbecome_root();
433 /*******************************************************************
434 Function to free the per handle data.
435 ********************************************************************/
437 static void free_samr_info(void *ptr)
439 struct samr_info *info=(struct samr_info *) ptr;
441 /* Only free the dispinfo cache if no one bothered to set up
442 a timeout. */
444 if (info->disp_info && info->disp_info->cache_timeout_event == NULL) {
445 free_samr_cache(info->disp_info);
448 talloc_destroy(info->mem_ctx);
451 /*******************************************************************
452 Idle event handler. Throw away the disp info cache.
453 ********************************************************************/
455 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
456 struct timed_event *te,
457 const struct timeval *now,
458 void *private_data)
460 DISP_INFO *disp_info = (DISP_INFO *)private_data;
462 TALLOC_FREE(disp_info->cache_timeout_event);
464 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
465 "out\n"));
466 free_samr_cache(disp_info);
469 /*******************************************************************
470 Setup cache removal idle event handler.
471 ********************************************************************/
473 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
475 /* Remove any pending timeout and update. */
477 TALLOC_FREE(disp_info->cache_timeout_event);
479 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
480 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
481 (unsigned int)secs_fromnow ));
483 disp_info->cache_timeout_event = event_add_timed(
484 smbd_event_context(), NULL,
485 timeval_current_ofs(secs_fromnow, 0),
486 "disp_info_cache_idle_timeout_handler",
487 disp_info_cache_idle_timeout_handler, (void *)disp_info);
490 /*******************************************************************
491 Force flush any cache. We do this on any samr_set_xxx call.
492 We must also remove the timeout handler.
493 ********************************************************************/
495 static void force_flush_samr_cache(DISP_INFO *disp_info)
497 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
498 return;
501 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
502 TALLOC_FREE(disp_info->cache_timeout_event);
503 free_samr_cache(disp_info);
506 /*******************************************************************
507 Ensure password info is never given out. Paranioa... JRA.
508 ********************************************************************/
510 static void samr_clear_sam_passwd(struct samu *sam_pass)
513 if (!sam_pass)
514 return;
516 /* These now zero out the old password */
518 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
519 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
522 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
524 struct samr_displayentry *entry;
526 if (info->builtin_domain) {
527 /* No users in builtin. */
528 return 0;
531 if (info->users == NULL) {
532 info->users = pdb_search_users(acct_flags);
533 if (info->users == NULL) {
534 return 0;
537 /* Fetch the last possible entry, thus trigger an enumeration */
538 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
540 /* Ensure we cache this enumeration. */
541 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
543 return info->users->num_entries;
546 static uint32 count_sam_groups(struct disp_info *info)
548 struct samr_displayentry *entry;
550 if (info->builtin_domain) {
551 /* No groups in builtin. */
552 return 0;
555 if (info->groups == NULL) {
556 info->groups = pdb_search_groups();
557 if (info->groups == NULL) {
558 return 0;
561 /* Fetch the last possible entry, thus trigger an enumeration */
562 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
564 /* Ensure we cache this enumeration. */
565 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
567 return info->groups->num_entries;
570 static uint32 count_sam_aliases(struct disp_info *info)
572 struct samr_displayentry *entry;
574 if (info->aliases == NULL) {
575 info->aliases = pdb_search_aliases(&info->sid);
576 if (info->aliases == NULL) {
577 return 0;
580 /* Fetch the last possible entry, thus trigger an enumeration */
581 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
583 /* Ensure we cache this enumeration. */
584 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
586 return info->aliases->num_entries;
589 /*******************************************************************
590 _samr_Close
591 ********************************************************************/
593 NTSTATUS _samr_Close(pipes_struct *p, struct samr_Close *r)
595 if (!close_policy_hnd(p, r->in.handle)) {
596 return NT_STATUS_INVALID_HANDLE;
599 ZERO_STRUCTP(r->out.handle);
601 return NT_STATUS_OK;
604 /*******************************************************************
605 _samr_OpenDomain
606 ********************************************************************/
608 NTSTATUS _samr_OpenDomain(pipes_struct *p,
609 struct samr_OpenDomain *r)
611 struct samr_info *info;
612 SEC_DESC *psd = NULL;
613 uint32 acc_granted;
614 uint32 des_access = r->in.access_mask;
615 NTSTATUS status;
616 size_t sd_size;
617 SE_PRIV se_rights;
619 /* find the connection policy handle. */
621 if ( !find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info) )
622 return NT_STATUS_INVALID_HANDLE;
624 status = access_check_samr_function(info->acc_granted,
625 SAMR_ACCESS_OPEN_DOMAIN,
626 "_samr_OpenDomain" );
628 if ( !NT_STATUS_IS_OK(status) )
629 return status;
631 /*check if access can be granted as requested by client. */
632 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
634 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
635 se_map_generic( &des_access, &dom_generic_mapping );
637 se_priv_copy( &se_rights, &se_machine_account );
638 se_priv_add( &se_rights, &se_add_users );
640 status = access_check_samr_object( psd, p->pipe_user.nt_user_token,
641 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
642 &acc_granted, "_samr_OpenDomain" );
644 if ( !NT_STATUS_IS_OK(status) )
645 return status;
647 if (!sid_check_is_domain(r->in.sid) &&
648 !sid_check_is_builtin(r->in.sid)) {
649 return NT_STATUS_NO_SUCH_DOMAIN;
652 /* associate the domain SID with the (unique) handle. */
653 if ((info = get_samr_info_by_sid(r->in.sid))==NULL)
654 return NT_STATUS_NO_MEMORY;
655 info->acc_granted = acc_granted;
657 /* get a (unique) handle. open a policy on it. */
658 if (!create_policy_hnd(p, r->out.domain_handle, free_samr_info, (void *)info))
659 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
661 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
663 return NT_STATUS_OK;
666 /*******************************************************************
667 _samr_GetUserPwInfo
668 ********************************************************************/
670 NTSTATUS _samr_GetUserPwInfo(pipes_struct *p,
671 struct samr_GetUserPwInfo *r)
673 struct samr_info *info = NULL;
674 enum lsa_SidType sid_type;
675 uint32_t min_password_length = 0;
676 uint32_t password_properties = 0;
677 bool ret = false;
678 NTSTATUS status;
680 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
682 /* find the policy handle. open a policy on it. */
683 if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info)) {
684 return NT_STATUS_INVALID_HANDLE;
687 status = access_check_samr_function(info->acc_granted,
688 SAMR_USER_ACCESS_GET_ATTRIBUTES,
689 "_samr_GetUserPwInfo" );
690 if (!NT_STATUS_IS_OK(status)) {
691 return status;
694 if (!sid_check_is_in_our_domain(&info->sid)) {
695 return NT_STATUS_OBJECT_TYPE_MISMATCH;
698 become_root();
699 ret = lookup_sid(p->mem_ctx, &info->sid, NULL, NULL, &sid_type);
700 unbecome_root();
701 if (ret == false) {
702 return NT_STATUS_NO_SUCH_USER;
705 switch (sid_type) {
706 case SID_NAME_USER:
707 become_root();
708 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
709 &min_password_length);
710 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
711 &password_properties);
712 unbecome_root();
714 if (lp_check_password_script() && *lp_check_password_script()) {
715 password_properties |= DOMAIN_PASSWORD_COMPLEX;
718 break;
719 default:
720 break;
723 r->out.info->min_password_length = min_password_length;
724 r->out.info->password_properties = password_properties;
726 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
728 return NT_STATUS_OK;
731 /*******************************************************************
732 ********************************************************************/
734 static bool get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol,
735 DOM_SID *sid, uint32 *acc_granted,
736 DISP_INFO **ppdisp_info)
738 struct samr_info *info = NULL;
740 /* find the policy handle. open a policy on it. */
741 if (!find_policy_by_hnd(p, pol, (void **)(void *)&info))
742 return False;
744 if (!info)
745 return False;
747 *sid = info->sid;
748 *acc_granted = info->acc_granted;
749 if (ppdisp_info) {
750 *ppdisp_info = info->disp_info;
753 return True;
756 /*******************************************************************
757 _samr_SetSecurity
758 ********************************************************************/
760 NTSTATUS _samr_SetSecurity(pipes_struct *p,
761 struct samr_SetSecurity *r)
763 DOM_SID pol_sid;
764 uint32 acc_granted, i;
765 SEC_ACL *dacl;
766 bool ret;
767 struct samu *sampass=NULL;
768 NTSTATUS status;
770 if (!get_lsa_policy_samr_sid(p, r->in.handle, &pol_sid, &acc_granted, NULL))
771 return NT_STATUS_INVALID_HANDLE;
773 if (!(sampass = samu_new( p->mem_ctx))) {
774 DEBUG(0,("No memory!\n"));
775 return NT_STATUS_NO_MEMORY;
778 /* get the user record */
779 become_root();
780 ret = pdb_getsampwsid(sampass, &pol_sid);
781 unbecome_root();
783 if (!ret) {
784 DEBUG(4, ("User %s not found\n", sid_string_dbg(&pol_sid)));
785 TALLOC_FREE(sampass);
786 return NT_STATUS_INVALID_HANDLE;
789 dacl = r->in.sdbuf->sd->dacl;
790 for (i=0; i < dacl->num_aces; i++) {
791 if (sid_equal(&pol_sid, &dacl->aces[i].trustee)) {
792 ret = pdb_set_pass_can_change(sampass,
793 (dacl->aces[i].access_mask &
794 SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
795 True: False);
796 break;
800 if (!ret) {
801 TALLOC_FREE(sampass);
802 return NT_STATUS_ACCESS_DENIED;
805 status = access_check_samr_function(acc_granted,
806 SAMR_USER_ACCESS_SET_ATTRIBUTES,
807 "_samr_SetSecurity");
808 if (NT_STATUS_IS_OK(status)) {
809 become_root();
810 status = pdb_update_sam_account(sampass);
811 unbecome_root();
814 TALLOC_FREE(sampass);
816 return status;
819 /*******************************************************************
820 build correct perms based on policies and password times for _samr_query_sec_obj
821 *******************************************************************/
822 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, DOM_SID *user_sid)
824 struct samu *sampass=NULL;
825 bool ret;
827 if ( !(sampass = samu_new( mem_ctx )) ) {
828 DEBUG(0,("No memory!\n"));
829 return False;
832 become_root();
833 ret = pdb_getsampwsid(sampass, user_sid);
834 unbecome_root();
836 if (ret == False) {
837 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
838 TALLOC_FREE(sampass);
839 return False;
842 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
844 if (pdb_get_pass_can_change(sampass)) {
845 TALLOC_FREE(sampass);
846 return True;
848 TALLOC_FREE(sampass);
849 return False;
853 /*******************************************************************
854 _samr_QuerySecurity
855 ********************************************************************/
857 NTSTATUS _samr_QuerySecurity(pipes_struct *p,
858 struct samr_QuerySecurity *r)
860 NTSTATUS status;
861 DOM_SID pol_sid;
862 SEC_DESC * psd = NULL;
863 uint32 acc_granted;
864 size_t sd_size;
866 /* Get the SID. */
867 if (!get_lsa_policy_samr_sid(p, r->in.handle, &pol_sid, &acc_granted, NULL))
868 return NT_STATUS_INVALID_HANDLE;
870 DEBUG(10,("_samr_QuerySecurity: querying security on SID: %s\n",
871 sid_string_dbg(&pol_sid)));
873 status = access_check_samr_function(acc_granted,
874 STD_RIGHT_READ_CONTROL_ACCESS,
875 "_samr_QuerySecurity");
876 if (!NT_STATUS_IS_OK(status)) {
877 return status;
880 /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
882 /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
883 if (pol_sid.sid_rev_num == 0) {
884 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
885 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
886 } else if (sid_equal(&pol_sid,get_global_sam_sid())) {
887 /* check if it is our domain SID */
888 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
889 "with SID: %s\n", sid_string_dbg(&pol_sid)));
890 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
891 } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
892 /* check if it is the Builtin Domain */
893 /* TODO: Builtin probably needs a different SD with restricted write access*/
894 DEBUG(5,("_samr_QuerySecurity: querying security on Builtin "
895 "Domain with SID: %s\n", sid_string_dbg(&pol_sid)));
896 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
897 } else if (sid_check_is_in_our_domain(&pol_sid) ||
898 sid_check_is_in_builtin(&pol_sid)) {
899 /* TODO: different SDs have to be generated for aliases groups and users.
900 Currently all three get a default user SD */
901 DEBUG(10,("_samr_QuerySecurity: querying security on Object "
902 "with SID: %s\n", sid_string_dbg(&pol_sid)));
903 if (check_change_pw_access(p->mem_ctx, &pol_sid)) {
904 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
905 &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
906 } else {
907 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_nopwchange_generic_mapping,
908 &pol_sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
910 } else {
911 return NT_STATUS_OBJECT_TYPE_MISMATCH;
914 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
915 return NT_STATUS_NO_MEMORY;
917 return status;
920 /*******************************************************************
921 makes a SAM_ENTRY / UNISTR2* structure from a user list.
922 ********************************************************************/
924 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
925 struct samr_SamEntry **sam_pp,
926 uint32_t num_entries,
927 uint32_t start_idx,
928 struct samr_displayentry *entries)
930 uint32_t i;
931 struct samr_SamEntry *sam;
933 *sam_pp = NULL;
935 if (num_entries == 0) {
936 return NT_STATUS_OK;
939 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
940 if (sam == NULL) {
941 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
942 return NT_STATUS_NO_MEMORY;
945 for (i = 0; i < num_entries; i++) {
946 #if 0
948 * usrmgr expects a non-NULL terminated string with
949 * trust relationships
951 if (entries[i].acct_flags & ACB_DOMTRUST) {
952 init_unistr2(&uni_temp_name, entries[i].account_name,
953 UNI_FLAGS_NONE);
954 } else {
955 init_unistr2(&uni_temp_name, entries[i].account_name,
956 UNI_STR_TERMINATE);
958 #endif
959 init_lsa_String(&sam[i].name, entries[i].account_name);
960 sam[i].idx = entries[i].rid;
963 *sam_pp = sam;
965 return NT_STATUS_OK;
968 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
970 /*******************************************************************
971 _samr_EnumDomainUsers
972 ********************************************************************/
974 NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
975 struct samr_EnumDomainUsers *r)
977 NTSTATUS status;
978 struct samr_info *info = NULL;
979 int num_account;
980 uint32 enum_context = *r->in.resume_handle;
981 enum remote_arch_types ra_type = get_remote_arch();
982 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
983 uint32 max_entries = max_sam_entries;
984 struct samr_displayentry *entries = NULL;
985 struct samr_SamArray *samr_array = NULL;
986 struct samr_SamEntry *samr_entries = NULL;
988 /* find the policy handle. open a policy on it. */
989 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
990 return NT_STATUS_INVALID_HANDLE;
992 status = access_check_samr_function(info->acc_granted,
993 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
994 "_samr_EnumDomainUsers");
995 if (!NT_STATUS_IS_OK(status)) {
996 return status;
999 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1001 if (info->builtin_domain) {
1002 /* No users in builtin. */
1003 *r->out.resume_handle = *r->in.resume_handle;
1004 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
1005 return status;
1008 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1009 if (!samr_array) {
1010 return NT_STATUS_NO_MEMORY;
1012 *r->out.sam = samr_array;
1014 become_root();
1016 /* AS ROOT !!!! */
1018 if ((info->disp_info->enum_users != NULL) &&
1019 (info->disp_info->enum_acb_mask != r->in.acct_flags)) {
1020 pdb_search_destroy(info->disp_info->enum_users);
1021 info->disp_info->enum_users = NULL;
1024 if (info->disp_info->enum_users == NULL) {
1025 info->disp_info->enum_users = pdb_search_users(r->in.acct_flags);
1026 info->disp_info->enum_acb_mask = r->in.acct_flags;
1029 if (info->disp_info->enum_users == NULL) {
1030 /* END AS ROOT !!!! */
1031 unbecome_root();
1032 return NT_STATUS_ACCESS_DENIED;
1035 num_account = pdb_search_entries(info->disp_info->enum_users,
1036 enum_context, max_entries,
1037 &entries);
1039 /* END AS ROOT !!!! */
1041 unbecome_root();
1043 if (num_account == 0) {
1044 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
1045 "total entries\n"));
1046 *r->out.resume_handle = *r->in.resume_handle;
1047 return NT_STATUS_OK;
1050 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
1051 num_account, enum_context,
1052 entries);
1053 if (!NT_STATUS_IS_OK(status)) {
1054 return status;
1057 if (max_entries <= num_account) {
1058 status = STATUS_MORE_ENTRIES;
1059 } else {
1060 status = NT_STATUS_OK;
1063 /* Ensure we cache this enumeration. */
1064 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1066 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
1068 samr_array->count = num_account;
1069 samr_array->entries = samr_entries;
1071 *r->out.resume_handle = *r->in.resume_handle + num_account;
1072 *r->out.num_entries = num_account;
1074 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1076 return status;
1079 /*******************************************************************
1080 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1081 ********************************************************************/
1083 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
1084 struct samr_SamEntry **sam_pp,
1085 uint32_t num_sam_entries,
1086 struct samr_displayentry *entries)
1088 struct samr_SamEntry *sam;
1089 uint32_t i;
1091 *sam_pp = NULL;
1093 if (num_sam_entries == 0) {
1094 return;
1097 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1098 if (sam == NULL) {
1099 return;
1102 for (i = 0; i < num_sam_entries; i++) {
1104 * JRA. I think this should include the null. TNG does not.
1106 init_lsa_String(&sam[i].name, entries[i].account_name);
1107 sam[i].idx = entries[i].rid;
1110 *sam_pp = sam;
1113 /*******************************************************************
1114 _samr_EnumDomainGroups
1115 ********************************************************************/
1117 NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
1118 struct samr_EnumDomainGroups *r)
1120 NTSTATUS status;
1121 struct samr_info *info = NULL;
1122 struct samr_displayentry *groups;
1123 uint32 num_groups;
1124 struct samr_SamArray *samr_array = NULL;
1125 struct samr_SamEntry *samr_entries = NULL;
1127 /* find the policy handle. open a policy on it. */
1128 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1129 return NT_STATUS_INVALID_HANDLE;
1131 status = access_check_samr_function(info->acc_granted,
1132 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
1133 "_samr_EnumDomainGroups");
1134 if (!NT_STATUS_IS_OK(status)) {
1135 return status;
1138 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1140 if (info->builtin_domain) {
1141 /* No groups in builtin. */
1142 *r->out.resume_handle = *r->in.resume_handle;
1143 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1144 return status;
1147 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1148 if (!samr_array) {
1149 return NT_STATUS_NO_MEMORY;
1152 /* the domain group array is being allocated in the function below */
1154 become_root();
1156 if (info->disp_info->groups == NULL) {
1157 info->disp_info->groups = pdb_search_groups();
1159 if (info->disp_info->groups == NULL) {
1160 unbecome_root();
1161 return NT_STATUS_ACCESS_DENIED;
1165 num_groups = pdb_search_entries(info->disp_info->groups,
1166 *r->in.resume_handle,
1167 MAX_SAM_ENTRIES, &groups);
1168 unbecome_root();
1170 /* Ensure we cache this enumeration. */
1171 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1173 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1174 num_groups, groups);
1176 samr_array->count = num_groups;
1177 samr_array->entries = samr_entries;
1179 *r->out.sam = samr_array;
1180 *r->out.num_entries = num_groups;
1181 /* this was missing, IMHO:
1182 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1185 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1187 return status;
1190 /*******************************************************************
1191 _samr_EnumDomainAliases
1192 ********************************************************************/
1194 NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
1195 struct samr_EnumDomainAliases *r)
1197 NTSTATUS status;
1198 struct samr_info *info;
1199 struct samr_displayentry *aliases;
1200 uint32 num_aliases = 0;
1201 struct samr_SamArray *samr_array = NULL;
1202 struct samr_SamEntry *samr_entries = NULL;
1204 /* find the policy handle. open a policy on it. */
1205 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1206 return NT_STATUS_INVALID_HANDLE;
1208 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1209 sid_string_dbg(&info->sid)));
1211 status = access_check_samr_function(info->acc_granted,
1212 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
1213 "_samr_EnumDomainAliases");
1214 if (!NT_STATUS_IS_OK(status)) {
1215 return status;
1218 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1219 if (!samr_array) {
1220 return NT_STATUS_NO_MEMORY;
1223 become_root();
1225 if (info->disp_info->aliases == NULL) {
1226 info->disp_info->aliases = pdb_search_aliases(&info->sid);
1227 if (info->disp_info->aliases == NULL) {
1228 unbecome_root();
1229 return NT_STATUS_ACCESS_DENIED;
1233 num_aliases = pdb_search_entries(info->disp_info->aliases,
1234 *r->in.resume_handle,
1235 MAX_SAM_ENTRIES, &aliases);
1236 unbecome_root();
1238 /* Ensure we cache this enumeration. */
1239 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1241 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1242 num_aliases, aliases);
1244 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1246 samr_array->count = num_aliases;
1247 samr_array->entries = samr_entries;
1249 *r->out.sam = samr_array;
1250 *r->out.num_entries = num_aliases;
1251 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1253 return status;
1256 /*******************************************************************
1257 inits a samr_DispInfoGeneral structure.
1258 ********************************************************************/
1260 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1261 struct samr_DispInfoGeneral *r,
1262 uint32_t num_entries,
1263 uint32_t start_idx,
1264 struct samr_displayentry *entries)
1266 uint32 i;
1268 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1270 if (num_entries == 0) {
1271 return NT_STATUS_OK;
1274 r->count = num_entries;
1276 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1277 if (!r->entries) {
1278 return NT_STATUS_NO_MEMORY;
1281 for (i = 0; i < num_entries ; i++) {
1283 init_lsa_String(&r->entries[i].account_name,
1284 entries[i].account_name);
1286 init_lsa_String(&r->entries[i].description,
1287 entries[i].description);
1289 init_lsa_String(&r->entries[i].full_name,
1290 entries[i].fullname);
1292 r->entries[i].rid = entries[i].rid;
1293 r->entries[i].acct_flags = entries[i].acct_flags;
1294 r->entries[i].idx = start_idx+i+1;
1297 return NT_STATUS_OK;
1300 /*******************************************************************
1301 inits a samr_DispInfoFull structure.
1302 ********************************************************************/
1304 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1305 struct samr_DispInfoFull *r,
1306 uint32_t num_entries,
1307 uint32_t start_idx,
1308 struct samr_displayentry *entries)
1310 uint32_t i;
1312 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1314 if (num_entries == 0) {
1315 return NT_STATUS_OK;
1318 r->count = num_entries;
1320 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFull, num_entries);
1321 if (!r->entries) {
1322 return NT_STATUS_NO_MEMORY;
1325 for (i = 0; i < num_entries ; i++) {
1327 init_lsa_String(&r->entries[i].account_name,
1328 entries[i].account_name);
1330 init_lsa_String(&r->entries[i].description,
1331 entries[i].description);
1333 r->entries[i].rid = entries[i].rid;
1334 r->entries[i].acct_flags = entries[i].acct_flags;
1335 r->entries[i].idx = start_idx+i+1;
1338 return NT_STATUS_OK;
1341 /*******************************************************************
1342 inits a samr_DispInfoFullGroups structure.
1343 ********************************************************************/
1345 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1346 struct samr_DispInfoFullGroups *r,
1347 uint32_t num_entries,
1348 uint32_t start_idx,
1349 struct samr_displayentry *entries)
1351 uint32_t i;
1353 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1355 if (num_entries == 0) {
1356 return NT_STATUS_OK;
1359 r->count = num_entries;
1361 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1362 if (!r->entries) {
1363 return NT_STATUS_NO_MEMORY;
1366 for (i = 0; i < num_entries ; i++) {
1368 init_lsa_String(&r->entries[i].account_name,
1369 entries[i].account_name);
1371 init_lsa_String(&r->entries[i].description,
1372 entries[i].description);
1374 r->entries[i].rid = entries[i].rid;
1375 r->entries[i].acct_flags = entries[i].acct_flags;
1376 r->entries[i].idx = start_idx+i+1;
1379 return NT_STATUS_OK;
1382 /*******************************************************************
1383 inits a samr_DispInfoAscii structure.
1384 ********************************************************************/
1386 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1387 struct samr_DispInfoAscii *r,
1388 uint32_t num_entries,
1389 uint32_t start_idx,
1390 struct samr_displayentry *entries)
1392 uint32_t i;
1394 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1396 if (num_entries == 0) {
1397 return NT_STATUS_OK;
1400 r->count = num_entries;
1402 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1403 if (!r->entries) {
1404 return NT_STATUS_NO_MEMORY;
1407 for (i = 0; i < num_entries ; i++) {
1409 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1410 entries[i].account_name);
1412 r->entries[i].idx = start_idx+i+1;
1415 return NT_STATUS_OK;
1418 /*******************************************************************
1419 inits a samr_DispInfoAscii structure.
1420 ********************************************************************/
1422 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1423 struct samr_DispInfoAscii *r,
1424 uint32_t num_entries,
1425 uint32_t start_idx,
1426 struct samr_displayentry *entries)
1428 uint32_t i;
1430 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1432 if (num_entries == 0) {
1433 return NT_STATUS_OK;
1436 r->count = num_entries;
1438 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1439 if (!r->entries) {
1440 return NT_STATUS_NO_MEMORY;
1443 for (i = 0; i < num_entries ; i++) {
1445 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1446 entries[i].account_name);
1448 r->entries[i].idx = start_idx+i+1;
1451 return NT_STATUS_OK;
1454 /*******************************************************************
1455 _samr_QueryDisplayInfo
1456 ********************************************************************/
1458 NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
1459 struct samr_QueryDisplayInfo *r)
1461 NTSTATUS status;
1462 struct samr_info *info = NULL;
1463 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1465 uint32 max_entries = r->in.max_entries;
1466 uint32 enum_context = r->in.start_idx;
1467 uint32 max_size = r->in.buf_size;
1469 union samr_DispInfo *disp_info = r->out.info;
1471 uint32 temp_size=0, total_data_size=0;
1472 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1473 uint32 num_account = 0;
1474 enum remote_arch_types ra_type = get_remote_arch();
1475 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1476 struct samr_displayentry *entries = NULL;
1478 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1480 /* find the policy handle. open a policy on it. */
1481 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
1482 return NT_STATUS_INVALID_HANDLE;
1484 if (info->builtin_domain) {
1485 DEBUG(5,("_samr_QueryDisplayInfo: Nothing in BUILTIN\n"));
1486 return NT_STATUS_OK;
1489 status = access_check_samr_function(info->acc_granted,
1490 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
1491 "_samr_QueryDisplayInfo");
1492 if (!NT_STATUS_IS_OK(status)) {
1493 return status;
1497 * calculate how many entries we will return.
1498 * based on
1499 * - the number of entries the client asked
1500 * - our limit on that
1501 * - the starting point (enumeration context)
1502 * - the buffer size the client will accept
1506 * We are a lot more like W2K. Instead of reading the SAM
1507 * each time to find the records we need to send back,
1508 * we read it once and link that copy to the sam handle.
1509 * For large user list (over the MAX_SAM_ENTRIES)
1510 * it's a definitive win.
1511 * second point to notice: between enumerations
1512 * our sam is now the same as it's a snapshoot.
1513 * third point: got rid of the static SAM_USER_21 struct
1514 * no more intermediate.
1515 * con: it uses much more memory, as a full copy is stored
1516 * in memory.
1518 * If you want to change it, think twice and think
1519 * of the second point , that's really important.
1521 * JFM, 12/20/2001
1524 if ((r->in.level < 1) || (r->in.level > 5)) {
1525 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1526 (unsigned int)r->in.level ));
1527 return NT_STATUS_INVALID_INFO_CLASS;
1530 /* first limit the number of entries we will return */
1531 if(max_entries > max_sam_entries) {
1532 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1533 "entries, limiting to %d\n", max_entries,
1534 max_sam_entries));
1535 max_entries = max_sam_entries;
1538 /* calculate the size and limit on the number of entries we will
1539 * return */
1541 temp_size=max_entries*struct_size;
1543 if (temp_size>max_size) {
1544 max_entries=MIN((max_size/struct_size),max_entries);;
1545 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1546 "only %d entries\n", max_entries));
1549 become_root();
1551 /* THe following done as ROOT. Don't return without unbecome_root(). */
1553 switch (r->in.level) {
1554 case 0x1:
1555 case 0x4:
1556 if (info->disp_info->users == NULL) {
1557 info->disp_info->users = pdb_search_users(ACB_NORMAL);
1558 if (info->disp_info->users == NULL) {
1559 unbecome_root();
1560 return NT_STATUS_ACCESS_DENIED;
1562 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1563 (unsigned int)enum_context ));
1564 } else {
1565 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1566 (unsigned int)enum_context ));
1569 num_account = pdb_search_entries(info->disp_info->users,
1570 enum_context, max_entries,
1571 &entries);
1572 break;
1573 case 0x2:
1574 if (info->disp_info->machines == NULL) {
1575 info->disp_info->machines =
1576 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
1577 if (info->disp_info->machines == NULL) {
1578 unbecome_root();
1579 return NT_STATUS_ACCESS_DENIED;
1581 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1582 (unsigned int)enum_context ));
1583 } else {
1584 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1585 (unsigned int)enum_context ));
1588 num_account = pdb_search_entries(info->disp_info->machines,
1589 enum_context, max_entries,
1590 &entries);
1591 break;
1592 case 0x3:
1593 case 0x5:
1594 if (info->disp_info->groups == NULL) {
1595 info->disp_info->groups = pdb_search_groups();
1596 if (info->disp_info->groups == NULL) {
1597 unbecome_root();
1598 return NT_STATUS_ACCESS_DENIED;
1600 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1601 (unsigned int)enum_context ));
1602 } else {
1603 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1604 (unsigned int)enum_context ));
1607 num_account = pdb_search_entries(info->disp_info->groups,
1608 enum_context, max_entries,
1609 &entries);
1610 break;
1611 default:
1612 unbecome_root();
1613 smb_panic("info class changed");
1614 break;
1616 unbecome_root();
1619 /* Now create reply structure */
1620 switch (r->in.level) {
1621 case 0x1:
1622 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1623 num_account, enum_context,
1624 entries);
1625 break;
1626 case 0x2:
1627 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1628 num_account, enum_context,
1629 entries);
1630 break;
1631 case 0x3:
1632 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1633 num_account, enum_context,
1634 entries);
1635 break;
1636 case 0x4:
1637 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1638 num_account, enum_context,
1639 entries);
1640 break;
1641 case 0x5:
1642 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1643 num_account, enum_context,
1644 entries);
1645 break;
1646 default:
1647 smb_panic("info class changed");
1648 break;
1651 if (!NT_STATUS_IS_OK(disp_ret))
1652 return disp_ret;
1654 /* calculate the total size */
1655 total_data_size=num_account*struct_size;
1657 if (max_entries <= num_account) {
1658 status = STATUS_MORE_ENTRIES;
1659 } else {
1660 status = NT_STATUS_OK;
1663 /* Ensure we cache this enumeration. */
1664 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1666 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1668 *r->out.total_size = total_data_size;
1669 *r->out.returned_size = temp_size;
1671 return status;
1674 /****************************************************************
1675 _samr_QueryDisplayInfo2
1676 ****************************************************************/
1678 NTSTATUS _samr_QueryDisplayInfo2(pipes_struct *p,
1679 struct samr_QueryDisplayInfo2 *r)
1681 struct samr_QueryDisplayInfo q;
1683 q.in.domain_handle = r->in.domain_handle;
1684 q.in.level = r->in.level;
1685 q.in.start_idx = r->in.start_idx;
1686 q.in.max_entries = r->in.max_entries;
1687 q.in.buf_size = r->in.buf_size;
1689 q.out.total_size = r->out.total_size;
1690 q.out.returned_size = r->out.returned_size;
1691 q.out.info = r->out.info;
1693 return _samr_QueryDisplayInfo(p, &q);
1696 /****************************************************************
1697 _samr_QueryDisplayInfo3
1698 ****************************************************************/
1700 NTSTATUS _samr_QueryDisplayInfo3(pipes_struct *p,
1701 struct samr_QueryDisplayInfo3 *r)
1703 struct samr_QueryDisplayInfo q;
1705 q.in.domain_handle = r->in.domain_handle;
1706 q.in.level = r->in.level;
1707 q.in.start_idx = r->in.start_idx;
1708 q.in.max_entries = r->in.max_entries;
1709 q.in.buf_size = r->in.buf_size;
1711 q.out.total_size = r->out.total_size;
1712 q.out.returned_size = r->out.returned_size;
1713 q.out.info = r->out.info;
1715 return _samr_QueryDisplayInfo(p, &q);
1718 /*******************************************************************
1719 _samr_QueryAliasInfo
1720 ********************************************************************/
1722 NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
1723 struct samr_QueryAliasInfo *r)
1725 DOM_SID sid;
1726 struct acct_info info;
1727 uint32 acc_granted;
1728 NTSTATUS status;
1729 union samr_AliasInfo *alias_info = NULL;
1730 const char *alias_name = NULL;
1731 const char *alias_description = NULL;
1733 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1735 alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1736 if (!alias_info) {
1737 return NT_STATUS_NO_MEMORY;
1740 /* find the policy handle. open a policy on it. */
1741 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &sid, &acc_granted, NULL))
1742 return NT_STATUS_INVALID_HANDLE;
1744 status = access_check_samr_function(acc_granted,
1745 SAMR_ALIAS_ACCESS_LOOKUP_INFO,
1746 "_samr_QueryAliasInfo");
1747 if (!NT_STATUS_IS_OK(status)) {
1748 return status;
1751 become_root();
1752 status = pdb_get_aliasinfo(&sid, &info);
1753 unbecome_root();
1755 if ( !NT_STATUS_IS_OK(status))
1756 return status;
1758 /* FIXME: info contains fstrings */
1759 alias_name = talloc_strdup(r, info.acct_name);
1760 alias_description = talloc_strdup(r, info.acct_desc);
1762 switch (r->in.level) {
1763 case ALIASINFOALL:
1764 init_samr_alias_info1(&alias_info->all,
1765 alias_name,
1767 alias_description);
1768 break;
1769 case ALIASINFODESCRIPTION:
1770 init_samr_alias_info3(&alias_info->description,
1771 alias_description);
1772 break;
1773 default:
1774 return NT_STATUS_INVALID_INFO_CLASS;
1777 *r->out.info = alias_info;
1779 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1781 return NT_STATUS_OK;
1784 #if 0
1785 /*******************************************************************
1786 samr_reply_lookup_ids
1787 ********************************************************************/
1789 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1791 uint32 rid[MAX_SAM_ENTRIES];
1792 int num_rids = q_u->num_sids1;
1794 r_u->status = NT_STATUS_OK;
1796 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1798 if (num_rids > MAX_SAM_ENTRIES) {
1799 num_rids = MAX_SAM_ENTRIES;
1800 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1803 #if 0
1804 int i;
1805 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1807 for (i = 0; i < num_rids && status == 0; i++)
1809 struct sam_passwd *sam_pass;
1810 fstring user_name;
1813 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1814 q_u->uni_user_name[i].uni_str_len));
1816 /* find the user account */
1817 become_root();
1818 sam_pass = get_smb21pwd_entry(user_name, 0);
1819 unbecome_root();
1821 if (sam_pass == NULL)
1823 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1824 rid[i] = 0;
1826 else
1828 rid[i] = sam_pass->user_rid;
1831 #endif
1833 num_rids = 1;
1834 rid[0] = BUILTIN_ALIAS_RID_USERS;
1836 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1838 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1840 return r_u->status;
1842 #endif
1844 /*******************************************************************
1845 _samr_LookupNames
1846 ********************************************************************/
1848 NTSTATUS _samr_LookupNames(pipes_struct *p,
1849 struct samr_LookupNames *r)
1851 NTSTATUS status;
1852 uint32 *rid;
1853 enum lsa_SidType *type;
1854 int i;
1855 int num_rids = r->in.num_names;
1856 DOM_SID pol_sid;
1857 uint32 acc_granted;
1858 struct samr_Ids rids, types;
1859 uint32_t num_mapped = 0;
1861 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1863 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &pol_sid, &acc_granted, NULL)) {
1864 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1867 status = access_check_samr_function(acc_granted,
1868 0, /* Don't know the acc_bits yet */
1869 "_samr_LookupNames");
1870 if (!NT_STATUS_IS_OK(status)) {
1871 return status;
1874 if (num_rids > MAX_SAM_ENTRIES) {
1875 num_rids = MAX_SAM_ENTRIES;
1876 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1879 rid = talloc_array(p->mem_ctx, uint32, num_rids);
1880 NT_STATUS_HAVE_NO_MEMORY(rid);
1882 type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1883 NT_STATUS_HAVE_NO_MEMORY(type);
1885 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1886 sid_string_dbg(&pol_sid)));
1888 for (i = 0; i < num_rids; i++) {
1890 status = NT_STATUS_NONE_MAPPED;
1891 type[i] = SID_NAME_UNKNOWN;
1893 rid[i] = 0xffffffff;
1895 if (sid_check_is_builtin(&pol_sid)) {
1896 if (lookup_builtin_name(r->in.names[i].string,
1897 &rid[i]))
1899 type[i] = SID_NAME_ALIAS;
1901 } else {
1902 lookup_global_sam_name(r->in.names[i].string, 0,
1903 &rid[i], &type[i]);
1906 if (type[i] != SID_NAME_UNKNOWN) {
1907 num_mapped++;
1911 if (num_mapped == num_rids) {
1912 status = NT_STATUS_OK;
1913 } else if (num_mapped == 0) {
1914 status = NT_STATUS_NONE_MAPPED;
1915 } else {
1916 status = STATUS_SOME_UNMAPPED;
1919 rids.count = num_rids;
1920 rids.ids = rid;
1922 types.count = num_rids;
1923 types.ids = type;
1925 *r->out.rids = rids;
1926 *r->out.types = types;
1928 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1930 return status;
1933 /*******************************************************************
1934 _samr_ChangePasswordUser2
1935 ********************************************************************/
1937 NTSTATUS _samr_ChangePasswordUser2(pipes_struct *p,
1938 struct samr_ChangePasswordUser2 *r)
1940 NTSTATUS status;
1941 fstring user_name;
1942 fstring wks;
1944 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1946 fstrcpy(user_name, r->in.account->string);
1947 fstrcpy(wks, r->in.server->string);
1949 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1952 * Pass the user through the NT -> unix user mapping
1953 * function.
1956 (void)map_username(user_name);
1959 * UNIX username case mangling not required, pass_oem_change
1960 * is case insensitive.
1963 status = pass_oem_change(user_name,
1964 r->in.lm_password->data,
1965 r->in.lm_verifier->hash,
1966 r->in.nt_password->data,
1967 r->in.nt_verifier->hash,
1968 NULL);
1970 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1972 return status;
1975 /*******************************************************************
1976 _samr_ChangePasswordUser3
1977 ********************************************************************/
1979 NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
1980 struct samr_ChangePasswordUser3 *r)
1982 NTSTATUS status;
1983 fstring user_name;
1984 const char *wks = NULL;
1985 uint32 reject_reason;
1986 struct samr_DomInfo1 *dominfo = NULL;
1987 struct samr_ChangeReject *reject = NULL;
1989 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1991 fstrcpy(user_name, r->in.account->string);
1992 if (r->in.server && r->in.server->string) {
1993 wks = r->in.server->string;
1996 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
1999 * Pass the user through the NT -> unix user mapping
2000 * function.
2003 (void)map_username(user_name);
2006 * UNIX username case mangling not required, pass_oem_change
2007 * is case insensitive.
2010 status = pass_oem_change(user_name,
2011 r->in.lm_password->data,
2012 r->in.lm_verifier->hash,
2013 r->in.nt_password->data,
2014 r->in.nt_verifier->hash,
2015 &reject_reason);
2017 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
2018 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
2020 uint32 min_pass_len,pass_hist,password_properties;
2021 time_t u_expire, u_min_age;
2022 NTTIME nt_expire, nt_min_age;
2023 uint32 account_policy_temp;
2025 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
2026 if (!dominfo) {
2027 return NT_STATUS_NO_MEMORY;
2030 reject = TALLOC_ZERO_P(p->mem_ctx, struct samr_ChangeReject);
2031 if (!reject) {
2032 return NT_STATUS_NO_MEMORY;
2035 become_root();
2037 /* AS ROOT !!! */
2039 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2040 min_pass_len = account_policy_temp;
2042 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2043 pass_hist = account_policy_temp;
2045 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2046 password_properties = account_policy_temp;
2048 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2049 u_expire = account_policy_temp;
2051 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2052 u_min_age = account_policy_temp;
2054 /* !AS ROOT */
2056 unbecome_root();
2058 unix_to_nt_time_abs(&nt_expire, u_expire);
2059 unix_to_nt_time_abs(&nt_min_age, u_min_age);
2061 if (lp_check_password_script() && *lp_check_password_script()) {
2062 password_properties |= DOMAIN_PASSWORD_COMPLEX;
2065 init_samr_DomInfo1(dominfo,
2066 min_pass_len,
2067 pass_hist,
2068 password_properties,
2069 u_expire,
2070 u_min_age);
2072 reject->reason = reject_reason;
2074 *r->out.dominfo = dominfo;
2075 *r->out.reject = reject;
2078 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2080 return status;
2083 /*******************************************************************
2084 makes a SAMR_R_LOOKUP_RIDS structure.
2085 ********************************************************************/
2087 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
2088 const char **names,
2089 struct lsa_String **lsa_name_array_p)
2091 struct lsa_String *lsa_name_array = NULL;
2092 uint32_t i;
2094 *lsa_name_array_p = NULL;
2096 if (num_names != 0) {
2097 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
2098 if (!lsa_name_array) {
2099 return false;
2103 for (i = 0; i < num_names; i++) {
2104 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
2105 init_lsa_String(&lsa_name_array[i], names[i]);
2108 *lsa_name_array_p = lsa_name_array;
2110 return true;
2113 /*******************************************************************
2114 _samr_LookupRids
2115 ********************************************************************/
2117 NTSTATUS _samr_LookupRids(pipes_struct *p,
2118 struct samr_LookupRids *r)
2120 NTSTATUS status;
2121 const char **names;
2122 enum lsa_SidType *attrs = NULL;
2123 uint32 *wire_attrs = NULL;
2124 DOM_SID pol_sid;
2125 int num_rids = (int)r->in.num_rids;
2126 uint32 acc_granted;
2127 int i;
2128 struct lsa_Strings names_array;
2129 struct samr_Ids types_array;
2130 struct lsa_String *lsa_names = NULL;
2132 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2134 /* find the policy handle. open a policy on it. */
2135 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &pol_sid, &acc_granted, NULL))
2136 return NT_STATUS_INVALID_HANDLE;
2138 status = access_check_samr_function(acc_granted,
2139 0, /* Don't know the acc_bits yet */
2140 "_samr_LookupRids");
2141 if (!NT_STATUS_IS_OK(status)) {
2142 return status;
2145 if (num_rids > 1000) {
2146 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2147 "to samba4 idl this is not possible\n", num_rids));
2148 return NT_STATUS_UNSUCCESSFUL;
2151 if (num_rids) {
2152 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
2153 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
2154 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
2156 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2157 return NT_STATUS_NO_MEMORY;
2158 } else {
2159 names = NULL;
2160 attrs = NULL;
2161 wire_attrs = NULL;
2164 become_root(); /* lookup_sid can require root privs */
2165 status = pdb_lookup_rids(&pol_sid, num_rids, r->in.rids,
2166 names, attrs);
2167 unbecome_root();
2169 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2170 status = NT_STATUS_OK;
2173 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2174 &lsa_names)) {
2175 return NT_STATUS_NO_MEMORY;
2178 /* Convert from enum lsa_SidType to uint32 for wire format. */
2179 for (i = 0; i < num_rids; i++) {
2180 wire_attrs[i] = (uint32)attrs[i];
2183 names_array.count = num_rids;
2184 names_array.names = lsa_names;
2186 types_array.count = num_rids;
2187 types_array.ids = wire_attrs;
2189 *r->out.names = names_array;
2190 *r->out.types = types_array;
2192 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2194 return status;
2197 /*******************************************************************
2198 _samr_OpenUser
2199 ********************************************************************/
2201 NTSTATUS _samr_OpenUser(pipes_struct *p,
2202 struct samr_OpenUser *r)
2204 struct samu *sampass=NULL;
2205 DOM_SID sid;
2206 POLICY_HND domain_pol = *r->in.domain_handle;
2207 POLICY_HND *user_pol = r->out.user_handle;
2208 struct samr_info *info = NULL;
2209 SEC_DESC *psd = NULL;
2210 uint32 acc_granted;
2211 uint32 des_access = r->in.access_mask;
2212 size_t sd_size;
2213 bool ret;
2214 NTSTATUS nt_status;
2215 SE_PRIV se_rights;
2217 /* find the domain policy handle and get domain SID / access bits in the domain policy. */
2219 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
2220 return NT_STATUS_INVALID_HANDLE;
2222 nt_status = access_check_samr_function(acc_granted,
2223 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
2224 "_samr_OpenUser" );
2226 if ( !NT_STATUS_IS_OK(nt_status) )
2227 return nt_status;
2229 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2230 return NT_STATUS_NO_MEMORY;
2233 /* append the user's RID to it */
2235 if (!sid_append_rid(&sid, r->in.rid))
2236 return NT_STATUS_NO_SUCH_USER;
2238 /* check if access can be granted as requested by client. */
2240 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
2242 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2243 se_map_generic(&des_access, &usr_generic_mapping);
2245 se_priv_copy( &se_rights, &se_machine_account );
2246 se_priv_add( &se_rights, &se_add_users );
2248 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2249 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2250 &acc_granted, "_samr_OpenUser");
2252 if ( !NT_STATUS_IS_OK(nt_status) )
2253 return nt_status;
2255 become_root();
2256 ret=pdb_getsampwsid(sampass, &sid);
2257 unbecome_root();
2259 /* check that the SID exists in our domain. */
2260 if (ret == False) {
2261 return NT_STATUS_NO_SUCH_USER;
2264 TALLOC_FREE(sampass);
2266 /* associate the user's SID and access bits with the new handle. */
2267 if ((info = get_samr_info_by_sid(&sid)) == NULL)
2268 return NT_STATUS_NO_MEMORY;
2269 info->acc_granted = acc_granted;
2271 /* get a (unique) handle. open a policy on it. */
2272 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
2273 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2275 return NT_STATUS_OK;
2278 /*************************************************************************
2279 *************************************************************************/
2281 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2282 DATA_BLOB *blob,
2283 struct lsa_BinaryString **_r)
2285 struct lsa_BinaryString *r;
2287 if (!blob || !_r) {
2288 return NT_STATUS_INVALID_PARAMETER;
2291 r = TALLOC_ZERO_P(mem_ctx, struct lsa_BinaryString);
2292 if (!r) {
2293 return NT_STATUS_NO_MEMORY;
2296 r->array = TALLOC_ZERO_ARRAY(mem_ctx, uint16_t, blob->length/2);
2297 if (!r->array) {
2298 return NT_STATUS_NO_MEMORY;
2300 memcpy(r->array, blob->data, blob->length);
2301 r->size = blob->length;
2302 r->length = blob->length;
2304 if (!r->array) {
2305 return NT_STATUS_NO_MEMORY;
2308 *_r = r;
2310 return NT_STATUS_OK;
2313 static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
2314 struct samr_UserInfo5 *r,
2315 struct samu *pw,
2316 DOM_SID *domain_sid)
2318 const DOM_SID *sid_user, *sid_group;
2319 uint32_t rid, primary_gid;
2320 NTTIME last_logon, last_logoff, last_password_change,
2321 acct_expiry;
2322 const char *account_name, *full_name, *home_directory, *home_drive,
2323 *logon_script, *profile_path, *description,
2324 *workstations, *comment;
2325 struct samr_LogonHours logon_hours;
2327 ZERO_STRUCTP(r);
2329 sid_user = pdb_get_user_sid(pw);
2331 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2332 DEBUG(0, ("get_user_info_5: User %s has SID %s, \nwhich conflicts with "
2333 "the domain sid %s. Failing operation.\n",
2334 pdb_get_username(pw), sid_string_dbg(sid_user),
2335 sid_string_dbg(domain_sid)));
2336 return NT_STATUS_UNSUCCESSFUL;
2339 become_root();
2340 sid_group = pdb_get_group_sid(pw);
2341 unbecome_root();
2343 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2344 DEBUG(0, ("get_user_info_5: User %s has Primary Group SID %s, \n"
2345 "which conflicts with the domain sid %s. Failing operation.\n",
2346 pdb_get_username(pw), sid_string_dbg(sid_group),
2347 sid_string_dbg(domain_sid)));
2348 return NT_STATUS_UNSUCCESSFUL;
2351 unix_to_nt_time(&last_logon, pdb_get_logon_time(pw));
2352 unix_to_nt_time(&last_logoff, pdb_get_logoff_time(pw));
2353 unix_to_nt_time(&acct_expiry, pdb_get_kickoff_time(pw));
2354 unix_to_nt_time(&last_password_change, pdb_get_pass_last_set_time(pw));
2356 account_name = talloc_strdup(mem_ctx, pdb_get_username(pw));
2357 full_name = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2358 home_directory = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2359 home_drive = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2360 logon_script = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2361 profile_path = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2362 description = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2363 workstations = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2364 comment = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2366 logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2368 init_samr_user_info5(r,
2369 account_name,
2370 full_name,
2371 rid,
2372 primary_gid,
2373 home_directory,
2374 home_drive,
2375 logon_script,
2376 profile_path,
2377 description,
2378 workstations,
2379 last_logon,
2380 last_logoff,
2381 logon_hours,
2382 pdb_get_bad_password_count(pw),
2383 pdb_get_logon_count(pw),
2384 last_password_change,
2385 acct_expiry,
2386 pdb_get_acct_ctrl(pw));
2388 return NT_STATUS_OK;
2391 /*************************************************************************
2392 get_user_info_7. Safe. Only gives out account_name.
2393 *************************************************************************/
2395 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2396 struct samr_UserInfo7 *r,
2397 struct samu *smbpass)
2399 const char *account_name = NULL;
2401 ZERO_STRUCTP(r);
2403 account_name = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2404 if (!account_name) {
2405 return NT_STATUS_NO_MEMORY;
2408 init_samr_user_info7(r, account_name);
2410 return NT_STATUS_OK;
2413 /*************************************************************************
2414 get_user_info_9. Only gives out primary group SID.
2415 *************************************************************************/
2417 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2418 struct samr_UserInfo9 *r,
2419 struct samu *smbpass)
2421 ZERO_STRUCTP(r);
2423 init_samr_user_info9(r, pdb_get_group_rid(smbpass));
2425 return NT_STATUS_OK;
2428 /*************************************************************************
2429 get_user_info_16. Safe. Only gives out acb bits.
2430 *************************************************************************/
2432 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2433 struct samr_UserInfo16 *r,
2434 struct samu *smbpass)
2436 ZERO_STRUCTP(r);
2438 init_samr_user_info16(r, pdb_get_acct_ctrl(smbpass));
2440 return NT_STATUS_OK;
2443 /*************************************************************************
2444 get_user_info_18. OK - this is the killer as it gives out password info.
2445 Ensure that this is only allowed on an encrypted connection with a root
2446 user. JRA.
2447 *************************************************************************/
2449 static NTSTATUS get_user_info_18(pipes_struct *p,
2450 TALLOC_CTX *mem_ctx,
2451 struct samr_UserInfo18 *r,
2452 DOM_SID *user_sid)
2454 struct samu *smbpass=NULL;
2455 bool ret;
2457 ZERO_STRUCTP(r);
2459 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
2460 return NT_STATUS_ACCESS_DENIED;
2463 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
2464 return NT_STATUS_ACCESS_DENIED;
2468 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2471 if ( !(smbpass = samu_new( mem_ctx )) ) {
2472 return NT_STATUS_NO_MEMORY;
2475 ret = pdb_getsampwsid(smbpass, user_sid);
2477 if (ret == False) {
2478 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2479 TALLOC_FREE(smbpass);
2480 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2483 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2485 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2486 TALLOC_FREE(smbpass);
2487 return NT_STATUS_ACCOUNT_DISABLED;
2490 init_samr_user_info18(r,
2491 pdb_get_lanman_passwd(smbpass),
2492 pdb_get_nt_passwd(smbpass),
2493 0 /* FIXME */);
2495 TALLOC_FREE(smbpass);
2497 return NT_STATUS_OK;
2500 /*************************************************************************
2501 get_user_info_20
2502 *************************************************************************/
2504 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2505 struct samr_UserInfo20 *r,
2506 struct samu *sampass)
2508 const char *munged_dial = NULL;
2509 DATA_BLOB blob;
2510 NTSTATUS status;
2511 struct lsa_BinaryString *parameters = NULL;
2513 ZERO_STRUCTP(r);
2515 munged_dial = pdb_get_munged_dial(sampass);
2517 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2518 munged_dial, (int)strlen(munged_dial)));
2520 if (munged_dial) {
2521 blob = base64_decode_data_blob(munged_dial);
2522 } else {
2523 blob = data_blob_string_const("");
2526 status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2527 data_blob_free(&blob);
2528 if (!NT_STATUS_IS_OK(status)) {
2529 return status;
2532 init_samr_user_info20(r, parameters);
2534 return NT_STATUS_OK;
2538 /*************************************************************************
2539 get_user_info_21
2540 *************************************************************************/
2542 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2543 struct samr_UserInfo21 *r,
2544 struct samu *pw,
2545 DOM_SID *domain_sid)
2547 NTSTATUS status;
2548 const DOM_SID *sid_user, *sid_group;
2549 uint32_t rid, primary_gid;
2550 NTTIME last_logon, last_logoff, last_password_change,
2551 acct_expiry, allow_password_change, force_password_change;
2552 time_t must_change_time;
2553 uint8_t password_expired;
2554 const char *account_name, *full_name, *home_directory, *home_drive,
2555 *logon_script, *profile_path, *description,
2556 *workstations, *comment;
2557 struct samr_LogonHours logon_hours;
2558 struct lsa_BinaryString *parameters = NULL;
2559 const char *munged_dial = NULL;
2560 DATA_BLOB blob;
2562 ZERO_STRUCTP(r);
2564 sid_user = pdb_get_user_sid(pw);
2566 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2567 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2568 "the domain sid %s. Failing operation.\n",
2569 pdb_get_username(pw), sid_string_dbg(sid_user),
2570 sid_string_dbg(domain_sid)));
2571 return NT_STATUS_UNSUCCESSFUL;
2574 become_root();
2575 sid_group = pdb_get_group_sid(pw);
2576 unbecome_root();
2578 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2579 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2580 "which conflicts with the domain sid %s. Failing operation.\n",
2581 pdb_get_username(pw), sid_string_dbg(sid_group),
2582 sid_string_dbg(domain_sid)));
2583 return NT_STATUS_UNSUCCESSFUL;
2586 unix_to_nt_time(&last_logon, pdb_get_logon_time(pw));
2587 unix_to_nt_time(&last_logoff, pdb_get_logoff_time(pw));
2588 unix_to_nt_time(&acct_expiry, pdb_get_kickoff_time(pw));
2589 unix_to_nt_time(&last_password_change, pdb_get_pass_last_set_time(pw));
2590 unix_to_nt_time(&allow_password_change, pdb_get_pass_can_change_time(pw));
2592 must_change_time = pdb_get_pass_must_change_time(pw);
2593 if (must_change_time == get_time_t_max()) {
2594 unix_to_nt_time_abs(&force_password_change, must_change_time);
2595 } else {
2596 unix_to_nt_time(&force_password_change, must_change_time);
2599 if (pdb_get_pass_must_change_time(pw) == 0) {
2600 password_expired = PASS_MUST_CHANGE_AT_NEXT_LOGON;
2601 } else {
2602 password_expired = 0;
2605 munged_dial = pdb_get_munged_dial(pw);
2606 if (munged_dial) {
2607 blob = base64_decode_data_blob(munged_dial);
2608 } else {
2609 blob = data_blob_string_const("");
2612 status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2613 data_blob_free(&blob);
2614 if (!NT_STATUS_IS_OK(status)) {
2615 return status;
2618 account_name = talloc_strdup(mem_ctx, pdb_get_username(pw));
2619 full_name = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2620 home_directory = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2621 home_drive = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2622 logon_script = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2623 profile_path = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2624 description = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2625 workstations = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2626 comment = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2628 logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2629 #if 0
2632 Look at a user on a real NT4 PDC with usrmgr, press
2633 'ok'. Then you will see that fields_present is set to
2634 0x08f827fa. Look at the user immediately after that again,
2635 and you will see that 0x00fffff is returned. This solves
2636 the problem that you get access denied after having looked
2637 at the user.
2638 -- Volker
2641 #endif
2643 init_samr_user_info21(r,
2644 last_logon,
2645 last_logoff,
2646 last_password_change,
2647 acct_expiry,
2648 allow_password_change,
2649 force_password_change,
2650 account_name,
2651 full_name,
2652 home_directory,
2653 home_drive,
2654 logon_script,
2655 profile_path,
2656 description,
2657 workstations,
2658 comment,
2659 parameters,
2660 rid,
2661 primary_gid,
2662 pdb_get_acct_ctrl(pw),
2663 pdb_build_fields_present(pw),
2664 logon_hours,
2665 pdb_get_bad_password_count(pw),
2666 pdb_get_logon_count(pw),
2667 0, /* country_code */
2668 0, /* code_page */
2669 0, /* lm_password_set */
2670 0, /* nt_password_set */
2671 password_expired);
2673 return NT_STATUS_OK;
2676 /*******************************************************************
2677 _samr_QueryUserInfo
2678 ********************************************************************/
2680 NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
2681 struct samr_QueryUserInfo *r)
2683 NTSTATUS status;
2684 union samr_UserInfo *user_info = NULL;
2685 struct samr_info *info = NULL;
2686 DOM_SID domain_sid;
2687 uint32 rid;
2688 bool ret = false;
2689 struct samu *pwd = NULL;
2691 /* search for the handle */
2692 if (!find_policy_by_hnd(p, r->in.user_handle, (void **)(void *)&info))
2693 return NT_STATUS_INVALID_HANDLE;
2695 status = access_check_samr_function(info->acc_granted,
2696 SAMR_USER_ACCESS_GET_ATTRIBUTES,
2697 "_samr_QueryUserInfo");
2698 if (!NT_STATUS_IS_OK(status)) {
2699 return status;
2702 domain_sid = info->sid;
2704 sid_split_rid(&domain_sid, &rid);
2706 if (!sid_check_is_in_our_domain(&info->sid))
2707 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2709 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
2710 sid_string_dbg(&info->sid)));
2712 user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
2713 if (!user_info) {
2714 return NT_STATUS_NO_MEMORY;
2717 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
2719 if (!(pwd = samu_new(p->mem_ctx))) {
2720 return NT_STATUS_NO_MEMORY;
2723 become_root();
2724 ret = pdb_getsampwsid(pwd, &info->sid);
2725 unbecome_root();
2727 if (ret == false) {
2728 DEBUG(4,("User %s not found\n", sid_string_dbg(&info->sid)));
2729 TALLOC_FREE(pwd);
2730 return NT_STATUS_NO_SUCH_USER;
2733 DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
2735 samr_clear_sam_passwd(pwd);
2737 switch (r->in.level) {
2738 case 5:
2739 status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
2740 break;
2741 case 7:
2742 status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
2743 break;
2744 case 9:
2745 status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
2746 break;
2747 case 16:
2748 status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
2749 break;
2750 case 18:
2751 /* level 18 is special */
2752 status = get_user_info_18(p, p->mem_ctx, &user_info->info18, &info->sid);
2753 break;
2754 case 20:
2755 status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
2756 break;
2757 case 21:
2758 status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid);
2759 break;
2760 default:
2761 status = NT_STATUS_INVALID_INFO_CLASS;
2762 break;
2765 TALLOC_FREE(pwd);
2767 *r->out.info = user_info;
2769 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
2771 return status;
2774 /****************************************************************
2775 ****************************************************************/
2777 NTSTATUS _samr_QueryUserInfo2(pipes_struct *p,
2778 struct samr_QueryUserInfo2 *r)
2780 struct samr_QueryUserInfo u;
2782 u.in.user_handle = r->in.user_handle;
2783 u.in.level = r->in.level;
2784 u.out.info = r->out.info;
2786 return _samr_QueryUserInfo(p, &u);
2789 /*******************************************************************
2790 _samr_GetGroupsForUser
2791 ********************************************************************/
2793 NTSTATUS _samr_GetGroupsForUser(pipes_struct *p,
2794 struct samr_GetGroupsForUser *r)
2796 struct samu *sam_pass=NULL;
2797 DOM_SID sid;
2798 DOM_SID *sids;
2799 struct samr_RidWithAttribute dom_gid;
2800 struct samr_RidWithAttribute *gids = NULL;
2801 uint32 primary_group_rid;
2802 size_t num_groups = 0;
2803 gid_t *unix_gids;
2804 size_t i, num_gids;
2805 uint32 acc_granted;
2806 bool ret;
2807 NTSTATUS result;
2808 bool success = False;
2810 struct samr_RidWithAttributeArray *rids = NULL;
2813 * from the SID in the request:
2814 * we should send back the list of DOMAIN GROUPS
2815 * the user is a member of
2817 * and only the DOMAIN GROUPS
2818 * no ALIASES !!! neither aliases of the domain
2819 * nor aliases of the builtin SID
2821 * JFM, 12/2/2001
2824 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2826 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
2827 if (!rids) {
2828 return NT_STATUS_NO_MEMORY;
2831 /* find the policy handle. open a policy on it. */
2832 if (!get_lsa_policy_samr_sid(p, r->in.user_handle, &sid, &acc_granted, NULL))
2833 return NT_STATUS_INVALID_HANDLE;
2835 result = access_check_samr_function(acc_granted,
2836 SAMR_USER_ACCESS_GET_GROUPS,
2837 "_samr_GetGroupsForUser");
2838 if (!NT_STATUS_IS_OK(result)) {
2839 return result;
2842 if (!sid_check_is_in_our_domain(&sid))
2843 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2845 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2846 return NT_STATUS_NO_MEMORY;
2849 become_root();
2850 ret = pdb_getsampwsid(sam_pass, &sid);
2851 unbecome_root();
2853 if (!ret) {
2854 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2855 sid_string_dbg(&sid)));
2856 return NT_STATUS_NO_SUCH_USER;
2859 sids = NULL;
2861 /* make both calls inside the root block */
2862 become_root();
2863 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2864 &sids, &unix_gids, &num_groups);
2865 if ( NT_STATUS_IS_OK(result) ) {
2866 success = sid_peek_check_rid(get_global_sam_sid(),
2867 pdb_get_group_sid(sam_pass),
2868 &primary_group_rid);
2870 unbecome_root();
2872 if (!NT_STATUS_IS_OK(result)) {
2873 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2874 sid_string_dbg(&sid)));
2875 return result;
2878 if ( !success ) {
2879 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2880 sid_string_dbg(pdb_get_group_sid(sam_pass)),
2881 pdb_get_username(sam_pass)));
2882 TALLOC_FREE(sam_pass);
2883 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2886 gids = NULL;
2887 num_gids = 0;
2889 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2890 SE_GROUP_ENABLED);
2891 dom_gid.rid = primary_group_rid;
2892 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2894 for (i=0; i<num_groups; i++) {
2896 if (!sid_peek_check_rid(get_global_sam_sid(),
2897 &(sids[i]), &dom_gid.rid)) {
2898 DEBUG(10, ("Found sid %s not in our domain\n",
2899 sid_string_dbg(&sids[i])));
2900 continue;
2903 if (dom_gid.rid == primary_group_rid) {
2904 /* We added the primary group directly from the
2905 * sam_account. The other SIDs are unique from
2906 * enum_group_memberships */
2907 continue;
2910 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2913 rids->count = num_gids;
2914 rids->rids = gids;
2916 *r->out.rids = rids;
2918 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2920 return result;
2923 /*******************************************************************
2924 _samr_QueryDomainInfo
2925 ********************************************************************/
2927 NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
2928 struct samr_QueryDomainInfo *r)
2930 NTSTATUS status = NT_STATUS_OK;
2931 struct samr_info *info = NULL;
2932 union samr_DomainInfo *dom_info;
2933 uint32 min_pass_len,pass_hist,password_properties;
2934 time_t u_expire, u_min_age;
2935 NTTIME nt_expire, nt_min_age;
2937 time_t u_lock_duration, u_reset_time;
2938 NTTIME nt_lock_duration, nt_reset_time;
2939 uint32 lockout;
2940 time_t u_logout;
2941 NTTIME nt_logout;
2943 uint32 account_policy_temp;
2945 time_t seq_num;
2946 uint32 server_role;
2948 uint32 num_users=0, num_groups=0, num_aliases=0;
2950 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
2952 dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
2953 if (!dom_info) {
2954 return NT_STATUS_NO_MEMORY;
2957 /* find the policy handle. open a policy on it. */
2958 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info)) {
2959 return NT_STATUS_INVALID_HANDLE;
2962 status = access_check_samr_function(info->acc_granted,
2963 SAMR_ACCESS_OPEN_DOMAIN,
2964 "_samr_QueryDomainInfo" );
2966 if ( !NT_STATUS_IS_OK(status) )
2967 return status;
2969 switch (r->in.level) {
2970 case 0x01:
2972 become_root();
2974 /* AS ROOT !!! */
2976 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2977 min_pass_len = account_policy_temp;
2979 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2980 pass_hist = account_policy_temp;
2982 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2983 password_properties = account_policy_temp;
2985 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2986 u_expire = account_policy_temp;
2988 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2989 u_min_age = account_policy_temp;
2991 /* !AS ROOT */
2993 unbecome_root();
2995 unix_to_nt_time_abs(&nt_expire, u_expire);
2996 unix_to_nt_time_abs(&nt_min_age, u_min_age);
2998 if (lp_check_password_script() && *lp_check_password_script()) {
2999 password_properties |= DOMAIN_PASSWORD_COMPLEX;
3002 init_samr_DomInfo1(&dom_info->info1,
3003 (uint16)min_pass_len,
3004 (uint16)pass_hist,
3005 password_properties,
3006 nt_expire,
3007 nt_min_age);
3008 break;
3009 case 0x02:
3011 become_root();
3013 /* AS ROOT !!! */
3015 num_users = count_sam_users(info->disp_info, ACB_NORMAL);
3016 num_groups = count_sam_groups(info->disp_info);
3017 num_aliases = count_sam_aliases(info->disp_info);
3019 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
3020 u_logout = account_policy_temp;
3022 unix_to_nt_time_abs(&nt_logout, u_logout);
3024 if (!pdb_get_seq_num(&seq_num))
3025 seq_num = time(NULL);
3027 /* !AS ROOT */
3029 unbecome_root();
3031 server_role = ROLE_DOMAIN_PDC;
3032 if (lp_server_role() == ROLE_DOMAIN_BDC)
3033 server_role = ROLE_DOMAIN_BDC;
3035 init_samr_DomInfo2(&dom_info->info2,
3036 nt_logout,
3037 lp_serverstring(),
3038 lp_workgroup(),
3039 global_myname(),
3040 seq_num,
3042 server_role,
3044 num_users,
3045 num_groups,
3046 num_aliases);
3047 break;
3048 case 0x03:
3050 become_root();
3052 /* AS ROOT !!! */
3055 uint32 ul;
3056 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
3057 u_logout = (time_t)ul;
3060 /* !AS ROOT */
3062 unbecome_root();
3064 unix_to_nt_time_abs(&nt_logout, u_logout);
3066 init_samr_DomInfo3(&dom_info->info3,
3067 nt_logout);
3069 break;
3070 case 0x04:
3071 init_samr_DomInfo4(&dom_info->info4,
3072 lp_serverstring());
3073 break;
3074 case 0x05:
3075 init_samr_DomInfo5(&dom_info->info5,
3076 get_global_sam_name());
3077 break;
3078 case 0x06:
3079 /* NT returns its own name when a PDC. win2k and later
3080 * only the name of the PDC if itself is a BDC (samba4
3081 * idl) */
3082 init_samr_DomInfo6(&dom_info->info6,
3083 global_myname());
3084 break;
3085 case 0x07:
3086 server_role = ROLE_DOMAIN_PDC;
3087 if (lp_server_role() == ROLE_DOMAIN_BDC)
3088 server_role = ROLE_DOMAIN_BDC;
3090 init_samr_DomInfo7(&dom_info->info7,
3091 server_role);
3092 break;
3093 case 0x08:
3095 become_root();
3097 /* AS ROOT !!! */
3099 if (!pdb_get_seq_num(&seq_num)) {
3100 seq_num = time(NULL);
3103 /* !AS ROOT */
3105 unbecome_root();
3107 init_samr_DomInfo8(&dom_info->info8,
3108 seq_num,
3110 break;
3111 case 0x0c:
3113 become_root();
3115 /* AS ROOT !!! */
3117 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3118 u_lock_duration = account_policy_temp;
3119 if (u_lock_duration != -1) {
3120 u_lock_duration *= 60;
3123 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
3124 u_reset_time = account_policy_temp * 60;
3126 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3127 lockout = account_policy_temp;
3129 /* !AS ROOT */
3131 unbecome_root();
3133 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
3134 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
3136 init_samr_DomInfo12(&dom_info->info12,
3137 nt_lock_duration,
3138 nt_reset_time,
3139 (uint16)lockout);
3140 break;
3141 default:
3142 return NT_STATUS_INVALID_INFO_CLASS;
3145 *r->out.info = dom_info;
3147 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3149 return status;
3152 /* W2k3 seems to use the same check for all 3 objects that can be created via
3153 * SAMR, if you try to create for example "Dialup" as an alias it says
3154 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3155 * database. */
3157 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3159 enum lsa_SidType type;
3160 bool result;
3162 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3164 become_root();
3165 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3166 * whether the name already exists */
3167 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3168 NULL, NULL, NULL, &type);
3169 unbecome_root();
3171 if (!result) {
3172 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3173 return NT_STATUS_OK;
3176 DEBUG(5, ("trying to create %s, exists as %s\n",
3177 new_name, sid_type_lookup(type)));
3179 if (type == SID_NAME_DOM_GRP) {
3180 return NT_STATUS_GROUP_EXISTS;
3182 if (type == SID_NAME_ALIAS) {
3183 return NT_STATUS_ALIAS_EXISTS;
3186 /* Yes, the default is NT_STATUS_USER_EXISTS */
3187 return NT_STATUS_USER_EXISTS;
3190 /*******************************************************************
3191 _samr_CreateUser2
3192 ********************************************************************/
3194 NTSTATUS _samr_CreateUser2(pipes_struct *p,
3195 struct samr_CreateUser2 *r)
3197 const char *account = NULL;
3198 DOM_SID sid;
3199 POLICY_HND dom_pol = *r->in.domain_handle;
3200 uint32_t acb_info = r->in.acct_flags;
3201 POLICY_HND *user_pol = r->out.user_handle;
3202 struct samr_info *info = NULL;
3203 NTSTATUS nt_status;
3204 uint32 acc_granted;
3205 SEC_DESC *psd;
3206 size_t sd_size;
3207 /* check this, when giving away 'add computer to domain' privs */
3208 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3209 bool can_add_account = False;
3210 SE_PRIV se_rights;
3211 DISP_INFO *disp_info = NULL;
3213 /* Get the domain SID stored in the domain policy */
3214 if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted,
3215 &disp_info))
3216 return NT_STATUS_INVALID_HANDLE;
3218 if (disp_info->builtin_domain) {
3219 DEBUG(5,("_samr_CreateUser2: Refusing user create in BUILTIN\n"));
3220 return NT_STATUS_ACCESS_DENIED;
3223 nt_status = access_check_samr_function(acc_granted,
3224 SAMR_DOMAIN_ACCESS_CREATE_USER,
3225 "_samr_CreateUser2");
3226 if (!NT_STATUS_IS_OK(nt_status)) {
3227 return nt_status;
3230 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3231 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3232 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3233 this parameter is not an account type */
3234 return NT_STATUS_INVALID_PARAMETER;
3237 account = r->in.account_name->string;
3238 if (account == NULL) {
3239 return NT_STATUS_NO_MEMORY;
3242 nt_status = can_create(p->mem_ctx, account);
3243 if (!NT_STATUS_IS_OK(nt_status)) {
3244 return nt_status;
3247 /* determine which user right we need to check based on the acb_info */
3249 if ( acb_info & ACB_WSTRUST )
3251 se_priv_copy( &se_rights, &se_machine_account );
3252 can_add_account = user_has_privileges(
3253 p->pipe_user.nt_user_token, &se_rights );
3255 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3256 account for domain trusts and changes the ACB flags later */
3257 else if ( acb_info & ACB_NORMAL &&
3258 (account[strlen(account)-1] != '$') )
3260 se_priv_copy( &se_rights, &se_add_users );
3261 can_add_account = user_has_privileges(
3262 p->pipe_user.nt_user_token, &se_rights );
3264 else /* implicit assumption of a BDC or domain trust account here
3265 * (we already check the flags earlier) */
3267 if ( lp_enable_privileges() ) {
3268 /* only Domain Admins can add a BDC or domain trust */
3269 se_priv_copy( &se_rights, &se_priv_none );
3270 can_add_account = nt_token_check_domain_rid(
3271 p->pipe_user.nt_user_token,
3272 DOMAIN_GROUP_RID_ADMINS );
3276 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3277 uidtoname(p->pipe_user.ut.uid),
3278 can_add_account ? "True":"False" ));
3280 /********** BEGIN Admin BLOCK **********/
3282 if ( can_add_account )
3283 become_root();
3285 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3286 r->out.rid);
3288 if ( can_add_account )
3289 unbecome_root();
3291 /********** END Admin BLOCK **********/
3293 /* now check for failure */
3295 if ( !NT_STATUS_IS_OK(nt_status) )
3296 return nt_status;
3298 /* Get the user's SID */
3300 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3302 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3304 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3305 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3306 se_map_generic(&des_access, &usr_generic_mapping);
3308 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3309 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
3310 &acc_granted, "_samr_CreateUser2");
3312 if ( !NT_STATUS_IS_OK(nt_status) ) {
3313 return nt_status;
3316 /* associate the user's SID with the new handle. */
3317 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
3318 return NT_STATUS_NO_MEMORY;
3321 ZERO_STRUCTP(info);
3322 info->sid = sid;
3323 info->acc_granted = acc_granted;
3325 /* get a (unique) handle. open a policy on it. */
3326 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
3327 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3330 /* After a "set" ensure we have no cached display info. */
3331 force_flush_samr_cache(info->disp_info);
3333 *r->out.access_granted = acc_granted;
3335 return NT_STATUS_OK;
3338 /****************************************************************
3339 ****************************************************************/
3341 NTSTATUS _samr_CreateUser(pipes_struct *p,
3342 struct samr_CreateUser *r)
3344 struct samr_CreateUser2 c;
3345 uint32_t access_granted;
3347 c.in.domain_handle = r->in.domain_handle;
3348 c.in.account_name = r->in.account_name;
3349 c.in.acct_flags = ACB_NORMAL;
3350 c.in.access_mask = r->in.access_mask;
3351 c.out.user_handle = r->out.user_handle;
3352 c.out.access_granted = &access_granted;
3353 c.out.rid = r->out.rid;
3355 return _samr_CreateUser2(p, &c);
3358 /*******************************************************************
3359 _samr_Connect
3360 ********************************************************************/
3362 NTSTATUS _samr_Connect(pipes_struct *p,
3363 struct samr_Connect *r)
3365 struct samr_info *info = NULL;
3366 uint32 des_access = r->in.access_mask;
3368 /* Access check */
3370 if (!pipe_access_check(p)) {
3371 DEBUG(3, ("access denied to _samr_Connect\n"));
3372 return NT_STATUS_ACCESS_DENIED;
3375 /* set up the SAMR connect_anon response */
3377 /* associate the user's SID with the new handle. */
3378 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3379 return NT_STATUS_NO_MEMORY;
3381 /* don't give away the farm but this is probably ok. The SAMR_ACCESS_ENUM_DOMAINS
3382 was observed from a win98 client trying to enumerate users (when configured
3383 user level access control on shares) --jerry */
3385 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3387 se_map_generic( &des_access, &sam_generic_mapping );
3388 info->acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS|SAMR_ACCESS_OPEN_DOMAIN);
3390 /* get a (unique) handle. open a policy on it. */
3391 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3392 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3394 return NT_STATUS_OK;
3397 /*******************************************************************
3398 _samr_Connect2
3399 ********************************************************************/
3401 NTSTATUS _samr_Connect2(pipes_struct *p,
3402 struct samr_Connect2 *r)
3404 struct samr_info *info = NULL;
3405 SEC_DESC *psd = NULL;
3406 uint32 acc_granted;
3407 uint32 des_access = r->in.access_mask;
3408 NTSTATUS nt_status;
3409 size_t sd_size;
3412 DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
3414 /* Access check */
3416 if (!pipe_access_check(p)) {
3417 DEBUG(3, ("access denied to _samr_Connect2\n"));
3418 return NT_STATUS_ACCESS_DENIED;
3421 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3423 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3424 se_map_generic(&des_access, &sam_generic_mapping);
3426 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3427 NULL, 0, des_access, &acc_granted, "_samr_Connect2");
3429 if ( !NT_STATUS_IS_OK(nt_status) )
3430 return nt_status;
3432 /* associate the user's SID and access granted with the new handle. */
3433 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3434 return NT_STATUS_NO_MEMORY;
3436 info->acc_granted = acc_granted;
3437 info->status = r->in.access_mask; /* this looks so wrong... - gd */
3439 /* get a (unique) handle. open a policy on it. */
3440 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3441 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3443 DEBUG(5,("_samr_Connect2: %d\n", __LINE__));
3445 return nt_status;
3448 /*******************************************************************
3449 _samr_Connect4
3450 ********************************************************************/
3452 NTSTATUS _samr_Connect4(pipes_struct *p,
3453 struct samr_Connect4 *r)
3455 struct samr_info *info = NULL;
3456 SEC_DESC *psd = NULL;
3457 uint32 acc_granted;
3458 uint32 des_access = r->in.access_mask;
3459 NTSTATUS nt_status;
3460 size_t sd_size;
3463 DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
3465 /* Access check */
3467 if (!pipe_access_check(p)) {
3468 DEBUG(3, ("access denied to samr_Connect4\n"));
3469 return NT_STATUS_ACCESS_DENIED;
3472 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3474 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3475 se_map_generic(&des_access, &sam_generic_mapping);
3477 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3478 NULL, 0, des_access, &acc_granted, "_samr_Connect4");
3480 if ( !NT_STATUS_IS_OK(nt_status) )
3481 return nt_status;
3483 /* associate the user's SID and access granted with the new handle. */
3484 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3485 return NT_STATUS_NO_MEMORY;
3487 info->acc_granted = acc_granted;
3488 info->status = r->in.access_mask; /* ??? */
3490 /* get a (unique) handle. open a policy on it. */
3491 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3492 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3494 DEBUG(5,("_samr_Connect4: %d\n", __LINE__));
3496 return NT_STATUS_OK;
3499 /*******************************************************************
3500 _samr_Connect5
3501 ********************************************************************/
3503 NTSTATUS _samr_Connect5(pipes_struct *p,
3504 struct samr_Connect5 *r)
3506 struct samr_info *info = NULL;
3507 SEC_DESC *psd = NULL;
3508 uint32 acc_granted;
3509 uint32 des_access = r->in.access_mask;
3510 NTSTATUS nt_status;
3511 size_t sd_size;
3512 struct samr_ConnectInfo1 info1;
3514 DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
3516 /* Access check */
3518 if (!pipe_access_check(p)) {
3519 DEBUG(3, ("access denied to samr_Connect5\n"));
3520 return NT_STATUS_ACCESS_DENIED;
3523 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3525 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3526 se_map_generic(&des_access, &sam_generic_mapping);
3528 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3529 NULL, 0, des_access, &acc_granted, "_samr_Connect5");
3531 if ( !NT_STATUS_IS_OK(nt_status) )
3532 return nt_status;
3534 /* associate the user's SID and access granted with the new handle. */
3535 if ((info = get_samr_info_by_sid(NULL)) == NULL)
3536 return NT_STATUS_NO_MEMORY;
3538 info->acc_granted = acc_granted;
3539 info->status = r->in.access_mask; /* ??? */
3541 /* get a (unique) handle. open a policy on it. */
3542 if (!create_policy_hnd(p, r->out.connect_handle, free_samr_info, (void *)info))
3543 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3545 DEBUG(5,("_samr_Connect5: %d\n", __LINE__));
3547 info1.client_version = SAMR_CONNECT_AFTER_W2K;
3548 info1.unknown2 = 0;
3550 *r->out.level_out = 1;
3551 r->out.info_out->info1 = info1;
3553 return NT_STATUS_OK;
3556 /**********************************************************************
3557 _samr_LookupDomain
3558 **********************************************************************/
3560 NTSTATUS _samr_LookupDomain(pipes_struct *p,
3561 struct samr_LookupDomain *r)
3563 NTSTATUS status = NT_STATUS_OK;
3564 struct samr_info *info;
3565 const char *domain_name;
3566 DOM_SID *sid = NULL;
3568 if (!find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info))
3569 return NT_STATUS_INVALID_HANDLE;
3571 /* win9x user manager likes to use SAMR_ACCESS_ENUM_DOMAINS here.
3572 Reverted that change so we will work with RAS servers again */
3574 status = access_check_samr_function(info->acc_granted,
3575 SAMR_ACCESS_OPEN_DOMAIN,
3576 "_samr_LookupDomain");
3577 if (!NT_STATUS_IS_OK(status)) {
3578 return status;
3581 domain_name = r->in.domain_name->string;
3582 if (!domain_name) {
3583 return NT_STATUS_INVALID_PARAMETER;
3586 sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
3587 if (!sid) {
3588 return NT_STATUS_NO_MEMORY;
3591 if (strequal(domain_name, builtin_domain_name())) {
3592 sid_copy(sid, &global_sid_Builtin);
3593 } else {
3594 if (!secrets_fetch_domain_sid(domain_name, sid)) {
3595 status = NT_STATUS_NO_SUCH_DOMAIN;
3599 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
3600 sid_string_dbg(sid)));
3602 *r->out.sid = sid;
3604 return status;
3607 /**********************************************************************
3608 _samr_EnumDomains
3609 **********************************************************************/
3611 NTSTATUS _samr_EnumDomains(pipes_struct *p,
3612 struct samr_EnumDomains *r)
3614 NTSTATUS status;
3615 struct samr_info *info;
3616 uint32_t num_entries = 2;
3617 struct samr_SamEntry *entry_array = NULL;
3618 struct samr_SamArray *sam;
3620 if (!find_policy_by_hnd(p, r->in.connect_handle, (void**)(void *)&info))
3621 return NT_STATUS_INVALID_HANDLE;
3623 status = access_check_samr_function(info->acc_granted,
3624 SAMR_ACCESS_ENUM_DOMAINS,
3625 "_samr_EnumDomains");
3626 if (!NT_STATUS_IS_OK(status)) {
3627 return status;
3630 sam = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
3631 if (!sam) {
3632 return NT_STATUS_NO_MEMORY;
3635 entry_array = TALLOC_ZERO_ARRAY(p->mem_ctx,
3636 struct samr_SamEntry,
3637 num_entries);
3638 if (!entry_array) {
3639 return NT_STATUS_NO_MEMORY;
3642 entry_array[0].idx = 0;
3643 init_lsa_String(&entry_array[0].name, get_global_sam_name());
3645 entry_array[1].idx = 1;
3646 init_lsa_String(&entry_array[1].name, "Builtin");
3648 sam->count = num_entries;
3649 sam->entries = entry_array;
3651 *r->out.sam = sam;
3652 *r->out.num_entries = num_entries;
3654 return status;
3657 /*******************************************************************
3658 _samr_OpenAlias
3659 ********************************************************************/
3661 NTSTATUS _samr_OpenAlias(pipes_struct *p,
3662 struct samr_OpenAlias *r)
3664 DOM_SID sid;
3665 POLICY_HND domain_pol = *r->in.domain_handle;
3666 uint32 alias_rid = r->in.rid;
3667 POLICY_HND *alias_pol = r->out.alias_handle;
3668 struct samr_info *info = NULL;
3669 SEC_DESC *psd = NULL;
3670 uint32 acc_granted;
3671 uint32 des_access = r->in.access_mask;
3672 size_t sd_size;
3673 NTSTATUS status;
3674 SE_PRIV se_rights;
3676 /* find the domain policy and get the SID / access bits stored in the domain policy */
3678 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
3679 return NT_STATUS_INVALID_HANDLE;
3681 status = access_check_samr_function(acc_granted,
3682 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
3683 "_samr_OpenAlias");
3685 if ( !NT_STATUS_IS_OK(status) )
3686 return status;
3688 /* append the alias' RID to it */
3690 if (!sid_append_rid(&sid, alias_rid))
3691 return NT_STATUS_NO_SUCH_ALIAS;
3693 /*check if access can be granted as requested by client. */
3695 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
3697 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
3698 se_map_generic(&des_access,&ali_generic_mapping);
3700 se_priv_copy( &se_rights, &se_add_users );
3703 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
3704 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
3705 &acc_granted, "_samr_OpenAlias");
3707 if ( !NT_STATUS_IS_OK(status) )
3708 return status;
3711 /* Check we actually have the requested alias */
3712 enum lsa_SidType type;
3713 bool result;
3714 gid_t gid;
3716 become_root();
3717 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
3718 unbecome_root();
3720 if (!result || (type != SID_NAME_ALIAS)) {
3721 return NT_STATUS_NO_SUCH_ALIAS;
3724 /* make sure there is a mapping */
3726 if ( !sid_to_gid( &sid, &gid ) ) {
3727 return NT_STATUS_NO_SUCH_ALIAS;
3732 /* associate the alias SID with the new handle. */
3733 if ((info = get_samr_info_by_sid(&sid)) == NULL)
3734 return NT_STATUS_NO_MEMORY;
3736 info->acc_granted = acc_granted;
3738 /* get a (unique) handle. open a policy on it. */
3739 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
3740 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3742 return NT_STATUS_OK;
3745 /*******************************************************************
3746 set_user_info_7
3747 ********************************************************************/
3749 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
3750 struct samr_UserInfo7 *id7,
3751 struct samu *pwd)
3753 NTSTATUS rc;
3755 if (id7 == NULL) {
3756 DEBUG(5, ("set_user_info_7: NULL id7\n"));
3757 return NT_STATUS_ACCESS_DENIED;
3760 if (!id7->account_name.string) {
3761 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
3762 return NT_STATUS_ACCESS_DENIED;
3765 /* check to see if the new username already exists. Note: we can't
3766 reliably lock all backends, so there is potentially the
3767 possibility that a user can be created in between this check and
3768 the rename. The rename should fail, but may not get the
3769 exact same failure status code. I think this is small enough
3770 of a window for this type of operation and the results are
3771 simply that the rename fails with a slightly different status
3772 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3774 rc = can_create(mem_ctx, id7->account_name.string);
3775 if (!NT_STATUS_IS_OK(rc)) {
3776 return rc;
3779 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
3781 return rc;
3784 /*******************************************************************
3785 set_user_info_16
3786 ********************************************************************/
3788 static bool set_user_info_16(struct samr_UserInfo16 *id16,
3789 struct samu *pwd)
3791 if (id16 == NULL) {
3792 DEBUG(5, ("set_user_info_16: NULL id16\n"));
3793 return False;
3796 /* FIX ME: check if the value is really changed --metze */
3797 if (!pdb_set_acct_ctrl(pwd, id16->acct_flags, PDB_CHANGED)) {
3798 return False;
3801 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3802 return False;
3805 return True;
3808 /*******************************************************************
3809 set_user_info_18
3810 ********************************************************************/
3812 static NTSTATUS set_user_info_18(struct samr_UserInfo18 *id18,
3813 TALLOC_CTX *mem_ctx,
3814 DATA_BLOB *session_key,
3815 struct samu *pwd)
3817 if (id18 == NULL) {
3818 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3819 return NT_STATUS_INVALID_PARAMETER;
3822 if (id18->nt_pwd_active || id18->lm_pwd_active) {
3823 if (!session_key->length) {
3824 return NT_STATUS_NO_USER_SESSION_KEY;
3828 if (id18->nt_pwd_active) {
3830 DATA_BLOB in, out;
3832 in = data_blob_const(id18->nt_pwd.hash, 16);
3833 out = data_blob_talloc_zero(mem_ctx, 16);
3835 sess_crypt_blob(&out, &in, session_key, false);
3837 if (!pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED)) {
3838 return NT_STATUS_ACCESS_DENIED;
3841 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
3844 if (id18->lm_pwd_active) {
3846 DATA_BLOB in, out;
3848 in = data_blob_const(id18->lm_pwd.hash, 16);
3849 out = data_blob_talloc_zero(mem_ctx, 16);
3851 sess_crypt_blob(&out, &in, session_key, false);
3853 if (!pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED)) {
3854 return NT_STATUS_ACCESS_DENIED;
3857 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
3860 if (id18->password_expired) {
3861 pdb_set_pass_last_set_time(pwd, 0, PDB_CHANGED);
3862 } else {
3863 /* FIXME */
3864 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
3867 return pdb_update_sam_account(pwd);
3870 /*******************************************************************
3871 set_user_info_20
3872 ********************************************************************/
3874 static bool set_user_info_20(struct samr_UserInfo20 *id20,
3875 struct samu *pwd)
3877 if (id20 == NULL) {
3878 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3879 return False;
3882 copy_id20_to_sam_passwd(pwd, id20);
3884 /* write the change out */
3885 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3886 return False;
3889 return True;
3892 /*******************************************************************
3893 set_user_info_21
3894 ********************************************************************/
3896 static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx,
3897 struct samr_UserInfo21 *id21,
3898 struct samu *pwd)
3900 NTSTATUS status;
3902 if (id21 == NULL) {
3903 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3904 return NT_STATUS_INVALID_PARAMETER;
3907 if (id21->fields_present == 0) {
3908 return NT_STATUS_INVALID_PARAMETER;
3911 if (id21->fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
3912 return NT_STATUS_ACCESS_DENIED;
3915 /* we need to separately check for an account rename first */
3917 if (id21->account_name.string &&
3918 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
3921 /* check to see if the new username already exists. Note: we can't
3922 reliably lock all backends, so there is potentially the
3923 possibility that a user can be created in between this check and
3924 the rename. The rename should fail, but may not get the
3925 exact same failure status code. I think this is small enough
3926 of a window for this type of operation and the results are
3927 simply that the rename fails with a slightly different status
3928 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3930 status = can_create(mem_ctx, id21->account_name.string);
3931 if (!NT_STATUS_IS_OK(status)) {
3932 return status;
3935 status = pdb_rename_sam_account(pwd, id21->account_name.string);
3937 if (!NT_STATUS_IS_OK(status)) {
3938 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
3939 nt_errstr(status)));
3940 return status;
3943 /* set the new username so that later
3944 functions can work on the new account */
3945 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
3948 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
3951 * The funny part about the previous two calls is
3952 * that pwd still has the password hashes from the
3953 * passdb entry. These have not been updated from
3954 * id21. I don't know if they need to be set. --jerry
3957 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3958 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3959 if ( !NT_STATUS_IS_OK(status) ) {
3960 return status;
3964 /* Don't worry about writing out the user account since the
3965 primary group SID is generated solely from the user's Unix
3966 primary group. */
3968 /* write the change out */
3969 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3970 return status;
3973 return NT_STATUS_OK;
3976 /*******************************************************************
3977 set_user_info_23
3978 ********************************************************************/
3980 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
3981 struct samr_UserInfo23 *id23,
3982 struct samu *pwd)
3984 char *plaintext_buf = NULL;
3985 uint32 len = 0;
3986 uint32_t acct_ctrl;
3987 NTSTATUS status;
3989 if (id23 == NULL) {
3990 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3991 return NT_STATUS_INVALID_PARAMETER;
3994 if (id23->info.fields_present == 0) {
3995 return NT_STATUS_INVALID_PARAMETER;
3998 if (id23->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
3999 return NT_STATUS_ACCESS_DENIED;
4002 if ((id23->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4003 (id23->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4005 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
4006 pdb_get_username(pwd)));
4008 if (!decode_pw_buffer(mem_ctx,
4009 id23->password.data,
4010 &plaintext_buf,
4011 &len,
4012 STR_UNICODE)) {
4013 return NT_STATUS_WRONG_PASSWORD;
4016 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4017 return NT_STATUS_ACCESS_DENIED;
4021 copy_id23_to_sam_passwd(pwd, id23);
4023 acct_ctrl = pdb_get_acct_ctrl(pwd);
4025 /* if it's a trust account, don't update /etc/passwd */
4026 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4027 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4028 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4029 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
4030 } else if (plaintext_buf) {
4031 /* update the UNIX password */
4032 if (lp_unix_password_sync() ) {
4033 struct passwd *passwd;
4034 if (pdb_get_username(pwd) == NULL) {
4035 DEBUG(1, ("chgpasswd: User without name???\n"));
4036 return NT_STATUS_ACCESS_DENIED;
4039 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4040 if (passwd == NULL) {
4041 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4044 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
4045 return NT_STATUS_ACCESS_DENIED;
4047 TALLOC_FREE(passwd);
4051 if (plaintext_buf) {
4052 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4055 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
4056 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
4057 pwd)))) {
4058 return status;
4061 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4062 return status;
4065 return NT_STATUS_OK;
4068 /*******************************************************************
4069 set_user_info_pw
4070 ********************************************************************/
4072 static bool set_user_info_pw(uint8 *pass, struct samu *pwd,
4073 int level)
4075 uint32 len = 0;
4076 char *plaintext_buf = NULL;
4077 uint32 acct_ctrl;
4078 time_t last_set_time;
4079 enum pdb_value_state last_set_state;
4081 DEBUG(5, ("Attempting administrator password change for user %s\n",
4082 pdb_get_username(pwd)));
4084 acct_ctrl = pdb_get_acct_ctrl(pwd);
4085 /* we need to know if it's expired, because this is an admin change, not a
4086 user change, so it's still expired when we're done */
4087 last_set_state = pdb_get_init_flags(pwd, PDB_PASSLASTSET);
4088 last_set_time = pdb_get_pass_last_set_time(pwd);
4090 if (!decode_pw_buffer(talloc_tos(),
4091 pass,
4092 &plaintext_buf,
4093 &len,
4094 STR_UNICODE)) {
4095 return False;
4098 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4099 return False;
4102 /* if it's a trust account, don't update /etc/passwd */
4103 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4104 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4105 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4106 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
4107 } else {
4108 /* update the UNIX password */
4109 if (lp_unix_password_sync()) {
4110 struct passwd *passwd;
4112 if (pdb_get_username(pwd) == NULL) {
4113 DEBUG(1, ("chgpasswd: User without name???\n"));
4114 return False;
4117 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4118 if (passwd == NULL) {
4119 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4122 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
4123 return False;
4125 TALLOC_FREE(passwd);
4129 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4132 * A level 25 change does reset the pwdlastset field, a level 24
4133 * change does not. I know this is probably not the full story, but
4134 * it is needed to make XP join LDAP correctly, without it the later
4135 * auth2 check can fail with PWD_MUST_CHANGE.
4137 if (level != 25) {
4139 * restore last set time as this is an admin change, not a
4140 * user pw change
4142 pdb_set_pass_last_set_time (pwd, last_set_time,
4143 last_set_state);
4146 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
4148 /* update the SAMBA password */
4149 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
4150 return False;
4153 return True;
4156 /*******************************************************************
4157 set_user_info_25
4158 ********************************************************************/
4160 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
4161 struct samr_UserInfo25 *id25,
4162 struct samu *pwd)
4164 NTSTATUS status;
4166 if (id25 == NULL) {
4167 DEBUG(5, ("set_user_info_25: NULL id25\n"));
4168 return NT_STATUS_INVALID_PARAMETER;
4171 if (id25->info.fields_present == 0) {
4172 return NT_STATUS_INVALID_PARAMETER;
4175 if (id25->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4176 return NT_STATUS_ACCESS_DENIED;
4179 copy_id25_to_sam_passwd(pwd, id25);
4181 /* write the change out */
4182 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4183 return status;
4187 * We need to "pdb_update_sam_account" before the unix primary group
4188 * is set, because the idealx scripts would also change the
4189 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
4190 * the delete explicit / add explicit, which would then fail to find
4191 * the previous primaryGroupSid value.
4194 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4195 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4196 if ( !NT_STATUS_IS_OK(status) ) {
4197 return status;
4201 return NT_STATUS_OK;
4204 /*******************************************************************
4205 samr_SetUserInfo
4206 ********************************************************************/
4208 NTSTATUS _samr_SetUserInfo(pipes_struct *p,
4209 struct samr_SetUserInfo *r)
4211 NTSTATUS status;
4212 struct samu *pwd = NULL;
4213 DOM_SID sid;
4214 POLICY_HND *pol = r->in.user_handle;
4215 union samr_UserInfo *info = r->in.info;
4216 uint16_t switch_value = r->in.level;
4217 uint32_t acc_granted;
4218 uint32_t acc_required;
4219 bool ret;
4220 bool has_enough_rights = False;
4221 uint32_t acb_info;
4222 DISP_INFO *disp_info = NULL;
4224 DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
4226 /* find the policy handle. open a policy on it. */
4227 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info)) {
4228 return NT_STATUS_INVALID_HANDLE;
4231 /* This is tricky. A WinXP domain join sets
4232 (SAMR_USER_ACCESS_SET_PASSWORD|SAMR_USER_ACCESS_SET_ATTRIBUTES|SAMR_USER_ACCESS_GET_ATTRIBUTES)
4233 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
4234 standard Win32 API calls just ask for SAMR_USER_ACCESS_SET_PASSWORD in the SamrOpenUser().
4235 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
4236 we'll use the set from the WinXP join as the basis. */
4238 switch (switch_value) {
4239 case 18:
4240 case 24:
4241 case 25:
4242 case 26:
4243 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
4244 break;
4245 default:
4246 acc_required = SAMR_USER_ACCESS_SET_PASSWORD |
4247 SAMR_USER_ACCESS_SET_ATTRIBUTES |
4248 SAMR_USER_ACCESS_GET_ATTRIBUTES;
4249 break;
4252 status = access_check_samr_function(acc_granted,
4253 acc_required,
4254 "_samr_SetUserInfo");
4255 if (!NT_STATUS_IS_OK(status)) {
4256 return status;
4259 DEBUG(5, ("_samr_SetUserInfo: sid:%s, level:%d\n",
4260 sid_string_dbg(&sid), switch_value));
4262 if (info == NULL) {
4263 DEBUG(5, ("_samr_SetUserInfo: NULL info level\n"));
4264 return NT_STATUS_INVALID_INFO_CLASS;
4267 if (!(pwd = samu_new(NULL))) {
4268 return NT_STATUS_NO_MEMORY;
4271 become_root();
4272 ret = pdb_getsampwsid(pwd, &sid);
4273 unbecome_root();
4275 if (!ret) {
4276 TALLOC_FREE(pwd);
4277 return NT_STATUS_NO_SUCH_USER;
4280 /* deal with machine password changes differently from userinfo changes */
4281 /* check to see if we have the sufficient rights */
4283 acb_info = pdb_get_acct_ctrl(pwd);
4284 if (acb_info & ACB_WSTRUST)
4285 has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
4286 &se_machine_account);
4287 else if (acb_info & ACB_NORMAL)
4288 has_enough_rights = user_has_privileges(p->pipe_user.nt_user_token,
4289 &se_add_users);
4290 else if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
4291 if (lp_enable_privileges()) {
4292 has_enough_rights = nt_token_check_domain_rid(p->pipe_user.nt_user_token,
4293 DOMAIN_GROUP_RID_ADMINS);
4297 DEBUG(5, ("_samr_SetUserInfo: %s does%s possess sufficient rights\n",
4298 uidtoname(p->pipe_user.ut.uid),
4299 has_enough_rights ? "" : " not"));
4301 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
4303 if (has_enough_rights) {
4304 become_root();
4307 /* ok! user info levels (lots: see MSDEV help), off we go... */
4309 switch (switch_value) {
4311 case 7:
4312 status = set_user_info_7(p->mem_ctx,
4313 &info->info7, pwd);
4314 break;
4316 case 16:
4317 if (!set_user_info_16(&info->info16, pwd)) {
4318 status = NT_STATUS_ACCESS_DENIED;
4320 break;
4322 case 18:
4323 /* Used by AS/U JRA. */
4324 status = set_user_info_18(&info->info18,
4325 p->mem_ctx,
4326 &p->server_info->user_session_key,
4327 pwd);
4328 break;
4330 case 20:
4331 if (!set_user_info_20(&info->info20, pwd)) {
4332 status = NT_STATUS_ACCESS_DENIED;
4334 break;
4336 case 21:
4337 status = set_user_info_21(p->mem_ctx,
4338 &info->info21, pwd);
4339 break;
4341 case 23:
4342 if (!p->server_info->user_session_key.length) {
4343 status = NT_STATUS_NO_USER_SESSION_KEY;
4345 SamOEMhashBlob(info->info23.password.data, 516,
4346 &p->server_info->user_session_key);
4348 dump_data(100, info->info23.password.data, 516);
4350 status = set_user_info_23(p->mem_ctx,
4351 &info->info23, pwd);
4352 break;
4354 case 24:
4355 if (!p->server_info->user_session_key.length) {
4356 status = NT_STATUS_NO_USER_SESSION_KEY;
4358 SamOEMhashBlob(info->info24.password.data,
4359 516,
4360 &p->server_info->user_session_key);
4362 dump_data(100, info->info24.password.data, 516);
4364 if (!set_user_info_pw(info->info24.password.data, pwd,
4365 switch_value)) {
4366 status = NT_STATUS_WRONG_PASSWORD;
4368 break;
4370 case 25:
4371 if (!p->server_info->user_session_key.length) {
4372 status = NT_STATUS_NO_USER_SESSION_KEY;
4374 encode_or_decode_arc4_passwd_buffer(
4375 info->info25.password.data,
4376 &p->server_info->user_session_key);
4378 dump_data(100, info->info25.password.data, 532);
4380 status = set_user_info_25(p->mem_ctx,
4381 &info->info25, pwd);
4382 if (!NT_STATUS_IS_OK(status)) {
4383 goto done;
4385 if (!set_user_info_pw(info->info25.password.data, pwd,
4386 switch_value)) {
4387 status = NT_STATUS_WRONG_PASSWORD;
4389 break;
4391 case 26:
4392 if (!p->server_info->user_session_key.length) {
4393 status = NT_STATUS_NO_USER_SESSION_KEY;
4395 encode_or_decode_arc4_passwd_buffer(
4396 info->info26.password.data,
4397 &p->server_info->user_session_key);
4399 dump_data(100, info->info26.password.data, 516);
4401 if (!set_user_info_pw(info->info26.password.data, pwd,
4402 switch_value)) {
4403 status = NT_STATUS_WRONG_PASSWORD;
4405 break;
4407 default:
4408 status = NT_STATUS_INVALID_INFO_CLASS;
4411 done:
4413 TALLOC_FREE(pwd);
4415 if (has_enough_rights) {
4416 unbecome_root();
4419 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
4421 if (NT_STATUS_IS_OK(status)) {
4422 force_flush_samr_cache(disp_info);
4425 return status;
4428 /*******************************************************************
4429 _samr_SetUserInfo2
4430 ********************************************************************/
4432 NTSTATUS _samr_SetUserInfo2(pipes_struct *p,
4433 struct samr_SetUserInfo2 *r)
4435 struct samr_SetUserInfo q;
4437 q.in.user_handle = r->in.user_handle;
4438 q.in.level = r->in.level;
4439 q.in.info = r->in.info;
4441 return _samr_SetUserInfo(p, &q);
4444 /*********************************************************************
4445 _samr_GetAliasMembership
4446 *********************************************************************/
4448 NTSTATUS _samr_GetAliasMembership(pipes_struct *p,
4449 struct samr_GetAliasMembership *r)
4451 size_t num_alias_rids;
4452 uint32 *alias_rids;
4453 struct samr_info *info = NULL;
4454 size_t i;
4456 NTSTATUS ntstatus1;
4457 NTSTATUS ntstatus2;
4459 DOM_SID *members;
4461 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__));
4463 /* find the policy handle. open a policy on it. */
4464 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
4465 return NT_STATUS_INVALID_HANDLE;
4467 ntstatus1 = access_check_samr_function(info->acc_granted,
4468 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS,
4469 "_samr_GetAliasMembership");
4470 ntstatus2 = access_check_samr_function(info->acc_granted,
4471 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
4472 "_samr_GetAliasMembership");
4474 if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
4475 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
4476 !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
4477 return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
4481 if (!sid_check_is_domain(&info->sid) &&
4482 !sid_check_is_builtin(&info->sid))
4483 return NT_STATUS_OBJECT_TYPE_MISMATCH;
4485 if (r->in.sids->num_sids) {
4486 members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, r->in.sids->num_sids);
4488 if (members == NULL)
4489 return NT_STATUS_NO_MEMORY;
4490 } else {
4491 members = NULL;
4494 for (i=0; i<r->in.sids->num_sids; i++)
4495 sid_copy(&members[i], r->in.sids->sids[i].sid);
4497 alias_rids = NULL;
4498 num_alias_rids = 0;
4500 become_root();
4501 ntstatus1 = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
4502 r->in.sids->num_sids,
4503 &alias_rids, &num_alias_rids);
4504 unbecome_root();
4506 if (!NT_STATUS_IS_OK(ntstatus1)) {
4507 return ntstatus1;
4510 r->out.rids->count = num_alias_rids;
4511 r->out.rids->ids = alias_rids;
4513 return NT_STATUS_OK;
4516 /*********************************************************************
4517 _samr_GetMembersInAlias
4518 *********************************************************************/
4520 NTSTATUS _samr_GetMembersInAlias(pipes_struct *p,
4521 struct samr_GetMembersInAlias *r)
4523 NTSTATUS status;
4524 size_t i;
4525 size_t num_sids = 0;
4526 struct lsa_SidPtr *sids = NULL;
4527 DOM_SID *pdb_sids = NULL;
4529 DOM_SID alias_sid;
4531 uint32 acc_granted;
4533 /* find the policy handle. open a policy on it. */
4534 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, NULL))
4535 return NT_STATUS_INVALID_HANDLE;
4537 status = access_check_samr_function(acc_granted,
4538 SAMR_ALIAS_ACCESS_GET_MEMBERS,
4539 "_samr_GetMembersInAlias");
4540 if (!NT_STATUS_IS_OK(status)) {
4541 return status;
4544 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
4546 become_root();
4547 status = pdb_enum_aliasmem(&alias_sid, &pdb_sids, &num_sids);
4548 unbecome_root();
4550 if (!NT_STATUS_IS_OK(status)) {
4551 return status;
4554 if (num_sids) {
4555 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr, num_sids);
4556 if (sids == NULL) {
4557 TALLOC_FREE(pdb_sids);
4558 return NT_STATUS_NO_MEMORY;
4562 for (i = 0; i < num_sids; i++) {
4563 sids[i].sid = sid_dup_talloc(p->mem_ctx, &pdb_sids[i]);
4564 if (!sids[i].sid) {
4565 TALLOC_FREE(pdb_sids);
4566 return NT_STATUS_NO_MEMORY;
4570 r->out.sids->num_sids = num_sids;
4571 r->out.sids->sids = sids;
4573 TALLOC_FREE(pdb_sids);
4575 return NT_STATUS_OK;
4578 /*********************************************************************
4579 _samr_QueryGroupMember
4580 *********************************************************************/
4582 NTSTATUS _samr_QueryGroupMember(pipes_struct *p,
4583 struct samr_QueryGroupMember *r)
4585 DOM_SID group_sid;
4586 size_t i, num_members;
4588 uint32 *rid=NULL;
4589 uint32 *attr=NULL;
4591 uint32 acc_granted;
4593 NTSTATUS status;
4594 struct samr_RidTypeArray *rids = NULL;
4596 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidTypeArray);
4597 if (!rids) {
4598 return NT_STATUS_NO_MEMORY;
4601 /* find the policy handle. open a policy on it. */
4602 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, NULL))
4603 return NT_STATUS_INVALID_HANDLE;
4605 status = access_check_samr_function(acc_granted,
4606 SAMR_GROUP_ACCESS_GET_MEMBERS,
4607 "_samr_QueryGroupMember");
4608 if (!NT_STATUS_IS_OK(status)) {
4609 return status;
4612 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid)));
4614 if (!sid_check_is_in_our_domain(&group_sid)) {
4615 DEBUG(3, ("sid %s is not in our domain\n",
4616 sid_string_dbg(&group_sid)));
4617 return NT_STATUS_NO_SUCH_GROUP;
4620 DEBUG(10, ("lookup on Domain SID\n"));
4622 become_root();
4623 status = pdb_enum_group_members(p->mem_ctx, &group_sid,
4624 &rid, &num_members);
4625 unbecome_root();
4627 if (!NT_STATUS_IS_OK(status))
4628 return status;
4630 if (num_members) {
4631 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
4632 if (attr == NULL) {
4633 return NT_STATUS_NO_MEMORY;
4635 } else {
4636 attr = NULL;
4639 for (i=0; i<num_members; i++)
4640 attr[i] = SID_NAME_USER;
4642 rids->count = num_members;
4643 rids->types = attr;
4644 rids->rids = rid;
4646 *r->out.rids = rids;
4648 return NT_STATUS_OK;
4651 /*********************************************************************
4652 _samr_AddAliasMember
4653 *********************************************************************/
4655 NTSTATUS _samr_AddAliasMember(pipes_struct *p,
4656 struct samr_AddAliasMember *r)
4658 DOM_SID alias_sid;
4659 uint32 acc_granted;
4660 SE_PRIV se_rights;
4661 bool can_add_accounts;
4662 NTSTATUS status;
4663 DISP_INFO *disp_info = NULL;
4665 /* Find the policy handle. Open a policy on it. */
4666 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, &disp_info))
4667 return NT_STATUS_INVALID_HANDLE;
4669 status = access_check_samr_function(acc_granted,
4670 SAMR_ALIAS_ACCESS_ADD_MEMBER,
4671 "_samr_AddAliasMember");
4672 if (!NT_STATUS_IS_OK(status)) {
4673 return status;
4676 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
4678 se_priv_copy( &se_rights, &se_add_users );
4679 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4681 /******** BEGIN SeAddUsers BLOCK *********/
4683 if ( can_add_accounts )
4684 become_root();
4686 status = pdb_add_aliasmem(&alias_sid, r->in.sid);
4688 if ( can_add_accounts )
4689 unbecome_root();
4691 /******** END SeAddUsers BLOCK *********/
4693 if (NT_STATUS_IS_OK(status)) {
4694 force_flush_samr_cache(disp_info);
4697 return status;
4700 /*********************************************************************
4701 _samr_DeleteAliasMember
4702 *********************************************************************/
4704 NTSTATUS _samr_DeleteAliasMember(pipes_struct *p,
4705 struct samr_DeleteAliasMember *r)
4707 DOM_SID alias_sid;
4708 uint32 acc_granted;
4709 SE_PRIV se_rights;
4710 bool can_add_accounts;
4711 NTSTATUS status;
4712 DISP_INFO *disp_info = NULL;
4714 /* Find the policy handle. Open a policy on it. */
4715 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, &disp_info))
4716 return NT_STATUS_INVALID_HANDLE;
4718 status = access_check_samr_function(acc_granted,
4719 SAMR_ALIAS_ACCESS_REMOVE_MEMBER,
4720 "_samr_DeleteAliasMember");
4721 if (!NT_STATUS_IS_OK(status)) {
4722 return status;
4725 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
4726 sid_string_dbg(&alias_sid)));
4728 se_priv_copy( &se_rights, &se_add_users );
4729 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4731 /******** BEGIN SeAddUsers BLOCK *********/
4733 if ( can_add_accounts )
4734 become_root();
4736 status = pdb_del_aliasmem(&alias_sid, r->in.sid);
4738 if ( can_add_accounts )
4739 unbecome_root();
4741 /******** END SeAddUsers BLOCK *********/
4743 if (NT_STATUS_IS_OK(status)) {
4744 force_flush_samr_cache(disp_info);
4747 return status;
4750 /*********************************************************************
4751 _samr_AddGroupMember
4752 *********************************************************************/
4754 NTSTATUS _samr_AddGroupMember(pipes_struct *p,
4755 struct samr_AddGroupMember *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;
4765 /* Find the policy handle. Open a policy on it. */
4766 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
4767 return NT_STATUS_INVALID_HANDLE;
4769 status = access_check_samr_function(acc_granted,
4770 SAMR_GROUP_ACCESS_ADD_MEMBER,
4771 "_samr_AddGroupMember");
4772 if (!NT_STATUS_IS_OK(status)) {
4773 return status;
4776 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid)));
4778 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4779 &group_rid)) {
4780 return NT_STATUS_INVALID_HANDLE;
4783 se_priv_copy( &se_rights, &se_add_users );
4784 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4786 /******** BEGIN SeAddUsers BLOCK *********/
4788 if ( can_add_accounts )
4789 become_root();
4791 status = pdb_add_groupmem(p->mem_ctx, group_rid, r->in.rid);
4793 if ( can_add_accounts )
4794 unbecome_root();
4796 /******** END SeAddUsers BLOCK *********/
4798 force_flush_samr_cache(disp_info);
4800 return status;
4803 /*********************************************************************
4804 _samr_DeleteGroupMember
4805 *********************************************************************/
4807 NTSTATUS _samr_DeleteGroupMember(pipes_struct *p,
4808 struct samr_DeleteGroupMember *r)
4811 NTSTATUS status;
4812 DOM_SID group_sid;
4813 uint32 group_rid;
4814 uint32 acc_granted;
4815 SE_PRIV se_rights;
4816 bool can_add_accounts;
4817 DISP_INFO *disp_info = NULL;
4820 * delete the group member named r->in.rid
4821 * who is a member of the sid associated with the handle
4822 * the rid is a user's rid as the group is a domain group.
4825 /* Find the policy handle. Open a policy on it. */
4826 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
4827 return NT_STATUS_INVALID_HANDLE;
4829 status = access_check_samr_function(acc_granted,
4830 SAMR_GROUP_ACCESS_REMOVE_MEMBER,
4831 "_samr_DeleteGroupMember");
4832 if (!NT_STATUS_IS_OK(status)) {
4833 return status;
4836 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4837 &group_rid)) {
4838 return NT_STATUS_INVALID_HANDLE;
4841 se_priv_copy( &se_rights, &se_add_users );
4842 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4844 /******** BEGIN SeAddUsers BLOCK *********/
4846 if ( can_add_accounts )
4847 become_root();
4849 status = pdb_del_groupmem(p->mem_ctx, group_rid, r->in.rid);
4851 if ( can_add_accounts )
4852 unbecome_root();
4854 /******** END SeAddUsers BLOCK *********/
4856 force_flush_samr_cache(disp_info);
4858 return status;
4861 /*********************************************************************
4862 _samr_DeleteUser
4863 *********************************************************************/
4865 NTSTATUS _samr_DeleteUser(pipes_struct *p,
4866 struct samr_DeleteUser *r)
4868 NTSTATUS status;
4869 DOM_SID user_sid;
4870 struct samu *sam_pass=NULL;
4871 uint32 acc_granted;
4872 bool can_add_accounts;
4873 uint32 acb_info;
4874 DISP_INFO *disp_info = NULL;
4875 bool ret;
4877 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__));
4879 /* Find the policy handle. Open a policy on it. */
4880 if (!get_lsa_policy_samr_sid(p, r->in.user_handle, &user_sid, &acc_granted, &disp_info))
4881 return NT_STATUS_INVALID_HANDLE;
4883 status = access_check_samr_function(acc_granted,
4884 STD_RIGHT_DELETE_ACCESS,
4885 "_samr_DeleteUser");
4886 if (!NT_STATUS_IS_OK(status)) {
4887 return status;
4890 if (!sid_check_is_in_our_domain(&user_sid))
4891 return NT_STATUS_CANNOT_DELETE;
4893 /* check if the user exists before trying to delete */
4894 if ( !(sam_pass = samu_new( NULL )) ) {
4895 return NT_STATUS_NO_MEMORY;
4898 become_root();
4899 ret = pdb_getsampwsid(sam_pass, &user_sid);
4900 unbecome_root();
4902 if( !ret ) {
4903 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
4904 sid_string_dbg(&user_sid)));
4905 TALLOC_FREE(sam_pass);
4906 return NT_STATUS_NO_SUCH_USER;
4909 acb_info = pdb_get_acct_ctrl(sam_pass);
4911 /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
4912 if ( acb_info & ACB_WSTRUST ) {
4913 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account );
4914 } else {
4915 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4918 /******** BEGIN SeAddUsers BLOCK *********/
4920 if ( can_add_accounts )
4921 become_root();
4923 status = pdb_delete_user(p->mem_ctx, sam_pass);
4925 if ( can_add_accounts )
4926 unbecome_root();
4928 /******** END SeAddUsers BLOCK *********/
4930 if ( !NT_STATUS_IS_OK(status) ) {
4931 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
4932 "user %s: %s.\n", pdb_get_username(sam_pass),
4933 nt_errstr(status)));
4934 TALLOC_FREE(sam_pass);
4935 return status;
4939 TALLOC_FREE(sam_pass);
4941 if (!close_policy_hnd(p, r->in.user_handle))
4942 return NT_STATUS_OBJECT_NAME_INVALID;
4944 ZERO_STRUCTP(r->out.user_handle);
4946 force_flush_samr_cache(disp_info);
4948 return NT_STATUS_OK;
4951 /*********************************************************************
4952 _samr_DeleteDomainGroup
4953 *********************************************************************/
4955 NTSTATUS _samr_DeleteDomainGroup(pipes_struct *p,
4956 struct samr_DeleteDomainGroup *r)
4958 NTSTATUS status;
4959 DOM_SID group_sid;
4960 uint32 group_rid;
4961 uint32 acc_granted;
4962 SE_PRIV se_rights;
4963 bool can_add_accounts;
4964 DISP_INFO *disp_info = NULL;
4966 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__));
4968 /* Find the policy handle. Open a policy on it. */
4969 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
4970 return NT_STATUS_INVALID_HANDLE;
4972 status = access_check_samr_function(acc_granted,
4973 STD_RIGHT_DELETE_ACCESS,
4974 "_samr_DeleteDomainGroup");
4975 if (!NT_STATUS_IS_OK(status)) {
4976 return status;
4979 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid)));
4981 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4982 &group_rid)) {
4983 return NT_STATUS_NO_SUCH_GROUP;
4986 se_priv_copy( &se_rights, &se_add_users );
4987 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4989 /******** BEGIN SeAddUsers BLOCK *********/
4991 if ( can_add_accounts )
4992 become_root();
4994 status = pdb_delete_dom_group(p->mem_ctx, group_rid);
4996 if ( can_add_accounts )
4997 unbecome_root();
4999 /******** END SeAddUsers BLOCK *********/
5001 if ( !NT_STATUS_IS_OK(status) ) {
5002 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
5003 "entry for group %s: %s\n",
5004 sid_string_dbg(&group_sid),
5005 nt_errstr(status)));
5006 return status;
5009 if (!close_policy_hnd(p, r->in.group_handle))
5010 return NT_STATUS_OBJECT_NAME_INVALID;
5012 force_flush_samr_cache(disp_info);
5014 return NT_STATUS_OK;
5017 /*********************************************************************
5018 _samr_DeleteDomAlias
5019 *********************************************************************/
5021 NTSTATUS _samr_DeleteDomAlias(pipes_struct *p,
5022 struct samr_DeleteDomAlias *r)
5024 DOM_SID alias_sid;
5025 uint32 acc_granted;
5026 SE_PRIV se_rights;
5027 bool can_add_accounts;
5028 NTSTATUS status;
5029 DISP_INFO *disp_info = NULL;
5031 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__));
5033 /* Find the policy handle. Open a policy on it. */
5034 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &alias_sid, &acc_granted, &disp_info))
5035 return NT_STATUS_INVALID_HANDLE;
5037 /* copy the handle to the outgoing reply */
5039 memcpy(r->out.alias_handle, r->in.alias_handle, sizeof(r->out.alias_handle));
5041 status = access_check_samr_function(acc_granted,
5042 STD_RIGHT_DELETE_ACCESS,
5043 "_samr_DeleteDomAlias");
5044 if (!NT_STATUS_IS_OK(status)) {
5045 return status;
5048 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid)));
5050 /* Don't let Windows delete builtin groups */
5052 if ( sid_check_is_in_builtin( &alias_sid ) ) {
5053 return NT_STATUS_SPECIAL_ACCOUNT;
5056 if (!sid_check_is_in_our_domain(&alias_sid))
5057 return NT_STATUS_NO_SUCH_ALIAS;
5059 DEBUG(10, ("lookup on Local SID\n"));
5061 se_priv_copy( &se_rights, &se_add_users );
5062 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
5064 /******** BEGIN SeAddUsers BLOCK *********/
5066 if ( can_add_accounts )
5067 become_root();
5069 /* Have passdb delete the alias */
5070 status = pdb_delete_alias(&alias_sid);
5072 if ( can_add_accounts )
5073 unbecome_root();
5075 /******** END SeAddUsers BLOCK *********/
5077 if ( !NT_STATUS_IS_OK(status))
5078 return status;
5080 if (!close_policy_hnd(p, r->in.alias_handle))
5081 return NT_STATUS_OBJECT_NAME_INVALID;
5083 force_flush_samr_cache(disp_info);
5085 return NT_STATUS_OK;
5088 /*********************************************************************
5089 _samr_CreateDomainGroup
5090 *********************************************************************/
5092 NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
5093 struct samr_CreateDomainGroup *r)
5096 NTSTATUS status;
5097 DOM_SID dom_sid;
5098 DOM_SID info_sid;
5099 const char *name;
5100 struct samr_info *info;
5101 uint32 acc_granted;
5102 SE_PRIV se_rights;
5103 bool can_add_accounts;
5104 DISP_INFO *disp_info = NULL;
5106 /* Find the policy handle. Open a policy on it. */
5107 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &dom_sid, &acc_granted, &disp_info))
5108 return NT_STATUS_INVALID_HANDLE;
5110 status = access_check_samr_function(acc_granted,
5111 SAMR_DOMAIN_ACCESS_CREATE_GROUP,
5112 "_samr_CreateDomainGroup");
5113 if (!NT_STATUS_IS_OK(status)) {
5114 return status;
5117 if (!sid_equal(&dom_sid, get_global_sam_sid()))
5118 return NT_STATUS_ACCESS_DENIED;
5120 name = r->in.name->string;
5121 if (name == NULL) {
5122 return NT_STATUS_NO_MEMORY;
5125 status = can_create(p->mem_ctx, name);
5126 if (!NT_STATUS_IS_OK(status)) {
5127 return status;
5130 se_priv_copy( &se_rights, &se_add_users );
5131 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
5133 /******** BEGIN SeAddUsers BLOCK *********/
5135 if ( can_add_accounts )
5136 become_root();
5138 /* check that we successfully create the UNIX group */
5140 status = pdb_create_dom_group(p->mem_ctx, name, r->out.rid);
5142 if ( can_add_accounts )
5143 unbecome_root();
5145 /******** END SeAddUsers BLOCK *********/
5147 /* check if we should bail out here */
5149 if ( !NT_STATUS_IS_OK(status) )
5150 return status;
5152 sid_compose(&info_sid, get_global_sam_sid(), *r->out.rid);
5154 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
5155 return NT_STATUS_NO_MEMORY;
5157 /* they created it; let the user do what he wants with it */
5159 info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
5161 /* get a (unique) handle. open a policy on it. */
5162 if (!create_policy_hnd(p, r->out.group_handle, free_samr_info, (void *)info))
5163 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5165 force_flush_samr_cache(disp_info);
5167 return NT_STATUS_OK;
5170 /*********************************************************************
5171 _samr_CreateDomAlias
5172 *********************************************************************/
5174 NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
5175 struct samr_CreateDomAlias *r)
5177 DOM_SID dom_sid;
5178 DOM_SID info_sid;
5179 const char *name = NULL;
5180 struct samr_info *info;
5181 uint32 acc_granted;
5182 gid_t gid;
5183 NTSTATUS result;
5184 SE_PRIV se_rights;
5185 bool can_add_accounts;
5186 DISP_INFO *disp_info = NULL;
5188 /* Find the policy handle. Open a policy on it. */
5189 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &dom_sid, &acc_granted, &disp_info))
5190 return NT_STATUS_INVALID_HANDLE;
5192 result = access_check_samr_function(acc_granted,
5193 SAMR_DOMAIN_ACCESS_CREATE_ALIAS,
5194 "_samr_CreateDomAlias");
5195 if (!NT_STATUS_IS_OK(result)) {
5196 return result;
5199 if (!sid_equal(&dom_sid, get_global_sam_sid()))
5200 return NT_STATUS_ACCESS_DENIED;
5202 name = r->in.alias_name->string;
5204 se_priv_copy( &se_rights, &se_add_users );
5205 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
5207 result = can_create(p->mem_ctx, name);
5208 if (!NT_STATUS_IS_OK(result)) {
5209 return result;
5212 /******** BEGIN SeAddUsers BLOCK *********/
5214 if ( can_add_accounts )
5215 become_root();
5217 /* Have passdb create the alias */
5218 result = pdb_create_alias(name, r->out.rid);
5220 if ( can_add_accounts )
5221 unbecome_root();
5223 /******** END SeAddUsers BLOCK *********/
5225 if (!NT_STATUS_IS_OK(result)) {
5226 DEBUG(10, ("pdb_create_alias failed: %s\n",
5227 nt_errstr(result)));
5228 return result;
5231 sid_copy(&info_sid, get_global_sam_sid());
5232 sid_append_rid(&info_sid, *r->out.rid);
5234 if (!sid_to_gid(&info_sid, &gid)) {
5235 DEBUG(10, ("Could not find alias just created\n"));
5236 return NT_STATUS_ACCESS_DENIED;
5239 /* check if the group has been successfully created */
5240 if ( getgrgid(gid) == NULL ) {
5241 DEBUG(10, ("getgrgid(%d) of just created alias failed\n",
5242 gid));
5243 return NT_STATUS_ACCESS_DENIED;
5246 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
5247 return NT_STATUS_NO_MEMORY;
5249 /* they created it; let the user do what he wants with it */
5251 info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
5253 /* get a (unique) handle. open a policy on it. */
5254 if (!create_policy_hnd(p, r->out.alias_handle, free_samr_info, (void *)info))
5255 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5257 force_flush_samr_cache(disp_info);
5259 return NT_STATUS_OK;
5262 /*********************************************************************
5263 _samr_QueryGroupInfo
5264 *********************************************************************/
5266 NTSTATUS _samr_QueryGroupInfo(pipes_struct *p,
5267 struct samr_QueryGroupInfo *r)
5269 NTSTATUS status;
5270 DOM_SID group_sid;
5271 GROUP_MAP map;
5272 union samr_GroupInfo *info = NULL;
5273 uint32 acc_granted;
5274 bool ret;
5275 uint32_t attributes = SE_GROUP_MANDATORY |
5276 SE_GROUP_ENABLED_BY_DEFAULT |
5277 SE_GROUP_ENABLED;
5278 const char *group_name = NULL;
5279 const char *group_description = NULL;
5281 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, NULL))
5282 return NT_STATUS_INVALID_HANDLE;
5284 status = access_check_samr_function(acc_granted,
5285 SAMR_GROUP_ACCESS_LOOKUP_INFO,
5286 "_samr_QueryGroupInfo");
5287 if (!NT_STATUS_IS_OK(status)) {
5288 return status;
5291 become_root();
5292 ret = get_domain_group_from_sid(group_sid, &map);
5293 unbecome_root();
5294 if (!ret)
5295 return NT_STATUS_INVALID_HANDLE;
5297 /* FIXME: map contains fstrings */
5298 group_name = talloc_strdup(r, map.nt_name);
5299 group_description = talloc_strdup(r, map.comment);
5301 info = TALLOC_ZERO_P(p->mem_ctx, union samr_GroupInfo);
5302 if (!info) {
5303 return NT_STATUS_NO_MEMORY;
5306 switch (r->in.level) {
5307 case 1: {
5308 uint32 *members;
5309 size_t num_members;
5311 become_root();
5312 status = pdb_enum_group_members(
5313 p->mem_ctx, &group_sid, &members, &num_members);
5314 unbecome_root();
5316 if (!NT_STATUS_IS_OK(status)) {
5317 return status;
5320 init_samr_group_info1(&info->all,
5321 group_name,
5322 attributes,
5323 num_members,
5324 group_description);
5325 break;
5327 case 2:
5328 init_samr_group_info2(&info->name,
5329 group_name);
5330 break;
5331 case 3:
5332 init_samr_group_info3(&info->attributes,
5333 attributes);
5334 break;
5335 case 4:
5336 init_samr_group_info4(&info->description,
5337 group_description);
5338 break;
5339 case 5: {
5341 uint32 *members;
5342 size_t num_members;
5346 become_root();
5347 status = pdb_enum_group_members(
5348 p->mem_ctx, &group_sid, &members, &num_members);
5349 unbecome_root();
5351 if (!NT_STATUS_IS_OK(status)) {
5352 return status;
5355 init_samr_group_info5(&info->all2,
5356 group_name,
5357 attributes,
5358 0, /* num_members - in w2k3 this is always 0 */
5359 group_description);
5361 break;
5363 default:
5364 return NT_STATUS_INVALID_INFO_CLASS;
5367 *r->out.info = info;
5369 return NT_STATUS_OK;
5372 /*********************************************************************
5373 _samr_SetGroupInfo
5374 *********************************************************************/
5376 NTSTATUS _samr_SetGroupInfo(pipes_struct *p,
5377 struct samr_SetGroupInfo *r)
5379 DOM_SID group_sid;
5380 GROUP_MAP map;
5381 uint32 acc_granted;
5382 NTSTATUS status;
5383 bool ret;
5384 bool can_mod_accounts;
5385 DISP_INFO *disp_info = NULL;
5387 if (!get_lsa_policy_samr_sid(p, r->in.group_handle, &group_sid, &acc_granted, &disp_info))
5388 return NT_STATUS_INVALID_HANDLE;
5390 status = access_check_samr_function(acc_granted,
5391 SAMR_GROUP_ACCESS_SET_INFO,
5392 "_samr_SetGroupInfo");
5393 if (!NT_STATUS_IS_OK(status)) {
5394 return status;
5397 become_root();
5398 ret = get_domain_group_from_sid(group_sid, &map);
5399 unbecome_root();
5400 if (!ret)
5401 return NT_STATUS_NO_SUCH_GROUP;
5403 switch (r->in.level) {
5404 case 1:
5405 fstrcpy(map.comment, r->in.info->all.description.string);
5406 break;
5407 case 4:
5408 fstrcpy(map.comment, r->in.info->description.string);
5409 break;
5410 default:
5411 return NT_STATUS_INVALID_INFO_CLASS;
5414 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
5416 /******** BEGIN SeAddUsers BLOCK *********/
5418 if ( can_mod_accounts )
5419 become_root();
5421 status = pdb_update_group_mapping_entry(&map);
5423 if ( can_mod_accounts )
5424 unbecome_root();
5426 /******** End SeAddUsers BLOCK *********/
5428 if (NT_STATUS_IS_OK(status)) {
5429 force_flush_samr_cache(disp_info);
5432 return status;
5435 /*********************************************************************
5436 _samr_SetAliasInfo
5437 *********************************************************************/
5439 NTSTATUS _samr_SetAliasInfo(pipes_struct *p,
5440 struct samr_SetAliasInfo *r)
5442 DOM_SID group_sid;
5443 struct acct_info info;
5444 uint32 acc_granted;
5445 bool can_mod_accounts;
5446 NTSTATUS status;
5447 DISP_INFO *disp_info = NULL;
5449 if (!get_lsa_policy_samr_sid(p, r->in.alias_handle, &group_sid, &acc_granted, &disp_info))
5450 return NT_STATUS_INVALID_HANDLE;
5452 status = access_check_samr_function(acc_granted,
5453 SAMR_ALIAS_ACCESS_SET_INFO,
5454 "_samr_SetAliasInfo");
5455 if (!NT_STATUS_IS_OK(status)) {
5456 return status;
5459 /* get the current group information */
5461 become_root();
5462 status = pdb_get_aliasinfo( &group_sid, &info );
5463 unbecome_root();
5465 if ( !NT_STATUS_IS_OK(status))
5466 return status;
5468 switch (r->in.level) {
5469 case ALIASINFONAME:
5471 fstring group_name;
5473 /* We currently do not support renaming groups in the
5474 the BUILTIN domain. Refer to util_builtin.c to understand
5475 why. The eventually needs to be fixed to be like Windows
5476 where you can rename builtin groups, just not delete them */
5478 if ( sid_check_is_in_builtin( &group_sid ) ) {
5479 return NT_STATUS_SPECIAL_ACCOUNT;
5482 /* There has to be a valid name (and it has to be different) */
5484 if ( !r->in.info->name.string )
5485 return NT_STATUS_INVALID_PARAMETER;
5487 /* If the name is the same just reply "ok". Yes this
5488 doesn't allow you to change the case of a group name. */
5490 if ( strequal( r->in.info->name.string, info.acct_name ) )
5491 return NT_STATUS_OK;
5493 fstrcpy( info.acct_name, r->in.info->name.string);
5495 /* make sure the name doesn't already exist as a user
5496 or local group */
5498 fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
5499 status = can_create( p->mem_ctx, group_name );
5500 if ( !NT_STATUS_IS_OK( status ) )
5501 return status;
5502 break;
5504 case ALIASINFODESCRIPTION:
5505 if (r->in.info->description.string) {
5506 fstrcpy(info.acct_desc,
5507 r->in.info->description.string);
5508 } else {
5509 fstrcpy( info.acct_desc, "" );
5511 break;
5512 default:
5513 return NT_STATUS_INVALID_INFO_CLASS;
5516 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
5518 /******** BEGIN SeAddUsers BLOCK *********/
5520 if ( can_mod_accounts )
5521 become_root();
5523 status = pdb_set_aliasinfo( &group_sid, &info );
5525 if ( can_mod_accounts )
5526 unbecome_root();
5528 /******** End SeAddUsers BLOCK *********/
5530 if (NT_STATUS_IS_OK(status))
5531 force_flush_samr_cache(disp_info);
5533 return status;
5536 /****************************************************************
5537 _samr_GetDomPwInfo
5538 ****************************************************************/
5540 NTSTATUS _samr_GetDomPwInfo(pipes_struct *p,
5541 struct samr_GetDomPwInfo *r)
5543 uint32_t min_password_length = 0;
5544 uint32_t password_properties = 0;
5546 /* Perform access check. Since this rpc does not require a
5547 policy handle it will not be caught by the access checks on
5548 SAMR_CONNECT or SAMR_CONNECT_ANON. */
5550 if (!pipe_access_check(p)) {
5551 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
5552 return NT_STATUS_ACCESS_DENIED;
5555 become_root();
5556 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
5557 &min_password_length);
5558 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
5559 &password_properties);
5560 unbecome_root();
5562 if (lp_check_password_script() && *lp_check_password_script()) {
5563 password_properties |= DOMAIN_PASSWORD_COMPLEX;
5566 r->out.info->min_password_length = min_password_length;
5567 r->out.info->password_properties = password_properties;
5569 return NT_STATUS_OK;
5572 /*********************************************************************
5573 _samr_OpenGroup
5574 *********************************************************************/
5576 NTSTATUS _samr_OpenGroup(pipes_struct *p,
5577 struct samr_OpenGroup *r)
5580 DOM_SID sid;
5581 DOM_SID info_sid;
5582 GROUP_MAP map;
5583 struct samr_info *info;
5584 SEC_DESC *psd = NULL;
5585 uint32 acc_granted;
5586 uint32 des_access = r->in.access_mask;
5587 size_t sd_size;
5588 NTSTATUS status;
5589 fstring sid_string;
5590 bool ret;
5591 SE_PRIV se_rights;
5593 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &sid, &acc_granted, NULL))
5594 return NT_STATUS_INVALID_HANDLE;
5596 status = access_check_samr_function(acc_granted,
5597 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
5598 "_samr_OpenGroup");
5600 if ( !NT_STATUS_IS_OK(status) )
5601 return status;
5603 /*check if access can be granted as requested by client. */
5604 map_max_allowed_access(p->pipe_user.nt_user_token, &des_access);
5606 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
5607 se_map_generic(&des_access,&grp_generic_mapping);
5609 se_priv_copy( &se_rights, &se_add_users );
5611 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
5612 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access,
5613 &acc_granted, "_samr_OpenGroup");
5615 if ( !NT_STATUS_IS_OK(status) )
5616 return status;
5618 /* this should not be hard-coded like this */
5620 if (!sid_equal(&sid, get_global_sam_sid()))
5621 return NT_STATUS_ACCESS_DENIED;
5623 sid_copy(&info_sid, get_global_sam_sid());
5624 sid_append_rid(&info_sid, r->in.rid);
5625 sid_to_fstring(sid_string, &info_sid);
5627 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
5628 return NT_STATUS_NO_MEMORY;
5630 info->acc_granted = acc_granted;
5632 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n", sid_string));
5634 /* check if that group really exists */
5635 become_root();
5636 ret = get_domain_group_from_sid(info->sid, &map);
5637 unbecome_root();
5638 if (!ret)
5639 return NT_STATUS_NO_SUCH_GROUP;
5641 /* get a (unique) handle. open a policy on it. */
5642 if (!create_policy_hnd(p, r->out.group_handle, free_samr_info, (void *)info))
5643 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
5645 return NT_STATUS_OK;
5648 /*********************************************************************
5649 _samr_RemoveMemberFromForeignDomain
5650 *********************************************************************/
5652 NTSTATUS _samr_RemoveMemberFromForeignDomain(pipes_struct *p,
5653 struct samr_RemoveMemberFromForeignDomain *r)
5655 DOM_SID delete_sid, domain_sid;
5656 uint32 acc_granted;
5657 NTSTATUS result;
5658 DISP_INFO *disp_info = NULL;
5660 sid_copy( &delete_sid, r->in.sid );
5662 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
5663 sid_string_dbg(&delete_sid)));
5665 /* Find the policy handle. Open a policy on it. */
5667 if (!get_lsa_policy_samr_sid(p, r->in.domain_handle, &domain_sid,
5668 &acc_granted, &disp_info))
5669 return NT_STATUS_INVALID_HANDLE;
5671 result = access_check_samr_function(acc_granted,
5672 STD_RIGHT_DELETE_ACCESS,
5673 "_samr_RemoveMemberFromForeignDomain");
5675 if (!NT_STATUS_IS_OK(result))
5676 return result;
5678 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
5679 sid_string_dbg(&domain_sid)));
5681 /* we can only delete a user from a group since we don't have
5682 nested groups anyways. So in the latter case, just say OK */
5684 /* TODO: The above comment nowadays is bogus. Since we have nested
5685 * groups now, and aliases members are never reported out of the unix
5686 * group membership, the "just say OK" makes this call a no-op. For
5687 * us. This needs fixing however. */
5689 /* I've only ever seen this in the wild when deleting a user from
5690 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
5691 * is the user about to be deleted. I very much suspect this is the
5692 * only application of this call. To verify this, let people report
5693 * other cases. */
5695 if (!sid_check_is_builtin(&domain_sid)) {
5696 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
5697 "global_sam_sid() = %s\n",
5698 sid_string_dbg(&domain_sid),
5699 sid_string_dbg(get_global_sam_sid())));
5700 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
5701 return NT_STATUS_OK;
5704 force_flush_samr_cache(disp_info);
5706 result = NT_STATUS_OK;
5708 return result;
5711 /*******************************************************************
5712 _samr_QueryDomainInfo2
5713 ********************************************************************/
5715 NTSTATUS _samr_QueryDomainInfo2(pipes_struct *p,
5716 struct samr_QueryDomainInfo2 *r)
5718 struct samr_QueryDomainInfo q;
5720 q.in.domain_handle = r->in.domain_handle;
5721 q.in.level = r->in.level;
5723 q.out.info = r->out.info;
5725 return _samr_QueryDomainInfo(p, &q);
5728 /*******************************************************************
5729 _samr_SetDomainInfo
5730 ********************************************************************/
5732 NTSTATUS _samr_SetDomainInfo(pipes_struct *p,
5733 struct samr_SetDomainInfo *r)
5735 struct samr_info *info = NULL;
5736 time_t u_expire, u_min_age;
5737 time_t u_logout;
5738 time_t u_lock_duration, u_reset_time;
5739 NTSTATUS result;
5741 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5743 /* find the policy handle. open a policy on it. */
5744 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info))
5745 return NT_STATUS_INVALID_HANDLE;
5747 /* We do have different access bits for info
5748 * levels here, but we're really just looking for
5749 * GENERIC_RIGHTS_DOMAIN_WRITE access. Unfortunately
5750 * this maps to different specific bits. So
5751 * assume if we have SAMR_DOMAIN_ACCESS_SET_INFO_1
5752 * set we are ok. */
5754 result = access_check_samr_function(info->acc_granted,
5755 SAMR_DOMAIN_ACCESS_SET_INFO_1,
5756 "_samr_SetDomainInfo");
5758 if (!NT_STATUS_IS_OK(result))
5759 return result;
5761 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r->in.level));
5763 switch (r->in.level) {
5764 case 0x01:
5765 u_expire=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.max_password_age);
5766 u_min_age=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.min_password_age);
5767 pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)r->in.info->info1.min_password_length);
5768 pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)r->in.info->info1.password_history_length);
5769 pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)r->in.info->info1.password_properties);
5770 pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
5771 pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
5772 break;
5773 case 0x02:
5774 break;
5775 case 0x03:
5776 u_logout=nt_time_to_unix_abs((NTTIME *)&r->in.info->info3.force_logoff_time);
5777 pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
5778 break;
5779 case 0x05:
5780 break;
5781 case 0x06:
5782 break;
5783 case 0x07:
5784 break;
5785 case 0x0c:
5786 u_lock_duration=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_duration);
5787 if (u_lock_duration != -1)
5788 u_lock_duration /= 60;
5790 u_reset_time=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_window)/60;
5792 pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
5793 pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
5794 pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)r->in.info->info12.lockout_threshold);
5795 break;
5796 default:
5797 return NT_STATUS_INVALID_INFO_CLASS;
5800 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5802 return NT_STATUS_OK;
5805 /****************************************************************
5806 _samr_GetDisplayEnumerationIndex
5807 ****************************************************************/
5809 NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
5810 struct samr_GetDisplayEnumerationIndex *r)
5812 struct samr_info *info = NULL;
5813 uint32_t max_entries = (uint32_t) -1;
5814 uint32_t enum_context = 0;
5815 int i;
5816 uint32_t num_account = 0;
5817 struct samr_displayentry *entries = NULL;
5818 NTSTATUS status;
5820 DEBUG(5,("_samr_GetDisplayEnumerationIndex: %d\n", __LINE__));
5822 /* find the policy handle. open a policy on it. */
5823 if (!find_policy_by_hnd(p, r->in.domain_handle, (void **)(void *)&info)) {
5824 return NT_STATUS_INVALID_HANDLE;
5827 status = access_check_samr_function(info->acc_granted,
5828 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
5829 "_samr_GetDisplayEnumerationIndex");
5830 if (!NT_STATUS_IS_OK(status)) {
5831 return status;
5834 if ((r->in.level < 1) || (r->in.level > 3)) {
5835 DEBUG(0,("_samr_GetDisplayEnumerationIndex: "
5836 "Unknown info level (%u)\n",
5837 r->in.level));
5838 return NT_STATUS_INVALID_INFO_CLASS;
5841 become_root();
5843 /* The following done as ROOT. Don't return without unbecome_root(). */
5845 switch (r->in.level) {
5846 case 1:
5847 if (info->disp_info->users == NULL) {
5848 info->disp_info->users = pdb_search_users(ACB_NORMAL);
5849 if (info->disp_info->users == NULL) {
5850 unbecome_root();
5851 return NT_STATUS_ACCESS_DENIED;
5853 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5854 "starting user enumeration at index %u\n",
5855 (unsigned int)enum_context));
5856 } else {
5857 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5858 "using cached user enumeration at index %u\n",
5859 (unsigned int)enum_context));
5861 num_account = pdb_search_entries(info->disp_info->users,
5862 enum_context, max_entries,
5863 &entries);
5864 break;
5865 case 2:
5866 if (info->disp_info->machines == NULL) {
5867 info->disp_info->machines =
5868 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
5869 if (info->disp_info->machines == NULL) {
5870 unbecome_root();
5871 return NT_STATUS_ACCESS_DENIED;
5873 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5874 "starting machine enumeration at index %u\n",
5875 (unsigned int)enum_context));
5876 } else {
5877 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5878 "using cached machine enumeration at index %u\n",
5879 (unsigned int)enum_context));
5881 num_account = pdb_search_entries(info->disp_info->machines,
5882 enum_context, max_entries,
5883 &entries);
5884 break;
5885 case 3:
5886 if (info->disp_info->groups == NULL) {
5887 info->disp_info->groups = pdb_search_groups();
5888 if (info->disp_info->groups == NULL) {
5889 unbecome_root();
5890 return NT_STATUS_ACCESS_DENIED;
5892 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5893 "starting group enumeration at index %u\n",
5894 (unsigned int)enum_context));
5895 } else {
5896 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5897 "using cached group enumeration at index %u\n",
5898 (unsigned int)enum_context));
5900 num_account = pdb_search_entries(info->disp_info->groups,
5901 enum_context, max_entries,
5902 &entries);
5903 break;
5904 default:
5905 unbecome_root();
5906 smb_panic("info class changed");
5907 break;
5910 unbecome_root();
5912 /* Ensure we cache this enumeration. */
5913 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
5915 DEBUG(10,("_samr_GetDisplayEnumerationIndex: looking for :%s\n",
5916 r->in.name->string));
5918 for (i=0; i<num_account; i++) {
5919 if (strequal(entries[i].account_name, r->in.name->string)) {
5920 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5921 "found %s at idx %d\n",
5922 r->in.name->string, i));
5923 *r->out.idx = i;
5924 return NT_STATUS_OK;
5928 /* assuming account_name lives at the very end */
5929 *r->out.idx = num_account;
5931 return NT_STATUS_NO_MORE_ENTRIES;
5934 /****************************************************************
5935 _samr_GetDisplayEnumerationIndex2
5936 ****************************************************************/
5938 NTSTATUS _samr_GetDisplayEnumerationIndex2(pipes_struct *p,
5939 struct samr_GetDisplayEnumerationIndex2 *r)
5941 struct samr_GetDisplayEnumerationIndex q;
5943 q.in.domain_handle = r->in.domain_handle;
5944 q.in.level = r->in.level;
5945 q.in.name = r->in.name;
5947 q.out.idx = r->out.idx;
5949 return _samr_GetDisplayEnumerationIndex(p, &q);
5952 /****************************************************************
5953 ****************************************************************/
5955 NTSTATUS _samr_Shutdown(pipes_struct *p,
5956 struct samr_Shutdown *r)
5958 p->rng_fault_state = true;
5959 return NT_STATUS_NOT_IMPLEMENTED;
5962 /****************************************************************
5963 ****************************************************************/
5965 NTSTATUS _samr_SetMemberAttributesOfGroup(pipes_struct *p,
5966 struct samr_SetMemberAttributesOfGroup *r)
5968 p->rng_fault_state = true;
5969 return NT_STATUS_NOT_IMPLEMENTED;
5972 /****************************************************************
5973 ****************************************************************/
5975 NTSTATUS _samr_ChangePasswordUser(pipes_struct *p,
5976 struct samr_ChangePasswordUser *r)
5978 p->rng_fault_state = true;
5979 return NT_STATUS_NOT_IMPLEMENTED;
5982 /****************************************************************
5983 ****************************************************************/
5985 NTSTATUS _samr_TestPrivateFunctionsDomain(pipes_struct *p,
5986 struct samr_TestPrivateFunctionsDomain *r)
5988 p->rng_fault_state = true;
5989 return NT_STATUS_NOT_IMPLEMENTED;
5992 /****************************************************************
5993 ****************************************************************/
5995 NTSTATUS _samr_TestPrivateFunctionsUser(pipes_struct *p,
5996 struct samr_TestPrivateFunctionsUser *r)
5998 p->rng_fault_state = true;
5999 return NT_STATUS_NOT_IMPLEMENTED;
6002 /****************************************************************
6003 ****************************************************************/
6005 NTSTATUS _samr_AddMultipleMembersToAlias(pipes_struct *p,
6006 struct samr_AddMultipleMembersToAlias *r)
6008 p->rng_fault_state = true;
6009 return NT_STATUS_NOT_IMPLEMENTED;
6012 /****************************************************************
6013 ****************************************************************/
6015 NTSTATUS _samr_RemoveMultipleMembersFromAlias(pipes_struct *p,
6016 struct samr_RemoveMultipleMembersFromAlias *r)
6018 p->rng_fault_state = true;
6019 return NT_STATUS_NOT_IMPLEMENTED;
6022 /****************************************************************
6023 ****************************************************************/
6025 NTSTATUS _samr_OemChangePasswordUser2(pipes_struct *p,
6026 struct samr_OemChangePasswordUser2 *r)
6028 p->rng_fault_state = true;
6029 return NT_STATUS_NOT_IMPLEMENTED;
6032 /****************************************************************
6033 ****************************************************************/
6035 NTSTATUS _samr_SetBootKeyInformation(pipes_struct *p,
6036 struct samr_SetBootKeyInformation *r)
6038 p->rng_fault_state = true;
6039 return NT_STATUS_NOT_IMPLEMENTED;
6042 /****************************************************************
6043 ****************************************************************/
6045 NTSTATUS _samr_GetBootKeyInformation(pipes_struct *p,
6046 struct samr_GetBootKeyInformation *r)
6048 p->rng_fault_state = true;
6049 return NT_STATUS_NOT_IMPLEMENTED;
6052 /****************************************************************
6053 ****************************************************************/
6055 NTSTATUS _samr_Connect3(pipes_struct *p,
6056 struct samr_Connect3 *r)
6058 p->rng_fault_state = true;
6059 return NT_STATUS_NOT_IMPLEMENTED;
6062 /****************************************************************
6063 ****************************************************************/
6065 NTSTATUS _samr_RidToSid(pipes_struct *p,
6066 struct samr_RidToSid *r)
6068 p->rng_fault_state = true;
6069 return NT_STATUS_NOT_IMPLEMENTED;
6072 /****************************************************************
6073 ****************************************************************/
6075 NTSTATUS _samr_SetDsrmPassword(pipes_struct *p,
6076 struct samr_SetDsrmPassword *r)
6078 p->rng_fault_state = true;
6079 return NT_STATUS_NOT_IMPLEMENTED;
6082 /****************************************************************
6083 ****************************************************************/
6085 NTSTATUS _samr_ValidatePassword(pipes_struct *p,
6086 struct samr_ValidatePassword *r)
6088 p->rng_fault_state = true;
6089 return NT_STATUS_NOT_IMPLEMENTED;