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.
35 #include "../libcli/auth/libcli_auth.h"
38 #define DBGC_CLASS DBGC_RPC_SRV
40 #define SAMR_USR_RIGHTS_WRITE_PW \
41 ( READ_CONTROL_ACCESS | \
42 SAMR_USER_ACCESS_CHANGE_PASSWORD | \
43 SAMR_USER_ACCESS_SET_LOC_COM)
44 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
45 ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
47 #define DISP_INFO_CACHE_TIMEOUT 10
49 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
50 #define MAX_SAM_ENTRIES_W95 50
52 struct samr_connect_info {
56 struct samr_domain_info {
58 struct disp_info *disp_info;
61 struct samr_user_info {
65 struct samr_group_info {
69 struct samr_alias_info {
73 typedef struct disp_info {
74 DOM_SID sid; /* identify which domain this is. */
75 struct pdb_search *users; /* querydispinfo 1 and 4 */
76 struct pdb_search *machines; /* querydispinfo 2 */
77 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
78 struct pdb_search *aliases; /* enumaliases */
81 struct pdb_search *enum_users; /* enumusers with a mask */
83 struct timed_event *cache_timeout_event; /* cache idle timeout
87 /* We keep a static list of these by SID as modern clients close down
88 all resources between each request in a complete enumeration. */
91 /* for use by the \PIPE\samr policy */
93 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
98 static const struct generic_mapping sam_generic_mapping = {
99 GENERIC_RIGHTS_SAM_READ,
100 GENERIC_RIGHTS_SAM_WRITE,
101 GENERIC_RIGHTS_SAM_EXECUTE,
102 GENERIC_RIGHTS_SAM_ALL_ACCESS};
103 static const struct generic_mapping dom_generic_mapping = {
104 GENERIC_RIGHTS_DOMAIN_READ,
105 GENERIC_RIGHTS_DOMAIN_WRITE,
106 GENERIC_RIGHTS_DOMAIN_EXECUTE,
107 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
108 static const struct generic_mapping usr_generic_mapping = {
109 GENERIC_RIGHTS_USER_READ,
110 GENERIC_RIGHTS_USER_WRITE,
111 GENERIC_RIGHTS_USER_EXECUTE,
112 GENERIC_RIGHTS_USER_ALL_ACCESS};
113 static const struct generic_mapping usr_nopwchange_generic_mapping = {
114 GENERIC_RIGHTS_USER_READ,
115 GENERIC_RIGHTS_USER_WRITE,
116 GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
117 GENERIC_RIGHTS_USER_ALL_ACCESS};
118 static const struct generic_mapping grp_generic_mapping = {
119 GENERIC_RIGHTS_GROUP_READ,
120 GENERIC_RIGHTS_GROUP_WRITE,
121 GENERIC_RIGHTS_GROUP_EXECUTE,
122 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
123 static const struct generic_mapping ali_generic_mapping = {
124 GENERIC_RIGHTS_ALIAS_READ,
125 GENERIC_RIGHTS_ALIAS_WRITE,
126 GENERIC_RIGHTS_ALIAS_EXECUTE,
127 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
129 /*******************************************************************
130 *******************************************************************/
132 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
133 const struct generic_mapping *map,
134 DOM_SID *sid, uint32 sid_access )
136 DOM_SID domadmin_sid;
137 SEC_ACE ace[5]; /* at most 5 entries */
142 /* basic access for Everyone */
144 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
145 map->generic_execute | map->generic_read, 0);
147 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
149 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
150 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
151 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
152 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
154 /* Add Full Access for Domain Admins if we are a DC */
157 sid_copy( &domadmin_sid, get_global_sam_sid() );
158 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
159 init_sec_ace(&ace[i++], &domadmin_sid,
160 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
163 /* if we have a sid, give it some special access */
166 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sid_access, 0);
169 /* create the security descriptor */
171 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
172 return NT_STATUS_NO_MEMORY;
174 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
175 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
176 psa, sd_size)) == NULL)
177 return NT_STATUS_NO_MEMORY;
182 /*******************************************************************
183 Checks if access to an object should be granted, and returns that
184 level of access for further checks.
185 ********************************************************************/
187 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
188 SE_PRIV *rights, uint32 rights_mask,
189 uint32 des_access, uint32 *acc_granted,
192 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
193 uint32 saved_mask = 0;
195 /* check privileges; certain SAM access bits should be overridden
196 by privileges (mostly having to do with creating/modifying/deleting
199 if ( rights && user_has_any_privilege( token, rights ) ) {
201 saved_mask = (des_access & rights_mask);
202 des_access &= ~saved_mask;
204 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
209 /* check the security descriptor first */
211 status = se_access_check(psd, token, des_access, acc_granted);
212 if (NT_STATUS_IS_OK(status)) {
216 /* give root a free pass */
218 if ( geteuid() == sec_initial_uid() ) {
220 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
221 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
223 *acc_granted = des_access;
225 status = NT_STATUS_OK;
231 /* add in any bits saved during the privilege check (only
232 matters is status is ok) */
234 *acc_granted |= rights_mask;
236 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
237 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
238 des_access, *acc_granted));
243 /*******************************************************************
244 Checks if access to a function can be granted
245 ********************************************************************/
247 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
249 DEBUG(5,("%s: access check ((granted: %#010x; required: %#010x)\n",
250 debug, acc_granted, acc_required));
252 /* check the security descriptor first */
254 if ( (acc_granted&acc_required) == acc_required )
257 /* give root a free pass */
259 if (geteuid() == sec_initial_uid()) {
261 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
262 debug, acc_granted, acc_required));
263 DEBUGADD(4,("but overwritten by euid == 0\n"));
268 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: %#010x)\n",
269 debug, acc_granted, acc_required));
271 return NT_STATUS_ACCESS_DENIED;
274 /*******************************************************************
275 Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
276 ********************************************************************/
278 static void map_max_allowed_access(const NT_USER_TOKEN *token,
279 uint32_t *pacc_requested)
281 if (!((*pacc_requested) & MAXIMUM_ALLOWED_ACCESS)) {
284 *pacc_requested &= ~MAXIMUM_ALLOWED_ACCESS;
286 /* At least try for generic read. */
287 *pacc_requested = GENERIC_READ_ACCESS;
289 /* root gets anything. */
290 if (geteuid() == sec_initial_uid()) {
291 *pacc_requested |= GENERIC_ALL_ACCESS;
295 /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
297 if (is_sid_in_token(token, &global_sid_Builtin_Administrators) ||
298 is_sid_in_token(token, &global_sid_Builtin_Account_Operators)) {
299 *pacc_requested |= GENERIC_ALL_ACCESS;
303 /* Full access for DOMAIN\Domain Admins. */
305 DOM_SID domadmin_sid;
306 sid_copy( &domadmin_sid, get_global_sam_sid() );
307 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
308 if (is_sid_in_token(token, &domadmin_sid)) {
309 *pacc_requested |= GENERIC_ALL_ACCESS;
313 /* TODO ! Check privileges. */
316 /*******************************************************************
317 Fetch or create a dispinfo struct.
318 ********************************************************************/
320 static DISP_INFO *get_samr_dispinfo_by_sid(const struct dom_sid *psid)
323 * We do a static cache for DISP_INFO's here. Explanation can be found
324 * in Jeremy's checkin message to r11793:
326 * Fix the SAMR cache so it works across completely insane
327 * client behaviour (ie.:
328 * open pipe/open SAMR handle/enumerate 0 - 1024
329 * close SAMR handle, close pipe.
330 * open pipe/open SAMR handle/enumerate 1024 - 2048...
331 * close SAMR handle, close pipe.
332 * And on ad-nausium. Amazing.... probably object-oriented
333 * client side programming in action yet again.
334 * This change should *massively* improve performance when
335 * enumerating users from an LDAP database.
338 * "Our" and the builtin domain are the only ones where we ever
339 * enumerate stuff, so just cache 2 entries.
342 static struct disp_info *builtin_dispinfo;
343 static struct disp_info *domain_dispinfo;
345 /* There are two cases to consider here:
346 1) The SID is a domain SID and we look for an equality match, or
347 2) This is an account SID and so we return the DISP_INFO* for our
354 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
356 * Necessary only once, but it does not really hurt.
358 if (builtin_dispinfo == NULL) {
359 builtin_dispinfo = talloc_zero(
360 talloc_autofree_context(), struct disp_info);
361 if (builtin_dispinfo == NULL) {
365 sid_copy(&builtin_dispinfo->sid, &global_sid_Builtin);
367 return builtin_dispinfo;
370 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
372 * Necessary only once, but it does not really hurt.
374 if (domain_dispinfo == NULL) {
375 domain_dispinfo = talloc_zero(
376 talloc_autofree_context(), struct disp_info);
377 if (domain_dispinfo == NULL) {
381 sid_copy(&domain_dispinfo->sid, get_global_sam_sid());
383 return domain_dispinfo;
389 /*******************************************************************
390 Create a samr_info struct.
391 ********************************************************************/
393 static int samr_info_destructor(struct samr_info *info);
395 static struct samr_info *get_samr_info_by_sid(TALLOC_CTX *mem_ctx,
398 struct samr_info *info;
400 info = talloc_zero(mem_ctx, struct samr_info);
404 talloc_set_destructor(info, samr_info_destructor);
406 DEBUG(10, ("get_samr_info_by_sid: created new info for sid %s\n",
407 sid_string_dbg(psid)));
410 sid_copy( &info->sid, psid);
412 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
415 info->disp_info = get_samr_dispinfo_by_sid(psid);
420 /*******************************************************************
421 Function to free the per SID data.
422 ********************************************************************/
424 static void free_samr_cache(DISP_INFO *disp_info)
426 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
427 sid_string_dbg(&disp_info->sid)));
429 /* We need to become root here because the paged search might have to
430 * tell the LDAP server we're not interested in the rest anymore. */
434 TALLOC_FREE(disp_info->users);
435 TALLOC_FREE(disp_info->machines);
436 TALLOC_FREE(disp_info->groups);
437 TALLOC_FREE(disp_info->aliases);
438 TALLOC_FREE(disp_info->enum_users);
443 static int samr_info_destructor(struct samr_info *info)
445 /* Only free the dispinfo cache if no one bothered to set up
448 if (info->disp_info && info->disp_info->cache_timeout_event == NULL) {
449 free_samr_cache(info->disp_info);
454 /*******************************************************************
455 Idle event handler. Throw away the disp info cache.
456 ********************************************************************/
458 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
459 struct timed_event *te,
463 DISP_INFO *disp_info = (DISP_INFO *)private_data;
465 TALLOC_FREE(disp_info->cache_timeout_event);
467 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
469 free_samr_cache(disp_info);
472 /*******************************************************************
473 Setup cache removal idle event handler.
474 ********************************************************************/
476 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
478 /* Remove any pending timeout and update. */
480 TALLOC_FREE(disp_info->cache_timeout_event);
482 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
483 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
484 (unsigned int)secs_fromnow ));
486 disp_info->cache_timeout_event = event_add_timed(
487 smbd_event_context(), NULL,
488 timeval_current_ofs(secs_fromnow, 0),
489 disp_info_cache_idle_timeout_handler, (void *)disp_info);
492 /*******************************************************************
493 Force flush any cache. We do this on any samr_set_xxx call.
494 We must also remove the timeout handler.
495 ********************************************************************/
497 static void force_flush_samr_cache(const struct dom_sid *sid)
499 struct disp_info *disp_info = get_samr_dispinfo_by_sid(sid);
501 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
505 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
506 TALLOC_FREE(disp_info->cache_timeout_event);
507 free_samr_cache(disp_info);
510 /*******************************************************************
511 Ensure password info is never given out. Paranioa... JRA.
512 ********************************************************************/
514 static void samr_clear_sam_passwd(struct samu *sam_pass)
520 /* These now zero out the old password */
522 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
523 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
526 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
528 struct samr_displayentry *entry;
530 if (sid_check_is_builtin(&info->sid)) {
531 /* No users in builtin. */
535 if (info->users == NULL) {
536 info->users = pdb_search_users(info, acct_flags);
537 if (info->users == NULL) {
541 /* Fetch the last possible entry, thus trigger an enumeration */
542 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
544 /* Ensure we cache this enumeration. */
545 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
547 return info->users->num_entries;
550 static uint32 count_sam_groups(struct disp_info *info)
552 struct samr_displayentry *entry;
554 if (sid_check_is_builtin(&info->sid)) {
555 /* No groups in builtin. */
559 if (info->groups == NULL) {
560 info->groups = pdb_search_groups(info);
561 if (info->groups == NULL) {
565 /* Fetch the last possible entry, thus trigger an enumeration */
566 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
568 /* Ensure we cache this enumeration. */
569 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
571 return info->groups->num_entries;
574 static uint32 count_sam_aliases(struct disp_info *info)
576 struct samr_displayentry *entry;
578 if (info->aliases == NULL) {
579 info->aliases = pdb_search_aliases(info, &info->sid);
580 if (info->aliases == NULL) {
584 /* Fetch the last possible entry, thus trigger an enumeration */
585 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
587 /* Ensure we cache this enumeration. */
588 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
590 return info->aliases->num_entries;
593 /*******************************************************************
595 ********************************************************************/
597 NTSTATUS _samr_Close(pipes_struct *p, struct samr_Close *r)
599 if (!close_policy_hnd(p, r->in.handle)) {
600 return NT_STATUS_INVALID_HANDLE;
603 ZERO_STRUCTP(r->out.handle);
608 /*******************************************************************
610 ********************************************************************/
612 NTSTATUS _samr_OpenDomain(pipes_struct *p,
613 struct samr_OpenDomain *r)
615 struct samr_connect_info *cinfo;
616 struct samr_domain_info *dinfo;
617 SEC_DESC *psd = NULL;
619 uint32 des_access = r->in.access_mask;
624 /* find the connection policy handle. */
626 cinfo = policy_handle_find(p, r->in.connect_handle, 0, NULL,
627 struct samr_connect_info, &status);
628 if (!NT_STATUS_IS_OK(status)) {
632 /*check if access can be granted as requested by client. */
633 map_max_allowed_access(p->server_info->ptok, &des_access);
635 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
636 se_map_generic( &des_access, &dom_generic_mapping );
638 se_priv_copy( &se_rights, &se_machine_account );
639 se_priv_add( &se_rights, &se_add_users );
641 status = access_check_samr_object( psd, p->server_info->ptok,
642 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
643 &acc_granted, "_samr_OpenDomain" );
645 if ( !NT_STATUS_IS_OK(status) )
648 if (!sid_check_is_domain(r->in.sid) &&
649 !sid_check_is_builtin(r->in.sid)) {
650 return NT_STATUS_NO_SUCH_DOMAIN;
653 dinfo = policy_handle_create(p, r->out.domain_handle, acc_granted,
654 struct samr_domain_info, &status);
655 if (!NT_STATUS_IS_OK(status)) {
658 dinfo->sid = *r->in.sid;
659 dinfo->disp_info = get_samr_dispinfo_by_sid(r->in.sid);
661 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
666 /*******************************************************************
668 ********************************************************************/
670 NTSTATUS _samr_GetUserPwInfo(pipes_struct *p,
671 struct samr_GetUserPwInfo *r)
673 struct samr_user_info *uinfo;
674 enum lsa_SidType sid_type;
675 uint32_t min_password_length = 0;
676 uint32_t password_properties = 0;
680 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
682 uinfo = policy_handle_find(p, r->in.user_handle,
683 SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
684 struct samr_user_info, &status);
685 if (!NT_STATUS_IS_OK(status)) {
689 if (!sid_check_is_in_our_domain(&uinfo->sid)) {
690 return NT_STATUS_OBJECT_TYPE_MISMATCH;
694 ret = lookup_sid(p->mem_ctx, &uinfo->sid, NULL, NULL, &sid_type);
697 return NT_STATUS_NO_SUCH_USER;
703 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
704 &min_password_length);
705 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
706 &password_properties);
709 if (lp_check_password_script() && *lp_check_password_script()) {
710 password_properties |= DOMAIN_PASSWORD_COMPLEX;
718 r->out.info->min_password_length = min_password_length;
719 r->out.info->password_properties = password_properties;
721 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
726 /*******************************************************************
727 ********************************************************************/
729 static bool get_lsa_policy_samr_sid( pipes_struct *p, struct policy_handle *pol,
730 DOM_SID *sid, uint32 *acc_granted,
731 DISP_INFO **ppdisp_info)
733 struct samr_info *info = NULL;
735 /* find the policy handle. open a policy on it. */
736 if (!find_policy_by_hnd(p, pol, (void **)(void *)&info))
743 *acc_granted = info->acc_granted;
745 *ppdisp_info = info->disp_info;
751 /*******************************************************************
753 ********************************************************************/
755 NTSTATUS _samr_SetSecurity(pipes_struct *p,
756 struct samr_SetSecurity *r)
758 struct samr_user_info *uinfo;
762 struct samu *sampass=NULL;
765 uinfo = policy_handle_find(p, r->in.handle,
766 SAMR_USER_ACCESS_SET_ATTRIBUTES, NULL,
767 struct samr_user_info, &status);
768 if (!NT_STATUS_IS_OK(status)) {
772 if (!(sampass = samu_new( p->mem_ctx))) {
773 DEBUG(0,("No memory!\n"));
774 return NT_STATUS_NO_MEMORY;
777 /* get the user record */
779 ret = pdb_getsampwsid(sampass, &uinfo->sid);
783 DEBUG(4, ("User %s not found\n",
784 sid_string_dbg(&uinfo->sid)));
785 TALLOC_FREE(sampass);
786 return NT_STATUS_INVALID_HANDLE;
789 dacl = r->in.sdbuf->sd->dacl;
790 for (i=0; i < dacl->num_aces; i++) {
791 if (sid_equal(&uinfo->sid, &dacl->aces[i].trustee)) {
792 ret = pdb_set_pass_can_change(sampass,
793 (dacl->aces[i].access_mask &
794 SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
801 TALLOC_FREE(sampass);
802 return NT_STATUS_ACCESS_DENIED;
806 status = pdb_update_sam_account(sampass);
809 TALLOC_FREE(sampass);
814 /*******************************************************************
815 build correct perms based on policies and password times for _samr_query_sec_obj
816 *******************************************************************/
817 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, DOM_SID *user_sid)
819 struct samu *sampass=NULL;
822 if ( !(sampass = samu_new( mem_ctx )) ) {
823 DEBUG(0,("No memory!\n"));
828 ret = pdb_getsampwsid(sampass, user_sid);
832 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
833 TALLOC_FREE(sampass);
837 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
839 if (pdb_get_pass_can_change(sampass)) {
840 TALLOC_FREE(sampass);
843 TALLOC_FREE(sampass);
848 /*******************************************************************
850 ********************************************************************/
852 NTSTATUS _samr_QuerySecurity(pipes_struct *p,
853 struct samr_QuerySecurity *r)
857 SEC_DESC * psd = NULL;
862 if (!get_lsa_policy_samr_sid(p, r->in.handle, &pol_sid, &acc_granted, NULL))
863 return NT_STATUS_INVALID_HANDLE;
865 DEBUG(10,("_samr_QuerySecurity: querying security on SID: %s\n",
866 sid_string_dbg(&pol_sid)));
868 status = access_check_samr_function(acc_granted,
869 STD_RIGHT_READ_CONTROL_ACCESS,
870 "_samr_QuerySecurity");
871 if (!NT_STATUS_IS_OK(status)) {
875 /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
877 /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
878 if (pol_sid.sid_rev_num == 0) {
879 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
880 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
881 } else if (sid_equal(&pol_sid,get_global_sam_sid())) {
882 /* check if it is our domain SID */
883 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
884 "with SID: %s\n", sid_string_dbg(&pol_sid)));
885 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
886 } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
887 /* check if it is the Builtin Domain */
888 /* TODO: Builtin probably needs a different SD with restricted write access*/
889 DEBUG(5,("_samr_QuerySecurity: querying security on Builtin "
890 "Domain with SID: %s\n", sid_string_dbg(&pol_sid)));
891 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
892 } else if (sid_check_is_in_our_domain(&pol_sid) ||
893 sid_check_is_in_builtin(&pol_sid)) {
894 /* TODO: different SDs have to be generated for aliases groups and users.
895 Currently all three get a default user SD */
896 DEBUG(10,("_samr_QuerySecurity: querying security on Object "
897 "with SID: %s\n", sid_string_dbg(&pol_sid)));
898 if (check_change_pw_access(p->mem_ctx, &pol_sid)) {
899 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
900 &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
902 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_nopwchange_generic_mapping,
903 &pol_sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
906 return NT_STATUS_OBJECT_TYPE_MISMATCH;
909 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
910 return NT_STATUS_NO_MEMORY;
915 /*******************************************************************
916 makes a SAM_ENTRY / UNISTR2* structure from a user list.
917 ********************************************************************/
919 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
920 struct samr_SamEntry **sam_pp,
921 uint32_t num_entries,
923 struct samr_displayentry *entries)
926 struct samr_SamEntry *sam;
930 if (num_entries == 0) {
934 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
936 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
937 return NT_STATUS_NO_MEMORY;
940 for (i = 0; i < num_entries; i++) {
943 * usrmgr expects a non-NULL terminated string with
944 * trust relationships
946 if (entries[i].acct_flags & ACB_DOMTRUST) {
947 init_unistr2(&uni_temp_name, entries[i].account_name,
950 init_unistr2(&uni_temp_name, entries[i].account_name,
954 init_lsa_String(&sam[i].name, entries[i].account_name);
955 sam[i].idx = entries[i].rid;
963 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
965 /*******************************************************************
966 _samr_EnumDomainUsers
967 ********************************************************************/
969 NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
970 struct samr_EnumDomainUsers *r)
973 struct samr_domain_info *dinfo;
975 uint32 enum_context = *r->in.resume_handle;
976 enum remote_arch_types ra_type = get_remote_arch();
977 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
978 uint32 max_entries = max_sam_entries;
979 struct samr_displayentry *entries = NULL;
980 struct samr_SamArray *samr_array = NULL;
981 struct samr_SamEntry *samr_entries = NULL;
983 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
985 dinfo = policy_handle_find(p, r->in.domain_handle,
986 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
987 struct samr_domain_info, &status);
988 if (!NT_STATUS_IS_OK(status)) {
992 if (sid_check_is_builtin(&dinfo->sid)) {
993 /* No users in builtin. */
994 *r->out.resume_handle = *r->in.resume_handle;
995 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
999 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1001 return NT_STATUS_NO_MEMORY;
1003 *r->out.sam = samr_array;
1009 if ((dinfo->disp_info->enum_users != NULL) &&
1010 (dinfo->disp_info->enum_acb_mask != r->in.acct_flags)) {
1011 TALLOC_FREE(dinfo->disp_info->enum_users);
1014 if (dinfo->disp_info->enum_users == NULL) {
1015 dinfo->disp_info->enum_users = pdb_search_users(
1016 dinfo->disp_info, r->in.acct_flags);
1017 dinfo->disp_info->enum_acb_mask = r->in.acct_flags;
1020 if (dinfo->disp_info->enum_users == NULL) {
1021 /* END AS ROOT !!!! */
1023 return NT_STATUS_ACCESS_DENIED;
1026 num_account = pdb_search_entries(dinfo->disp_info->enum_users,
1027 enum_context, max_entries,
1030 /* END AS ROOT !!!! */
1034 if (num_account == 0) {
1035 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
1036 "total entries\n"));
1037 *r->out.resume_handle = *r->in.resume_handle;
1038 return NT_STATUS_OK;
1041 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
1042 num_account, enum_context,
1044 if (!NT_STATUS_IS_OK(status)) {
1048 if (max_entries <= num_account) {
1049 status = STATUS_MORE_ENTRIES;
1051 status = NT_STATUS_OK;
1054 /* Ensure we cache this enumeration. */
1055 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1057 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
1059 samr_array->count = num_account;
1060 samr_array->entries = samr_entries;
1062 *r->out.resume_handle = *r->in.resume_handle + num_account;
1063 *r->out.num_entries = num_account;
1065 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1070 /*******************************************************************
1071 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1072 ********************************************************************/
1074 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
1075 struct samr_SamEntry **sam_pp,
1076 uint32_t num_sam_entries,
1077 struct samr_displayentry *entries)
1079 struct samr_SamEntry *sam;
1084 if (num_sam_entries == 0) {
1088 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1093 for (i = 0; i < num_sam_entries; i++) {
1095 * JRA. I think this should include the null. TNG does not.
1097 init_lsa_String(&sam[i].name, entries[i].account_name);
1098 sam[i].idx = entries[i].rid;
1104 /*******************************************************************
1105 _samr_EnumDomainGroups
1106 ********************************************************************/
1108 NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
1109 struct samr_EnumDomainGroups *r)
1112 struct samr_domain_info *dinfo;
1113 struct samr_displayentry *groups;
1115 struct samr_SamArray *samr_array = NULL;
1116 struct samr_SamEntry *samr_entries = NULL;
1118 dinfo = policy_handle_find(p, r->in.domain_handle,
1119 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1120 struct samr_domain_info, &status);
1121 if (!NT_STATUS_IS_OK(status)) {
1125 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1127 if (sid_check_is_builtin(&dinfo->sid)) {
1128 /* No groups in builtin. */
1129 *r->out.resume_handle = *r->in.resume_handle;
1130 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1134 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1136 return NT_STATUS_NO_MEMORY;
1139 /* the domain group array is being allocated in the function below */
1143 if (dinfo->disp_info->groups == NULL) {
1144 dinfo->disp_info->groups = pdb_search_groups(dinfo->disp_info);
1146 if (dinfo->disp_info->groups == NULL) {
1148 return NT_STATUS_ACCESS_DENIED;
1152 num_groups = pdb_search_entries(dinfo->disp_info->groups,
1153 *r->in.resume_handle,
1154 MAX_SAM_ENTRIES, &groups);
1157 /* Ensure we cache this enumeration. */
1158 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1160 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1161 num_groups, groups);
1163 samr_array->count = num_groups;
1164 samr_array->entries = samr_entries;
1166 *r->out.sam = samr_array;
1167 *r->out.num_entries = num_groups;
1168 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1170 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1175 /*******************************************************************
1176 _samr_EnumDomainAliases
1177 ********************************************************************/
1179 NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
1180 struct samr_EnumDomainAliases *r)
1183 struct samr_domain_info *dinfo;
1184 struct samr_displayentry *aliases;
1185 uint32 num_aliases = 0;
1186 struct samr_SamArray *samr_array = NULL;
1187 struct samr_SamEntry *samr_entries = NULL;
1189 dinfo = policy_handle_find(p, r->in.domain_handle,
1190 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1191 struct samr_domain_info, &status);
1192 if (!NT_STATUS_IS_OK(status)) {
1196 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1197 sid_string_dbg(&dinfo->sid)));
1199 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1201 return NT_STATUS_NO_MEMORY;
1206 if (dinfo->disp_info->aliases == NULL) {
1207 dinfo->disp_info->aliases = pdb_search_aliases(
1208 dinfo->disp_info, &dinfo->sid);
1209 if (dinfo->disp_info->aliases == NULL) {
1211 return NT_STATUS_ACCESS_DENIED;
1215 num_aliases = pdb_search_entries(dinfo->disp_info->aliases,
1216 *r->in.resume_handle,
1217 MAX_SAM_ENTRIES, &aliases);
1220 /* Ensure we cache this enumeration. */
1221 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1223 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1224 num_aliases, aliases);
1226 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1228 samr_array->count = num_aliases;
1229 samr_array->entries = samr_entries;
1231 *r->out.sam = samr_array;
1232 *r->out.num_entries = num_aliases;
1233 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1238 /*******************************************************************
1239 inits a samr_DispInfoGeneral structure.
1240 ********************************************************************/
1242 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1243 struct samr_DispInfoGeneral *r,
1244 uint32_t num_entries,
1246 struct samr_displayentry *entries)
1250 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1252 if (num_entries == 0) {
1253 return NT_STATUS_OK;
1256 r->count = num_entries;
1258 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1260 return NT_STATUS_NO_MEMORY;
1263 for (i = 0; i < num_entries ; i++) {
1265 init_lsa_String(&r->entries[i].account_name,
1266 entries[i].account_name);
1268 init_lsa_String(&r->entries[i].description,
1269 entries[i].description);
1271 init_lsa_String(&r->entries[i].full_name,
1272 entries[i].fullname);
1274 r->entries[i].rid = entries[i].rid;
1275 r->entries[i].acct_flags = entries[i].acct_flags;
1276 r->entries[i].idx = start_idx+i+1;
1279 return NT_STATUS_OK;
1282 /*******************************************************************
1283 inits a samr_DispInfoFull structure.
1284 ********************************************************************/
1286 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1287 struct samr_DispInfoFull *r,
1288 uint32_t num_entries,
1290 struct samr_displayentry *entries)
1294 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1296 if (num_entries == 0) {
1297 return NT_STATUS_OK;
1300 r->count = num_entries;
1302 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFull, num_entries);
1304 return NT_STATUS_NO_MEMORY;
1307 for (i = 0; i < num_entries ; i++) {
1309 init_lsa_String(&r->entries[i].account_name,
1310 entries[i].account_name);
1312 init_lsa_String(&r->entries[i].description,
1313 entries[i].description);
1315 r->entries[i].rid = entries[i].rid;
1316 r->entries[i].acct_flags = entries[i].acct_flags;
1317 r->entries[i].idx = start_idx+i+1;
1320 return NT_STATUS_OK;
1323 /*******************************************************************
1324 inits a samr_DispInfoFullGroups structure.
1325 ********************************************************************/
1327 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1328 struct samr_DispInfoFullGroups *r,
1329 uint32_t num_entries,
1331 struct samr_displayentry *entries)
1335 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1337 if (num_entries == 0) {
1338 return NT_STATUS_OK;
1341 r->count = num_entries;
1343 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1345 return NT_STATUS_NO_MEMORY;
1348 for (i = 0; i < num_entries ; i++) {
1350 init_lsa_String(&r->entries[i].account_name,
1351 entries[i].account_name);
1353 init_lsa_String(&r->entries[i].description,
1354 entries[i].description);
1356 r->entries[i].rid = entries[i].rid;
1357 r->entries[i].acct_flags = entries[i].acct_flags;
1358 r->entries[i].idx = start_idx+i+1;
1361 return NT_STATUS_OK;
1364 /*******************************************************************
1365 inits a samr_DispInfoAscii structure.
1366 ********************************************************************/
1368 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1369 struct samr_DispInfoAscii *r,
1370 uint32_t num_entries,
1372 struct samr_displayentry *entries)
1376 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1378 if (num_entries == 0) {
1379 return NT_STATUS_OK;
1382 r->count = num_entries;
1384 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1386 return NT_STATUS_NO_MEMORY;
1389 for (i = 0; i < num_entries ; i++) {
1391 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1392 entries[i].account_name);
1394 r->entries[i].idx = start_idx+i+1;
1397 return NT_STATUS_OK;
1400 /*******************************************************************
1401 inits a samr_DispInfoAscii structure.
1402 ********************************************************************/
1404 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1405 struct samr_DispInfoAscii *r,
1406 uint32_t num_entries,
1408 struct samr_displayentry *entries)
1412 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1414 if (num_entries == 0) {
1415 return NT_STATUS_OK;
1418 r->count = num_entries;
1420 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1422 return NT_STATUS_NO_MEMORY;
1425 for (i = 0; i < num_entries ; i++) {
1427 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1428 entries[i].account_name);
1430 r->entries[i].idx = start_idx+i+1;
1433 return NT_STATUS_OK;
1436 /*******************************************************************
1437 _samr_QueryDisplayInfo
1438 ********************************************************************/
1440 NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
1441 struct samr_QueryDisplayInfo *r)
1444 struct samr_domain_info *dinfo;
1445 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1447 uint32 max_entries = r->in.max_entries;
1448 uint32 enum_context = r->in.start_idx;
1449 uint32 max_size = r->in.buf_size;
1451 union samr_DispInfo *disp_info = r->out.info;
1453 uint32 temp_size=0, total_data_size=0;
1454 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1455 uint32 num_account = 0;
1456 enum remote_arch_types ra_type = get_remote_arch();
1457 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1458 struct samr_displayentry *entries = NULL;
1460 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1462 dinfo = policy_handle_find(p, r->in.domain_handle,
1463 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1464 struct samr_domain_info, &status);
1465 if (!NT_STATUS_IS_OK(status)) {
1470 * calculate how many entries we will return.
1472 * - the number of entries the client asked
1473 * - our limit on that
1474 * - the starting point (enumeration context)
1475 * - the buffer size the client will accept
1479 * We are a lot more like W2K. Instead of reading the SAM
1480 * each time to find the records we need to send back,
1481 * we read it once and link that copy to the sam handle.
1482 * For large user list (over the MAX_SAM_ENTRIES)
1483 * it's a definitive win.
1484 * second point to notice: between enumerations
1485 * our sam is now the same as it's a snapshoot.
1486 * third point: got rid of the static SAM_USER_21 struct
1487 * no more intermediate.
1488 * con: it uses much more memory, as a full copy is stored
1491 * If you want to change it, think twice and think
1492 * of the second point , that's really important.
1497 if ((r->in.level < 1) || (r->in.level > 5)) {
1498 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1499 (unsigned int)r->in.level ));
1500 return NT_STATUS_INVALID_INFO_CLASS;
1503 /* first limit the number of entries we will return */
1504 if(max_entries > max_sam_entries) {
1505 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1506 "entries, limiting to %d\n", max_entries,
1508 max_entries = max_sam_entries;
1511 /* calculate the size and limit on the number of entries we will
1514 temp_size=max_entries*struct_size;
1516 if (temp_size>max_size) {
1517 max_entries=MIN((max_size/struct_size),max_entries);;
1518 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1519 "only %d entries\n", max_entries));
1524 /* THe following done as ROOT. Don't return without unbecome_root(). */
1526 switch (r->in.level) {
1529 if (dinfo->disp_info->users == NULL) {
1530 dinfo->disp_info->users = pdb_search_users(
1531 dinfo->disp_info, ACB_NORMAL);
1532 if (dinfo->disp_info->users == NULL) {
1534 return NT_STATUS_ACCESS_DENIED;
1536 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1537 (unsigned int)enum_context ));
1539 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1540 (unsigned int)enum_context ));
1543 num_account = pdb_search_entries(dinfo->disp_info->users,
1544 enum_context, max_entries,
1548 if (dinfo->disp_info->machines == NULL) {
1549 dinfo->disp_info->machines = pdb_search_users(
1550 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
1551 if (dinfo->disp_info->machines == NULL) {
1553 return NT_STATUS_ACCESS_DENIED;
1555 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1556 (unsigned int)enum_context ));
1558 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1559 (unsigned int)enum_context ));
1562 num_account = pdb_search_entries(dinfo->disp_info->machines,
1563 enum_context, max_entries,
1568 if (dinfo->disp_info->groups == NULL) {
1569 dinfo->disp_info->groups = pdb_search_groups(
1571 if (dinfo->disp_info->groups == NULL) {
1573 return NT_STATUS_ACCESS_DENIED;
1575 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1576 (unsigned int)enum_context ));
1578 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1579 (unsigned int)enum_context ));
1582 num_account = pdb_search_entries(dinfo->disp_info->groups,
1583 enum_context, max_entries,
1588 smb_panic("info class changed");
1594 /* Now create reply structure */
1595 switch (r->in.level) {
1597 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1598 num_account, enum_context,
1602 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1603 num_account, enum_context,
1607 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1608 num_account, enum_context,
1612 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1613 num_account, enum_context,
1617 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1618 num_account, enum_context,
1622 smb_panic("info class changed");
1626 if (!NT_STATUS_IS_OK(disp_ret))
1629 /* calculate the total size */
1630 total_data_size=num_account*struct_size;
1632 if (max_entries <= num_account) {
1633 status = STATUS_MORE_ENTRIES;
1635 status = NT_STATUS_OK;
1638 /* Ensure we cache this enumeration. */
1639 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1641 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1643 *r->out.total_size = total_data_size;
1644 *r->out.returned_size = temp_size;
1649 /****************************************************************
1650 _samr_QueryDisplayInfo2
1651 ****************************************************************/
1653 NTSTATUS _samr_QueryDisplayInfo2(pipes_struct *p,
1654 struct samr_QueryDisplayInfo2 *r)
1656 struct samr_QueryDisplayInfo q;
1658 q.in.domain_handle = r->in.domain_handle;
1659 q.in.level = r->in.level;
1660 q.in.start_idx = r->in.start_idx;
1661 q.in.max_entries = r->in.max_entries;
1662 q.in.buf_size = r->in.buf_size;
1664 q.out.total_size = r->out.total_size;
1665 q.out.returned_size = r->out.returned_size;
1666 q.out.info = r->out.info;
1668 return _samr_QueryDisplayInfo(p, &q);
1671 /****************************************************************
1672 _samr_QueryDisplayInfo3
1673 ****************************************************************/
1675 NTSTATUS _samr_QueryDisplayInfo3(pipes_struct *p,
1676 struct samr_QueryDisplayInfo3 *r)
1678 struct samr_QueryDisplayInfo q;
1680 q.in.domain_handle = r->in.domain_handle;
1681 q.in.level = r->in.level;
1682 q.in.start_idx = r->in.start_idx;
1683 q.in.max_entries = r->in.max_entries;
1684 q.in.buf_size = r->in.buf_size;
1686 q.out.total_size = r->out.total_size;
1687 q.out.returned_size = r->out.returned_size;
1688 q.out.info = r->out.info;
1690 return _samr_QueryDisplayInfo(p, &q);
1693 /*******************************************************************
1694 _samr_QueryAliasInfo
1695 ********************************************************************/
1697 NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
1698 struct samr_QueryAliasInfo *r)
1700 struct samr_alias_info *ainfo;
1701 struct acct_info info;
1703 union samr_AliasInfo *alias_info = NULL;
1704 const char *alias_name = NULL;
1705 const char *alias_description = NULL;
1707 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1709 ainfo = policy_handle_find(p, r->in.alias_handle,
1710 SAMR_ALIAS_ACCESS_LOOKUP_INFO, NULL,
1711 struct samr_alias_info, &status);
1712 if (!NT_STATUS_IS_OK(status)) {
1716 alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1718 return NT_STATUS_NO_MEMORY;
1722 status = pdb_get_aliasinfo(&ainfo->sid, &info);
1725 if ( !NT_STATUS_IS_OK(status))
1728 /* FIXME: info contains fstrings */
1729 alias_name = talloc_strdup(r, info.acct_name);
1730 alias_description = talloc_strdup(r, info.acct_desc);
1732 switch (r->in.level) {
1734 alias_info->all.name.string = alias_name;
1735 alias_info->all.num_members = 1; /* ??? */
1736 alias_info->all.description.string = alias_description;
1738 case ALIASINFODESCRIPTION:
1739 alias_info->description.string = alias_description;
1742 return NT_STATUS_INVALID_INFO_CLASS;
1745 *r->out.info = alias_info;
1747 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1749 return NT_STATUS_OK;
1752 /*******************************************************************
1754 ********************************************************************/
1756 NTSTATUS _samr_LookupNames(pipes_struct *p,
1757 struct samr_LookupNames *r)
1759 struct samr_domain_info *dinfo;
1762 enum lsa_SidType *type;
1764 int num_rids = r->in.num_names;
1765 struct samr_Ids rids, types;
1766 uint32_t num_mapped = 0;
1768 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1770 dinfo = policy_handle_find(p, r->in.domain_handle,
1771 0 /* Don't know the acc_bits yet */, NULL,
1772 struct samr_domain_info, &status);
1773 if (!NT_STATUS_IS_OK(status)) {
1777 if (num_rids > MAX_SAM_ENTRIES) {
1778 num_rids = MAX_SAM_ENTRIES;
1779 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1782 rid = talloc_array(p->mem_ctx, uint32, num_rids);
1783 NT_STATUS_HAVE_NO_MEMORY(rid);
1785 type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1786 NT_STATUS_HAVE_NO_MEMORY(type);
1788 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1789 sid_string_dbg(&dinfo->sid)));
1791 for (i = 0; i < num_rids; i++) {
1793 status = NT_STATUS_NONE_MAPPED;
1794 type[i] = SID_NAME_UNKNOWN;
1796 rid[i] = 0xffffffff;
1798 if (sid_check_is_builtin(&dinfo->sid)) {
1799 if (lookup_builtin_name(r->in.names[i].string,
1802 type[i] = SID_NAME_ALIAS;
1805 lookup_global_sam_name(r->in.names[i].string, 0,
1809 if (type[i] != SID_NAME_UNKNOWN) {
1814 if (num_mapped == num_rids) {
1815 status = NT_STATUS_OK;
1816 } else if (num_mapped == 0) {
1817 status = NT_STATUS_NONE_MAPPED;
1819 status = STATUS_SOME_UNMAPPED;
1822 rids.count = num_rids;
1825 types.count = num_rids;
1828 *r->out.rids = rids;
1829 *r->out.types = types;
1831 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1836 /*******************************************************************
1837 _samr_ChangePasswordUser2
1838 ********************************************************************/
1840 NTSTATUS _samr_ChangePasswordUser2(pipes_struct *p,
1841 struct samr_ChangePasswordUser2 *r)
1847 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1849 fstrcpy(user_name, r->in.account->string);
1850 fstrcpy(wks, r->in.server->string);
1852 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1855 * Pass the user through the NT -> unix user mapping
1859 (void)map_username(user_name);
1862 * UNIX username case mangling not required, pass_oem_change
1863 * is case insensitive.
1866 status = pass_oem_change(user_name,
1867 r->in.lm_password->data,
1868 r->in.lm_verifier->hash,
1869 r->in.nt_password->data,
1870 r->in.nt_verifier->hash,
1873 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1878 /*******************************************************************
1879 _samr_ChangePasswordUser3
1880 ********************************************************************/
1882 NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
1883 struct samr_ChangePasswordUser3 *r)
1887 const char *wks = NULL;
1888 uint32 reject_reason;
1889 struct samr_DomInfo1 *dominfo = NULL;
1890 struct samr_ChangeReject *reject = NULL;
1893 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1895 fstrcpy(user_name, r->in.account->string);
1896 if (r->in.server && r->in.server->string) {
1897 wks = r->in.server->string;
1900 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
1903 * Pass the user through the NT -> unix user mapping
1907 (void)map_username(user_name);
1910 * UNIX username case mangling not required, pass_oem_change
1911 * is case insensitive.
1914 status = pass_oem_change(user_name,
1915 r->in.lm_password->data,
1916 r->in.lm_verifier->hash,
1917 r->in.nt_password->data,
1918 r->in.nt_verifier->hash,
1921 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
1922 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
1924 time_t u_expire, u_min_age;
1925 uint32 account_policy_temp;
1927 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
1929 return NT_STATUS_NO_MEMORY;
1932 reject = TALLOC_ZERO_P(p->mem_ctx, struct samr_ChangeReject);
1934 return NT_STATUS_NO_MEMORY;
1941 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &tmp);
1942 dominfo->min_password_length = tmp;
1944 pdb_get_account_policy(AP_PASSWORD_HISTORY, &tmp);
1945 dominfo->password_history_length = tmp;
1947 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
1948 &dominfo->password_properties);
1950 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
1951 u_expire = account_policy_temp;
1953 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
1954 u_min_age = account_policy_temp;
1960 unix_to_nt_time_abs((NTTIME *)&dominfo->max_password_age, u_expire);
1961 unix_to_nt_time_abs((NTTIME *)&dominfo->min_password_age, u_min_age);
1963 if (lp_check_password_script() && *lp_check_password_script()) {
1964 dominfo->password_properties |= DOMAIN_PASSWORD_COMPLEX;
1967 reject->reason = reject_reason;
1969 *r->out.dominfo = dominfo;
1970 *r->out.reject = reject;
1973 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1978 /*******************************************************************
1979 makes a SAMR_R_LOOKUP_RIDS structure.
1980 ********************************************************************/
1982 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1984 struct lsa_String **lsa_name_array_p)
1986 struct lsa_String *lsa_name_array = NULL;
1989 *lsa_name_array_p = NULL;
1991 if (num_names != 0) {
1992 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
1993 if (!lsa_name_array) {
1998 for (i = 0; i < num_names; i++) {
1999 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
2000 init_lsa_String(&lsa_name_array[i], names[i]);
2003 *lsa_name_array_p = lsa_name_array;
2008 /*******************************************************************
2010 ********************************************************************/
2012 NTSTATUS _samr_LookupRids(pipes_struct *p,
2013 struct samr_LookupRids *r)
2015 struct samr_domain_info *dinfo;
2018 enum lsa_SidType *attrs = NULL;
2019 uint32 *wire_attrs = NULL;
2020 int num_rids = (int)r->in.num_rids;
2022 struct lsa_Strings names_array;
2023 struct samr_Ids types_array;
2024 struct lsa_String *lsa_names = NULL;
2026 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2028 dinfo = policy_handle_find(p, r->in.domain_handle,
2029 0 /* Don't know the acc_bits yet */, NULL,
2030 struct samr_domain_info, &status);
2031 if (!NT_STATUS_IS_OK(status)) {
2035 if (num_rids > 1000) {
2036 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2037 "to samba4 idl this is not possible\n", num_rids));
2038 return NT_STATUS_UNSUCCESSFUL;
2042 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
2043 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
2044 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
2046 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2047 return NT_STATUS_NO_MEMORY;
2054 become_root(); /* lookup_sid can require root privs */
2055 status = pdb_lookup_rids(&dinfo->sid, num_rids, r->in.rids,
2059 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2060 status = NT_STATUS_OK;
2063 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2065 return NT_STATUS_NO_MEMORY;
2068 /* Convert from enum lsa_SidType to uint32 for wire format. */
2069 for (i = 0; i < num_rids; i++) {
2070 wire_attrs[i] = (uint32)attrs[i];
2073 names_array.count = num_rids;
2074 names_array.names = lsa_names;
2076 types_array.count = num_rids;
2077 types_array.ids = wire_attrs;
2079 *r->out.names = names_array;
2080 *r->out.types = types_array;
2082 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2087 /*******************************************************************
2089 ********************************************************************/
2091 NTSTATUS _samr_OpenUser(pipes_struct *p,
2092 struct samr_OpenUser *r)
2094 struct samu *sampass=NULL;
2096 struct samr_domain_info *dinfo;
2097 struct samr_user_info *uinfo;
2098 SEC_DESC *psd = NULL;
2100 uint32 des_access = r->in.access_mask;
2107 dinfo = policy_handle_find(p, r->in.domain_handle,
2108 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
2109 struct samr_domain_info, &status);
2110 if (!NT_STATUS_IS_OK(status)) {
2114 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2115 return NT_STATUS_NO_MEMORY;
2118 /* append the user's RID to it */
2120 if (!sid_compose(&sid, &dinfo->sid, r->in.rid))
2121 return NT_STATUS_NO_SUCH_USER;
2123 /* check if access can be granted as requested by client. */
2125 map_max_allowed_access(p->server_info->ptok, &des_access);
2127 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2128 se_map_generic(&des_access, &usr_generic_mapping);
2130 se_priv_copy( &se_rights, &se_machine_account );
2131 se_priv_add( &se_rights, &se_add_users );
2133 nt_status = access_check_samr_object(psd, p->server_info->ptok,
2134 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2135 &acc_granted, "_samr_OpenUser");
2137 if ( !NT_STATUS_IS_OK(nt_status) )
2141 ret=pdb_getsampwsid(sampass, &sid);
2144 /* check that the SID exists in our domain. */
2146 return NT_STATUS_NO_SUCH_USER;
2149 TALLOC_FREE(sampass);
2151 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
2152 struct samr_user_info, &nt_status);
2153 if (!NT_STATUS_IS_OK(nt_status)) {
2158 return NT_STATUS_OK;
2161 /*************************************************************************
2162 *************************************************************************/
2164 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2166 struct lsa_BinaryString **_r)
2168 struct lsa_BinaryString *r;
2171 return NT_STATUS_INVALID_PARAMETER;
2174 r = TALLOC_ZERO_P(mem_ctx, struct lsa_BinaryString);
2176 return NT_STATUS_NO_MEMORY;
2179 r->array = TALLOC_ZERO_ARRAY(mem_ctx, uint16_t, blob->length/2);
2181 return NT_STATUS_NO_MEMORY;
2183 memcpy(r->array, blob->data, blob->length);
2184 r->size = blob->length;
2185 r->length = blob->length;
2188 return NT_STATUS_NO_MEMORY;
2193 return NT_STATUS_OK;
2196 static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
2197 struct samr_UserInfo5 *r,
2199 DOM_SID *domain_sid)
2201 const DOM_SID *sid_user, *sid_group;
2202 uint32_t rid, primary_gid;
2204 sid_user = pdb_get_user_sid(pw);
2206 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2207 DEBUG(0, ("get_user_info_5: User %s has SID %s, \nwhich conflicts with "
2208 "the domain sid %s. Failing operation.\n",
2209 pdb_get_username(pw), sid_string_dbg(sid_user),
2210 sid_string_dbg(domain_sid)));
2211 return NT_STATUS_UNSUCCESSFUL;
2215 sid_group = pdb_get_group_sid(pw);
2218 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2219 DEBUG(0, ("get_user_info_5: User %s has Primary Group SID %s, \n"
2220 "which conflicts with the domain sid %s. Failing operation.\n",
2221 pdb_get_username(pw), sid_string_dbg(sid_group),
2222 sid_string_dbg(domain_sid)));
2223 return NT_STATUS_UNSUCCESSFUL;
2226 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2227 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2228 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2229 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2231 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2232 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2233 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2234 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2235 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2236 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2237 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2238 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2240 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2242 r->primary_gid = primary_gid;
2243 r->acct_flags = pdb_get_acct_ctrl(pw);
2244 r->bad_password_count = pdb_get_bad_password_count(pw);
2245 r->logon_count = pdb_get_logon_count(pw);
2247 return NT_STATUS_OK;
2250 /*************************************************************************
2251 get_user_info_7. Safe. Only gives out account_name.
2252 *************************************************************************/
2254 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2255 struct samr_UserInfo7 *r,
2256 struct samu *smbpass)
2258 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2259 if (!r->account_name.string) {
2260 return NT_STATUS_NO_MEMORY;
2263 return NT_STATUS_OK;
2266 /*************************************************************************
2267 get_user_info_9. Only gives out primary group SID.
2268 *************************************************************************/
2270 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2271 struct samr_UserInfo9 *r,
2272 struct samu *smbpass)
2274 r->primary_gid = pdb_get_group_rid(smbpass);
2276 return NT_STATUS_OK;
2279 /*************************************************************************
2280 get_user_info_16. Safe. Only gives out acb bits.
2281 *************************************************************************/
2283 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2284 struct samr_UserInfo16 *r,
2285 struct samu *smbpass)
2287 r->acct_flags = pdb_get_acct_ctrl(smbpass);
2289 return NT_STATUS_OK;
2292 /*************************************************************************
2293 get_user_info_18. OK - this is the killer as it gives out password info.
2294 Ensure that this is only allowed on an encrypted connection with a root
2296 *************************************************************************/
2298 static NTSTATUS get_user_info_18(pipes_struct *p,
2299 TALLOC_CTX *mem_ctx,
2300 struct samr_UserInfo18 *r,
2303 struct samu *smbpass=NULL;
2308 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
2309 return NT_STATUS_ACCESS_DENIED;
2312 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
2313 return NT_STATUS_ACCESS_DENIED;
2317 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2320 if ( !(smbpass = samu_new( mem_ctx )) ) {
2321 return NT_STATUS_NO_MEMORY;
2324 ret = pdb_getsampwsid(smbpass, user_sid);
2327 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2328 TALLOC_FREE(smbpass);
2329 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2332 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2334 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2335 TALLOC_FREE(smbpass);
2336 return NT_STATUS_ACCOUNT_DISABLED;
2339 r->lm_pwd_active = true;
2340 r->nt_pwd_active = true;
2341 memcpy(r->lm_pwd.hash, pdb_get_lanman_passwd(smbpass), 16);
2342 memcpy(r->nt_pwd.hash, pdb_get_nt_passwd(smbpass), 16);
2343 r->password_expired = 0; /* FIXME */
2345 TALLOC_FREE(smbpass);
2347 return NT_STATUS_OK;
2350 /*************************************************************************
2352 *************************************************************************/
2354 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2355 struct samr_UserInfo20 *r,
2356 struct samu *sampass)
2358 const char *munged_dial = NULL;
2361 struct lsa_BinaryString *parameters = NULL;
2365 munged_dial = pdb_get_munged_dial(sampass);
2367 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2368 munged_dial, (int)strlen(munged_dial)));
2371 blob = base64_decode_data_blob(munged_dial);
2373 blob = data_blob_string_const_null("");
2376 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2377 data_blob_free(&blob);
2378 if (!NT_STATUS_IS_OK(status)) {
2382 r->parameters = *parameters;
2384 return NT_STATUS_OK;
2388 /*************************************************************************
2390 *************************************************************************/
2392 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2393 struct samr_UserInfo21 *r,
2395 DOM_SID *domain_sid)
2398 const DOM_SID *sid_user, *sid_group;
2399 uint32_t rid, primary_gid;
2400 NTTIME force_password_change;
2401 time_t must_change_time;
2402 struct lsa_BinaryString *parameters = NULL;
2403 const char *munged_dial = NULL;
2408 sid_user = pdb_get_user_sid(pw);
2410 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2411 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2412 "the domain sid %s. Failing operation.\n",
2413 pdb_get_username(pw), sid_string_dbg(sid_user),
2414 sid_string_dbg(domain_sid)));
2415 return NT_STATUS_UNSUCCESSFUL;
2419 sid_group = pdb_get_group_sid(pw);
2422 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2423 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2424 "which conflicts with the domain sid %s. Failing operation.\n",
2425 pdb_get_username(pw), sid_string_dbg(sid_group),
2426 sid_string_dbg(domain_sid)));
2427 return NT_STATUS_UNSUCCESSFUL;
2430 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2431 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2432 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2433 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2434 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2436 must_change_time = pdb_get_pass_must_change_time(pw);
2437 if (must_change_time == get_time_t_max()) {
2438 unix_to_nt_time_abs(&force_password_change, must_change_time);
2440 unix_to_nt_time(&force_password_change, must_change_time);
2443 munged_dial = pdb_get_munged_dial(pw);
2445 blob = base64_decode_data_blob(munged_dial);
2447 blob = data_blob_string_const_null("");
2450 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2451 data_blob_free(&blob);
2452 if (!NT_STATUS_IS_OK(status)) {
2456 r->force_password_change = force_password_change;
2458 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2459 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2460 r->home_directory.string = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2461 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2462 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2463 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2464 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2465 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2466 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2468 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2469 r->parameters = *parameters;
2471 r->primary_gid = primary_gid;
2472 r->acct_flags = pdb_get_acct_ctrl(pw);
2473 r->bad_password_count = pdb_get_bad_password_count(pw);
2474 r->logon_count = pdb_get_logon_count(pw);
2475 r->fields_present = pdb_build_fields_present(pw);
2476 r->password_expired = (pdb_get_pass_must_change_time(pw) == 0) ?
2477 PASS_MUST_CHANGE_AT_NEXT_LOGON : 0;
2478 r->country_code = 0;
2480 r->lm_password_set = 0;
2481 r->nt_password_set = 0;
2486 Look at a user on a real NT4 PDC with usrmgr, press
2487 'ok'. Then you will see that fields_present is set to
2488 0x08f827fa. Look at the user immediately after that again,
2489 and you will see that 0x00fffff is returned. This solves
2490 the problem that you get access denied after having looked
2498 return NT_STATUS_OK;
2501 /*******************************************************************
2503 ********************************************************************/
2505 NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
2506 struct samr_QueryUserInfo *r)
2509 union samr_UserInfo *user_info = NULL;
2510 struct samr_user_info *uinfo;
2514 struct samu *pwd = NULL;
2516 uinfo = policy_handle_find(p, r->in.user_handle,
2517 SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
2518 struct samr_user_info, &status);
2519 if (!NT_STATUS_IS_OK(status)) {
2523 domain_sid = uinfo->sid;
2525 sid_split_rid(&domain_sid, &rid);
2527 if (!sid_check_is_in_our_domain(&uinfo->sid))
2528 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2530 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
2531 sid_string_dbg(&uinfo->sid)));
2533 user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
2535 return NT_STATUS_NO_MEMORY;
2538 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
2540 if (!(pwd = samu_new(p->mem_ctx))) {
2541 return NT_STATUS_NO_MEMORY;
2545 ret = pdb_getsampwsid(pwd, &uinfo->sid);
2549 DEBUG(4,("User %s not found\n", sid_string_dbg(&uinfo->sid)));
2551 return NT_STATUS_NO_SUCH_USER;
2554 DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
2556 samr_clear_sam_passwd(pwd);
2558 switch (r->in.level) {
2560 status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
2563 status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
2566 status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
2569 status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
2572 /* level 18 is special */
2573 status = get_user_info_18(p, p->mem_ctx, &user_info->info18,
2577 status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
2580 status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid);
2583 status = NT_STATUS_INVALID_INFO_CLASS;
2589 *r->out.info = user_info;
2591 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
2596 /****************************************************************
2597 ****************************************************************/
2599 NTSTATUS _samr_QueryUserInfo2(pipes_struct *p,
2600 struct samr_QueryUserInfo2 *r)
2602 struct samr_QueryUserInfo u;
2604 u.in.user_handle = r->in.user_handle;
2605 u.in.level = r->in.level;
2606 u.out.info = r->out.info;
2608 return _samr_QueryUserInfo(p, &u);
2611 /*******************************************************************
2612 _samr_GetGroupsForUser
2613 ********************************************************************/
2615 NTSTATUS _samr_GetGroupsForUser(pipes_struct *p,
2616 struct samr_GetGroupsForUser *r)
2618 struct samr_user_info *uinfo;
2619 struct samu *sam_pass=NULL;
2621 struct samr_RidWithAttribute dom_gid;
2622 struct samr_RidWithAttribute *gids = NULL;
2623 uint32 primary_group_rid;
2624 size_t num_groups = 0;
2629 bool success = False;
2631 struct samr_RidWithAttributeArray *rids = NULL;
2634 * from the SID in the request:
2635 * we should send back the list of DOMAIN GROUPS
2636 * the user is a member of
2638 * and only the DOMAIN GROUPS
2639 * no ALIASES !!! neither aliases of the domain
2640 * nor aliases of the builtin SID
2645 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2647 uinfo = policy_handle_find(p, r->in.user_handle,
2648 SAMR_USER_ACCESS_GET_GROUPS, NULL,
2649 struct samr_user_info, &result);
2650 if (!NT_STATUS_IS_OK(result)) {
2654 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
2656 return NT_STATUS_NO_MEMORY;
2659 if (!sid_check_is_in_our_domain(&uinfo->sid))
2660 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2662 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2663 return NT_STATUS_NO_MEMORY;
2667 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
2671 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2672 sid_string_dbg(&uinfo->sid)));
2673 return NT_STATUS_NO_SUCH_USER;
2678 /* make both calls inside the root block */
2680 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2681 &sids, &unix_gids, &num_groups);
2682 if ( NT_STATUS_IS_OK(result) ) {
2683 success = sid_peek_check_rid(get_global_sam_sid(),
2684 pdb_get_group_sid(sam_pass),
2685 &primary_group_rid);
2689 if (!NT_STATUS_IS_OK(result)) {
2690 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2691 sid_string_dbg(&uinfo->sid)));
2696 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2697 sid_string_dbg(pdb_get_group_sid(sam_pass)),
2698 pdb_get_username(sam_pass)));
2699 TALLOC_FREE(sam_pass);
2700 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2706 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2708 dom_gid.rid = primary_group_rid;
2709 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2711 for (i=0; i<num_groups; i++) {
2713 if (!sid_peek_check_rid(get_global_sam_sid(),
2714 &(sids[i]), &dom_gid.rid)) {
2715 DEBUG(10, ("Found sid %s not in our domain\n",
2716 sid_string_dbg(&sids[i])));
2720 if (dom_gid.rid == primary_group_rid) {
2721 /* We added the primary group directly from the
2722 * sam_account. The other SIDs are unique from
2723 * enum_group_memberships */
2727 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2730 rids->count = num_gids;
2733 *r->out.rids = rids;
2735 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2740 /*******************************************************************
2741 _samr_QueryDomainInfo
2742 ********************************************************************/
2744 NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
2745 struct samr_QueryDomainInfo *r)
2747 NTSTATUS status = NT_STATUS_OK;
2748 struct samr_domain_info *dinfo;
2749 union samr_DomainInfo *dom_info;
2750 time_t u_expire, u_min_age;
2752 time_t u_lock_duration, u_reset_time;
2755 uint32 account_policy_temp;
2760 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
2762 dinfo = policy_handle_find(p, r->in.domain_handle,
2763 SAMR_ACCESS_LOOKUP_DOMAIN, NULL,
2764 struct samr_domain_info, &status);
2765 if (!NT_STATUS_IS_OK(status)) {
2769 dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
2771 return NT_STATUS_NO_MEMORY;
2774 switch (r->in.level) {
2781 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
2782 &account_policy_temp);
2783 dom_info->info1.min_password_length = account_policy_temp;
2785 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2786 dom_info->info1.password_history_length = account_policy_temp;
2788 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
2789 &dom_info->info1.password_properties);
2791 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2792 u_expire = account_policy_temp;
2794 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2795 u_min_age = account_policy_temp;
2801 unix_to_nt_time_abs((NTTIME *)&dom_info->info1.max_password_age, u_expire);
2802 unix_to_nt_time_abs((NTTIME *)&dom_info->info1.min_password_age, u_min_age);
2804 if (lp_check_password_script() && *lp_check_password_script()) {
2805 dom_info->info1.password_properties |= DOMAIN_PASSWORD_COMPLEX;
2815 dom_info->general.num_users = count_sam_users(
2816 dinfo->disp_info, ACB_NORMAL);
2817 dom_info->general.num_groups = count_sam_groups(
2819 dom_info->general.num_aliases = count_sam_aliases(
2822 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &u_logout);
2824 unix_to_nt_time_abs(&dom_info->general.force_logoff_time, u_logout);
2826 if (!pdb_get_seq_num(&seq_num))
2827 seq_num = time(NULL);
2833 server_role = ROLE_DOMAIN_PDC;
2834 if (lp_server_role() == ROLE_DOMAIN_BDC)
2835 server_role = ROLE_DOMAIN_BDC;
2837 dom_info->general.oem_information.string = lp_serverstring();
2838 dom_info->general.domain_name.string = lp_workgroup();
2839 dom_info->general.primary.string = global_myname();
2840 dom_info->general.sequence_num = seq_num;
2841 dom_info->general.domain_server_state = DOMAIN_SERVER_ENABLED;
2842 dom_info->general.role = server_role;
2843 dom_info->general.unknown3 = 1;
2854 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
2855 u_logout = (time_t)ul;
2862 unix_to_nt_time_abs(&dom_info->info3.force_logoff_time, u_logout);
2866 dom_info->oem.oem_information.string = lp_serverstring();
2869 dom_info->info5.domain_name.string = get_global_sam_name();
2872 /* NT returns its own name when a PDC. win2k and later
2873 * only the name of the PDC if itself is a BDC (samba4
2875 dom_info->info6.primary.string = global_myname();
2878 server_role = ROLE_DOMAIN_PDC;
2879 if (lp_server_role() == ROLE_DOMAIN_BDC)
2880 server_role = ROLE_DOMAIN_BDC;
2882 dom_info->info7.role = server_role;
2890 if (!pdb_get_seq_num(&seq_num)) {
2891 seq_num = time(NULL);
2898 dom_info->info8.sequence_num = seq_num;
2899 dom_info->info8.domain_create_time = 0;
2908 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
2909 u_lock_duration = account_policy_temp;
2910 if (u_lock_duration != -1) {
2911 u_lock_duration *= 60;
2914 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
2915 u_reset_time = account_policy_temp * 60;
2917 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT,
2918 &account_policy_temp);
2919 dom_info->info12.lockout_threshold = account_policy_temp;
2925 unix_to_nt_time_abs(&dom_info->info12.lockout_duration,
2927 unix_to_nt_time_abs(&dom_info->info12.lockout_window,
2932 return NT_STATUS_INVALID_INFO_CLASS;
2935 *r->out.info = dom_info;
2937 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
2942 /* W2k3 seems to use the same check for all 3 objects that can be created via
2943 * SAMR, if you try to create for example "Dialup" as an alias it says
2944 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
2947 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
2949 enum lsa_SidType type;
2952 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
2955 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
2956 * whether the name already exists */
2957 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
2958 NULL, NULL, NULL, &type);
2962 DEBUG(10, ("%s does not exist, can create it\n", new_name));
2963 return NT_STATUS_OK;
2966 DEBUG(5, ("trying to create %s, exists as %s\n",
2967 new_name, sid_type_lookup(type)));
2969 if (type == SID_NAME_DOM_GRP) {
2970 return NT_STATUS_GROUP_EXISTS;
2972 if (type == SID_NAME_ALIAS) {
2973 return NT_STATUS_ALIAS_EXISTS;
2976 /* Yes, the default is NT_STATUS_USER_EXISTS */
2977 return NT_STATUS_USER_EXISTS;
2980 /*******************************************************************
2982 ********************************************************************/
2984 NTSTATUS _samr_CreateUser2(pipes_struct *p,
2985 struct samr_CreateUser2 *r)
2987 const char *account = NULL;
2989 uint32_t acb_info = r->in.acct_flags;
2990 struct samr_domain_info *dinfo;
2991 struct samr_user_info *uinfo;
2996 /* check this, when giving away 'add computer to domain' privs */
2997 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
2998 bool can_add_account = False;
3001 dinfo = policy_handle_find(p, r->in.domain_handle,
3002 SAMR_DOMAIN_ACCESS_CREATE_USER, NULL,
3003 struct samr_domain_info, &nt_status);
3004 if (!NT_STATUS_IS_OK(nt_status)) {
3008 if (sid_check_is_builtin(&dinfo->sid)) {
3009 DEBUG(5,("_samr_CreateUser2: Refusing user create in BUILTIN\n"));
3010 return NT_STATUS_ACCESS_DENIED;
3013 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3014 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3015 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3016 this parameter is not an account type */
3017 return NT_STATUS_INVALID_PARAMETER;
3020 account = r->in.account_name->string;
3021 if (account == NULL) {
3022 return NT_STATUS_NO_MEMORY;
3025 nt_status = can_create(p->mem_ctx, account);
3026 if (!NT_STATUS_IS_OK(nt_status)) {
3030 /* determine which user right we need to check based on the acb_info */
3032 if ( acb_info & ACB_WSTRUST )
3034 se_priv_copy( &se_rights, &se_machine_account );
3035 can_add_account = user_has_privileges(
3036 p->server_info->ptok, &se_rights );
3038 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3039 account for domain trusts and changes the ACB flags later */
3040 else if ( acb_info & ACB_NORMAL &&
3041 (account[strlen(account)-1] != '$') )
3043 se_priv_copy( &se_rights, &se_add_users );
3044 can_add_account = user_has_privileges(
3045 p->server_info->ptok, &se_rights );
3047 else /* implicit assumption of a BDC or domain trust account here
3048 * (we already check the flags earlier) */
3050 if ( lp_enable_privileges() ) {
3051 /* only Domain Admins can add a BDC or domain trust */
3052 se_priv_copy( &se_rights, &se_priv_none );
3053 can_add_account = nt_token_check_domain_rid(
3054 p->server_info->ptok,
3055 DOMAIN_GROUP_RID_ADMINS );
3059 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3060 uidtoname(p->server_info->utok.uid),
3061 can_add_account ? "True":"False" ));
3063 /********** BEGIN Admin BLOCK **********/
3065 if ( can_add_account )
3068 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3071 if ( can_add_account )
3074 /********** END Admin BLOCK **********/
3076 /* now check for failure */
3078 if ( !NT_STATUS_IS_OK(nt_status) )
3081 /* Get the user's SID */
3083 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3085 map_max_allowed_access(p->server_info->ptok, &des_access);
3087 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3088 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3089 se_map_generic(&des_access, &usr_generic_mapping);
3091 nt_status = access_check_samr_object(psd, p->server_info->ptok,
3092 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
3093 &acc_granted, "_samr_CreateUser2");
3095 if ( !NT_STATUS_IS_OK(nt_status) ) {
3099 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
3100 struct samr_user_info, &nt_status);
3101 if (!NT_STATUS_IS_OK(nt_status)) {
3106 /* After a "set" ensure we have no cached display info. */
3107 force_flush_samr_cache(&sid);
3109 *r->out.access_granted = acc_granted;
3111 return NT_STATUS_OK;
3114 /****************************************************************
3115 ****************************************************************/
3117 NTSTATUS _samr_CreateUser(pipes_struct *p,
3118 struct samr_CreateUser *r)
3120 struct samr_CreateUser2 c;
3121 uint32_t access_granted;
3123 c.in.domain_handle = r->in.domain_handle;
3124 c.in.account_name = r->in.account_name;
3125 c.in.acct_flags = ACB_NORMAL;
3126 c.in.access_mask = r->in.access_mask;
3127 c.out.user_handle = r->out.user_handle;
3128 c.out.access_granted = &access_granted;
3129 c.out.rid = r->out.rid;
3131 return _samr_CreateUser2(p, &c);
3134 /*******************************************************************
3136 ********************************************************************/
3138 NTSTATUS _samr_Connect(pipes_struct *p,
3139 struct samr_Connect *r)
3141 struct samr_connect_info *info;
3142 uint32_t acc_granted;
3143 struct policy_handle hnd;
3144 uint32 des_access = r->in.access_mask;
3149 if (!pipe_access_check(p)) {
3150 DEBUG(3, ("access denied to _samr_Connect\n"));
3151 return NT_STATUS_ACCESS_DENIED;
3154 /* don't give away the farm but this is probably ok. The SAMR_ACCESS_ENUM_DOMAINS
3155 was observed from a win98 client trying to enumerate users (when configured
3156 user level access control on shares) --jerry */
3158 map_max_allowed_access(p->server_info->ptok, &des_access);
3160 se_map_generic( &des_access, &sam_generic_mapping );
3162 acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS
3163 |SAMR_ACCESS_LOOKUP_DOMAIN);
3165 /* set up the SAMR connect_anon response */
3167 info = policy_handle_create(p, &hnd, acc_granted,
3168 struct samr_connect_info,
3170 if (!NT_STATUS_IS_OK(status)) {
3174 *r->out.connect_handle = hnd;
3175 return NT_STATUS_OK;
3178 /*******************************************************************
3180 ********************************************************************/
3182 NTSTATUS _samr_Connect2(pipes_struct *p,
3183 struct samr_Connect2 *r)
3185 struct samr_connect_info *info = NULL;
3186 struct policy_handle hnd;
3187 SEC_DESC *psd = NULL;
3189 uint32 des_access = r->in.access_mask;
3192 const char *fn = "_samr_Connect2";
3194 switch (p->hdr_req.opnum) {
3195 case NDR_SAMR_CONNECT2:
3196 fn = "_samr_Connect2";
3198 case NDR_SAMR_CONNECT3:
3199 fn = "_samr_Connect3";
3201 case NDR_SAMR_CONNECT4:
3202 fn = "_samr_Connect4";
3204 case NDR_SAMR_CONNECT5:
3205 fn = "_samr_Connect5";
3209 DEBUG(5,("%s: %d\n", fn, __LINE__));
3213 if (!pipe_access_check(p)) {
3214 DEBUG(3, ("access denied to %s\n", fn));
3215 return NT_STATUS_ACCESS_DENIED;
3218 map_max_allowed_access(p->server_info->ptok, &des_access);
3220 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3221 se_map_generic(&des_access, &sam_generic_mapping);
3223 nt_status = access_check_samr_object(psd, p->server_info->ptok,
3224 NULL, 0, des_access, &acc_granted, fn);
3226 if ( !NT_STATUS_IS_OK(nt_status) )
3229 info = policy_handle_create(p, &hnd, acc_granted,
3230 struct samr_connect_info, &nt_status);
3231 if (!NT_STATUS_IS_OK(nt_status)) {
3235 DEBUG(5,("%s: %d\n", fn, __LINE__));
3237 *r->out.connect_handle = hnd;
3238 return NT_STATUS_OK;
3241 /****************************************************************
3243 ****************************************************************/
3245 NTSTATUS _samr_Connect3(pipes_struct *p,
3246 struct samr_Connect3 *r)
3248 struct samr_Connect2 c;
3250 c.in.system_name = r->in.system_name;
3251 c.in.access_mask = r->in.access_mask;
3252 c.out.connect_handle = r->out.connect_handle;
3254 return _samr_Connect2(p, &c);
3257 /*******************************************************************
3259 ********************************************************************/
3261 NTSTATUS _samr_Connect4(pipes_struct *p,
3262 struct samr_Connect4 *r)
3264 struct samr_Connect2 c;
3266 c.in.system_name = r->in.system_name;
3267 c.in.access_mask = r->in.access_mask;
3268 c.out.connect_handle = r->out.connect_handle;
3270 return _samr_Connect2(p, &c);
3273 /*******************************************************************
3275 ********************************************************************/
3277 NTSTATUS _samr_Connect5(pipes_struct *p,
3278 struct samr_Connect5 *r)
3281 struct samr_Connect2 c;
3282 struct samr_ConnectInfo1 info1;
3284 info1.client_version = SAMR_CONNECT_AFTER_W2K;
3287 c.in.system_name = r->in.system_name;
3288 c.in.access_mask = r->in.access_mask;
3289 c.out.connect_handle = r->out.connect_handle;
3291 status = _samr_Connect2(p, &c);
3292 if (!NT_STATUS_IS_OK(status)) {
3296 *r->out.level_out = 1;
3297 r->out.info_out->info1 = info1;
3299 return NT_STATUS_OK;
3302 /**********************************************************************
3304 **********************************************************************/
3306 NTSTATUS _samr_LookupDomain(pipes_struct *p,
3307 struct samr_LookupDomain *r)
3310 struct samr_connect_info *info;
3311 const char *domain_name;
3312 DOM_SID *sid = NULL;
3314 /* win9x user manager likes to use SAMR_ACCESS_ENUM_DOMAINS here.
3315 Reverted that change so we will work with RAS servers again */
3317 info = policy_handle_find(p, r->in.connect_handle,
3318 SAMR_ACCESS_LOOKUP_DOMAIN, NULL,
3319 struct samr_connect_info,
3321 if (!NT_STATUS_IS_OK(status)) {
3325 domain_name = r->in.domain_name->string;
3327 return NT_STATUS_INVALID_PARAMETER;
3330 sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
3332 return NT_STATUS_NO_MEMORY;
3335 if (strequal(domain_name, builtin_domain_name())) {
3336 sid_copy(sid, &global_sid_Builtin);
3338 if (!secrets_fetch_domain_sid(domain_name, sid)) {
3339 status = NT_STATUS_NO_SUCH_DOMAIN;
3343 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
3344 sid_string_dbg(sid)));
3351 /**********************************************************************
3353 **********************************************************************/
3355 NTSTATUS _samr_EnumDomains(pipes_struct *p,
3356 struct samr_EnumDomains *r)
3359 struct samr_connect_info *info;
3360 uint32_t num_entries = 2;
3361 struct samr_SamEntry *entry_array = NULL;
3362 struct samr_SamArray *sam;
3364 info = policy_handle_find(p, r->in.connect_handle,
3365 SAMR_ACCESS_ENUM_DOMAINS, NULL,
3366 struct samr_connect_info, &status);
3367 if (!NT_STATUS_IS_OK(status)) {
3371 sam = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
3373 return NT_STATUS_NO_MEMORY;
3376 entry_array = TALLOC_ZERO_ARRAY(p->mem_ctx,
3377 struct samr_SamEntry,
3380 return NT_STATUS_NO_MEMORY;
3383 entry_array[0].idx = 0;
3384 init_lsa_String(&entry_array[0].name, get_global_sam_name());
3386 entry_array[1].idx = 1;
3387 init_lsa_String(&entry_array[1].name, "Builtin");
3389 sam->count = num_entries;
3390 sam->entries = entry_array;
3393 *r->out.num_entries = num_entries;
3398 /*******************************************************************
3400 ********************************************************************/
3402 NTSTATUS _samr_OpenAlias(pipes_struct *p,
3403 struct samr_OpenAlias *r)
3406 uint32 alias_rid = r->in.rid;
3407 struct samr_alias_info *ainfo;
3408 struct samr_domain_info *dinfo;
3409 SEC_DESC *psd = NULL;
3411 uint32 des_access = r->in.access_mask;
3416 dinfo = policy_handle_find(p, r->in.domain_handle,
3417 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
3418 struct samr_domain_info, &status);
3419 if (!NT_STATUS_IS_OK(status)) {
3423 /* append the alias' RID to it */
3425 if (!sid_compose(&sid, &dinfo->sid, alias_rid))
3426 return NT_STATUS_NO_SUCH_ALIAS;
3428 /*check if access can be granted as requested by client. */
3430 map_max_allowed_access(p->server_info->ptok, &des_access);
3432 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
3433 se_map_generic(&des_access,&ali_generic_mapping);
3435 se_priv_copy( &se_rights, &se_add_users );
3438 status = access_check_samr_object(psd, p->server_info->ptok,
3439 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
3440 &acc_granted, "_samr_OpenAlias");
3442 if ( !NT_STATUS_IS_OK(status) )
3446 /* Check we actually have the requested alias */
3447 enum lsa_SidType type;
3452 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
3455 if (!result || (type != SID_NAME_ALIAS)) {
3456 return NT_STATUS_NO_SUCH_ALIAS;
3459 /* make sure there is a mapping */
3461 if ( !sid_to_gid( &sid, &gid ) ) {
3462 return NT_STATUS_NO_SUCH_ALIAS;
3467 ainfo = policy_handle_create(p, r->out.alias_handle, acc_granted,
3468 struct samr_alias_info, &status);
3469 if (!NT_STATUS_IS_OK(status)) {
3474 return NT_STATUS_OK;
3477 /*******************************************************************
3479 ********************************************************************/
3481 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
3482 struct samr_UserInfo7 *id7,
3488 DEBUG(5, ("set_user_info_7: NULL id7\n"));
3489 return NT_STATUS_ACCESS_DENIED;
3492 if (!id7->account_name.string) {
3493 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
3494 return NT_STATUS_ACCESS_DENIED;
3497 /* check to see if the new username already exists. Note: we can't
3498 reliably lock all backends, so there is potentially the
3499 possibility that a user can be created in between this check and
3500 the rename. The rename should fail, but may not get the
3501 exact same failure status code. I think this is small enough
3502 of a window for this type of operation and the results are
3503 simply that the rename fails with a slightly different status
3504 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3506 rc = can_create(mem_ctx, id7->account_name.string);
3507 if (!NT_STATUS_IS_OK(rc)) {
3511 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
3516 /*******************************************************************
3518 ********************************************************************/
3520 static bool set_user_info_16(struct samr_UserInfo16 *id16,
3524 DEBUG(5, ("set_user_info_16: NULL id16\n"));
3528 /* FIX ME: check if the value is really changed --metze */
3529 if (!pdb_set_acct_ctrl(pwd, id16->acct_flags, PDB_CHANGED)) {
3533 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3540 /*******************************************************************
3542 ********************************************************************/
3544 static NTSTATUS set_user_info_18(struct samr_UserInfo18 *id18,
3545 TALLOC_CTX *mem_ctx,
3546 DATA_BLOB *session_key,
3550 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3551 return NT_STATUS_INVALID_PARAMETER;
3554 if (id18->nt_pwd_active || id18->lm_pwd_active) {
3555 if (!session_key->length) {
3556 return NT_STATUS_NO_USER_SESSION_KEY;
3560 if (id18->nt_pwd_active) {
3564 in = data_blob_const(id18->nt_pwd.hash, 16);
3565 out = data_blob_talloc_zero(mem_ctx, 16);
3567 sess_crypt_blob(&out, &in, session_key, false);
3569 if (!pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED)) {
3570 return NT_STATUS_ACCESS_DENIED;
3573 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
3576 if (id18->lm_pwd_active) {
3580 in = data_blob_const(id18->lm_pwd.hash, 16);
3581 out = data_blob_talloc_zero(mem_ctx, 16);
3583 sess_crypt_blob(&out, &in, session_key, false);
3585 if (!pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED)) {
3586 return NT_STATUS_ACCESS_DENIED;
3589 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
3592 copy_id18_to_sam_passwd(pwd, id18);
3594 return pdb_update_sam_account(pwd);
3597 /*******************************************************************
3599 ********************************************************************/
3601 static bool set_user_info_20(struct samr_UserInfo20 *id20,
3605 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3609 copy_id20_to_sam_passwd(pwd, id20);
3611 /* write the change out */
3612 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3619 /*******************************************************************
3621 ********************************************************************/
3623 static NTSTATUS set_user_info_21(struct samr_UserInfo21 *id21,
3624 TALLOC_CTX *mem_ctx,
3625 DATA_BLOB *session_key,
3631 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3632 return NT_STATUS_INVALID_PARAMETER;
3635 if (id21->fields_present == 0) {
3636 return NT_STATUS_INVALID_PARAMETER;
3639 if (id21->fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
3640 return NT_STATUS_ACCESS_DENIED;
3643 if (id21->fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) {
3644 if (id21->nt_password_set) {
3647 if ((id21->nt_owf_password.length != 16) ||
3648 (id21->nt_owf_password.size != 16)) {
3649 return NT_STATUS_INVALID_PARAMETER;
3652 if (!session_key->length) {
3653 return NT_STATUS_NO_USER_SESSION_KEY;
3656 in = data_blob_const(id21->nt_owf_password.array, 16);
3657 out = data_blob_talloc_zero(mem_ctx, 16);
3659 sess_crypt_blob(&out, &in, session_key, false);
3661 pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED);
3662 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
3666 if (id21->fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT) {
3667 if (id21->lm_password_set) {
3670 if ((id21->lm_owf_password.length != 16) ||
3671 (id21->lm_owf_password.size != 16)) {
3672 return NT_STATUS_INVALID_PARAMETER;
3675 if (!session_key->length) {
3676 return NT_STATUS_NO_USER_SESSION_KEY;
3679 in = data_blob_const(id21->lm_owf_password.array, 16);
3680 out = data_blob_talloc_zero(mem_ctx, 16);
3682 sess_crypt_blob(&out, &in, session_key, false);
3684 pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED);
3685 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
3689 /* we need to separately check for an account rename first */
3691 if (id21->account_name.string &&
3692 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
3695 /* check to see if the new username already exists. Note: we can't
3696 reliably lock all backends, so there is potentially the
3697 possibility that a user can be created in between this check and
3698 the rename. The rename should fail, but may not get the
3699 exact same failure status code. I think this is small enough
3700 of a window for this type of operation and the results are
3701 simply that the rename fails with a slightly different status
3702 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3704 status = can_create(mem_ctx, id21->account_name.string);
3705 if (!NT_STATUS_IS_OK(status)) {
3709 status = pdb_rename_sam_account(pwd, id21->account_name.string);
3711 if (!NT_STATUS_IS_OK(status)) {
3712 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
3713 nt_errstr(status)));
3717 /* set the new username so that later
3718 functions can work on the new account */
3719 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
3722 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
3725 * The funny part about the previous two calls is
3726 * that pwd still has the password hashes from the
3727 * passdb entry. These have not been updated from
3728 * id21. I don't know if they need to be set. --jerry
3731 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3732 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3733 if ( !NT_STATUS_IS_OK(status) ) {
3738 /* Don't worry about writing out the user account since the
3739 primary group SID is generated solely from the user's Unix
3742 /* write the change out */
3743 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3747 return NT_STATUS_OK;
3750 /*******************************************************************
3752 ********************************************************************/
3754 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
3755 struct samr_UserInfo23 *id23,
3758 char *plaintext_buf = NULL;
3764 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3765 return NT_STATUS_INVALID_PARAMETER;
3768 if (id23->info.fields_present == 0) {
3769 return NT_STATUS_INVALID_PARAMETER;
3772 if (id23->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
3773 return NT_STATUS_ACCESS_DENIED;
3776 if ((id23->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
3777 (id23->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
3779 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3780 pdb_get_username(pwd)));
3782 if (!decode_pw_buffer(mem_ctx,
3783 id23->password.data,
3787 return NT_STATUS_WRONG_PASSWORD;
3790 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3791 return NT_STATUS_ACCESS_DENIED;
3795 copy_id23_to_sam_passwd(pwd, id23);
3797 acct_ctrl = pdb_get_acct_ctrl(pwd);
3799 /* if it's a trust account, don't update /etc/passwd */
3800 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3801 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3802 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3803 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
3804 } else if (plaintext_buf) {
3805 /* update the UNIX password */
3806 if (lp_unix_password_sync() ) {
3807 struct passwd *passwd;
3808 if (pdb_get_username(pwd) == NULL) {
3809 DEBUG(1, ("chgpasswd: User without name???\n"));
3810 return NT_STATUS_ACCESS_DENIED;
3813 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
3814 if (passwd == NULL) {
3815 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3818 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3819 return NT_STATUS_ACCESS_DENIED;
3821 TALLOC_FREE(passwd);
3825 if (plaintext_buf) {
3826 memset(plaintext_buf, '\0', strlen(plaintext_buf));
3829 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
3830 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
3835 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3839 return NT_STATUS_OK;
3842 /*******************************************************************
3844 ********************************************************************/
3846 static bool set_user_info_pw(uint8 *pass, struct samu *pwd)
3849 char *plaintext_buf = NULL;
3852 DEBUG(5, ("Attempting administrator password change for user %s\n",
3853 pdb_get_username(pwd)));
3855 acct_ctrl = pdb_get_acct_ctrl(pwd);
3857 if (!decode_pw_buffer(talloc_tos(),
3865 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3869 /* if it's a trust account, don't update /etc/passwd */
3870 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3871 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3872 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3873 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3875 /* update the UNIX password */
3876 if (lp_unix_password_sync()) {
3877 struct passwd *passwd;
3879 if (pdb_get_username(pwd) == NULL) {
3880 DEBUG(1, ("chgpasswd: User without name???\n"));
3884 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
3885 if (passwd == NULL) {
3886 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3889 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3892 TALLOC_FREE(passwd);
3896 memset(plaintext_buf, '\0', strlen(plaintext_buf));
3898 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
3903 /*******************************************************************
3905 ********************************************************************/
3907 static NTSTATUS set_user_info_24(TALLOC_CTX *mem_ctx,
3908 struct samr_UserInfo24 *id24,
3914 DEBUG(5, ("set_user_info_24: NULL id24\n"));
3915 return NT_STATUS_INVALID_PARAMETER;
3918 if (!set_user_info_pw(id24->password.data, pwd)) {
3919 return NT_STATUS_WRONG_PASSWORD;
3922 copy_id24_to_sam_passwd(pwd, id24);
3924 status = pdb_update_sam_account(pwd);
3925 if (!NT_STATUS_IS_OK(status)) {
3929 return NT_STATUS_OK;
3932 /*******************************************************************
3934 ********************************************************************/
3936 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
3937 struct samr_UserInfo25 *id25,
3943 DEBUG(5, ("set_user_info_25: NULL id25\n"));
3944 return NT_STATUS_INVALID_PARAMETER;
3947 if (id25->info.fields_present == 0) {
3948 return NT_STATUS_INVALID_PARAMETER;
3951 if (id25->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
3952 return NT_STATUS_ACCESS_DENIED;
3955 if ((id25->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
3956 (id25->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
3958 if (!set_user_info_pw(id25->password.data, pwd)) {
3959 return NT_STATUS_WRONG_PASSWORD;
3963 copy_id25_to_sam_passwd(pwd, id25);
3965 /* write the change out */
3966 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3971 * We need to "pdb_update_sam_account" before the unix primary group
3972 * is set, because the idealx scripts would also change the
3973 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
3974 * the delete explicit / add explicit, which would then fail to find
3975 * the previous primaryGroupSid value.
3978 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3979 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3980 if ( !NT_STATUS_IS_OK(status) ) {
3985 return NT_STATUS_OK;
3988 /*******************************************************************
3990 ********************************************************************/
3992 static NTSTATUS set_user_info_26(TALLOC_CTX *mem_ctx,
3993 struct samr_UserInfo26 *id26,
3999 DEBUG(5, ("set_user_info_26: NULL id26\n"));
4000 return NT_STATUS_INVALID_PARAMETER;
4003 if (!set_user_info_pw(id26->password.data, pwd)) {
4004 return NT_STATUS_WRONG_PASSWORD;
4007 copy_id26_to_sam_passwd(pwd, id26);
4009 status = pdb_update_sam_account(pwd);
4010 if (!NT_STATUS_IS_OK(status)) {
4014 return NT_STATUS_OK;
4018 /*******************************************************************
4020 ********************************************************************/
4022 NTSTATUS _samr_SetUserInfo(pipes_struct *p,
4023 struct samr_SetUserInfo *r)
4025 struct samr_user_info *uinfo;
4027 struct samu *pwd = NULL;
4028 union samr_UserInfo *info = r->in.info;
4029 uint16_t switch_value = r->in.level;
4030 uint32_t acc_required;
4032 bool has_enough_rights = False;
4035 DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
4037 /* This is tricky. A WinXP domain join sets
4038 (SAMR_USER_ACCESS_SET_PASSWORD|SAMR_USER_ACCESS_SET_ATTRIBUTES|SAMR_USER_ACCESS_GET_ATTRIBUTES)
4039 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
4040 standard Win32 API calls just ask for SAMR_USER_ACCESS_SET_PASSWORD in the SamrOpenUser().
4041 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
4042 we'll use the set from the WinXP join as the basis. */
4044 switch (switch_value) {
4049 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
4052 acc_required = SAMR_USER_ACCESS_SET_PASSWORD |
4053 SAMR_USER_ACCESS_SET_ATTRIBUTES |
4054 SAMR_USER_ACCESS_GET_ATTRIBUTES;
4058 uinfo = policy_handle_find(p, r->in.user_handle, acc_required, NULL,
4059 struct samr_user_info, &status);
4060 if (!NT_STATUS_IS_OK(status)) {
4064 DEBUG(5, ("_samr_SetUserInfo: sid:%s, level:%d\n",
4065 sid_string_dbg(&uinfo->sid), switch_value));
4068 DEBUG(5, ("_samr_SetUserInfo: NULL info level\n"));
4069 return NT_STATUS_INVALID_INFO_CLASS;
4072 if (!(pwd = samu_new(NULL))) {
4073 return NT_STATUS_NO_MEMORY;
4077 ret = pdb_getsampwsid(pwd, &uinfo->sid);
4082 return NT_STATUS_NO_SUCH_USER;
4085 /* deal with machine password changes differently from userinfo changes */
4086 /* check to see if we have the sufficient rights */
4088 acb_info = pdb_get_acct_ctrl(pwd);
4089 if (acb_info & ACB_WSTRUST)
4090 has_enough_rights = user_has_privileges(p->server_info->ptok,
4091 &se_machine_account);
4092 else if (acb_info & ACB_NORMAL)
4093 has_enough_rights = user_has_privileges(p->server_info->ptok,
4095 else if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
4096 if (lp_enable_privileges()) {
4097 has_enough_rights = nt_token_check_domain_rid(p->server_info->ptok,
4098 DOMAIN_GROUP_RID_ADMINS);
4102 DEBUG(5, ("_samr_SetUserInfo: %s does%s possess sufficient rights\n",
4103 uidtoname(p->server_info->utok.uid),
4104 has_enough_rights ? "" : " not"));
4106 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
4108 if (has_enough_rights) {
4112 /* ok! user info levels (lots: see MSDEV help), off we go... */
4114 switch (switch_value) {
4117 status = set_user_info_7(p->mem_ctx,
4122 if (!set_user_info_16(&info->info16, pwd)) {
4123 status = NT_STATUS_ACCESS_DENIED;
4128 /* Used by AS/U JRA. */
4129 status = set_user_info_18(&info->info18,
4131 &p->server_info->user_session_key,
4136 if (!set_user_info_20(&info->info20, pwd)) {
4137 status = NT_STATUS_ACCESS_DENIED;
4142 status = set_user_info_21(&info->info21,
4144 &p->server_info->user_session_key,
4149 if (!p->server_info->user_session_key.length) {
4150 status = NT_STATUS_NO_USER_SESSION_KEY;
4152 arcfour_crypt_blob(info->info23.password.data, 516,
4153 &p->server_info->user_session_key);
4155 dump_data(100, info->info23.password.data, 516);
4157 status = set_user_info_23(p->mem_ctx,
4158 &info->info23, pwd);
4162 if (!p->server_info->user_session_key.length) {
4163 status = NT_STATUS_NO_USER_SESSION_KEY;
4165 arcfour_crypt_blob(info->info24.password.data,
4167 &p->server_info->user_session_key);
4169 dump_data(100, info->info24.password.data, 516);
4171 status = set_user_info_24(p->mem_ctx,
4172 &info->info24, pwd);
4176 if (!p->server_info->user_session_key.length) {
4177 status = NT_STATUS_NO_USER_SESSION_KEY;
4179 encode_or_decode_arc4_passwd_buffer(
4180 info->info25.password.data,
4181 &p->server_info->user_session_key);
4183 dump_data(100, info->info25.password.data, 532);
4185 status = set_user_info_25(p->mem_ctx,
4186 &info->info25, pwd);
4190 if (!p->server_info->user_session_key.length) {
4191 status = NT_STATUS_NO_USER_SESSION_KEY;
4193 encode_or_decode_arc4_passwd_buffer(
4194 info->info26.password.data,
4195 &p->server_info->user_session_key);
4197 dump_data(100, info->info26.password.data, 516);
4199 status = set_user_info_26(p->mem_ctx,
4200 &info->info26, pwd);
4204 status = NT_STATUS_INVALID_INFO_CLASS;
4209 if (has_enough_rights) {
4213 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
4215 if (NT_STATUS_IS_OK(status)) {
4216 force_flush_samr_cache(&uinfo->sid);
4222 /*******************************************************************
4224 ********************************************************************/
4226 NTSTATUS _samr_SetUserInfo2(pipes_struct *p,
4227 struct samr_SetUserInfo2 *r)
4229 struct samr_SetUserInfo q;
4231 q.in.user_handle = r->in.user_handle;
4232 q.in.level = r->in.level;
4233 q.in.info = r->in.info;
4235 return _samr_SetUserInfo(p, &q);
4238 /*********************************************************************
4239 _samr_GetAliasMembership
4240 *********************************************************************/
4242 NTSTATUS _samr_GetAliasMembership(pipes_struct *p,
4243 struct samr_GetAliasMembership *r)
4245 size_t num_alias_rids;
4247 struct samr_domain_info *dinfo;
4254 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__));
4256 dinfo = policy_handle_find(p, r->in.domain_handle,
4257 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS
4258 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
4259 struct samr_domain_info, &status);
4260 if (!NT_STATUS_IS_OK(status)) {
4264 if (!sid_check_is_domain(&dinfo->sid) &&
4265 !sid_check_is_builtin(&dinfo->sid))
4266 return NT_STATUS_OBJECT_TYPE_MISMATCH;
4268 if (r->in.sids->num_sids) {
4269 members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, r->in.sids->num_sids);
4271 if (members == NULL)
4272 return NT_STATUS_NO_MEMORY;
4277 for (i=0; i<r->in.sids->num_sids; i++)
4278 sid_copy(&members[i], r->in.sids->sids[i].sid);
4284 status = pdb_enum_alias_memberships(p->mem_ctx, &dinfo->sid, members,
4285 r->in.sids->num_sids,
4286 &alias_rids, &num_alias_rids);
4289 if (!NT_STATUS_IS_OK(status)) {
4293 r->out.rids->count = num_alias_rids;
4294 r->out.rids->ids = alias_rids;
4296 return NT_STATUS_OK;
4299 /*********************************************************************
4300 _samr_GetMembersInAlias
4301 *********************************************************************/
4303 NTSTATUS _samr_GetMembersInAlias(pipes_struct *p,
4304 struct samr_GetMembersInAlias *r)
4306 struct samr_alias_info *ainfo;
4309 size_t num_sids = 0;
4310 struct lsa_SidPtr *sids = NULL;
4311 DOM_SID *pdb_sids = NULL;
4313 ainfo = policy_handle_find(p, r->in.alias_handle,
4314 SAMR_ALIAS_ACCESS_GET_MEMBERS, NULL,
4315 struct samr_alias_info, &status);
4316 if (!NT_STATUS_IS_OK(status)) {
4320 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
4323 status = pdb_enum_aliasmem(&ainfo->sid, &pdb_sids, &num_sids);
4326 if (!NT_STATUS_IS_OK(status)) {
4331 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr, num_sids);
4333 TALLOC_FREE(pdb_sids);
4334 return NT_STATUS_NO_MEMORY;
4338 for (i = 0; i < num_sids; i++) {
4339 sids[i].sid = sid_dup_talloc(p->mem_ctx, &pdb_sids[i]);
4341 TALLOC_FREE(pdb_sids);
4342 return NT_STATUS_NO_MEMORY;
4346 r->out.sids->num_sids = num_sids;
4347 r->out.sids->sids = sids;
4349 TALLOC_FREE(pdb_sids);
4351 return NT_STATUS_OK;
4354 /*********************************************************************
4355 _samr_QueryGroupMember
4356 *********************************************************************/
4358 NTSTATUS _samr_QueryGroupMember(pipes_struct *p,
4359 struct samr_QueryGroupMember *r)
4361 struct samr_group_info *ginfo;
4362 size_t i, num_members;
4368 struct samr_RidTypeArray *rids = NULL;
4370 ginfo = policy_handle_find(p, r->in.group_handle,
4371 SAMR_GROUP_ACCESS_GET_MEMBERS, NULL,
4372 struct samr_group_info, &status);
4373 if (!NT_STATUS_IS_OK(status)) {
4377 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidTypeArray);
4379 return NT_STATUS_NO_MEMORY;
4382 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
4384 if (!sid_check_is_in_our_domain(&ginfo->sid)) {
4385 DEBUG(3, ("sid %s is not in our domain\n",
4386 sid_string_dbg(&ginfo->sid)));
4387 return NT_STATUS_NO_SUCH_GROUP;
4390 DEBUG(10, ("lookup on Domain SID\n"));
4393 status = pdb_enum_group_members(p->mem_ctx, &ginfo->sid,
4394 &rid, &num_members);
4397 if (!NT_STATUS_IS_OK(status))
4401 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
4403 return NT_STATUS_NO_MEMORY;
4409 for (i=0; i<num_members; i++)
4410 attr[i] = SID_NAME_USER;
4412 rids->count = num_members;
4416 *r->out.rids = rids;
4418 return NT_STATUS_OK;
4421 /*********************************************************************
4422 _samr_AddAliasMember
4423 *********************************************************************/
4425 NTSTATUS _samr_AddAliasMember(pipes_struct *p,
4426 struct samr_AddAliasMember *r)
4428 struct samr_alias_info *ainfo;
4430 bool can_add_accounts;
4433 ainfo = policy_handle_find(p, r->in.alias_handle,
4434 SAMR_ALIAS_ACCESS_ADD_MEMBER, NULL,
4435 struct samr_alias_info, &status);
4436 if (!NT_STATUS_IS_OK(status)) {
4440 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
4442 se_priv_copy( &se_rights, &se_add_users );
4443 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4445 /******** BEGIN SeAddUsers BLOCK *********/
4447 if ( can_add_accounts )
4450 status = pdb_add_aliasmem(&ainfo->sid, r->in.sid);
4452 if ( can_add_accounts )
4455 /******** END SeAddUsers BLOCK *********/
4457 if (NT_STATUS_IS_OK(status)) {
4458 force_flush_samr_cache(&ainfo->sid);
4464 /*********************************************************************
4465 _samr_DeleteAliasMember
4466 *********************************************************************/
4468 NTSTATUS _samr_DeleteAliasMember(pipes_struct *p,
4469 struct samr_DeleteAliasMember *r)
4471 struct samr_alias_info *ainfo;
4473 bool can_add_accounts;
4476 ainfo = policy_handle_find(p, r->in.alias_handle,
4477 SAMR_ALIAS_ACCESS_REMOVE_MEMBER, NULL,
4478 struct samr_alias_info, &status);
4479 if (!NT_STATUS_IS_OK(status)) {
4483 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
4484 sid_string_dbg(&ainfo->sid)));
4486 se_priv_copy( &se_rights, &se_add_users );
4487 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4489 /******** BEGIN SeAddUsers BLOCK *********/
4491 if ( can_add_accounts )
4494 status = pdb_del_aliasmem(&ainfo->sid, r->in.sid);
4496 if ( can_add_accounts )
4499 /******** END SeAddUsers BLOCK *********/
4501 if (NT_STATUS_IS_OK(status)) {
4502 force_flush_samr_cache(&ainfo->sid);
4508 /*********************************************************************
4509 _samr_AddGroupMember
4510 *********************************************************************/
4512 NTSTATUS _samr_AddGroupMember(pipes_struct *p,
4513 struct samr_AddGroupMember *r)
4515 struct samr_group_info *ginfo;
4519 bool can_add_accounts;
4521 ginfo = policy_handle_find(p, r->in.group_handle,
4522 SAMR_GROUP_ACCESS_ADD_MEMBER, NULL,
4523 struct samr_group_info, &status);
4524 if (!NT_STATUS_IS_OK(status)) {
4528 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
4530 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
4532 return NT_STATUS_INVALID_HANDLE;
4535 se_priv_copy( &se_rights, &se_add_users );
4536 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4538 /******** BEGIN SeAddUsers BLOCK *********/
4540 if ( can_add_accounts )
4543 status = pdb_add_groupmem(p->mem_ctx, group_rid, r->in.rid);
4545 if ( can_add_accounts )
4548 /******** END SeAddUsers BLOCK *********/
4550 force_flush_samr_cache(&ginfo->sid);
4555 /*********************************************************************
4556 _samr_DeleteGroupMember
4557 *********************************************************************/
4559 NTSTATUS _samr_DeleteGroupMember(pipes_struct *p,
4560 struct samr_DeleteGroupMember *r)
4563 struct samr_group_info *ginfo;
4567 bool can_add_accounts;
4570 * delete the group member named r->in.rid
4571 * who is a member of the sid associated with the handle
4572 * the rid is a user's rid as the group is a domain group.
4575 ginfo = policy_handle_find(p, r->in.group_handle,
4576 SAMR_GROUP_ACCESS_REMOVE_MEMBER, NULL,
4577 struct samr_group_info, &status);
4578 if (!NT_STATUS_IS_OK(status)) {
4582 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
4584 return NT_STATUS_INVALID_HANDLE;
4587 se_priv_copy( &se_rights, &se_add_users );
4588 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4590 /******** BEGIN SeAddUsers BLOCK *********/
4592 if ( can_add_accounts )
4595 status = pdb_del_groupmem(p->mem_ctx, group_rid, r->in.rid);
4597 if ( can_add_accounts )
4600 /******** END SeAddUsers BLOCK *********/
4602 force_flush_samr_cache(&ginfo->sid);
4607 /*********************************************************************
4609 *********************************************************************/
4611 NTSTATUS _samr_DeleteUser(pipes_struct *p,
4612 struct samr_DeleteUser *r)
4614 struct samr_user_info *uinfo;
4616 struct samu *sam_pass=NULL;
4617 bool can_add_accounts;
4621 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__));
4623 uinfo = policy_handle_find(p, r->in.user_handle,
4624 STD_RIGHT_DELETE_ACCESS, NULL,
4625 struct samr_user_info, &status);
4626 if (!NT_STATUS_IS_OK(status)) {
4630 if (!sid_check_is_in_our_domain(&uinfo->sid))
4631 return NT_STATUS_CANNOT_DELETE;
4633 /* check if the user exists before trying to delete */
4634 if ( !(sam_pass = samu_new( NULL )) ) {
4635 return NT_STATUS_NO_MEMORY;
4639 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
4643 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
4644 sid_string_dbg(&uinfo->sid)));
4645 TALLOC_FREE(sam_pass);
4646 return NT_STATUS_NO_SUCH_USER;
4649 acb_info = pdb_get_acct_ctrl(sam_pass);
4651 /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
4652 if ( acb_info & ACB_WSTRUST ) {
4653 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_machine_account );
4655 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_add_users );
4658 /******** BEGIN SeAddUsers BLOCK *********/
4660 if ( can_add_accounts )
4663 status = pdb_delete_user(p->mem_ctx, sam_pass);
4665 if ( can_add_accounts )
4668 /******** END SeAddUsers BLOCK *********/
4670 if ( !NT_STATUS_IS_OK(status) ) {
4671 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
4672 "user %s: %s.\n", pdb_get_username(sam_pass),
4673 nt_errstr(status)));
4674 TALLOC_FREE(sam_pass);
4679 TALLOC_FREE(sam_pass);
4681 if (!close_policy_hnd(p, r->in.user_handle))
4682 return NT_STATUS_OBJECT_NAME_INVALID;
4684 ZERO_STRUCTP(r->out.user_handle);
4686 force_flush_samr_cache(&uinfo->sid);
4688 return NT_STATUS_OK;
4691 /*********************************************************************
4692 _samr_DeleteDomainGroup
4693 *********************************************************************/
4695 NTSTATUS _samr_DeleteDomainGroup(pipes_struct *p,
4696 struct samr_DeleteDomainGroup *r)
4698 struct samr_group_info *ginfo;
4702 bool can_add_accounts;
4704 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__));
4706 ginfo = policy_handle_find(p, r->in.group_handle,
4707 STD_RIGHT_DELETE_ACCESS, NULL,
4708 struct samr_group_info, &status);
4709 if (!NT_STATUS_IS_OK(status)) {
4713 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
4715 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
4717 return NT_STATUS_NO_SUCH_GROUP;
4720 se_priv_copy( &se_rights, &se_add_users );
4721 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4723 /******** BEGIN SeAddUsers BLOCK *********/
4725 if ( can_add_accounts )
4728 status = pdb_delete_dom_group(p->mem_ctx, group_rid);
4730 if ( can_add_accounts )
4733 /******** END SeAddUsers BLOCK *********/
4735 if ( !NT_STATUS_IS_OK(status) ) {
4736 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
4737 "entry for group %s: %s\n",
4738 sid_string_dbg(&ginfo->sid),
4739 nt_errstr(status)));
4743 if (!close_policy_hnd(p, r->in.group_handle))
4744 return NT_STATUS_OBJECT_NAME_INVALID;
4746 force_flush_samr_cache(&ginfo->sid);
4748 return NT_STATUS_OK;
4751 /*********************************************************************
4752 _samr_DeleteDomAlias
4753 *********************************************************************/
4755 NTSTATUS _samr_DeleteDomAlias(pipes_struct *p,
4756 struct samr_DeleteDomAlias *r)
4758 struct samr_alias_info *ainfo;
4760 bool can_add_accounts;
4763 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__));
4765 ainfo = policy_handle_find(p, r->in.alias_handle,
4766 STD_RIGHT_DELETE_ACCESS, NULL,
4767 struct samr_alias_info, &status);
4768 if (!NT_STATUS_IS_OK(status)) {
4772 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
4774 /* Don't let Windows delete builtin groups */
4776 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
4777 return NT_STATUS_SPECIAL_ACCOUNT;
4780 if (!sid_check_is_in_our_domain(&ainfo->sid))
4781 return NT_STATUS_NO_SUCH_ALIAS;
4783 DEBUG(10, ("lookup on Local SID\n"));
4785 se_priv_copy( &se_rights, &se_add_users );
4786 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4788 /******** BEGIN SeAddUsers BLOCK *********/
4790 if ( can_add_accounts )
4793 /* Have passdb delete the alias */
4794 status = pdb_delete_alias(&ainfo->sid);
4796 if ( can_add_accounts )
4799 /******** END SeAddUsers BLOCK *********/
4801 if ( !NT_STATUS_IS_OK(status))
4804 if (!close_policy_hnd(p, r->in.alias_handle))
4805 return NT_STATUS_OBJECT_NAME_INVALID;
4807 force_flush_samr_cache(&ainfo->sid);
4809 return NT_STATUS_OK;
4812 /*********************************************************************
4813 _samr_CreateDomainGroup
4814 *********************************************************************/
4816 NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
4817 struct samr_CreateDomainGroup *r)
4822 struct samr_domain_info *dinfo;
4823 struct samr_group_info *ginfo;
4825 bool can_add_accounts;
4827 dinfo = policy_handle_find(p, r->in.domain_handle,
4828 SAMR_DOMAIN_ACCESS_CREATE_GROUP, NULL,
4829 struct samr_domain_info, &status);
4830 if (!NT_STATUS_IS_OK(status)) {
4834 if (!sid_equal(&dinfo->sid, get_global_sam_sid()))
4835 return NT_STATUS_ACCESS_DENIED;
4837 name = r->in.name->string;
4839 return NT_STATUS_NO_MEMORY;
4842 status = can_create(p->mem_ctx, name);
4843 if (!NT_STATUS_IS_OK(status)) {
4847 se_priv_copy( &se_rights, &se_add_users );
4848 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4850 /******** BEGIN SeAddUsers BLOCK *********/
4852 if ( can_add_accounts )
4855 /* check that we successfully create the UNIX group */
4857 status = pdb_create_dom_group(p->mem_ctx, name, r->out.rid);
4859 if ( can_add_accounts )
4862 /******** END SeAddUsers BLOCK *********/
4864 /* check if we should bail out here */
4866 if ( !NT_STATUS_IS_OK(status) )
4869 ginfo = policy_handle_create(p, r->out.group_handle,
4870 GENERIC_RIGHTS_GROUP_ALL_ACCESS,
4871 struct samr_group_info, &status);
4872 if (!NT_STATUS_IS_OK(status)) {
4875 sid_compose(&ginfo->sid, &dinfo->sid, *r->out.rid);
4877 force_flush_samr_cache(&dinfo->sid);
4879 return NT_STATUS_OK;
4882 /*********************************************************************
4883 _samr_CreateDomAlias
4884 *********************************************************************/
4886 NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
4887 struct samr_CreateDomAlias *r)
4890 const char *name = NULL;
4891 struct samr_domain_info *dinfo;
4892 struct samr_alias_info *ainfo;
4896 bool can_add_accounts;
4898 dinfo = policy_handle_find(p, r->in.domain_handle,
4899 SAMR_DOMAIN_ACCESS_CREATE_ALIAS, NULL,
4900 struct samr_domain_info, &result);
4901 if (!NT_STATUS_IS_OK(result)) {
4905 if (!sid_equal(&dinfo->sid, get_global_sam_sid()))
4906 return NT_STATUS_ACCESS_DENIED;
4908 name = r->in.alias_name->string;
4910 se_priv_copy( &se_rights, &se_add_users );
4911 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4913 result = can_create(p->mem_ctx, name);
4914 if (!NT_STATUS_IS_OK(result)) {
4918 /******** BEGIN SeAddUsers BLOCK *********/
4920 if ( can_add_accounts )
4923 /* Have passdb create the alias */
4924 result = pdb_create_alias(name, r->out.rid);
4926 if ( can_add_accounts )
4929 /******** END SeAddUsers BLOCK *********/
4931 if (!NT_STATUS_IS_OK(result)) {
4932 DEBUG(10, ("pdb_create_alias failed: %s\n",
4933 nt_errstr(result)));
4937 sid_compose(&info_sid, &dinfo->sid, *r->out.rid);
4939 if (!sid_to_gid(&info_sid, &gid)) {
4940 DEBUG(10, ("Could not find alias just created\n"));
4941 return NT_STATUS_ACCESS_DENIED;
4944 /* check if the group has been successfully created */
4945 if ( getgrgid(gid) == NULL ) {
4946 DEBUG(10, ("getgrgid(%d) of just created alias failed\n",
4948 return NT_STATUS_ACCESS_DENIED;
4951 ainfo = policy_handle_create(p, r->out.alias_handle,
4952 GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
4953 struct samr_alias_info, &result);
4954 if (!NT_STATUS_IS_OK(result)) {
4957 ainfo->sid = info_sid;
4959 force_flush_samr_cache(&info_sid);
4961 return NT_STATUS_OK;
4964 /*********************************************************************
4965 _samr_QueryGroupInfo
4966 *********************************************************************/
4968 NTSTATUS _samr_QueryGroupInfo(pipes_struct *p,
4969 struct samr_QueryGroupInfo *r)
4971 struct samr_group_info *ginfo;
4974 union samr_GroupInfo *info = NULL;
4976 uint32_t attributes = SE_GROUP_MANDATORY |
4977 SE_GROUP_ENABLED_BY_DEFAULT |
4979 const char *group_name = NULL;
4980 const char *group_description = NULL;
4982 ginfo = policy_handle_find(p, r->in.group_handle,
4983 SAMR_GROUP_ACCESS_LOOKUP_INFO, NULL,
4984 struct samr_group_info, &status);
4985 if (!NT_STATUS_IS_OK(status)) {
4990 ret = get_domain_group_from_sid(ginfo->sid, &map);
4993 return NT_STATUS_INVALID_HANDLE;
4995 /* FIXME: map contains fstrings */
4996 group_name = talloc_strdup(r, map.nt_name);
4997 group_description = talloc_strdup(r, map.comment);
4999 info = TALLOC_ZERO_P(p->mem_ctx, union samr_GroupInfo);
5001 return NT_STATUS_NO_MEMORY;
5004 switch (r->in.level) {
5010 status = pdb_enum_group_members(
5011 p->mem_ctx, &ginfo->sid, &members,
5015 if (!NT_STATUS_IS_OK(status)) {
5019 info->all.name.string = group_name;
5020 info->all.attributes = attributes;
5021 info->all.num_members = num_members;
5022 info->all.description.string = group_description;
5026 info->name.string = group_name;
5029 info->attributes.attributes = attributes;
5032 info->description.string = group_description;
5042 status = pdb_enum_group_members(
5043 p->mem_ctx, &ginfo->sid, &members,
5047 if (!NT_STATUS_IS_OK(status)) {
5051 info->all2.name.string = group_name;
5052 info->all2.attributes = attributes;
5053 info->all2.num_members = 0; /* num_members - in w2k3 this is always 0 */
5054 info->all2.description.string = group_description;
5059 return NT_STATUS_INVALID_INFO_CLASS;
5062 *r->out.info = info;
5064 return NT_STATUS_OK;
5067 /*********************************************************************
5069 *********************************************************************/
5071 NTSTATUS _samr_SetGroupInfo(pipes_struct *p,
5072 struct samr_SetGroupInfo *r)
5074 struct samr_group_info *ginfo;
5078 bool can_mod_accounts;
5080 ginfo = policy_handle_find(p, r->in.group_handle,
5081 SAMR_GROUP_ACCESS_SET_INFO, NULL,
5082 struct samr_group_info, &status);
5083 if (!NT_STATUS_IS_OK(status)) {
5088 ret = get_domain_group_from_sid(ginfo->sid, &map);
5091 return NT_STATUS_NO_SUCH_GROUP;
5093 switch (r->in.level) {
5095 fstrcpy(map.comment, r->in.info->all.description.string);
5098 /* group rename is not supported yet */
5099 return NT_STATUS_NOT_SUPPORTED;
5101 fstrcpy(map.comment, r->in.info->description.string);
5104 return NT_STATUS_INVALID_INFO_CLASS;
5107 can_mod_accounts = user_has_privileges( p->server_info->ptok, &se_add_users );
5109 /******** BEGIN SeAddUsers BLOCK *********/
5111 if ( can_mod_accounts )
5114 status = pdb_update_group_mapping_entry(&map);
5116 if ( can_mod_accounts )
5119 /******** End SeAddUsers BLOCK *********/
5121 if (NT_STATUS_IS_OK(status)) {
5122 force_flush_samr_cache(&ginfo->sid);
5128 /*********************************************************************
5130 *********************************************************************/
5132 NTSTATUS _samr_SetAliasInfo(pipes_struct *p,
5133 struct samr_SetAliasInfo *r)
5135 struct samr_alias_info *ainfo;
5136 struct acct_info info;
5137 bool can_mod_accounts;
5140 ainfo = policy_handle_find(p, r->in.alias_handle,
5141 SAMR_ALIAS_ACCESS_SET_INFO, NULL,
5142 struct samr_alias_info, &status);
5143 if (!NT_STATUS_IS_OK(status)) {
5147 /* get the current group information */
5150 status = pdb_get_aliasinfo( &ainfo->sid, &info );
5153 if ( !NT_STATUS_IS_OK(status))
5156 switch (r->in.level) {
5161 /* We currently do not support renaming groups in the
5162 the BUILTIN domain. Refer to util_builtin.c to understand
5163 why. The eventually needs to be fixed to be like Windows
5164 where you can rename builtin groups, just not delete them */
5166 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
5167 return NT_STATUS_SPECIAL_ACCOUNT;
5170 /* There has to be a valid name (and it has to be different) */
5172 if ( !r->in.info->name.string )
5173 return NT_STATUS_INVALID_PARAMETER;
5175 /* If the name is the same just reply "ok". Yes this
5176 doesn't allow you to change the case of a group name. */
5178 if ( strequal( r->in.info->name.string, info.acct_name ) )
5179 return NT_STATUS_OK;
5181 fstrcpy( info.acct_name, r->in.info->name.string);
5183 /* make sure the name doesn't already exist as a user
5186 fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
5187 status = can_create( p->mem_ctx, group_name );
5188 if ( !NT_STATUS_IS_OK( status ) )
5192 case ALIASINFODESCRIPTION:
5193 if (r->in.info->description.string) {
5194 fstrcpy(info.acct_desc,
5195 r->in.info->description.string);
5197 fstrcpy( info.acct_desc, "" );
5201 return NT_STATUS_INVALID_INFO_CLASS;
5204 can_mod_accounts = user_has_privileges( p->server_info->ptok, &se_add_users );
5206 /******** BEGIN SeAddUsers BLOCK *********/
5208 if ( can_mod_accounts )
5211 status = pdb_set_aliasinfo( &ainfo->sid, &info );
5213 if ( can_mod_accounts )
5216 /******** End SeAddUsers BLOCK *********/
5218 if (NT_STATUS_IS_OK(status))
5219 force_flush_samr_cache(&ainfo->sid);
5224 /****************************************************************
5226 ****************************************************************/
5228 NTSTATUS _samr_GetDomPwInfo(pipes_struct *p,
5229 struct samr_GetDomPwInfo *r)
5231 uint32_t min_password_length = 0;
5232 uint32_t password_properties = 0;
5234 /* Perform access check. Since this rpc does not require a
5235 policy handle it will not be caught by the access checks on
5236 SAMR_CONNECT or SAMR_CONNECT_ANON. */
5238 if (!pipe_access_check(p)) {
5239 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
5240 return NT_STATUS_ACCESS_DENIED;
5244 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
5245 &min_password_length);
5246 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
5247 &password_properties);
5250 if (lp_check_password_script() && *lp_check_password_script()) {
5251 password_properties |= DOMAIN_PASSWORD_COMPLEX;
5254 r->out.info->min_password_length = min_password_length;
5255 r->out.info->password_properties = password_properties;
5257 return NT_STATUS_OK;
5260 /*********************************************************************
5262 *********************************************************************/
5264 NTSTATUS _samr_OpenGroup(pipes_struct *p,
5265 struct samr_OpenGroup *r)
5270 struct samr_domain_info *dinfo;
5271 struct samr_group_info *ginfo;
5272 SEC_DESC *psd = NULL;
5274 uint32 des_access = r->in.access_mask;
5280 dinfo = policy_handle_find(p, r->in.domain_handle,
5281 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
5282 struct samr_domain_info, &status);
5283 if (!NT_STATUS_IS_OK(status)) {
5287 /*check if access can be granted as requested by client. */
5288 map_max_allowed_access(p->server_info->ptok, &des_access);
5290 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
5291 se_map_generic(&des_access,&grp_generic_mapping);
5293 se_priv_copy( &se_rights, &se_add_users );
5295 status = access_check_samr_object(psd, p->server_info->ptok,
5296 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access,
5297 &acc_granted, "_samr_OpenGroup");
5299 if ( !NT_STATUS_IS_OK(status) )
5302 /* this should not be hard-coded like this */
5304 if (!sid_equal(&dinfo->sid, get_global_sam_sid()))
5305 return NT_STATUS_ACCESS_DENIED;
5307 sid_compose(&info_sid, &dinfo->sid, r->in.rid);
5309 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n",
5310 sid_string_dbg(&info_sid)));
5312 /* check if that group really exists */
5314 ret = get_domain_group_from_sid(info_sid, &map);
5317 return NT_STATUS_NO_SUCH_GROUP;
5319 ginfo = policy_handle_create(p, r->out.group_handle,
5320 GENERIC_RIGHTS_GROUP_ALL_ACCESS,
5321 struct samr_group_info, &status);
5322 if (!NT_STATUS_IS_OK(status)) {
5325 ginfo->sid = info_sid;
5327 return NT_STATUS_OK;
5330 /*********************************************************************
5331 _samr_RemoveMemberFromForeignDomain
5332 *********************************************************************/
5334 NTSTATUS _samr_RemoveMemberFromForeignDomain(pipes_struct *p,
5335 struct samr_RemoveMemberFromForeignDomain *r)
5337 struct samr_domain_info *dinfo;
5340 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
5341 sid_string_dbg(r->in.sid)));
5343 /* Find the policy handle. Open a policy on it. */
5345 dinfo = policy_handle_find(p, r->in.domain_handle,
5346 STD_RIGHT_DELETE_ACCESS, NULL,
5347 struct samr_domain_info, &result);
5348 if (!NT_STATUS_IS_OK(result)) {
5352 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
5353 sid_string_dbg(&dinfo->sid)));
5355 /* we can only delete a user from a group since we don't have
5356 nested groups anyways. So in the latter case, just say OK */
5358 /* TODO: The above comment nowadays is bogus. Since we have nested
5359 * groups now, and aliases members are never reported out of the unix
5360 * group membership, the "just say OK" makes this call a no-op. For
5361 * us. This needs fixing however. */
5363 /* I've only ever seen this in the wild when deleting a user from
5364 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
5365 * is the user about to be deleted. I very much suspect this is the
5366 * only application of this call. To verify this, let people report
5369 if (!sid_check_is_builtin(&dinfo->sid)) {
5370 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
5371 "global_sam_sid() = %s\n",
5372 sid_string_dbg(&dinfo->sid),
5373 sid_string_dbg(get_global_sam_sid())));
5374 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
5375 return NT_STATUS_OK;
5378 force_flush_samr_cache(&dinfo->sid);
5380 result = NT_STATUS_OK;
5385 /*******************************************************************
5386 _samr_QueryDomainInfo2
5387 ********************************************************************/
5389 NTSTATUS _samr_QueryDomainInfo2(pipes_struct *p,
5390 struct samr_QueryDomainInfo2 *r)
5392 struct samr_QueryDomainInfo q;
5394 q.in.domain_handle = r->in.domain_handle;
5395 q.in.level = r->in.level;
5397 q.out.info = r->out.info;
5399 return _samr_QueryDomainInfo(p, &q);
5402 /*******************************************************************
5404 ********************************************************************/
5406 NTSTATUS _samr_SetDomainInfo(pipes_struct *p,
5407 struct samr_SetDomainInfo *r)
5409 struct samr_domain_info *dinfo;
5410 time_t u_expire, u_min_age;
5412 time_t u_lock_duration, u_reset_time;
5415 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5417 /* We do have different access bits for info
5418 * levels here, but we're really just looking for
5419 * GENERIC_RIGHTS_DOMAIN_WRITE access. Unfortunately
5420 * this maps to different specific bits. So
5421 * assume if we have SAMR_DOMAIN_ACCESS_SET_INFO_1
5424 dinfo = policy_handle_find(p, r->in.domain_handle,
5425 SAMR_DOMAIN_ACCESS_SET_INFO_1, NULL,
5426 struct samr_domain_info, &result);
5427 if (!NT_STATUS_IS_OK(result)) {
5431 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r->in.level));
5433 switch (r->in.level) {
5435 u_expire=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.max_password_age);
5436 u_min_age=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.min_password_age);
5437 pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)r->in.info->info1.min_password_length);
5438 pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)r->in.info->info1.password_history_length);
5439 pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)r->in.info->info1.password_properties);
5440 pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
5441 pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
5446 u_logout=nt_time_to_unix_abs((NTTIME *)&r->in.info->info3.force_logoff_time);
5447 pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
5456 u_lock_duration=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_duration);
5457 if (u_lock_duration != -1)
5458 u_lock_duration /= 60;
5460 u_reset_time=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_window)/60;
5462 pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
5463 pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
5464 pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)r->in.info->info12.lockout_threshold);
5467 return NT_STATUS_INVALID_INFO_CLASS;
5470 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5472 return NT_STATUS_OK;
5475 /****************************************************************
5476 _samr_GetDisplayEnumerationIndex
5477 ****************************************************************/
5479 NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
5480 struct samr_GetDisplayEnumerationIndex *r)
5482 struct samr_domain_info *dinfo;
5483 uint32_t max_entries = (uint32_t) -1;
5484 uint32_t enum_context = 0;
5486 uint32_t num_account = 0;
5487 struct samr_displayentry *entries = NULL;
5490 DEBUG(5,("_samr_GetDisplayEnumerationIndex: %d\n", __LINE__));
5492 dinfo = policy_handle_find(p, r->in.domain_handle,
5493 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
5494 struct samr_domain_info, &status);
5495 if (!NT_STATUS_IS_OK(status)) {
5499 if ((r->in.level < 1) || (r->in.level > 3)) {
5500 DEBUG(0,("_samr_GetDisplayEnumerationIndex: "
5501 "Unknown info level (%u)\n",
5503 return NT_STATUS_INVALID_INFO_CLASS;
5508 /* The following done as ROOT. Don't return without unbecome_root(). */
5510 switch (r->in.level) {
5512 if (dinfo->disp_info->users == NULL) {
5513 dinfo->disp_info->users = pdb_search_users(
5514 dinfo->disp_info, ACB_NORMAL);
5515 if (dinfo->disp_info->users == NULL) {
5517 return NT_STATUS_ACCESS_DENIED;
5519 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5520 "starting user enumeration at index %u\n",
5521 (unsigned int)enum_context));
5523 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5524 "using cached user enumeration at index %u\n",
5525 (unsigned int)enum_context));
5527 num_account = pdb_search_entries(dinfo->disp_info->users,
5528 enum_context, max_entries,
5532 if (dinfo->disp_info->machines == NULL) {
5533 dinfo->disp_info->machines = pdb_search_users(
5534 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
5535 if (dinfo->disp_info->machines == NULL) {
5537 return NT_STATUS_ACCESS_DENIED;
5539 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5540 "starting machine enumeration at index %u\n",
5541 (unsigned int)enum_context));
5543 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5544 "using cached machine enumeration at index %u\n",
5545 (unsigned int)enum_context));
5547 num_account = pdb_search_entries(dinfo->disp_info->machines,
5548 enum_context, max_entries,
5552 if (dinfo->disp_info->groups == NULL) {
5553 dinfo->disp_info->groups = pdb_search_groups(
5555 if (dinfo->disp_info->groups == NULL) {
5557 return NT_STATUS_ACCESS_DENIED;
5559 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5560 "starting group enumeration at index %u\n",
5561 (unsigned int)enum_context));
5563 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5564 "using cached group enumeration at index %u\n",
5565 (unsigned int)enum_context));
5567 num_account = pdb_search_entries(dinfo->disp_info->groups,
5568 enum_context, max_entries,
5573 smb_panic("info class changed");
5579 /* Ensure we cache this enumeration. */
5580 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
5582 DEBUG(10,("_samr_GetDisplayEnumerationIndex: looking for :%s\n",
5583 r->in.name->string));
5585 for (i=0; i<num_account; i++) {
5586 if (strequal(entries[i].account_name, r->in.name->string)) {
5587 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5588 "found %s at idx %d\n",
5589 r->in.name->string, i));
5591 return NT_STATUS_OK;
5595 /* assuming account_name lives at the very end */
5596 *r->out.idx = num_account;
5598 return NT_STATUS_NO_MORE_ENTRIES;
5601 /****************************************************************
5602 _samr_GetDisplayEnumerationIndex2
5603 ****************************************************************/
5605 NTSTATUS _samr_GetDisplayEnumerationIndex2(pipes_struct *p,
5606 struct samr_GetDisplayEnumerationIndex2 *r)
5608 struct samr_GetDisplayEnumerationIndex q;
5610 q.in.domain_handle = r->in.domain_handle;
5611 q.in.level = r->in.level;
5612 q.in.name = r->in.name;
5614 q.out.idx = r->out.idx;
5616 return _samr_GetDisplayEnumerationIndex(p, &q);
5619 /****************************************************************
5620 ****************************************************************/
5622 NTSTATUS _samr_Shutdown(pipes_struct *p,
5623 struct samr_Shutdown *r)
5625 p->rng_fault_state = true;
5626 return NT_STATUS_NOT_IMPLEMENTED;
5629 /****************************************************************
5630 ****************************************************************/
5632 NTSTATUS _samr_SetMemberAttributesOfGroup(pipes_struct *p,
5633 struct samr_SetMemberAttributesOfGroup *r)
5635 p->rng_fault_state = true;
5636 return NT_STATUS_NOT_IMPLEMENTED;
5639 /****************************************************************
5640 ****************************************************************/
5642 NTSTATUS _samr_ChangePasswordUser(pipes_struct *p,
5643 struct samr_ChangePasswordUser *r)
5645 p->rng_fault_state = true;
5646 return NT_STATUS_NOT_IMPLEMENTED;
5649 /****************************************************************
5650 ****************************************************************/
5652 NTSTATUS _samr_TestPrivateFunctionsDomain(pipes_struct *p,
5653 struct samr_TestPrivateFunctionsDomain *r)
5655 p->rng_fault_state = true;
5656 return NT_STATUS_NOT_IMPLEMENTED;
5659 /****************************************************************
5660 ****************************************************************/
5662 NTSTATUS _samr_TestPrivateFunctionsUser(pipes_struct *p,
5663 struct samr_TestPrivateFunctionsUser *r)
5665 p->rng_fault_state = true;
5666 return NT_STATUS_NOT_IMPLEMENTED;
5669 /****************************************************************
5670 ****************************************************************/
5672 NTSTATUS _samr_AddMultipleMembersToAlias(pipes_struct *p,
5673 struct samr_AddMultipleMembersToAlias *r)
5675 p->rng_fault_state = true;
5676 return NT_STATUS_NOT_IMPLEMENTED;
5679 /****************************************************************
5680 ****************************************************************/
5682 NTSTATUS _samr_RemoveMultipleMembersFromAlias(pipes_struct *p,
5683 struct samr_RemoveMultipleMembersFromAlias *r)
5685 p->rng_fault_state = true;
5686 return NT_STATUS_NOT_IMPLEMENTED;
5689 /****************************************************************
5690 ****************************************************************/
5692 NTSTATUS _samr_OemChangePasswordUser2(pipes_struct *p,
5693 struct samr_OemChangePasswordUser2 *r)
5695 p->rng_fault_state = true;
5696 return NT_STATUS_NOT_IMPLEMENTED;
5699 /****************************************************************
5700 ****************************************************************/
5702 NTSTATUS _samr_SetBootKeyInformation(pipes_struct *p,
5703 struct samr_SetBootKeyInformation *r)
5705 p->rng_fault_state = true;
5706 return NT_STATUS_NOT_IMPLEMENTED;
5709 /****************************************************************
5710 ****************************************************************/
5712 NTSTATUS _samr_GetBootKeyInformation(pipes_struct *p,
5713 struct samr_GetBootKeyInformation *r)
5715 p->rng_fault_state = true;
5716 return NT_STATUS_NOT_IMPLEMENTED;
5719 /****************************************************************
5720 ****************************************************************/
5722 NTSTATUS _samr_RidToSid(pipes_struct *p,
5723 struct samr_RidToSid *r)
5725 p->rng_fault_state = true;
5726 return NT_STATUS_NOT_IMPLEMENTED;
5729 /****************************************************************
5730 ****************************************************************/
5732 NTSTATUS _samr_SetDsrmPassword(pipes_struct *p,
5733 struct samr_SetDsrmPassword *r)
5735 p->rng_fault_state = true;
5736 return NT_STATUS_NOT_IMPLEMENTED;
5739 /****************************************************************
5740 ****************************************************************/
5742 NTSTATUS _samr_ValidatePassword(pipes_struct *p,
5743 struct samr_ValidatePassword *r)
5745 p->rng_fault_state = true;
5746 return NT_STATUS_NOT_IMPLEMENTED;