s3-samr: Fix crash bug in _samr_QueryUserInfo{2} level 18.
[Samba/ekacnet.git] / source3 / rpc_server / srv_samr_nt.c
blob0b8e7a521c6310eeb0e9880347c63a1f5446ec8c
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"
35 #include "smbd/globals.h"
36 #include "../libcli/auth/libcli_auth.h"
37 #include "../librpc/gen_ndr/srv_samr.h"
38 #include "rpc_server/srv_samr_util.h"
39 #include "../lib/crypto/arcfour.h"
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_RPC_SRV
44 #define SAMR_USR_RIGHTS_WRITE_PW \
45 ( READ_CONTROL_ACCESS | \
46 SAMR_USER_ACCESS_CHANGE_PASSWORD | \
47 SAMR_USER_ACCESS_SET_LOC_COM)
48 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
49 ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
51 #define DISP_INFO_CACHE_TIMEOUT 10
53 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
54 #define MAX_SAM_ENTRIES_W95 50
56 struct samr_connect_info {
57 uint8_t dummy;
60 struct samr_domain_info {
61 struct dom_sid sid;
62 struct disp_info *disp_info;
65 struct samr_user_info {
66 struct dom_sid sid;
69 struct samr_group_info {
70 struct dom_sid sid;
73 struct samr_alias_info {
74 struct dom_sid sid;
77 typedef struct disp_info {
78 struct dom_sid sid; /* identify which domain this is. */
79 struct pdb_search *users; /* querydispinfo 1 and 4 */
80 struct pdb_search *machines; /* querydispinfo 2 */
81 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
82 struct pdb_search *aliases; /* enumaliases */
84 uint32_t enum_acb_mask;
85 struct pdb_search *enum_users; /* enumusers with a mask */
87 struct timed_event *cache_timeout_event; /* cache idle timeout
88 * handler. */
89 } DISP_INFO;
91 static const struct generic_mapping sam_generic_mapping = {
92 GENERIC_RIGHTS_SAM_READ,
93 GENERIC_RIGHTS_SAM_WRITE,
94 GENERIC_RIGHTS_SAM_EXECUTE,
95 GENERIC_RIGHTS_SAM_ALL_ACCESS};
96 static const struct generic_mapping dom_generic_mapping = {
97 GENERIC_RIGHTS_DOMAIN_READ,
98 GENERIC_RIGHTS_DOMAIN_WRITE,
99 GENERIC_RIGHTS_DOMAIN_EXECUTE,
100 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
101 static const struct generic_mapping usr_generic_mapping = {
102 GENERIC_RIGHTS_USER_READ,
103 GENERIC_RIGHTS_USER_WRITE,
104 GENERIC_RIGHTS_USER_EXECUTE,
105 GENERIC_RIGHTS_USER_ALL_ACCESS};
106 static const struct generic_mapping usr_nopwchange_generic_mapping = {
107 GENERIC_RIGHTS_USER_READ,
108 GENERIC_RIGHTS_USER_WRITE,
109 GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
110 GENERIC_RIGHTS_USER_ALL_ACCESS};
111 static const struct generic_mapping grp_generic_mapping = {
112 GENERIC_RIGHTS_GROUP_READ,
113 GENERIC_RIGHTS_GROUP_WRITE,
114 GENERIC_RIGHTS_GROUP_EXECUTE,
115 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
116 static const struct generic_mapping ali_generic_mapping = {
117 GENERIC_RIGHTS_ALIAS_READ,
118 GENERIC_RIGHTS_ALIAS_WRITE,
119 GENERIC_RIGHTS_ALIAS_EXECUTE,
120 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
122 /*******************************************************************
123 *******************************************************************/
125 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, struct security_descriptor **psd, size_t *sd_size,
126 const struct generic_mapping *map,
127 struct dom_sid *sid, uint32 sid_access )
129 struct dom_sid domadmin_sid;
130 struct security_ace ace[5]; /* at most 5 entries */
131 size_t i = 0;
133 struct security_acl *psa = NULL;
135 /* basic access for Everyone */
137 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
138 map->generic_execute | map->generic_read, 0);
140 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
142 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
143 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
144 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
145 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
147 /* Add Full Access for Domain Admins if we are a DC */
149 if ( IS_DC ) {
150 sid_compose(&domadmin_sid, get_global_sam_sid(),
151 DOMAIN_RID_ADMINS);
152 init_sec_ace(&ace[i++], &domadmin_sid,
153 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
156 /* if we have a sid, give it some special access */
158 if ( sid ) {
159 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sid_access, 0);
162 /* create the security descriptor */
164 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
165 return NT_STATUS_NO_MEMORY;
167 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
168 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
169 psa, sd_size)) == NULL)
170 return NT_STATUS_NO_MEMORY;
172 return NT_STATUS_OK;
175 /*******************************************************************
176 Checks if access to an object should be granted, and returns that
177 level of access for further checks.
178 ********************************************************************/
180 NTSTATUS access_check_object( struct security_descriptor *psd, NT_USER_TOKEN *token,
181 SE_PRIV *rights, uint32 rights_mask,
182 uint32 des_access, uint32 *acc_granted,
183 const char *debug )
185 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
186 uint32 saved_mask = 0;
188 /* check privileges; certain SAM access bits should be overridden
189 by privileges (mostly having to do with creating/modifying/deleting
190 users and groups) */
192 if (rights && !se_priv_equal(rights, &se_priv_none) &&
193 user_has_any_privilege(token, rights)) {
195 saved_mask = (des_access & rights_mask);
196 des_access &= ~saved_mask;
198 DEBUG(4,("access_check_object: user rights access mask [0x%x]\n",
199 rights_mask));
203 /* check the security descriptor first */
205 status = se_access_check(psd, token, des_access, acc_granted);
206 if (NT_STATUS_IS_OK(status)) {
207 goto done;
210 /* give root a free pass */
212 if ( geteuid() == sec_initial_uid() ) {
214 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
215 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
217 *acc_granted = des_access;
219 status = NT_STATUS_OK;
220 goto done;
224 done:
225 /* add in any bits saved during the privilege check (only
226 matters is status is ok) */
228 *acc_granted |= rights_mask;
230 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
231 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
232 des_access, *acc_granted));
234 return status;
238 /*******************************************************************
239 Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
240 ********************************************************************/
242 void map_max_allowed_access(const NT_USER_TOKEN *nt_token,
243 const struct unix_user_token *unix_token,
244 uint32_t *pacc_requested)
246 if (!((*pacc_requested) & MAXIMUM_ALLOWED_ACCESS)) {
247 return;
249 *pacc_requested &= ~MAXIMUM_ALLOWED_ACCESS;
251 /* At least try for generic read|execute - Everyone gets that. */
252 *pacc_requested = GENERIC_READ_ACCESS|GENERIC_EXECUTE_ACCESS;
254 /* root gets anything. */
255 if (unix_token->uid == sec_initial_uid()) {
256 *pacc_requested |= GENERIC_ALL_ACCESS;
257 return;
260 /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
262 if (is_sid_in_token(nt_token, &global_sid_Builtin_Administrators) ||
263 is_sid_in_token(nt_token, &global_sid_Builtin_Account_Operators)) {
264 *pacc_requested |= GENERIC_ALL_ACCESS;
265 return;
268 /* Full access for DOMAIN\Domain Admins. */
269 if ( IS_DC ) {
270 struct dom_sid domadmin_sid;
271 sid_compose(&domadmin_sid, get_global_sam_sid(),
272 DOMAIN_RID_ADMINS);
273 if (is_sid_in_token(nt_token, &domadmin_sid)) {
274 *pacc_requested |= GENERIC_ALL_ACCESS;
275 return;
278 /* TODO ! Check privileges. */
281 /*******************************************************************
282 Fetch or create a dispinfo struct.
283 ********************************************************************/
285 static DISP_INFO *get_samr_dispinfo_by_sid(const struct dom_sid *psid)
288 * We do a static cache for DISP_INFO's here. Explanation can be found
289 * in Jeremy's checkin message to r11793:
291 * Fix the SAMR cache so it works across completely insane
292 * client behaviour (ie.:
293 * open pipe/open SAMR handle/enumerate 0 - 1024
294 * close SAMR handle, close pipe.
295 * open pipe/open SAMR handle/enumerate 1024 - 2048...
296 * close SAMR handle, close pipe.
297 * And on ad-nausium. Amazing.... probably object-oriented
298 * client side programming in action yet again.
299 * This change should *massively* improve performance when
300 * enumerating users from an LDAP database.
301 * Jeremy.
303 * "Our" and the builtin domain are the only ones where we ever
304 * enumerate stuff, so just cache 2 entries.
307 static struct disp_info *builtin_dispinfo;
308 static struct disp_info *domain_dispinfo;
310 /* There are two cases to consider here:
311 1) The SID is a domain SID and we look for an equality match, or
312 2) This is an account SID and so we return the DISP_INFO* for our
313 domain */
315 if (psid == NULL) {
316 return NULL;
319 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
321 * Necessary only once, but it does not really hurt.
323 if (builtin_dispinfo == NULL) {
324 builtin_dispinfo = talloc_zero(
325 talloc_autofree_context(), struct disp_info);
326 if (builtin_dispinfo == NULL) {
327 return NULL;
330 sid_copy(&builtin_dispinfo->sid, &global_sid_Builtin);
332 return builtin_dispinfo;
335 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
337 * Necessary only once, but it does not really hurt.
339 if (domain_dispinfo == NULL) {
340 domain_dispinfo = talloc_zero(
341 talloc_autofree_context(), struct disp_info);
342 if (domain_dispinfo == NULL) {
343 return NULL;
346 sid_copy(&domain_dispinfo->sid, get_global_sam_sid());
348 return domain_dispinfo;
351 return NULL;
354 /*******************************************************************
355 Function to free the per SID data.
356 ********************************************************************/
358 static void free_samr_cache(DISP_INFO *disp_info)
360 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
361 sid_string_dbg(&disp_info->sid)));
363 /* We need to become root here because the paged search might have to
364 * tell the LDAP server we're not interested in the rest anymore. */
366 become_root();
368 TALLOC_FREE(disp_info->users);
369 TALLOC_FREE(disp_info->machines);
370 TALLOC_FREE(disp_info->groups);
371 TALLOC_FREE(disp_info->aliases);
372 TALLOC_FREE(disp_info->enum_users);
374 unbecome_root();
377 /*******************************************************************
378 Idle event handler. Throw away the disp info cache.
379 ********************************************************************/
381 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
382 struct timed_event *te,
383 struct timeval now,
384 void *private_data)
386 DISP_INFO *disp_info = (DISP_INFO *)private_data;
388 TALLOC_FREE(disp_info->cache_timeout_event);
390 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
391 "out\n"));
392 free_samr_cache(disp_info);
395 /*******************************************************************
396 Setup cache removal idle event handler.
397 ********************************************************************/
399 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
401 /* Remove any pending timeout and update. */
403 TALLOC_FREE(disp_info->cache_timeout_event);
405 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
406 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
407 (unsigned int)secs_fromnow ));
409 disp_info->cache_timeout_event = event_add_timed(
410 smbd_event_context(), NULL,
411 timeval_current_ofs(secs_fromnow, 0),
412 disp_info_cache_idle_timeout_handler, (void *)disp_info);
415 /*******************************************************************
416 Force flush any cache. We do this on any samr_set_xxx call.
417 We must also remove the timeout handler.
418 ********************************************************************/
420 static void force_flush_samr_cache(const struct dom_sid *sid)
422 struct disp_info *disp_info = get_samr_dispinfo_by_sid(sid);
424 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
425 return;
428 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
429 TALLOC_FREE(disp_info->cache_timeout_event);
430 free_samr_cache(disp_info);
433 /*******************************************************************
434 Ensure password info is never given out. Paranioa... JRA.
435 ********************************************************************/
437 static void samr_clear_sam_passwd(struct samu *sam_pass)
440 if (!sam_pass)
441 return;
443 /* These now zero out the old password */
445 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
446 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
449 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
451 struct samr_displayentry *entry;
453 if (sid_check_is_builtin(&info->sid)) {
454 /* No users in builtin. */
455 return 0;
458 if (info->users == NULL) {
459 info->users = pdb_search_users(info, acct_flags);
460 if (info->users == NULL) {
461 return 0;
464 /* Fetch the last possible entry, thus trigger an enumeration */
465 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
467 /* Ensure we cache this enumeration. */
468 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
470 return info->users->num_entries;
473 static uint32 count_sam_groups(struct disp_info *info)
475 struct samr_displayentry *entry;
477 if (sid_check_is_builtin(&info->sid)) {
478 /* No groups in builtin. */
479 return 0;
482 if (info->groups == NULL) {
483 info->groups = pdb_search_groups(info);
484 if (info->groups == NULL) {
485 return 0;
488 /* Fetch the last possible entry, thus trigger an enumeration */
489 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
491 /* Ensure we cache this enumeration. */
492 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
494 return info->groups->num_entries;
497 static uint32 count_sam_aliases(struct disp_info *info)
499 struct samr_displayentry *entry;
501 if (info->aliases == NULL) {
502 info->aliases = pdb_search_aliases(info, &info->sid);
503 if (info->aliases == NULL) {
504 return 0;
507 /* Fetch the last possible entry, thus trigger an enumeration */
508 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
510 /* Ensure we cache this enumeration. */
511 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
513 return info->aliases->num_entries;
516 /*******************************************************************
517 _samr_Close
518 ********************************************************************/
520 NTSTATUS _samr_Close(pipes_struct *p, struct samr_Close *r)
522 if (!close_policy_hnd(p, r->in.handle)) {
523 return NT_STATUS_INVALID_HANDLE;
526 ZERO_STRUCTP(r->out.handle);
528 return NT_STATUS_OK;
531 /*******************************************************************
532 _samr_OpenDomain
533 ********************************************************************/
535 NTSTATUS _samr_OpenDomain(pipes_struct *p,
536 struct samr_OpenDomain *r)
538 struct samr_connect_info *cinfo;
539 struct samr_domain_info *dinfo;
540 struct security_descriptor *psd = NULL;
541 uint32 acc_granted;
542 uint32 des_access = r->in.access_mask;
543 NTSTATUS status;
544 size_t sd_size;
545 uint32_t extra_access = SAMR_DOMAIN_ACCESS_CREATE_USER;
546 SE_PRIV se_rights;
548 /* find the connection policy handle. */
550 cinfo = policy_handle_find(p, r->in.connect_handle, 0, NULL,
551 struct samr_connect_info, &status);
552 if (!NT_STATUS_IS_OK(status)) {
553 return status;
556 /*check if access can be granted as requested by client. */
557 map_max_allowed_access(p->server_info->ptok,
558 &p->server_info->utok,
559 &des_access);
561 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
562 se_map_generic( &des_access, &dom_generic_mapping );
565 * Users with SeMachineAccount or SeAddUser get additional
566 * SAMR_DOMAIN_ACCESS_CREATE_USER access.
568 se_priv_copy( &se_rights, &se_machine_account );
569 se_priv_add( &se_rights, &se_add_users );
572 * Users with SeAddUser get the ability to manipulate groups
573 * and aliases.
575 if (user_has_any_privilege(p->server_info->ptok, &se_add_users)) {
576 extra_access |= (SAMR_DOMAIN_ACCESS_CREATE_GROUP |
577 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS |
578 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT |
579 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS |
580 SAMR_DOMAIN_ACCESS_CREATE_ALIAS);
583 status = access_check_object( psd, p->server_info->ptok,
584 &se_rights, extra_access, des_access,
585 &acc_granted, "_samr_OpenDomain" );
587 if ( !NT_STATUS_IS_OK(status) )
588 return status;
590 if (!sid_check_is_domain(r->in.sid) &&
591 !sid_check_is_builtin(r->in.sid)) {
592 return NT_STATUS_NO_SUCH_DOMAIN;
595 dinfo = policy_handle_create(p, r->out.domain_handle, acc_granted,
596 struct samr_domain_info, &status);
597 if (!NT_STATUS_IS_OK(status)) {
598 return status;
600 dinfo->sid = *r->in.sid;
601 dinfo->disp_info = get_samr_dispinfo_by_sid(r->in.sid);
603 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
605 return NT_STATUS_OK;
608 /*******************************************************************
609 _samr_GetUserPwInfo
610 ********************************************************************/
612 NTSTATUS _samr_GetUserPwInfo(pipes_struct *p,
613 struct samr_GetUserPwInfo *r)
615 struct samr_user_info *uinfo;
616 enum lsa_SidType sid_type;
617 uint32_t min_password_length = 0;
618 uint32_t password_properties = 0;
619 bool ret = false;
620 NTSTATUS status;
622 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
624 uinfo = policy_handle_find(p, r->in.user_handle,
625 SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
626 struct samr_user_info, &status);
627 if (!NT_STATUS_IS_OK(status)) {
628 return status;
631 if (!sid_check_is_in_our_domain(&uinfo->sid)) {
632 return NT_STATUS_OBJECT_TYPE_MISMATCH;
635 become_root();
636 ret = lookup_sid(p->mem_ctx, &uinfo->sid, NULL, NULL, &sid_type);
637 unbecome_root();
638 if (ret == false) {
639 return NT_STATUS_NO_SUCH_USER;
642 switch (sid_type) {
643 case SID_NAME_USER:
644 become_root();
645 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
646 &min_password_length);
647 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
648 &password_properties);
649 unbecome_root();
651 if (lp_check_password_script() && *lp_check_password_script()) {
652 password_properties |= DOMAIN_PASSWORD_COMPLEX;
655 break;
656 default:
657 break;
660 r->out.info->min_password_length = min_password_length;
661 r->out.info->password_properties = password_properties;
663 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
665 return NT_STATUS_OK;
668 /*******************************************************************
669 _samr_SetSecurity
670 ********************************************************************/
672 NTSTATUS _samr_SetSecurity(pipes_struct *p,
673 struct samr_SetSecurity *r)
675 struct samr_user_info *uinfo;
676 uint32 i;
677 struct security_acl *dacl;
678 bool ret;
679 struct samu *sampass=NULL;
680 NTSTATUS status;
682 uinfo = policy_handle_find(p, r->in.handle,
683 SAMR_USER_ACCESS_SET_ATTRIBUTES, NULL,
684 struct samr_user_info, &status);
685 if (!NT_STATUS_IS_OK(status)) {
686 return status;
689 if (!(sampass = samu_new( p->mem_ctx))) {
690 DEBUG(0,("No memory!\n"));
691 return NT_STATUS_NO_MEMORY;
694 /* get the user record */
695 become_root();
696 ret = pdb_getsampwsid(sampass, &uinfo->sid);
697 unbecome_root();
699 if (!ret) {
700 DEBUG(4, ("User %s not found\n",
701 sid_string_dbg(&uinfo->sid)));
702 TALLOC_FREE(sampass);
703 return NT_STATUS_INVALID_HANDLE;
706 dacl = r->in.sdbuf->sd->dacl;
707 for (i=0; i < dacl->num_aces; i++) {
708 if (sid_equal(&uinfo->sid, &dacl->aces[i].trustee)) {
709 ret = pdb_set_pass_can_change(sampass,
710 (dacl->aces[i].access_mask &
711 SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
712 True: False);
713 break;
717 if (!ret) {
718 TALLOC_FREE(sampass);
719 return NT_STATUS_ACCESS_DENIED;
722 become_root();
723 status = pdb_update_sam_account(sampass);
724 unbecome_root();
726 TALLOC_FREE(sampass);
728 return status;
731 /*******************************************************************
732 build correct perms based on policies and password times for _samr_query_sec_obj
733 *******************************************************************/
734 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, struct dom_sid *user_sid)
736 struct samu *sampass=NULL;
737 bool ret;
739 if ( !(sampass = samu_new( mem_ctx )) ) {
740 DEBUG(0,("No memory!\n"));
741 return False;
744 become_root();
745 ret = pdb_getsampwsid(sampass, user_sid);
746 unbecome_root();
748 if (ret == False) {
749 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
750 TALLOC_FREE(sampass);
751 return False;
754 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
756 if (pdb_get_pass_can_change(sampass)) {
757 TALLOC_FREE(sampass);
758 return True;
760 TALLOC_FREE(sampass);
761 return False;
765 /*******************************************************************
766 _samr_QuerySecurity
767 ********************************************************************/
769 NTSTATUS _samr_QuerySecurity(pipes_struct *p,
770 struct samr_QuerySecurity *r)
772 struct samr_connect_info *cinfo;
773 struct samr_domain_info *dinfo;
774 struct samr_user_info *uinfo;
775 struct samr_group_info *ginfo;
776 struct samr_alias_info *ainfo;
777 NTSTATUS status;
778 struct security_descriptor * psd = NULL;
779 size_t sd_size = 0;
781 cinfo = policy_handle_find(p, r->in.handle,
782 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
783 struct samr_connect_info, &status);
784 if (NT_STATUS_IS_OK(status)) {
785 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
786 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
787 &sam_generic_mapping, NULL, 0);
788 goto done;
791 dinfo = policy_handle_find(p, r->in.handle,
792 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
793 struct samr_domain_info, &status);
794 if (NT_STATUS_IS_OK(status)) {
795 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
796 "with SID: %s\n", sid_string_dbg(&dinfo->sid)));
798 * TODO: Builtin probably needs a different SD with restricted
799 * write access
801 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
802 &dom_generic_mapping, NULL, 0);
803 goto done;
806 uinfo = policy_handle_find(p, r->in.handle,
807 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
808 struct samr_user_info, &status);
809 if (NT_STATUS_IS_OK(status)) {
810 DEBUG(10,("_samr_QuerySecurity: querying security on user "
811 "Object with SID: %s\n",
812 sid_string_dbg(&uinfo->sid)));
813 if (check_change_pw_access(p->mem_ctx, &uinfo->sid)) {
814 status = make_samr_object_sd(
815 p->mem_ctx, &psd, &sd_size,
816 &usr_generic_mapping,
817 &uinfo->sid, SAMR_USR_RIGHTS_WRITE_PW);
818 } else {
819 status = make_samr_object_sd(
820 p->mem_ctx, &psd, &sd_size,
821 &usr_nopwchange_generic_mapping,
822 &uinfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
824 goto done;
827 ginfo = policy_handle_find(p, r->in.handle,
828 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
829 struct samr_group_info, &status);
830 if (NT_STATUS_IS_OK(status)) {
832 * TODO: different SDs have to be generated for aliases groups
833 * and users. Currently all three get a default user SD
835 DEBUG(10,("_samr_QuerySecurity: querying security on group "
836 "Object with SID: %s\n",
837 sid_string_dbg(&ginfo->sid)));
838 status = make_samr_object_sd(
839 p->mem_ctx, &psd, &sd_size,
840 &usr_nopwchange_generic_mapping,
841 &ginfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
842 goto done;
845 ainfo = policy_handle_find(p, r->in.handle,
846 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
847 struct samr_alias_info, &status);
848 if (NT_STATUS_IS_OK(status)) {
850 * TODO: different SDs have to be generated for aliases groups
851 * and users. Currently all three get a default user SD
853 DEBUG(10,("_samr_QuerySecurity: querying security on alias "
854 "Object with SID: %s\n",
855 sid_string_dbg(&ainfo->sid)));
856 status = make_samr_object_sd(
857 p->mem_ctx, &psd, &sd_size,
858 &usr_nopwchange_generic_mapping,
859 &ainfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
860 goto done;
863 return NT_STATUS_OBJECT_TYPE_MISMATCH;
864 done:
865 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
866 return NT_STATUS_NO_MEMORY;
868 return status;
871 /*******************************************************************
872 makes a SAM_ENTRY / UNISTR2* structure from a user list.
873 ********************************************************************/
875 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
876 struct samr_SamEntry **sam_pp,
877 uint32_t num_entries,
878 uint32_t start_idx,
879 struct samr_displayentry *entries)
881 uint32_t i;
882 struct samr_SamEntry *sam;
884 *sam_pp = NULL;
886 if (num_entries == 0) {
887 return NT_STATUS_OK;
890 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
891 if (sam == NULL) {
892 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
893 return NT_STATUS_NO_MEMORY;
896 for (i = 0; i < num_entries; i++) {
897 #if 0
899 * usrmgr expects a non-NULL terminated string with
900 * trust relationships
902 if (entries[i].acct_flags & ACB_DOMTRUST) {
903 init_unistr2(&uni_temp_name, entries[i].account_name,
904 UNI_FLAGS_NONE);
905 } else {
906 init_unistr2(&uni_temp_name, entries[i].account_name,
907 UNI_STR_TERMINATE);
909 #endif
910 init_lsa_String(&sam[i].name, entries[i].account_name);
911 sam[i].idx = entries[i].rid;
914 *sam_pp = sam;
916 return NT_STATUS_OK;
919 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
921 /*******************************************************************
922 _samr_EnumDomainUsers
923 ********************************************************************/
925 NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
926 struct samr_EnumDomainUsers *r)
928 NTSTATUS status;
929 struct samr_domain_info *dinfo;
930 int num_account;
931 uint32 enum_context = *r->in.resume_handle;
932 enum remote_arch_types ra_type = get_remote_arch();
933 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
934 uint32 max_entries = max_sam_entries;
935 struct samr_displayentry *entries = NULL;
936 struct samr_SamArray *samr_array = NULL;
937 struct samr_SamEntry *samr_entries = NULL;
939 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
941 dinfo = policy_handle_find(p, r->in.domain_handle,
942 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
943 struct samr_domain_info, &status);
944 if (!NT_STATUS_IS_OK(status)) {
945 return status;
948 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
949 if (!samr_array) {
950 return NT_STATUS_NO_MEMORY;
952 *r->out.sam = samr_array;
954 if (sid_check_is_builtin(&dinfo->sid)) {
955 /* No users in builtin. */
956 *r->out.resume_handle = *r->in.resume_handle;
957 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
958 return status;
961 become_root();
963 /* AS ROOT !!!! */
965 if ((dinfo->disp_info->enum_users != NULL) &&
966 (dinfo->disp_info->enum_acb_mask != r->in.acct_flags)) {
967 TALLOC_FREE(dinfo->disp_info->enum_users);
970 if (dinfo->disp_info->enum_users == NULL) {
971 dinfo->disp_info->enum_users = pdb_search_users(
972 dinfo->disp_info, r->in.acct_flags);
973 dinfo->disp_info->enum_acb_mask = r->in.acct_flags;
976 if (dinfo->disp_info->enum_users == NULL) {
977 /* END AS ROOT !!!! */
978 unbecome_root();
979 return NT_STATUS_ACCESS_DENIED;
982 num_account = pdb_search_entries(dinfo->disp_info->enum_users,
983 enum_context, max_entries,
984 &entries);
986 /* END AS ROOT !!!! */
988 unbecome_root();
990 if (num_account == 0) {
991 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
992 "total entries\n"));
993 *r->out.resume_handle = *r->in.resume_handle;
994 return NT_STATUS_OK;
997 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
998 num_account, enum_context,
999 entries);
1000 if (!NT_STATUS_IS_OK(status)) {
1001 return status;
1004 if (max_entries <= num_account) {
1005 status = STATUS_MORE_ENTRIES;
1006 } else {
1007 status = NT_STATUS_OK;
1010 /* Ensure we cache this enumeration. */
1011 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1013 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
1015 samr_array->count = num_account;
1016 samr_array->entries = samr_entries;
1018 *r->out.resume_handle = *r->in.resume_handle + num_account;
1019 *r->out.num_entries = num_account;
1021 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1023 return status;
1026 /*******************************************************************
1027 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1028 ********************************************************************/
1030 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
1031 struct samr_SamEntry **sam_pp,
1032 uint32_t num_sam_entries,
1033 struct samr_displayentry *entries)
1035 struct samr_SamEntry *sam;
1036 uint32_t i;
1038 *sam_pp = NULL;
1040 if (num_sam_entries == 0) {
1041 return;
1044 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1045 if (sam == NULL) {
1046 return;
1049 for (i = 0; i < num_sam_entries; i++) {
1051 * JRA. I think this should include the null. TNG does not.
1053 init_lsa_String(&sam[i].name, entries[i].account_name);
1054 sam[i].idx = entries[i].rid;
1057 *sam_pp = sam;
1060 /*******************************************************************
1061 _samr_EnumDomainGroups
1062 ********************************************************************/
1064 NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
1065 struct samr_EnumDomainGroups *r)
1067 NTSTATUS status;
1068 struct samr_domain_info *dinfo;
1069 struct samr_displayentry *groups;
1070 uint32 num_groups;
1071 struct samr_SamArray *samr_array = NULL;
1072 struct samr_SamEntry *samr_entries = NULL;
1074 dinfo = policy_handle_find(p, r->in.domain_handle,
1075 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1076 struct samr_domain_info, &status);
1077 if (!NT_STATUS_IS_OK(status)) {
1078 return status;
1081 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1083 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1084 if (!samr_array) {
1085 return NT_STATUS_NO_MEMORY;
1087 *r->out.sam = samr_array;
1089 if (sid_check_is_builtin(&dinfo->sid)) {
1090 /* No groups in builtin. */
1091 *r->out.resume_handle = *r->in.resume_handle;
1092 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1093 return status;
1096 /* the domain group array is being allocated in the function below */
1098 become_root();
1100 if (dinfo->disp_info->groups == NULL) {
1101 dinfo->disp_info->groups = pdb_search_groups(dinfo->disp_info);
1103 if (dinfo->disp_info->groups == NULL) {
1104 unbecome_root();
1105 return NT_STATUS_ACCESS_DENIED;
1109 num_groups = pdb_search_entries(dinfo->disp_info->groups,
1110 *r->in.resume_handle,
1111 MAX_SAM_ENTRIES, &groups);
1112 unbecome_root();
1114 /* Ensure we cache this enumeration. */
1115 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1117 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1118 num_groups, groups);
1120 if (MAX_SAM_ENTRIES <= num_groups) {
1121 status = STATUS_MORE_ENTRIES;
1122 } else {
1123 status = NT_STATUS_OK;
1126 samr_array->count = num_groups;
1127 samr_array->entries = samr_entries;
1129 *r->out.num_entries = num_groups;
1130 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1132 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1134 return status;
1137 /*******************************************************************
1138 _samr_EnumDomainAliases
1139 ********************************************************************/
1141 NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
1142 struct samr_EnumDomainAliases *r)
1144 NTSTATUS status;
1145 struct samr_domain_info *dinfo;
1146 struct samr_displayentry *aliases;
1147 uint32 num_aliases = 0;
1148 struct samr_SamArray *samr_array = NULL;
1149 struct samr_SamEntry *samr_entries = NULL;
1151 dinfo = policy_handle_find(p, r->in.domain_handle,
1152 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1153 struct samr_domain_info, &status);
1154 if (!NT_STATUS_IS_OK(status)) {
1155 return status;
1158 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1159 sid_string_dbg(&dinfo->sid)));
1161 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1162 if (!samr_array) {
1163 return NT_STATUS_NO_MEMORY;
1166 become_root();
1168 if (dinfo->disp_info->aliases == NULL) {
1169 dinfo->disp_info->aliases = pdb_search_aliases(
1170 dinfo->disp_info, &dinfo->sid);
1171 if (dinfo->disp_info->aliases == NULL) {
1172 unbecome_root();
1173 return NT_STATUS_ACCESS_DENIED;
1177 num_aliases = pdb_search_entries(dinfo->disp_info->aliases,
1178 *r->in.resume_handle,
1179 MAX_SAM_ENTRIES, &aliases);
1180 unbecome_root();
1182 /* Ensure we cache this enumeration. */
1183 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1185 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1186 num_aliases, aliases);
1188 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1190 if (MAX_SAM_ENTRIES <= num_aliases) {
1191 status = STATUS_MORE_ENTRIES;
1192 } else {
1193 status = NT_STATUS_OK;
1196 samr_array->count = num_aliases;
1197 samr_array->entries = samr_entries;
1199 *r->out.sam = samr_array;
1200 *r->out.num_entries = num_aliases;
1201 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1203 return status;
1206 /*******************************************************************
1207 inits a samr_DispInfoGeneral structure.
1208 ********************************************************************/
1210 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1211 struct samr_DispInfoGeneral *r,
1212 uint32_t num_entries,
1213 uint32_t start_idx,
1214 struct samr_displayentry *entries)
1216 uint32 i;
1218 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1220 if (num_entries == 0) {
1221 return NT_STATUS_OK;
1224 r->count = num_entries;
1226 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1227 if (!r->entries) {
1228 return NT_STATUS_NO_MEMORY;
1231 for (i = 0; i < num_entries ; i++) {
1233 init_lsa_String(&r->entries[i].account_name,
1234 entries[i].account_name);
1236 init_lsa_String(&r->entries[i].description,
1237 entries[i].description);
1239 init_lsa_String(&r->entries[i].full_name,
1240 entries[i].fullname);
1242 r->entries[i].rid = entries[i].rid;
1243 r->entries[i].acct_flags = entries[i].acct_flags;
1244 r->entries[i].idx = start_idx+i+1;
1247 return NT_STATUS_OK;
1250 /*******************************************************************
1251 inits a samr_DispInfoFull structure.
1252 ********************************************************************/
1254 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1255 struct samr_DispInfoFull *r,
1256 uint32_t num_entries,
1257 uint32_t start_idx,
1258 struct samr_displayentry *entries)
1260 uint32_t i;
1262 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1264 if (num_entries == 0) {
1265 return NT_STATUS_OK;
1268 r->count = num_entries;
1270 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFull, num_entries);
1271 if (!r->entries) {
1272 return NT_STATUS_NO_MEMORY;
1275 for (i = 0; i < num_entries ; i++) {
1277 init_lsa_String(&r->entries[i].account_name,
1278 entries[i].account_name);
1280 init_lsa_String(&r->entries[i].description,
1281 entries[i].description);
1283 r->entries[i].rid = entries[i].rid;
1284 r->entries[i].acct_flags = entries[i].acct_flags;
1285 r->entries[i].idx = start_idx+i+1;
1288 return NT_STATUS_OK;
1291 /*******************************************************************
1292 inits a samr_DispInfoFullGroups structure.
1293 ********************************************************************/
1295 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1296 struct samr_DispInfoFullGroups *r,
1297 uint32_t num_entries,
1298 uint32_t start_idx,
1299 struct samr_displayentry *entries)
1301 uint32_t i;
1303 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1305 if (num_entries == 0) {
1306 return NT_STATUS_OK;
1309 r->count = num_entries;
1311 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1312 if (!r->entries) {
1313 return NT_STATUS_NO_MEMORY;
1316 for (i = 0; i < num_entries ; i++) {
1318 init_lsa_String(&r->entries[i].account_name,
1319 entries[i].account_name);
1321 init_lsa_String(&r->entries[i].description,
1322 entries[i].description);
1324 r->entries[i].rid = entries[i].rid;
1325 r->entries[i].acct_flags = entries[i].acct_flags;
1326 r->entries[i].idx = start_idx+i+1;
1329 return NT_STATUS_OK;
1332 /*******************************************************************
1333 inits a samr_DispInfoAscii structure.
1334 ********************************************************************/
1336 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1337 struct samr_DispInfoAscii *r,
1338 uint32_t num_entries,
1339 uint32_t start_idx,
1340 struct samr_displayentry *entries)
1342 uint32_t i;
1344 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1346 if (num_entries == 0) {
1347 return NT_STATUS_OK;
1350 r->count = num_entries;
1352 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1353 if (!r->entries) {
1354 return NT_STATUS_NO_MEMORY;
1357 for (i = 0; i < num_entries ; i++) {
1359 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1360 entries[i].account_name);
1362 r->entries[i].idx = start_idx+i+1;
1365 return NT_STATUS_OK;
1368 /*******************************************************************
1369 inits a samr_DispInfoAscii structure.
1370 ********************************************************************/
1372 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1373 struct samr_DispInfoAscii *r,
1374 uint32_t num_entries,
1375 uint32_t start_idx,
1376 struct samr_displayentry *entries)
1378 uint32_t i;
1380 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1382 if (num_entries == 0) {
1383 return NT_STATUS_OK;
1386 r->count = num_entries;
1388 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1389 if (!r->entries) {
1390 return NT_STATUS_NO_MEMORY;
1393 for (i = 0; i < num_entries ; i++) {
1395 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1396 entries[i].account_name);
1398 r->entries[i].idx = start_idx+i+1;
1401 return NT_STATUS_OK;
1404 /*******************************************************************
1405 _samr_QueryDisplayInfo
1406 ********************************************************************/
1408 NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
1409 struct samr_QueryDisplayInfo *r)
1411 NTSTATUS status;
1412 struct samr_domain_info *dinfo;
1413 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1415 uint32 max_entries = r->in.max_entries;
1417 union samr_DispInfo *disp_info = r->out.info;
1419 uint32 temp_size=0;
1420 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1421 uint32 num_account = 0;
1422 enum remote_arch_types ra_type = get_remote_arch();
1423 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1424 struct samr_displayentry *entries = NULL;
1426 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1428 dinfo = policy_handle_find(p, r->in.domain_handle,
1429 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1430 struct samr_domain_info, &status);
1431 if (!NT_STATUS_IS_OK(status)) {
1432 return status;
1435 if (sid_check_is_builtin(&dinfo->sid)) {
1436 DEBUG(5,("_samr_QueryDisplayInfo: no users in BUILTIN\n"));
1437 return NT_STATUS_OK;
1441 * calculate how many entries we will return.
1442 * based on
1443 * - the number of entries the client asked
1444 * - our limit on that
1445 * - the starting point (enumeration context)
1446 * - the buffer size the client will accept
1450 * We are a lot more like W2K. Instead of reading the SAM
1451 * each time to find the records we need to send back,
1452 * we read it once and link that copy to the sam handle.
1453 * For large user list (over the MAX_SAM_ENTRIES)
1454 * it's a definitive win.
1455 * second point to notice: between enumerations
1456 * our sam is now the same as it's a snapshoot.
1457 * third point: got rid of the static SAM_USER_21 struct
1458 * no more intermediate.
1459 * con: it uses much more memory, as a full copy is stored
1460 * in memory.
1462 * If you want to change it, think twice and think
1463 * of the second point , that's really important.
1465 * JFM, 12/20/2001
1468 if ((r->in.level < 1) || (r->in.level > 5)) {
1469 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1470 (unsigned int)r->in.level ));
1471 return NT_STATUS_INVALID_INFO_CLASS;
1474 /* first limit the number of entries we will return */
1475 if (r->in.max_entries > max_sam_entries) {
1476 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1477 "entries, limiting to %d\n", r->in.max_entries,
1478 max_sam_entries));
1479 max_entries = max_sam_entries;
1482 /* calculate the size and limit on the number of entries we will
1483 * return */
1485 temp_size=max_entries*struct_size;
1487 if (temp_size > r->in.buf_size) {
1488 max_entries = MIN((r->in.buf_size / struct_size),max_entries);;
1489 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1490 "only %d entries\n", max_entries));
1493 become_root();
1495 /* THe following done as ROOT. Don't return without unbecome_root(). */
1497 switch (r->in.level) {
1498 case 1:
1499 case 4:
1500 if (dinfo->disp_info->users == NULL) {
1501 dinfo->disp_info->users = pdb_search_users(
1502 dinfo->disp_info, ACB_NORMAL);
1503 if (dinfo->disp_info->users == NULL) {
1504 unbecome_root();
1505 return NT_STATUS_ACCESS_DENIED;
1507 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1508 (unsigned int)r->in.start_idx));
1509 } else {
1510 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1511 (unsigned int)r->in.start_idx));
1514 num_account = pdb_search_entries(dinfo->disp_info->users,
1515 r->in.start_idx, max_entries,
1516 &entries);
1517 break;
1518 case 2:
1519 if (dinfo->disp_info->machines == NULL) {
1520 dinfo->disp_info->machines = pdb_search_users(
1521 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
1522 if (dinfo->disp_info->machines == NULL) {
1523 unbecome_root();
1524 return NT_STATUS_ACCESS_DENIED;
1526 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1527 (unsigned int)r->in.start_idx));
1528 } else {
1529 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1530 (unsigned int)r->in.start_idx));
1533 num_account = pdb_search_entries(dinfo->disp_info->machines,
1534 r->in.start_idx, max_entries,
1535 &entries);
1536 break;
1537 case 3:
1538 case 5:
1539 if (dinfo->disp_info->groups == NULL) {
1540 dinfo->disp_info->groups = pdb_search_groups(
1541 dinfo->disp_info);
1542 if (dinfo->disp_info->groups == NULL) {
1543 unbecome_root();
1544 return NT_STATUS_ACCESS_DENIED;
1546 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1547 (unsigned int)r->in.start_idx));
1548 } else {
1549 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1550 (unsigned int)r->in.start_idx));
1553 num_account = pdb_search_entries(dinfo->disp_info->groups,
1554 r->in.start_idx, max_entries,
1555 &entries);
1556 break;
1557 default:
1558 unbecome_root();
1559 smb_panic("info class changed");
1560 break;
1562 unbecome_root();
1565 /* Now create reply structure */
1566 switch (r->in.level) {
1567 case 1:
1568 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1569 num_account, r->in.start_idx,
1570 entries);
1571 break;
1572 case 2:
1573 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1574 num_account, r->in.start_idx,
1575 entries);
1576 break;
1577 case 3:
1578 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1579 num_account, r->in.start_idx,
1580 entries);
1581 break;
1582 case 4:
1583 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1584 num_account, r->in.start_idx,
1585 entries);
1586 break;
1587 case 5:
1588 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1589 num_account, r->in.start_idx,
1590 entries);
1591 break;
1592 default:
1593 smb_panic("info class changed");
1594 break;
1597 if (!NT_STATUS_IS_OK(disp_ret))
1598 return disp_ret;
1600 if (max_entries <= num_account) {
1601 status = STATUS_MORE_ENTRIES;
1602 } else {
1603 status = NT_STATUS_OK;
1606 /* Ensure we cache this enumeration. */
1607 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1609 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1611 *r->out.total_size = num_account * struct_size;
1612 *r->out.returned_size = num_account ? temp_size : 0;
1614 return status;
1617 /****************************************************************
1618 _samr_QueryDisplayInfo2
1619 ****************************************************************/
1621 NTSTATUS _samr_QueryDisplayInfo2(pipes_struct *p,
1622 struct samr_QueryDisplayInfo2 *r)
1624 struct samr_QueryDisplayInfo q;
1626 q.in.domain_handle = r->in.domain_handle;
1627 q.in.level = r->in.level;
1628 q.in.start_idx = r->in.start_idx;
1629 q.in.max_entries = r->in.max_entries;
1630 q.in.buf_size = r->in.buf_size;
1632 q.out.total_size = r->out.total_size;
1633 q.out.returned_size = r->out.returned_size;
1634 q.out.info = r->out.info;
1636 return _samr_QueryDisplayInfo(p, &q);
1639 /****************************************************************
1640 _samr_QueryDisplayInfo3
1641 ****************************************************************/
1643 NTSTATUS _samr_QueryDisplayInfo3(pipes_struct *p,
1644 struct samr_QueryDisplayInfo3 *r)
1646 struct samr_QueryDisplayInfo q;
1648 q.in.domain_handle = r->in.domain_handle;
1649 q.in.level = r->in.level;
1650 q.in.start_idx = r->in.start_idx;
1651 q.in.max_entries = r->in.max_entries;
1652 q.in.buf_size = r->in.buf_size;
1654 q.out.total_size = r->out.total_size;
1655 q.out.returned_size = r->out.returned_size;
1656 q.out.info = r->out.info;
1658 return _samr_QueryDisplayInfo(p, &q);
1661 /*******************************************************************
1662 _samr_QueryAliasInfo
1663 ********************************************************************/
1665 NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
1666 struct samr_QueryAliasInfo *r)
1668 struct samr_alias_info *ainfo;
1669 struct acct_info info;
1670 NTSTATUS status;
1671 union samr_AliasInfo *alias_info = NULL;
1672 const char *alias_name = NULL;
1673 const char *alias_description = NULL;
1675 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1677 ainfo = policy_handle_find(p, r->in.alias_handle,
1678 SAMR_ALIAS_ACCESS_LOOKUP_INFO, NULL,
1679 struct samr_alias_info, &status);
1680 if (!NT_STATUS_IS_OK(status)) {
1681 return status;
1684 alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1685 if (!alias_info) {
1686 return NT_STATUS_NO_MEMORY;
1689 become_root();
1690 status = pdb_get_aliasinfo(&ainfo->sid, &info);
1691 unbecome_root();
1693 if ( !NT_STATUS_IS_OK(status))
1694 return status;
1696 /* FIXME: info contains fstrings */
1697 alias_name = talloc_strdup(r, info.acct_name);
1698 alias_description = talloc_strdup(r, info.acct_desc);
1700 switch (r->in.level) {
1701 case ALIASINFOALL:
1702 alias_info->all.name.string = alias_name;
1703 alias_info->all.num_members = 1; /* ??? */
1704 alias_info->all.description.string = alias_description;
1705 break;
1706 case ALIASINFONAME:
1707 alias_info->name.string = alias_name;
1708 break;
1709 case ALIASINFODESCRIPTION:
1710 alias_info->description.string = alias_description;
1711 break;
1712 default:
1713 return NT_STATUS_INVALID_INFO_CLASS;
1716 *r->out.info = alias_info;
1718 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1720 return NT_STATUS_OK;
1723 /*******************************************************************
1724 _samr_LookupNames
1725 ********************************************************************/
1727 NTSTATUS _samr_LookupNames(pipes_struct *p,
1728 struct samr_LookupNames *r)
1730 struct samr_domain_info *dinfo;
1731 NTSTATUS status;
1732 uint32 *rid;
1733 enum lsa_SidType *type;
1734 int i;
1735 int num_rids = r->in.num_names;
1736 struct samr_Ids rids, types;
1737 uint32_t num_mapped = 0;
1739 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1741 dinfo = policy_handle_find(p, r->in.domain_handle,
1742 0 /* Don't know the acc_bits yet */, NULL,
1743 struct samr_domain_info, &status);
1744 if (!NT_STATUS_IS_OK(status)) {
1745 return status;
1748 if (num_rids > MAX_SAM_ENTRIES) {
1749 num_rids = MAX_SAM_ENTRIES;
1750 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1753 rid = talloc_array(p->mem_ctx, uint32, num_rids);
1754 NT_STATUS_HAVE_NO_MEMORY(rid);
1756 type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1757 NT_STATUS_HAVE_NO_MEMORY(type);
1759 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1760 sid_string_dbg(&dinfo->sid)));
1762 for (i = 0; i < num_rids; i++) {
1764 status = NT_STATUS_NONE_MAPPED;
1765 type[i] = SID_NAME_UNKNOWN;
1767 rid[i] = 0xffffffff;
1769 if (sid_check_is_builtin(&dinfo->sid)) {
1770 if (lookup_builtin_name(r->in.names[i].string,
1771 &rid[i]))
1773 type[i] = SID_NAME_ALIAS;
1775 } else {
1776 lookup_global_sam_name(r->in.names[i].string, 0,
1777 &rid[i], &type[i]);
1780 if (type[i] != SID_NAME_UNKNOWN) {
1781 num_mapped++;
1785 if (num_mapped == num_rids) {
1786 status = NT_STATUS_OK;
1787 } else if (num_mapped == 0) {
1788 status = NT_STATUS_NONE_MAPPED;
1789 } else {
1790 status = STATUS_SOME_UNMAPPED;
1793 rids.count = num_rids;
1794 rids.ids = rid;
1796 types.count = num_rids;
1797 types.ids = type;
1799 *r->out.rids = rids;
1800 *r->out.types = types;
1802 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1804 return status;
1807 /****************************************************************
1808 _samr_ChangePasswordUser
1809 ****************************************************************/
1811 NTSTATUS _samr_ChangePasswordUser(pipes_struct *p,
1812 struct samr_ChangePasswordUser *r)
1814 NTSTATUS status;
1815 bool ret = false;
1816 struct samr_user_info *uinfo;
1817 struct samu *pwd;
1818 struct samr_Password new_lmPwdHash, new_ntPwdHash, checkHash;
1819 struct samr_Password lm_pwd, nt_pwd;
1821 uinfo = policy_handle_find(p, r->in.user_handle,
1822 SAMR_USER_ACCESS_SET_PASSWORD, NULL,
1823 struct samr_user_info, &status);
1824 if (!NT_STATUS_IS_OK(status)) {
1825 return status;
1828 DEBUG(5,("_samr_ChangePasswordUser: sid:%s\n",
1829 sid_string_dbg(&uinfo->sid)));
1831 if (!(pwd = samu_new(NULL))) {
1832 return NT_STATUS_NO_MEMORY;
1835 become_root();
1836 ret = pdb_getsampwsid(pwd, &uinfo->sid);
1837 unbecome_root();
1839 if (!ret) {
1840 TALLOC_FREE(pwd);
1841 return NT_STATUS_WRONG_PASSWORD;
1845 const uint8_t *lm_pass, *nt_pass;
1847 lm_pass = pdb_get_lanman_passwd(pwd);
1848 nt_pass = pdb_get_nt_passwd(pwd);
1850 if (!lm_pass || !nt_pass) {
1851 status = NT_STATUS_WRONG_PASSWORD;
1852 goto out;
1855 memcpy(&lm_pwd.hash, lm_pass, sizeof(lm_pwd.hash));
1856 memcpy(&nt_pwd.hash, nt_pass, sizeof(nt_pwd.hash));
1859 /* basic sanity checking on parameters. Do this before any database ops */
1860 if (!r->in.lm_present || !r->in.nt_present ||
1861 !r->in.old_lm_crypted || !r->in.new_lm_crypted ||
1862 !r->in.old_nt_crypted || !r->in.new_nt_crypted) {
1863 /* we should really handle a change with lm not
1864 present */
1865 status = NT_STATUS_INVALID_PARAMETER_MIX;
1866 goto out;
1869 /* decrypt and check the new lm hash */
1870 D_P16(lm_pwd.hash, r->in.new_lm_crypted->hash, new_lmPwdHash.hash);
1871 D_P16(new_lmPwdHash.hash, r->in.old_lm_crypted->hash, checkHash.hash);
1872 if (memcmp(checkHash.hash, lm_pwd.hash, 16) != 0) {
1873 status = NT_STATUS_WRONG_PASSWORD;
1874 goto out;
1877 /* decrypt and check the new nt hash */
1878 D_P16(nt_pwd.hash, r->in.new_nt_crypted->hash, new_ntPwdHash.hash);
1879 D_P16(new_ntPwdHash.hash, r->in.old_nt_crypted->hash, checkHash.hash);
1880 if (memcmp(checkHash.hash, nt_pwd.hash, 16) != 0) {
1881 status = NT_STATUS_WRONG_PASSWORD;
1882 goto out;
1885 /* The NT Cross is not required by Win2k3 R2, but if present
1886 check the nt cross hash */
1887 if (r->in.cross1_present && r->in.nt_cross) {
1888 D_P16(lm_pwd.hash, r->in.nt_cross->hash, checkHash.hash);
1889 if (memcmp(checkHash.hash, new_ntPwdHash.hash, 16) != 0) {
1890 status = NT_STATUS_WRONG_PASSWORD;
1891 goto out;
1895 /* The LM Cross is not required by Win2k3 R2, but if present
1896 check the lm cross hash */
1897 if (r->in.cross2_present && r->in.lm_cross) {
1898 D_P16(nt_pwd.hash, r->in.lm_cross->hash, checkHash.hash);
1899 if (memcmp(checkHash.hash, new_lmPwdHash.hash, 16) != 0) {
1900 status = NT_STATUS_WRONG_PASSWORD;
1901 goto out;
1905 if (!pdb_set_nt_passwd(pwd, new_ntPwdHash.hash, PDB_CHANGED) ||
1906 !pdb_set_lanman_passwd(pwd, new_lmPwdHash.hash, PDB_CHANGED)) {
1907 status = NT_STATUS_ACCESS_DENIED;
1908 goto out;
1911 status = pdb_update_sam_account(pwd);
1912 out:
1913 TALLOC_FREE(pwd);
1915 return status;
1918 /*******************************************************************
1919 _samr_ChangePasswordUser2
1920 ********************************************************************/
1922 NTSTATUS _samr_ChangePasswordUser2(pipes_struct *p,
1923 struct samr_ChangePasswordUser2 *r)
1925 struct smbd_server_connection *sconn = smbd_server_conn;
1926 NTSTATUS status;
1927 fstring user_name;
1928 fstring wks;
1930 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1932 fstrcpy(user_name, r->in.account->string);
1933 fstrcpy(wks, r->in.server->string);
1935 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1938 * Pass the user through the NT -> unix user mapping
1939 * function.
1942 (void)map_username(sconn, user_name);
1945 * UNIX username case mangling not required, pass_oem_change
1946 * is case insensitive.
1949 status = pass_oem_change(user_name,
1950 r->in.lm_password->data,
1951 r->in.lm_verifier->hash,
1952 r->in.nt_password->data,
1953 r->in.nt_verifier->hash,
1954 NULL);
1956 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1958 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1959 return NT_STATUS_WRONG_PASSWORD;
1962 return status;
1965 /****************************************************************
1966 _samr_OemChangePasswordUser2
1967 ****************************************************************/
1969 NTSTATUS _samr_OemChangePasswordUser2(pipes_struct *p,
1970 struct samr_OemChangePasswordUser2 *r)
1972 struct smbd_server_connection *sconn = smbd_server_conn;
1973 NTSTATUS status;
1974 fstring user_name;
1975 const char *wks = NULL;
1977 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
1979 fstrcpy(user_name, r->in.account->string);
1980 if (r->in.server && r->in.server->string) {
1981 wks = r->in.server->string;
1984 DEBUG(5,("_samr_OemChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1987 * Pass the user through the NT -> unix user mapping
1988 * function.
1991 (void)map_username(sconn, user_name);
1994 * UNIX username case mangling not required, pass_oem_change
1995 * is case insensitive.
1998 if (!r->in.hash || !r->in.password) {
1999 return NT_STATUS_INVALID_PARAMETER;
2002 status = pass_oem_change(user_name,
2003 r->in.password->data,
2004 r->in.hash->hash,
2007 NULL);
2009 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2010 return NT_STATUS_WRONG_PASSWORD;
2013 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
2015 return status;
2018 /*******************************************************************
2019 _samr_ChangePasswordUser3
2020 ********************************************************************/
2022 NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
2023 struct samr_ChangePasswordUser3 *r)
2025 struct smbd_server_connection *sconn = smbd_server_conn;
2026 NTSTATUS status;
2027 fstring user_name;
2028 const char *wks = NULL;
2029 enum samPwdChangeReason reject_reason;
2030 struct samr_DomInfo1 *dominfo = NULL;
2031 struct userPwdChangeFailureInformation *reject = NULL;
2032 uint32_t tmp;
2034 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2036 fstrcpy(user_name, r->in.account->string);
2037 if (r->in.server && r->in.server->string) {
2038 wks = r->in.server->string;
2041 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
2044 * Pass the user through the NT -> unix user mapping
2045 * function.
2048 (void)map_username(sconn, user_name);
2051 * UNIX username case mangling not required, pass_oem_change
2052 * is case insensitive.
2055 status = pass_oem_change(user_name,
2056 r->in.lm_password->data,
2057 r->in.lm_verifier->hash,
2058 r->in.nt_password->data,
2059 r->in.nt_verifier->hash,
2060 &reject_reason);
2061 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2062 return NT_STATUS_WRONG_PASSWORD;
2065 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
2066 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
2068 time_t u_expire, u_min_age;
2069 uint32 account_policy_temp;
2071 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
2072 if (!dominfo) {
2073 return NT_STATUS_NO_MEMORY;
2076 reject = TALLOC_ZERO_P(p->mem_ctx,
2077 struct userPwdChangeFailureInformation);
2078 if (!reject) {
2079 return NT_STATUS_NO_MEMORY;
2082 become_root();
2084 /* AS ROOT !!! */
2086 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN, &tmp);
2087 dominfo->min_password_length = tmp;
2089 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &tmp);
2090 dominfo->password_history_length = tmp;
2092 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
2093 &dominfo->password_properties);
2095 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &account_policy_temp);
2096 u_expire = account_policy_temp;
2098 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &account_policy_temp);
2099 u_min_age = account_policy_temp;
2101 /* !AS ROOT */
2103 unbecome_root();
2105 unix_to_nt_time_abs((NTTIME *)&dominfo->max_password_age, u_expire);
2106 unix_to_nt_time_abs((NTTIME *)&dominfo->min_password_age, u_min_age);
2108 if (lp_check_password_script() && *lp_check_password_script()) {
2109 dominfo->password_properties |= DOMAIN_PASSWORD_COMPLEX;
2112 reject->extendedFailureReason = reject_reason;
2114 *r->out.dominfo = dominfo;
2115 *r->out.reject = reject;
2118 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2120 return status;
2123 /*******************************************************************
2124 makes a SAMR_R_LOOKUP_RIDS structure.
2125 ********************************************************************/
2127 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
2128 const char **names,
2129 struct lsa_String **lsa_name_array_p)
2131 struct lsa_String *lsa_name_array = NULL;
2132 uint32_t i;
2134 *lsa_name_array_p = NULL;
2136 if (num_names != 0) {
2137 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
2138 if (!lsa_name_array) {
2139 return false;
2143 for (i = 0; i < num_names; i++) {
2144 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
2145 init_lsa_String(&lsa_name_array[i], names[i]);
2148 *lsa_name_array_p = lsa_name_array;
2150 return true;
2153 /*******************************************************************
2154 _samr_LookupRids
2155 ********************************************************************/
2157 NTSTATUS _samr_LookupRids(pipes_struct *p,
2158 struct samr_LookupRids *r)
2160 struct samr_domain_info *dinfo;
2161 NTSTATUS status;
2162 const char **names;
2163 enum lsa_SidType *attrs = NULL;
2164 uint32 *wire_attrs = NULL;
2165 int num_rids = (int)r->in.num_rids;
2166 int i;
2167 struct lsa_Strings names_array;
2168 struct samr_Ids types_array;
2169 struct lsa_String *lsa_names = NULL;
2171 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2173 dinfo = policy_handle_find(p, r->in.domain_handle,
2174 0 /* Don't know the acc_bits yet */, NULL,
2175 struct samr_domain_info, &status);
2176 if (!NT_STATUS_IS_OK(status)) {
2177 return status;
2180 if (num_rids > 1000) {
2181 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2182 "to samba4 idl this is not possible\n", num_rids));
2183 return NT_STATUS_UNSUCCESSFUL;
2186 if (num_rids) {
2187 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
2188 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
2189 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
2191 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2192 return NT_STATUS_NO_MEMORY;
2193 } else {
2194 names = NULL;
2195 attrs = NULL;
2196 wire_attrs = NULL;
2199 become_root(); /* lookup_sid can require root privs */
2200 status = pdb_lookup_rids(&dinfo->sid, num_rids, r->in.rids,
2201 names, attrs);
2202 unbecome_root();
2204 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2205 status = NT_STATUS_OK;
2208 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2209 &lsa_names)) {
2210 return NT_STATUS_NO_MEMORY;
2213 /* Convert from enum lsa_SidType to uint32 for wire format. */
2214 for (i = 0; i < num_rids; i++) {
2215 wire_attrs[i] = (uint32)attrs[i];
2218 names_array.count = num_rids;
2219 names_array.names = lsa_names;
2221 types_array.count = num_rids;
2222 types_array.ids = wire_attrs;
2224 *r->out.names = names_array;
2225 *r->out.types = types_array;
2227 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2229 return status;
2232 /*******************************************************************
2233 _samr_OpenUser
2234 ********************************************************************/
2236 NTSTATUS _samr_OpenUser(pipes_struct *p,
2237 struct samr_OpenUser *r)
2239 struct samu *sampass=NULL;
2240 struct dom_sid sid;
2241 struct samr_domain_info *dinfo;
2242 struct samr_user_info *uinfo;
2243 struct security_descriptor *psd = NULL;
2244 uint32 acc_granted;
2245 uint32 des_access = r->in.access_mask;
2246 uint32_t extra_access = 0;
2247 size_t sd_size;
2248 bool ret;
2249 NTSTATUS nt_status;
2250 SE_PRIV se_rights;
2251 NTSTATUS status;
2253 dinfo = policy_handle_find(p, r->in.domain_handle,
2254 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
2255 struct samr_domain_info, &status);
2256 if (!NT_STATUS_IS_OK(status)) {
2257 return status;
2260 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2261 return NT_STATUS_NO_MEMORY;
2264 /* append the user's RID to it */
2266 if (!sid_compose(&sid, &dinfo->sid, r->in.rid))
2267 return NT_STATUS_NO_SUCH_USER;
2269 /* check if access can be granted as requested by client. */
2270 map_max_allowed_access(p->server_info->ptok,
2271 &p->server_info->utok,
2272 &des_access);
2274 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2275 se_map_generic(&des_access, &usr_generic_mapping);
2278 * Get the sampass first as we need to check privileges
2279 * based on what kind of user object this is.
2280 * But don't reveal info too early if it didn't exist.
2283 become_root();
2284 ret=pdb_getsampwsid(sampass, &sid);
2285 unbecome_root();
2287 se_priv_copy(&se_rights, &se_priv_none);
2290 * We do the override access checks on *open*, not at
2291 * SetUserInfo time.
2293 if (ret) {
2294 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
2296 if ((acb_info & ACB_WSTRUST) &&
2297 user_has_any_privilege(p->server_info->ptok,
2298 &se_machine_account)) {
2300 * SeMachineAccount is needed to add
2301 * GENERIC_RIGHTS_USER_WRITE to a machine
2302 * account.
2304 se_priv_add(&se_rights, &se_machine_account);
2305 DEBUG(10,("_samr_OpenUser: adding machine account "
2306 "rights to handle for user %s\n",
2307 pdb_get_username(sampass) ));
2309 if ((acb_info & ACB_NORMAL) &&
2310 user_has_any_privilege(p->server_info->ptok,
2311 &se_add_users)) {
2313 * SeAddUsers is needed to add
2314 * GENERIC_RIGHTS_USER_WRITE to a normal
2315 * account.
2317 se_priv_add(&se_rights, &se_add_users);
2318 DEBUG(10,("_samr_OpenUser: adding add user "
2319 "rights to handle for user %s\n",
2320 pdb_get_username(sampass) ));
2323 * Cheat - allow GENERIC_RIGHTS_USER_WRITE if pipe user is
2324 * in DOMAIN_RID_ADMINS. This is almost certainly not
2325 * what Windows does but is a hack for people who haven't
2326 * set up privileges on groups in Samba.
2328 if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
2329 if (lp_enable_privileges() && nt_token_check_domain_rid(p->server_info->ptok,
2330 DOMAIN_RID_ADMINS)) {
2331 des_access &= ~GENERIC_RIGHTS_USER_WRITE;
2332 extra_access = GENERIC_RIGHTS_USER_WRITE;
2333 DEBUG(4,("_samr_OpenUser: Allowing "
2334 "GENERIC_RIGHTS_USER_WRITE for "
2335 "rid admins\n"));
2340 TALLOC_FREE(sampass);
2342 nt_status = access_check_object(psd, p->server_info->ptok,
2343 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2344 &acc_granted, "_samr_OpenUser");
2346 if ( !NT_STATUS_IS_OK(nt_status) )
2347 return nt_status;
2349 /* check that the SID exists in our domain. */
2350 if (ret == False) {
2351 return NT_STATUS_NO_SUCH_USER;
2354 /* If we did the rid admins hack above, allow access. */
2355 acc_granted |= extra_access;
2357 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
2358 struct samr_user_info, &nt_status);
2359 if (!NT_STATUS_IS_OK(nt_status)) {
2360 return nt_status;
2362 uinfo->sid = sid;
2364 return NT_STATUS_OK;
2367 /*************************************************************************
2368 *************************************************************************/
2370 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2371 DATA_BLOB *blob,
2372 struct lsa_BinaryString **_r)
2374 struct lsa_BinaryString *r;
2376 if (!blob || !_r) {
2377 return NT_STATUS_INVALID_PARAMETER;
2380 r = TALLOC_ZERO_P(mem_ctx, struct lsa_BinaryString);
2381 if (!r) {
2382 return NT_STATUS_NO_MEMORY;
2385 r->array = TALLOC_ZERO_ARRAY(mem_ctx, uint16_t, blob->length/2);
2386 if (!r->array) {
2387 return NT_STATUS_NO_MEMORY;
2389 memcpy(r->array, blob->data, blob->length);
2390 r->size = blob->length;
2391 r->length = blob->length;
2393 if (!r->array) {
2394 return NT_STATUS_NO_MEMORY;
2397 *_r = r;
2399 return NT_STATUS_OK;
2402 /*************************************************************************
2403 *************************************************************************/
2405 static struct samr_LogonHours get_logon_hours_from_pdb(TALLOC_CTX *mem_ctx,
2406 struct samu *pw)
2408 struct samr_LogonHours hours;
2409 const int units_per_week = 168;
2411 ZERO_STRUCT(hours);
2412 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
2413 if (!hours.bits) {
2414 return hours;
2417 hours.units_per_week = units_per_week;
2418 memset(hours.bits, 0xFF, units_per_week);
2420 if (pdb_get_hours(pw)) {
2421 memcpy(hours.bits, pdb_get_hours(pw),
2422 MIN(pdb_get_hours_len(pw), units_per_week));
2425 return hours;
2428 /*************************************************************************
2429 get_user_info_1.
2430 *************************************************************************/
2432 static NTSTATUS get_user_info_1(TALLOC_CTX *mem_ctx,
2433 struct samr_UserInfo1 *r,
2434 struct samu *pw,
2435 struct dom_sid *domain_sid)
2437 const struct dom_sid *sid_group;
2438 uint32_t primary_gid;
2440 become_root();
2441 sid_group = pdb_get_group_sid(pw);
2442 unbecome_root();
2444 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2445 DEBUG(0, ("get_user_info_1: User %s has Primary Group SID %s, \n"
2446 "which conflicts with the domain sid %s. Failing operation.\n",
2447 pdb_get_username(pw), sid_string_dbg(sid_group),
2448 sid_string_dbg(domain_sid)));
2449 return NT_STATUS_UNSUCCESSFUL;
2452 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2453 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2454 r->primary_gid = primary_gid;
2455 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2456 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2458 return NT_STATUS_OK;
2461 /*************************************************************************
2462 get_user_info_2.
2463 *************************************************************************/
2465 static NTSTATUS get_user_info_2(TALLOC_CTX *mem_ctx,
2466 struct samr_UserInfo2 *r,
2467 struct samu *pw)
2469 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2470 r->unknown.string = NULL;
2471 r->country_code = 0;
2472 r->code_page = 0;
2474 return NT_STATUS_OK;
2477 /*************************************************************************
2478 get_user_info_3.
2479 *************************************************************************/
2481 static NTSTATUS get_user_info_3(TALLOC_CTX *mem_ctx,
2482 struct samr_UserInfo3 *r,
2483 struct samu *pw,
2484 struct dom_sid *domain_sid)
2486 const struct dom_sid *sid_user, *sid_group;
2487 uint32_t rid, primary_gid;
2489 sid_user = pdb_get_user_sid(pw);
2491 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2492 DEBUG(0, ("get_user_info_3: User %s has SID %s, \nwhich conflicts with "
2493 "the domain sid %s. Failing operation.\n",
2494 pdb_get_username(pw), sid_string_dbg(sid_user),
2495 sid_string_dbg(domain_sid)));
2496 return NT_STATUS_UNSUCCESSFUL;
2499 become_root();
2500 sid_group = pdb_get_group_sid(pw);
2501 unbecome_root();
2503 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2504 DEBUG(0, ("get_user_info_3: User %s has Primary Group SID %s, \n"
2505 "which conflicts with the domain sid %s. Failing operation.\n",
2506 pdb_get_username(pw), sid_string_dbg(sid_group),
2507 sid_string_dbg(domain_sid)));
2508 return NT_STATUS_UNSUCCESSFUL;
2511 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2512 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2513 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2514 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2515 unix_to_nt_time(&r->force_password_change, pdb_get_pass_must_change_time(pw));
2517 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2518 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2519 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2520 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2521 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2522 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2523 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2525 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2526 r->rid = rid;
2527 r->primary_gid = primary_gid;
2528 r->acct_flags = pdb_get_acct_ctrl(pw);
2529 r->bad_password_count = pdb_get_bad_password_count(pw);
2530 r->logon_count = pdb_get_logon_count(pw);
2532 return NT_STATUS_OK;
2535 /*************************************************************************
2536 get_user_info_4.
2537 *************************************************************************/
2539 static NTSTATUS get_user_info_4(TALLOC_CTX *mem_ctx,
2540 struct samr_UserInfo4 *r,
2541 struct samu *pw)
2543 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2545 return NT_STATUS_OK;
2548 /*************************************************************************
2549 get_user_info_5.
2550 *************************************************************************/
2552 static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
2553 struct samr_UserInfo5 *r,
2554 struct samu *pw,
2555 struct dom_sid *domain_sid)
2557 const struct dom_sid *sid_user, *sid_group;
2558 uint32_t rid, primary_gid;
2560 sid_user = pdb_get_user_sid(pw);
2562 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2563 DEBUG(0, ("get_user_info_5: User %s has SID %s, \nwhich conflicts with "
2564 "the domain sid %s. Failing operation.\n",
2565 pdb_get_username(pw), sid_string_dbg(sid_user),
2566 sid_string_dbg(domain_sid)));
2567 return NT_STATUS_UNSUCCESSFUL;
2570 become_root();
2571 sid_group = pdb_get_group_sid(pw);
2572 unbecome_root();
2574 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2575 DEBUG(0, ("get_user_info_5: User %s has Primary Group SID %s, \n"
2576 "which conflicts with the domain sid %s. Failing operation.\n",
2577 pdb_get_username(pw), sid_string_dbg(sid_group),
2578 sid_string_dbg(domain_sid)));
2579 return NT_STATUS_UNSUCCESSFUL;
2582 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2583 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2584 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2585 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2587 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2588 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2589 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2590 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2591 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2592 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2593 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2594 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2596 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2597 r->rid = rid;
2598 r->primary_gid = primary_gid;
2599 r->acct_flags = pdb_get_acct_ctrl(pw);
2600 r->bad_password_count = pdb_get_bad_password_count(pw);
2601 r->logon_count = pdb_get_logon_count(pw);
2603 return NT_STATUS_OK;
2606 /*************************************************************************
2607 get_user_info_6.
2608 *************************************************************************/
2610 static NTSTATUS get_user_info_6(TALLOC_CTX *mem_ctx,
2611 struct samr_UserInfo6 *r,
2612 struct samu *pw)
2614 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2615 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2617 return NT_STATUS_OK;
2620 /*************************************************************************
2621 get_user_info_7. Safe. Only gives out account_name.
2622 *************************************************************************/
2624 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2625 struct samr_UserInfo7 *r,
2626 struct samu *smbpass)
2628 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2629 if (!r->account_name.string) {
2630 return NT_STATUS_NO_MEMORY;
2633 return NT_STATUS_OK;
2636 /*************************************************************************
2637 get_user_info_8.
2638 *************************************************************************/
2640 static NTSTATUS get_user_info_8(TALLOC_CTX *mem_ctx,
2641 struct samr_UserInfo8 *r,
2642 struct samu *pw)
2644 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2646 return NT_STATUS_OK;
2649 /*************************************************************************
2650 get_user_info_9. Only gives out primary group SID.
2651 *************************************************************************/
2653 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2654 struct samr_UserInfo9 *r,
2655 struct samu *smbpass)
2657 r->primary_gid = pdb_get_group_rid(smbpass);
2659 return NT_STATUS_OK;
2662 /*************************************************************************
2663 get_user_info_10.
2664 *************************************************************************/
2666 static NTSTATUS get_user_info_10(TALLOC_CTX *mem_ctx,
2667 struct samr_UserInfo10 *r,
2668 struct samu *pw)
2670 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2671 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2673 return NT_STATUS_OK;
2676 /*************************************************************************
2677 get_user_info_11.
2678 *************************************************************************/
2680 static NTSTATUS get_user_info_11(TALLOC_CTX *mem_ctx,
2681 struct samr_UserInfo11 *r,
2682 struct samu *pw)
2684 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2686 return NT_STATUS_OK;
2689 /*************************************************************************
2690 get_user_info_12.
2691 *************************************************************************/
2693 static NTSTATUS get_user_info_12(TALLOC_CTX *mem_ctx,
2694 struct samr_UserInfo12 *r,
2695 struct samu *pw)
2697 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2699 return NT_STATUS_OK;
2702 /*************************************************************************
2703 get_user_info_13.
2704 *************************************************************************/
2706 static NTSTATUS get_user_info_13(TALLOC_CTX *mem_ctx,
2707 struct samr_UserInfo13 *r,
2708 struct samu *pw)
2710 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2712 return NT_STATUS_OK;
2715 /*************************************************************************
2716 get_user_info_14.
2717 *************************************************************************/
2719 static NTSTATUS get_user_info_14(TALLOC_CTX *mem_ctx,
2720 struct samr_UserInfo14 *r,
2721 struct samu *pw)
2723 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2725 return NT_STATUS_OK;
2728 /*************************************************************************
2729 get_user_info_16. Safe. Only gives out acb bits.
2730 *************************************************************************/
2732 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2733 struct samr_UserInfo16 *r,
2734 struct samu *smbpass)
2736 r->acct_flags = pdb_get_acct_ctrl(smbpass);
2738 return NT_STATUS_OK;
2741 /*************************************************************************
2742 get_user_info_17.
2743 *************************************************************************/
2745 static NTSTATUS get_user_info_17(TALLOC_CTX *mem_ctx,
2746 struct samr_UserInfo17 *r,
2747 struct samu *pw)
2749 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2751 return NT_STATUS_OK;
2754 /*************************************************************************
2755 get_user_info_18. OK - this is the killer as it gives out password info.
2756 Ensure that this is only allowed on an encrypted connection with a root
2757 user. JRA.
2758 *************************************************************************/
2760 static NTSTATUS get_user_info_18(pipes_struct *p,
2761 TALLOC_CTX *mem_ctx,
2762 struct samr_UserInfo18 *r,
2763 struct dom_sid *user_sid)
2765 struct samu *smbpass=NULL;
2766 bool ret;
2767 const uint8_t *nt_pass = NULL;
2768 const uint8_t *lm_pass = NULL;
2770 ZERO_STRUCTP(r);
2772 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
2773 return NT_STATUS_ACCESS_DENIED;
2776 if (p->auth.auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
2777 return NT_STATUS_ACCESS_DENIED;
2781 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2784 if ( !(smbpass = samu_new( mem_ctx )) ) {
2785 return NT_STATUS_NO_MEMORY;
2788 ret = pdb_getsampwsid(smbpass, user_sid);
2790 if (ret == False) {
2791 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2792 TALLOC_FREE(smbpass);
2793 return (geteuid() == sec_initial_uid()) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2796 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2798 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2799 TALLOC_FREE(smbpass);
2800 return NT_STATUS_ACCOUNT_DISABLED;
2803 lm_pass = pdb_get_lanman_passwd(smbpass);
2804 if (lm_pass != NULL) {
2805 memcpy(r->lm_pwd.hash, lm_pass, 16);
2806 r->lm_pwd_active = true;
2809 nt_pass = pdb_get_nt_passwd(smbpass);
2810 if (nt_pass != NULL) {
2811 memcpy(r->nt_pwd.hash, nt_pass, 16);
2812 r->nt_pwd_active = true;
2814 r->password_expired = 0; /* FIXME */
2816 TALLOC_FREE(smbpass);
2818 return NT_STATUS_OK;
2821 /*************************************************************************
2822 get_user_info_20
2823 *************************************************************************/
2825 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2826 struct samr_UserInfo20 *r,
2827 struct samu *sampass)
2829 const char *munged_dial = NULL;
2830 DATA_BLOB blob;
2831 NTSTATUS status;
2832 struct lsa_BinaryString *parameters = NULL;
2834 ZERO_STRUCTP(r);
2836 munged_dial = pdb_get_munged_dial(sampass);
2838 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2839 munged_dial, (int)strlen(munged_dial)));
2841 if (munged_dial) {
2842 blob = base64_decode_data_blob(munged_dial);
2843 } else {
2844 blob = data_blob_string_const_null("");
2847 status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2848 data_blob_free(&blob);
2849 if (!NT_STATUS_IS_OK(status)) {
2850 return status;
2853 r->parameters = *parameters;
2855 return NT_STATUS_OK;
2859 /*************************************************************************
2860 get_user_info_21
2861 *************************************************************************/
2863 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2864 struct samr_UserInfo21 *r,
2865 struct samu *pw,
2866 struct dom_sid *domain_sid,
2867 uint32_t acc_granted)
2869 NTSTATUS status;
2870 const struct dom_sid *sid_user, *sid_group;
2871 uint32_t rid, primary_gid;
2872 NTTIME force_password_change;
2873 time_t must_change_time;
2874 struct lsa_BinaryString *parameters = NULL;
2875 const char *munged_dial = NULL;
2876 DATA_BLOB blob;
2878 ZERO_STRUCTP(r);
2880 sid_user = pdb_get_user_sid(pw);
2882 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2883 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2884 "the domain sid %s. Failing operation.\n",
2885 pdb_get_username(pw), sid_string_dbg(sid_user),
2886 sid_string_dbg(domain_sid)));
2887 return NT_STATUS_UNSUCCESSFUL;
2890 become_root();
2891 sid_group = pdb_get_group_sid(pw);
2892 unbecome_root();
2894 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2895 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2896 "which conflicts with the domain sid %s. Failing operation.\n",
2897 pdb_get_username(pw), sid_string_dbg(sid_group),
2898 sid_string_dbg(domain_sid)));
2899 return NT_STATUS_UNSUCCESSFUL;
2902 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2903 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2904 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2905 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2906 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2908 must_change_time = pdb_get_pass_must_change_time(pw);
2909 if (must_change_time == get_time_t_max()) {
2910 unix_to_nt_time_abs(&force_password_change, must_change_time);
2911 } else {
2912 unix_to_nt_time(&force_password_change, must_change_time);
2915 munged_dial = pdb_get_munged_dial(pw);
2916 if (munged_dial) {
2917 blob = base64_decode_data_blob(munged_dial);
2918 } else {
2919 blob = data_blob_string_const_null("");
2922 status = init_samr_parameters_string(mem_ctx, &blob, &parameters);
2923 data_blob_free(&blob);
2924 if (!NT_STATUS_IS_OK(status)) {
2925 return status;
2928 r->force_password_change = force_password_change;
2930 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2931 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2932 r->home_directory.string = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2933 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2934 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2935 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2936 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2937 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2938 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2940 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2941 r->parameters = *parameters;
2942 r->rid = rid;
2943 r->primary_gid = primary_gid;
2944 r->acct_flags = pdb_get_acct_ctrl(pw);
2945 r->bad_password_count = pdb_get_bad_password_count(pw);
2946 r->logon_count = pdb_get_logon_count(pw);
2947 r->fields_present = pdb_build_fields_present(pw);
2948 r->password_expired = (pdb_get_pass_must_change_time(pw) == 0) ?
2949 PASS_MUST_CHANGE_AT_NEXT_LOGON : 0;
2950 r->country_code = 0;
2951 r->code_page = 0;
2952 r->lm_password_set = 0;
2953 r->nt_password_set = 0;
2955 #if 0
2958 Look at a user on a real NT4 PDC with usrmgr, press
2959 'ok'. Then you will see that fields_present is set to
2960 0x08f827fa. Look at the user immediately after that again,
2961 and you will see that 0x00fffff is returned. This solves
2962 the problem that you get access denied after having looked
2963 at the user.
2964 -- Volker
2967 #endif
2970 return NT_STATUS_OK;
2973 /*******************************************************************
2974 _samr_QueryUserInfo
2975 ********************************************************************/
2977 NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
2978 struct samr_QueryUserInfo *r)
2980 NTSTATUS status;
2981 union samr_UserInfo *user_info = NULL;
2982 struct samr_user_info *uinfo;
2983 struct dom_sid domain_sid;
2984 uint32 rid;
2985 bool ret = false;
2986 struct samu *pwd = NULL;
2987 uint32_t acc_required, acc_granted;
2989 switch (r->in.level) {
2990 case 1: /* UserGeneralInformation */
2991 /* USER_READ_GENERAL */
2992 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC;
2993 break;
2994 case 2: /* UserPreferencesInformation */
2995 /* USER_READ_PREFERENCES | USER_READ_GENERAL */
2996 acc_required = SAMR_USER_ACCESS_GET_LOCALE |
2997 SAMR_USER_ACCESS_GET_NAME_ETC;
2998 break;
2999 case 3: /* UserLogonInformation */
3000 /* USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT */
3001 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC |
3002 SAMR_USER_ACCESS_GET_LOCALE |
3003 SAMR_USER_ACCESS_GET_LOGONINFO |
3004 SAMR_USER_ACCESS_GET_ATTRIBUTES;
3005 break;
3006 case 4: /* UserLogonHoursInformation */
3007 /* USER_READ_LOGON */
3008 acc_required = SAMR_USER_ACCESS_GET_LOGONINFO;
3009 break;
3010 case 5: /* UserAccountInformation */
3011 /* USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT */
3012 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC |
3013 SAMR_USER_ACCESS_GET_LOCALE |
3014 SAMR_USER_ACCESS_GET_LOGONINFO |
3015 SAMR_USER_ACCESS_GET_ATTRIBUTES;
3016 break;
3017 case 6: /* UserNameInformation */
3018 case 7: /* UserAccountNameInformation */
3019 case 8: /* UserFullNameInformation */
3020 case 9: /* UserPrimaryGroupInformation */
3021 case 13: /* UserAdminCommentInformation */
3022 /* USER_READ_GENERAL */
3023 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC;
3024 break;
3025 case 10: /* UserHomeInformation */
3026 case 11: /* UserScriptInformation */
3027 case 12: /* UserProfileInformation */
3028 case 14: /* UserWorkStationsInformation */
3029 /* USER_READ_LOGON */
3030 acc_required = SAMR_USER_ACCESS_GET_LOGONINFO;
3031 break;
3032 case 16: /* UserControlInformation */
3033 case 17: /* UserExpiresInformation */
3034 case 20: /* UserParametersInformation */
3035 /* USER_READ_ACCOUNT */
3036 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
3037 break;
3038 case 21: /* UserAllInformation */
3039 /* FIXME! - gd */
3040 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
3041 break;
3042 case 18: /* UserInternal1Information */
3043 /* FIXME! - gd */
3044 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
3045 break;
3046 case 23: /* UserInternal4Information */
3047 case 24: /* UserInternal4InformationNew */
3048 case 25: /* UserInternal4InformationNew */
3049 case 26: /* UserInternal5InformationNew */
3050 default:
3051 return NT_STATUS_INVALID_INFO_CLASS;
3052 break;
3055 uinfo = policy_handle_find(p, r->in.user_handle,
3056 acc_required, &acc_granted,
3057 struct samr_user_info, &status);
3058 if (!NT_STATUS_IS_OK(status)) {
3059 return status;
3062 domain_sid = uinfo->sid;
3064 sid_split_rid(&domain_sid, &rid);
3066 if (!sid_check_is_in_our_domain(&uinfo->sid))
3067 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3069 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
3070 sid_string_dbg(&uinfo->sid)));
3072 user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
3073 if (!user_info) {
3074 return NT_STATUS_NO_MEMORY;
3077 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
3079 if (!(pwd = samu_new(p->mem_ctx))) {
3080 return NT_STATUS_NO_MEMORY;
3083 become_root();
3084 ret = pdb_getsampwsid(pwd, &uinfo->sid);
3085 unbecome_root();
3087 if (ret == false) {
3088 DEBUG(4,("User %s not found\n", sid_string_dbg(&uinfo->sid)));
3089 TALLOC_FREE(pwd);
3090 return NT_STATUS_NO_SUCH_USER;
3093 DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
3095 samr_clear_sam_passwd(pwd);
3097 switch (r->in.level) {
3098 case 1:
3099 status = get_user_info_1(p->mem_ctx, &user_info->info1, pwd, &domain_sid);
3100 break;
3101 case 2:
3102 status = get_user_info_2(p->mem_ctx, &user_info->info2, pwd);
3103 break;
3104 case 3:
3105 status = get_user_info_3(p->mem_ctx, &user_info->info3, pwd, &domain_sid);
3106 break;
3107 case 4:
3108 status = get_user_info_4(p->mem_ctx, &user_info->info4, pwd);
3109 break;
3110 case 5:
3111 status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
3112 break;
3113 case 6:
3114 status = get_user_info_6(p->mem_ctx, &user_info->info6, pwd);
3115 break;
3116 case 7:
3117 status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
3118 break;
3119 case 8:
3120 status = get_user_info_8(p->mem_ctx, &user_info->info8, pwd);
3121 break;
3122 case 9:
3123 status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
3124 break;
3125 case 10:
3126 status = get_user_info_10(p->mem_ctx, &user_info->info10, pwd);
3127 break;
3128 case 11:
3129 status = get_user_info_11(p->mem_ctx, &user_info->info11, pwd);
3130 break;
3131 case 12:
3132 status = get_user_info_12(p->mem_ctx, &user_info->info12, pwd);
3133 break;
3134 case 13:
3135 status = get_user_info_13(p->mem_ctx, &user_info->info13, pwd);
3136 break;
3137 case 14:
3138 status = get_user_info_14(p->mem_ctx, &user_info->info14, pwd);
3139 break;
3140 case 16:
3141 status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
3142 break;
3143 case 17:
3144 status = get_user_info_17(p->mem_ctx, &user_info->info17, pwd);
3145 break;
3146 case 18:
3147 /* level 18 is special */
3148 status = get_user_info_18(p, p->mem_ctx, &user_info->info18,
3149 &uinfo->sid);
3150 break;
3151 case 20:
3152 status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
3153 break;
3154 case 21:
3155 status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid, acc_granted);
3156 break;
3157 default:
3158 status = NT_STATUS_INVALID_INFO_CLASS;
3159 break;
3162 if (!NT_STATUS_IS_OK(status)) {
3163 goto done;
3166 *r->out.info = user_info;
3168 done:
3169 TALLOC_FREE(pwd);
3171 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
3173 return status;
3176 /****************************************************************
3177 ****************************************************************/
3179 NTSTATUS _samr_QueryUserInfo2(pipes_struct *p,
3180 struct samr_QueryUserInfo2 *r)
3182 struct samr_QueryUserInfo u;
3184 u.in.user_handle = r->in.user_handle;
3185 u.in.level = r->in.level;
3186 u.out.info = r->out.info;
3188 return _samr_QueryUserInfo(p, &u);
3191 /*******************************************************************
3192 _samr_GetGroupsForUser
3193 ********************************************************************/
3195 NTSTATUS _samr_GetGroupsForUser(pipes_struct *p,
3196 struct samr_GetGroupsForUser *r)
3198 struct samr_user_info *uinfo;
3199 struct samu *sam_pass=NULL;
3200 struct dom_sid *sids;
3201 struct samr_RidWithAttribute dom_gid;
3202 struct samr_RidWithAttribute *gids = NULL;
3203 uint32 primary_group_rid;
3204 size_t num_groups = 0;
3205 gid_t *unix_gids;
3206 size_t i, num_gids;
3207 bool ret;
3208 NTSTATUS result;
3209 bool success = False;
3211 struct samr_RidWithAttributeArray *rids = NULL;
3214 * from the SID in the request:
3215 * we should send back the list of DOMAIN GROUPS
3216 * the user is a member of
3218 * and only the DOMAIN GROUPS
3219 * no ALIASES !!! neither aliases of the domain
3220 * nor aliases of the builtin SID
3222 * JFM, 12/2/2001
3225 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
3227 uinfo = policy_handle_find(p, r->in.user_handle,
3228 SAMR_USER_ACCESS_GET_GROUPS, NULL,
3229 struct samr_user_info, &result);
3230 if (!NT_STATUS_IS_OK(result)) {
3231 return result;
3234 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
3235 if (!rids) {
3236 return NT_STATUS_NO_MEMORY;
3239 if (!sid_check_is_in_our_domain(&uinfo->sid))
3240 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3242 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
3243 return NT_STATUS_NO_MEMORY;
3246 become_root();
3247 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
3248 unbecome_root();
3250 if (!ret) {
3251 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
3252 sid_string_dbg(&uinfo->sid)));
3253 return NT_STATUS_NO_SUCH_USER;
3256 sids = NULL;
3258 /* make both calls inside the root block */
3259 become_root();
3260 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
3261 &sids, &unix_gids, &num_groups);
3262 if ( NT_STATUS_IS_OK(result) ) {
3263 success = sid_peek_check_rid(get_global_sam_sid(),
3264 pdb_get_group_sid(sam_pass),
3265 &primary_group_rid);
3267 unbecome_root();
3269 if (!NT_STATUS_IS_OK(result)) {
3270 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
3271 sid_string_dbg(&uinfo->sid)));
3272 return result;
3275 if ( !success ) {
3276 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
3277 sid_string_dbg(pdb_get_group_sid(sam_pass)),
3278 pdb_get_username(sam_pass)));
3279 TALLOC_FREE(sam_pass);
3280 return NT_STATUS_INTERNAL_DB_CORRUPTION;
3283 gids = NULL;
3284 num_gids = 0;
3286 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
3287 SE_GROUP_ENABLED);
3288 dom_gid.rid = primary_group_rid;
3289 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
3291 for (i=0; i<num_groups; i++) {
3293 if (!sid_peek_check_rid(get_global_sam_sid(),
3294 &(sids[i]), &dom_gid.rid)) {
3295 DEBUG(10, ("Found sid %s not in our domain\n",
3296 sid_string_dbg(&sids[i])));
3297 continue;
3300 if (dom_gid.rid == primary_group_rid) {
3301 /* We added the primary group directly from the
3302 * sam_account. The other SIDs are unique from
3303 * enum_group_memberships */
3304 continue;
3307 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
3310 rids->count = num_gids;
3311 rids->rids = gids;
3313 *r->out.rids = rids;
3315 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
3317 return result;
3320 /*******************************************************************
3321 ********************************************************************/
3323 static uint32_t samr_get_server_role(void)
3325 uint32_t role = ROLE_DOMAIN_PDC;
3327 if (lp_server_role() == ROLE_DOMAIN_BDC) {
3328 role = ROLE_DOMAIN_BDC;
3331 return role;
3334 /*******************************************************************
3335 ********************************************************************/
3337 static NTSTATUS query_dom_info_1(TALLOC_CTX *mem_ctx,
3338 struct samr_DomInfo1 *r)
3340 uint32_t account_policy_temp;
3341 time_t u_expire, u_min_age;
3343 become_root();
3345 /* AS ROOT !!! */
3347 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN, &account_policy_temp);
3348 r->min_password_length = account_policy_temp;
3350 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &account_policy_temp);
3351 r->password_history_length = account_policy_temp;
3353 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
3354 &r->password_properties);
3356 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &account_policy_temp);
3357 u_expire = account_policy_temp;
3359 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &account_policy_temp);
3360 u_min_age = account_policy_temp;
3362 /* !AS ROOT */
3364 unbecome_root();
3366 unix_to_nt_time_abs((NTTIME *)&r->max_password_age, u_expire);
3367 unix_to_nt_time_abs((NTTIME *)&r->min_password_age, u_min_age);
3369 if (lp_check_password_script() && *lp_check_password_script()) {
3370 r->password_properties |= DOMAIN_PASSWORD_COMPLEX;
3373 return NT_STATUS_OK;
3376 /*******************************************************************
3377 ********************************************************************/
3379 static NTSTATUS query_dom_info_2(TALLOC_CTX *mem_ctx,
3380 struct samr_DomGeneralInformation *r,
3381 struct samr_domain_info *dinfo)
3383 uint32_t u_logout;
3384 time_t seq_num;
3386 become_root();
3388 /* AS ROOT !!! */
3390 r->num_users = count_sam_users(dinfo->disp_info, ACB_NORMAL);
3391 r->num_groups = count_sam_groups(dinfo->disp_info);
3392 r->num_aliases = count_sam_aliases(dinfo->disp_info);
3394 pdb_get_account_policy(PDB_POLICY_TIME_TO_LOGOUT, &u_logout);
3396 unix_to_nt_time_abs(&r->force_logoff_time, u_logout);
3398 if (!pdb_get_seq_num(&seq_num)) {
3399 seq_num = time(NULL);
3402 /* !AS ROOT */
3404 unbecome_root();
3406 r->oem_information.string = lp_serverstring();
3407 r->domain_name.string = lp_workgroup();
3408 r->primary.string = global_myname();
3409 r->sequence_num = seq_num;
3410 r->domain_server_state = DOMAIN_SERVER_ENABLED;
3411 r->role = samr_get_server_role();
3412 r->unknown3 = 1;
3414 return NT_STATUS_OK;
3417 /*******************************************************************
3418 ********************************************************************/
3420 static NTSTATUS query_dom_info_3(TALLOC_CTX *mem_ctx,
3421 struct samr_DomInfo3 *r)
3423 uint32_t u_logout;
3425 become_root();
3427 /* AS ROOT !!! */
3430 uint32_t ul;
3431 pdb_get_account_policy(PDB_POLICY_TIME_TO_LOGOUT, &ul);
3432 u_logout = (time_t)ul;
3435 /* !AS ROOT */
3437 unbecome_root();
3439 unix_to_nt_time_abs(&r->force_logoff_time, u_logout);
3441 return NT_STATUS_OK;
3444 /*******************************************************************
3445 ********************************************************************/
3447 static NTSTATUS query_dom_info_4(TALLOC_CTX *mem_ctx,
3448 struct samr_DomOEMInformation *r)
3450 r->oem_information.string = lp_serverstring();
3452 return NT_STATUS_OK;
3455 /*******************************************************************
3456 ********************************************************************/
3458 static NTSTATUS query_dom_info_5(TALLOC_CTX *mem_ctx,
3459 struct samr_DomInfo5 *r)
3461 r->domain_name.string = get_global_sam_name();
3463 return NT_STATUS_OK;
3466 /*******************************************************************
3467 ********************************************************************/
3469 static NTSTATUS query_dom_info_6(TALLOC_CTX *mem_ctx,
3470 struct samr_DomInfo6 *r)
3472 /* NT returns its own name when a PDC. win2k and later
3473 * only the name of the PDC if itself is a BDC (samba4
3474 * idl) */
3475 r->primary.string = global_myname();
3477 return NT_STATUS_OK;
3480 /*******************************************************************
3481 ********************************************************************/
3483 static NTSTATUS query_dom_info_7(TALLOC_CTX *mem_ctx,
3484 struct samr_DomInfo7 *r)
3486 r->role = samr_get_server_role();
3488 return NT_STATUS_OK;
3491 /*******************************************************************
3492 ********************************************************************/
3494 static NTSTATUS query_dom_info_8(TALLOC_CTX *mem_ctx,
3495 struct samr_DomInfo8 *r)
3497 time_t seq_num;
3499 become_root();
3501 /* AS ROOT !!! */
3503 if (!pdb_get_seq_num(&seq_num)) {
3504 seq_num = time(NULL);
3507 /* !AS ROOT */
3509 unbecome_root();
3511 r->sequence_num = seq_num;
3512 r->domain_create_time = 0;
3514 return NT_STATUS_OK;
3517 /*******************************************************************
3518 ********************************************************************/
3520 static NTSTATUS query_dom_info_9(TALLOC_CTX *mem_ctx,
3521 struct samr_DomInfo9 *r)
3523 r->domain_server_state = DOMAIN_SERVER_ENABLED;
3525 return NT_STATUS_OK;
3528 /*******************************************************************
3529 ********************************************************************/
3531 static NTSTATUS query_dom_info_11(TALLOC_CTX *mem_ctx,
3532 struct samr_DomGeneralInformation2 *r,
3533 struct samr_domain_info *dinfo)
3535 NTSTATUS status;
3536 uint32_t account_policy_temp;
3537 time_t u_lock_duration, u_reset_time;
3539 status = query_dom_info_2(mem_ctx, &r->general, dinfo);
3540 if (!NT_STATUS_IS_OK(status)) {
3541 return status;
3544 /* AS ROOT !!! */
3546 become_root();
3548 pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3549 u_lock_duration = account_policy_temp;
3550 if (u_lock_duration != -1) {
3551 u_lock_duration *= 60;
3554 pdb_get_account_policy(PDB_POLICY_RESET_COUNT_TIME, &account_policy_temp);
3555 u_reset_time = account_policy_temp * 60;
3557 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3558 r->lockout_threshold = account_policy_temp;
3560 /* !AS ROOT */
3562 unbecome_root();
3564 unix_to_nt_time_abs(&r->lockout_duration, u_lock_duration);
3565 unix_to_nt_time_abs(&r->lockout_window, u_reset_time);
3567 return NT_STATUS_OK;
3570 /*******************************************************************
3571 ********************************************************************/
3573 static NTSTATUS query_dom_info_12(TALLOC_CTX *mem_ctx,
3574 struct samr_DomInfo12 *r)
3576 uint32_t account_policy_temp;
3577 time_t u_lock_duration, u_reset_time;
3579 become_root();
3581 /* AS ROOT !!! */
3583 pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3584 u_lock_duration = account_policy_temp;
3585 if (u_lock_duration != -1) {
3586 u_lock_duration *= 60;
3589 pdb_get_account_policy(PDB_POLICY_RESET_COUNT_TIME, &account_policy_temp);
3590 u_reset_time = account_policy_temp * 60;
3592 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3593 r->lockout_threshold = account_policy_temp;
3595 /* !AS ROOT */
3597 unbecome_root();
3599 unix_to_nt_time_abs(&r->lockout_duration, u_lock_duration);
3600 unix_to_nt_time_abs(&r->lockout_window, u_reset_time);
3602 return NT_STATUS_OK;
3605 /*******************************************************************
3606 ********************************************************************/
3608 static NTSTATUS query_dom_info_13(TALLOC_CTX *mem_ctx,
3609 struct samr_DomInfo13 *r)
3611 time_t seq_num;
3613 become_root();
3615 /* AS ROOT !!! */
3617 if (!pdb_get_seq_num(&seq_num)) {
3618 seq_num = time(NULL);
3621 /* !AS ROOT */
3623 unbecome_root();
3625 r->sequence_num = seq_num;
3626 r->domain_create_time = 0;
3627 r->modified_count_at_last_promotion = 0;
3629 return NT_STATUS_OK;
3632 /*******************************************************************
3633 _samr_QueryDomainInfo
3634 ********************************************************************/
3636 NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
3637 struct samr_QueryDomainInfo *r)
3639 NTSTATUS status = NT_STATUS_OK;
3640 struct samr_domain_info *dinfo;
3641 union samr_DomainInfo *dom_info;
3643 uint32_t acc_required;
3645 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3647 switch (r->in.level) {
3648 case 1: /* DomainPasswordInformation */
3649 case 12: /* DomainLockoutInformation */
3650 /* DOMAIN_READ_PASSWORD_PARAMETERS */
3651 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1;
3652 break;
3653 case 11: /* DomainGeneralInformation2 */
3654 /* DOMAIN_READ_PASSWORD_PARAMETERS |
3655 * DOMAIN_READ_OTHER_PARAMETERS */
3656 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1 |
3657 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2;
3658 break;
3659 case 2: /* DomainGeneralInformation */
3660 case 3: /* DomainLogoffInformation */
3661 case 4: /* DomainOemInformation */
3662 case 5: /* DomainReplicationInformation */
3663 case 6: /* DomainReplicationInformation */
3664 case 7: /* DomainServerRoleInformation */
3665 case 8: /* DomainModifiedInformation */
3666 case 9: /* DomainStateInformation */
3667 case 10: /* DomainUasInformation */
3668 case 13: /* DomainModifiedInformation2 */
3669 /* DOMAIN_READ_OTHER_PARAMETERS */
3670 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2;
3671 break;
3672 default:
3673 return NT_STATUS_INVALID_INFO_CLASS;
3676 dinfo = policy_handle_find(p, r->in.domain_handle,
3677 acc_required, NULL,
3678 struct samr_domain_info, &status);
3679 if (!NT_STATUS_IS_OK(status)) {
3680 return status;
3683 dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
3684 if (!dom_info) {
3685 return NT_STATUS_NO_MEMORY;
3688 switch (r->in.level) {
3689 case 1:
3690 status = query_dom_info_1(p->mem_ctx, &dom_info->info1);
3691 break;
3692 case 2:
3693 status = query_dom_info_2(p->mem_ctx, &dom_info->general, dinfo);
3694 break;
3695 case 3:
3696 status = query_dom_info_3(p->mem_ctx, &dom_info->info3);
3697 break;
3698 case 4:
3699 status = query_dom_info_4(p->mem_ctx, &dom_info->oem);
3700 break;
3701 case 5:
3702 status = query_dom_info_5(p->mem_ctx, &dom_info->info5);
3703 break;
3704 case 6:
3705 status = query_dom_info_6(p->mem_ctx, &dom_info->info6);
3706 break;
3707 case 7:
3708 status = query_dom_info_7(p->mem_ctx, &dom_info->info7);
3709 break;
3710 case 8:
3711 status = query_dom_info_8(p->mem_ctx, &dom_info->info8);
3712 break;
3713 case 9:
3714 status = query_dom_info_9(p->mem_ctx, &dom_info->info9);
3715 break;
3716 case 11:
3717 status = query_dom_info_11(p->mem_ctx, &dom_info->general2, dinfo);
3718 break;
3719 case 12:
3720 status = query_dom_info_12(p->mem_ctx, &dom_info->info12);
3721 break;
3722 case 13:
3723 status = query_dom_info_13(p->mem_ctx, &dom_info->info13);
3724 break;
3725 default:
3726 return NT_STATUS_INVALID_INFO_CLASS;
3729 if (!NT_STATUS_IS_OK(status)) {
3730 return status;
3733 *r->out.info = dom_info;
3735 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3737 return status;
3740 /* W2k3 seems to use the same check for all 3 objects that can be created via
3741 * SAMR, if you try to create for example "Dialup" as an alias it says
3742 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3743 * database. */
3745 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3747 enum lsa_SidType type;
3748 bool result;
3750 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3752 become_root();
3753 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3754 * whether the name already exists */
3755 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3756 NULL, NULL, NULL, &type);
3757 unbecome_root();
3759 if (!result) {
3760 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3761 return NT_STATUS_OK;
3764 DEBUG(5, ("trying to create %s, exists as %s\n",
3765 new_name, sid_type_lookup(type)));
3767 if (type == SID_NAME_DOM_GRP) {
3768 return NT_STATUS_GROUP_EXISTS;
3770 if (type == SID_NAME_ALIAS) {
3771 return NT_STATUS_ALIAS_EXISTS;
3774 /* Yes, the default is NT_STATUS_USER_EXISTS */
3775 return NT_STATUS_USER_EXISTS;
3778 /*******************************************************************
3779 _samr_CreateUser2
3780 ********************************************************************/
3782 NTSTATUS _samr_CreateUser2(pipes_struct *p,
3783 struct samr_CreateUser2 *r)
3785 const char *account = NULL;
3786 struct dom_sid sid;
3787 uint32_t acb_info = r->in.acct_flags;
3788 struct samr_domain_info *dinfo;
3789 struct samr_user_info *uinfo;
3790 NTSTATUS nt_status;
3791 uint32 acc_granted;
3792 struct security_descriptor *psd;
3793 size_t sd_size;
3794 /* check this, when giving away 'add computer to domain' privs */
3795 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3796 bool can_add_account = False;
3797 SE_PRIV se_rights;
3799 dinfo = policy_handle_find(p, r->in.domain_handle,
3800 SAMR_DOMAIN_ACCESS_CREATE_USER, NULL,
3801 struct samr_domain_info, &nt_status);
3802 if (!NT_STATUS_IS_OK(nt_status)) {
3803 return nt_status;
3806 if (sid_check_is_builtin(&dinfo->sid)) {
3807 DEBUG(5,("_samr_CreateUser2: Refusing user create in BUILTIN\n"));
3808 return NT_STATUS_ACCESS_DENIED;
3811 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3812 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3813 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3814 this parameter is not an account type */
3815 return NT_STATUS_INVALID_PARAMETER;
3818 account = r->in.account_name->string;
3819 if (account == NULL) {
3820 return NT_STATUS_NO_MEMORY;
3823 nt_status = can_create(p->mem_ctx, account);
3824 if (!NT_STATUS_IS_OK(nt_status)) {
3825 return nt_status;
3828 /* determine which user right we need to check based on the acb_info */
3830 if (geteuid() == sec_initial_uid()) {
3831 se_priv_copy(&se_rights, &se_priv_none);
3832 can_add_account = true;
3833 } else if (acb_info & ACB_WSTRUST) {
3834 se_priv_copy(&se_rights, &se_machine_account);
3835 can_add_account = user_has_privileges(
3836 p->server_info->ptok, &se_rights );
3837 } else if (acb_info & ACB_NORMAL &&
3838 (account[strlen(account)-1] != '$')) {
3839 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3840 account for domain trusts and changes the ACB flags later */
3841 se_priv_copy(&se_rights, &se_add_users);
3842 can_add_account = user_has_privileges(
3843 p->server_info->ptok, &se_rights );
3844 } else if (lp_enable_privileges()) {
3845 /* implicit assumption of a BDC or domain trust account here
3846 * (we already check the flags earlier) */
3847 /* only Domain Admins can add a BDC or domain trust */
3848 se_priv_copy(&se_rights, &se_priv_none);
3849 can_add_account = nt_token_check_domain_rid(
3850 p->server_info->ptok,
3851 DOMAIN_RID_ADMINS );
3854 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3855 uidtoname(p->server_info->utok.uid),
3856 can_add_account ? "True":"False" ));
3858 if (!can_add_account) {
3859 return NT_STATUS_ACCESS_DENIED;
3862 /********** BEGIN Admin BLOCK **********/
3864 become_root();
3865 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3866 r->out.rid);
3867 unbecome_root();
3869 /********** END Admin BLOCK **********/
3871 /* now check for failure */
3873 if ( !NT_STATUS_IS_OK(nt_status) )
3874 return nt_status;
3876 /* Get the user's SID */
3878 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3880 map_max_allowed_access(p->server_info->ptok,
3881 &p->server_info->utok,
3882 &des_access);
3884 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3885 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3886 se_map_generic(&des_access, &usr_generic_mapping);
3889 * JRA - TESTME. We just created this user so we
3890 * had rights to create them. Do we need to check
3891 * any further access on this object ? Can't we
3892 * just assume we have all the rights we need ?
3895 nt_status = access_check_object(psd, p->server_info->ptok,
3896 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
3897 &acc_granted, "_samr_CreateUser2");
3899 if ( !NT_STATUS_IS_OK(nt_status) ) {
3900 return nt_status;
3903 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
3904 struct samr_user_info, &nt_status);
3905 if (!NT_STATUS_IS_OK(nt_status)) {
3906 return nt_status;
3908 uinfo->sid = sid;
3910 /* After a "set" ensure we have no cached display info. */
3911 force_flush_samr_cache(&sid);
3913 *r->out.access_granted = acc_granted;
3915 return NT_STATUS_OK;
3918 /****************************************************************
3919 ****************************************************************/
3921 NTSTATUS _samr_CreateUser(pipes_struct *p,
3922 struct samr_CreateUser *r)
3924 struct samr_CreateUser2 c;
3925 uint32_t access_granted;
3927 c.in.domain_handle = r->in.domain_handle;
3928 c.in.account_name = r->in.account_name;
3929 c.in.acct_flags = ACB_NORMAL;
3930 c.in.access_mask = r->in.access_mask;
3931 c.out.user_handle = r->out.user_handle;
3932 c.out.access_granted = &access_granted;
3933 c.out.rid = r->out.rid;
3935 return _samr_CreateUser2(p, &c);
3938 /*******************************************************************
3939 _samr_Connect
3940 ********************************************************************/
3942 NTSTATUS _samr_Connect(pipes_struct *p,
3943 struct samr_Connect *r)
3945 struct samr_connect_info *info;
3946 uint32_t acc_granted;
3947 struct policy_handle hnd;
3948 uint32 des_access = r->in.access_mask;
3949 NTSTATUS status;
3951 /* Access check */
3953 if (!pipe_access_check(p)) {
3954 DEBUG(3, ("access denied to _samr_Connect\n"));
3955 return NT_STATUS_ACCESS_DENIED;
3958 /* don't give away the farm but this is probably ok. The SAMR_ACCESS_ENUM_DOMAINS
3959 was observed from a win98 client trying to enumerate users (when configured
3960 user level access control on shares) --jerry */
3962 map_max_allowed_access(p->server_info->ptok,
3963 &p->server_info->utok,
3964 &des_access);
3966 se_map_generic( &des_access, &sam_generic_mapping );
3968 acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS
3969 |SAMR_ACCESS_LOOKUP_DOMAIN);
3971 /* set up the SAMR connect_anon response */
3973 info = policy_handle_create(p, &hnd, acc_granted,
3974 struct samr_connect_info,
3975 &status);
3976 if (!NT_STATUS_IS_OK(status)) {
3977 return status;
3980 *r->out.connect_handle = hnd;
3981 return NT_STATUS_OK;
3984 /*******************************************************************
3985 _samr_Connect2
3986 ********************************************************************/
3988 NTSTATUS _samr_Connect2(pipes_struct *p,
3989 struct samr_Connect2 *r)
3991 struct samr_connect_info *info = NULL;
3992 struct policy_handle hnd;
3993 struct security_descriptor *psd = NULL;
3994 uint32 acc_granted;
3995 uint32 des_access = r->in.access_mask;
3996 NTSTATUS nt_status;
3997 size_t sd_size;
3998 const char *fn = "_samr_Connect2";
4000 switch (p->hdr_req.opnum) {
4001 case NDR_SAMR_CONNECT2:
4002 fn = "_samr_Connect2";
4003 break;
4004 case NDR_SAMR_CONNECT3:
4005 fn = "_samr_Connect3";
4006 break;
4007 case NDR_SAMR_CONNECT4:
4008 fn = "_samr_Connect4";
4009 break;
4010 case NDR_SAMR_CONNECT5:
4011 fn = "_samr_Connect5";
4012 break;
4015 DEBUG(5,("%s: %d\n", fn, __LINE__));
4017 /* Access check */
4019 if (!pipe_access_check(p)) {
4020 DEBUG(3, ("access denied to %s\n", fn));
4021 return NT_STATUS_ACCESS_DENIED;
4024 map_max_allowed_access(p->server_info->ptok,
4025 &p->server_info->utok,
4026 &des_access);
4028 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
4029 se_map_generic(&des_access, &sam_generic_mapping);
4031 nt_status = access_check_object(psd, p->server_info->ptok,
4032 NULL, 0, des_access, &acc_granted, fn);
4034 if ( !NT_STATUS_IS_OK(nt_status) )
4035 return nt_status;
4037 info = policy_handle_create(p, &hnd, acc_granted,
4038 struct samr_connect_info, &nt_status);
4039 if (!NT_STATUS_IS_OK(nt_status)) {
4040 return nt_status;
4043 DEBUG(5,("%s: %d\n", fn, __LINE__));
4045 *r->out.connect_handle = hnd;
4046 return NT_STATUS_OK;
4049 /****************************************************************
4050 _samr_Connect3
4051 ****************************************************************/
4053 NTSTATUS _samr_Connect3(pipes_struct *p,
4054 struct samr_Connect3 *r)
4056 struct samr_Connect2 c;
4058 c.in.system_name = r->in.system_name;
4059 c.in.access_mask = r->in.access_mask;
4060 c.out.connect_handle = r->out.connect_handle;
4062 return _samr_Connect2(p, &c);
4065 /*******************************************************************
4066 _samr_Connect4
4067 ********************************************************************/
4069 NTSTATUS _samr_Connect4(pipes_struct *p,
4070 struct samr_Connect4 *r)
4072 struct samr_Connect2 c;
4074 c.in.system_name = r->in.system_name;
4075 c.in.access_mask = r->in.access_mask;
4076 c.out.connect_handle = r->out.connect_handle;
4078 return _samr_Connect2(p, &c);
4081 /*******************************************************************
4082 _samr_Connect5
4083 ********************************************************************/
4085 NTSTATUS _samr_Connect5(pipes_struct *p,
4086 struct samr_Connect5 *r)
4088 NTSTATUS status;
4089 struct samr_Connect2 c;
4090 struct samr_ConnectInfo1 info1;
4092 info1.client_version = SAMR_CONNECT_AFTER_W2K;
4093 info1.unknown2 = 0;
4095 c.in.system_name = r->in.system_name;
4096 c.in.access_mask = r->in.access_mask;
4097 c.out.connect_handle = r->out.connect_handle;
4099 *r->out.level_out = 1;
4101 status = _samr_Connect2(p, &c);
4102 if (!NT_STATUS_IS_OK(status)) {
4103 return status;
4106 r->out.info_out->info1 = info1;
4108 return NT_STATUS_OK;
4111 /**********************************************************************
4112 _samr_LookupDomain
4113 **********************************************************************/
4115 NTSTATUS _samr_LookupDomain(pipes_struct *p,
4116 struct samr_LookupDomain *r)
4118 NTSTATUS status;
4119 struct samr_connect_info *info;
4120 const char *domain_name;
4121 struct dom_sid *sid = NULL;
4123 /* win9x user manager likes to use SAMR_ACCESS_ENUM_DOMAINS here.
4124 Reverted that change so we will work with RAS servers again */
4126 info = policy_handle_find(p, r->in.connect_handle,
4127 SAMR_ACCESS_LOOKUP_DOMAIN, NULL,
4128 struct samr_connect_info,
4129 &status);
4130 if (!NT_STATUS_IS_OK(status)) {
4131 return status;
4134 domain_name = r->in.domain_name->string;
4135 if (!domain_name) {
4136 return NT_STATUS_INVALID_PARAMETER;
4139 sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
4140 if (!sid) {
4141 return NT_STATUS_NO_MEMORY;
4144 if (strequal(domain_name, builtin_domain_name())) {
4145 sid_copy(sid, &global_sid_Builtin);
4146 } else {
4147 if (!secrets_fetch_domain_sid(domain_name, sid)) {
4148 status = NT_STATUS_NO_SUCH_DOMAIN;
4152 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
4153 sid_string_dbg(sid)));
4155 *r->out.sid = sid;
4157 return status;
4160 /**********************************************************************
4161 _samr_EnumDomains
4162 **********************************************************************/
4164 NTSTATUS _samr_EnumDomains(pipes_struct *p,
4165 struct samr_EnumDomains *r)
4167 NTSTATUS status;
4168 struct samr_connect_info *info;
4169 uint32_t num_entries = 2;
4170 struct samr_SamEntry *entry_array = NULL;
4171 struct samr_SamArray *sam;
4173 info = policy_handle_find(p, r->in.connect_handle,
4174 SAMR_ACCESS_ENUM_DOMAINS, NULL,
4175 struct samr_connect_info, &status);
4176 if (!NT_STATUS_IS_OK(status)) {
4177 return status;
4180 sam = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
4181 if (!sam) {
4182 return NT_STATUS_NO_MEMORY;
4185 entry_array = TALLOC_ZERO_ARRAY(p->mem_ctx,
4186 struct samr_SamEntry,
4187 num_entries);
4188 if (!entry_array) {
4189 return NT_STATUS_NO_MEMORY;
4192 entry_array[0].idx = 0;
4193 init_lsa_String(&entry_array[0].name, get_global_sam_name());
4195 entry_array[1].idx = 1;
4196 init_lsa_String(&entry_array[1].name, "Builtin");
4198 sam->count = num_entries;
4199 sam->entries = entry_array;
4201 *r->out.sam = sam;
4202 *r->out.num_entries = num_entries;
4204 return status;
4207 /*******************************************************************
4208 _samr_OpenAlias
4209 ********************************************************************/
4211 NTSTATUS _samr_OpenAlias(pipes_struct *p,
4212 struct samr_OpenAlias *r)
4214 struct dom_sid sid;
4215 uint32 alias_rid = r->in.rid;
4216 struct samr_alias_info *ainfo;
4217 struct samr_domain_info *dinfo;
4218 struct security_descriptor *psd = NULL;
4219 uint32 acc_granted;
4220 uint32 des_access = r->in.access_mask;
4221 size_t sd_size;
4222 NTSTATUS status;
4223 SE_PRIV se_rights;
4225 dinfo = policy_handle_find(p, r->in.domain_handle,
4226 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
4227 struct samr_domain_info, &status);
4228 if (!NT_STATUS_IS_OK(status)) {
4229 return status;
4232 /* append the alias' RID to it */
4234 if (!sid_compose(&sid, &dinfo->sid, alias_rid))
4235 return NT_STATUS_NO_SUCH_ALIAS;
4237 /*check if access can be granted as requested by client. */
4239 map_max_allowed_access(p->server_info->ptok,
4240 &p->server_info->utok,
4241 &des_access);
4243 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
4244 se_map_generic(&des_access,&ali_generic_mapping);
4246 se_priv_copy( &se_rights, &se_add_users );
4248 status = access_check_object(psd, p->server_info->ptok,
4249 &se_rights, GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
4250 des_access, &acc_granted, "_samr_OpenAlias");
4252 if ( !NT_STATUS_IS_OK(status) )
4253 return status;
4256 /* Check we actually have the requested alias */
4257 enum lsa_SidType type;
4258 bool result;
4259 gid_t gid;
4261 become_root();
4262 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
4263 unbecome_root();
4265 if (!result || (type != SID_NAME_ALIAS)) {
4266 return NT_STATUS_NO_SUCH_ALIAS;
4269 /* make sure there is a mapping */
4271 if ( !sid_to_gid( &sid, &gid ) ) {
4272 return NT_STATUS_NO_SUCH_ALIAS;
4277 ainfo = policy_handle_create(p, r->out.alias_handle, acc_granted,
4278 struct samr_alias_info, &status);
4279 if (!NT_STATUS_IS_OK(status)) {
4280 return status;
4282 ainfo->sid = sid;
4284 return NT_STATUS_OK;
4287 /*******************************************************************
4288 set_user_info_2
4289 ********************************************************************/
4291 static NTSTATUS set_user_info_2(TALLOC_CTX *mem_ctx,
4292 struct samr_UserInfo2 *id2,
4293 struct samu *pwd)
4295 if (id2 == NULL) {
4296 DEBUG(5,("set_user_info_2: NULL id2\n"));
4297 return NT_STATUS_ACCESS_DENIED;
4300 copy_id2_to_sam_passwd(pwd, id2);
4302 return pdb_update_sam_account(pwd);
4305 /*******************************************************************
4306 set_user_info_4
4307 ********************************************************************/
4309 static NTSTATUS set_user_info_4(TALLOC_CTX *mem_ctx,
4310 struct samr_UserInfo4 *id4,
4311 struct samu *pwd)
4313 if (id4 == NULL) {
4314 DEBUG(5,("set_user_info_2: NULL id4\n"));
4315 return NT_STATUS_ACCESS_DENIED;
4318 copy_id4_to_sam_passwd(pwd, id4);
4320 return pdb_update_sam_account(pwd);
4323 /*******************************************************************
4324 set_user_info_6
4325 ********************************************************************/
4327 static NTSTATUS set_user_info_6(TALLOC_CTX *mem_ctx,
4328 struct samr_UserInfo6 *id6,
4329 struct samu *pwd)
4331 if (id6 == NULL) {
4332 DEBUG(5,("set_user_info_6: NULL id6\n"));
4333 return NT_STATUS_ACCESS_DENIED;
4336 copy_id6_to_sam_passwd(pwd, id6);
4338 return pdb_update_sam_account(pwd);
4341 /*******************************************************************
4342 set_user_info_7
4343 ********************************************************************/
4345 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
4346 struct samr_UserInfo7 *id7,
4347 struct samu *pwd)
4349 NTSTATUS rc;
4351 if (id7 == NULL) {
4352 DEBUG(5, ("set_user_info_7: NULL id7\n"));
4353 return NT_STATUS_ACCESS_DENIED;
4356 if (!id7->account_name.string) {
4357 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
4358 return NT_STATUS_ACCESS_DENIED;
4361 /* check to see if the new username already exists. Note: we can't
4362 reliably lock all backends, so there is potentially the
4363 possibility that a user can be created in between this check and
4364 the rename. The rename should fail, but may not get the
4365 exact same failure status code. I think this is small enough
4366 of a window for this type of operation and the results are
4367 simply that the rename fails with a slightly different status
4368 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
4370 rc = can_create(mem_ctx, id7->account_name.string);
4372 /* when there is nothing to change, we're done here */
4373 if (NT_STATUS_EQUAL(rc, NT_STATUS_USER_EXISTS) &&
4374 strequal(id7->account_name.string, pdb_get_username(pwd))) {
4375 return NT_STATUS_OK;
4377 if (!NT_STATUS_IS_OK(rc)) {
4378 return rc;
4381 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
4383 return rc;
4386 /*******************************************************************
4387 set_user_info_8
4388 ********************************************************************/
4390 static NTSTATUS set_user_info_8(TALLOC_CTX *mem_ctx,
4391 struct samr_UserInfo8 *id8,
4392 struct samu *pwd)
4394 if (id8 == NULL) {
4395 DEBUG(5,("set_user_info_8: NULL id8\n"));
4396 return NT_STATUS_ACCESS_DENIED;
4399 copy_id8_to_sam_passwd(pwd, id8);
4401 return pdb_update_sam_account(pwd);
4404 /*******************************************************************
4405 set_user_info_10
4406 ********************************************************************/
4408 static NTSTATUS set_user_info_10(TALLOC_CTX *mem_ctx,
4409 struct samr_UserInfo10 *id10,
4410 struct samu *pwd)
4412 if (id10 == NULL) {
4413 DEBUG(5,("set_user_info_8: NULL id10\n"));
4414 return NT_STATUS_ACCESS_DENIED;
4417 copy_id10_to_sam_passwd(pwd, id10);
4419 return pdb_update_sam_account(pwd);
4422 /*******************************************************************
4423 set_user_info_11
4424 ********************************************************************/
4426 static NTSTATUS set_user_info_11(TALLOC_CTX *mem_ctx,
4427 struct samr_UserInfo11 *id11,
4428 struct samu *pwd)
4430 if (id11 == NULL) {
4431 DEBUG(5,("set_user_info_11: NULL id11\n"));
4432 return NT_STATUS_ACCESS_DENIED;
4435 copy_id11_to_sam_passwd(pwd, id11);
4437 return pdb_update_sam_account(pwd);
4440 /*******************************************************************
4441 set_user_info_12
4442 ********************************************************************/
4444 static NTSTATUS set_user_info_12(TALLOC_CTX *mem_ctx,
4445 struct samr_UserInfo12 *id12,
4446 struct samu *pwd)
4448 if (id12 == NULL) {
4449 DEBUG(5,("set_user_info_12: NULL id12\n"));
4450 return NT_STATUS_ACCESS_DENIED;
4453 copy_id12_to_sam_passwd(pwd, id12);
4455 return pdb_update_sam_account(pwd);
4458 /*******************************************************************
4459 set_user_info_13
4460 ********************************************************************/
4462 static NTSTATUS set_user_info_13(TALLOC_CTX *mem_ctx,
4463 struct samr_UserInfo13 *id13,
4464 struct samu *pwd)
4466 if (id13 == NULL) {
4467 DEBUG(5,("set_user_info_13: NULL id13\n"));
4468 return NT_STATUS_ACCESS_DENIED;
4471 copy_id13_to_sam_passwd(pwd, id13);
4473 return pdb_update_sam_account(pwd);
4476 /*******************************************************************
4477 set_user_info_14
4478 ********************************************************************/
4480 static NTSTATUS set_user_info_14(TALLOC_CTX *mem_ctx,
4481 struct samr_UserInfo14 *id14,
4482 struct samu *pwd)
4484 if (id14 == NULL) {
4485 DEBUG(5,("set_user_info_14: NULL id14\n"));
4486 return NT_STATUS_ACCESS_DENIED;
4489 copy_id14_to_sam_passwd(pwd, id14);
4491 return pdb_update_sam_account(pwd);
4494 /*******************************************************************
4495 set_user_info_16
4496 ********************************************************************/
4498 static NTSTATUS set_user_info_16(TALLOC_CTX *mem_ctx,
4499 struct samr_UserInfo16 *id16,
4500 struct samu *pwd)
4502 if (id16 == NULL) {
4503 DEBUG(5,("set_user_info_16: NULL id16\n"));
4504 return NT_STATUS_ACCESS_DENIED;
4507 copy_id16_to_sam_passwd(pwd, id16);
4509 return pdb_update_sam_account(pwd);
4512 /*******************************************************************
4513 set_user_info_17
4514 ********************************************************************/
4516 static NTSTATUS set_user_info_17(TALLOC_CTX *mem_ctx,
4517 struct samr_UserInfo17 *id17,
4518 struct samu *pwd)
4520 if (id17 == NULL) {
4521 DEBUG(5,("set_user_info_17: NULL id17\n"));
4522 return NT_STATUS_ACCESS_DENIED;
4525 copy_id17_to_sam_passwd(pwd, id17);
4527 return pdb_update_sam_account(pwd);
4530 /*******************************************************************
4531 set_user_info_18
4532 ********************************************************************/
4534 static NTSTATUS set_user_info_18(struct samr_UserInfo18 *id18,
4535 TALLOC_CTX *mem_ctx,
4536 DATA_BLOB *session_key,
4537 struct samu *pwd)
4539 if (id18 == NULL) {
4540 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
4541 return NT_STATUS_INVALID_PARAMETER;
4544 if (id18->nt_pwd_active || id18->lm_pwd_active) {
4545 if (!session_key->length) {
4546 return NT_STATUS_NO_USER_SESSION_KEY;
4550 if (id18->nt_pwd_active) {
4552 DATA_BLOB in, out;
4554 in = data_blob_const(id18->nt_pwd.hash, 16);
4555 out = data_blob_talloc_zero(mem_ctx, 16);
4557 sess_crypt_blob(&out, &in, session_key, false);
4559 if (!pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED)) {
4560 return NT_STATUS_ACCESS_DENIED;
4563 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4566 if (id18->lm_pwd_active) {
4568 DATA_BLOB in, out;
4570 in = data_blob_const(id18->lm_pwd.hash, 16);
4571 out = data_blob_talloc_zero(mem_ctx, 16);
4573 sess_crypt_blob(&out, &in, session_key, false);
4575 if (!pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED)) {
4576 return NT_STATUS_ACCESS_DENIED;
4579 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4582 copy_id18_to_sam_passwd(pwd, id18);
4584 return pdb_update_sam_account(pwd);
4587 /*******************************************************************
4588 set_user_info_20
4589 ********************************************************************/
4591 static NTSTATUS set_user_info_20(TALLOC_CTX *mem_ctx,
4592 struct samr_UserInfo20 *id20,
4593 struct samu *pwd)
4595 if (id20 == NULL) {
4596 DEBUG(5,("set_user_info_20: NULL id20\n"));
4597 return NT_STATUS_ACCESS_DENIED;
4600 copy_id20_to_sam_passwd(pwd, id20);
4602 return pdb_update_sam_account(pwd);
4605 /*******************************************************************
4606 set_user_info_21
4607 ********************************************************************/
4609 static NTSTATUS set_user_info_21(struct samr_UserInfo21 *id21,
4610 TALLOC_CTX *mem_ctx,
4611 DATA_BLOB *session_key,
4612 struct samu *pwd)
4614 NTSTATUS status;
4616 if (id21 == NULL) {
4617 DEBUG(5, ("set_user_info_21: NULL id21\n"));
4618 return NT_STATUS_INVALID_PARAMETER;
4621 if (id21->fields_present == 0) {
4622 return NT_STATUS_INVALID_PARAMETER;
4625 if (id21->fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4626 return NT_STATUS_ACCESS_DENIED;
4629 if (id21->fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) {
4630 if (id21->nt_password_set) {
4631 DATA_BLOB in, out;
4633 if ((id21->nt_owf_password.length != 16) ||
4634 (id21->nt_owf_password.size != 16)) {
4635 return NT_STATUS_INVALID_PARAMETER;
4638 if (!session_key->length) {
4639 return NT_STATUS_NO_USER_SESSION_KEY;
4642 in = data_blob_const(id21->nt_owf_password.array, 16);
4643 out = data_blob_talloc_zero(mem_ctx, 16);
4645 sess_crypt_blob(&out, &in, session_key, false);
4647 pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED);
4648 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4652 if (id21->fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT) {
4653 if (id21->lm_password_set) {
4654 DATA_BLOB in, out;
4656 if ((id21->lm_owf_password.length != 16) ||
4657 (id21->lm_owf_password.size != 16)) {
4658 return NT_STATUS_INVALID_PARAMETER;
4661 if (!session_key->length) {
4662 return NT_STATUS_NO_USER_SESSION_KEY;
4665 in = data_blob_const(id21->lm_owf_password.array, 16);
4666 out = data_blob_talloc_zero(mem_ctx, 16);
4668 sess_crypt_blob(&out, &in, session_key, false);
4670 pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED);
4671 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4675 /* we need to separately check for an account rename first */
4677 if (id21->account_name.string &&
4678 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
4681 /* check to see if the new username already exists. Note: we can't
4682 reliably lock all backends, so there is potentially the
4683 possibility that a user can be created in between this check and
4684 the rename. The rename should fail, but may not get the
4685 exact same failure status code. I think this is small enough
4686 of a window for this type of operation and the results are
4687 simply that the rename fails with a slightly different status
4688 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
4690 status = can_create(mem_ctx, id21->account_name.string);
4691 if (!NT_STATUS_IS_OK(status)) {
4692 return status;
4695 status = pdb_rename_sam_account(pwd, id21->account_name.string);
4697 if (!NT_STATUS_IS_OK(status)) {
4698 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
4699 nt_errstr(status)));
4700 return status;
4703 /* set the new username so that later
4704 functions can work on the new account */
4705 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
4708 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
4711 * The funny part about the previous two calls is
4712 * that pwd still has the password hashes from the
4713 * passdb entry. These have not been updated from
4714 * id21. I don't know if they need to be set. --jerry
4717 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4718 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4719 if ( !NT_STATUS_IS_OK(status) ) {
4720 return status;
4724 /* Don't worry about writing out the user account since the
4725 primary group SID is generated solely from the user's Unix
4726 primary group. */
4728 /* write the change out */
4729 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4730 return status;
4733 return NT_STATUS_OK;
4736 /*******************************************************************
4737 set_user_info_23
4738 ********************************************************************/
4740 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
4741 struct samr_UserInfo23 *id23,
4742 struct samu *pwd)
4744 char *plaintext_buf = NULL;
4745 size_t len = 0;
4746 uint32_t acct_ctrl;
4747 NTSTATUS status;
4749 if (id23 == NULL) {
4750 DEBUG(5, ("set_user_info_23: NULL id23\n"));
4751 return NT_STATUS_INVALID_PARAMETER;
4754 if (id23->info.fields_present == 0) {
4755 return NT_STATUS_INVALID_PARAMETER;
4758 if (id23->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4759 return NT_STATUS_ACCESS_DENIED;
4762 if ((id23->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4763 (id23->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4765 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
4766 pdb_get_username(pwd)));
4768 if (!decode_pw_buffer(mem_ctx,
4769 id23->password.data,
4770 &plaintext_buf,
4771 &len,
4772 CH_UTF16)) {
4773 return NT_STATUS_WRONG_PASSWORD;
4776 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4777 return NT_STATUS_ACCESS_DENIED;
4781 copy_id23_to_sam_passwd(pwd, id23);
4783 acct_ctrl = pdb_get_acct_ctrl(pwd);
4785 /* if it's a trust account, don't update /etc/passwd */
4786 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4787 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4788 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4789 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
4790 } else if (plaintext_buf) {
4791 /* update the UNIX password */
4792 if (lp_unix_password_sync() ) {
4793 struct passwd *passwd;
4794 if (pdb_get_username(pwd) == NULL) {
4795 DEBUG(1, ("chgpasswd: User without name???\n"));
4796 return NT_STATUS_ACCESS_DENIED;
4799 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4800 if (passwd == NULL) {
4801 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4804 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
4805 return NT_STATUS_ACCESS_DENIED;
4807 TALLOC_FREE(passwd);
4811 if (plaintext_buf) {
4812 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4815 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
4816 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
4817 pwd)))) {
4818 return status;
4821 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4822 return status;
4825 return NT_STATUS_OK;
4828 /*******************************************************************
4829 set_user_info_pw
4830 ********************************************************************/
4832 static bool set_user_info_pw(uint8 *pass, struct samu *pwd)
4834 size_t len = 0;
4835 char *plaintext_buf = NULL;
4836 uint32 acct_ctrl;
4838 DEBUG(5, ("Attempting administrator password change for user %s\n",
4839 pdb_get_username(pwd)));
4841 acct_ctrl = pdb_get_acct_ctrl(pwd);
4843 if (!decode_pw_buffer(talloc_tos(),
4844 pass,
4845 &plaintext_buf,
4846 &len,
4847 CH_UTF16)) {
4848 return False;
4851 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4852 return False;
4855 /* if it's a trust account, don't update /etc/passwd */
4856 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4857 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4858 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4859 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
4860 } else {
4861 /* update the UNIX password */
4862 if (lp_unix_password_sync()) {
4863 struct passwd *passwd;
4865 if (pdb_get_username(pwd) == NULL) {
4866 DEBUG(1, ("chgpasswd: User without name???\n"));
4867 return False;
4870 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4871 if (passwd == NULL) {
4872 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4875 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
4876 return False;
4878 TALLOC_FREE(passwd);
4882 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4884 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
4886 return True;
4889 /*******************************************************************
4890 set_user_info_24
4891 ********************************************************************/
4893 static NTSTATUS set_user_info_24(TALLOC_CTX *mem_ctx,
4894 struct samr_UserInfo24 *id24,
4895 struct samu *pwd)
4897 NTSTATUS status;
4899 if (id24 == NULL) {
4900 DEBUG(5, ("set_user_info_24: NULL id24\n"));
4901 return NT_STATUS_INVALID_PARAMETER;
4904 if (!set_user_info_pw(id24->password.data, pwd)) {
4905 return NT_STATUS_WRONG_PASSWORD;
4908 copy_id24_to_sam_passwd(pwd, id24);
4910 status = pdb_update_sam_account(pwd);
4911 if (!NT_STATUS_IS_OK(status)) {
4912 return status;
4915 return NT_STATUS_OK;
4918 /*******************************************************************
4919 set_user_info_25
4920 ********************************************************************/
4922 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
4923 struct samr_UserInfo25 *id25,
4924 struct samu *pwd)
4926 NTSTATUS status;
4928 if (id25 == NULL) {
4929 DEBUG(5, ("set_user_info_25: NULL id25\n"));
4930 return NT_STATUS_INVALID_PARAMETER;
4933 if (id25->info.fields_present == 0) {
4934 return NT_STATUS_INVALID_PARAMETER;
4937 if (id25->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4938 return NT_STATUS_ACCESS_DENIED;
4941 if ((id25->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4942 (id25->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4944 if (!set_user_info_pw(id25->password.data, pwd)) {
4945 return NT_STATUS_WRONG_PASSWORD;
4949 copy_id25_to_sam_passwd(pwd, id25);
4951 /* write the change out */
4952 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4953 return status;
4957 * We need to "pdb_update_sam_account" before the unix primary group
4958 * is set, because the idealx scripts would also change the
4959 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
4960 * the delete explicit / add explicit, which would then fail to find
4961 * the previous primaryGroupSid value.
4964 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4965 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4966 if ( !NT_STATUS_IS_OK(status) ) {
4967 return status;
4971 return NT_STATUS_OK;
4974 /*******************************************************************
4975 set_user_info_26
4976 ********************************************************************/
4978 static NTSTATUS set_user_info_26(TALLOC_CTX *mem_ctx,
4979 struct samr_UserInfo26 *id26,
4980 struct samu *pwd)
4982 NTSTATUS status;
4984 if (id26 == NULL) {
4985 DEBUG(5, ("set_user_info_26: NULL id26\n"));
4986 return NT_STATUS_INVALID_PARAMETER;
4989 if (!set_user_info_pw(id26->password.data, pwd)) {
4990 return NT_STATUS_WRONG_PASSWORD;
4993 copy_id26_to_sam_passwd(pwd, id26);
4995 status = pdb_update_sam_account(pwd);
4996 if (!NT_STATUS_IS_OK(status)) {
4997 return status;
5000 return NT_STATUS_OK;
5003 /*************************************************************
5004 **************************************************************/
5006 static uint32_t samr_set_user_info_map_fields_to_access_mask(uint32_t fields)
5008 uint32_t acc_required = 0;
5010 /* USER_ALL_USERNAME */
5011 if (fields & SAMR_FIELD_ACCOUNT_NAME)
5012 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5013 /* USER_ALL_FULLNAME */
5014 if (fields & SAMR_FIELD_FULL_NAME)
5015 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5016 /* USER_ALL_PRIMARYGROUPID */
5017 if (fields & SAMR_FIELD_PRIMARY_GID)
5018 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5019 /* USER_ALL_HOMEDIRECTORY */
5020 if (fields & SAMR_FIELD_HOME_DIRECTORY)
5021 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5022 /* USER_ALL_HOMEDIRECTORYDRIVE */
5023 if (fields & SAMR_FIELD_HOME_DRIVE)
5024 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5025 /* USER_ALL_SCRIPTPATH */
5026 if (fields & SAMR_FIELD_LOGON_SCRIPT)
5027 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5028 /* USER_ALL_PROFILEPATH */
5029 if (fields & SAMR_FIELD_PROFILE_PATH)
5030 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5031 /* USER_ALL_ADMINCOMMENT */
5032 if (fields & SAMR_FIELD_COMMENT)
5033 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5034 /* USER_ALL_WORKSTATIONS */
5035 if (fields & SAMR_FIELD_WORKSTATIONS)
5036 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5037 /* USER_ALL_LOGONHOURS */
5038 if (fields & SAMR_FIELD_LOGON_HOURS)
5039 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5040 /* USER_ALL_ACCOUNTEXPIRES */
5041 if (fields & SAMR_FIELD_ACCT_EXPIRY)
5042 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5043 /* USER_ALL_USERACCOUNTCONTROL */
5044 if (fields & SAMR_FIELD_ACCT_FLAGS)
5045 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5046 /* USER_ALL_PARAMETERS */
5047 if (fields & SAMR_FIELD_PARAMETERS)
5048 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5049 /* USER_ALL_USERCOMMENT */
5050 if (fields & SAMR_FIELD_COMMENT)
5051 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5052 /* USER_ALL_COUNTRYCODE */
5053 if (fields & SAMR_FIELD_COUNTRY_CODE)
5054 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5055 /* USER_ALL_CODEPAGE */
5056 if (fields & SAMR_FIELD_CODE_PAGE)
5057 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5058 /* USER_ALL_NTPASSWORDPRESENT */
5059 if (fields & SAMR_FIELD_NT_PASSWORD_PRESENT)
5060 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5061 /* USER_ALL_LMPASSWORDPRESENT */
5062 if (fields & SAMR_FIELD_LM_PASSWORD_PRESENT)
5063 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5064 /* USER_ALL_PASSWORDEXPIRED */
5065 if (fields & SAMR_FIELD_EXPIRED_FLAG)
5066 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5068 return acc_required;
5071 /*******************************************************************
5072 samr_SetUserInfo
5073 ********************************************************************/
5075 NTSTATUS _samr_SetUserInfo(pipes_struct *p,
5076 struct samr_SetUserInfo *r)
5078 struct samr_user_info *uinfo;
5079 NTSTATUS status;
5080 struct samu *pwd = NULL;
5081 union samr_UserInfo *info = r->in.info;
5082 uint32_t acc_required = 0;
5083 uint32_t fields = 0;
5084 bool ret;
5086 DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
5088 /* This is tricky. A WinXP domain join sets
5089 (SAMR_USER_ACCESS_SET_PASSWORD|SAMR_USER_ACCESS_SET_ATTRIBUTES|SAMR_USER_ACCESS_GET_ATTRIBUTES)
5090 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
5091 standard Win32 API calls just ask for SAMR_USER_ACCESS_SET_PASSWORD in the SamrOpenUser().
5092 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
5093 we'll use the set from the WinXP join as the basis. */
5095 switch (r->in.level) {
5096 case 2: /* UserPreferencesInformation */
5097 /* USER_WRITE_ACCOUNT | USER_WRITE_PREFERENCES */
5098 acc_required = SAMR_USER_ACCESS_SET_ATTRIBUTES | SAMR_USER_ACCESS_SET_LOC_COM;
5099 break;
5100 case 4: /* UserLogonHoursInformation */
5101 case 6: /* UserNameInformation */
5102 case 7: /* UserAccountNameInformation */
5103 case 8: /* UserFullNameInformation */
5104 case 9: /* UserPrimaryGroupInformation */
5105 case 10: /* UserHomeInformation */
5106 case 11: /* UserScriptInformation */
5107 case 12: /* UserProfileInformation */
5108 case 13: /* UserAdminCommentInformation */
5109 case 14: /* UserWorkStationsInformation */
5110 case 16: /* UserControlInformation */
5111 case 17: /* UserExpiresInformation */
5112 case 20: /* UserParametersInformation */
5113 /* USER_WRITE_ACCOUNT */
5114 acc_required = SAMR_USER_ACCESS_SET_ATTRIBUTES;
5115 break;
5116 case 18: /* UserInternal1Information */
5117 /* FIXME: gd, this is a guess */
5118 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
5119 break;
5120 case 21: /* UserAllInformation */
5121 fields = info->info21.fields_present;
5122 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5123 break;
5124 case 23: /* UserInternal4Information */
5125 fields = info->info23.info.fields_present;
5126 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5127 break;
5128 case 25: /* UserInternal4InformationNew */
5129 fields = info->info25.info.fields_present;
5130 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5131 break;
5132 case 24: /* UserInternal5Information */
5133 case 26: /* UserInternal5InformationNew */
5134 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
5135 break;
5136 default:
5137 return NT_STATUS_INVALID_INFO_CLASS;
5140 uinfo = policy_handle_find(p, r->in.user_handle, acc_required, NULL,
5141 struct samr_user_info, &status);
5142 if (!NT_STATUS_IS_OK(status)) {
5143 return status;
5146 DEBUG(5, ("_samr_SetUserInfo: sid:%s, level:%d\n",
5147 sid_string_dbg(&uinfo->sid), r->in.level));
5149 if (info == NULL) {
5150 DEBUG(5, ("_samr_SetUserInfo: NULL info level\n"));
5151 return NT_STATUS_INVALID_INFO_CLASS;
5154 if (!(pwd = samu_new(NULL))) {
5155 return NT_STATUS_NO_MEMORY;
5158 become_root();
5159 ret = pdb_getsampwsid(pwd, &uinfo->sid);
5160 unbecome_root();
5162 if (!ret) {
5163 TALLOC_FREE(pwd);
5164 return NT_STATUS_NO_SUCH_USER;
5167 /* ================ BEGIN Privilege BLOCK ================ */
5169 become_root();
5171 /* ok! user info levels (lots: see MSDEV help), off we go... */
5173 switch (r->in.level) {
5175 case 2:
5176 status = set_user_info_2(p->mem_ctx,
5177 &info->info2, pwd);
5178 break;
5180 case 4:
5181 status = set_user_info_4(p->mem_ctx,
5182 &info->info4, pwd);
5183 break;
5185 case 6:
5186 status = set_user_info_6(p->mem_ctx,
5187 &info->info6, pwd);
5188 break;
5190 case 7:
5191 status = set_user_info_7(p->mem_ctx,
5192 &info->info7, pwd);
5193 break;
5195 case 8:
5196 status = set_user_info_8(p->mem_ctx,
5197 &info->info8, pwd);
5198 break;
5200 case 10:
5201 status = set_user_info_10(p->mem_ctx,
5202 &info->info10, pwd);
5203 break;
5205 case 11:
5206 status = set_user_info_11(p->mem_ctx,
5207 &info->info11, pwd);
5208 break;
5210 case 12:
5211 status = set_user_info_12(p->mem_ctx,
5212 &info->info12, pwd);
5213 break;
5215 case 13:
5216 status = set_user_info_13(p->mem_ctx,
5217 &info->info13, pwd);
5218 break;
5220 case 14:
5221 status = set_user_info_14(p->mem_ctx,
5222 &info->info14, pwd);
5223 break;
5225 case 16:
5226 status = set_user_info_16(p->mem_ctx,
5227 &info->info16, pwd);
5228 break;
5230 case 17:
5231 status = set_user_info_17(p->mem_ctx,
5232 &info->info17, pwd);
5233 break;
5235 case 18:
5236 /* Used by AS/U JRA. */
5237 status = set_user_info_18(&info->info18,
5238 p->mem_ctx,
5239 &p->server_info->user_session_key,
5240 pwd);
5241 break;
5243 case 20:
5244 status = set_user_info_20(p->mem_ctx,
5245 &info->info20, pwd);
5246 break;
5248 case 21:
5249 status = set_user_info_21(&info->info21,
5250 p->mem_ctx,
5251 &p->server_info->user_session_key,
5252 pwd);
5253 break;
5255 case 23:
5256 if (!p->server_info->user_session_key.length) {
5257 status = NT_STATUS_NO_USER_SESSION_KEY;
5259 arcfour_crypt_blob(info->info23.password.data, 516,
5260 &p->server_info->user_session_key);
5262 dump_data(100, info->info23.password.data, 516);
5264 status = set_user_info_23(p->mem_ctx,
5265 &info->info23, pwd);
5266 break;
5268 case 24:
5269 if (!p->server_info->user_session_key.length) {
5270 status = NT_STATUS_NO_USER_SESSION_KEY;
5272 arcfour_crypt_blob(info->info24.password.data,
5273 516,
5274 &p->server_info->user_session_key);
5276 dump_data(100, info->info24.password.data, 516);
5278 status = set_user_info_24(p->mem_ctx,
5279 &info->info24, pwd);
5280 break;
5282 case 25:
5283 if (!p->server_info->user_session_key.length) {
5284 status = NT_STATUS_NO_USER_SESSION_KEY;
5286 encode_or_decode_arc4_passwd_buffer(
5287 info->info25.password.data,
5288 &p->server_info->user_session_key);
5290 dump_data(100, info->info25.password.data, 532);
5292 status = set_user_info_25(p->mem_ctx,
5293 &info->info25, pwd);
5294 break;
5296 case 26:
5297 if (!p->server_info->user_session_key.length) {
5298 status = NT_STATUS_NO_USER_SESSION_KEY;
5300 encode_or_decode_arc4_passwd_buffer(
5301 info->info26.password.data,
5302 &p->server_info->user_session_key);
5304 dump_data(100, info->info26.password.data, 516);
5306 status = set_user_info_26(p->mem_ctx,
5307 &info->info26, pwd);
5308 break;
5310 default:
5311 status = NT_STATUS_INVALID_INFO_CLASS;
5314 TALLOC_FREE(pwd);
5316 unbecome_root();
5318 /* ================ END Privilege BLOCK ================ */
5320 if (NT_STATUS_IS_OK(status)) {
5321 force_flush_samr_cache(&uinfo->sid);
5324 return status;
5327 /*******************************************************************
5328 _samr_SetUserInfo2
5329 ********************************************************************/
5331 NTSTATUS _samr_SetUserInfo2(pipes_struct *p,
5332 struct samr_SetUserInfo2 *r)
5334 struct samr_SetUserInfo q;
5336 q.in.user_handle = r->in.user_handle;
5337 q.in.level = r->in.level;
5338 q.in.info = r->in.info;
5340 return _samr_SetUserInfo(p, &q);
5343 /*********************************************************************
5344 _samr_GetAliasMembership
5345 *********************************************************************/
5347 NTSTATUS _samr_GetAliasMembership(pipes_struct *p,
5348 struct samr_GetAliasMembership *r)
5350 size_t num_alias_rids;
5351 uint32 *alias_rids;
5352 struct samr_domain_info *dinfo;
5353 size_t i;
5355 NTSTATUS status;
5357 struct dom_sid *members;
5359 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__));
5361 dinfo = policy_handle_find(p, r->in.domain_handle,
5362 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS
5363 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
5364 struct samr_domain_info, &status);
5365 if (!NT_STATUS_IS_OK(status)) {
5366 return status;
5369 if (!sid_check_is_domain(&dinfo->sid) &&
5370 !sid_check_is_builtin(&dinfo->sid))
5371 return NT_STATUS_OBJECT_TYPE_MISMATCH;
5373 if (r->in.sids->num_sids) {
5374 members = TALLOC_ARRAY(p->mem_ctx, struct dom_sid, r->in.sids->num_sids);
5376 if (members == NULL)
5377 return NT_STATUS_NO_MEMORY;
5378 } else {
5379 members = NULL;
5382 for (i=0; i<r->in.sids->num_sids; i++)
5383 sid_copy(&members[i], r->in.sids->sids[i].sid);
5385 alias_rids = NULL;
5386 num_alias_rids = 0;
5388 become_root();
5389 status = pdb_enum_alias_memberships(p->mem_ctx, &dinfo->sid, members,
5390 r->in.sids->num_sids,
5391 &alias_rids, &num_alias_rids);
5392 unbecome_root();
5394 if (!NT_STATUS_IS_OK(status)) {
5395 return status;
5398 r->out.rids->count = num_alias_rids;
5399 r->out.rids->ids = alias_rids;
5401 if (r->out.rids->ids == NULL) {
5402 /* Windows domain clients don't accept a NULL ptr here */
5403 r->out.rids->ids = talloc_zero(p->mem_ctx, uint32_t);
5405 if (r->out.rids->ids == NULL) {
5406 return NT_STATUS_NO_MEMORY;
5409 return NT_STATUS_OK;
5412 /*********************************************************************
5413 _samr_GetMembersInAlias
5414 *********************************************************************/
5416 NTSTATUS _samr_GetMembersInAlias(pipes_struct *p,
5417 struct samr_GetMembersInAlias *r)
5419 struct samr_alias_info *ainfo;
5420 NTSTATUS status;
5421 size_t i;
5422 size_t num_sids = 0;
5423 struct lsa_SidPtr *sids = NULL;
5424 struct dom_sid *pdb_sids = NULL;
5426 ainfo = policy_handle_find(p, r->in.alias_handle,
5427 SAMR_ALIAS_ACCESS_GET_MEMBERS, NULL,
5428 struct samr_alias_info, &status);
5429 if (!NT_STATUS_IS_OK(status)) {
5430 return status;
5433 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5435 become_root();
5436 status = pdb_enum_aliasmem(&ainfo->sid, talloc_tos(), &pdb_sids,
5437 &num_sids);
5438 unbecome_root();
5440 if (!NT_STATUS_IS_OK(status)) {
5441 return status;
5444 if (num_sids) {
5445 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr, num_sids);
5446 if (sids == NULL) {
5447 TALLOC_FREE(pdb_sids);
5448 return NT_STATUS_NO_MEMORY;
5452 for (i = 0; i < num_sids; i++) {
5453 sids[i].sid = sid_dup_talloc(p->mem_ctx, &pdb_sids[i]);
5454 if (!sids[i].sid) {
5455 TALLOC_FREE(pdb_sids);
5456 return NT_STATUS_NO_MEMORY;
5460 r->out.sids->num_sids = num_sids;
5461 r->out.sids->sids = sids;
5463 TALLOC_FREE(pdb_sids);
5465 return NT_STATUS_OK;
5468 /*********************************************************************
5469 _samr_QueryGroupMember
5470 *********************************************************************/
5472 NTSTATUS _samr_QueryGroupMember(pipes_struct *p,
5473 struct samr_QueryGroupMember *r)
5475 struct samr_group_info *ginfo;
5476 size_t i, num_members;
5478 uint32 *rid=NULL;
5479 uint32 *attr=NULL;
5481 NTSTATUS status;
5482 struct samr_RidTypeArray *rids = NULL;
5484 ginfo = policy_handle_find(p, r->in.group_handle,
5485 SAMR_GROUP_ACCESS_GET_MEMBERS, NULL,
5486 struct samr_group_info, &status);
5487 if (!NT_STATUS_IS_OK(status)) {
5488 return status;
5491 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidTypeArray);
5492 if (!rids) {
5493 return NT_STATUS_NO_MEMORY;
5496 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5498 if (!sid_check_is_in_our_domain(&ginfo->sid)) {
5499 DEBUG(3, ("sid %s is not in our domain\n",
5500 sid_string_dbg(&ginfo->sid)));
5501 return NT_STATUS_NO_SUCH_GROUP;
5504 DEBUG(10, ("lookup on Domain SID\n"));
5506 become_root();
5507 status = pdb_enum_group_members(p->mem_ctx, &ginfo->sid,
5508 &rid, &num_members);
5509 unbecome_root();
5511 if (!NT_STATUS_IS_OK(status))
5512 return status;
5514 if (num_members) {
5515 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
5516 if (attr == NULL) {
5517 return NT_STATUS_NO_MEMORY;
5519 } else {
5520 attr = NULL;
5523 for (i=0; i<num_members; i++)
5524 attr[i] = SID_NAME_USER;
5526 rids->count = num_members;
5527 rids->types = attr;
5528 rids->rids = rid;
5530 *r->out.rids = rids;
5532 return NT_STATUS_OK;
5535 /*********************************************************************
5536 _samr_AddAliasMember
5537 *********************************************************************/
5539 NTSTATUS _samr_AddAliasMember(pipes_struct *p,
5540 struct samr_AddAliasMember *r)
5542 struct samr_alias_info *ainfo;
5543 NTSTATUS status;
5545 ainfo = policy_handle_find(p, r->in.alias_handle,
5546 SAMR_ALIAS_ACCESS_ADD_MEMBER, NULL,
5547 struct samr_alias_info, &status);
5548 if (!NT_STATUS_IS_OK(status)) {
5549 return status;
5552 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5554 /******** BEGIN SeAddUsers BLOCK *********/
5556 become_root();
5557 status = pdb_add_aliasmem(&ainfo->sid, r->in.sid);
5558 unbecome_root();
5560 /******** END SeAddUsers BLOCK *********/
5562 if (NT_STATUS_IS_OK(status)) {
5563 force_flush_samr_cache(&ainfo->sid);
5566 return status;
5569 /*********************************************************************
5570 _samr_DeleteAliasMember
5571 *********************************************************************/
5573 NTSTATUS _samr_DeleteAliasMember(pipes_struct *p,
5574 struct samr_DeleteAliasMember *r)
5576 struct samr_alias_info *ainfo;
5577 NTSTATUS status;
5579 ainfo = policy_handle_find(p, r->in.alias_handle,
5580 SAMR_ALIAS_ACCESS_REMOVE_MEMBER, NULL,
5581 struct samr_alias_info, &status);
5582 if (!NT_STATUS_IS_OK(status)) {
5583 return status;
5586 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
5587 sid_string_dbg(&ainfo->sid)));
5589 /******** BEGIN SeAddUsers BLOCK *********/
5591 become_root();
5592 status = pdb_del_aliasmem(&ainfo->sid, r->in.sid);
5593 unbecome_root();
5595 /******** END SeAddUsers BLOCK *********/
5597 if (NT_STATUS_IS_OK(status)) {
5598 force_flush_samr_cache(&ainfo->sid);
5601 return status;
5604 /*********************************************************************
5605 _samr_AddGroupMember
5606 *********************************************************************/
5608 NTSTATUS _samr_AddGroupMember(pipes_struct *p,
5609 struct samr_AddGroupMember *r)
5611 struct samr_group_info *ginfo;
5612 NTSTATUS status;
5613 uint32 group_rid;
5615 ginfo = policy_handle_find(p, r->in.group_handle,
5616 SAMR_GROUP_ACCESS_ADD_MEMBER, NULL,
5617 struct samr_group_info, &status);
5618 if (!NT_STATUS_IS_OK(status)) {
5619 return status;
5622 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5624 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5625 &group_rid)) {
5626 return NT_STATUS_INVALID_HANDLE;
5629 /******** BEGIN SeAddUsers BLOCK *********/
5631 become_root();
5632 status = pdb_add_groupmem(p->mem_ctx, group_rid, r->in.rid);
5633 unbecome_root();
5635 /******** END SeAddUsers BLOCK *********/
5637 force_flush_samr_cache(&ginfo->sid);
5639 return status;
5642 /*********************************************************************
5643 _samr_DeleteGroupMember
5644 *********************************************************************/
5646 NTSTATUS _samr_DeleteGroupMember(pipes_struct *p,
5647 struct samr_DeleteGroupMember *r)
5650 struct samr_group_info *ginfo;
5651 NTSTATUS status;
5652 uint32 group_rid;
5655 * delete the group member named r->in.rid
5656 * who is a member of the sid associated with the handle
5657 * the rid is a user's rid as the group is a domain group.
5660 ginfo = policy_handle_find(p, r->in.group_handle,
5661 SAMR_GROUP_ACCESS_REMOVE_MEMBER, NULL,
5662 struct samr_group_info, &status);
5663 if (!NT_STATUS_IS_OK(status)) {
5664 return status;
5667 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5668 &group_rid)) {
5669 return NT_STATUS_INVALID_HANDLE;
5672 /******** BEGIN SeAddUsers BLOCK *********/
5674 become_root();
5675 status = pdb_del_groupmem(p->mem_ctx, group_rid, r->in.rid);
5676 unbecome_root();
5678 /******** END SeAddUsers BLOCK *********/
5680 force_flush_samr_cache(&ginfo->sid);
5682 return status;
5685 /*********************************************************************
5686 _samr_DeleteUser
5687 *********************************************************************/
5689 NTSTATUS _samr_DeleteUser(pipes_struct *p,
5690 struct samr_DeleteUser *r)
5692 struct samr_user_info *uinfo;
5693 NTSTATUS status;
5694 struct samu *sam_pass=NULL;
5695 bool ret;
5697 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__));
5699 uinfo = policy_handle_find(p, r->in.user_handle,
5700 STD_RIGHT_DELETE_ACCESS, NULL,
5701 struct samr_user_info, &status);
5702 if (!NT_STATUS_IS_OK(status)) {
5703 return status;
5706 if (!sid_check_is_in_our_domain(&uinfo->sid))
5707 return NT_STATUS_CANNOT_DELETE;
5709 /* check if the user exists before trying to delete */
5710 if ( !(sam_pass = samu_new( NULL )) ) {
5711 return NT_STATUS_NO_MEMORY;
5714 become_root();
5715 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
5716 unbecome_root();
5718 if(!ret) {
5719 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
5720 sid_string_dbg(&uinfo->sid)));
5721 TALLOC_FREE(sam_pass);
5722 return NT_STATUS_NO_SUCH_USER;
5725 /******** BEGIN SeAddUsers BLOCK *********/
5727 become_root();
5728 status = pdb_delete_user(p->mem_ctx, sam_pass);
5729 unbecome_root();
5731 /******** END SeAddUsers BLOCK *********/
5733 if ( !NT_STATUS_IS_OK(status) ) {
5734 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
5735 "user %s: %s.\n", pdb_get_username(sam_pass),
5736 nt_errstr(status)));
5737 TALLOC_FREE(sam_pass);
5738 return status;
5742 TALLOC_FREE(sam_pass);
5744 force_flush_samr_cache(&uinfo->sid);
5746 if (!close_policy_hnd(p, r->in.user_handle))
5747 return NT_STATUS_OBJECT_NAME_INVALID;
5749 ZERO_STRUCTP(r->out.user_handle);
5751 return NT_STATUS_OK;
5754 /*********************************************************************
5755 _samr_DeleteDomainGroup
5756 *********************************************************************/
5758 NTSTATUS _samr_DeleteDomainGroup(pipes_struct *p,
5759 struct samr_DeleteDomainGroup *r)
5761 struct samr_group_info *ginfo;
5762 NTSTATUS status;
5763 uint32 group_rid;
5765 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__));
5767 ginfo = policy_handle_find(p, r->in.group_handle,
5768 STD_RIGHT_DELETE_ACCESS, NULL,
5769 struct samr_group_info, &status);
5770 if (!NT_STATUS_IS_OK(status)) {
5771 return status;
5774 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5776 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5777 &group_rid)) {
5778 return NT_STATUS_NO_SUCH_GROUP;
5781 /******** BEGIN SeAddUsers BLOCK *********/
5783 become_root();
5784 status = pdb_delete_dom_group(p->mem_ctx, group_rid);
5785 unbecome_root();
5787 /******** END SeAddUsers BLOCK *********/
5789 if ( !NT_STATUS_IS_OK(status) ) {
5790 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
5791 "entry for group %s: %s\n",
5792 sid_string_dbg(&ginfo->sid),
5793 nt_errstr(status)));
5794 return status;
5797 force_flush_samr_cache(&ginfo->sid);
5799 if (!close_policy_hnd(p, r->in.group_handle))
5800 return NT_STATUS_OBJECT_NAME_INVALID;
5802 return NT_STATUS_OK;
5805 /*********************************************************************
5806 _samr_DeleteDomAlias
5807 *********************************************************************/
5809 NTSTATUS _samr_DeleteDomAlias(pipes_struct *p,
5810 struct samr_DeleteDomAlias *r)
5812 struct samr_alias_info *ainfo;
5813 NTSTATUS status;
5815 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__));
5817 ainfo = policy_handle_find(p, r->in.alias_handle,
5818 STD_RIGHT_DELETE_ACCESS, NULL,
5819 struct samr_alias_info, &status);
5820 if (!NT_STATUS_IS_OK(status)) {
5821 return status;
5824 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5826 /* Don't let Windows delete builtin groups */
5828 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
5829 return NT_STATUS_SPECIAL_ACCOUNT;
5832 if (!sid_check_is_in_our_domain(&ainfo->sid))
5833 return NT_STATUS_NO_SUCH_ALIAS;
5835 DEBUG(10, ("lookup on Local SID\n"));
5837 /******** BEGIN SeAddUsers BLOCK *********/
5839 become_root();
5840 /* Have passdb delete the alias */
5841 status = pdb_delete_alias(&ainfo->sid);
5842 unbecome_root();
5844 /******** END SeAddUsers BLOCK *********/
5846 if ( !NT_STATUS_IS_OK(status))
5847 return status;
5849 force_flush_samr_cache(&ainfo->sid);
5851 if (!close_policy_hnd(p, r->in.alias_handle))
5852 return NT_STATUS_OBJECT_NAME_INVALID;
5854 return NT_STATUS_OK;
5857 /*********************************************************************
5858 _samr_CreateDomainGroup
5859 *********************************************************************/
5861 NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
5862 struct samr_CreateDomainGroup *r)
5865 NTSTATUS status;
5866 const char *name;
5867 struct samr_domain_info *dinfo;
5868 struct samr_group_info *ginfo;
5870 dinfo = policy_handle_find(p, r->in.domain_handle,
5871 SAMR_DOMAIN_ACCESS_CREATE_GROUP, NULL,
5872 struct samr_domain_info, &status);
5873 if (!NT_STATUS_IS_OK(status)) {
5874 return status;
5877 if (!sid_check_is_domain(&dinfo->sid)) {
5878 return NT_STATUS_ACCESS_DENIED;
5881 name = r->in.name->string;
5882 if (name == NULL) {
5883 return NT_STATUS_NO_MEMORY;
5886 status = can_create(p->mem_ctx, name);
5887 if (!NT_STATUS_IS_OK(status)) {
5888 return status;
5891 /******** BEGIN SeAddUsers BLOCK *********/
5893 become_root();
5894 /* check that we successfully create the UNIX group */
5895 status = pdb_create_dom_group(p->mem_ctx, name, r->out.rid);
5896 unbecome_root();
5898 /******** END SeAddUsers BLOCK *********/
5900 /* check if we should bail out here */
5902 if ( !NT_STATUS_IS_OK(status) )
5903 return status;
5905 ginfo = policy_handle_create(p, r->out.group_handle,
5906 GENERIC_RIGHTS_GROUP_ALL_ACCESS,
5907 struct samr_group_info, &status);
5908 if (!NT_STATUS_IS_OK(status)) {
5909 return status;
5911 sid_compose(&ginfo->sid, &dinfo->sid, *r->out.rid);
5913 force_flush_samr_cache(&dinfo->sid);
5915 return NT_STATUS_OK;
5918 /*********************************************************************
5919 _samr_CreateDomAlias
5920 *********************************************************************/
5922 NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
5923 struct samr_CreateDomAlias *r)
5925 struct dom_sid info_sid;
5926 const char *name = NULL;
5927 struct samr_domain_info *dinfo;
5928 struct samr_alias_info *ainfo;
5929 gid_t gid;
5930 NTSTATUS result;
5932 dinfo = policy_handle_find(p, r->in.domain_handle,
5933 SAMR_DOMAIN_ACCESS_CREATE_ALIAS, NULL,
5934 struct samr_domain_info, &result);
5935 if (!NT_STATUS_IS_OK(result)) {
5936 return result;
5939 if (!sid_check_is_domain(&dinfo->sid)) {
5940 return NT_STATUS_ACCESS_DENIED;
5943 name = r->in.alias_name->string;
5945 result = can_create(p->mem_ctx, name);
5946 if (!NT_STATUS_IS_OK(result)) {
5947 return result;
5950 /******** BEGIN SeAddUsers BLOCK *********/
5952 become_root();
5953 /* Have passdb create the alias */
5954 result = pdb_create_alias(name, r->out.rid);
5955 unbecome_root();
5957 /******** END SeAddUsers BLOCK *********/
5959 if (!NT_STATUS_IS_OK(result)) {
5960 DEBUG(10, ("pdb_create_alias failed: %s\n",
5961 nt_errstr(result)));
5962 return result;
5965 sid_compose(&info_sid, &dinfo->sid, *r->out.rid);
5967 if (!sid_to_gid(&info_sid, &gid)) {
5968 DEBUG(10, ("Could not find alias just created\n"));
5969 return NT_STATUS_ACCESS_DENIED;
5972 /* check if the group has been successfully created */
5973 if ( getgrgid(gid) == NULL ) {
5974 DEBUG(10, ("getgrgid(%u) of just created alias failed\n",
5975 (unsigned int)gid));
5976 return NT_STATUS_ACCESS_DENIED;
5979 ainfo = policy_handle_create(p, r->out.alias_handle,
5980 GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
5981 struct samr_alias_info, &result);
5982 if (!NT_STATUS_IS_OK(result)) {
5983 return result;
5985 ainfo->sid = info_sid;
5987 force_flush_samr_cache(&info_sid);
5989 return NT_STATUS_OK;
5992 /*********************************************************************
5993 _samr_QueryGroupInfo
5994 *********************************************************************/
5996 NTSTATUS _samr_QueryGroupInfo(pipes_struct *p,
5997 struct samr_QueryGroupInfo *r)
5999 struct samr_group_info *ginfo;
6000 NTSTATUS status;
6001 GROUP_MAP map;
6002 union samr_GroupInfo *info = NULL;
6003 bool ret;
6004 uint32_t attributes = SE_GROUP_MANDATORY |
6005 SE_GROUP_ENABLED_BY_DEFAULT |
6006 SE_GROUP_ENABLED;
6007 const char *group_name = NULL;
6008 const char *group_description = NULL;
6010 ginfo = policy_handle_find(p, r->in.group_handle,
6011 SAMR_GROUP_ACCESS_LOOKUP_INFO, NULL,
6012 struct samr_group_info, &status);
6013 if (!NT_STATUS_IS_OK(status)) {
6014 return status;
6017 become_root();
6018 ret = get_domain_group_from_sid(ginfo->sid, &map);
6019 unbecome_root();
6020 if (!ret)
6021 return NT_STATUS_INVALID_HANDLE;
6023 /* FIXME: map contains fstrings */
6024 group_name = talloc_strdup(r, map.nt_name);
6025 group_description = talloc_strdup(r, map.comment);
6027 info = TALLOC_ZERO_P(p->mem_ctx, union samr_GroupInfo);
6028 if (!info) {
6029 return NT_STATUS_NO_MEMORY;
6032 switch (r->in.level) {
6033 case 1: {
6034 uint32 *members;
6035 size_t num_members;
6037 become_root();
6038 status = pdb_enum_group_members(
6039 p->mem_ctx, &ginfo->sid, &members,
6040 &num_members);
6041 unbecome_root();
6043 if (!NT_STATUS_IS_OK(status)) {
6044 return status;
6047 info->all.name.string = group_name;
6048 info->all.attributes = attributes;
6049 info->all.num_members = num_members;
6050 info->all.description.string = group_description;
6051 break;
6053 case 2:
6054 info->name.string = group_name;
6055 break;
6056 case 3:
6057 info->attributes.attributes = attributes;
6058 break;
6059 case 4:
6060 info->description.string = group_description;
6061 break;
6062 case 5: {
6064 uint32 *members;
6065 size_t num_members;
6069 become_root();
6070 status = pdb_enum_group_members(
6071 p->mem_ctx, &ginfo->sid, &members,
6072 &num_members);
6073 unbecome_root();
6075 if (!NT_STATUS_IS_OK(status)) {
6076 return status;
6079 info->all2.name.string = group_name;
6080 info->all2.attributes = attributes;
6081 info->all2.num_members = 0; /* num_members - in w2k3 this is always 0 */
6082 info->all2.description.string = group_description;
6084 break;
6086 default:
6087 return NT_STATUS_INVALID_INFO_CLASS;
6090 *r->out.info = info;
6092 return NT_STATUS_OK;
6095 /*********************************************************************
6096 _samr_SetGroupInfo
6097 *********************************************************************/
6099 NTSTATUS _samr_SetGroupInfo(pipes_struct *p,
6100 struct samr_SetGroupInfo *r)
6102 struct samr_group_info *ginfo;
6103 GROUP_MAP map;
6104 NTSTATUS status;
6105 bool ret;
6107 ginfo = policy_handle_find(p, r->in.group_handle,
6108 SAMR_GROUP_ACCESS_SET_INFO, NULL,
6109 struct samr_group_info, &status);
6110 if (!NT_STATUS_IS_OK(status)) {
6111 return status;
6114 become_root();
6115 ret = get_domain_group_from_sid(ginfo->sid, &map);
6116 unbecome_root();
6117 if (!ret)
6118 return NT_STATUS_NO_SUCH_GROUP;
6120 switch (r->in.level) {
6121 case 2:
6122 fstrcpy(map.nt_name, r->in.info->name.string);
6123 break;
6124 case 3:
6125 break;
6126 case 4:
6127 fstrcpy(map.comment, r->in.info->description.string);
6128 break;
6129 default:
6130 return NT_STATUS_INVALID_INFO_CLASS;
6133 /******** BEGIN SeAddUsers BLOCK *********/
6135 become_root();
6136 status = pdb_update_group_mapping_entry(&map);
6137 unbecome_root();
6139 /******** End SeAddUsers BLOCK *********/
6141 if (NT_STATUS_IS_OK(status)) {
6142 force_flush_samr_cache(&ginfo->sid);
6145 return status;
6148 /*********************************************************************
6149 _samr_SetAliasInfo
6150 *********************************************************************/
6152 NTSTATUS _samr_SetAliasInfo(pipes_struct *p,
6153 struct samr_SetAliasInfo *r)
6155 struct samr_alias_info *ainfo;
6156 struct acct_info info;
6157 NTSTATUS status;
6159 ainfo = policy_handle_find(p, r->in.alias_handle,
6160 SAMR_ALIAS_ACCESS_SET_INFO, NULL,
6161 struct samr_alias_info, &status);
6162 if (!NT_STATUS_IS_OK(status)) {
6163 return status;
6166 /* get the current group information */
6168 become_root();
6169 status = pdb_get_aliasinfo( &ainfo->sid, &info );
6170 unbecome_root();
6172 if ( !NT_STATUS_IS_OK(status))
6173 return status;
6175 switch (r->in.level) {
6176 case ALIASINFONAME:
6178 fstring group_name;
6180 /* We currently do not support renaming groups in the
6181 the BUILTIN domain. Refer to util_builtin.c to understand
6182 why. The eventually needs to be fixed to be like Windows
6183 where you can rename builtin groups, just not delete them */
6185 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
6186 return NT_STATUS_SPECIAL_ACCOUNT;
6189 /* There has to be a valid name (and it has to be different) */
6191 if ( !r->in.info->name.string )
6192 return NT_STATUS_INVALID_PARAMETER;
6194 /* If the name is the same just reply "ok". Yes this
6195 doesn't allow you to change the case of a group name. */
6197 if ( strequal( r->in.info->name.string, info.acct_name ) )
6198 return NT_STATUS_OK;
6200 fstrcpy( info.acct_name, r->in.info->name.string);
6202 /* make sure the name doesn't already exist as a user
6203 or local group */
6205 fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
6206 status = can_create( p->mem_ctx, group_name );
6207 if ( !NT_STATUS_IS_OK( status ) )
6208 return status;
6209 break;
6211 case ALIASINFODESCRIPTION:
6212 if (r->in.info->description.string) {
6213 fstrcpy(info.acct_desc,
6214 r->in.info->description.string);
6215 } else {
6216 fstrcpy( info.acct_desc, "" );
6218 break;
6219 default:
6220 return NT_STATUS_INVALID_INFO_CLASS;
6223 /******** BEGIN SeAddUsers BLOCK *********/
6225 become_root();
6226 status = pdb_set_aliasinfo( &ainfo->sid, &info );
6227 unbecome_root();
6229 /******** End SeAddUsers BLOCK *********/
6231 if (NT_STATUS_IS_OK(status))
6232 force_flush_samr_cache(&ainfo->sid);
6234 return status;
6237 /****************************************************************
6238 _samr_GetDomPwInfo
6239 ****************************************************************/
6241 NTSTATUS _samr_GetDomPwInfo(pipes_struct *p,
6242 struct samr_GetDomPwInfo *r)
6244 uint32_t min_password_length = 0;
6245 uint32_t password_properties = 0;
6247 /* Perform access check. Since this rpc does not require a
6248 policy handle it will not be caught by the access checks on
6249 SAMR_CONNECT or SAMR_CONNECT_ANON. */
6251 if (!pipe_access_check(p)) {
6252 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
6253 return NT_STATUS_ACCESS_DENIED;
6256 become_root();
6257 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
6258 &min_password_length);
6259 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
6260 &password_properties);
6261 unbecome_root();
6263 if (lp_check_password_script() && *lp_check_password_script()) {
6264 password_properties |= DOMAIN_PASSWORD_COMPLEX;
6267 r->out.info->min_password_length = min_password_length;
6268 r->out.info->password_properties = password_properties;
6270 return NT_STATUS_OK;
6273 /*********************************************************************
6274 _samr_OpenGroup
6275 *********************************************************************/
6277 NTSTATUS _samr_OpenGroup(pipes_struct *p,
6278 struct samr_OpenGroup *r)
6281 struct dom_sid info_sid;
6282 GROUP_MAP map;
6283 struct samr_domain_info *dinfo;
6284 struct samr_group_info *ginfo;
6285 struct security_descriptor *psd = NULL;
6286 uint32 acc_granted;
6287 uint32 des_access = r->in.access_mask;
6288 size_t sd_size;
6289 NTSTATUS status;
6290 bool ret;
6291 SE_PRIV se_rights;
6293 dinfo = policy_handle_find(p, r->in.domain_handle,
6294 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
6295 struct samr_domain_info, &status);
6296 if (!NT_STATUS_IS_OK(status)) {
6297 return status;
6300 /*check if access can be granted as requested by client. */
6301 map_max_allowed_access(p->server_info->ptok,
6302 &p->server_info->utok,
6303 &des_access);
6305 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
6306 se_map_generic(&des_access,&grp_generic_mapping);
6308 se_priv_copy( &se_rights, &se_add_users );
6310 status = access_check_object(psd, p->server_info->ptok,
6311 &se_rights, GENERIC_RIGHTS_GROUP_ALL_ACCESS,
6312 des_access, &acc_granted, "_samr_OpenGroup");
6314 if ( !NT_STATUS_IS_OK(status) )
6315 return status;
6317 /* this should not be hard-coded like this */
6319 if (!sid_check_is_domain(&dinfo->sid)) {
6320 return NT_STATUS_ACCESS_DENIED;
6323 sid_compose(&info_sid, &dinfo->sid, r->in.rid);
6325 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n",
6326 sid_string_dbg(&info_sid)));
6328 /* check if that group really exists */
6329 become_root();
6330 ret = get_domain_group_from_sid(info_sid, &map);
6331 unbecome_root();
6332 if (!ret)
6333 return NT_STATUS_NO_SUCH_GROUP;
6335 ginfo = policy_handle_create(p, r->out.group_handle,
6336 acc_granted,
6337 struct samr_group_info, &status);
6338 if (!NT_STATUS_IS_OK(status)) {
6339 return status;
6341 ginfo->sid = info_sid;
6343 return NT_STATUS_OK;
6346 /*********************************************************************
6347 _samr_RemoveMemberFromForeignDomain
6348 *********************************************************************/
6350 NTSTATUS _samr_RemoveMemberFromForeignDomain(pipes_struct *p,
6351 struct samr_RemoveMemberFromForeignDomain *r)
6353 struct samr_domain_info *dinfo;
6354 NTSTATUS result;
6356 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
6357 sid_string_dbg(r->in.sid)));
6359 /* Find the policy handle. Open a policy on it. */
6361 dinfo = policy_handle_find(p, r->in.domain_handle,
6362 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
6363 struct samr_domain_info, &result);
6364 if (!NT_STATUS_IS_OK(result)) {
6365 return result;
6368 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
6369 sid_string_dbg(&dinfo->sid)));
6371 /* we can only delete a user from a group since we don't have
6372 nested groups anyways. So in the latter case, just say OK */
6374 /* TODO: The above comment nowadays is bogus. Since we have nested
6375 * groups now, and aliases members are never reported out of the unix
6376 * group membership, the "just say OK" makes this call a no-op. For
6377 * us. This needs fixing however. */
6379 /* I've only ever seen this in the wild when deleting a user from
6380 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
6381 * is the user about to be deleted. I very much suspect this is the
6382 * only application of this call. To verify this, let people report
6383 * other cases. */
6385 if (!sid_check_is_builtin(&dinfo->sid)) {
6386 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
6387 "global_sam_sid() = %s\n",
6388 sid_string_dbg(&dinfo->sid),
6389 sid_string_dbg(get_global_sam_sid())));
6390 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
6391 return NT_STATUS_OK;
6394 force_flush_samr_cache(&dinfo->sid);
6396 result = NT_STATUS_OK;
6398 return result;
6401 /*******************************************************************
6402 _samr_QueryDomainInfo2
6403 ********************************************************************/
6405 NTSTATUS _samr_QueryDomainInfo2(pipes_struct *p,
6406 struct samr_QueryDomainInfo2 *r)
6408 struct samr_QueryDomainInfo q;
6410 q.in.domain_handle = r->in.domain_handle;
6411 q.in.level = r->in.level;
6413 q.out.info = r->out.info;
6415 return _samr_QueryDomainInfo(p, &q);
6418 /*******************************************************************
6419 ********************************************************************/
6421 static NTSTATUS set_dom_info_1(TALLOC_CTX *mem_ctx,
6422 struct samr_DomInfo1 *r)
6424 time_t u_expire, u_min_age;
6426 u_expire = nt_time_to_unix_abs((NTTIME *)&r->max_password_age);
6427 u_min_age = nt_time_to_unix_abs((NTTIME *)&r->min_password_age);
6429 pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
6430 (uint32_t)r->min_password_length);
6431 pdb_set_account_policy(PDB_POLICY_PASSWORD_HISTORY,
6432 (uint32_t)r->password_history_length);
6433 pdb_set_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
6434 (uint32_t)r->password_properties);
6435 pdb_set_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, (int)u_expire);
6436 pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, (int)u_min_age);
6438 return NT_STATUS_OK;
6441 /*******************************************************************
6442 ********************************************************************/
6444 static NTSTATUS set_dom_info_3(TALLOC_CTX *mem_ctx,
6445 struct samr_DomInfo3 *r)
6447 time_t u_logout;
6449 u_logout = nt_time_to_unix_abs((NTTIME *)&r->force_logoff_time);
6451 pdb_set_account_policy(PDB_POLICY_TIME_TO_LOGOUT, (int)u_logout);
6453 return NT_STATUS_OK;
6456 /*******************************************************************
6457 ********************************************************************/
6459 static NTSTATUS set_dom_info_12(TALLOC_CTX *mem_ctx,
6460 struct samr_DomInfo12 *r)
6462 time_t u_lock_duration, u_reset_time;
6464 u_lock_duration = nt_time_to_unix_abs((NTTIME *)&r->lockout_duration);
6465 if (u_lock_duration != -1) {
6466 u_lock_duration /= 60;
6469 u_reset_time = nt_time_to_unix_abs((NTTIME *)&r->lockout_window)/60;
6471 pdb_set_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
6472 pdb_set_account_policy(PDB_POLICY_RESET_COUNT_TIME, (int)u_reset_time);
6473 pdb_set_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT,
6474 (uint32_t)r->lockout_threshold);
6476 return NT_STATUS_OK;
6479 /*******************************************************************
6480 _samr_SetDomainInfo
6481 ********************************************************************/
6483 NTSTATUS _samr_SetDomainInfo(pipes_struct *p,
6484 struct samr_SetDomainInfo *r)
6486 struct samr_domain_info *dinfo;
6487 NTSTATUS status;
6488 uint32_t acc_required = 0;
6490 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
6492 switch (r->in.level) {
6493 case 1: /* DomainPasswordInformation */
6494 case 12: /* DomainLockoutInformation */
6495 /* DOMAIN_WRITE_PASSWORD_PARAMETERS */
6496 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_1;
6497 break;
6498 case 3: /* DomainLogoffInformation */
6499 case 4: /* DomainOemInformation */
6500 /* DOMAIN_WRITE_OTHER_PARAMETERS */
6501 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_2;
6502 break;
6503 case 6: /* DomainReplicationInformation */
6504 case 9: /* DomainStateInformation */
6505 case 7: /* DomainServerRoleInformation */
6506 /* DOMAIN_ADMINISTER_SERVER */
6507 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_3;
6508 break;
6509 default:
6510 return NT_STATUS_INVALID_INFO_CLASS;
6513 dinfo = policy_handle_find(p, r->in.domain_handle,
6514 acc_required, NULL,
6515 struct samr_domain_info, &status);
6516 if (!NT_STATUS_IS_OK(status)) {
6517 return status;
6520 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r->in.level));
6522 switch (r->in.level) {
6523 case 1:
6524 status = set_dom_info_1(p->mem_ctx, &r->in.info->info1);
6525 break;
6526 case 3:
6527 status = set_dom_info_3(p->mem_ctx, &r->in.info->info3);
6528 break;
6529 case 4:
6530 break;
6531 case 6:
6532 break;
6533 case 7:
6534 break;
6535 case 9:
6536 break;
6537 case 12:
6538 status = set_dom_info_12(p->mem_ctx, &r->in.info->info12);
6539 break;
6540 default:
6541 return NT_STATUS_INVALID_INFO_CLASS;
6544 if (!NT_STATUS_IS_OK(status)) {
6545 return status;
6548 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
6550 return NT_STATUS_OK;
6553 /****************************************************************
6554 _samr_GetDisplayEnumerationIndex
6555 ****************************************************************/
6557 NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
6558 struct samr_GetDisplayEnumerationIndex *r)
6560 struct samr_domain_info *dinfo;
6561 uint32_t max_entries = (uint32_t) -1;
6562 uint32_t enum_context = 0;
6563 int i;
6564 uint32_t num_account = 0;
6565 struct samr_displayentry *entries = NULL;
6566 NTSTATUS status;
6568 DEBUG(5,("_samr_GetDisplayEnumerationIndex: %d\n", __LINE__));
6570 dinfo = policy_handle_find(p, r->in.domain_handle,
6571 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
6572 struct samr_domain_info, &status);
6573 if (!NT_STATUS_IS_OK(status)) {
6574 return status;
6577 if ((r->in.level < 1) || (r->in.level > 3)) {
6578 DEBUG(0,("_samr_GetDisplayEnumerationIndex: "
6579 "Unknown info level (%u)\n",
6580 r->in.level));
6581 return NT_STATUS_INVALID_INFO_CLASS;
6584 become_root();
6586 /* The following done as ROOT. Don't return without unbecome_root(). */
6588 switch (r->in.level) {
6589 case 1:
6590 if (dinfo->disp_info->users == NULL) {
6591 dinfo->disp_info->users = pdb_search_users(
6592 dinfo->disp_info, ACB_NORMAL);
6593 if (dinfo->disp_info->users == NULL) {
6594 unbecome_root();
6595 return NT_STATUS_ACCESS_DENIED;
6597 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6598 "starting user enumeration at index %u\n",
6599 (unsigned int)enum_context));
6600 } else {
6601 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6602 "using cached user enumeration at index %u\n",
6603 (unsigned int)enum_context));
6605 num_account = pdb_search_entries(dinfo->disp_info->users,
6606 enum_context, max_entries,
6607 &entries);
6608 break;
6609 case 2:
6610 if (dinfo->disp_info->machines == NULL) {
6611 dinfo->disp_info->machines = pdb_search_users(
6612 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
6613 if (dinfo->disp_info->machines == NULL) {
6614 unbecome_root();
6615 return NT_STATUS_ACCESS_DENIED;
6617 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6618 "starting machine enumeration at index %u\n",
6619 (unsigned int)enum_context));
6620 } else {
6621 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6622 "using cached machine enumeration at index %u\n",
6623 (unsigned int)enum_context));
6625 num_account = pdb_search_entries(dinfo->disp_info->machines,
6626 enum_context, max_entries,
6627 &entries);
6628 break;
6629 case 3:
6630 if (dinfo->disp_info->groups == NULL) {
6631 dinfo->disp_info->groups = pdb_search_groups(
6632 dinfo->disp_info);
6633 if (dinfo->disp_info->groups == NULL) {
6634 unbecome_root();
6635 return NT_STATUS_ACCESS_DENIED;
6637 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6638 "starting group enumeration at index %u\n",
6639 (unsigned int)enum_context));
6640 } else {
6641 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6642 "using cached group enumeration at index %u\n",
6643 (unsigned int)enum_context));
6645 num_account = pdb_search_entries(dinfo->disp_info->groups,
6646 enum_context, max_entries,
6647 &entries);
6648 break;
6649 default:
6650 unbecome_root();
6651 smb_panic("info class changed");
6652 break;
6655 unbecome_root();
6657 /* Ensure we cache this enumeration. */
6658 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
6660 DEBUG(10,("_samr_GetDisplayEnumerationIndex: looking for :%s\n",
6661 r->in.name->string));
6663 for (i=0; i<num_account; i++) {
6664 if (strequal(entries[i].account_name, r->in.name->string)) {
6665 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6666 "found %s at idx %d\n",
6667 r->in.name->string, i));
6668 *r->out.idx = i;
6669 return NT_STATUS_OK;
6673 /* assuming account_name lives at the very end */
6674 *r->out.idx = num_account;
6676 return NT_STATUS_NO_MORE_ENTRIES;
6679 /****************************************************************
6680 _samr_GetDisplayEnumerationIndex2
6681 ****************************************************************/
6683 NTSTATUS _samr_GetDisplayEnumerationIndex2(pipes_struct *p,
6684 struct samr_GetDisplayEnumerationIndex2 *r)
6686 struct samr_GetDisplayEnumerationIndex q;
6688 q.in.domain_handle = r->in.domain_handle;
6689 q.in.level = r->in.level;
6690 q.in.name = r->in.name;
6692 q.out.idx = r->out.idx;
6694 return _samr_GetDisplayEnumerationIndex(p, &q);
6697 /****************************************************************
6698 _samr_RidToSid
6699 ****************************************************************/
6701 NTSTATUS _samr_RidToSid(pipes_struct *p,
6702 struct samr_RidToSid *r)
6704 struct samr_domain_info *dinfo;
6705 NTSTATUS status;
6706 struct dom_sid sid;
6708 dinfo = policy_handle_find(p, r->in.domain_handle,
6709 0, NULL,
6710 struct samr_domain_info, &status);
6711 if (!NT_STATUS_IS_OK(status)) {
6712 return status;
6715 if (!sid_compose(&sid, &dinfo->sid, r->in.rid)) {
6716 return NT_STATUS_NO_MEMORY;
6719 *r->out.sid = sid_dup_talloc(p->mem_ctx, &sid);
6720 if (!*r->out.sid) {
6721 return NT_STATUS_NO_MEMORY;
6724 return NT_STATUS_OK;
6727 /****************************************************************
6728 ****************************************************************/
6730 static enum samr_ValidationStatus samr_ValidatePassword_Change(TALLOC_CTX *mem_ctx,
6731 const struct samr_PwInfo *dom_pw_info,
6732 const struct samr_ValidatePasswordReq2 *req,
6733 struct samr_ValidatePasswordRepCtr *rep)
6735 NTSTATUS status;
6737 if (req->password.string == NULL) {
6738 return SAMR_VALIDATION_STATUS_SUCCESS;
6740 if (strlen(req->password.string) < dom_pw_info->min_password_length) {
6741 ZERO_STRUCT(rep->info);
6742 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
6744 if (dom_pw_info->password_properties & DOMAIN_PASSWORD_COMPLEX) {
6745 status = check_password_complexity(req->account.string,
6746 req->password.string,
6747 NULL);
6748 if (!NT_STATUS_IS_OK(status)) {
6749 ZERO_STRUCT(rep->info);
6750 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
6754 return SAMR_VALIDATION_STATUS_SUCCESS;
6757 /****************************************************************
6758 ****************************************************************/
6760 static enum samr_ValidationStatus samr_ValidatePassword_Reset(TALLOC_CTX *mem_ctx,
6761 const struct samr_PwInfo *dom_pw_info,
6762 const struct samr_ValidatePasswordReq3 *req,
6763 struct samr_ValidatePasswordRepCtr *rep)
6765 NTSTATUS status;
6767 if (req->password.string == NULL) {
6768 return SAMR_VALIDATION_STATUS_SUCCESS;
6770 if (strlen(req->password.string) < dom_pw_info->min_password_length) {
6771 ZERO_STRUCT(rep->info);
6772 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
6774 if (dom_pw_info->password_properties & DOMAIN_PASSWORD_COMPLEX) {
6775 status = check_password_complexity(req->account.string,
6776 req->password.string,
6777 NULL);
6778 if (!NT_STATUS_IS_OK(status)) {
6779 ZERO_STRUCT(rep->info);
6780 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
6784 return SAMR_VALIDATION_STATUS_SUCCESS;
6787 /****************************************************************
6788 _samr_ValidatePassword
6789 ****************************************************************/
6791 NTSTATUS _samr_ValidatePassword(pipes_struct *p,
6792 struct samr_ValidatePassword *r)
6794 union samr_ValidatePasswordRep *rep;
6795 NTSTATUS status;
6796 struct samr_GetDomPwInfo pw;
6797 struct samr_PwInfo dom_pw_info;
6799 if (r->in.level < 1 || r->in.level > 3) {
6800 return NT_STATUS_INVALID_INFO_CLASS;
6803 pw.in.domain_name = NULL;
6804 pw.out.info = &dom_pw_info;
6806 status = _samr_GetDomPwInfo(p, &pw);
6807 if (!NT_STATUS_IS_OK(status)) {
6808 return status;
6811 rep = talloc_zero(p->mem_ctx, union samr_ValidatePasswordRep);
6812 if (!rep) {
6813 return NT_STATUS_NO_MEMORY;
6816 switch (r->in.level) {
6817 case 1:
6818 status = NT_STATUS_NOT_SUPPORTED;
6819 break;
6820 case 2:
6821 rep->ctr2.status = samr_ValidatePassword_Change(p->mem_ctx,
6822 &dom_pw_info,
6823 &r->in.req->req2,
6824 &rep->ctr2);
6825 break;
6826 case 3:
6827 rep->ctr3.status = samr_ValidatePassword_Reset(p->mem_ctx,
6828 &dom_pw_info,
6829 &r->in.req->req3,
6830 &rep->ctr3);
6831 break;
6832 default:
6833 status = NT_STATUS_INVALID_INFO_CLASS;
6834 break;
6837 if (!NT_STATUS_IS_OK(status)) {
6838 talloc_free(rep);
6839 return status;
6842 *r->out.rep = rep;
6844 return NT_STATUS_OK;
6847 /****************************************************************
6848 ****************************************************************/
6850 NTSTATUS _samr_Shutdown(pipes_struct *p,
6851 struct samr_Shutdown *r)
6853 p->rng_fault_state = true;
6854 return NT_STATUS_NOT_IMPLEMENTED;
6857 /****************************************************************
6858 ****************************************************************/
6860 NTSTATUS _samr_SetMemberAttributesOfGroup(pipes_struct *p,
6861 struct samr_SetMemberAttributesOfGroup *r)
6863 p->rng_fault_state = true;
6864 return NT_STATUS_NOT_IMPLEMENTED;
6867 /****************************************************************
6868 ****************************************************************/
6870 NTSTATUS _samr_TestPrivateFunctionsDomain(pipes_struct *p,
6871 struct samr_TestPrivateFunctionsDomain *r)
6873 return NT_STATUS_NOT_IMPLEMENTED;
6876 /****************************************************************
6877 ****************************************************************/
6879 NTSTATUS _samr_TestPrivateFunctionsUser(pipes_struct *p,
6880 struct samr_TestPrivateFunctionsUser *r)
6882 return NT_STATUS_NOT_IMPLEMENTED;
6885 /****************************************************************
6886 ****************************************************************/
6888 NTSTATUS _samr_AddMultipleMembersToAlias(pipes_struct *p,
6889 struct samr_AddMultipleMembersToAlias *r)
6891 p->rng_fault_state = true;
6892 return NT_STATUS_NOT_IMPLEMENTED;
6895 /****************************************************************
6896 ****************************************************************/
6898 NTSTATUS _samr_RemoveMultipleMembersFromAlias(pipes_struct *p,
6899 struct samr_RemoveMultipleMembersFromAlias *r)
6901 p->rng_fault_state = true;
6902 return NT_STATUS_NOT_IMPLEMENTED;
6905 /****************************************************************
6906 ****************************************************************/
6908 NTSTATUS _samr_SetBootKeyInformation(pipes_struct *p,
6909 struct samr_SetBootKeyInformation *r)
6911 p->rng_fault_state = true;
6912 return NT_STATUS_NOT_IMPLEMENTED;
6915 /****************************************************************
6916 ****************************************************************/
6918 NTSTATUS _samr_GetBootKeyInformation(pipes_struct *p,
6919 struct samr_GetBootKeyInformation *r)
6921 p->rng_fault_state = true;
6922 return NT_STATUS_NOT_IMPLEMENTED;
6925 /****************************************************************
6926 ****************************************************************/
6928 NTSTATUS _samr_SetDsrmPassword(pipes_struct *p,
6929 struct samr_SetDsrmPassword *r)
6931 p->rng_fault_state = true;
6932 return NT_STATUS_NOT_IMPLEMENTED;