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.
37 #define DBGC_CLASS DBGC_RPC_SRV
39 #define SAMR_USR_RIGHTS_WRITE_PW \
40 ( READ_CONTROL_ACCESS | \
41 SA_RIGHT_USER_CHANGE_PASSWORD | \
42 SA_RIGHT_USER_SET_LOC_COM )
43 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
44 ( READ_CONTROL_ACCESS | SA_RIGHT_USER_SET_LOC_COM )
46 #define DISP_INFO_CACHE_TIMEOUT 10
48 typedef struct disp_info
{
49 DOM_SID sid
; /* identify which domain this is. */
50 bool builtin_domain
; /* Quick flag to check if this is the builtin domain. */
51 struct pdb_search
*users
; /* querydispinfo 1 and 4 */
52 struct pdb_search
*machines
; /* querydispinfo 2 */
53 struct pdb_search
*groups
; /* querydispinfo 3 and 5, enumgroups */
54 struct pdb_search
*aliases
; /* enumaliases */
57 struct pdb_search
*enum_users
; /* enumusers with a mask */
59 struct timed_event
*cache_timeout_event
; /* cache idle timeout
63 /* We keep a static list of these by SID as modern clients close down
64 all resources between each request in a complete enumeration. */
67 /* for use by the \PIPE\samr policy */
69 bool builtin_domain
; /* Quick flag to check if this is the builtin domain. */
70 uint32 status
; /* some sort of flag. best to record it. comes from opnum 0x39 */
76 static const struct generic_mapping sam_generic_mapping
= {
77 GENERIC_RIGHTS_SAM_READ
,
78 GENERIC_RIGHTS_SAM_WRITE
,
79 GENERIC_RIGHTS_SAM_EXECUTE
,
80 GENERIC_RIGHTS_SAM_ALL_ACCESS
};
81 static const struct generic_mapping dom_generic_mapping
= {
82 GENERIC_RIGHTS_DOMAIN_READ
,
83 GENERIC_RIGHTS_DOMAIN_WRITE
,
84 GENERIC_RIGHTS_DOMAIN_EXECUTE
,
85 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS
};
86 static const struct generic_mapping usr_generic_mapping
= {
87 GENERIC_RIGHTS_USER_READ
,
88 GENERIC_RIGHTS_USER_WRITE
,
89 GENERIC_RIGHTS_USER_EXECUTE
,
90 GENERIC_RIGHTS_USER_ALL_ACCESS
};
91 static const struct generic_mapping usr_nopwchange_generic_mapping
= {
92 GENERIC_RIGHTS_USER_READ
,
93 GENERIC_RIGHTS_USER_WRITE
,
94 GENERIC_RIGHTS_USER_EXECUTE
& ~SA_RIGHT_USER_CHANGE_PASSWORD
,
95 GENERIC_RIGHTS_USER_ALL_ACCESS
};
96 static const struct generic_mapping grp_generic_mapping
= {
97 GENERIC_RIGHTS_GROUP_READ
,
98 GENERIC_RIGHTS_GROUP_WRITE
,
99 GENERIC_RIGHTS_GROUP_EXECUTE
,
100 GENERIC_RIGHTS_GROUP_ALL_ACCESS
};
101 static const struct generic_mapping ali_generic_mapping
= {
102 GENERIC_RIGHTS_ALIAS_READ
,
103 GENERIC_RIGHTS_ALIAS_WRITE
,
104 GENERIC_RIGHTS_ALIAS_EXECUTE
,
105 GENERIC_RIGHTS_ALIAS_ALL_ACCESS
};
107 /*******************************************************************
108 *******************************************************************/
110 static NTSTATUS
make_samr_object_sd( TALLOC_CTX
*ctx
, SEC_DESC
**psd
, size_t *sd_size
,
111 const struct generic_mapping
*map
,
112 DOM_SID
*sid
, uint32 sid_access
)
114 DOM_SID domadmin_sid
;
115 SEC_ACE ace
[5]; /* at most 5 entries */
121 /* basic access for Everyone */
123 init_sec_access(&mask
, map
->generic_execute
| map
->generic_read
);
124 init_sec_ace(&ace
[i
++], &global_sid_World
, SEC_ACE_TYPE_ACCESS_ALLOWED
, mask
, 0);
126 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
128 init_sec_access(&mask
, map
->generic_all
);
130 init_sec_ace(&ace
[i
++], &global_sid_Builtin_Administrators
, SEC_ACE_TYPE_ACCESS_ALLOWED
, mask
, 0);
131 init_sec_ace(&ace
[i
++], &global_sid_Builtin_Account_Operators
, SEC_ACE_TYPE_ACCESS_ALLOWED
, mask
, 0);
133 /* Add Full Access for Domain Admins if we are a DC */
136 sid_copy( &domadmin_sid
, get_global_sam_sid() );
137 sid_append_rid( &domadmin_sid
, DOMAIN_GROUP_RID_ADMINS
);
138 init_sec_ace(&ace
[i
++], &domadmin_sid
, SEC_ACE_TYPE_ACCESS_ALLOWED
, mask
, 0);
141 /* if we have a sid, give it some special access */
144 init_sec_access( &mask
, sid_access
);
145 init_sec_ace(&ace
[i
++], sid
, SEC_ACE_TYPE_ACCESS_ALLOWED
, mask
, 0);
148 /* create the security descriptor */
150 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, i
, ace
)) == NULL
)
151 return NT_STATUS_NO_MEMORY
;
153 if ((*psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
154 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
155 psa
, sd_size
)) == NULL
)
156 return NT_STATUS_NO_MEMORY
;
161 /*******************************************************************
162 Checks if access to an object should be granted, and returns that
163 level of access for further checks.
164 ********************************************************************/
166 static NTSTATUS
access_check_samr_object( SEC_DESC
*psd
, NT_USER_TOKEN
*token
,
167 SE_PRIV
*rights
, uint32 rights_mask
,
168 uint32 des_access
, uint32
*acc_granted
,
171 NTSTATUS status
= NT_STATUS_ACCESS_DENIED
;
172 uint32 saved_mask
= 0;
174 /* check privileges; certain SAM access bits should be overridden
175 by privileges (mostly having to do with creating/modifying/deleting
178 if ( rights
&& user_has_any_privilege( token
, rights
) ) {
180 saved_mask
= (des_access
& rights_mask
);
181 des_access
&= ~saved_mask
;
183 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
188 /* check the security descriptor first */
190 if ( se_access_check(psd
, token
, des_access
, acc_granted
, &status
) )
193 /* give root a free pass */
195 if ( geteuid() == sec_initial_uid() ) {
197 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug
, des_access
));
198 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
200 *acc_granted
= des_access
;
202 status
= NT_STATUS_OK
;
208 /* add in any bits saved during the privilege check (only
209 matters is status is ok) */
211 *acc_granted
|= rights_mask
;
213 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
214 debug
, NT_STATUS_IS_OK(status
) ? "GRANTED" : "DENIED",
215 des_access
, *acc_granted
));
220 /*******************************************************************
221 Checks if access to a function can be granted
222 ********************************************************************/
224 static NTSTATUS
access_check_samr_function(uint32 acc_granted
, uint32 acc_required
, const char *debug
)
226 DEBUG(5,("%s: access check ((granted: %#010x; required: %#010x)\n",
227 debug
, acc_granted
, acc_required
));
229 /* check the security descriptor first */
231 if ( (acc_granted
&acc_required
) == acc_required
)
234 /* give root a free pass */
236 if (geteuid() == sec_initial_uid()) {
238 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
239 debug
, acc_granted
, acc_required
));
240 DEBUGADD(4,("but overwritten by euid == 0\n"));
245 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: %#010x)\n",
246 debug
, acc_granted
, acc_required
));
248 return NT_STATUS_ACCESS_DENIED
;
251 /*******************************************************************
252 Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
253 ********************************************************************/
255 static void map_max_allowed_access(const NT_USER_TOKEN
*token
,
256 uint32_t *pacc_requested
)
258 if (!((*pacc_requested
) & MAXIMUM_ALLOWED_ACCESS
)) {
261 *pacc_requested
&= ~MAXIMUM_ALLOWED_ACCESS
;
263 /* At least try for generic read. */
264 *pacc_requested
= GENERIC_READ_ACCESS
;
266 /* root gets anything. */
267 if (geteuid() == sec_initial_uid()) {
268 *pacc_requested
|= GENERIC_ALL_ACCESS
;
272 /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
274 if (is_sid_in_token(token
, &global_sid_Builtin_Administrators
) ||
275 is_sid_in_token(token
, &global_sid_Builtin_Account_Operators
)) {
276 *pacc_requested
|= GENERIC_ALL_ACCESS
;
280 /* Full access for DOMAIN\Domain Admins. */
282 DOM_SID domadmin_sid
;
283 sid_copy( &domadmin_sid
, get_global_sam_sid() );
284 sid_append_rid( &domadmin_sid
, DOMAIN_GROUP_RID_ADMINS
);
285 if (is_sid_in_token(token
, &domadmin_sid
)) {
286 *pacc_requested
|= GENERIC_ALL_ACCESS
;
290 /* TODO ! Check privileges. */
293 /*******************************************************************
294 Fetch or create a dispinfo struct.
295 ********************************************************************/
297 static DISP_INFO
*get_samr_dispinfo_by_sid(DOM_SID
*psid
)
300 * We do a static cache for DISP_INFO's here. Explanation can be found
301 * in Jeremy's checkin message to r11793:
303 * Fix the SAMR cache so it works across completely insane
304 * client behaviour (ie.:
305 * open pipe/open SAMR handle/enumerate 0 - 1024
306 * close SAMR handle, close pipe.
307 * open pipe/open SAMR handle/enumerate 1024 - 2048...
308 * close SAMR handle, close pipe.
309 * And on ad-nausium. Amazing.... probably object-oriented
310 * client side programming in action yet again.
311 * This change should *massively* improve performance when
312 * enumerating users from an LDAP database.
315 * "Our" and the builtin domain are the only ones where we ever
316 * enumerate stuff, so just cache 2 entries.
319 static struct disp_info builtin_dispinfo
;
320 static struct disp_info domain_dispinfo
;
322 /* There are two cases to consider here:
323 1) The SID is a domain SID and we look for an equality match, or
324 2) This is an account SID and so we return the DISP_INFO* for our
331 if (sid_check_is_builtin(psid
) || sid_check_is_in_builtin(psid
)) {
333 * Necessary only once, but it does not really hurt.
335 sid_copy(&builtin_dispinfo
.sid
, &global_sid_Builtin
);
337 return &builtin_dispinfo
;
340 if (sid_check_is_domain(psid
) || sid_check_is_in_our_domain(psid
)) {
342 * Necessary only once, but it does not really hurt.
344 sid_copy(&domain_dispinfo
.sid
, get_global_sam_sid());
346 return &domain_dispinfo
;
352 /*******************************************************************
353 Create a samr_info struct.
354 ********************************************************************/
356 static struct samr_info
*get_samr_info_by_sid(DOM_SID
*psid
)
358 struct samr_info
*info
;
363 sid_to_fstring(sid_str
, psid
);
365 fstrcpy(sid_str
,"(NULL)");
368 mem_ctx
= talloc_init("samr_info for domain sid %s", sid_str
);
370 if ((info
= TALLOC_ZERO_P(mem_ctx
, struct samr_info
)) == NULL
)
373 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str
));
375 sid_copy( &info
->sid
, psid
);
376 info
->builtin_domain
= sid_check_is_builtin(psid
);
378 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
379 info
->builtin_domain
= False
;
381 info
->mem_ctx
= mem_ctx
;
383 info
->disp_info
= get_samr_dispinfo_by_sid(psid
);
388 /*******************************************************************
389 Function to free the per SID data.
390 ********************************************************************/
392 static void free_samr_cache(DISP_INFO
*disp_info
)
394 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
395 sid_string_dbg(&disp_info
->sid
)));
397 /* We need to become root here because the paged search might have to
398 * tell the LDAP server we're not interested in the rest anymore. */
402 if (disp_info
->users
) {
403 DEBUG(10,("free_samr_cache: deleting users cache\n"));
404 pdb_search_destroy(disp_info
->users
);
405 disp_info
->users
= NULL
;
407 if (disp_info
->machines
) {
408 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
409 pdb_search_destroy(disp_info
->machines
);
410 disp_info
->machines
= NULL
;
412 if (disp_info
->groups
) {
413 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
414 pdb_search_destroy(disp_info
->groups
);
415 disp_info
->groups
= NULL
;
417 if (disp_info
->aliases
) {
418 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
419 pdb_search_destroy(disp_info
->aliases
);
420 disp_info
->aliases
= NULL
;
422 if (disp_info
->enum_users
) {
423 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
424 pdb_search_destroy(disp_info
->enum_users
);
425 disp_info
->enum_users
= NULL
;
427 disp_info
->enum_acb_mask
= 0;
432 /*******************************************************************
433 Function to free the per handle data.
434 ********************************************************************/
436 static void free_samr_info(void *ptr
)
438 struct samr_info
*info
=(struct samr_info
*) ptr
;
440 /* Only free the dispinfo cache if no one bothered to set up
443 if (info
->disp_info
&& info
->disp_info
->cache_timeout_event
== NULL
) {
444 free_samr_cache(info
->disp_info
);
447 talloc_destroy(info
->mem_ctx
);
450 /*******************************************************************
451 Idle event handler. Throw away the disp info cache.
452 ********************************************************************/
454 static void disp_info_cache_idle_timeout_handler(struct event_context
*ev_ctx
,
455 struct timed_event
*te
,
456 const struct timeval
*now
,
459 DISP_INFO
*disp_info
= (DISP_INFO
*)private_data
;
461 TALLOC_FREE(disp_info
->cache_timeout_event
);
463 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
465 free_samr_cache(disp_info
);
468 /*******************************************************************
469 Setup cache removal idle event handler.
470 ********************************************************************/
472 static void set_disp_info_cache_timeout(DISP_INFO
*disp_info
, time_t secs_fromnow
)
474 /* Remove any pending timeout and update. */
476 TALLOC_FREE(disp_info
->cache_timeout_event
);
478 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
479 "SID %s for %u seconds\n", sid_string_dbg(&disp_info
->sid
),
480 (unsigned int)secs_fromnow
));
482 disp_info
->cache_timeout_event
= event_add_timed(
483 smbd_event_context(), NULL
,
484 timeval_current_ofs(secs_fromnow
, 0),
485 "disp_info_cache_idle_timeout_handler",
486 disp_info_cache_idle_timeout_handler
, (void *)disp_info
);
489 /*******************************************************************
490 Force flush any cache. We do this on any samr_set_xxx call.
491 We must also remove the timeout handler.
492 ********************************************************************/
494 static void force_flush_samr_cache(DISP_INFO
*disp_info
)
496 if ((disp_info
== NULL
) || (disp_info
->cache_timeout_event
== NULL
)) {
500 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
501 TALLOC_FREE(disp_info
->cache_timeout_event
);
502 free_samr_cache(disp_info
);
505 /*******************************************************************
506 Ensure password info is never given out. Paranioa... JRA.
507 ********************************************************************/
509 static void samr_clear_sam_passwd(struct samu
*sam_pass
)
515 /* These now zero out the old password */
517 pdb_set_lanman_passwd(sam_pass
, NULL
, PDB_DEFAULT
);
518 pdb_set_nt_passwd(sam_pass
, NULL
, PDB_DEFAULT
);
521 static uint32
count_sam_users(struct disp_info
*info
, uint32 acct_flags
)
523 struct samr_displayentry
*entry
;
525 if (info
->builtin_domain
) {
526 /* No users in builtin. */
530 if (info
->users
== NULL
) {
531 info
->users
= pdb_search_users(acct_flags
);
532 if (info
->users
== NULL
) {
536 /* Fetch the last possible entry, thus trigger an enumeration */
537 pdb_search_entries(info
->users
, 0xffffffff, 1, &entry
);
539 /* Ensure we cache this enumeration. */
540 set_disp_info_cache_timeout(info
, DISP_INFO_CACHE_TIMEOUT
);
542 return info
->users
->num_entries
;
545 static uint32
count_sam_groups(struct disp_info
*info
)
547 struct samr_displayentry
*entry
;
549 if (info
->builtin_domain
) {
550 /* No groups in builtin. */
554 if (info
->groups
== NULL
) {
555 info
->groups
= pdb_search_groups();
556 if (info
->groups
== NULL
) {
560 /* Fetch the last possible entry, thus trigger an enumeration */
561 pdb_search_entries(info
->groups
, 0xffffffff, 1, &entry
);
563 /* Ensure we cache this enumeration. */
564 set_disp_info_cache_timeout(info
, DISP_INFO_CACHE_TIMEOUT
);
566 return info
->groups
->num_entries
;
569 static uint32
count_sam_aliases(struct disp_info
*info
)
571 struct samr_displayentry
*entry
;
573 if (info
->aliases
== NULL
) {
574 info
->aliases
= pdb_search_aliases(&info
->sid
);
575 if (info
->aliases
== NULL
) {
579 /* Fetch the last possible entry, thus trigger an enumeration */
580 pdb_search_entries(info
->aliases
, 0xffffffff, 1, &entry
);
582 /* Ensure we cache this enumeration. */
583 set_disp_info_cache_timeout(info
, DISP_INFO_CACHE_TIMEOUT
);
585 return info
->aliases
->num_entries
;
588 /*******************************************************************
590 ********************************************************************/
592 NTSTATUS
_samr_Close(pipes_struct
*p
, struct samr_Close
*r
)
594 if (!close_policy_hnd(p
, r
->in
.handle
)) {
595 return NT_STATUS_INVALID_HANDLE
;
598 ZERO_STRUCTP(r
->out
.handle
);
603 /*******************************************************************
605 ********************************************************************/
607 NTSTATUS
_samr_OpenDomain(pipes_struct
*p
,
608 struct samr_OpenDomain
*r
)
610 struct samr_info
*info
;
611 SEC_DESC
*psd
= NULL
;
613 uint32 des_access
= r
->in
.access_mask
;
618 /* find the connection policy handle. */
620 if ( !find_policy_by_hnd(p
, r
->in
.connect_handle
, (void**)(void *)&info
) )
621 return NT_STATUS_INVALID_HANDLE
;
623 status
= access_check_samr_function(info
->acc_granted
,
624 SA_RIGHT_SAM_OPEN_DOMAIN
,
625 "_samr_OpenDomain" );
627 if ( !NT_STATUS_IS_OK(status
) )
630 /*check if access can be granted as requested by client. */
631 map_max_allowed_access(p
->pipe_user
.nt_user_token
, &des_access
);
633 make_samr_object_sd( p
->mem_ctx
, &psd
, &sd_size
, &dom_generic_mapping
, NULL
, 0 );
634 se_map_generic( &des_access
, &dom_generic_mapping
);
636 se_priv_copy( &se_rights
, &se_machine_account
);
637 se_priv_add( &se_rights
, &se_add_users
);
639 status
= access_check_samr_object( psd
, p
->pipe_user
.nt_user_token
,
640 &se_rights
, GENERIC_RIGHTS_DOMAIN_WRITE
, des_access
,
641 &acc_granted
, "_samr_OpenDomain" );
643 if ( !NT_STATUS_IS_OK(status
) )
646 if (!sid_check_is_domain(r
->in
.sid
) &&
647 !sid_check_is_builtin(r
->in
.sid
)) {
648 return NT_STATUS_NO_SUCH_DOMAIN
;
651 /* associate the domain SID with the (unique) handle. */
652 if ((info
= get_samr_info_by_sid(r
->in
.sid
))==NULL
)
653 return NT_STATUS_NO_MEMORY
;
654 info
->acc_granted
= acc_granted
;
656 /* get a (unique) handle. open a policy on it. */
657 if (!create_policy_hnd(p
, r
->out
.domain_handle
, free_samr_info
, (void *)info
))
658 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
660 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__
));
665 /*******************************************************************
667 ********************************************************************/
669 NTSTATUS
_samr_GetUserPwInfo(pipes_struct
*p
,
670 struct samr_GetUserPwInfo
*r
)
672 struct samr_info
*info
= NULL
;
673 enum lsa_SidType sid_type
;
674 uint32_t min_password_length
= 0;
675 uint32_t password_properties
= 0;
679 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__
));
681 /* find the policy handle. open a policy on it. */
682 if (!find_policy_by_hnd(p
, r
->in
.user_handle
, (void **)(void *)&info
)) {
683 return NT_STATUS_INVALID_HANDLE
;
686 status
= access_check_samr_function(info
->acc_granted
,
687 SAMR_USER_ACCESS_GET_ATTRIBUTES
,
688 "_samr_GetUserPwInfo" );
689 if (!NT_STATUS_IS_OK(status
)) {
693 if (!sid_check_is_in_our_domain(&info
->sid
)) {
694 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
698 ret
= lookup_sid(p
->mem_ctx
, &info
->sid
, NULL
, NULL
, &sid_type
);
701 return NT_STATUS_NO_SUCH_USER
;
707 pdb_get_account_policy(AP_MIN_PASSWORD_LEN
,
708 &min_password_length
);
709 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS
,
710 &password_properties
);
713 if (lp_check_password_script() && *lp_check_password_script()) {
714 password_properties
|= DOMAIN_PASSWORD_COMPLEX
;
722 r
->out
.info
->min_password_length
= min_password_length
;
723 r
->out
.info
->password_properties
= password_properties
;
725 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__
));
730 /*******************************************************************
731 ********************************************************************/
733 static bool get_lsa_policy_samr_sid( pipes_struct
*p
, POLICY_HND
*pol
,
734 DOM_SID
*sid
, uint32
*acc_granted
,
735 DISP_INFO
**ppdisp_info
)
737 struct samr_info
*info
= NULL
;
739 /* find the policy handle. open a policy on it. */
740 if (!find_policy_by_hnd(p
, pol
, (void **)(void *)&info
))
747 *acc_granted
= info
->acc_granted
;
749 *ppdisp_info
= info
->disp_info
;
755 /*******************************************************************
757 ********************************************************************/
759 NTSTATUS
_samr_SetSecurity(pipes_struct
*p
,
760 struct samr_SetSecurity
*r
)
763 uint32 acc_granted
, i
;
766 struct samu
*sampass
=NULL
;
769 if (!get_lsa_policy_samr_sid(p
, r
->in
.handle
, &pol_sid
, &acc_granted
, NULL
))
770 return NT_STATUS_INVALID_HANDLE
;
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
, &pol_sid
);
783 DEBUG(4, ("User %s not found\n", sid_string_dbg(&pol_sid
)));
784 TALLOC_FREE(sampass
);
785 return NT_STATUS_INVALID_HANDLE
;
788 dacl
= r
->in
.sdbuf
->sd
->dacl
;
789 for (i
=0; i
< dacl
->num_aces
; i
++) {
790 if (sid_equal(&pol_sid
, &dacl
->aces
[i
].trustee
)) {
791 ret
= pdb_set_pass_can_change(sampass
,
792 (dacl
->aces
[i
].access_mask
&
793 SA_RIGHT_USER_CHANGE_PASSWORD
) ?
800 TALLOC_FREE(sampass
);
801 return NT_STATUS_ACCESS_DENIED
;
804 status
= access_check_samr_function(acc_granted
,
805 SA_RIGHT_USER_SET_ATTRIBUTES
,
806 "_samr_SetSecurity");
807 if (NT_STATUS_IS_OK(status
)) {
809 status
= pdb_update_sam_account(sampass
);
813 TALLOC_FREE(sampass
);
818 /*******************************************************************
819 build correct perms based on policies and password times for _samr_query_sec_obj
820 *******************************************************************/
821 static bool check_change_pw_access(TALLOC_CTX
*mem_ctx
, DOM_SID
*user_sid
)
823 struct samu
*sampass
=NULL
;
826 if ( !(sampass
= samu_new( mem_ctx
)) ) {
827 DEBUG(0,("No memory!\n"));
832 ret
= pdb_getsampwsid(sampass
, user_sid
);
836 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid
)));
837 TALLOC_FREE(sampass
);
841 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass
) ));
843 if (pdb_get_pass_can_change(sampass
)) {
844 TALLOC_FREE(sampass
);
847 TALLOC_FREE(sampass
);
852 /*******************************************************************
854 ********************************************************************/
856 NTSTATUS
_samr_QuerySecurity(pipes_struct
*p
,
857 struct samr_QuerySecurity
*r
)
861 SEC_DESC
* psd
= NULL
;
866 if (!get_lsa_policy_samr_sid(p
, r
->in
.handle
, &pol_sid
, &acc_granted
, NULL
))
867 return NT_STATUS_INVALID_HANDLE
;
869 DEBUG(10,("_samr_QuerySecurity: querying security on SID: %s\n",
870 sid_string_dbg(&pol_sid
)));
872 /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
874 /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
875 if (pol_sid
.sid_rev_num
== 0) {
876 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
877 status
= make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &sam_generic_mapping
, NULL
, 0);
878 } else if (sid_equal(&pol_sid
,get_global_sam_sid())) {
879 /* check if it is our domain SID */
880 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
881 "with SID: %s\n", sid_string_dbg(&pol_sid
)));
882 status
= make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &dom_generic_mapping
, NULL
, 0);
883 } else if (sid_equal(&pol_sid
,&global_sid_Builtin
)) {
884 /* check if it is the Builtin Domain */
885 /* TODO: Builtin probably needs a different SD with restricted write access*/
886 DEBUG(5,("_samr_QuerySecurity: querying security on Builtin "
887 "Domain with SID: %s\n", sid_string_dbg(&pol_sid
)));
888 status
= make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &dom_generic_mapping
, NULL
, 0);
889 } else if (sid_check_is_in_our_domain(&pol_sid
) ||
890 sid_check_is_in_builtin(&pol_sid
)) {
891 /* TODO: different SDs have to be generated for aliases groups and users.
892 Currently all three get a default user SD */
893 DEBUG(10,("_samr_QuerySecurity: querying security on Object "
894 "with SID: %s\n", sid_string_dbg(&pol_sid
)));
895 if (check_change_pw_access(p
->mem_ctx
, &pol_sid
)) {
896 status
= make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &usr_generic_mapping
,
897 &pol_sid
, SAMR_USR_RIGHTS_WRITE_PW
);
899 status
= make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &usr_nopwchange_generic_mapping
,
900 &pol_sid
, SAMR_USR_RIGHTS_CANT_WRITE_PW
);
903 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
906 if ((*r
->out
.sdbuf
= make_sec_desc_buf(p
->mem_ctx
, sd_size
, psd
)) == NULL
)
907 return NT_STATUS_NO_MEMORY
;
912 /*******************************************************************
913 makes a SAM_ENTRY / UNISTR2* structure from a user list.
914 ********************************************************************/
916 static NTSTATUS
make_user_sam_entry_list(TALLOC_CTX
*ctx
,
917 struct samr_SamEntry
**sam_pp
,
918 uint32_t num_entries
,
920 struct samr_displayentry
*entries
)
923 struct samr_SamEntry
*sam
;
927 if (num_entries
== 0) {
931 sam
= TALLOC_ZERO_ARRAY(ctx
, struct samr_SamEntry
, num_entries
);
933 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
934 return NT_STATUS_NO_MEMORY
;
937 for (i
= 0; i
< num_entries
; i
++) {
940 * usrmgr expects a non-NULL terminated string with
941 * trust relationships
943 if (entries
[i
].acct_flags
& ACB_DOMTRUST
) {
944 init_unistr2(&uni_temp_name
, entries
[i
].account_name
,
947 init_unistr2(&uni_temp_name
, entries
[i
].account_name
,
951 init_lsa_String(&sam
[i
].name
, entries
[i
].account_name
);
952 sam
[i
].idx
= entries
[i
].rid
;
960 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
962 /*******************************************************************
963 _samr_EnumDomainUsers
964 ********************************************************************/
966 NTSTATUS
_samr_EnumDomainUsers(pipes_struct
*p
,
967 struct samr_EnumDomainUsers
*r
)
970 struct samr_info
*info
= NULL
;
972 uint32 enum_context
= *r
->in
.resume_handle
;
973 enum remote_arch_types ra_type
= get_remote_arch();
974 int max_sam_entries
= (ra_type
== RA_WIN95
) ? MAX_SAM_ENTRIES_W95
: MAX_SAM_ENTRIES_W2K
;
975 uint32 max_entries
= max_sam_entries
;
976 struct samr_displayentry
*entries
= NULL
;
977 struct samr_SamArray
*samr_array
= NULL
;
978 struct samr_SamEntry
*samr_entries
= NULL
;
980 /* find the policy handle. open a policy on it. */
981 if (!find_policy_by_hnd(p
, r
->in
.domain_handle
, (void **)(void *)&info
))
982 return NT_STATUS_INVALID_HANDLE
;
984 status
= access_check_samr_function(info
->acc_granted
,
985 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS
,
986 "_samr_EnumDomainUsers");
987 if (!NT_STATUS_IS_OK(status
)) {
991 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__
));
993 if (info
->builtin_domain
) {
994 /* No users in builtin. */
995 *r
->out
.resume_handle
= *r
->in
.resume_handle
;
996 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
1000 samr_array
= TALLOC_ZERO_P(p
->mem_ctx
, struct samr_SamArray
);
1002 return NT_STATUS_NO_MEMORY
;
1009 if ((info
->disp_info
->enum_users
!= NULL
) &&
1010 (info
->disp_info
->enum_acb_mask
!= r
->in
.acct_flags
)) {
1011 pdb_search_destroy(info
->disp_info
->enum_users
);
1012 info
->disp_info
->enum_users
= NULL
;
1015 if (info
->disp_info
->enum_users
== NULL
) {
1016 info
->disp_info
->enum_users
= pdb_search_users(r
->in
.acct_flags
);
1017 info
->disp_info
->enum_acb_mask
= r
->in
.acct_flags
;
1020 if (info
->disp_info
->enum_users
== NULL
) {
1021 /* END AS ROOT !!!! */
1023 return NT_STATUS_ACCESS_DENIED
;
1026 num_account
= pdb_search_entries(info
->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(info
->disp_info
, DISP_INFO_CACHE_TIMEOUT
);
1057 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__
));
1059 samr_array
->count
= num_account
;
1060 samr_array
->entries
= samr_entries
;
1062 *r
->out
.resume_handle
= *r
->in
.resume_handle
+ num_account
;
1063 *r
->out
.sam
= samr_array
;
1064 *r
->out
.num_entries
= num_account
;
1066 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__
));
1071 /*******************************************************************
1072 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1073 ********************************************************************/
1075 static void make_group_sam_entry_list(TALLOC_CTX
*ctx
,
1076 struct samr_SamEntry
**sam_pp
,
1077 uint32_t num_sam_entries
,
1078 struct samr_displayentry
*entries
)
1080 struct samr_SamEntry
*sam
;
1085 if (num_sam_entries
== 0) {
1089 sam
= TALLOC_ZERO_ARRAY(ctx
, struct samr_SamEntry
, num_sam_entries
);
1094 for (i
= 0; i
< num_sam_entries
; i
++) {
1096 * JRA. I think this should include the null. TNG does not.
1098 init_lsa_String(&sam
[i
].name
, entries
[i
].account_name
);
1099 sam
[i
].idx
= entries
[i
].rid
;
1105 /*******************************************************************
1106 _samr_EnumDomainGroups
1107 ********************************************************************/
1109 NTSTATUS
_samr_EnumDomainGroups(pipes_struct
*p
,
1110 struct samr_EnumDomainGroups
*r
)
1113 struct samr_info
*info
= NULL
;
1114 struct samr_displayentry
*groups
;
1116 struct samr_SamArray
*samr_array
= NULL
;
1117 struct samr_SamEntry
*samr_entries
= NULL
;
1119 /* find the policy handle. open a policy on it. */
1120 if (!find_policy_by_hnd(p
, r
->in
.domain_handle
, (void **)(void *)&info
))
1121 return NT_STATUS_INVALID_HANDLE
;
1123 status
= access_check_samr_function(info
->acc_granted
,
1124 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS
,
1125 "_samr_EnumDomainGroups");
1126 if (!NT_STATUS_IS_OK(status
)) {
1130 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__
));
1132 if (info
->builtin_domain
) {
1133 /* No groups in builtin. */
1134 *r
->out
.resume_handle
= *r
->in
.resume_handle
;
1135 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1139 samr_array
= TALLOC_ZERO_P(p
->mem_ctx
, struct samr_SamArray
);
1141 return NT_STATUS_NO_MEMORY
;
1144 /* the domain group array is being allocated in the function below */
1148 if (info
->disp_info
->groups
== NULL
) {
1149 info
->disp_info
->groups
= pdb_search_groups();
1151 if (info
->disp_info
->groups
== NULL
) {
1153 return NT_STATUS_ACCESS_DENIED
;
1157 num_groups
= pdb_search_entries(info
->disp_info
->groups
,
1158 *r
->in
.resume_handle
,
1159 MAX_SAM_ENTRIES
, &groups
);
1162 /* Ensure we cache this enumeration. */
1163 set_disp_info_cache_timeout(info
->disp_info
, DISP_INFO_CACHE_TIMEOUT
);
1165 make_group_sam_entry_list(p
->mem_ctx
, &samr_entries
,
1166 num_groups
, groups
);
1168 samr_array
->count
= num_groups
;
1169 samr_array
->entries
= samr_entries
;
1171 *r
->out
.sam
= samr_array
;
1172 *r
->out
.num_entries
= num_groups
;
1173 /* this was missing, IMHO:
1174 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1177 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__
));
1182 /*******************************************************************
1183 _samr_EnumDomainAliases
1184 ********************************************************************/
1186 NTSTATUS
_samr_EnumDomainAliases(pipes_struct
*p
,
1187 struct samr_EnumDomainAliases
*r
)
1190 struct samr_info
*info
;
1191 struct samr_displayentry
*aliases
;
1192 uint32 num_aliases
= 0;
1193 struct samr_SamArray
*samr_array
= NULL
;
1194 struct samr_SamEntry
*samr_entries
= NULL
;
1196 /* find the policy handle. open a policy on it. */
1197 if (!find_policy_by_hnd(p
, r
->in
.domain_handle
, (void **)(void *)&info
))
1198 return NT_STATUS_INVALID_HANDLE
;
1200 status
= access_check_samr_function(info
->acc_granted
,
1201 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS
,
1202 "_samr_EnumDomainAliases");
1203 if (!NT_STATUS_IS_OK(status
)) {
1207 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1208 sid_string_dbg(&info
->sid
)));
1210 samr_array
= TALLOC_ZERO_P(p
->mem_ctx
, struct samr_SamArray
);
1212 return NT_STATUS_NO_MEMORY
;
1217 if (info
->disp_info
->aliases
== NULL
) {
1218 info
->disp_info
->aliases
= pdb_search_aliases(&info
->sid
);
1219 if (info
->disp_info
->aliases
== NULL
) {
1221 return NT_STATUS_ACCESS_DENIED
;
1225 num_aliases
= pdb_search_entries(info
->disp_info
->aliases
,
1226 *r
->in
.resume_handle
,
1227 MAX_SAM_ENTRIES
, &aliases
);
1230 /* Ensure we cache this enumeration. */
1231 set_disp_info_cache_timeout(info
->disp_info
, DISP_INFO_CACHE_TIMEOUT
);
1233 make_group_sam_entry_list(p
->mem_ctx
, &samr_entries
,
1234 num_aliases
, aliases
);
1236 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__
));
1238 samr_array
->count
= num_aliases
;
1239 samr_array
->entries
= samr_entries
;
1241 *r
->out
.sam
= samr_array
;
1242 *r
->out
.num_entries
= num_aliases
;
1243 *r
->out
.resume_handle
= num_aliases
+ *r
->in
.resume_handle
;
1248 /*******************************************************************
1249 inits a samr_DispInfoGeneral structure.
1250 ********************************************************************/
1252 static NTSTATUS
init_samr_dispinfo_1(TALLOC_CTX
*ctx
,
1253 struct samr_DispInfoGeneral
*r
,
1254 uint32_t num_entries
,
1256 struct samr_displayentry
*entries
)
1260 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries
));
1262 if (num_entries
== 0) {
1263 return NT_STATUS_OK
;
1266 r
->count
= num_entries
;
1268 r
->entries
= TALLOC_ZERO_ARRAY(ctx
, struct samr_DispEntryGeneral
, num_entries
);
1270 return NT_STATUS_NO_MEMORY
;
1273 for (i
= 0; i
< num_entries
; i
++) {
1275 init_lsa_String(&r
->entries
[i
].account_name
,
1276 entries
[i
].account_name
);
1278 init_lsa_String(&r
->entries
[i
].description
,
1279 entries
[i
].description
);
1281 init_lsa_String(&r
->entries
[i
].full_name
,
1282 entries
[i
].fullname
);
1284 r
->entries
[i
].rid
= entries
[i
].rid
;
1285 r
->entries
[i
].acct_flags
= entries
[i
].acct_flags
;
1286 r
->entries
[i
].idx
= start_idx
+i
+1;
1289 return NT_STATUS_OK
;
1292 /*******************************************************************
1293 inits a samr_DispInfoFull structure.
1294 ********************************************************************/
1296 static NTSTATUS
init_samr_dispinfo_2(TALLOC_CTX
*ctx
,
1297 struct samr_DispInfoFull
*r
,
1298 uint32_t num_entries
,
1300 struct samr_displayentry
*entries
)
1304 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries
));
1306 if (num_entries
== 0) {
1307 return NT_STATUS_OK
;
1310 r
->count
= num_entries
;
1312 r
->entries
= TALLOC_ZERO_ARRAY(ctx
, struct samr_DispEntryFull
, num_entries
);
1314 return NT_STATUS_NO_MEMORY
;
1317 for (i
= 0; i
< num_entries
; i
++) {
1319 init_lsa_String(&r
->entries
[i
].account_name
,
1320 entries
[i
].account_name
);
1322 init_lsa_String(&r
->entries
[i
].description
,
1323 entries
[i
].description
);
1325 r
->entries
[i
].rid
= entries
[i
].rid
;
1326 r
->entries
[i
].acct_flags
= entries
[i
].acct_flags
;
1327 r
->entries
[i
].idx
= start_idx
+i
+1;
1330 return NT_STATUS_OK
;
1333 /*******************************************************************
1334 inits a samr_DispInfoFullGroups structure.
1335 ********************************************************************/
1337 static NTSTATUS
init_samr_dispinfo_3(TALLOC_CTX
*ctx
,
1338 struct samr_DispInfoFullGroups
*r
,
1339 uint32_t num_entries
,
1341 struct samr_displayentry
*entries
)
1345 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries
));
1347 if (num_entries
== 0) {
1348 return NT_STATUS_OK
;
1351 r
->count
= num_entries
;
1353 r
->entries
= TALLOC_ZERO_ARRAY(ctx
, struct samr_DispEntryFullGroup
, num_entries
);
1355 return NT_STATUS_NO_MEMORY
;
1358 for (i
= 0; i
< num_entries
; i
++) {
1360 init_lsa_String(&r
->entries
[i
].account_name
,
1361 entries
[i
].account_name
);
1363 init_lsa_String(&r
->entries
[i
].description
,
1364 entries
[i
].description
);
1366 r
->entries
[i
].rid
= entries
[i
].rid
;
1367 r
->entries
[i
].acct_flags
= entries
[i
].acct_flags
;
1368 r
->entries
[i
].idx
= start_idx
+i
+1;
1371 return NT_STATUS_OK
;
1374 /*******************************************************************
1375 inits a samr_DispInfoAscii structure.
1376 ********************************************************************/
1378 static NTSTATUS
init_samr_dispinfo_4(TALLOC_CTX
*ctx
,
1379 struct samr_DispInfoAscii
*r
,
1380 uint32_t num_entries
,
1382 struct samr_displayentry
*entries
)
1386 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries
));
1388 if (num_entries
== 0) {
1389 return NT_STATUS_OK
;
1392 r
->count
= num_entries
;
1394 r
->entries
= TALLOC_ZERO_ARRAY(ctx
, struct samr_DispEntryAscii
, num_entries
);
1396 return NT_STATUS_NO_MEMORY
;
1399 for (i
= 0; i
< num_entries
; i
++) {
1401 init_lsa_AsciiStringLarge(&r
->entries
[i
].account_name
,
1402 entries
[i
].account_name
);
1404 r
->entries
[i
].idx
= start_idx
+i
+1;
1407 return NT_STATUS_OK
;
1410 /*******************************************************************
1411 inits a samr_DispInfoAscii structure.
1412 ********************************************************************/
1414 static NTSTATUS
init_samr_dispinfo_5(TALLOC_CTX
*ctx
,
1415 struct samr_DispInfoAscii
*r
,
1416 uint32_t num_entries
,
1418 struct samr_displayentry
*entries
)
1422 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries
));
1424 if (num_entries
== 0) {
1425 return NT_STATUS_OK
;
1428 r
->count
= num_entries
;
1430 r
->entries
= TALLOC_ZERO_ARRAY(ctx
, struct samr_DispEntryAscii
, num_entries
);
1432 return NT_STATUS_NO_MEMORY
;
1435 for (i
= 0; i
< num_entries
; i
++) {
1437 init_lsa_AsciiStringLarge(&r
->entries
[i
].account_name
,
1438 entries
[i
].account_name
);
1440 r
->entries
[i
].idx
= start_idx
+i
+1;
1443 return NT_STATUS_OK
;
1446 /*******************************************************************
1447 _samr_QueryDisplayInfo
1448 ********************************************************************/
1450 NTSTATUS
_samr_QueryDisplayInfo(pipes_struct
*p
,
1451 struct samr_QueryDisplayInfo
*r
)
1454 struct samr_info
*info
= NULL
;
1455 uint32 struct_size
=0x20; /* W2K always reply that, client doesn't care */
1457 uint32 max_entries
= r
->in
.max_entries
;
1458 uint32 enum_context
= r
->in
.start_idx
;
1459 uint32 max_size
= r
->in
.buf_size
;
1461 union samr_DispInfo
*disp_info
= r
->out
.info
;
1463 uint32 temp_size
=0, total_data_size
=0;
1464 NTSTATUS disp_ret
= NT_STATUS_UNSUCCESSFUL
;
1465 uint32 num_account
= 0;
1466 enum remote_arch_types ra_type
= get_remote_arch();
1467 int max_sam_entries
= (ra_type
== RA_WIN95
) ? MAX_SAM_ENTRIES_W95
: MAX_SAM_ENTRIES_W2K
;
1468 struct samr_displayentry
*entries
= NULL
;
1470 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__
));
1472 /* find the policy handle. open a policy on it. */
1473 if (!find_policy_by_hnd(p
, r
->in
.domain_handle
, (void **)(void *)&info
))
1474 return NT_STATUS_INVALID_HANDLE
;
1477 * calculate how many entries we will return.
1479 * - the number of entries the client asked
1480 * - our limit on that
1481 * - the starting point (enumeration context)
1482 * - the buffer size the client will accept
1486 * We are a lot more like W2K. Instead of reading the SAM
1487 * each time to find the records we need to send back,
1488 * we read it once and link that copy to the sam handle.
1489 * For large user list (over the MAX_SAM_ENTRIES)
1490 * it's a definitive win.
1491 * second point to notice: between enumerations
1492 * our sam is now the same as it's a snapshoot.
1493 * third point: got rid of the static SAM_USER_21 struct
1494 * no more intermediate.
1495 * con: it uses much more memory, as a full copy is stored
1498 * If you want to change it, think twice and think
1499 * of the second point , that's really important.
1504 if ((r
->in
.level
< 1) || (r
->in
.level
> 5)) {
1505 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1506 (unsigned int)r
->in
.level
));
1507 return NT_STATUS_INVALID_INFO_CLASS
;
1510 /* first limit the number of entries we will return */
1511 if(max_entries
> max_sam_entries
) {
1512 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1513 "entries, limiting to %d\n", max_entries
,
1515 max_entries
= max_sam_entries
;
1518 /* calculate the size and limit on the number of entries we will
1521 temp_size
=max_entries
*struct_size
;
1523 if (temp_size
>max_size
) {
1524 max_entries
=MIN((max_size
/struct_size
),max_entries
);;
1525 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1526 "only %d entries\n", max_entries
));
1531 /* THe following done as ROOT. Don't return without unbecome_root(). */
1533 switch (r
->in
.level
) {
1536 if (info
->disp_info
->users
== NULL
) {
1537 info
->disp_info
->users
= pdb_search_users(ACB_NORMAL
);
1538 if (info
->disp_info
->users
== NULL
) {
1540 return NT_STATUS_ACCESS_DENIED
;
1542 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1543 (unsigned int)enum_context
));
1545 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1546 (unsigned int)enum_context
));
1549 num_account
= pdb_search_entries(info
->disp_info
->users
,
1550 enum_context
, max_entries
,
1554 if (info
->disp_info
->machines
== NULL
) {
1555 info
->disp_info
->machines
=
1556 pdb_search_users(ACB_WSTRUST
|ACB_SVRTRUST
);
1557 if (info
->disp_info
->machines
== NULL
) {
1559 return NT_STATUS_ACCESS_DENIED
;
1561 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1562 (unsigned int)enum_context
));
1564 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1565 (unsigned int)enum_context
));
1568 num_account
= pdb_search_entries(info
->disp_info
->machines
,
1569 enum_context
, max_entries
,
1574 if (info
->disp_info
->groups
== NULL
) {
1575 info
->disp_info
->groups
= pdb_search_groups();
1576 if (info
->disp_info
->groups
== NULL
) {
1578 return NT_STATUS_ACCESS_DENIED
;
1580 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1581 (unsigned int)enum_context
));
1583 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1584 (unsigned int)enum_context
));
1587 num_account
= pdb_search_entries(info
->disp_info
->groups
,
1588 enum_context
, max_entries
,
1593 smb_panic("info class changed");
1599 /* Now create reply structure */
1600 switch (r
->in
.level
) {
1602 disp_ret
= init_samr_dispinfo_1(p
->mem_ctx
, &disp_info
->info1
,
1603 num_account
, enum_context
,
1607 disp_ret
= init_samr_dispinfo_2(p
->mem_ctx
, &disp_info
->info2
,
1608 num_account
, enum_context
,
1612 disp_ret
= init_samr_dispinfo_3(p
->mem_ctx
, &disp_info
->info3
,
1613 num_account
, enum_context
,
1617 disp_ret
= init_samr_dispinfo_4(p
->mem_ctx
, &disp_info
->info4
,
1618 num_account
, enum_context
,
1622 disp_ret
= init_samr_dispinfo_5(p
->mem_ctx
, &disp_info
->info5
,
1623 num_account
, enum_context
,
1627 smb_panic("info class changed");
1631 if (!NT_STATUS_IS_OK(disp_ret
))
1634 /* calculate the total size */
1635 total_data_size
=num_account
*struct_size
;
1638 status
= STATUS_MORE_ENTRIES
;
1640 status
= NT_STATUS_OK
;
1643 /* Ensure we cache this enumeration. */
1644 set_disp_info_cache_timeout(info
->disp_info
, DISP_INFO_CACHE_TIMEOUT
);
1646 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__
));
1648 *r
->out
.total_size
= total_data_size
;
1649 *r
->out
.returned_size
= temp_size
;
1654 /****************************************************************
1655 _samr_QueryDisplayInfo2
1656 ****************************************************************/
1658 NTSTATUS
_samr_QueryDisplayInfo2(pipes_struct
*p
,
1659 struct samr_QueryDisplayInfo2
*r
)
1661 struct samr_QueryDisplayInfo q
;
1663 q
.in
.domain_handle
= r
->in
.domain_handle
;
1664 q
.in
.level
= r
->in
.level
;
1665 q
.in
.start_idx
= r
->in
.start_idx
;
1666 q
.in
.max_entries
= r
->in
.max_entries
;
1667 q
.in
.buf_size
= r
->in
.buf_size
;
1669 q
.out
.total_size
= r
->out
.total_size
;
1670 q
.out
.returned_size
= r
->out
.returned_size
;
1671 q
.out
.info
= r
->out
.info
;
1673 return _samr_QueryDisplayInfo(p
, &q
);
1676 /****************************************************************
1677 _samr_QueryDisplayInfo3
1678 ****************************************************************/
1680 NTSTATUS
_samr_QueryDisplayInfo3(pipes_struct
*p
,
1681 struct samr_QueryDisplayInfo3
*r
)
1683 struct samr_QueryDisplayInfo q
;
1685 q
.in
.domain_handle
= r
->in
.domain_handle
;
1686 q
.in
.level
= r
->in
.level
;
1687 q
.in
.start_idx
= r
->in
.start_idx
;
1688 q
.in
.max_entries
= r
->in
.max_entries
;
1689 q
.in
.buf_size
= r
->in
.buf_size
;
1691 q
.out
.total_size
= r
->out
.total_size
;
1692 q
.out
.returned_size
= r
->out
.returned_size
;
1693 q
.out
.info
= r
->out
.info
;
1695 return _samr_QueryDisplayInfo(p
, &q
);
1698 /*******************************************************************
1699 _samr_QueryAliasInfo
1700 ********************************************************************/
1702 NTSTATUS
_samr_QueryAliasInfo(pipes_struct
*p
,
1703 struct samr_QueryAliasInfo
*r
)
1706 struct acct_info info
;
1709 union samr_AliasInfo
*alias_info
= NULL
;
1710 const char *alias_name
= NULL
;
1711 const char *alias_description
= NULL
;
1713 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__
));
1715 alias_info
= TALLOC_ZERO_P(p
->mem_ctx
, union samr_AliasInfo
);
1717 return NT_STATUS_NO_MEMORY
;
1720 /* find the policy handle. open a policy on it. */
1721 if (!get_lsa_policy_samr_sid(p
, r
->in
.alias_handle
, &sid
, &acc_granted
, NULL
))
1722 return NT_STATUS_INVALID_HANDLE
;
1724 status
= access_check_samr_function(acc_granted
,
1725 SA_RIGHT_ALIAS_LOOKUP_INFO
,
1726 "_samr_QueryAliasInfo");
1727 if (!NT_STATUS_IS_OK(status
)) {
1732 status
= pdb_get_aliasinfo(&sid
, &info
);
1735 if ( !NT_STATUS_IS_OK(status
))
1738 /* FIXME: info contains fstrings */
1739 alias_name
= talloc_strdup(r
, info
.acct_name
);
1740 alias_description
= talloc_strdup(r
, info
.acct_desc
);
1742 switch (r
->in
.level
) {
1744 init_samr_alias_info1(&alias_info
->all
,
1749 case ALIASINFODESCRIPTION
:
1750 init_samr_alias_info3(&alias_info
->description
,
1754 return NT_STATUS_INVALID_INFO_CLASS
;
1757 *r
->out
.info
= alias_info
;
1759 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__
));
1761 return NT_STATUS_OK
;
1765 /*******************************************************************
1766 samr_reply_lookup_ids
1767 ********************************************************************/
1769 uint32
_samr_lookup_ids(pipes_struct
*p
, SAMR_Q_LOOKUP_IDS
*q_u
, SAMR_R_LOOKUP_IDS
*r_u
)
1771 uint32 rid
[MAX_SAM_ENTRIES
];
1772 int num_rids
= q_u
->num_sids1
;
1774 r_u
->status
= NT_STATUS_OK
;
1776 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__
));
1778 if (num_rids
> MAX_SAM_ENTRIES
) {
1779 num_rids
= MAX_SAM_ENTRIES
;
1780 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids
));
1785 SMB_ASSERT_ARRAY(q_u
->uni_user_name
, num_rids
);
1787 for (i
= 0; i
< num_rids
&& status
== 0; i
++)
1789 struct sam_passwd
*sam_pass
;
1793 fstrcpy(user_name
, unistrn2(q_u
->uni_user_name
[i
].buffer
,
1794 q_u
->uni_user_name
[i
].uni_str_len
));
1796 /* find the user account */
1798 sam_pass
= get_smb21pwd_entry(user_name
, 0);
1801 if (sam_pass
== NULL
)
1803 status
= 0xC0000000 | NT_STATUS_NO_SUCH_USER
;
1808 rid
[i
] = sam_pass
->user_rid
;
1814 rid
[0] = BUILTIN_ALIAS_RID_USERS
;
1816 init_samr_r_lookup_ids(&r_u
, num_rids
, rid
, NT_STATUS_OK
);
1818 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__
));
1824 /*******************************************************************
1826 ********************************************************************/
1828 NTSTATUS
_samr_LookupNames(pipes_struct
*p
,
1829 struct samr_LookupNames
*r
)
1833 enum lsa_SidType
*type
;
1835 int num_rids
= r
->in
.num_names
;
1838 struct samr_Ids rids
, types
;
1840 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__
));
1842 if (!get_lsa_policy_samr_sid(p
, r
->in
.domain_handle
, &pol_sid
, &acc_granted
, NULL
)) {
1843 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
1846 status
= access_check_samr_function(acc_granted
,
1847 0, /* Don't know the acc_bits yet */
1848 "_samr_LookupNames");
1849 if (!NT_STATUS_IS_OK(status
)) {
1853 if (num_rids
> MAX_SAM_ENTRIES
) {
1854 num_rids
= MAX_SAM_ENTRIES
;
1855 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids
));
1858 rid
= talloc_array(p
->mem_ctx
, uint32
, num_rids
);
1859 NT_STATUS_HAVE_NO_MEMORY(rid
);
1861 type
= talloc_array(p
->mem_ctx
, enum lsa_SidType
, num_rids
);
1862 NT_STATUS_HAVE_NO_MEMORY(type
);
1864 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1865 sid_string_dbg(&pol_sid
)));
1867 for (i
= 0; i
< num_rids
; i
++) {
1869 status
= NT_STATUS_NONE_MAPPED
;
1870 type
[i
] = SID_NAME_UNKNOWN
;
1872 rid
[i
] = 0xffffffff;
1874 if (sid_check_is_builtin(&pol_sid
)) {
1875 if (lookup_builtin_name(r
->in
.names
[i
].string
,
1878 type
[i
] = SID_NAME_ALIAS
;
1881 lookup_global_sam_name(r
->in
.names
[i
].string
, 0,
1885 if (type
[i
] != SID_NAME_UNKNOWN
) {
1886 status
= NT_STATUS_OK
;
1890 rids
.count
= num_rids
;
1893 types
.count
= num_rids
;
1896 *r
->out
.rids
= rids
;
1897 *r
->out
.types
= types
;
1899 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__
));
1904 /*******************************************************************
1905 _samr_ChangePasswordUser2
1906 ********************************************************************/
1908 NTSTATUS
_samr_ChangePasswordUser2(pipes_struct
*p
,
1909 struct samr_ChangePasswordUser2
*r
)
1915 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__
));
1917 fstrcpy(user_name
, r
->in
.account
->string
);
1918 fstrcpy(wks
, r
->in
.server
->string
);
1920 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name
, wks
));
1923 * Pass the user through the NT -> unix user mapping
1927 (void)map_username(user_name
);
1930 * UNIX username case mangling not required, pass_oem_change
1931 * is case insensitive.
1934 status
= pass_oem_change(user_name
,
1935 r
->in
.lm_password
->data
,
1936 r
->in
.lm_verifier
->hash
,
1937 r
->in
.nt_password
->data
,
1938 r
->in
.nt_verifier
->hash
,
1941 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__
));
1946 /*******************************************************************
1947 _samr_ChangePasswordUser3
1948 ********************************************************************/
1950 NTSTATUS
_samr_ChangePasswordUser3(pipes_struct
*p
,
1951 struct samr_ChangePasswordUser3
*r
)
1955 const char *wks
= NULL
;
1956 uint32 reject_reason
;
1957 struct samr_DomInfo1
*dominfo
= NULL
;
1958 struct samr_ChangeReject
*reject
= NULL
;
1960 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__
));
1962 fstrcpy(user_name
, r
->in
.account
->string
);
1963 if (r
->in
.server
&& r
->in
.server
->string
) {
1964 wks
= r
->in
.server
->string
;
1967 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name
, wks
));
1970 * Pass the user through the NT -> unix user mapping
1974 (void)map_username(user_name
);
1977 * UNIX username case mangling not required, pass_oem_change
1978 * is case insensitive.
1981 status
= pass_oem_change(user_name
,
1982 r
->in
.lm_password
->data
,
1983 r
->in
.lm_verifier
->hash
,
1984 r
->in
.nt_password
->data
,
1985 r
->in
.nt_verifier
->hash
,
1988 if (NT_STATUS_EQUAL(status
, NT_STATUS_PASSWORD_RESTRICTION
) ||
1989 NT_STATUS_EQUAL(status
, NT_STATUS_ACCOUNT_RESTRICTION
)) {
1991 uint32 min_pass_len
,pass_hist
,password_properties
;
1992 time_t u_expire
, u_min_age
;
1993 NTTIME nt_expire
, nt_min_age
;
1994 uint32 account_policy_temp
;
1996 dominfo
= TALLOC_ZERO_P(p
->mem_ctx
, struct samr_DomInfo1
);
1998 return NT_STATUS_NO_MEMORY
;
2001 reject
= TALLOC_ZERO_P(p
->mem_ctx
, struct samr_ChangeReject
);
2003 return NT_STATUS_NO_MEMORY
;
2010 pdb_get_account_policy(AP_MIN_PASSWORD_LEN
, &account_policy_temp
);
2011 min_pass_len
= account_policy_temp
;
2013 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &account_policy_temp
);
2014 pass_hist
= account_policy_temp
;
2016 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS
, &account_policy_temp
);
2017 password_properties
= account_policy_temp
;
2019 pdb_get_account_policy(AP_MAX_PASSWORD_AGE
, &account_policy_temp
);
2020 u_expire
= account_policy_temp
;
2022 pdb_get_account_policy(AP_MIN_PASSWORD_AGE
, &account_policy_temp
);
2023 u_min_age
= account_policy_temp
;
2029 unix_to_nt_time_abs(&nt_expire
, u_expire
);
2030 unix_to_nt_time_abs(&nt_min_age
, u_min_age
);
2032 if (lp_check_password_script() && *lp_check_password_script()) {
2033 password_properties
|= DOMAIN_PASSWORD_COMPLEX
;
2036 init_samr_DomInfo1(dominfo
,
2039 password_properties
,
2043 reject
->reason
= reject_reason
;
2045 *r
->out
.dominfo
= dominfo
;
2046 *r
->out
.reject
= reject
;
2049 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__
));
2054 /*******************************************************************
2055 makes a SAMR_R_LOOKUP_RIDS structure.
2056 ********************************************************************/
2058 static bool make_samr_lookup_rids(TALLOC_CTX
*ctx
, uint32 num_names
,
2060 struct lsa_String
**lsa_name_array_p
)
2062 struct lsa_String
*lsa_name_array
= NULL
;
2065 *lsa_name_array_p
= NULL
;
2067 if (num_names
!= 0) {
2068 lsa_name_array
= TALLOC_ZERO_ARRAY(ctx
, struct lsa_String
, num_names
);
2069 if (!lsa_name_array
) {
2074 for (i
= 0; i
< num_names
; i
++) {
2075 DEBUG(10, ("names[%d]:%s\n", i
, names
[i
] && *names
[i
] ? names
[i
] : ""));
2076 init_lsa_String(&lsa_name_array
[i
], names
[i
]);
2079 *lsa_name_array_p
= lsa_name_array
;
2084 /*******************************************************************
2086 ********************************************************************/
2088 NTSTATUS
_samr_LookupRids(pipes_struct
*p
,
2089 struct samr_LookupRids
*r
)
2093 enum lsa_SidType
*attrs
= NULL
;
2094 uint32
*wire_attrs
= NULL
;
2096 int num_rids
= (int)r
->in
.num_rids
;
2099 struct lsa_Strings names_array
;
2100 struct samr_Ids types_array
;
2101 struct lsa_String
*lsa_names
= NULL
;
2103 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__
));
2105 /* find the policy handle. open a policy on it. */
2106 if (!get_lsa_policy_samr_sid(p
, r
->in
.domain_handle
, &pol_sid
, &acc_granted
, NULL
))
2107 return NT_STATUS_INVALID_HANDLE
;
2109 if (num_rids
> 1000) {
2110 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2111 "to samba4 idl this is not possible\n", num_rids
));
2112 return NT_STATUS_UNSUCCESSFUL
;
2116 names
= TALLOC_ZERO_ARRAY(p
->mem_ctx
, const char *, num_rids
);
2117 attrs
= TALLOC_ZERO_ARRAY(p
->mem_ctx
, enum lsa_SidType
, num_rids
);
2118 wire_attrs
= TALLOC_ZERO_ARRAY(p
->mem_ctx
, uint32
, num_rids
);
2120 if ((names
== NULL
) || (attrs
== NULL
) || (wire_attrs
==NULL
))
2121 return NT_STATUS_NO_MEMORY
;
2128 become_root(); /* lookup_sid can require root privs */
2129 status
= pdb_lookup_rids(&pol_sid
, num_rids
, r
->in
.rids
,
2133 if (NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
) && (num_rids
== 0)) {
2134 status
= NT_STATUS_OK
;
2137 if (!make_samr_lookup_rids(p
->mem_ctx
, num_rids
, names
,
2139 return NT_STATUS_NO_MEMORY
;
2142 /* Convert from enum lsa_SidType to uint32 for wire format. */
2143 for (i
= 0; i
< num_rids
; i
++) {
2144 wire_attrs
[i
] = (uint32
)attrs
[i
];
2147 names_array
.count
= num_rids
;
2148 names_array
.names
= lsa_names
;
2150 types_array
.count
= num_rids
;
2151 types_array
.ids
= wire_attrs
;
2153 *r
->out
.names
= names_array
;
2154 *r
->out
.types
= types_array
;
2156 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__
));
2161 /*******************************************************************
2163 ********************************************************************/
2165 NTSTATUS
_samr_OpenUser(pipes_struct
*p
,
2166 struct samr_OpenUser
*r
)
2168 struct samu
*sampass
=NULL
;
2170 POLICY_HND domain_pol
= *r
->in
.domain_handle
;
2171 POLICY_HND
*user_pol
= r
->out
.user_handle
;
2172 struct samr_info
*info
= NULL
;
2173 SEC_DESC
*psd
= NULL
;
2175 uint32 des_access
= r
->in
.access_mask
;
2181 /* find the domain policy handle and get domain SID / access bits in the domain policy. */
2183 if ( !get_lsa_policy_samr_sid(p
, &domain_pol
, &sid
, &acc_granted
, NULL
) )
2184 return NT_STATUS_INVALID_HANDLE
;
2186 nt_status
= access_check_samr_function(acc_granted
,
2187 SA_RIGHT_DOMAIN_OPEN_ACCOUNT
,
2190 if ( !NT_STATUS_IS_OK(nt_status
) )
2193 if ( !(sampass
= samu_new( p
->mem_ctx
)) ) {
2194 return NT_STATUS_NO_MEMORY
;
2197 /* append the user's RID to it */
2199 if (!sid_append_rid(&sid
, r
->in
.rid
))
2200 return NT_STATUS_NO_SUCH_USER
;
2202 /* check if access can be granted as requested by client. */
2204 map_max_allowed_access(p
->pipe_user
.nt_user_token
, &des_access
);
2206 make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &usr_generic_mapping
, &sid
, SAMR_USR_RIGHTS_WRITE_PW
);
2207 se_map_generic(&des_access
, &usr_generic_mapping
);
2209 se_priv_copy( &se_rights
, &se_machine_account
);
2210 se_priv_add( &se_rights
, &se_add_users
);
2212 nt_status
= access_check_samr_object(psd
, p
->pipe_user
.nt_user_token
,
2213 &se_rights
, GENERIC_RIGHTS_USER_WRITE
, des_access
,
2214 &acc_granted
, "_samr_OpenUser");
2216 if ( !NT_STATUS_IS_OK(nt_status
) )
2220 ret
=pdb_getsampwsid(sampass
, &sid
);
2223 /* check that the SID exists in our domain. */
2225 return NT_STATUS_NO_SUCH_USER
;
2228 TALLOC_FREE(sampass
);
2230 /* associate the user's SID and access bits with the new handle. */
2231 if ((info
= get_samr_info_by_sid(&sid
)) == NULL
)
2232 return NT_STATUS_NO_MEMORY
;
2233 info
->acc_granted
= acc_granted
;
2235 /* get a (unique) handle. open a policy on it. */
2236 if (!create_policy_hnd(p
, user_pol
, free_samr_info
, (void *)info
))
2237 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2239 return NT_STATUS_OK
;
2242 /*************************************************************************
2243 *************************************************************************/
2245 static NTSTATUS
init_samr_parameters_string(TALLOC_CTX
*mem_ctx
,
2247 struct lsa_BinaryString
**_r
)
2249 struct lsa_BinaryString
*r
;
2252 return NT_STATUS_INVALID_PARAMETER
;
2255 r
= TALLOC_ZERO_P(mem_ctx
, struct lsa_BinaryString
);
2257 return NT_STATUS_NO_MEMORY
;
2260 r
->array
= TALLOC_ZERO_ARRAY(mem_ctx
, uint16_t, blob
->length
/2);
2262 return NT_STATUS_NO_MEMORY
;
2264 memcpy(r
->array
, blob
->data
, blob
->length
);
2265 r
->size
= blob
->length
;
2266 r
->length
= blob
->length
;
2269 return NT_STATUS_NO_MEMORY
;
2274 return NT_STATUS_OK
;
2277 /*************************************************************************
2278 get_user_info_7. Safe. Only gives out account_name.
2279 *************************************************************************/
2281 static NTSTATUS
get_user_info_7(TALLOC_CTX
*mem_ctx
,
2282 struct samr_UserInfo7
*r
,
2285 struct samu
*smbpass
=NULL
;
2287 const char *account_name
= NULL
;
2291 if ( !(smbpass
= samu_new( mem_ctx
)) ) {
2292 return NT_STATUS_NO_MEMORY
;
2296 ret
= pdb_getsampwsid(smbpass
, user_sid
);
2300 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid
)));
2301 return NT_STATUS_NO_SUCH_USER
;
2304 account_name
= talloc_strdup(mem_ctx
, pdb_get_username(smbpass
));
2305 if (!account_name
) {
2306 TALLOC_FREE(smbpass
);
2307 return NT_STATUS_NO_MEMORY
;
2309 TALLOC_FREE(smbpass
);
2311 DEBUG(3,("User:[%s]\n", account_name
));
2313 init_samr_user_info7(r
, account_name
);
2315 return NT_STATUS_OK
;
2318 /*************************************************************************
2319 get_user_info_9. Only gives out primary group SID.
2320 *************************************************************************/
2322 static NTSTATUS
get_user_info_9(TALLOC_CTX
*mem_ctx
,
2323 struct samr_UserInfo9
*r
,
2326 struct samu
*smbpass
=NULL
;
2331 if ( !(smbpass
= samu_new( mem_ctx
)) ) {
2332 return NT_STATUS_NO_MEMORY
;
2336 ret
= pdb_getsampwsid(smbpass
, user_sid
);
2340 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid
)));
2341 TALLOC_FREE(smbpass
);
2342 return NT_STATUS_NO_SUCH_USER
;
2345 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass
) ));
2347 init_samr_user_info9(r
, pdb_get_group_rid(smbpass
));
2349 TALLOC_FREE(smbpass
);
2351 return NT_STATUS_OK
;
2354 /*************************************************************************
2355 get_user_info_16. Safe. Only gives out acb bits.
2356 *************************************************************************/
2358 static NTSTATUS
get_user_info_16(TALLOC_CTX
*mem_ctx
,
2359 struct samr_UserInfo16
*r
,
2362 struct samu
*smbpass
=NULL
;
2367 if ( !(smbpass
= samu_new( mem_ctx
)) ) {
2368 return NT_STATUS_NO_MEMORY
;
2372 ret
= pdb_getsampwsid(smbpass
, user_sid
);
2376 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid
)));
2377 TALLOC_FREE(smbpass
);
2378 return NT_STATUS_NO_SUCH_USER
;
2381 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass
) ));
2383 init_samr_user_info16(r
, pdb_get_acct_ctrl(smbpass
));
2385 TALLOC_FREE(smbpass
);
2387 return NT_STATUS_OK
;
2390 /*************************************************************************
2391 get_user_info_18. OK - this is the killer as it gives out password info.
2392 Ensure that this is only allowed on an encrypted connection with a root
2394 *************************************************************************/
2396 static NTSTATUS
get_user_info_18(pipes_struct
*p
,
2397 TALLOC_CTX
*mem_ctx
,
2398 struct samr_UserInfo18
*r
,
2401 struct samu
*smbpass
=NULL
;
2406 if (p
->auth
.auth_type
!= PIPE_AUTH_TYPE_NTLMSSP
|| p
->auth
.auth_type
!= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
) {
2407 return NT_STATUS_ACCESS_DENIED
;
2410 if (p
->auth
.auth_level
!= PIPE_AUTH_LEVEL_PRIVACY
) {
2411 return NT_STATUS_ACCESS_DENIED
;
2415 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2418 if ( !(smbpass
= samu_new( mem_ctx
)) ) {
2419 return NT_STATUS_NO_MEMORY
;
2422 ret
= pdb_getsampwsid(smbpass
, user_sid
);
2425 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid
)));
2426 TALLOC_FREE(smbpass
);
2427 return (geteuid() == (uid_t
)0) ? NT_STATUS_NO_SUCH_USER
: NT_STATUS_ACCESS_DENIED
;
2430 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass
), pdb_get_acct_ctrl(smbpass
) ));
2432 if ( pdb_get_acct_ctrl(smbpass
) & ACB_DISABLED
) {
2433 TALLOC_FREE(smbpass
);
2434 return NT_STATUS_ACCOUNT_DISABLED
;
2437 init_samr_user_info18(r
, pdb_get_lanman_passwd(smbpass
),
2438 pdb_get_nt_passwd(smbpass
));
2440 TALLOC_FREE(smbpass
);
2442 return NT_STATUS_OK
;
2445 /*************************************************************************
2447 *************************************************************************/
2449 static NTSTATUS
get_user_info_20(TALLOC_CTX
*mem_ctx
,
2450 struct samr_UserInfo20
*r
,
2453 struct samu
*sampass
=NULL
;
2455 const char *munged_dial
= NULL
;
2458 struct lsa_BinaryString
*parameters
= NULL
;
2462 if ( !(sampass
= samu_new( mem_ctx
)) ) {
2463 return NT_STATUS_NO_MEMORY
;
2467 ret
= pdb_getsampwsid(sampass
, user_sid
);
2471 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid
)));
2472 TALLOC_FREE(sampass
);
2473 return NT_STATUS_NO_SUCH_USER
;
2476 munged_dial
= pdb_get_munged_dial(sampass
);
2478 samr_clear_sam_passwd(sampass
);
2480 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass
),
2481 munged_dial
, (int)strlen(munged_dial
)));
2484 blob
= base64_decode_data_blob(munged_dial
);
2486 blob
= data_blob_string_const("");
2489 status
= init_samr_parameters_string(mem_ctx
, &blob
, ¶meters
);
2490 data_blob_free(&blob
);
2491 TALLOC_FREE(sampass
);
2492 if (!NT_STATUS_IS_OK(status
)) {
2496 init_samr_user_info20(r
, parameters
);
2498 return NT_STATUS_OK
;
2502 /*************************************************************************
2504 *************************************************************************/
2506 static NTSTATUS
get_user_info_21(TALLOC_CTX
*mem_ctx
,
2507 struct samr_UserInfo21
*r
,
2509 DOM_SID
*domain_sid
)
2512 struct samu
*pw
= NULL
;
2514 const DOM_SID
*sid_user
, *sid_group
;
2515 uint32_t rid
, primary_gid
;
2516 NTTIME last_logon
, last_logoff
, last_password_change
,
2517 acct_expiry
, allow_password_change
, force_password_change
;
2518 time_t must_change_time
;
2519 uint8_t password_expired
;
2520 const char *account_name
, *full_name
, *home_directory
, *home_drive
,
2521 *logon_script
, *profile_path
, *description
,
2522 *workstations
, *comment
;
2523 struct samr_LogonHours logon_hours
;
2524 struct lsa_BinaryString
*parameters
= NULL
;
2525 const char *munged_dial
= NULL
;
2530 if (!(pw
= samu_new(mem_ctx
))) {
2531 return NT_STATUS_NO_MEMORY
;
2535 ret
= pdb_getsampwsid(pw
, user_sid
);
2539 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid
)));
2541 return NT_STATUS_NO_SUCH_USER
;
2544 samr_clear_sam_passwd(pw
);
2546 DEBUG(3,("User:[%s]\n", pdb_get_username(pw
)));
2548 sid_user
= pdb_get_user_sid(pw
);
2550 if (!sid_peek_check_rid(domain_sid
, sid_user
, &rid
)) {
2551 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2552 "the domain sid %s. Failing operation.\n",
2553 pdb_get_username(pw
), sid_string_dbg(sid_user
),
2554 sid_string_dbg(domain_sid
)));
2556 return NT_STATUS_UNSUCCESSFUL
;
2560 sid_group
= pdb_get_group_sid(pw
);
2563 if (!sid_peek_check_rid(domain_sid
, sid_group
, &primary_gid
)) {
2564 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2565 "which conflicts with the domain sid %s. Failing operation.\n",
2566 pdb_get_username(pw
), sid_string_dbg(sid_group
),
2567 sid_string_dbg(domain_sid
)));
2569 return NT_STATUS_UNSUCCESSFUL
;
2572 unix_to_nt_time(&last_logon
, pdb_get_logon_time(pw
));
2573 unix_to_nt_time(&last_logoff
, pdb_get_logoff_time(pw
));
2574 unix_to_nt_time(&acct_expiry
, pdb_get_kickoff_time(pw
));
2575 unix_to_nt_time(&last_password_change
, pdb_get_pass_last_set_time(pw
));
2576 unix_to_nt_time(&allow_password_change
, pdb_get_pass_can_change_time(pw
));
2578 must_change_time
= pdb_get_pass_must_change_time(pw
);
2579 if (must_change_time
== get_time_t_max()) {
2580 unix_to_nt_time_abs(&force_password_change
, must_change_time
);
2582 unix_to_nt_time(&force_password_change
, must_change_time
);
2585 if (pdb_get_pass_must_change_time(pw
) == 0) {
2586 password_expired
= PASS_MUST_CHANGE_AT_NEXT_LOGON
;
2588 password_expired
= 0;
2591 munged_dial
= pdb_get_munged_dial(pw
);
2593 blob
= base64_decode_data_blob(munged_dial
);
2595 blob
= data_blob_string_const("");
2598 status
= init_samr_parameters_string(mem_ctx
, &blob
, ¶meters
);
2599 data_blob_free(&blob
);
2600 if (!NT_STATUS_IS_OK(status
)) {
2605 account_name
= talloc_strdup(mem_ctx
, pdb_get_username(pw
));
2606 full_name
= talloc_strdup(mem_ctx
, pdb_get_fullname(pw
));
2607 home_directory
= talloc_strdup(mem_ctx
, pdb_get_homedir(pw
));
2608 home_drive
= talloc_strdup(mem_ctx
, pdb_get_dir_drive(pw
));
2609 logon_script
= talloc_strdup(mem_ctx
, pdb_get_logon_script(pw
));
2610 profile_path
= talloc_strdup(mem_ctx
, pdb_get_profile_path(pw
));
2611 description
= talloc_strdup(mem_ctx
, pdb_get_acct_desc(pw
));
2612 workstations
= talloc_strdup(mem_ctx
, pdb_get_workstations(pw
));
2613 comment
= talloc_strdup(mem_ctx
, pdb_get_comment(pw
));
2615 logon_hours
= get_logon_hours_from_pdb(mem_ctx
, pw
);
2619 Look at a user on a real NT4 PDC with usrmgr, press
2620 'ok'. Then you will see that fields_present is set to
2621 0x08f827fa. Look at the user immediately after that again,
2622 and you will see that 0x00fffff is returned. This solves
2623 the problem that you get access denied after having looked
2630 init_samr_user_info21(r
,
2633 last_password_change
,
2635 allow_password_change
,
2636 force_password_change
,
2649 pdb_get_acct_ctrl(pw
),
2650 pdb_build_fields_present(pw
),
2652 pdb_get_bad_password_count(pw
),
2653 pdb_get_logon_count(pw
),
2654 0, /* country_code */
2656 0, /* nt_password_set */
2657 0, /* lm_password_set */
2661 return NT_STATUS_OK
;
2664 /*******************************************************************
2666 ********************************************************************/
2668 NTSTATUS
_samr_QueryUserInfo(pipes_struct
*p
,
2669 struct samr_QueryUserInfo
*r
)
2672 union samr_UserInfo
*user_info
= NULL
;
2673 struct samr_info
*info
= NULL
;
2677 /* search for the handle */
2678 if (!find_policy_by_hnd(p
, r
->in
.user_handle
, (void **)(void *)&info
))
2679 return NT_STATUS_INVALID_HANDLE
;
2681 domain_sid
= info
->sid
;
2683 sid_split_rid(&domain_sid
, &rid
);
2685 if (!sid_check_is_in_our_domain(&info
->sid
))
2686 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
2688 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
2689 sid_string_dbg(&info
->sid
)));
2691 user_info
= TALLOC_ZERO_P(p
->mem_ctx
, union samr_UserInfo
);
2693 return NT_STATUS_NO_MEMORY
;
2696 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r
->in
.level
));
2698 switch (r
->in
.level
) {
2700 status
= get_user_info_7(p
->mem_ctx
, &user_info
->info7
, &info
->sid
);
2701 if (!NT_STATUS_IS_OK(status
)) {
2706 status
= get_user_info_9(p
->mem_ctx
, &user_info
->info9
, &info
->sid
);
2707 if (!NT_STATUS_IS_OK(status
)) {
2712 status
= get_user_info_16(p
->mem_ctx
, &user_info
->info16
, &info
->sid
);
2713 if (!NT_STATUS_IS_OK(status
)) {
2719 status
= get_user_info_18(p
, p
->mem_ctx
, &user_info
->info18
, &info
->sid
);
2720 if (!NT_STATUS_IS_OK(status
)) {
2726 status
= get_user_info_20(p
->mem_ctx
, &user_info
->info20
, &info
->sid
);
2727 if (!NT_STATUS_IS_OK(status
)) {
2733 status
= get_user_info_21(p
->mem_ctx
, &user_info
->info21
,
2734 &info
->sid
, &domain_sid
);
2735 if (!NT_STATUS_IS_OK(status
)) {
2741 return NT_STATUS_INVALID_INFO_CLASS
;
2744 *r
->out
.info
= user_info
;
2746 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__
));
2751 /*******************************************************************
2752 _samr_GetGroupsForUser
2753 ********************************************************************/
2755 NTSTATUS
_samr_GetGroupsForUser(pipes_struct
*p
,
2756 struct samr_GetGroupsForUser
*r
)
2758 struct samu
*sam_pass
=NULL
;
2761 struct samr_RidWithAttribute dom_gid
;
2762 struct samr_RidWithAttribute
*gids
= NULL
;
2763 uint32 primary_group_rid
;
2764 size_t num_groups
= 0;
2770 bool success
= False
;
2772 struct samr_RidWithAttributeArray
*rids
= NULL
;
2775 * from the SID in the request:
2776 * we should send back the list of DOMAIN GROUPS
2777 * the user is a member of
2779 * and only the DOMAIN GROUPS
2780 * no ALIASES !!! neither aliases of the domain
2781 * nor aliases of the builtin SID
2786 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__
));
2788 rids
= TALLOC_ZERO_P(p
->mem_ctx
, struct samr_RidWithAttributeArray
);
2790 return NT_STATUS_NO_MEMORY
;
2793 /* find the policy handle. open a policy on it. */
2794 if (!get_lsa_policy_samr_sid(p
, r
->in
.user_handle
, &sid
, &acc_granted
, NULL
))
2795 return NT_STATUS_INVALID_HANDLE
;
2797 result
= access_check_samr_function(acc_granted
,
2798 SA_RIGHT_USER_GET_GROUPS
,
2799 "_samr_GetGroupsForUser");
2800 if (!NT_STATUS_IS_OK(result
)) {
2804 if (!sid_check_is_in_our_domain(&sid
))
2805 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
2807 if ( !(sam_pass
= samu_new( p
->mem_ctx
)) ) {
2808 return NT_STATUS_NO_MEMORY
;
2812 ret
= pdb_getsampwsid(sam_pass
, &sid
);
2816 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2817 sid_string_dbg(&sid
)));
2818 return NT_STATUS_NO_SUCH_USER
;
2823 /* make both calls inside the root block */
2825 result
= pdb_enum_group_memberships(p
->mem_ctx
, sam_pass
,
2826 &sids
, &unix_gids
, &num_groups
);
2827 if ( NT_STATUS_IS_OK(result
) ) {
2828 success
= sid_peek_check_rid(get_global_sam_sid(),
2829 pdb_get_group_sid(sam_pass
),
2830 &primary_group_rid
);
2834 if (!NT_STATUS_IS_OK(result
)) {
2835 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2836 sid_string_dbg(&sid
)));
2841 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2842 sid_string_dbg(pdb_get_group_sid(sam_pass
)),
2843 pdb_get_username(sam_pass
)));
2844 TALLOC_FREE(sam_pass
);
2845 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
2851 dom_gid
.attributes
= (SE_GROUP_MANDATORY
|SE_GROUP_ENABLED_BY_DEFAULT
|
2853 dom_gid
.rid
= primary_group_rid
;
2854 ADD_TO_ARRAY(p
->mem_ctx
, struct samr_RidWithAttribute
, dom_gid
, &gids
, &num_gids
);
2856 for (i
=0; i
<num_groups
; i
++) {
2858 if (!sid_peek_check_rid(get_global_sam_sid(),
2859 &(sids
[i
]), &dom_gid
.rid
)) {
2860 DEBUG(10, ("Found sid %s not in our domain\n",
2861 sid_string_dbg(&sids
[i
])));
2865 if (dom_gid
.rid
== primary_group_rid
) {
2866 /* We added the primary group directly from the
2867 * sam_account. The other SIDs are unique from
2868 * enum_group_memberships */
2872 ADD_TO_ARRAY(p
->mem_ctx
, struct samr_RidWithAttribute
, dom_gid
, &gids
, &num_gids
);
2875 rids
->count
= num_gids
;
2878 *r
->out
.rids
= rids
;
2880 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__
));
2885 /*******************************************************************
2886 samr_QueryDomainInfo_internal
2887 ********************************************************************/
2889 static NTSTATUS
samr_QueryDomainInfo_internal(const char *fn_name
,
2891 struct policy_handle
*handle
,
2893 union samr_DomainInfo
**dom_info_ptr
)
2895 NTSTATUS status
= NT_STATUS_OK
;
2896 struct samr_info
*info
= NULL
;
2897 union samr_DomainInfo
*dom_info
;
2898 uint32 min_pass_len
,pass_hist
,password_properties
;
2899 time_t u_expire
, u_min_age
;
2900 NTTIME nt_expire
, nt_min_age
;
2902 time_t u_lock_duration
, u_reset_time
;
2903 NTTIME nt_lock_duration
, nt_reset_time
;
2908 uint32 account_policy_temp
;
2913 uint32 num_users
=0, num_groups
=0, num_aliases
=0;
2915 DEBUG(5,("%s: %d\n", fn_name
, __LINE__
));
2917 dom_info
= TALLOC_ZERO_P(p
->mem_ctx
, union samr_DomainInfo
);
2919 return NT_STATUS_NO_MEMORY
;
2922 *dom_info_ptr
= dom_info
;
2924 /* find the policy handle. open a policy on it. */
2925 if (!find_policy_by_hnd(p
, handle
, (void **)(void *)&info
)) {
2926 return NT_STATUS_INVALID_HANDLE
;
2936 pdb_get_account_policy(AP_MIN_PASSWORD_LEN
, &account_policy_temp
);
2937 min_pass_len
= account_policy_temp
;
2939 pdb_get_account_policy(AP_PASSWORD_HISTORY
, &account_policy_temp
);
2940 pass_hist
= account_policy_temp
;
2942 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS
, &account_policy_temp
);
2943 password_properties
= account_policy_temp
;
2945 pdb_get_account_policy(AP_MAX_PASSWORD_AGE
, &account_policy_temp
);
2946 u_expire
= account_policy_temp
;
2948 pdb_get_account_policy(AP_MIN_PASSWORD_AGE
, &account_policy_temp
);
2949 u_min_age
= account_policy_temp
;
2955 unix_to_nt_time_abs(&nt_expire
, u_expire
);
2956 unix_to_nt_time_abs(&nt_min_age
, u_min_age
);
2958 if (lp_check_password_script() && *lp_check_password_script()) {
2959 password_properties
|= DOMAIN_PASSWORD_COMPLEX
;
2962 init_samr_DomInfo1(&dom_info
->info1
,
2963 (uint16
)min_pass_len
,
2965 password_properties
,
2975 num_users
= count_sam_users(info
->disp_info
, ACB_NORMAL
);
2976 num_groups
= count_sam_groups(info
->disp_info
);
2977 num_aliases
= count_sam_aliases(info
->disp_info
);
2979 pdb_get_account_policy(AP_TIME_TO_LOGOUT
, &account_policy_temp
);
2980 u_logout
= account_policy_temp
;
2982 unix_to_nt_time_abs(&nt_logout
, u_logout
);
2984 if (!pdb_get_seq_num(&seq_num
))
2985 seq_num
= time(NULL
);
2991 server_role
= ROLE_DOMAIN_PDC
;
2992 if (lp_server_role() == ROLE_DOMAIN_BDC
)
2993 server_role
= ROLE_DOMAIN_BDC
;
2995 init_samr_DomInfo2(&dom_info
->info2
,
3016 pdb_get_account_policy(AP_TIME_TO_LOGOUT
, &ul
);
3017 u_logout
= (time_t)ul
;
3024 unix_to_nt_time_abs(&nt_logout
, u_logout
);
3026 init_samr_DomInfo3(&dom_info
->info3
,
3031 init_samr_DomInfo4(&dom_info
->info4
,
3035 init_samr_DomInfo5(&dom_info
->info5
,
3036 get_global_sam_name());
3039 /* NT returns its own name when a PDC. win2k and later
3040 * only the name of the PDC if itself is a BDC (samba4
3042 init_samr_DomInfo6(&dom_info
->info6
,
3046 server_role
= ROLE_DOMAIN_PDC
;
3047 if (lp_server_role() == ROLE_DOMAIN_BDC
)
3048 server_role
= ROLE_DOMAIN_BDC
;
3050 init_samr_DomInfo7(&dom_info
->info7
,
3059 if (!pdb_get_seq_num(&seq_num
)) {
3060 seq_num
= time(NULL
);
3067 init_samr_DomInfo8(&dom_info
->info8
,
3077 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION
, &account_policy_temp
);
3078 u_lock_duration
= account_policy_temp
;
3079 if (u_lock_duration
!= -1) {
3080 u_lock_duration
*= 60;
3083 pdb_get_account_policy(AP_RESET_COUNT_TIME
, &account_policy_temp
);
3084 u_reset_time
= account_policy_temp
* 60;
3086 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT
, &account_policy_temp
);
3087 lockout
= account_policy_temp
;
3093 unix_to_nt_time_abs(&nt_lock_duration
, u_lock_duration
);
3094 unix_to_nt_time_abs(&nt_reset_time
, u_reset_time
);
3096 init_samr_DomInfo12(&dom_info
->info12
,
3102 return NT_STATUS_INVALID_INFO_CLASS
;
3105 DEBUG(5,("%s: %d\n", fn_name
, __LINE__
));
3110 /*******************************************************************
3111 _samr_QueryDomainInfo
3112 ********************************************************************/
3114 NTSTATUS
_samr_QueryDomainInfo(pipes_struct
*p
,
3115 struct samr_QueryDomainInfo
*r
)
3117 return samr_QueryDomainInfo_internal("_samr_QueryDomainInfo",
3119 r
->in
.domain_handle
,
3124 /* W2k3 seems to use the same check for all 3 objects that can be created via
3125 * SAMR, if you try to create for example "Dialup" as an alias it says
3126 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3129 static NTSTATUS
can_create(TALLOC_CTX
*mem_ctx
, const char *new_name
)
3131 enum lsa_SidType type
;
3134 DEBUG(10, ("Checking whether [%s] can be created\n", new_name
));
3137 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3138 * whether the name already exists */
3139 result
= lookup_name(mem_ctx
, new_name
, LOOKUP_NAME_LOCAL
,
3140 NULL
, NULL
, NULL
, &type
);
3144 DEBUG(10, ("%s does not exist, can create it\n", new_name
));
3145 return NT_STATUS_OK
;
3148 DEBUG(5, ("trying to create %s, exists as %s\n",
3149 new_name
, sid_type_lookup(type
)));
3151 if (type
== SID_NAME_DOM_GRP
) {
3152 return NT_STATUS_GROUP_EXISTS
;
3154 if (type
== SID_NAME_ALIAS
) {
3155 return NT_STATUS_ALIAS_EXISTS
;
3158 /* Yes, the default is NT_STATUS_USER_EXISTS */
3159 return NT_STATUS_USER_EXISTS
;
3162 /*******************************************************************
3164 ********************************************************************/
3166 NTSTATUS
_samr_CreateUser2(pipes_struct
*p
,
3167 struct samr_CreateUser2
*r
)
3169 const char *account
= NULL
;
3171 POLICY_HND dom_pol
= *r
->in
.domain_handle
;
3172 uint32_t acb_info
= r
->in
.acct_flags
;
3173 POLICY_HND
*user_pol
= r
->out
.user_handle
;
3174 struct samr_info
*info
= NULL
;
3179 /* check this, when giving away 'add computer to domain' privs */
3180 uint32 des_access
= GENERIC_RIGHTS_USER_ALL_ACCESS
;
3181 bool can_add_account
= False
;
3183 DISP_INFO
*disp_info
= NULL
;
3185 /* Get the domain SID stored in the domain policy */
3186 if (!get_lsa_policy_samr_sid(p
, &dom_pol
, &sid
, &acc_granted
,
3188 return NT_STATUS_INVALID_HANDLE
;
3190 nt_status
= access_check_samr_function(acc_granted
,
3191 SA_RIGHT_DOMAIN_CREATE_USER
,
3192 "_samr_CreateUser2");
3193 if (!NT_STATUS_IS_OK(nt_status
)) {
3197 if (!(acb_info
== ACB_NORMAL
|| acb_info
== ACB_DOMTRUST
||
3198 acb_info
== ACB_WSTRUST
|| acb_info
== ACB_SVRTRUST
)) {
3199 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3200 this parameter is not an account type */
3201 return NT_STATUS_INVALID_PARAMETER
;
3204 account
= r
->in
.account_name
->string
;
3205 if (account
== NULL
) {
3206 return NT_STATUS_NO_MEMORY
;
3209 nt_status
= can_create(p
->mem_ctx
, account
);
3210 if (!NT_STATUS_IS_OK(nt_status
)) {
3214 /* determine which user right we need to check based on the acb_info */
3216 if ( acb_info
& ACB_WSTRUST
)
3218 se_priv_copy( &se_rights
, &se_machine_account
);
3219 can_add_account
= user_has_privileges(
3220 p
->pipe_user
.nt_user_token
, &se_rights
);
3222 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3223 account for domain trusts and changes the ACB flags later */
3224 else if ( acb_info
& ACB_NORMAL
&&
3225 (account
[strlen(account
)-1] != '$') )
3227 se_priv_copy( &se_rights
, &se_add_users
);
3228 can_add_account
= user_has_privileges(
3229 p
->pipe_user
.nt_user_token
, &se_rights
);
3231 else /* implicit assumption of a BDC or domain trust account here
3232 * (we already check the flags earlier) */
3234 if ( lp_enable_privileges() ) {
3235 /* only Domain Admins can add a BDC or domain trust */
3236 se_priv_copy( &se_rights
, &se_priv_none
);
3237 can_add_account
= nt_token_check_domain_rid(
3238 p
->pipe_user
.nt_user_token
,
3239 DOMAIN_GROUP_RID_ADMINS
);
3243 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3244 uidtoname(p
->pipe_user
.ut
.uid
),
3245 can_add_account
? "True":"False" ));
3247 /********** BEGIN Admin BLOCK **********/
3249 if ( can_add_account
)
3252 nt_status
= pdb_create_user(p
->mem_ctx
, account
, acb_info
,
3255 if ( can_add_account
)
3258 /********** END Admin BLOCK **********/
3260 /* now check for failure */
3262 if ( !NT_STATUS_IS_OK(nt_status
) )
3265 /* Get the user's SID */
3267 sid_compose(&sid
, get_global_sam_sid(), *r
->out
.rid
);
3269 map_max_allowed_access(p
->pipe_user
.nt_user_token
, &des_access
);
3271 make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &usr_generic_mapping
,
3272 &sid
, SAMR_USR_RIGHTS_WRITE_PW
);
3273 se_map_generic(&des_access
, &usr_generic_mapping
);
3275 nt_status
= access_check_samr_object(psd
, p
->pipe_user
.nt_user_token
,
3276 &se_rights
, GENERIC_RIGHTS_USER_WRITE
, des_access
,
3277 &acc_granted
, "_samr_CreateUser2");
3279 if ( !NT_STATUS_IS_OK(nt_status
) ) {
3283 /* associate the user's SID with the new handle. */
3284 if ((info
= get_samr_info_by_sid(&sid
)) == NULL
) {
3285 return NT_STATUS_NO_MEMORY
;
3290 info
->acc_granted
= acc_granted
;
3292 /* get a (unique) handle. open a policy on it. */
3293 if (!create_policy_hnd(p
, user_pol
, free_samr_info
, (void *)info
)) {
3294 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3297 /* After a "set" ensure we have no cached display info. */
3298 force_flush_samr_cache(info
->disp_info
);
3300 *r
->out
.access_granted
= acc_granted
;
3302 return NT_STATUS_OK
;
3305 /*******************************************************************
3307 ********************************************************************/
3309 NTSTATUS
_samr_Connect(pipes_struct
*p
,
3310 struct samr_Connect
*r
)
3312 struct samr_info
*info
= NULL
;
3313 uint32 des_access
= r
->in
.access_mask
;
3317 if (!pipe_access_check(p
)) {
3318 DEBUG(3, ("access denied to _samr_Connect\n"));
3319 return NT_STATUS_ACCESS_DENIED
;
3322 /* set up the SAMR connect_anon response */
3324 /* associate the user's SID with the new handle. */
3325 if ((info
= get_samr_info_by_sid(NULL
)) == NULL
)
3326 return NT_STATUS_NO_MEMORY
;
3328 /* don't give away the farm but this is probably ok. The SA_RIGHT_SAM_ENUM_DOMAINS
3329 was observed from a win98 client trying to enumerate users (when configured
3330 user level access control on shares) --jerry */
3332 map_max_allowed_access(p
->pipe_user
.nt_user_token
, &des_access
);
3334 se_map_generic( &des_access
, &sam_generic_mapping
);
3335 info
->acc_granted
= des_access
& (SA_RIGHT_SAM_ENUM_DOMAINS
|SA_RIGHT_SAM_OPEN_DOMAIN
);
3337 /* get a (unique) handle. open a policy on it. */
3338 if (!create_policy_hnd(p
, r
->out
.connect_handle
, free_samr_info
, (void *)info
))
3339 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3341 return NT_STATUS_OK
;
3344 /*******************************************************************
3346 ********************************************************************/
3348 NTSTATUS
_samr_Connect2(pipes_struct
*p
,
3349 struct samr_Connect2
*r
)
3351 struct samr_info
*info
= NULL
;
3352 SEC_DESC
*psd
= NULL
;
3354 uint32 des_access
= r
->in
.access_mask
;
3359 DEBUG(5,("_samr_Connect2: %d\n", __LINE__
));
3363 if (!pipe_access_check(p
)) {
3364 DEBUG(3, ("access denied to _samr_Connect2\n"));
3365 return NT_STATUS_ACCESS_DENIED
;
3368 map_max_allowed_access(p
->pipe_user
.nt_user_token
, &des_access
);
3370 make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &sam_generic_mapping
, NULL
, 0);
3371 se_map_generic(&des_access
, &sam_generic_mapping
);
3373 nt_status
= access_check_samr_object(psd
, p
->pipe_user
.nt_user_token
,
3374 NULL
, 0, des_access
, &acc_granted
, "_samr_Connect2");
3376 if ( !NT_STATUS_IS_OK(nt_status
) )
3379 /* associate the user's SID and access granted with the new handle. */
3380 if ((info
= get_samr_info_by_sid(NULL
)) == NULL
)
3381 return NT_STATUS_NO_MEMORY
;
3383 info
->acc_granted
= acc_granted
;
3384 info
->status
= r
->in
.access_mask
; /* this looks so wrong... - gd */
3386 /* get a (unique) handle. open a policy on it. */
3387 if (!create_policy_hnd(p
, r
->out
.connect_handle
, free_samr_info
, (void *)info
))
3388 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3390 DEBUG(5,("_samr_Connect2: %d\n", __LINE__
));
3395 /*******************************************************************
3397 ********************************************************************/
3399 NTSTATUS
_samr_Connect4(pipes_struct
*p
,
3400 struct samr_Connect4
*r
)
3402 struct samr_info
*info
= NULL
;
3403 SEC_DESC
*psd
= NULL
;
3405 uint32 des_access
= r
->in
.access_mask
;
3410 DEBUG(5,("_samr_Connect4: %d\n", __LINE__
));
3414 if (!pipe_access_check(p
)) {
3415 DEBUG(3, ("access denied to samr_Connect4\n"));
3416 return NT_STATUS_ACCESS_DENIED
;
3419 map_max_allowed_access(p
->pipe_user
.nt_user_token
, &des_access
);
3421 make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &sam_generic_mapping
, NULL
, 0);
3422 se_map_generic(&des_access
, &sam_generic_mapping
);
3424 nt_status
= access_check_samr_object(psd
, p
->pipe_user
.nt_user_token
,
3425 NULL
, 0, des_access
, &acc_granted
, "_samr_Connect4");
3427 if ( !NT_STATUS_IS_OK(nt_status
) )
3430 /* associate the user's SID and access granted with the new handle. */
3431 if ((info
= get_samr_info_by_sid(NULL
)) == NULL
)
3432 return NT_STATUS_NO_MEMORY
;
3434 info
->acc_granted
= acc_granted
;
3435 info
->status
= r
->in
.access_mask
; /* ??? */
3437 /* get a (unique) handle. open a policy on it. */
3438 if (!create_policy_hnd(p
, r
->out
.connect_handle
, free_samr_info
, (void *)info
))
3439 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3441 DEBUG(5,("_samr_Connect4: %d\n", __LINE__
));
3443 return NT_STATUS_OK
;
3446 /*******************************************************************
3448 ********************************************************************/
3450 NTSTATUS
_samr_Connect5(pipes_struct
*p
,
3451 struct samr_Connect5
*r
)
3453 struct samr_info
*info
= NULL
;
3454 SEC_DESC
*psd
= NULL
;
3456 uint32 des_access
= r
->in
.access_mask
;
3459 struct samr_ConnectInfo1 info1
;
3461 DEBUG(5,("_samr_Connect5: %d\n", __LINE__
));
3465 if (!pipe_access_check(p
)) {
3466 DEBUG(3, ("access denied to samr_Connect5\n"));
3467 return NT_STATUS_ACCESS_DENIED
;
3470 map_max_allowed_access(p
->pipe_user
.nt_user_token
, &des_access
);
3472 make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &sam_generic_mapping
, NULL
, 0);
3473 se_map_generic(&des_access
, &sam_generic_mapping
);
3475 nt_status
= access_check_samr_object(psd
, p
->pipe_user
.nt_user_token
,
3476 NULL
, 0, des_access
, &acc_granted
, "_samr_Connect5");
3478 if ( !NT_STATUS_IS_OK(nt_status
) )
3481 /* associate the user's SID and access granted with the new handle. */
3482 if ((info
= get_samr_info_by_sid(NULL
)) == NULL
)
3483 return NT_STATUS_NO_MEMORY
;
3485 info
->acc_granted
= acc_granted
;
3486 info
->status
= r
->in
.access_mask
; /* ??? */
3488 /* get a (unique) handle. open a policy on it. */
3489 if (!create_policy_hnd(p
, r
->out
.connect_handle
, free_samr_info
, (void *)info
))
3490 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3492 DEBUG(5,("_samr_Connect5: %d\n", __LINE__
));
3494 info1
.client_version
= SAMR_CONNECT_AFTER_W2K
;
3497 *r
->out
.level_out
= 1;
3498 r
->out
.info_out
->info1
= info1
;
3500 return NT_STATUS_OK
;
3503 /**********************************************************************
3505 **********************************************************************/
3507 NTSTATUS
_samr_LookupDomain(pipes_struct
*p
,
3508 struct samr_LookupDomain
*r
)
3510 NTSTATUS status
= NT_STATUS_OK
;
3511 struct samr_info
*info
;
3512 const char *domain_name
;
3513 DOM_SID
*sid
= NULL
;
3515 if (!find_policy_by_hnd(p
, r
->in
.connect_handle
, (void**)(void *)&info
))
3516 return NT_STATUS_INVALID_HANDLE
;
3518 /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.
3519 Reverted that change so we will work with RAS servers again */
3521 status
= access_check_samr_function(info
->acc_granted
,
3522 SA_RIGHT_SAM_OPEN_DOMAIN
,
3523 "_samr_LookupDomain");
3524 if (!NT_STATUS_IS_OK(status
)) {
3528 domain_name
= r
->in
.domain_name
->string
;
3530 sid
= TALLOC_ZERO_P(p
->mem_ctx
, struct dom_sid2
);
3532 return NT_STATUS_NO_MEMORY
;
3535 if (strequal(domain_name
, builtin_domain_name())) {
3536 sid_copy(sid
, &global_sid_Builtin
);
3538 if (!secrets_fetch_domain_sid(domain_name
, sid
)) {
3539 status
= NT_STATUS_NO_SUCH_DOMAIN
;
3543 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name
,
3544 sid_string_dbg(sid
)));
3551 /**********************************************************************
3553 **********************************************************************/
3555 NTSTATUS
_samr_EnumDomains(pipes_struct
*p
,
3556 struct samr_EnumDomains
*r
)
3559 struct samr_info
*info
;
3560 uint32_t num_entries
= 2;
3561 struct samr_SamEntry
*entry_array
= NULL
;
3562 struct samr_SamArray
*sam
;
3564 if (!find_policy_by_hnd(p
, r
->in
.connect_handle
, (void**)(void *)&info
))
3565 return NT_STATUS_INVALID_HANDLE
;
3567 status
= access_check_samr_function(info
->acc_granted
,
3568 SA_RIGHT_SAM_ENUM_DOMAINS
,
3569 "_samr_EnumDomains");
3570 if (!NT_STATUS_IS_OK(status
)) {
3574 sam
= TALLOC_ZERO_P(p
->mem_ctx
, struct samr_SamArray
);
3576 return NT_STATUS_NO_MEMORY
;
3579 entry_array
= TALLOC_ZERO_ARRAY(p
->mem_ctx
,
3580 struct samr_SamEntry
,
3583 return NT_STATUS_NO_MEMORY
;
3586 entry_array
[0].idx
= 0;
3587 init_lsa_String(&entry_array
[0].name
, get_global_sam_name());
3589 entry_array
[1].idx
= 1;
3590 init_lsa_String(&entry_array
[1].name
, "Builtin");
3592 sam
->count
= num_entries
;
3593 sam
->entries
= entry_array
;
3596 *r
->out
.num_entries
= num_entries
;
3601 /*******************************************************************
3603 ********************************************************************/
3605 NTSTATUS
_samr_OpenAlias(pipes_struct
*p
,
3606 struct samr_OpenAlias
*r
)
3609 POLICY_HND domain_pol
= *r
->in
.domain_handle
;
3610 uint32 alias_rid
= r
->in
.rid
;
3611 POLICY_HND
*alias_pol
= r
->out
.alias_handle
;
3612 struct samr_info
*info
= NULL
;
3613 SEC_DESC
*psd
= NULL
;
3615 uint32 des_access
= r
->in
.access_mask
;
3620 /* find the domain policy and get the SID / access bits stored in the domain policy */
3622 if ( !get_lsa_policy_samr_sid(p
, &domain_pol
, &sid
, &acc_granted
, NULL
) )
3623 return NT_STATUS_INVALID_HANDLE
;
3625 status
= access_check_samr_function(acc_granted
,
3626 SA_RIGHT_DOMAIN_OPEN_ACCOUNT
,
3629 if ( !NT_STATUS_IS_OK(status
) )
3632 /* append the alias' RID to it */
3634 if (!sid_append_rid(&sid
, alias_rid
))
3635 return NT_STATUS_NO_SUCH_ALIAS
;
3637 /*check if access can be granted as requested by client. */
3639 map_max_allowed_access(p
->pipe_user
.nt_user_token
, &des_access
);
3641 make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &ali_generic_mapping
, NULL
, 0);
3642 se_map_generic(&des_access
,&ali_generic_mapping
);
3644 se_priv_copy( &se_rights
, &se_add_users
);
3647 status
= access_check_samr_object(psd
, p
->pipe_user
.nt_user_token
,
3648 &se_rights
, GENERIC_RIGHTS_ALIAS_WRITE
, des_access
,
3649 &acc_granted
, "_samr_OpenAlias");
3651 if ( !NT_STATUS_IS_OK(status
) )
3655 /* Check we actually have the requested alias */
3656 enum lsa_SidType type
;
3661 result
= lookup_sid(NULL
, &sid
, NULL
, NULL
, &type
);
3664 if (!result
|| (type
!= SID_NAME_ALIAS
)) {
3665 return NT_STATUS_NO_SUCH_ALIAS
;
3668 /* make sure there is a mapping */
3670 if ( !sid_to_gid( &sid
, &gid
) ) {
3671 return NT_STATUS_NO_SUCH_ALIAS
;
3676 /* associate the alias SID with the new handle. */
3677 if ((info
= get_samr_info_by_sid(&sid
)) == NULL
)
3678 return NT_STATUS_NO_MEMORY
;
3680 info
->acc_granted
= acc_granted
;
3682 /* get a (unique) handle. open a policy on it. */
3683 if (!create_policy_hnd(p
, alias_pol
, free_samr_info
, (void *)info
))
3684 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3686 return NT_STATUS_OK
;
3689 /*******************************************************************
3691 ********************************************************************/
3693 static NTSTATUS
set_user_info_7(TALLOC_CTX
*mem_ctx
,
3694 struct samr_UserInfo7
*id7
,
3700 DEBUG(5, ("set_user_info_7: NULL id7\n"));
3702 return NT_STATUS_ACCESS_DENIED
;
3705 if (!id7
->account_name
.string
) {
3706 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
3708 return NT_STATUS_ACCESS_DENIED
;
3711 /* check to see if the new username already exists. Note: we can't
3712 reliably lock all backends, so there is potentially the
3713 possibility that a user can be created in between this check and
3714 the rename. The rename should fail, but may not get the
3715 exact same failure status code. I think this is small enough
3716 of a window for this type of operation and the results are
3717 simply that the rename fails with a slightly different status
3718 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3720 rc
= can_create(mem_ctx
, id7
->account_name
.string
);
3721 if (!NT_STATUS_IS_OK(rc
)) {
3725 rc
= pdb_rename_sam_account(pwd
, id7
->account_name
.string
);
3731 /*******************************************************************
3733 ********************************************************************/
3735 static bool set_user_info_16(struct samr_UserInfo16
*id16
,
3739 DEBUG(5, ("set_user_info_16: NULL id16\n"));
3744 /* FIX ME: check if the value is really changed --metze */
3745 if (!pdb_set_acct_ctrl(pwd
, id16
->acct_flags
, PDB_CHANGED
)) {
3750 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd
))) {
3760 /*******************************************************************
3762 ********************************************************************/
3764 static bool set_user_info_18(struct samr_UserInfo18
*id18
,
3768 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3773 if (!pdb_set_lanman_passwd (pwd
, id18
->lm_pwd
.hash
, PDB_CHANGED
)) {
3777 if (!pdb_set_nt_passwd (pwd
, id18
->nt_pwd
.hash
, PDB_CHANGED
)) {
3781 if (!pdb_set_pass_last_set_time (pwd
, time(NULL
), PDB_CHANGED
)) {
3786 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd
))) {
3795 /*******************************************************************
3797 ********************************************************************/
3799 static bool set_user_info_20(struct samr_UserInfo20
*id20
,
3803 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3807 copy_id20_to_sam_passwd(pwd
, id20
);
3809 /* write the change out */
3810 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd
))) {
3820 /*******************************************************************
3822 ********************************************************************/
3824 static NTSTATUS
set_user_info_21(TALLOC_CTX
*mem_ctx
,
3825 struct samr_UserInfo21
*id21
,
3831 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3832 return NT_STATUS_INVALID_PARAMETER
;
3835 /* we need to separately check for an account rename first */
3837 if (id21
->account_name
.string
&&
3838 (!strequal(id21
->account_name
.string
, pdb_get_username(pwd
))))
3841 /* check to see if the new username already exists. Note: we can't
3842 reliably lock all backends, so there is potentially the
3843 possibility that a user can be created in between this check and
3844 the rename. The rename should fail, but may not get the
3845 exact same failure status code. I think this is small enough
3846 of a window for this type of operation and the results are
3847 simply that the rename fails with a slightly different status
3848 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3850 status
= can_create(mem_ctx
, id21
->account_name
.string
);
3851 if (!NT_STATUS_IS_OK(status
)) {
3855 status
= pdb_rename_sam_account(pwd
, id21
->account_name
.string
);
3857 if (!NT_STATUS_IS_OK(status
)) {
3858 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
3859 nt_errstr(status
)));
3864 /* set the new username so that later
3865 functions can work on the new account */
3866 pdb_set_username(pwd
, id21
->account_name
.string
, PDB_SET
);
3869 copy_id21_to_sam_passwd("INFO_21", pwd
, id21
);
3872 * The funny part about the previous two calls is
3873 * that pwd still has the password hashes from the
3874 * passdb entry. These have not been updated from
3875 * id21. I don't know if they need to be set. --jerry
3878 if ( IS_SAM_CHANGED(pwd
, PDB_GROUPSID
) ) {
3879 status
= pdb_set_unix_primary_group(mem_ctx
, pwd
);
3880 if ( !NT_STATUS_IS_OK(status
) ) {
3885 /* Don't worry about writing out the user account since the
3886 primary group SID is generated solely from the user's Unix
3889 /* write the change out */
3890 if(!NT_STATUS_IS_OK(status
= pdb_update_sam_account(pwd
))) {
3897 return NT_STATUS_OK
;
3900 /*******************************************************************
3902 ********************************************************************/
3904 static NTSTATUS
set_user_info_23(TALLOC_CTX
*mem_ctx
,
3905 struct samr_UserInfo23
*id23
,
3908 char *plaintext_buf
= NULL
;
3914 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3915 return NT_STATUS_INVALID_PARAMETER
;
3918 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3919 pdb_get_username(pwd
)));
3921 acct_ctrl
= pdb_get_acct_ctrl(pwd
);
3923 if (!decode_pw_buffer(mem_ctx
,
3924 id23
->password
.data
,
3929 return NT_STATUS_INVALID_PARAMETER
;
3932 if (!pdb_set_plaintext_passwd (pwd
, plaintext_buf
)) {
3934 return NT_STATUS_ACCESS_DENIED
;
3937 copy_id23_to_sam_passwd(pwd
, id23
);
3939 /* if it's a trust account, don't update /etc/passwd */
3940 if ( ( (acct_ctrl
& ACB_DOMTRUST
) == ACB_DOMTRUST
) ||
3941 ( (acct_ctrl
& ACB_WSTRUST
) == ACB_WSTRUST
) ||
3942 ( (acct_ctrl
& ACB_SVRTRUST
) == ACB_SVRTRUST
) ) {
3943 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
3945 /* update the UNIX password */
3946 if (lp_unix_password_sync() ) {
3947 struct passwd
*passwd
;
3948 if (pdb_get_username(pwd
) == NULL
) {
3949 DEBUG(1, ("chgpasswd: User without name???\n"));
3951 return NT_STATUS_ACCESS_DENIED
;
3954 passwd
= Get_Pwnam_alloc(pwd
, pdb_get_username(pwd
));
3955 if (passwd
== NULL
) {
3956 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3959 if(!chgpasswd(pdb_get_username(pwd
), passwd
, "", plaintext_buf
, True
)) {
3961 return NT_STATUS_ACCESS_DENIED
;
3963 TALLOC_FREE(passwd
);
3967 memset(plaintext_buf
, '\0', strlen(plaintext_buf
));
3969 if (IS_SAM_CHANGED(pwd
, PDB_GROUPSID
) &&
3970 (!NT_STATUS_IS_OK(status
= pdb_set_unix_primary_group(mem_ctx
,
3976 if(!NT_STATUS_IS_OK(status
= pdb_update_sam_account(pwd
))) {
3983 return NT_STATUS_OK
;
3986 /*******************************************************************
3988 ********************************************************************/
3990 static bool set_user_info_pw(uint8
*pass
, struct samu
*pwd
,
3994 char *plaintext_buf
= NULL
;
3996 time_t last_set_time
;
3997 enum pdb_value_state last_set_state
;
3999 DEBUG(5, ("Attempting administrator password change for user %s\n",
4000 pdb_get_username(pwd
)));
4002 acct_ctrl
= pdb_get_acct_ctrl(pwd
);
4003 /* we need to know if it's expired, because this is an admin change, not a
4004 user change, so it's still expired when we're done */
4005 last_set_state
= pdb_get_init_flags(pwd
, PDB_PASSLASTSET
);
4006 last_set_time
= pdb_get_pass_last_set_time(pwd
);
4008 if (!decode_pw_buffer(talloc_tos(),
4017 if (!pdb_set_plaintext_passwd (pwd
, plaintext_buf
)) {
4022 /* if it's a trust account, don't update /etc/passwd */
4023 if ( ( (acct_ctrl
& ACB_DOMTRUST
) == ACB_DOMTRUST
) ||
4024 ( (acct_ctrl
& ACB_WSTRUST
) == ACB_WSTRUST
) ||
4025 ( (acct_ctrl
& ACB_SVRTRUST
) == ACB_SVRTRUST
) ) {
4026 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
4028 /* update the UNIX password */
4029 if (lp_unix_password_sync()) {
4030 struct passwd
*passwd
;
4032 if (pdb_get_username(pwd
) == NULL
) {
4033 DEBUG(1, ("chgpasswd: User without name???\n"));
4038 passwd
= Get_Pwnam_alloc(pwd
, pdb_get_username(pwd
));
4039 if (passwd
== NULL
) {
4040 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4043 if(!chgpasswd(pdb_get_username(pwd
), passwd
, "", plaintext_buf
, True
)) {
4047 TALLOC_FREE(passwd
);
4051 memset(plaintext_buf
, '\0', strlen(plaintext_buf
));
4054 * A level 25 change does reset the pwdlastset field, a level 24
4055 * change does not. I know this is probably not the full story, but
4056 * it is needed to make XP join LDAP correctly, without it the later
4057 * auth2 check can fail with PWD_MUST_CHANGE.
4061 * restore last set time as this is an admin change, not a
4064 pdb_set_pass_last_set_time (pwd
, last_set_time
,
4068 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
4070 /* update the SAMBA password */
4071 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd
))) {
4081 /*******************************************************************
4083 ********************************************************************/
4085 static NTSTATUS
set_user_info_25(TALLOC_CTX
*mem_ctx
,
4086 struct samr_UserInfo25
*id25
,
4092 DEBUG(5, ("set_user_info_25: NULL id25\n"));
4093 return NT_STATUS_INVALID_PARAMETER
;
4096 copy_id25_to_sam_passwd(pwd
, id25
);
4098 /* write the change out */
4099 if(!NT_STATUS_IS_OK(status
= pdb_update_sam_account(pwd
))) {
4105 * We need to "pdb_update_sam_account" before the unix primary group
4106 * is set, because the idealx scripts would also change the
4107 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
4108 * the delete explicit / add explicit, which would then fail to find
4109 * the previous primaryGroupSid value.
4112 if ( IS_SAM_CHANGED(pwd
, PDB_GROUPSID
) ) {
4113 status
= pdb_set_unix_primary_group(mem_ctx
, pwd
);
4114 if ( !NT_STATUS_IS_OK(status
) ) {
4119 /* WARNING: No TALLOC_FREE(pwd), we are about to set the password
4122 return NT_STATUS_OK
;
4125 /*******************************************************************
4126 samr_SetUserInfo_internal
4127 ********************************************************************/
4129 static NTSTATUS
samr_SetUserInfo_internal(const char *fn_name
,
4131 struct policy_handle
*user_handle
,
4133 union samr_UserInfo
*info
)
4136 struct samu
*pwd
= NULL
;
4138 POLICY_HND
*pol
= user_handle
;
4139 uint16_t switch_value
= level
;
4140 uint32_t acc_granted
;
4141 uint32_t acc_required
;
4143 bool has_enough_rights
= False
;
4145 DISP_INFO
*disp_info
= NULL
;
4147 DEBUG(5,("%s: %d\n", fn_name
, __LINE__
));
4149 /* find the policy handle. open a policy on it. */
4150 if (!get_lsa_policy_samr_sid(p
, pol
, &sid
, &acc_granted
, &disp_info
)) {
4151 return NT_STATUS_INVALID_HANDLE
;
4154 /* This is tricky. A WinXP domain join sets
4155 (SA_RIGHT_USER_SET_PASSWORD|SA_RIGHT_USER_SET_ATTRIBUTES|SA_RIGHT_USER_ACCT_FLAGS_EXPIRY)
4156 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
4157 standard Win32 API calls just ask for SA_RIGHT_USER_SET_PASSWORD in the SamrOpenUser().
4158 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
4159 we'll use the set from the WinXP join as the basis. */
4161 switch (switch_value
) {
4166 acc_required
= SA_RIGHT_USER_SET_PASSWORD
;
4169 acc_required
= SA_RIGHT_USER_SET_PASSWORD
|
4170 SA_RIGHT_USER_SET_ATTRIBUTES
|
4171 SA_RIGHT_USER_ACCT_FLAGS_EXPIRY
;
4175 status
= access_check_samr_function(acc_granted
,
4178 if (!NT_STATUS_IS_OK(status
)) {
4182 DEBUG(5, ("%s: sid:%s, level:%d\n",
4183 fn_name
, sid_string_dbg(&sid
), switch_value
));
4186 DEBUG(5, ("%s: NULL info level\n", fn_name
));
4187 return NT_STATUS_INVALID_INFO_CLASS
;
4190 if (!(pwd
= samu_new(NULL
))) {
4191 return NT_STATUS_NO_MEMORY
;
4195 ret
= pdb_getsampwsid(pwd
, &sid
);
4200 return NT_STATUS_NO_SUCH_USER
;
4203 /* deal with machine password changes differently from userinfo changes */
4204 /* check to see if we have the sufficient rights */
4206 acb_info
= pdb_get_acct_ctrl(pwd
);
4207 if (acb_info
& ACB_WSTRUST
)
4208 has_enough_rights
= user_has_privileges(p
->pipe_user
.nt_user_token
,
4209 &se_machine_account
);
4210 else if (acb_info
& ACB_NORMAL
)
4211 has_enough_rights
= user_has_privileges(p
->pipe_user
.nt_user_token
,
4213 else if (acb_info
& (ACB_SVRTRUST
|ACB_DOMTRUST
)) {
4214 if (lp_enable_privileges()) {
4215 has_enough_rights
= nt_token_check_domain_rid(p
->pipe_user
.nt_user_token
,
4216 DOMAIN_GROUP_RID_ADMINS
);
4220 DEBUG(5, ("%s: %s does%s possess sufficient rights\n",
4222 uidtoname(p
->pipe_user
.ut
.uid
),
4223 has_enough_rights
? "" : " not"));
4225 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
4227 if (has_enough_rights
) {
4231 /* ok! user info levels (lots: see MSDEV help), off we go... */
4233 switch (switch_value
) {
4236 status
= set_user_info_7(p
->mem_ctx
,
4241 if (!set_user_info_16(&info
->info16
, pwd
)) {
4242 status
= NT_STATUS_ACCESS_DENIED
;
4247 /* Used by AS/U JRA. */
4248 if (!set_user_info_18(&info
->info18
, pwd
)) {
4249 status
= NT_STATUS_ACCESS_DENIED
;
4254 if (!set_user_info_20(&info
->info20
, pwd
)) {
4255 status
= NT_STATUS_ACCESS_DENIED
;
4260 status
= set_user_info_21(p
->mem_ctx
,
4261 &info
->info21
, pwd
);
4265 if (!p
->session_key
.length
) {
4266 status
= NT_STATUS_NO_USER_SESSION_KEY
;
4268 SamOEMhashBlob(info
->info23
.password
.data
, 516,
4271 dump_data(100, info
->info23
.password
.data
, 516);
4273 status
= set_user_info_23(p
->mem_ctx
,
4274 &info
->info23
, pwd
);
4278 if (!p
->session_key
.length
) {
4279 status
= NT_STATUS_NO_USER_SESSION_KEY
;
4281 SamOEMhashBlob(info
->info24
.password
.data
,
4285 dump_data(100, info
->info24
.password
.data
, 516);
4287 if (!set_user_info_pw(info
->info24
.password
.data
, pwd
,
4289 status
= NT_STATUS_ACCESS_DENIED
;
4294 if (!p
->session_key
.length
) {
4295 status
= NT_STATUS_NO_USER_SESSION_KEY
;
4297 encode_or_decode_arc4_passwd_buffer(info
->info25
.password
.data
,
4300 dump_data(100, info
->info25
.password
.data
, 532);
4302 status
= set_user_info_25(p
->mem_ctx
,
4303 &info
->info25
, pwd
);
4304 if (!NT_STATUS_IS_OK(status
)) {
4307 if (!set_user_info_pw(info
->info25
.password
.data
, pwd
,
4309 status
= NT_STATUS_ACCESS_DENIED
;
4314 if (!p
->session_key
.length
) {
4315 status
= NT_STATUS_NO_USER_SESSION_KEY
;
4317 encode_or_decode_arc4_passwd_buffer(info
->info26
.password
.data
,
4320 dump_data(100, info
->info26
.password
.data
, 516);
4322 if (!set_user_info_pw(info
->info26
.password
.data
, pwd
,
4324 status
= NT_STATUS_ACCESS_DENIED
;
4329 status
= NT_STATUS_INVALID_INFO_CLASS
;
4334 if (has_enough_rights
) {
4338 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
4340 if (NT_STATUS_IS_OK(status
)) {
4341 force_flush_samr_cache(disp_info
);
4347 /*******************************************************************
4349 ********************************************************************/
4351 NTSTATUS
_samr_SetUserInfo(pipes_struct
*p
,
4352 struct samr_SetUserInfo
*r
)
4354 return samr_SetUserInfo_internal("_samr_SetUserInfo",
4361 /*******************************************************************
4363 ********************************************************************/
4365 NTSTATUS
_samr_SetUserInfo2(pipes_struct
*p
,
4366 struct samr_SetUserInfo2
*r
)
4368 return samr_SetUserInfo_internal("_samr_SetUserInfo2",
4375 /*********************************************************************
4376 _samr_GetAliasMembership
4377 *********************************************************************/
4379 NTSTATUS
_samr_GetAliasMembership(pipes_struct
*p
,
4380 struct samr_GetAliasMembership
*r
)
4382 size_t num_alias_rids
;
4384 struct samr_info
*info
= NULL
;
4392 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__
));
4394 /* find the policy handle. open a policy on it. */
4395 if (!find_policy_by_hnd(p
, r
->in
.domain_handle
, (void **)(void *)&info
))
4396 return NT_STATUS_INVALID_HANDLE
;
4398 ntstatus1
= access_check_samr_function(info
->acc_granted
,
4399 SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM
,
4400 "_samr_GetAliasMembership");
4401 ntstatus2
= access_check_samr_function(info
->acc_granted
,
4402 SA_RIGHT_DOMAIN_OPEN_ACCOUNT
,
4403 "_samr_GetAliasMembership");
4405 if (!NT_STATUS_IS_OK(ntstatus1
) || !NT_STATUS_IS_OK(ntstatus2
)) {
4406 if (!(NT_STATUS_EQUAL(ntstatus1
,NT_STATUS_ACCESS_DENIED
) && NT_STATUS_IS_OK(ntstatus2
)) &&
4407 !(NT_STATUS_EQUAL(ntstatus1
,NT_STATUS_ACCESS_DENIED
) && NT_STATUS_IS_OK(ntstatus1
))) {
4408 return (NT_STATUS_IS_OK(ntstatus1
)) ? ntstatus2
: ntstatus1
;
4412 if (!sid_check_is_domain(&info
->sid
) &&
4413 !sid_check_is_builtin(&info
->sid
))
4414 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
4416 if (r
->in
.sids
->num_sids
) {
4417 members
= TALLOC_ARRAY(p
->mem_ctx
, DOM_SID
, r
->in
.sids
->num_sids
);
4419 if (members
== NULL
)
4420 return NT_STATUS_NO_MEMORY
;
4425 for (i
=0; i
<r
->in
.sids
->num_sids
; i
++)
4426 sid_copy(&members
[i
], r
->in
.sids
->sids
[i
].sid
);
4432 ntstatus1
= pdb_enum_alias_memberships(p
->mem_ctx
, &info
->sid
, members
,
4433 r
->in
.sids
->num_sids
,
4434 &alias_rids
, &num_alias_rids
);
4437 if (!NT_STATUS_IS_OK(ntstatus1
)) {
4441 r
->out
.rids
->count
= num_alias_rids
;
4442 r
->out
.rids
->ids
= alias_rids
;
4444 return NT_STATUS_OK
;
4447 /*********************************************************************
4448 _samr_GetMembersInAlias
4449 *********************************************************************/
4451 NTSTATUS
_samr_GetMembersInAlias(pipes_struct
*p
,
4452 struct samr_GetMembersInAlias
*r
)
4456 size_t num_sids
= 0;
4457 struct lsa_SidPtr
*sids
= NULL
;
4458 DOM_SID
*pdb_sids
= NULL
;
4464 /* find the policy handle. open a policy on it. */
4465 if (!get_lsa_policy_samr_sid(p
, r
->in
.alias_handle
, &alias_sid
, &acc_granted
, NULL
))
4466 return NT_STATUS_INVALID_HANDLE
;
4468 status
= access_check_samr_function(acc_granted
,
4469 SA_RIGHT_ALIAS_GET_MEMBERS
,
4470 "_samr_GetMembersInAlias");
4471 if (!NT_STATUS_IS_OK(status
)) {
4475 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid
)));
4478 status
= pdb_enum_aliasmem(&alias_sid
, &pdb_sids
, &num_sids
);
4481 if (!NT_STATUS_IS_OK(status
)) {
4486 sids
= TALLOC_ZERO_ARRAY(p
->mem_ctx
, struct lsa_SidPtr
, num_sids
);
4488 TALLOC_FREE(pdb_sids
);
4489 return NT_STATUS_NO_MEMORY
;
4493 for (i
= 0; i
< num_sids
; i
++) {
4494 sids
[i
].sid
= sid_dup_talloc(p
->mem_ctx
, &pdb_sids
[i
]);
4496 TALLOC_FREE(pdb_sids
);
4497 return NT_STATUS_NO_MEMORY
;
4501 r
->out
.sids
->num_sids
= num_sids
;
4502 r
->out
.sids
->sids
= sids
;
4504 TALLOC_FREE(pdb_sids
);
4506 return NT_STATUS_OK
;
4509 /*********************************************************************
4510 _samr_QueryGroupMember
4511 *********************************************************************/
4513 NTSTATUS
_samr_QueryGroupMember(pipes_struct
*p
,
4514 struct samr_QueryGroupMember
*r
)
4517 size_t i
, num_members
;
4525 struct samr_RidTypeArray
*rids
= NULL
;
4527 rids
= TALLOC_ZERO_P(p
->mem_ctx
, struct samr_RidTypeArray
);
4529 return NT_STATUS_NO_MEMORY
;
4532 /* find the policy handle. open a policy on it. */
4533 if (!get_lsa_policy_samr_sid(p
, r
->in
.group_handle
, &group_sid
, &acc_granted
, NULL
))
4534 return NT_STATUS_INVALID_HANDLE
;
4536 status
= access_check_samr_function(acc_granted
,
4537 SA_RIGHT_GROUP_GET_MEMBERS
,
4538 "_samr_QueryGroupMember");
4539 if (!NT_STATUS_IS_OK(status
)) {
4543 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid
)));
4545 if (!sid_check_is_in_our_domain(&group_sid
)) {
4546 DEBUG(3, ("sid %s is not in our domain\n",
4547 sid_string_dbg(&group_sid
)));
4548 return NT_STATUS_NO_SUCH_GROUP
;
4551 DEBUG(10, ("lookup on Domain SID\n"));
4554 status
= pdb_enum_group_members(p
->mem_ctx
, &group_sid
,
4555 &rid
, &num_members
);
4558 if (!NT_STATUS_IS_OK(status
))
4562 attr
=TALLOC_ZERO_ARRAY(p
->mem_ctx
, uint32
, num_members
);
4564 return NT_STATUS_NO_MEMORY
;
4570 for (i
=0; i
<num_members
; i
++)
4571 attr
[i
] = SID_NAME_USER
;
4573 rids
->count
= num_members
;
4577 *r
->out
.rids
= rids
;
4579 return NT_STATUS_OK
;
4582 /*********************************************************************
4583 _samr_AddAliasMember
4584 *********************************************************************/
4586 NTSTATUS
_samr_AddAliasMember(pipes_struct
*p
,
4587 struct samr_AddAliasMember
*r
)
4592 bool can_add_accounts
;
4594 DISP_INFO
*disp_info
= NULL
;
4596 /* Find the policy handle. Open a policy on it. */
4597 if (!get_lsa_policy_samr_sid(p
, r
->in
.alias_handle
, &alias_sid
, &acc_granted
, &disp_info
))
4598 return NT_STATUS_INVALID_HANDLE
;
4600 status
= access_check_samr_function(acc_granted
,
4601 SA_RIGHT_ALIAS_ADD_MEMBER
,
4602 "_samr_AddAliasMember");
4603 if (!NT_STATUS_IS_OK(status
)) {
4607 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid
)));
4609 se_priv_copy( &se_rights
, &se_add_users
);
4610 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_rights
);
4612 /******** BEGIN SeAddUsers BLOCK *********/
4614 if ( can_add_accounts
)
4617 status
= pdb_add_aliasmem(&alias_sid
, r
->in
.sid
);
4619 if ( can_add_accounts
)
4622 /******** END SeAddUsers BLOCK *********/
4624 if (NT_STATUS_IS_OK(status
)) {
4625 force_flush_samr_cache(disp_info
);
4631 /*********************************************************************
4632 _samr_DeleteAliasMember
4633 *********************************************************************/
4635 NTSTATUS
_samr_DeleteAliasMember(pipes_struct
*p
,
4636 struct samr_DeleteAliasMember
*r
)
4641 bool can_add_accounts
;
4643 DISP_INFO
*disp_info
= NULL
;
4645 /* Find the policy handle. Open a policy on it. */
4646 if (!get_lsa_policy_samr_sid(p
, r
->in
.alias_handle
, &alias_sid
, &acc_granted
, &disp_info
))
4647 return NT_STATUS_INVALID_HANDLE
;
4649 status
= access_check_samr_function(acc_granted
,
4650 SA_RIGHT_ALIAS_REMOVE_MEMBER
,
4651 "_samr_DeleteAliasMember");
4652 if (!NT_STATUS_IS_OK(status
)) {
4656 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
4657 sid_string_dbg(&alias_sid
)));
4659 se_priv_copy( &se_rights
, &se_add_users
);
4660 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_rights
);
4662 /******** BEGIN SeAddUsers BLOCK *********/
4664 if ( can_add_accounts
)
4667 status
= pdb_del_aliasmem(&alias_sid
, r
->in
.sid
);
4669 if ( can_add_accounts
)
4672 /******** END SeAddUsers BLOCK *********/
4674 if (NT_STATUS_IS_OK(status
)) {
4675 force_flush_samr_cache(disp_info
);
4681 /*********************************************************************
4682 _samr_AddGroupMember
4683 *********************************************************************/
4685 NTSTATUS
_samr_AddGroupMember(pipes_struct
*p
,
4686 struct samr_AddGroupMember
*r
)
4693 bool can_add_accounts
;
4694 DISP_INFO
*disp_info
= NULL
;
4696 /* Find the policy handle. Open a policy on it. */
4697 if (!get_lsa_policy_samr_sid(p
, r
->in
.group_handle
, &group_sid
, &acc_granted
, &disp_info
))
4698 return NT_STATUS_INVALID_HANDLE
;
4700 status
= access_check_samr_function(acc_granted
,
4701 SA_RIGHT_GROUP_ADD_MEMBER
,
4702 "_samr_AddGroupMember");
4703 if (!NT_STATUS_IS_OK(status
)) {
4707 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid
)));
4709 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid
,
4711 return NT_STATUS_INVALID_HANDLE
;
4714 se_priv_copy( &se_rights
, &se_add_users
);
4715 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_rights
);
4717 /******** BEGIN SeAddUsers BLOCK *********/
4719 if ( can_add_accounts
)
4722 status
= pdb_add_groupmem(p
->mem_ctx
, group_rid
, r
->in
.rid
);
4724 if ( can_add_accounts
)
4727 /******** END SeAddUsers BLOCK *********/
4729 force_flush_samr_cache(disp_info
);
4734 /*********************************************************************
4735 _samr_DeleteGroupMember
4736 *********************************************************************/
4738 NTSTATUS
_samr_DeleteGroupMember(pipes_struct
*p
,
4739 struct samr_DeleteGroupMember
*r
)
4747 bool can_add_accounts
;
4748 DISP_INFO
*disp_info
= NULL
;
4751 * delete the group member named r->in.rid
4752 * who is a member of the sid associated with the handle
4753 * the rid is a user's rid as the group is a domain group.
4756 /* Find the policy handle. Open a policy on it. */
4757 if (!get_lsa_policy_samr_sid(p
, r
->in
.group_handle
, &group_sid
, &acc_granted
, &disp_info
))
4758 return NT_STATUS_INVALID_HANDLE
;
4760 status
= access_check_samr_function(acc_granted
,
4761 SA_RIGHT_GROUP_REMOVE_MEMBER
,
4762 "_samr_DeleteGroupMember");
4763 if (!NT_STATUS_IS_OK(status
)) {
4767 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid
,
4769 return NT_STATUS_INVALID_HANDLE
;
4772 se_priv_copy( &se_rights
, &se_add_users
);
4773 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_rights
);
4775 /******** BEGIN SeAddUsers BLOCK *********/
4777 if ( can_add_accounts
)
4780 status
= pdb_del_groupmem(p
->mem_ctx
, group_rid
, r
->in
.rid
);
4782 if ( can_add_accounts
)
4785 /******** END SeAddUsers BLOCK *********/
4787 force_flush_samr_cache(disp_info
);
4792 /*********************************************************************
4794 *********************************************************************/
4796 NTSTATUS
_samr_DeleteUser(pipes_struct
*p
,
4797 struct samr_DeleteUser
*r
)
4801 struct samu
*sam_pass
=NULL
;
4803 bool can_add_accounts
;
4805 DISP_INFO
*disp_info
= NULL
;
4808 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__
));
4810 /* Find the policy handle. Open a policy on it. */
4811 if (!get_lsa_policy_samr_sid(p
, r
->in
.user_handle
, &user_sid
, &acc_granted
, &disp_info
))
4812 return NT_STATUS_INVALID_HANDLE
;
4814 status
= access_check_samr_function(acc_granted
,
4815 STD_RIGHT_DELETE_ACCESS
,
4816 "_samr_DeleteUser");
4817 if (!NT_STATUS_IS_OK(status
)) {
4821 if (!sid_check_is_in_our_domain(&user_sid
))
4822 return NT_STATUS_CANNOT_DELETE
;
4824 /* check if the user exists before trying to delete */
4825 if ( !(sam_pass
= samu_new( NULL
)) ) {
4826 return NT_STATUS_NO_MEMORY
;
4830 ret
= pdb_getsampwsid(sam_pass
, &user_sid
);
4834 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
4835 sid_string_dbg(&user_sid
)));
4836 TALLOC_FREE(sam_pass
);
4837 return NT_STATUS_NO_SUCH_USER
;
4840 acb_info
= pdb_get_acct_ctrl(sam_pass
);
4842 /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
4843 if ( acb_info
& ACB_WSTRUST
) {
4844 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_machine_account
);
4846 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_add_users
);
4849 /******** BEGIN SeAddUsers BLOCK *********/
4851 if ( can_add_accounts
)
4854 status
= pdb_delete_user(p
->mem_ctx
, sam_pass
);
4856 if ( can_add_accounts
)
4859 /******** END SeAddUsers BLOCK *********/
4861 if ( !NT_STATUS_IS_OK(status
) ) {
4862 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
4863 "user %s: %s.\n", pdb_get_username(sam_pass
),
4864 nt_errstr(status
)));
4865 TALLOC_FREE(sam_pass
);
4870 TALLOC_FREE(sam_pass
);
4872 if (!close_policy_hnd(p
, r
->in
.user_handle
))
4873 return NT_STATUS_OBJECT_NAME_INVALID
;
4875 ZERO_STRUCTP(r
->out
.user_handle
);
4877 force_flush_samr_cache(disp_info
);
4879 return NT_STATUS_OK
;
4882 /*********************************************************************
4883 _samr_DeleteDomainGroup
4884 *********************************************************************/
4886 NTSTATUS
_samr_DeleteDomainGroup(pipes_struct
*p
,
4887 struct samr_DeleteDomainGroup
*r
)
4894 bool can_add_accounts
;
4895 DISP_INFO
*disp_info
= NULL
;
4897 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__
));
4899 /* Find the policy handle. Open a policy on it. */
4900 if (!get_lsa_policy_samr_sid(p
, r
->in
.group_handle
, &group_sid
, &acc_granted
, &disp_info
))
4901 return NT_STATUS_INVALID_HANDLE
;
4903 status
= access_check_samr_function(acc_granted
,
4904 STD_RIGHT_DELETE_ACCESS
,
4905 "_samr_DeleteDomainGroup");
4906 if (!NT_STATUS_IS_OK(status
)) {
4910 DEBUG(10, ("sid is %s\n", sid_string_dbg(&group_sid
)));
4912 if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid
,
4914 return NT_STATUS_NO_SUCH_GROUP
;
4917 se_priv_copy( &se_rights
, &se_add_users
);
4918 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_rights
);
4920 /******** BEGIN SeAddUsers BLOCK *********/
4922 if ( can_add_accounts
)
4925 status
= pdb_delete_dom_group(p
->mem_ctx
, group_rid
);
4927 if ( can_add_accounts
)
4930 /******** END SeAddUsers BLOCK *********/
4932 if ( !NT_STATUS_IS_OK(status
) ) {
4933 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
4934 "entry for group %s: %s\n",
4935 sid_string_dbg(&group_sid
),
4936 nt_errstr(status
)));
4940 if (!close_policy_hnd(p
, r
->in
.group_handle
))
4941 return NT_STATUS_OBJECT_NAME_INVALID
;
4943 force_flush_samr_cache(disp_info
);
4945 return NT_STATUS_OK
;
4948 /*********************************************************************
4949 _samr_DeleteDomAlias
4950 *********************************************************************/
4952 NTSTATUS
_samr_DeleteDomAlias(pipes_struct
*p
,
4953 struct samr_DeleteDomAlias
*r
)
4958 bool can_add_accounts
;
4960 DISP_INFO
*disp_info
= NULL
;
4962 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__
));
4964 /* Find the policy handle. Open a policy on it. */
4965 if (!get_lsa_policy_samr_sid(p
, r
->in
.alias_handle
, &alias_sid
, &acc_granted
, &disp_info
))
4966 return NT_STATUS_INVALID_HANDLE
;
4968 /* copy the handle to the outgoing reply */
4970 memcpy(r
->out
.alias_handle
, r
->in
.alias_handle
, sizeof(r
->out
.alias_handle
));
4972 status
= access_check_samr_function(acc_granted
,
4973 STD_RIGHT_DELETE_ACCESS
,
4974 "_samr_DeleteDomAlias");
4975 if (!NT_STATUS_IS_OK(status
)) {
4979 DEBUG(10, ("sid is %s\n", sid_string_dbg(&alias_sid
)));
4981 /* Don't let Windows delete builtin groups */
4983 if ( sid_check_is_in_builtin( &alias_sid
) ) {
4984 return NT_STATUS_SPECIAL_ACCOUNT
;
4987 if (!sid_check_is_in_our_domain(&alias_sid
))
4988 return NT_STATUS_NO_SUCH_ALIAS
;
4990 DEBUG(10, ("lookup on Local SID\n"));
4992 se_priv_copy( &se_rights
, &se_add_users
);
4993 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_rights
);
4995 /******** BEGIN SeAddUsers BLOCK *********/
4997 if ( can_add_accounts
)
5000 /* Have passdb delete the alias */
5001 status
= pdb_delete_alias(&alias_sid
);
5003 if ( can_add_accounts
)
5006 /******** END SeAddUsers BLOCK *********/
5008 if ( !NT_STATUS_IS_OK(status
))
5011 if (!close_policy_hnd(p
, r
->in
.alias_handle
))
5012 return NT_STATUS_OBJECT_NAME_INVALID
;
5014 force_flush_samr_cache(disp_info
);
5016 return NT_STATUS_OK
;
5019 /*********************************************************************
5020 _samr_CreateDomainGroup
5021 *********************************************************************/
5023 NTSTATUS
_samr_CreateDomainGroup(pipes_struct
*p
,
5024 struct samr_CreateDomainGroup
*r
)
5031 struct samr_info
*info
;
5034 bool can_add_accounts
;
5035 DISP_INFO
*disp_info
= NULL
;
5037 /* Find the policy handle. Open a policy on it. */
5038 if (!get_lsa_policy_samr_sid(p
, r
->in
.domain_handle
, &dom_sid
, &acc_granted
, &disp_info
))
5039 return NT_STATUS_INVALID_HANDLE
;
5041 status
= access_check_samr_function(acc_granted
,
5042 SA_RIGHT_DOMAIN_CREATE_GROUP
,
5043 "_samr_CreateDomainGroup");
5044 if (!NT_STATUS_IS_OK(status
)) {
5048 if (!sid_equal(&dom_sid
, get_global_sam_sid()))
5049 return NT_STATUS_ACCESS_DENIED
;
5051 name
= r
->in
.name
->string
;
5053 return NT_STATUS_NO_MEMORY
;
5056 status
= can_create(p
->mem_ctx
, name
);
5057 if (!NT_STATUS_IS_OK(status
)) {
5061 se_priv_copy( &se_rights
, &se_add_users
);
5062 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_rights
);
5064 /******** BEGIN SeAddUsers BLOCK *********/
5066 if ( can_add_accounts
)
5069 /* check that we successfully create the UNIX group */
5071 status
= pdb_create_dom_group(p
->mem_ctx
, name
, r
->out
.rid
);
5073 if ( can_add_accounts
)
5076 /******** END SeAddUsers BLOCK *********/
5078 /* check if we should bail out here */
5080 if ( !NT_STATUS_IS_OK(status
) )
5083 sid_compose(&info_sid
, get_global_sam_sid(), *r
->out
.rid
);
5085 if ((info
= get_samr_info_by_sid(&info_sid
)) == NULL
)
5086 return NT_STATUS_NO_MEMORY
;
5088 /* they created it; let the user do what he wants with it */
5090 info
->acc_granted
= GENERIC_RIGHTS_GROUP_ALL_ACCESS
;
5092 /* get a (unique) handle. open a policy on it. */
5093 if (!create_policy_hnd(p
, r
->out
.group_handle
, free_samr_info
, (void *)info
))
5094 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5096 force_flush_samr_cache(disp_info
);
5098 return NT_STATUS_OK
;
5101 /*********************************************************************
5102 _samr_CreateDomAlias
5103 *********************************************************************/
5105 NTSTATUS
_samr_CreateDomAlias(pipes_struct
*p
,
5106 struct samr_CreateDomAlias
*r
)
5110 const char *name
= NULL
;
5111 struct samr_info
*info
;
5116 bool can_add_accounts
;
5117 DISP_INFO
*disp_info
= NULL
;
5119 /* Find the policy handle. Open a policy on it. */
5120 if (!get_lsa_policy_samr_sid(p
, r
->in
.domain_handle
, &dom_sid
, &acc_granted
, &disp_info
))
5121 return NT_STATUS_INVALID_HANDLE
;
5123 result
= access_check_samr_function(acc_granted
,
5124 SA_RIGHT_DOMAIN_CREATE_ALIAS
,
5125 "_samr_CreateDomAlias");
5126 if (!NT_STATUS_IS_OK(result
)) {
5130 if (!sid_equal(&dom_sid
, get_global_sam_sid()))
5131 return NT_STATUS_ACCESS_DENIED
;
5133 name
= r
->in
.alias_name
->string
;
5135 se_priv_copy( &se_rights
, &se_add_users
);
5136 can_add_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_rights
);
5138 result
= can_create(p
->mem_ctx
, name
);
5139 if (!NT_STATUS_IS_OK(result
)) {
5143 /******** BEGIN SeAddUsers BLOCK *********/
5145 if ( can_add_accounts
)
5148 /* Have passdb create the alias */
5149 result
= pdb_create_alias(name
, r
->out
.rid
);
5151 if ( can_add_accounts
)
5154 /******** END SeAddUsers BLOCK *********/
5156 if (!NT_STATUS_IS_OK(result
)) {
5157 DEBUG(10, ("pdb_create_alias failed: %s\n",
5158 nt_errstr(result
)));
5162 sid_copy(&info_sid
, get_global_sam_sid());
5163 sid_append_rid(&info_sid
, *r
->out
.rid
);
5165 if (!sid_to_gid(&info_sid
, &gid
)) {
5166 DEBUG(10, ("Could not find alias just created\n"));
5167 return NT_STATUS_ACCESS_DENIED
;
5170 /* check if the group has been successfully created */
5171 if ( getgrgid(gid
) == NULL
) {
5172 DEBUG(10, ("getgrgid(%d) of just created alias failed\n",
5174 return NT_STATUS_ACCESS_DENIED
;
5177 if ((info
= get_samr_info_by_sid(&info_sid
)) == NULL
)
5178 return NT_STATUS_NO_MEMORY
;
5180 /* they created it; let the user do what he wants with it */
5182 info
->acc_granted
= GENERIC_RIGHTS_ALIAS_ALL_ACCESS
;
5184 /* get a (unique) handle. open a policy on it. */
5185 if (!create_policy_hnd(p
, r
->out
.alias_handle
, free_samr_info
, (void *)info
))
5186 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5188 force_flush_samr_cache(disp_info
);
5190 return NT_STATUS_OK
;
5193 /*********************************************************************
5194 _samr_QueryGroupInfo
5195 *********************************************************************/
5197 NTSTATUS
_samr_QueryGroupInfo(pipes_struct
*p
,
5198 struct samr_QueryGroupInfo
*r
)
5203 union samr_GroupInfo
*info
= NULL
;
5206 uint32_t attributes
= SE_GROUP_MANDATORY
|
5207 SE_GROUP_ENABLED_BY_DEFAULT
|
5209 const char *group_name
= NULL
;
5210 const char *group_description
= NULL
;
5212 if (!get_lsa_policy_samr_sid(p
, r
->in
.group_handle
, &group_sid
, &acc_granted
, NULL
))
5213 return NT_STATUS_INVALID_HANDLE
;
5215 status
= access_check_samr_function(acc_granted
,
5216 SA_RIGHT_GROUP_LOOKUP_INFO
,
5217 "_samr_QueryGroupInfo");
5218 if (!NT_STATUS_IS_OK(status
)) {
5223 ret
= get_domain_group_from_sid(group_sid
, &map
);
5226 return NT_STATUS_INVALID_HANDLE
;
5228 /* FIXME: map contains fstrings */
5229 group_name
= talloc_strdup(r
, map
.nt_name
);
5230 group_description
= talloc_strdup(r
, map
.comment
);
5232 info
= TALLOC_ZERO_P(p
->mem_ctx
, union samr_GroupInfo
);
5234 return NT_STATUS_NO_MEMORY
;
5237 switch (r
->in
.level
) {
5243 status
= pdb_enum_group_members(
5244 p
->mem_ctx
, &group_sid
, &members
, &num_members
);
5247 if (!NT_STATUS_IS_OK(status
)) {
5251 init_samr_group_info1(&info
->all
,
5259 init_samr_group_info2(&info
->name
,
5263 init_samr_group_info3(&info
->attributes
,
5267 init_samr_group_info4(&info
->description
,
5278 status = pdb_enum_group_members(
5279 p->mem_ctx, &group_sid, &members, &num_members);
5282 if (!NT_STATUS_IS_OK(status)) {
5286 init_samr_group_info5(&info
->all2
,
5289 0, /* num_members - in w2k3 this is always 0 */
5295 return NT_STATUS_INVALID_INFO_CLASS
;
5298 *r
->out
.info
= info
;
5300 return NT_STATUS_OK
;
5303 /*********************************************************************
5305 *********************************************************************/
5307 NTSTATUS
_samr_SetGroupInfo(pipes_struct
*p
,
5308 struct samr_SetGroupInfo
*r
)
5315 bool can_mod_accounts
;
5316 DISP_INFO
*disp_info
= NULL
;
5318 if (!get_lsa_policy_samr_sid(p
, r
->in
.group_handle
, &group_sid
, &acc_granted
, &disp_info
))
5319 return NT_STATUS_INVALID_HANDLE
;
5321 status
= access_check_samr_function(acc_granted
,
5322 SA_RIGHT_GROUP_SET_INFO
,
5323 "_samr_SetGroupInfo");
5324 if (!NT_STATUS_IS_OK(status
)) {
5329 ret
= get_domain_group_from_sid(group_sid
, &map
);
5332 return NT_STATUS_NO_SUCH_GROUP
;
5334 switch (r
->in
.level
) {
5336 fstrcpy(map
.comment
, r
->in
.info
->all
.description
.string
);
5339 fstrcpy(map
.comment
, r
->in
.info
->description
.string
);
5342 return NT_STATUS_INVALID_INFO_CLASS
;
5345 can_mod_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_add_users
);
5347 /******** BEGIN SeAddUsers BLOCK *********/
5349 if ( can_mod_accounts
)
5352 status
= pdb_update_group_mapping_entry(&map
);
5354 if ( can_mod_accounts
)
5357 /******** End SeAddUsers BLOCK *********/
5359 if (NT_STATUS_IS_OK(status
)) {
5360 force_flush_samr_cache(disp_info
);
5366 /*********************************************************************
5368 *********************************************************************/
5370 NTSTATUS
_samr_SetAliasInfo(pipes_struct
*p
,
5371 struct samr_SetAliasInfo
*r
)
5374 struct acct_info info
;
5376 bool can_mod_accounts
;
5378 DISP_INFO
*disp_info
= NULL
;
5380 if (!get_lsa_policy_samr_sid(p
, r
->in
.alias_handle
, &group_sid
, &acc_granted
, &disp_info
))
5381 return NT_STATUS_INVALID_HANDLE
;
5383 status
= access_check_samr_function(acc_granted
,
5384 SA_RIGHT_ALIAS_SET_INFO
,
5385 "_samr_SetAliasInfo");
5386 if (!NT_STATUS_IS_OK(status
)) {
5390 /* get the current group information */
5393 status
= pdb_get_aliasinfo( &group_sid
, &info
);
5396 if ( !NT_STATUS_IS_OK(status
))
5399 switch (r
->in
.level
) {
5404 /* We currently do not support renaming groups in the
5405 the BUILTIN domain. Refer to util_builtin.c to understand
5406 why. The eventually needs to be fixed to be like Windows
5407 where you can rename builtin groups, just not delete them */
5409 if ( sid_check_is_in_builtin( &group_sid
) ) {
5410 return NT_STATUS_SPECIAL_ACCOUNT
;
5413 /* There has to be a valid name (and it has to be different) */
5415 if ( !r
->in
.info
->name
.string
)
5416 return NT_STATUS_INVALID_PARAMETER
;
5418 /* If the name is the same just reply "ok". Yes this
5419 doesn't allow you to change the case of a group name. */
5421 if ( strequal( r
->in
.info
->name
.string
, info
.acct_name
) )
5422 return NT_STATUS_OK
;
5424 fstrcpy( info
.acct_name
, r
->in
.info
->name
.string
);
5426 /* make sure the name doesn't already exist as a user
5429 fstr_sprintf( group_name
, "%s\\%s", global_myname(), info
.acct_name
);
5430 status
= can_create( p
->mem_ctx
, group_name
);
5431 if ( !NT_STATUS_IS_OK( status
) )
5435 case ALIASINFODESCRIPTION
:
5436 if (r
->in
.info
->description
.string
) {
5437 fstrcpy(info
.acct_desc
,
5438 r
->in
.info
->description
.string
);
5440 fstrcpy( info
.acct_desc
, "" );
5444 return NT_STATUS_INVALID_INFO_CLASS
;
5447 can_mod_accounts
= user_has_privileges( p
->pipe_user
.nt_user_token
, &se_add_users
);
5449 /******** BEGIN SeAddUsers BLOCK *********/
5451 if ( can_mod_accounts
)
5454 status
= pdb_set_aliasinfo( &group_sid
, &info
);
5456 if ( can_mod_accounts
)
5459 /******** End SeAddUsers BLOCK *********/
5461 if (NT_STATUS_IS_OK(status
))
5462 force_flush_samr_cache(disp_info
);
5467 /****************************************************************
5469 ****************************************************************/
5471 NTSTATUS
_samr_GetDomPwInfo(pipes_struct
*p
,
5472 struct samr_GetDomPwInfo
*r
)
5474 uint32_t min_password_length
= 0;
5475 uint32_t password_properties
= 0;
5477 /* Perform access check. Since this rpc does not require a
5478 policy handle it will not be caught by the access checks on
5479 SAMR_CONNECT or SAMR_CONNECT_ANON. */
5481 if (!pipe_access_check(p
)) {
5482 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
5483 return NT_STATUS_ACCESS_DENIED
;
5487 pdb_get_account_policy(AP_MIN_PASSWORD_LEN
,
5488 &min_password_length
);
5489 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS
,
5490 &password_properties
);
5493 if (lp_check_password_script() && *lp_check_password_script()) {
5494 password_properties
|= DOMAIN_PASSWORD_COMPLEX
;
5497 r
->out
.info
->min_password_length
= min_password_length
;
5498 r
->out
.info
->password_properties
= password_properties
;
5500 return NT_STATUS_OK
;
5503 /*********************************************************************
5505 *********************************************************************/
5507 NTSTATUS
_samr_OpenGroup(pipes_struct
*p
,
5508 struct samr_OpenGroup
*r
)
5514 struct samr_info
*info
;
5515 SEC_DESC
*psd
= NULL
;
5517 uint32 des_access
= r
->in
.access_mask
;
5524 if (!get_lsa_policy_samr_sid(p
, r
->in
.domain_handle
, &sid
, &acc_granted
, NULL
))
5525 return NT_STATUS_INVALID_HANDLE
;
5527 status
= access_check_samr_function(acc_granted
,
5528 SA_RIGHT_DOMAIN_OPEN_ACCOUNT
,
5531 if ( !NT_STATUS_IS_OK(status
) )
5534 /*check if access can be granted as requested by client. */
5535 map_max_allowed_access(p
->pipe_user
.nt_user_token
, &des_access
);
5537 make_samr_object_sd(p
->mem_ctx
, &psd
, &sd_size
, &grp_generic_mapping
, NULL
, 0);
5538 se_map_generic(&des_access
,&grp_generic_mapping
);
5540 se_priv_copy( &se_rights
, &se_add_users
);
5542 status
= access_check_samr_object(psd
, p
->pipe_user
.nt_user_token
,
5543 &se_rights
, GENERIC_RIGHTS_GROUP_WRITE
, des_access
,
5544 &acc_granted
, "_samr_OpenGroup");
5546 if ( !NT_STATUS_IS_OK(status
) )
5549 /* this should not be hard-coded like this */
5551 if (!sid_equal(&sid
, get_global_sam_sid()))
5552 return NT_STATUS_ACCESS_DENIED
;
5554 sid_copy(&info_sid
, get_global_sam_sid());
5555 sid_append_rid(&info_sid
, r
->in
.rid
);
5556 sid_to_fstring(sid_string
, &info_sid
);
5558 if ((info
= get_samr_info_by_sid(&info_sid
)) == NULL
)
5559 return NT_STATUS_NO_MEMORY
;
5561 info
->acc_granted
= acc_granted
;
5563 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n", sid_string
));
5565 /* check if that group really exists */
5567 ret
= get_domain_group_from_sid(info
->sid
, &map
);
5570 return NT_STATUS_NO_SUCH_GROUP
;
5572 /* get a (unique) handle. open a policy on it. */
5573 if (!create_policy_hnd(p
, r
->out
.group_handle
, free_samr_info
, (void *)info
))
5574 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5576 return NT_STATUS_OK
;
5579 /*********************************************************************
5580 _samr_RemoveMemberFromForeignDomain
5581 *********************************************************************/
5583 NTSTATUS
_samr_RemoveMemberFromForeignDomain(pipes_struct
*p
,
5584 struct samr_RemoveMemberFromForeignDomain
*r
)
5586 DOM_SID delete_sid
, domain_sid
;
5589 DISP_INFO
*disp_info
= NULL
;
5591 sid_copy( &delete_sid
, r
->in
.sid
);
5593 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
5594 sid_string_dbg(&delete_sid
)));
5596 /* Find the policy handle. Open a policy on it. */
5598 if (!get_lsa_policy_samr_sid(p
, r
->in
.domain_handle
, &domain_sid
,
5599 &acc_granted
, &disp_info
))
5600 return NT_STATUS_INVALID_HANDLE
;
5602 result
= access_check_samr_function(acc_granted
,
5603 STD_RIGHT_DELETE_ACCESS
,
5604 "_samr_RemoveMemberFromForeignDomain");
5606 if (!NT_STATUS_IS_OK(result
))
5609 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
5610 sid_string_dbg(&domain_sid
)));
5612 /* we can only delete a user from a group since we don't have
5613 nested groups anyways. So in the latter case, just say OK */
5615 /* TODO: The above comment nowadays is bogus. Since we have nested
5616 * groups now, and aliases members are never reported out of the unix
5617 * group membership, the "just say OK" makes this call a no-op. For
5618 * us. This needs fixing however. */
5620 /* I've only ever seen this in the wild when deleting a user from
5621 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
5622 * is the user about to be deleted. I very much suspect this is the
5623 * only application of this call. To verify this, let people report
5626 if (!sid_check_is_builtin(&domain_sid
)) {
5627 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
5628 "global_sam_sid() = %s\n",
5629 sid_string_dbg(&domain_sid
),
5630 sid_string_dbg(get_global_sam_sid())));
5631 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
5632 return NT_STATUS_OK
;
5635 force_flush_samr_cache(disp_info
);
5637 result
= NT_STATUS_OK
;
5642 /*******************************************************************
5643 _samr_QueryDomainInfo2
5644 ********************************************************************/
5646 NTSTATUS
_samr_QueryDomainInfo2(pipes_struct
*p
,
5647 struct samr_QueryDomainInfo2
*r
)
5649 return samr_QueryDomainInfo_internal("_samr_QueryDomainInfo2",
5651 r
->in
.domain_handle
,
5656 /*******************************************************************
5658 ********************************************************************/
5660 NTSTATUS
_samr_SetDomainInfo(pipes_struct
*p
,
5661 struct samr_SetDomainInfo
*r
)
5663 time_t u_expire
, u_min_age
;
5665 time_t u_lock_duration
, u_reset_time
;
5667 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__
));
5669 /* find the policy handle. open a policy on it. */
5670 if (!find_policy_by_hnd(p
, r
->in
.domain_handle
, NULL
))
5671 return NT_STATUS_INVALID_HANDLE
;
5673 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r
->in
.level
));
5675 switch (r
->in
.level
) {
5677 u_expire
=nt_time_to_unix_abs((NTTIME
*)&r
->in
.info
->info1
.max_password_age
);
5678 u_min_age
=nt_time_to_unix_abs((NTTIME
*)&r
->in
.info
->info1
.min_password_age
);
5679 pdb_set_account_policy(AP_MIN_PASSWORD_LEN
, (uint32
)r
->in
.info
->info1
.min_password_length
);
5680 pdb_set_account_policy(AP_PASSWORD_HISTORY
, (uint32
)r
->in
.info
->info1
.password_history_length
);
5681 pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS
, (uint32
)r
->in
.info
->info1
.password_properties
);
5682 pdb_set_account_policy(AP_MAX_PASSWORD_AGE
, (int)u_expire
);
5683 pdb_set_account_policy(AP_MIN_PASSWORD_AGE
, (int)u_min_age
);
5688 u_logout
=nt_time_to_unix_abs((NTTIME
*)&r
->in
.info
->info3
.force_logoff_time
);
5689 pdb_set_account_policy(AP_TIME_TO_LOGOUT
, (int)u_logout
);
5698 u_lock_duration
=nt_time_to_unix_abs((NTTIME
*)&r
->in
.info
->info12
.lockout_duration
);
5699 if (u_lock_duration
!= -1)
5700 u_lock_duration
/= 60;
5702 u_reset_time
=nt_time_to_unix_abs((NTTIME
*)&r
->in
.info
->info12
.lockout_window
)/60;
5704 pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION
, (int)u_lock_duration
);
5705 pdb_set_account_policy(AP_RESET_COUNT_TIME
, (int)u_reset_time
);
5706 pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT
, (uint32
)r
->in
.info
->info12
.lockout_threshold
);
5709 return NT_STATUS_INVALID_INFO_CLASS
;
5712 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__
));
5714 return NT_STATUS_OK
;
5717 /****************************************************************
5718 _samr_GetDisplayEnumerationIndex
5719 ****************************************************************/
5721 NTSTATUS
_samr_GetDisplayEnumerationIndex(pipes_struct
*p
,
5722 struct samr_GetDisplayEnumerationIndex
*r
)
5724 struct samr_info
*info
= NULL
;
5725 uint32_t max_entries
= (uint32_t) -1;
5726 uint32_t enum_context
= 0;
5728 uint32_t num_account
= 0;
5729 struct samr_displayentry
*entries
= NULL
;
5731 DEBUG(5,("_samr_GetDisplayEnumerationIndex: %d\n", __LINE__
));
5733 /* find the policy handle. open a policy on it. */
5734 if (!find_policy_by_hnd(p
, r
->in
.domain_handle
, (void **)(void *)&info
)) {
5735 return NT_STATUS_INVALID_HANDLE
;
5738 if ((r
->in
.level
< 1) || (r
->in
.level
> 3)) {
5739 DEBUG(0,("_samr_GetDisplayEnumerationIndex: "
5740 "Unknown info level (%u)\n",
5742 return NT_STATUS_INVALID_INFO_CLASS
;
5747 /* The following done as ROOT. Don't return without unbecome_root(). */
5749 switch (r
->in
.level
) {
5751 if (info
->disp_info
->users
== NULL
) {
5752 info
->disp_info
->users
= pdb_search_users(ACB_NORMAL
);
5753 if (info
->disp_info
->users
== NULL
) {
5755 return NT_STATUS_ACCESS_DENIED
;
5757 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5758 "starting user enumeration at index %u\n",
5759 (unsigned int)enum_context
));
5761 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5762 "using cached user enumeration at index %u\n",
5763 (unsigned int)enum_context
));
5765 num_account
= pdb_search_entries(info
->disp_info
->users
,
5766 enum_context
, max_entries
,
5770 if (info
->disp_info
->machines
== NULL
) {
5771 info
->disp_info
->machines
=
5772 pdb_search_users(ACB_WSTRUST
|ACB_SVRTRUST
);
5773 if (info
->disp_info
->machines
== NULL
) {
5775 return NT_STATUS_ACCESS_DENIED
;
5777 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5778 "starting machine enumeration at index %u\n",
5779 (unsigned int)enum_context
));
5781 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5782 "using cached machine enumeration at index %u\n",
5783 (unsigned int)enum_context
));
5785 num_account
= pdb_search_entries(info
->disp_info
->machines
,
5786 enum_context
, max_entries
,
5790 if (info
->disp_info
->groups
== NULL
) {
5791 info
->disp_info
->groups
= pdb_search_groups();
5792 if (info
->disp_info
->groups
== NULL
) {
5794 return NT_STATUS_ACCESS_DENIED
;
5796 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5797 "starting group enumeration at index %u\n",
5798 (unsigned int)enum_context
));
5800 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5801 "using cached group enumeration at index %u\n",
5802 (unsigned int)enum_context
));
5804 num_account
= pdb_search_entries(info
->disp_info
->groups
,
5805 enum_context
, max_entries
,
5810 smb_panic("info class changed");
5816 /* Ensure we cache this enumeration. */
5817 set_disp_info_cache_timeout(info
->disp_info
, DISP_INFO_CACHE_TIMEOUT
);
5819 DEBUG(10,("_samr_GetDisplayEnumerationIndex: looking for :%s\n",
5820 r
->in
.name
->string
));
5822 for (i
=0; i
<num_account
; i
++) {
5823 if (strequal(entries
[i
].account_name
, r
->in
.name
->string
)) {
5824 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5825 "found %s at idx %d\n",
5826 r
->in
.name
->string
, i
));
5828 return NT_STATUS_OK
;
5832 /* assuming account_name lives at the very end */
5833 *r
->out
.idx
= num_account
;
5835 return NT_STATUS_NO_MORE_ENTRIES
;
5838 /****************************************************************
5839 _samr_GetDisplayEnumerationIndex2
5840 ****************************************************************/
5842 NTSTATUS
_samr_GetDisplayEnumerationIndex2(pipes_struct
*p
,
5843 struct samr_GetDisplayEnumerationIndex2
*r
)
5845 struct samr_GetDisplayEnumerationIndex q
;
5847 q
.in
.domain_handle
= r
->in
.domain_handle
;
5848 q
.in
.level
= r
->in
.level
;
5849 q
.in
.name
= r
->in
.name
;
5851 q
.out
.idx
= r
->out
.idx
;
5853 return _samr_GetDisplayEnumerationIndex(p
, &q
);
5856 /****************************************************************
5857 ****************************************************************/
5859 NTSTATUS
_samr_Shutdown(pipes_struct
*p
,
5860 struct samr_Shutdown
*r
)
5862 p
->rng_fault_state
= true;
5863 return NT_STATUS_NOT_IMPLEMENTED
;
5866 /****************************************************************
5867 ****************************************************************/
5869 NTSTATUS
_samr_CreateUser(pipes_struct
*p
,
5870 struct samr_CreateUser
*r
)
5872 p
->rng_fault_state
= true;
5873 return NT_STATUS_NOT_IMPLEMENTED
;
5876 /****************************************************************
5877 ****************************************************************/
5879 NTSTATUS
_samr_SetMemberAttributesOfGroup(pipes_struct
*p
,
5880 struct samr_SetMemberAttributesOfGroup
*r
)
5882 p
->rng_fault_state
= true;
5883 return NT_STATUS_NOT_IMPLEMENTED
;
5886 /****************************************************************
5887 ****************************************************************/
5889 NTSTATUS
_samr_ChangePasswordUser(pipes_struct
*p
,
5890 struct samr_ChangePasswordUser
*r
)
5892 p
->rng_fault_state
= true;
5893 return NT_STATUS_NOT_IMPLEMENTED
;
5896 /****************************************************************
5897 ****************************************************************/
5899 NTSTATUS
_samr_TestPrivateFunctionsDomain(pipes_struct
*p
,
5900 struct samr_TestPrivateFunctionsDomain
*r
)
5902 p
->rng_fault_state
= true;
5903 return NT_STATUS_NOT_IMPLEMENTED
;
5906 /****************************************************************
5907 ****************************************************************/
5909 NTSTATUS
_samr_TestPrivateFunctionsUser(pipes_struct
*p
,
5910 struct samr_TestPrivateFunctionsUser
*r
)
5912 p
->rng_fault_state
= true;
5913 return NT_STATUS_NOT_IMPLEMENTED
;
5916 /****************************************************************
5917 ****************************************************************/
5919 NTSTATUS
_samr_QueryUserInfo2(pipes_struct
*p
,
5920 struct samr_QueryUserInfo2
*r
)
5922 p
->rng_fault_state
= true;
5923 return NT_STATUS_NOT_IMPLEMENTED
;
5926 /****************************************************************
5927 ****************************************************************/
5929 NTSTATUS
_samr_AddMultipleMembersToAlias(pipes_struct
*p
,
5930 struct samr_AddMultipleMembersToAlias
*r
)
5932 p
->rng_fault_state
= true;
5933 return NT_STATUS_NOT_IMPLEMENTED
;
5936 /****************************************************************
5937 ****************************************************************/
5939 NTSTATUS
_samr_RemoveMultipleMembersFromAlias(pipes_struct
*p
,
5940 struct samr_RemoveMultipleMembersFromAlias
*r
)
5942 p
->rng_fault_state
= true;
5943 return NT_STATUS_NOT_IMPLEMENTED
;
5946 /****************************************************************
5947 ****************************************************************/
5949 NTSTATUS
_samr_OemChangePasswordUser2(pipes_struct
*p
,
5950 struct samr_OemChangePasswordUser2
*r
)
5952 p
->rng_fault_state
= true;
5953 return NT_STATUS_NOT_IMPLEMENTED
;
5956 /****************************************************************
5957 ****************************************************************/
5959 NTSTATUS
_samr_SetBootKeyInformation(pipes_struct
*p
,
5960 struct samr_SetBootKeyInformation
*r
)
5962 p
->rng_fault_state
= true;
5963 return NT_STATUS_NOT_IMPLEMENTED
;
5966 /****************************************************************
5967 ****************************************************************/
5969 NTSTATUS
_samr_GetBootKeyInformation(pipes_struct
*p
,
5970 struct samr_GetBootKeyInformation
*r
)
5972 p
->rng_fault_state
= true;
5973 return NT_STATUS_NOT_IMPLEMENTED
;
5976 /****************************************************************
5977 ****************************************************************/
5979 NTSTATUS
_samr_Connect3(pipes_struct
*p
,
5980 struct samr_Connect3
*r
)
5982 p
->rng_fault_state
= true;
5983 return NT_STATUS_NOT_IMPLEMENTED
;
5986 /****************************************************************
5987 ****************************************************************/
5989 NTSTATUS
_samr_RidToSid(pipes_struct
*p
,
5990 struct samr_RidToSid
*r
)
5992 p
->rng_fault_state
= true;
5993 return NT_STATUS_NOT_IMPLEMENTED
;
5996 /****************************************************************
5997 ****************************************************************/
5999 NTSTATUS
_samr_SetDsrmPassword(pipes_struct
*p
,
6000 struct samr_SetDsrmPassword
*r
)
6002 p
->rng_fault_state
= true;
6003 return NT_STATUS_NOT_IMPLEMENTED
;
6006 /****************************************************************
6007 ****************************************************************/
6009 NTSTATUS
_samr_ValidatePassword(pipes_struct
*p
,
6010 struct samr_ValidatePassword
*r
)
6012 p
->rng_fault_state
= true;
6013 return NT_STATUS_NOT_IMPLEMENTED
;