2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Paul Ashton 1997,
7 * Copyright (C) Marc Jacobsen 1999,
8 * Copyright (C) Jeremy Allison 2001-2008,
9 * Copyright (C) Jean François Micouleau 1998-2001,
10 * Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002,
11 * Copyright (C) Gerald (Jerry) Carter 2003-2004,
12 * Copyright (C) Simo Sorce 2003.
13 * Copyright (C) Volker Lendecke 2005.
14 * Copyright (C) Guenther Deschner 2008.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 * This is the implementation of the SAMR code.
35 #include "system/passwd.h"
36 #include "smbd/globals.h"
37 #include "../libcli/auth/libcli_auth.h"
38 #include "../librpc/gen_ndr/srv_samr.h"
39 #include "rpc_server/samr/srv_samr_util.h"
40 #include "../lib/crypto/arcfour.h"
42 #include "rpc_client/init_lsa.h"
43 #include "../libcli/security/security.h"
48 #define DBGC_CLASS DBGC_RPC_SRV
50 #define SAMR_USR_RIGHTS_WRITE_PW \
51 ( READ_CONTROL_ACCESS | \
52 SAMR_USER_ACCESS_CHANGE_PASSWORD | \
53 SAMR_USER_ACCESS_SET_LOC_COM)
54 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
55 ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
57 #define DISP_INFO_CACHE_TIMEOUT 10
59 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
60 #define MAX_SAM_ENTRIES_W95 50
62 struct samr_connect_info {
66 struct samr_domain_info {
68 struct disp_info *disp_info;
71 struct samr_user_info {
75 struct samr_group_info {
79 struct samr_alias_info {
83 typedef struct disp_info {
84 struct dom_sid sid; /* identify which domain this is. */
85 struct pdb_search *users; /* querydispinfo 1 and 4 */
86 struct pdb_search *machines; /* querydispinfo 2 */
87 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
88 struct pdb_search *aliases; /* enumaliases */
90 uint32_t enum_acb_mask;
91 struct pdb_search *enum_users; /* enumusers with a mask */
93 struct timed_event *cache_timeout_event; /* cache idle timeout
97 static const struct generic_mapping sam_generic_mapping = {
98 GENERIC_RIGHTS_SAM_READ,
99 GENERIC_RIGHTS_SAM_WRITE,
100 GENERIC_RIGHTS_SAM_EXECUTE,
101 GENERIC_RIGHTS_SAM_ALL_ACCESS};
102 static const struct generic_mapping dom_generic_mapping = {
103 GENERIC_RIGHTS_DOMAIN_READ,
104 GENERIC_RIGHTS_DOMAIN_WRITE,
105 GENERIC_RIGHTS_DOMAIN_EXECUTE,
106 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
107 static const struct generic_mapping usr_generic_mapping = {
108 GENERIC_RIGHTS_USER_READ,
109 GENERIC_RIGHTS_USER_WRITE,
110 GENERIC_RIGHTS_USER_EXECUTE,
111 GENERIC_RIGHTS_USER_ALL_ACCESS};
112 static const struct generic_mapping usr_nopwchange_generic_mapping = {
113 GENERIC_RIGHTS_USER_READ,
114 GENERIC_RIGHTS_USER_WRITE,
115 GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
116 GENERIC_RIGHTS_USER_ALL_ACCESS};
117 static const struct generic_mapping grp_generic_mapping = {
118 GENERIC_RIGHTS_GROUP_READ,
119 GENERIC_RIGHTS_GROUP_WRITE,
120 GENERIC_RIGHTS_GROUP_EXECUTE,
121 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
122 static const struct generic_mapping ali_generic_mapping = {
123 GENERIC_RIGHTS_ALIAS_READ,
124 GENERIC_RIGHTS_ALIAS_WRITE,
125 GENERIC_RIGHTS_ALIAS_EXECUTE,
126 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
128 /*******************************************************************
129 *******************************************************************/
131 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, struct security_descriptor **psd, size_t *sd_size,
132 const struct generic_mapping *map,
133 struct dom_sid *sid, uint32 sid_access )
135 struct dom_sid domadmin_sid;
136 struct security_ace ace[5]; /* at most 5 entries */
139 struct security_acl *psa = NULL;
141 /* basic access for Everyone */
143 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
144 map->generic_execute | map->generic_read, 0);
146 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
148 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
149 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
150 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
151 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
153 /* Add Full Access for Domain Admins if we are a DC */
156 sid_compose(&domadmin_sid, get_global_sam_sid(),
158 init_sec_ace(&ace[i++], &domadmin_sid,
159 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
162 /* if we have a sid, give it some special access */
165 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sid_access, 0);
168 /* create the security descriptor */
170 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
171 return NT_STATUS_NO_MEMORY;
173 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
174 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
175 psa, sd_size)) == NULL)
176 return NT_STATUS_NO_MEMORY;
181 /*******************************************************************
182 Checks if access to an object should be granted, and returns that
183 level of access for further checks.
185 If the user has either of needed_priv_1 or needed_priv_2 then they
186 get the rights in rights_mask in addition to any calulated rights.
188 This handles the unusual case where we need to allow two different
189 privileges to obtain exactly the same rights, which occours only in
191 ********************************************************************/
193 NTSTATUS access_check_object( struct security_descriptor *psd, struct security_token *token,
194 enum sec_privilege needed_priv_1, enum sec_privilege needed_priv_2,
196 uint32 des_access, uint32 *acc_granted,
199 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
200 uint32 saved_mask = 0;
202 /* check privileges; certain SAM access bits should be overridden
203 by privileges (mostly having to do with creating/modifying/deleting
206 if ((needed_priv_1 != SEC_PRIV_INVALID && security_token_has_privilege(token, needed_priv_1)) ||
207 (needed_priv_2 != SEC_PRIV_INVALID && security_token_has_privilege(token, needed_priv_2))) {
208 saved_mask = (des_access & rights_mask);
209 des_access &= ~saved_mask;
211 DEBUG(4,("access_check_object: user rights access mask [0x%x]\n",
216 /* check the security descriptor first */
218 status = se_access_check(psd, token, des_access, acc_granted);
219 if (NT_STATUS_IS_OK(status)) {
223 /* give root a free pass */
225 if ( geteuid() == sec_initial_uid() ) {
227 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
228 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
230 *acc_granted = des_access;
232 status = NT_STATUS_OK;
238 /* add in any bits saved during the privilege check (only
239 matters is status is ok) */
241 *acc_granted |= rights_mask;
243 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
244 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
245 des_access, *acc_granted));
251 /*******************************************************************
252 Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
253 ********************************************************************/
255 void map_max_allowed_access(const struct security_token *nt_token,
256 const struct security_unix_token *unix_token,
257 uint32_t *pacc_requested)
259 if (!((*pacc_requested) & MAXIMUM_ALLOWED_ACCESS)) {
262 *pacc_requested &= ~MAXIMUM_ALLOWED_ACCESS;
264 /* At least try for generic read|execute - Everyone gets that. */
265 *pacc_requested = GENERIC_READ_ACCESS|GENERIC_EXECUTE_ACCESS;
267 /* root gets anything. */
268 if (unix_token->uid == sec_initial_uid()) {
269 *pacc_requested |= GENERIC_ALL_ACCESS;
273 /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
275 if (security_token_has_sid(nt_token, &global_sid_Builtin_Administrators) ||
276 security_token_has_sid(nt_token, &global_sid_Builtin_Account_Operators)) {
277 *pacc_requested |= GENERIC_ALL_ACCESS;
281 /* Full access for DOMAIN\Domain Admins. */
283 struct dom_sid domadmin_sid;
284 sid_compose(&domadmin_sid, get_global_sam_sid(),
286 if (security_token_has_sid(nt_token, &domadmin_sid)) {
287 *pacc_requested |= GENERIC_ALL_ACCESS;
291 /* TODO ! Check privileges. */
294 /*******************************************************************
295 Fetch or create a dispinfo struct.
296 ********************************************************************/
298 static DISP_INFO *get_samr_dispinfo_by_sid(const struct dom_sid *psid)
301 * We do a static cache for DISP_INFO's here. Explanation can be found
302 * in Jeremy's checkin message to r11793:
304 * Fix the SAMR cache so it works across completely insane
305 * client behaviour (ie.:
306 * open pipe/open SAMR handle/enumerate 0 - 1024
307 * close SAMR handle, close pipe.
308 * open pipe/open SAMR handle/enumerate 1024 - 2048...
309 * close SAMR handle, close pipe.
310 * And on ad-nausium. Amazing.... probably object-oriented
311 * client side programming in action yet again.
312 * This change should *massively* improve performance when
313 * enumerating users from an LDAP database.
316 * "Our" and the builtin domain are the only ones where we ever
317 * enumerate stuff, so just cache 2 entries.
320 static struct disp_info *builtin_dispinfo;
321 static struct disp_info *domain_dispinfo;
323 /* There are two cases to consider here:
324 1) The SID is a domain SID and we look for an equality match, or
325 2) This is an account SID and so we return the DISP_INFO* for our
332 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
334 * Necessary only once, but it does not really hurt.
336 if (builtin_dispinfo == NULL) {
337 builtin_dispinfo = talloc_zero(NULL, struct disp_info);
338 if (builtin_dispinfo == NULL) {
342 sid_copy(&builtin_dispinfo->sid, &global_sid_Builtin);
344 return builtin_dispinfo;
347 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
349 * Necessary only once, but it does not really hurt.
351 if (domain_dispinfo == NULL) {
352 domain_dispinfo = talloc_zero(NULL, struct disp_info);
353 if (domain_dispinfo == NULL) {
357 sid_copy(&domain_dispinfo->sid, get_global_sam_sid());
359 return domain_dispinfo;
365 /*******************************************************************
366 Function to free the per SID data.
367 ********************************************************************/
369 static void free_samr_cache(DISP_INFO *disp_info)
371 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
372 sid_string_dbg(&disp_info->sid)));
374 /* We need to become root here because the paged search might have to
375 * tell the LDAP server we're not interested in the rest anymore. */
379 TALLOC_FREE(disp_info->users);
380 TALLOC_FREE(disp_info->machines);
381 TALLOC_FREE(disp_info->groups);
382 TALLOC_FREE(disp_info->aliases);
383 TALLOC_FREE(disp_info->enum_users);
388 /*******************************************************************
389 Idle event handler. Throw away the disp info cache.
390 ********************************************************************/
392 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
393 struct timed_event *te,
397 DISP_INFO *disp_info = (DISP_INFO *)private_data;
399 TALLOC_FREE(disp_info->cache_timeout_event);
401 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
403 free_samr_cache(disp_info);
406 /*******************************************************************
407 Setup cache removal idle event handler.
408 ********************************************************************/
410 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
412 /* Remove any pending timeout and update. */
414 TALLOC_FREE(disp_info->cache_timeout_event);
416 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
417 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
418 (unsigned int)secs_fromnow ));
420 disp_info->cache_timeout_event = event_add_timed(
421 server_event_context(), NULL,
422 timeval_current_ofs(secs_fromnow, 0),
423 disp_info_cache_idle_timeout_handler, (void *)disp_info);
426 /*******************************************************************
427 Force flush any cache. We do this on any samr_set_xxx call.
428 We must also remove the timeout handler.
429 ********************************************************************/
431 static void force_flush_samr_cache(const struct dom_sid *sid)
433 struct disp_info *disp_info = get_samr_dispinfo_by_sid(sid);
435 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
439 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
440 TALLOC_FREE(disp_info->cache_timeout_event);
441 free_samr_cache(disp_info);
444 /*******************************************************************
445 Ensure password info is never given out. Paranioa... JRA.
446 ********************************************************************/
448 static void samr_clear_sam_passwd(struct samu *sam_pass)
454 /* These now zero out the old password */
456 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
457 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
460 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
462 struct samr_displayentry *entry;
464 if (sid_check_is_builtin(&info->sid)) {
465 /* No users in builtin. */
469 if (info->users == NULL) {
470 info->users = pdb_search_users(info, acct_flags);
471 if (info->users == NULL) {
475 /* Fetch the last possible entry, thus trigger an enumeration */
476 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
478 /* Ensure we cache this enumeration. */
479 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
481 return info->users->num_entries;
484 static uint32 count_sam_groups(struct disp_info *info)
486 struct samr_displayentry *entry;
488 if (sid_check_is_builtin(&info->sid)) {
489 /* No groups in builtin. */
493 if (info->groups == NULL) {
494 info->groups = pdb_search_groups(info);
495 if (info->groups == NULL) {
499 /* Fetch the last possible entry, thus trigger an enumeration */
500 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
502 /* Ensure we cache this enumeration. */
503 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
505 return info->groups->num_entries;
508 static uint32 count_sam_aliases(struct disp_info *info)
510 struct samr_displayentry *entry;
512 if (info->aliases == NULL) {
513 info->aliases = pdb_search_aliases(info, &info->sid);
514 if (info->aliases == NULL) {
518 /* Fetch the last possible entry, thus trigger an enumeration */
519 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
521 /* Ensure we cache this enumeration. */
522 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
524 return info->aliases->num_entries;
527 /*******************************************************************
529 ********************************************************************/
531 NTSTATUS _samr_Close(struct pipes_struct *p, struct samr_Close *r)
533 if (!close_policy_hnd(p, r->in.handle)) {
534 return NT_STATUS_INVALID_HANDLE;
537 ZERO_STRUCTP(r->out.handle);
542 /*******************************************************************
544 ********************************************************************/
546 NTSTATUS _samr_OpenDomain(struct pipes_struct *p,
547 struct samr_OpenDomain *r)
549 struct samr_connect_info *cinfo;
550 struct samr_domain_info *dinfo;
551 struct security_descriptor *psd = NULL;
553 uint32 des_access = r->in.access_mask;
556 uint32_t extra_access = SAMR_DOMAIN_ACCESS_CREATE_USER;
558 /* find the connection policy handle. */
560 cinfo = policy_handle_find(p, r->in.connect_handle, 0, NULL,
561 struct samr_connect_info, &status);
562 if (!NT_STATUS_IS_OK(status)) {
566 /*check if access can be granted as requested by client. */
567 map_max_allowed_access(p->session_info->security_token,
568 &p->session_info->utok,
571 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
572 se_map_generic( &des_access, &dom_generic_mapping );
575 * Users with SeAddUser get the ability to manipulate groups
578 if (security_token_has_privilege(p->session_info->security_token, SEC_PRIV_ADD_USERS)) {
579 extra_access |= (SAMR_DOMAIN_ACCESS_CREATE_GROUP |
580 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS |
581 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT |
582 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS |
583 SAMR_DOMAIN_ACCESS_CREATE_ALIAS);
587 * Users with SeMachineAccount or SeAddUser get additional
588 * SAMR_DOMAIN_ACCESS_CREATE_USER access.
591 status = access_check_object( psd, p->session_info->security_token,
592 SEC_PRIV_MACHINE_ACCOUNT, SEC_PRIV_ADD_USERS,
593 extra_access, des_access,
594 &acc_granted, "_samr_OpenDomain" );
596 if ( !NT_STATUS_IS_OK(status) )
599 if (!sid_check_is_domain(r->in.sid) &&
600 !sid_check_is_builtin(r->in.sid)) {
601 return NT_STATUS_NO_SUCH_DOMAIN;
604 dinfo = policy_handle_create(p, r->out.domain_handle, acc_granted,
605 struct samr_domain_info, &status);
606 if (!NT_STATUS_IS_OK(status)) {
609 dinfo->sid = *r->in.sid;
610 dinfo->disp_info = get_samr_dispinfo_by_sid(r->in.sid);
612 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
617 /*******************************************************************
619 ********************************************************************/
621 NTSTATUS _samr_GetUserPwInfo(struct pipes_struct *p,
622 struct samr_GetUserPwInfo *r)
624 struct samr_user_info *uinfo;
625 enum lsa_SidType sid_type;
626 uint32_t min_password_length = 0;
627 uint32_t password_properties = 0;
631 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
633 uinfo = policy_handle_find(p, r->in.user_handle,
634 SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
635 struct samr_user_info, &status);
636 if (!NT_STATUS_IS_OK(status)) {
640 if (!sid_check_is_in_our_domain(&uinfo->sid)) {
641 return NT_STATUS_OBJECT_TYPE_MISMATCH;
645 ret = lookup_sid(p->mem_ctx, &uinfo->sid, NULL, NULL, &sid_type);
648 return NT_STATUS_NO_SUCH_USER;
654 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
655 &min_password_length);
656 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
657 &password_properties);
660 if (lp_check_password_script() && *lp_check_password_script()) {
661 password_properties |= DOMAIN_PASSWORD_COMPLEX;
669 r->out.info->min_password_length = min_password_length;
670 r->out.info->password_properties = password_properties;
672 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
677 /*******************************************************************
679 ********************************************************************/
681 NTSTATUS _samr_SetSecurity(struct pipes_struct *p,
682 struct samr_SetSecurity *r)
684 struct samr_user_info *uinfo;
686 struct security_acl *dacl;
688 struct samu *sampass=NULL;
691 uinfo = policy_handle_find(p, r->in.handle,
692 SAMR_USER_ACCESS_SET_ATTRIBUTES, NULL,
693 struct samr_user_info, &status);
694 if (!NT_STATUS_IS_OK(status)) {
698 if (!(sampass = samu_new( p->mem_ctx))) {
699 DEBUG(0,("No memory!\n"));
700 return NT_STATUS_NO_MEMORY;
703 /* get the user record */
705 ret = pdb_getsampwsid(sampass, &uinfo->sid);
709 DEBUG(4, ("User %s not found\n",
710 sid_string_dbg(&uinfo->sid)));
711 TALLOC_FREE(sampass);
712 return NT_STATUS_INVALID_HANDLE;
715 dacl = r->in.sdbuf->sd->dacl;
716 for (i=0; i < dacl->num_aces; i++) {
717 if (dom_sid_equal(&uinfo->sid, &dacl->aces[i].trustee)) {
718 ret = pdb_set_pass_can_change(sampass,
719 (dacl->aces[i].access_mask &
720 SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
727 TALLOC_FREE(sampass);
728 return NT_STATUS_ACCESS_DENIED;
732 status = pdb_update_sam_account(sampass);
735 TALLOC_FREE(sampass);
740 /*******************************************************************
741 build correct perms based on policies and password times for _samr_query_sec_obj
742 *******************************************************************/
743 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, struct dom_sid *user_sid)
745 struct samu *sampass=NULL;
748 if ( !(sampass = samu_new( mem_ctx )) ) {
749 DEBUG(0,("No memory!\n"));
754 ret = pdb_getsampwsid(sampass, user_sid);
758 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
759 TALLOC_FREE(sampass);
763 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
765 if (pdb_get_pass_can_change(sampass)) {
766 TALLOC_FREE(sampass);
769 TALLOC_FREE(sampass);
774 /*******************************************************************
776 ********************************************************************/
778 NTSTATUS _samr_QuerySecurity(struct pipes_struct *p,
779 struct samr_QuerySecurity *r)
781 struct samr_connect_info *cinfo;
782 struct samr_domain_info *dinfo;
783 struct samr_user_info *uinfo;
784 struct samr_group_info *ginfo;
785 struct samr_alias_info *ainfo;
787 struct security_descriptor * psd = NULL;
790 cinfo = policy_handle_find(p, r->in.handle,
791 SEC_STD_READ_CONTROL, NULL,
792 struct samr_connect_info, &status);
793 if (NT_STATUS_IS_OK(status)) {
794 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
795 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
796 &sam_generic_mapping, NULL, 0);
800 dinfo = policy_handle_find(p, r->in.handle,
801 SEC_STD_READ_CONTROL, NULL,
802 struct samr_domain_info, &status);
803 if (NT_STATUS_IS_OK(status)) {
804 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
805 "with SID: %s\n", sid_string_dbg(&dinfo->sid)));
807 * TODO: Builtin probably needs a different SD with restricted
810 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
811 &dom_generic_mapping, NULL, 0);
815 uinfo = policy_handle_find(p, r->in.handle,
816 SEC_STD_READ_CONTROL, NULL,
817 struct samr_user_info, &status);
818 if (NT_STATUS_IS_OK(status)) {
819 DEBUG(10,("_samr_QuerySecurity: querying security on user "
820 "Object with SID: %s\n",
821 sid_string_dbg(&uinfo->sid)));
822 if (check_change_pw_access(p->mem_ctx, &uinfo->sid)) {
823 status = make_samr_object_sd(
824 p->mem_ctx, &psd, &sd_size,
825 &usr_generic_mapping,
826 &uinfo->sid, SAMR_USR_RIGHTS_WRITE_PW);
828 status = make_samr_object_sd(
829 p->mem_ctx, &psd, &sd_size,
830 &usr_nopwchange_generic_mapping,
831 &uinfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
836 ginfo = policy_handle_find(p, r->in.handle,
837 SEC_STD_READ_CONTROL, NULL,
838 struct samr_group_info, &status);
839 if (NT_STATUS_IS_OK(status)) {
841 * TODO: different SDs have to be generated for aliases groups
842 * and users. Currently all three get a default user SD
844 DEBUG(10,("_samr_QuerySecurity: querying security on group "
845 "Object with SID: %s\n",
846 sid_string_dbg(&ginfo->sid)));
847 status = make_samr_object_sd(
848 p->mem_ctx, &psd, &sd_size,
849 &usr_nopwchange_generic_mapping,
850 &ginfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
854 ainfo = policy_handle_find(p, r->in.handle,
855 SEC_STD_READ_CONTROL, NULL,
856 struct samr_alias_info, &status);
857 if (NT_STATUS_IS_OK(status)) {
859 * TODO: different SDs have to be generated for aliases groups
860 * and users. Currently all three get a default user SD
862 DEBUG(10,("_samr_QuerySecurity: querying security on alias "
863 "Object with SID: %s\n",
864 sid_string_dbg(&ainfo->sid)));
865 status = make_samr_object_sd(
866 p->mem_ctx, &psd, &sd_size,
867 &usr_nopwchange_generic_mapping,
868 &ainfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
872 return NT_STATUS_OBJECT_TYPE_MISMATCH;
874 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
875 return NT_STATUS_NO_MEMORY;
880 /*******************************************************************
881 makes a SAM_ENTRY / UNISTR2* structure from a user list.
882 ********************************************************************/
884 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
885 struct samr_SamEntry **sam_pp,
886 uint32_t num_entries,
888 struct samr_displayentry *entries)
891 struct samr_SamEntry *sam;
895 if (num_entries == 0) {
899 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
901 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
902 return NT_STATUS_NO_MEMORY;
905 for (i = 0; i < num_entries; i++) {
908 * usrmgr expects a non-NULL terminated string with
909 * trust relationships
911 if (entries[i].acct_flags & ACB_DOMTRUST) {
912 init_unistr2(&uni_temp_name, entries[i].account_name,
915 init_unistr2(&uni_temp_name, entries[i].account_name,
919 init_lsa_String(&sam[i].name, entries[i].account_name);
920 sam[i].idx = entries[i].rid;
928 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
930 /*******************************************************************
931 _samr_EnumDomainUsers
932 ********************************************************************/
934 NTSTATUS _samr_EnumDomainUsers(struct pipes_struct *p,
935 struct samr_EnumDomainUsers *r)
938 struct samr_domain_info *dinfo;
940 uint32 enum_context = *r->in.resume_handle;
941 enum remote_arch_types ra_type = get_remote_arch();
942 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
943 uint32 max_entries = max_sam_entries;
944 struct samr_displayentry *entries = NULL;
945 struct samr_SamArray *samr_array = NULL;
946 struct samr_SamEntry *samr_entries = NULL;
948 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
950 dinfo = policy_handle_find(p, r->in.domain_handle,
951 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
952 struct samr_domain_info, &status);
953 if (!NT_STATUS_IS_OK(status)) {
957 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
959 return NT_STATUS_NO_MEMORY;
961 *r->out.sam = samr_array;
963 if (sid_check_is_builtin(&dinfo->sid)) {
964 /* No users in builtin. */
965 *r->out.resume_handle = *r->in.resume_handle;
966 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
974 if ((dinfo->disp_info->enum_users != NULL) &&
975 (dinfo->disp_info->enum_acb_mask != r->in.acct_flags)) {
976 TALLOC_FREE(dinfo->disp_info->enum_users);
979 if (dinfo->disp_info->enum_users == NULL) {
980 dinfo->disp_info->enum_users = pdb_search_users(
981 dinfo->disp_info, r->in.acct_flags);
982 dinfo->disp_info->enum_acb_mask = r->in.acct_flags;
985 if (dinfo->disp_info->enum_users == NULL) {
986 /* END AS ROOT !!!! */
988 return NT_STATUS_ACCESS_DENIED;
991 num_account = pdb_search_entries(dinfo->disp_info->enum_users,
992 enum_context, max_entries,
995 /* END AS ROOT !!!! */
999 if (num_account == 0) {
1000 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
1001 "total entries\n"));
1002 *r->out.resume_handle = *r->in.resume_handle;
1003 return NT_STATUS_OK;
1006 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
1007 num_account, enum_context,
1009 if (!NT_STATUS_IS_OK(status)) {
1013 if (max_entries <= num_account) {
1014 status = STATUS_MORE_ENTRIES;
1016 status = NT_STATUS_OK;
1019 /* Ensure we cache this enumeration. */
1020 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1022 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
1024 samr_array->count = num_account;
1025 samr_array->entries = samr_entries;
1027 *r->out.resume_handle = *r->in.resume_handle + num_account;
1028 *r->out.num_entries = num_account;
1030 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1035 /*******************************************************************
1036 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1037 ********************************************************************/
1039 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
1040 struct samr_SamEntry **sam_pp,
1041 uint32_t num_sam_entries,
1042 struct samr_displayentry *entries)
1044 struct samr_SamEntry *sam;
1049 if (num_sam_entries == 0) {
1053 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1058 for (i = 0; i < num_sam_entries; i++) {
1060 * JRA. I think this should include the null. TNG does not.
1062 init_lsa_String(&sam[i].name, entries[i].account_name);
1063 sam[i].idx = entries[i].rid;
1069 /*******************************************************************
1070 _samr_EnumDomainGroups
1071 ********************************************************************/
1073 NTSTATUS _samr_EnumDomainGroups(struct pipes_struct *p,
1074 struct samr_EnumDomainGroups *r)
1077 struct samr_domain_info *dinfo;
1078 struct samr_displayentry *groups;
1080 struct samr_SamArray *samr_array = NULL;
1081 struct samr_SamEntry *samr_entries = NULL;
1083 dinfo = policy_handle_find(p, r->in.domain_handle,
1084 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1085 struct samr_domain_info, &status);
1086 if (!NT_STATUS_IS_OK(status)) {
1090 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1092 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1094 return NT_STATUS_NO_MEMORY;
1096 *r->out.sam = samr_array;
1098 if (sid_check_is_builtin(&dinfo->sid)) {
1099 /* No groups in builtin. */
1100 *r->out.resume_handle = *r->in.resume_handle;
1101 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1105 /* the domain group array is being allocated in the function below */
1109 if (dinfo->disp_info->groups == NULL) {
1110 dinfo->disp_info->groups = pdb_search_groups(dinfo->disp_info);
1112 if (dinfo->disp_info->groups == NULL) {
1114 return NT_STATUS_ACCESS_DENIED;
1118 num_groups = pdb_search_entries(dinfo->disp_info->groups,
1119 *r->in.resume_handle,
1120 MAX_SAM_ENTRIES, &groups);
1123 /* Ensure we cache this enumeration. */
1124 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1126 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1127 num_groups, groups);
1129 if (MAX_SAM_ENTRIES <= num_groups) {
1130 status = STATUS_MORE_ENTRIES;
1132 status = NT_STATUS_OK;
1135 samr_array->count = num_groups;
1136 samr_array->entries = samr_entries;
1138 *r->out.num_entries = num_groups;
1139 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1141 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1146 /*******************************************************************
1147 _samr_EnumDomainAliases
1148 ********************************************************************/
1150 NTSTATUS _samr_EnumDomainAliases(struct pipes_struct *p,
1151 struct samr_EnumDomainAliases *r)
1154 struct samr_domain_info *dinfo;
1155 struct samr_displayentry *aliases;
1156 uint32 num_aliases = 0;
1157 struct samr_SamArray *samr_array = NULL;
1158 struct samr_SamEntry *samr_entries = NULL;
1160 dinfo = policy_handle_find(p, r->in.domain_handle,
1161 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1162 struct samr_domain_info, &status);
1163 if (!NT_STATUS_IS_OK(status)) {
1167 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1168 sid_string_dbg(&dinfo->sid)));
1170 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1172 return NT_STATUS_NO_MEMORY;
1177 if (dinfo->disp_info->aliases == NULL) {
1178 dinfo->disp_info->aliases = pdb_search_aliases(
1179 dinfo->disp_info, &dinfo->sid);
1180 if (dinfo->disp_info->aliases == NULL) {
1182 return NT_STATUS_ACCESS_DENIED;
1186 num_aliases = pdb_search_entries(dinfo->disp_info->aliases,
1187 *r->in.resume_handle,
1188 MAX_SAM_ENTRIES, &aliases);
1191 /* Ensure we cache this enumeration. */
1192 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1194 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1195 num_aliases, aliases);
1197 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1199 if (MAX_SAM_ENTRIES <= num_aliases) {
1200 status = STATUS_MORE_ENTRIES;
1202 status = NT_STATUS_OK;
1205 samr_array->count = num_aliases;
1206 samr_array->entries = samr_entries;
1208 *r->out.sam = samr_array;
1209 *r->out.num_entries = num_aliases;
1210 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1215 /*******************************************************************
1216 inits a samr_DispInfoGeneral structure.
1217 ********************************************************************/
1219 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1220 struct samr_DispInfoGeneral *r,
1221 uint32_t num_entries,
1223 struct samr_displayentry *entries)
1227 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1229 if (num_entries == 0) {
1230 return NT_STATUS_OK;
1233 r->count = num_entries;
1235 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1237 return NT_STATUS_NO_MEMORY;
1240 for (i = 0; i < num_entries ; i++) {
1242 init_lsa_String(&r->entries[i].account_name,
1243 entries[i].account_name);
1245 init_lsa_String(&r->entries[i].description,
1246 entries[i].description);
1248 init_lsa_String(&r->entries[i].full_name,
1249 entries[i].fullname);
1251 r->entries[i].rid = entries[i].rid;
1252 r->entries[i].acct_flags = entries[i].acct_flags;
1253 r->entries[i].idx = start_idx+i+1;
1256 return NT_STATUS_OK;
1259 /*******************************************************************
1260 inits a samr_DispInfoFull structure.
1261 ********************************************************************/
1263 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1264 struct samr_DispInfoFull *r,
1265 uint32_t num_entries,
1267 struct samr_displayentry *entries)
1271 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1273 if (num_entries == 0) {
1274 return NT_STATUS_OK;
1277 r->count = num_entries;
1279 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFull, num_entries);
1281 return NT_STATUS_NO_MEMORY;
1284 for (i = 0; i < num_entries ; i++) {
1286 init_lsa_String(&r->entries[i].account_name,
1287 entries[i].account_name);
1289 init_lsa_String(&r->entries[i].description,
1290 entries[i].description);
1292 r->entries[i].rid = entries[i].rid;
1293 r->entries[i].acct_flags = entries[i].acct_flags;
1294 r->entries[i].idx = start_idx+i+1;
1297 return NT_STATUS_OK;
1300 /*******************************************************************
1301 inits a samr_DispInfoFullGroups structure.
1302 ********************************************************************/
1304 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1305 struct samr_DispInfoFullGroups *r,
1306 uint32_t num_entries,
1308 struct samr_displayentry *entries)
1312 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1314 if (num_entries == 0) {
1315 return NT_STATUS_OK;
1318 r->count = num_entries;
1320 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1322 return NT_STATUS_NO_MEMORY;
1325 for (i = 0; i < num_entries ; i++) {
1327 init_lsa_String(&r->entries[i].account_name,
1328 entries[i].account_name);
1330 init_lsa_String(&r->entries[i].description,
1331 entries[i].description);
1333 r->entries[i].rid = entries[i].rid;
1334 r->entries[i].acct_flags = entries[i].acct_flags;
1335 r->entries[i].idx = start_idx+i+1;
1338 return NT_STATUS_OK;
1341 /*******************************************************************
1342 inits a samr_DispInfoAscii structure.
1343 ********************************************************************/
1345 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1346 struct samr_DispInfoAscii *r,
1347 uint32_t num_entries,
1349 struct samr_displayentry *entries)
1353 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1355 if (num_entries == 0) {
1356 return NT_STATUS_OK;
1359 r->count = num_entries;
1361 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1363 return NT_STATUS_NO_MEMORY;
1366 for (i = 0; i < num_entries ; i++) {
1368 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1369 entries[i].account_name);
1371 r->entries[i].idx = start_idx+i+1;
1374 return NT_STATUS_OK;
1377 /*******************************************************************
1378 inits a samr_DispInfoAscii structure.
1379 ********************************************************************/
1381 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1382 struct samr_DispInfoAscii *r,
1383 uint32_t num_entries,
1385 struct samr_displayentry *entries)
1389 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1391 if (num_entries == 0) {
1392 return NT_STATUS_OK;
1395 r->count = num_entries;
1397 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1399 return NT_STATUS_NO_MEMORY;
1402 for (i = 0; i < num_entries ; i++) {
1404 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1405 entries[i].account_name);
1407 r->entries[i].idx = start_idx+i+1;
1410 return NT_STATUS_OK;
1413 /*******************************************************************
1414 _samr_QueryDisplayInfo
1415 ********************************************************************/
1417 NTSTATUS _samr_QueryDisplayInfo(struct pipes_struct *p,
1418 struct samr_QueryDisplayInfo *r)
1421 struct samr_domain_info *dinfo;
1422 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1424 uint32 max_entries = r->in.max_entries;
1426 union samr_DispInfo *disp_info = r->out.info;
1429 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1430 uint32 num_account = 0;
1431 enum remote_arch_types ra_type = get_remote_arch();
1432 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1433 struct samr_displayentry *entries = NULL;
1435 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1437 dinfo = policy_handle_find(p, r->in.domain_handle,
1438 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1439 struct samr_domain_info, &status);
1440 if (!NT_STATUS_IS_OK(status)) {
1444 if (sid_check_is_builtin(&dinfo->sid)) {
1445 DEBUG(5,("_samr_QueryDisplayInfo: no users in BUILTIN\n"));
1446 return NT_STATUS_OK;
1450 * calculate how many entries we will return.
1452 * - the number of entries the client asked
1453 * - our limit on that
1454 * - the starting point (enumeration context)
1455 * - the buffer size the client will accept
1459 * We are a lot more like W2K. Instead of reading the SAM
1460 * each time to find the records we need to send back,
1461 * we read it once and link that copy to the sam handle.
1462 * For large user list (over the MAX_SAM_ENTRIES)
1463 * it's a definitive win.
1464 * second point to notice: between enumerations
1465 * our sam is now the same as it's a snapshoot.
1466 * third point: got rid of the static SAM_USER_21 struct
1467 * no more intermediate.
1468 * con: it uses much more memory, as a full copy is stored
1471 * If you want to change it, think twice and think
1472 * of the second point , that's really important.
1477 if ((r->in.level < 1) || (r->in.level > 5)) {
1478 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1479 (unsigned int)r->in.level ));
1480 return NT_STATUS_INVALID_INFO_CLASS;
1483 /* first limit the number of entries we will return */
1484 if (r->in.max_entries > max_sam_entries) {
1485 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1486 "entries, limiting to %d\n", r->in.max_entries,
1488 max_entries = max_sam_entries;
1491 /* calculate the size and limit on the number of entries we will
1494 temp_size=max_entries*struct_size;
1496 if (temp_size > r->in.buf_size) {
1497 max_entries = MIN((r->in.buf_size / struct_size),max_entries);
1498 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1499 "only %d entries\n", max_entries));
1504 /* THe following done as ROOT. Don't return without unbecome_root(). */
1506 switch (r->in.level) {
1509 if (dinfo->disp_info->users == NULL) {
1510 dinfo->disp_info->users = pdb_search_users(
1511 dinfo->disp_info, ACB_NORMAL);
1512 if (dinfo->disp_info->users == NULL) {
1514 return NT_STATUS_ACCESS_DENIED;
1516 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1517 (unsigned int)r->in.start_idx));
1519 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1520 (unsigned int)r->in.start_idx));
1523 num_account = pdb_search_entries(dinfo->disp_info->users,
1524 r->in.start_idx, max_entries,
1528 if (dinfo->disp_info->machines == NULL) {
1529 dinfo->disp_info->machines = pdb_search_users(
1530 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
1531 if (dinfo->disp_info->machines == NULL) {
1533 return NT_STATUS_ACCESS_DENIED;
1535 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1536 (unsigned int)r->in.start_idx));
1538 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1539 (unsigned int)r->in.start_idx));
1542 num_account = pdb_search_entries(dinfo->disp_info->machines,
1543 r->in.start_idx, max_entries,
1548 if (dinfo->disp_info->groups == NULL) {
1549 dinfo->disp_info->groups = pdb_search_groups(
1551 if (dinfo->disp_info->groups == NULL) {
1553 return NT_STATUS_ACCESS_DENIED;
1555 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1556 (unsigned int)r->in.start_idx));
1558 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1559 (unsigned int)r->in.start_idx));
1562 num_account = pdb_search_entries(dinfo->disp_info->groups,
1563 r->in.start_idx, max_entries,
1568 smb_panic("info class changed");
1574 /* Now create reply structure */
1575 switch (r->in.level) {
1577 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1578 num_account, r->in.start_idx,
1582 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1583 num_account, r->in.start_idx,
1587 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1588 num_account, r->in.start_idx,
1592 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1593 num_account, r->in.start_idx,
1597 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1598 num_account, r->in.start_idx,
1602 smb_panic("info class changed");
1606 if (!NT_STATUS_IS_OK(disp_ret))
1609 if (max_entries <= num_account) {
1610 status = STATUS_MORE_ENTRIES;
1612 status = NT_STATUS_OK;
1615 /* Ensure we cache this enumeration. */
1616 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1618 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1620 *r->out.total_size = num_account * struct_size;
1621 *r->out.returned_size = num_account ? temp_size : 0;
1626 /****************************************************************
1627 _samr_QueryDisplayInfo2
1628 ****************************************************************/
1630 NTSTATUS _samr_QueryDisplayInfo2(struct pipes_struct *p,
1631 struct samr_QueryDisplayInfo2 *r)
1633 struct samr_QueryDisplayInfo q;
1635 q.in.domain_handle = r->in.domain_handle;
1636 q.in.level = r->in.level;
1637 q.in.start_idx = r->in.start_idx;
1638 q.in.max_entries = r->in.max_entries;
1639 q.in.buf_size = r->in.buf_size;
1641 q.out.total_size = r->out.total_size;
1642 q.out.returned_size = r->out.returned_size;
1643 q.out.info = r->out.info;
1645 return _samr_QueryDisplayInfo(p, &q);
1648 /****************************************************************
1649 _samr_QueryDisplayInfo3
1650 ****************************************************************/
1652 NTSTATUS _samr_QueryDisplayInfo3(struct pipes_struct *p,
1653 struct samr_QueryDisplayInfo3 *r)
1655 struct samr_QueryDisplayInfo q;
1657 q.in.domain_handle = r->in.domain_handle;
1658 q.in.level = r->in.level;
1659 q.in.start_idx = r->in.start_idx;
1660 q.in.max_entries = r->in.max_entries;
1661 q.in.buf_size = r->in.buf_size;
1663 q.out.total_size = r->out.total_size;
1664 q.out.returned_size = r->out.returned_size;
1665 q.out.info = r->out.info;
1667 return _samr_QueryDisplayInfo(p, &q);
1670 /*******************************************************************
1671 _samr_QueryAliasInfo
1672 ********************************************************************/
1674 NTSTATUS _samr_QueryAliasInfo(struct pipes_struct *p,
1675 struct samr_QueryAliasInfo *r)
1677 struct samr_alias_info *ainfo;
1678 struct acct_info info;
1680 union samr_AliasInfo *alias_info = NULL;
1681 const char *alias_name = NULL;
1682 const char *alias_description = NULL;
1684 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1686 ainfo = policy_handle_find(p, r->in.alias_handle,
1687 SAMR_ALIAS_ACCESS_LOOKUP_INFO, NULL,
1688 struct samr_alias_info, &status);
1689 if (!NT_STATUS_IS_OK(status)) {
1693 alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1695 return NT_STATUS_NO_MEMORY;
1699 status = pdb_get_aliasinfo(&ainfo->sid, &info);
1702 if ( !NT_STATUS_IS_OK(status))
1705 /* FIXME: info contains fstrings */
1706 alias_name = talloc_strdup(r, info.acct_name);
1707 alias_description = talloc_strdup(r, info.acct_desc);
1709 switch (r->in.level) {
1711 alias_info->all.name.string = alias_name;
1712 alias_info->all.num_members = 1; /* ??? */
1713 alias_info->all.description.string = alias_description;
1716 alias_info->name.string = alias_name;
1718 case ALIASINFODESCRIPTION:
1719 alias_info->description.string = alias_description;
1722 return NT_STATUS_INVALID_INFO_CLASS;
1725 *r->out.info = alias_info;
1727 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1729 return NT_STATUS_OK;
1732 /*******************************************************************
1734 ********************************************************************/
1736 NTSTATUS _samr_LookupNames(struct pipes_struct *p,
1737 struct samr_LookupNames *r)
1739 struct samr_domain_info *dinfo;
1742 enum lsa_SidType *type;
1744 int num_rids = r->in.num_names;
1745 struct samr_Ids rids, types;
1746 uint32_t num_mapped = 0;
1748 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1750 dinfo = policy_handle_find(p, r->in.domain_handle,
1751 0 /* Don't know the acc_bits yet */, NULL,
1752 struct samr_domain_info, &status);
1753 if (!NT_STATUS_IS_OK(status)) {
1757 if (num_rids > MAX_SAM_ENTRIES) {
1758 num_rids = MAX_SAM_ENTRIES;
1759 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1762 rid = talloc_array(p->mem_ctx, uint32, num_rids);
1763 NT_STATUS_HAVE_NO_MEMORY(rid);
1765 type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1766 NT_STATUS_HAVE_NO_MEMORY(type);
1768 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1769 sid_string_dbg(&dinfo->sid)));
1771 for (i = 0; i < num_rids; i++) {
1773 status = NT_STATUS_NONE_MAPPED;
1774 type[i] = SID_NAME_UNKNOWN;
1776 rid[i] = 0xffffffff;
1778 if (sid_check_is_builtin(&dinfo->sid)) {
1779 if (lookup_builtin_name(r->in.names[i].string,
1782 type[i] = SID_NAME_ALIAS;
1785 lookup_global_sam_name(r->in.names[i].string, 0,
1789 if (type[i] != SID_NAME_UNKNOWN) {
1794 if (num_mapped == num_rids) {
1795 status = NT_STATUS_OK;
1796 } else if (num_mapped == 0) {
1797 status = NT_STATUS_NONE_MAPPED;
1799 status = STATUS_SOME_UNMAPPED;
1802 rids.count = num_rids;
1805 types.count = num_rids;
1806 types.ids = talloc_array(p->mem_ctx, uint32_t, num_rids);
1807 NT_STATUS_HAVE_NO_MEMORY(type);
1808 for (i = 0; i < num_rids; i++) {
1809 types.ids[i] = (type[i] & 0xffffffff);
1812 *r->out.rids = rids;
1813 *r->out.types = types;
1815 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1820 /****************************************************************
1821 _samr_ChangePasswordUser
1822 ****************************************************************/
1824 NTSTATUS _samr_ChangePasswordUser(struct pipes_struct *p,
1825 struct samr_ChangePasswordUser *r)
1829 struct samr_user_info *uinfo;
1831 struct samr_Password new_lmPwdHash, new_ntPwdHash, checkHash;
1832 struct samr_Password lm_pwd, nt_pwd;
1834 uinfo = policy_handle_find(p, r->in.user_handle,
1835 SAMR_USER_ACCESS_SET_PASSWORD, NULL,
1836 struct samr_user_info, &status);
1837 if (!NT_STATUS_IS_OK(status)) {
1841 DEBUG(5,("_samr_ChangePasswordUser: sid:%s\n",
1842 sid_string_dbg(&uinfo->sid)));
1844 if (!(pwd = samu_new(NULL))) {
1845 return NT_STATUS_NO_MEMORY;
1849 ret = pdb_getsampwsid(pwd, &uinfo->sid);
1854 return NT_STATUS_WRONG_PASSWORD;
1858 const uint8_t *lm_pass, *nt_pass;
1860 lm_pass = pdb_get_lanman_passwd(pwd);
1861 nt_pass = pdb_get_nt_passwd(pwd);
1863 if (!lm_pass || !nt_pass) {
1864 status = NT_STATUS_WRONG_PASSWORD;
1868 memcpy(&lm_pwd.hash, lm_pass, sizeof(lm_pwd.hash));
1869 memcpy(&nt_pwd.hash, nt_pass, sizeof(nt_pwd.hash));
1872 /* basic sanity checking on parameters. Do this before any database ops */
1873 if (!r->in.lm_present || !r->in.nt_present ||
1874 !r->in.old_lm_crypted || !r->in.new_lm_crypted ||
1875 !r->in.old_nt_crypted || !r->in.new_nt_crypted) {
1876 /* we should really handle a change with lm not
1878 status = NT_STATUS_INVALID_PARAMETER_MIX;
1882 /* decrypt and check the new lm hash */
1883 D_P16(lm_pwd.hash, r->in.new_lm_crypted->hash, new_lmPwdHash.hash);
1884 D_P16(new_lmPwdHash.hash, r->in.old_lm_crypted->hash, checkHash.hash);
1885 if (memcmp(checkHash.hash, lm_pwd.hash, 16) != 0) {
1886 status = NT_STATUS_WRONG_PASSWORD;
1890 /* decrypt and check the new nt hash */
1891 D_P16(nt_pwd.hash, r->in.new_nt_crypted->hash, new_ntPwdHash.hash);
1892 D_P16(new_ntPwdHash.hash, r->in.old_nt_crypted->hash, checkHash.hash);
1893 if (memcmp(checkHash.hash, nt_pwd.hash, 16) != 0) {
1894 status = NT_STATUS_WRONG_PASSWORD;
1898 /* The NT Cross is not required by Win2k3 R2, but if present
1899 check the nt cross hash */
1900 if (r->in.cross1_present && r->in.nt_cross) {
1901 D_P16(lm_pwd.hash, r->in.nt_cross->hash, checkHash.hash);
1902 if (memcmp(checkHash.hash, new_ntPwdHash.hash, 16) != 0) {
1903 status = NT_STATUS_WRONG_PASSWORD;
1908 /* The LM Cross is not required by Win2k3 R2, but if present
1909 check the lm cross hash */
1910 if (r->in.cross2_present && r->in.lm_cross) {
1911 D_P16(nt_pwd.hash, r->in.lm_cross->hash, checkHash.hash);
1912 if (memcmp(checkHash.hash, new_lmPwdHash.hash, 16) != 0) {
1913 status = NT_STATUS_WRONG_PASSWORD;
1918 if (!pdb_set_nt_passwd(pwd, new_ntPwdHash.hash, PDB_CHANGED) ||
1919 !pdb_set_lanman_passwd(pwd, new_lmPwdHash.hash, PDB_CHANGED)) {
1920 status = NT_STATUS_ACCESS_DENIED;
1924 status = pdb_update_sam_account(pwd);
1931 /*******************************************************************
1932 _samr_ChangePasswordUser2
1933 ********************************************************************/
1935 NTSTATUS _samr_ChangePasswordUser2(struct pipes_struct *p,
1936 struct samr_ChangePasswordUser2 *r)
1939 char *user_name = NULL;
1942 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1944 if (!r->in.account->string) {
1945 return NT_STATUS_INVALID_PARAMETER;
1947 fstrcpy(wks, r->in.server->string);
1949 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1952 * Pass the user through the NT -> unix user mapping
1956 (void)map_username(talloc_tos(), r->in.account->string, &user_name);
1958 return NT_STATUS_NO_MEMORY;
1962 * UNIX username case mangling not required, pass_oem_change
1963 * is case insensitive.
1966 status = pass_oem_change(user_name,
1968 r->in.lm_password->data,
1969 r->in.lm_verifier->hash,
1970 r->in.nt_password->data,
1971 r->in.nt_verifier->hash,
1974 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1976 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1977 return NT_STATUS_WRONG_PASSWORD;
1983 /****************************************************************
1984 _samr_OemChangePasswordUser2
1985 ****************************************************************/
1987 NTSTATUS _samr_OemChangePasswordUser2(struct pipes_struct *p,
1988 struct samr_OemChangePasswordUser2 *r)
1991 char *user_name = NULL;
1992 const char *wks = NULL;
1994 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
1996 if (!r->in.account->string) {
1997 return NT_STATUS_INVALID_PARAMETER;
1999 if (r->in.server && r->in.server->string) {
2000 wks = r->in.server->string;
2003 DEBUG(5,("_samr_OemChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
2006 * Pass the user through the NT -> unix user mapping
2010 (void)map_username(talloc_tos(), r->in.account->string, &user_name);
2012 return NT_STATUS_NO_MEMORY;
2016 * UNIX username case mangling not required, pass_oem_change
2017 * is case insensitive.
2020 if (!r->in.hash || !r->in.password) {
2021 return NT_STATUS_INVALID_PARAMETER;
2024 status = pass_oem_change(user_name,
2026 r->in.password->data,
2032 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2033 return NT_STATUS_WRONG_PASSWORD;
2036 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
2041 /*******************************************************************
2042 _samr_ChangePasswordUser3
2043 ********************************************************************/
2045 NTSTATUS _samr_ChangePasswordUser3(struct pipes_struct *p,
2046 struct samr_ChangePasswordUser3 *r)
2049 char *user_name = NULL;
2050 const char *wks = NULL;
2051 enum samPwdChangeReason reject_reason;
2052 struct samr_DomInfo1 *dominfo = NULL;
2053 struct userPwdChangeFailureInformation *reject = NULL;
2056 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2058 if (!r->in.account->string) {
2059 return NT_STATUS_INVALID_PARAMETER;
2061 if (r->in.server && r->in.server->string) {
2062 wks = r->in.server->string;
2065 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
2068 * Pass the user through the NT -> unix user mapping
2072 (void)map_username(talloc_tos(), r->in.account->string, &user_name);
2074 return NT_STATUS_NO_MEMORY;
2078 * UNIX username case mangling not required, pass_oem_change
2079 * is case insensitive.
2082 status = pass_oem_change(user_name,
2084 r->in.lm_password->data,
2085 r->in.lm_verifier->hash,
2086 r->in.nt_password->data,
2087 r->in.nt_verifier->hash,
2089 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2090 return NT_STATUS_WRONG_PASSWORD;
2093 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
2094 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
2096 time_t u_expire, u_min_age;
2097 uint32 account_policy_temp;
2099 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
2101 return NT_STATUS_NO_MEMORY;
2104 reject = TALLOC_ZERO_P(p->mem_ctx,
2105 struct userPwdChangeFailureInformation);
2107 return NT_STATUS_NO_MEMORY;
2114 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN, &tmp);
2115 dominfo->min_password_length = tmp;
2117 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &tmp);
2118 dominfo->password_history_length = tmp;
2120 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
2121 &dominfo->password_properties);
2123 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &account_policy_temp);
2124 u_expire = account_policy_temp;
2126 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &account_policy_temp);
2127 u_min_age = account_policy_temp;
2133 unix_to_nt_time_abs((NTTIME *)&dominfo->max_password_age, u_expire);
2134 unix_to_nt_time_abs((NTTIME *)&dominfo->min_password_age, u_min_age);
2136 if (lp_check_password_script() && *lp_check_password_script()) {
2137 dominfo->password_properties |= DOMAIN_PASSWORD_COMPLEX;
2140 reject->extendedFailureReason = reject_reason;
2142 *r->out.dominfo = dominfo;
2143 *r->out.reject = reject;
2146 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2151 /*******************************************************************
2152 makes a SAMR_R_LOOKUP_RIDS structure.
2153 ********************************************************************/
2155 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
2157 struct lsa_String **lsa_name_array_p)
2159 struct lsa_String *lsa_name_array = NULL;
2162 *lsa_name_array_p = NULL;
2164 if (num_names != 0) {
2165 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
2166 if (!lsa_name_array) {
2171 for (i = 0; i < num_names; i++) {
2172 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
2173 init_lsa_String(&lsa_name_array[i], names[i]);
2176 *lsa_name_array_p = lsa_name_array;
2181 /*******************************************************************
2183 ********************************************************************/
2185 NTSTATUS _samr_LookupRids(struct pipes_struct *p,
2186 struct samr_LookupRids *r)
2188 struct samr_domain_info *dinfo;
2191 enum lsa_SidType *attrs = NULL;
2192 uint32 *wire_attrs = NULL;
2193 int num_rids = (int)r->in.num_rids;
2195 struct lsa_Strings names_array;
2196 struct samr_Ids types_array;
2197 struct lsa_String *lsa_names = NULL;
2199 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2201 dinfo = policy_handle_find(p, r->in.domain_handle,
2202 0 /* Don't know the acc_bits yet */, NULL,
2203 struct samr_domain_info, &status);
2204 if (!NT_STATUS_IS_OK(status)) {
2208 if (num_rids > 1000) {
2209 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2210 "to samba4 idl this is not possible\n", num_rids));
2211 return NT_STATUS_UNSUCCESSFUL;
2215 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
2216 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
2217 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
2219 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2220 return NT_STATUS_NO_MEMORY;
2227 become_root(); /* lookup_sid can require root privs */
2228 status = pdb_lookup_rids(&dinfo->sid, num_rids, r->in.rids,
2232 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2233 status = NT_STATUS_OK;
2236 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2238 return NT_STATUS_NO_MEMORY;
2241 /* Convert from enum lsa_SidType to uint32 for wire format. */
2242 for (i = 0; i < num_rids; i++) {
2243 wire_attrs[i] = (uint32)attrs[i];
2246 names_array.count = num_rids;
2247 names_array.names = lsa_names;
2249 types_array.count = num_rids;
2250 types_array.ids = wire_attrs;
2252 *r->out.names = names_array;
2253 *r->out.types = types_array;
2255 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2260 /*******************************************************************
2262 ********************************************************************/
2264 NTSTATUS _samr_OpenUser(struct pipes_struct *p,
2265 struct samr_OpenUser *r)
2267 struct samu *sampass=NULL;
2269 struct samr_domain_info *dinfo;
2270 struct samr_user_info *uinfo;
2271 struct security_descriptor *psd = NULL;
2273 uint32 des_access = r->in.access_mask;
2274 uint32_t extra_access = 0;
2279 /* These two privileges, if != SEC_PRIV_INVALID, indicate
2280 * privileges that the user must have to complete this
2281 * operation in defience of the fixed ACL */
2282 enum sec_privilege needed_priv_1, needed_priv_2;
2285 dinfo = policy_handle_find(p, r->in.domain_handle,
2286 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
2287 struct samr_domain_info, &status);
2288 if (!NT_STATUS_IS_OK(status)) {
2292 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2293 return NT_STATUS_NO_MEMORY;
2296 /* append the user's RID to it */
2298 if (!sid_compose(&sid, &dinfo->sid, r->in.rid))
2299 return NT_STATUS_NO_SUCH_USER;
2301 /* check if access can be granted as requested by client. */
2302 map_max_allowed_access(p->session_info->security_token,
2303 &p->session_info->utok,
2306 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2307 se_map_generic(&des_access, &usr_generic_mapping);
2310 * Get the sampass first as we need to check privileges
2311 * based on what kind of user object this is.
2312 * But don't reveal info too early if it didn't exist.
2316 ret=pdb_getsampwsid(sampass, &sid);
2319 needed_priv_1 = SEC_PRIV_INVALID;
2320 needed_priv_2 = SEC_PRIV_INVALID;
2322 * We do the override access checks on *open*, not at
2326 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
2328 if (acb_info & ACB_WSTRUST) {
2330 * SeMachineAccount is needed to add
2331 * GENERIC_RIGHTS_USER_WRITE to a machine
2334 needed_priv_1 = SEC_PRIV_MACHINE_ACCOUNT;
2336 if (acb_info & ACB_NORMAL) {
2338 * SeAddUsers is needed to add
2339 * GENERIC_RIGHTS_USER_WRITE to a normal
2342 needed_priv_1 = SEC_PRIV_ADD_USERS;
2345 * Cheat - we have not set a specific privilege for
2346 * server (BDC) or domain trust account, so allow
2347 * GENERIC_RIGHTS_USER_WRITE if pipe user is in
2348 * DOMAIN_RID_ADMINS.
2350 if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
2351 if (lp_enable_privileges() && nt_token_check_domain_rid(p->session_info->security_token,
2352 DOMAIN_RID_ADMINS)) {
2353 des_access &= ~GENERIC_RIGHTS_USER_WRITE;
2354 extra_access = GENERIC_RIGHTS_USER_WRITE;
2355 DEBUG(4,("_samr_OpenUser: Allowing "
2356 "GENERIC_RIGHTS_USER_WRITE for "
2362 TALLOC_FREE(sampass);
2364 nt_status = access_check_object(psd, p->session_info->security_token,
2365 needed_priv_1, needed_priv_2,
2366 GENERIC_RIGHTS_USER_WRITE, des_access,
2367 &acc_granted, "_samr_OpenUser");
2369 if ( !NT_STATUS_IS_OK(nt_status) )
2372 /* check that the SID exists in our domain. */
2374 return NT_STATUS_NO_SUCH_USER;
2377 /* If we did the rid admins hack above, allow access. */
2378 acc_granted |= extra_access;
2380 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
2381 struct samr_user_info, &nt_status);
2382 if (!NT_STATUS_IS_OK(nt_status)) {
2387 return NT_STATUS_OK;
2390 /*************************************************************************
2391 *************************************************************************/
2393 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2395 struct lsa_BinaryString **_r)
2397 struct lsa_BinaryString *r;
2400 return NT_STATUS_INVALID_PARAMETER;
2403 r = TALLOC_ZERO_P(mem_ctx, struct lsa_BinaryString);
2405 return NT_STATUS_NO_MEMORY;
2408 r->array = TALLOC_ZERO_ARRAY(mem_ctx, uint16_t, blob->length/2);
2410 return NT_STATUS_NO_MEMORY;
2412 memcpy(r->array, blob->data, blob->length);
2413 r->size = blob->length;
2414 r->length = blob->length;
2417 return NT_STATUS_NO_MEMORY;
2422 return NT_STATUS_OK;
2425 /*************************************************************************
2426 *************************************************************************/
2428 static struct samr_LogonHours get_logon_hours_from_pdb(TALLOC_CTX *mem_ctx,
2431 struct samr_LogonHours hours;
2432 const int units_per_week = 168;
2435 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
2440 hours.units_per_week = units_per_week;
2441 memset(hours.bits, 0xFF, units_per_week);
2443 if (pdb_get_hours(pw)) {
2444 memcpy(hours.bits, pdb_get_hours(pw),
2445 MIN(pdb_get_hours_len(pw), units_per_week));
2451 /*************************************************************************
2453 *************************************************************************/
2455 static NTSTATUS get_user_info_1(TALLOC_CTX *mem_ctx,
2456 struct samr_UserInfo1 *r,
2458 struct dom_sid *domain_sid)
2460 const struct dom_sid *sid_group;
2461 uint32_t primary_gid;
2464 sid_group = pdb_get_group_sid(pw);
2467 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2468 DEBUG(0, ("get_user_info_1: User %s has Primary Group SID %s, \n"
2469 "which conflicts with the domain sid %s. Failing operation.\n",
2470 pdb_get_username(pw), sid_string_dbg(sid_group),
2471 sid_string_dbg(domain_sid)));
2472 return NT_STATUS_UNSUCCESSFUL;
2475 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2476 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2477 r->primary_gid = primary_gid;
2478 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2479 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2481 return NT_STATUS_OK;
2484 /*************************************************************************
2486 *************************************************************************/
2488 static NTSTATUS get_user_info_2(TALLOC_CTX *mem_ctx,
2489 struct samr_UserInfo2 *r,
2492 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2493 r->reserved.string = NULL;
2494 r->country_code = pdb_get_country_code(pw);
2495 r->code_page = pdb_get_code_page(pw);
2497 return NT_STATUS_OK;
2500 /*************************************************************************
2502 *************************************************************************/
2504 static NTSTATUS get_user_info_3(TALLOC_CTX *mem_ctx,
2505 struct samr_UserInfo3 *r,
2507 struct dom_sid *domain_sid)
2509 const struct dom_sid *sid_user, *sid_group;
2510 uint32_t rid, primary_gid;
2512 sid_user = pdb_get_user_sid(pw);
2514 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2515 DEBUG(0, ("get_user_info_3: User %s has SID %s, \nwhich conflicts with "
2516 "the domain sid %s. Failing operation.\n",
2517 pdb_get_username(pw), sid_string_dbg(sid_user),
2518 sid_string_dbg(domain_sid)));
2519 return NT_STATUS_UNSUCCESSFUL;
2523 sid_group = pdb_get_group_sid(pw);
2526 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2527 DEBUG(0, ("get_user_info_3: User %s has Primary Group SID %s, \n"
2528 "which conflicts with the domain sid %s. Failing operation.\n",
2529 pdb_get_username(pw), sid_string_dbg(sid_group),
2530 sid_string_dbg(domain_sid)));
2531 return NT_STATUS_UNSUCCESSFUL;
2534 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2535 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2536 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2537 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2538 unix_to_nt_time(&r->force_password_change, pdb_get_pass_must_change_time(pw));
2540 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2541 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2542 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2543 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2544 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2545 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2546 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2548 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2550 r->primary_gid = primary_gid;
2551 r->acct_flags = pdb_get_acct_ctrl(pw);
2552 r->bad_password_count = pdb_get_bad_password_count(pw);
2553 r->logon_count = pdb_get_logon_count(pw);
2555 return NT_STATUS_OK;
2558 /*************************************************************************
2560 *************************************************************************/
2562 static NTSTATUS get_user_info_4(TALLOC_CTX *mem_ctx,
2563 struct samr_UserInfo4 *r,
2566 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2568 return NT_STATUS_OK;
2571 /*************************************************************************
2573 *************************************************************************/
2575 static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
2576 struct samr_UserInfo5 *r,
2578 struct dom_sid *domain_sid)
2580 const struct dom_sid *sid_user, *sid_group;
2581 uint32_t rid, primary_gid;
2583 sid_user = pdb_get_user_sid(pw);
2585 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2586 DEBUG(0, ("get_user_info_5: User %s has SID %s, \nwhich conflicts with "
2587 "the domain sid %s. Failing operation.\n",
2588 pdb_get_username(pw), sid_string_dbg(sid_user),
2589 sid_string_dbg(domain_sid)));
2590 return NT_STATUS_UNSUCCESSFUL;
2594 sid_group = pdb_get_group_sid(pw);
2597 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2598 DEBUG(0, ("get_user_info_5: User %s has Primary Group SID %s, \n"
2599 "which conflicts with the domain sid %s. Failing operation.\n",
2600 pdb_get_username(pw), sid_string_dbg(sid_group),
2601 sid_string_dbg(domain_sid)));
2602 return NT_STATUS_UNSUCCESSFUL;
2605 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2606 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2607 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2608 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2610 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2611 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2612 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2613 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2614 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2615 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2616 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2617 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2619 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2621 r->primary_gid = primary_gid;
2622 r->acct_flags = pdb_get_acct_ctrl(pw);
2623 r->bad_password_count = pdb_get_bad_password_count(pw);
2624 r->logon_count = pdb_get_logon_count(pw);
2626 return NT_STATUS_OK;
2629 /*************************************************************************
2631 *************************************************************************/
2633 static NTSTATUS get_user_info_6(TALLOC_CTX *mem_ctx,
2634 struct samr_UserInfo6 *r,
2637 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2638 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2640 return NT_STATUS_OK;
2643 /*************************************************************************
2644 get_user_info_7. Safe. Only gives out account_name.
2645 *************************************************************************/
2647 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2648 struct samr_UserInfo7 *r,
2649 struct samu *smbpass)
2651 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2652 if (!r->account_name.string) {
2653 return NT_STATUS_NO_MEMORY;
2656 return NT_STATUS_OK;
2659 /*************************************************************************
2661 *************************************************************************/
2663 static NTSTATUS get_user_info_8(TALLOC_CTX *mem_ctx,
2664 struct samr_UserInfo8 *r,
2667 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2669 return NT_STATUS_OK;
2672 /*************************************************************************
2673 get_user_info_9. Only gives out primary group SID.
2674 *************************************************************************/
2676 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2677 struct samr_UserInfo9 *r,
2678 struct samu *smbpass)
2680 r->primary_gid = pdb_get_group_rid(smbpass);
2682 return NT_STATUS_OK;
2685 /*************************************************************************
2687 *************************************************************************/
2689 static NTSTATUS get_user_info_10(TALLOC_CTX *mem_ctx,
2690 struct samr_UserInfo10 *r,
2693 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2694 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2696 return NT_STATUS_OK;
2699 /*************************************************************************
2701 *************************************************************************/
2703 static NTSTATUS get_user_info_11(TALLOC_CTX *mem_ctx,
2704 struct samr_UserInfo11 *r,
2707 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2709 return NT_STATUS_OK;
2712 /*************************************************************************
2714 *************************************************************************/
2716 static NTSTATUS get_user_info_12(TALLOC_CTX *mem_ctx,
2717 struct samr_UserInfo12 *r,
2720 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2722 return NT_STATUS_OK;
2725 /*************************************************************************
2727 *************************************************************************/
2729 static NTSTATUS get_user_info_13(TALLOC_CTX *mem_ctx,
2730 struct samr_UserInfo13 *r,
2733 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2735 return NT_STATUS_OK;
2738 /*************************************************************************
2740 *************************************************************************/
2742 static NTSTATUS get_user_info_14(TALLOC_CTX *mem_ctx,
2743 struct samr_UserInfo14 *r,
2746 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2748 return NT_STATUS_OK;
2751 /*************************************************************************
2752 get_user_info_16. Safe. Only gives out acb bits.
2753 *************************************************************************/
2755 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2756 struct samr_UserInfo16 *r,
2757 struct samu *smbpass)
2759 r->acct_flags = pdb_get_acct_ctrl(smbpass);
2761 return NT_STATUS_OK;
2764 /*************************************************************************
2766 *************************************************************************/
2768 static NTSTATUS get_user_info_17(TALLOC_CTX *mem_ctx,
2769 struct samr_UserInfo17 *r,
2772 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2774 return NT_STATUS_OK;
2777 /*************************************************************************
2778 get_user_info_18. OK - this is the killer as it gives out password info.
2779 Ensure that this is only allowed on an encrypted connection with a root
2781 *************************************************************************/
2783 static NTSTATUS get_user_info_18(struct pipes_struct *p,
2784 TALLOC_CTX *mem_ctx,
2785 struct samr_UserInfo18 *r,
2786 struct dom_sid *user_sid)
2788 struct samu *smbpass=NULL;
2790 const uint8_t *nt_pass = NULL;
2791 const uint8_t *lm_pass = NULL;
2795 if (p->session_info->system) {
2799 if ((p->auth.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) ||
2800 (p->auth.auth_type != DCERPC_AUTH_TYPE_KRB5) ||
2801 (p->auth.auth_type != DCERPC_AUTH_TYPE_SPNEGO)) {
2802 return NT_STATUS_ACCESS_DENIED;
2805 if (p->auth.auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
2806 return NT_STATUS_ACCESS_DENIED;
2811 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2814 if ( !(smbpass = samu_new( mem_ctx )) ) {
2815 return NT_STATUS_NO_MEMORY;
2818 ret = pdb_getsampwsid(smbpass, user_sid);
2821 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2822 TALLOC_FREE(smbpass);
2823 return (geteuid() == sec_initial_uid()) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2826 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2828 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2829 TALLOC_FREE(smbpass);
2830 return NT_STATUS_ACCOUNT_DISABLED;
2833 lm_pass = pdb_get_lanman_passwd(smbpass);
2834 if (lm_pass != NULL) {
2835 memcpy(r->lm_pwd.hash, lm_pass, 16);
2836 r->lm_pwd_active = true;
2839 nt_pass = pdb_get_nt_passwd(smbpass);
2840 if (nt_pass != NULL) {
2841 memcpy(r->nt_pwd.hash, nt_pass, 16);
2842 r->nt_pwd_active = true;
2844 r->password_expired = 0; /* FIXME */
2846 TALLOC_FREE(smbpass);
2848 return NT_STATUS_OK;
2851 /*************************************************************************
2853 *************************************************************************/
2855 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2856 struct samr_UserInfo20 *r,
2857 struct samu *sampass)
2859 const char *munged_dial = NULL;
2862 struct lsa_BinaryString *parameters = NULL;
2866 munged_dial = pdb_get_munged_dial(sampass);
2868 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2869 munged_dial, (int)strlen(munged_dial)));
2872 blob = base64_decode_data_blob(munged_dial);
2874 blob = data_blob_string_const_null("");
2877 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2878 data_blob_free(&blob);
2879 if (!NT_STATUS_IS_OK(status)) {
2883 r->parameters = *parameters;
2885 return NT_STATUS_OK;
2889 /*************************************************************************
2891 *************************************************************************/
2893 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2894 struct samr_UserInfo21 *r,
2896 struct dom_sid *domain_sid,
2897 uint32_t acc_granted)
2900 const struct dom_sid *sid_user, *sid_group;
2901 uint32_t rid, primary_gid;
2902 NTTIME force_password_change;
2903 time_t must_change_time;
2904 struct lsa_BinaryString *parameters = NULL;
2905 const char *munged_dial = NULL;
2910 sid_user = pdb_get_user_sid(pw);
2912 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2913 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2914 "the domain sid %s. Failing operation.\n",
2915 pdb_get_username(pw), sid_string_dbg(sid_user),
2916 sid_string_dbg(domain_sid)));
2917 return NT_STATUS_UNSUCCESSFUL;
2921 sid_group = pdb_get_group_sid(pw);
2924 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2925 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2926 "which conflicts with the domain sid %s. Failing operation.\n",
2927 pdb_get_username(pw), sid_string_dbg(sid_group),
2928 sid_string_dbg(domain_sid)));
2929 return NT_STATUS_UNSUCCESSFUL;
2932 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2933 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2934 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2935 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2936 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2938 must_change_time = pdb_get_pass_must_change_time(pw);
2939 if (must_change_time == get_time_t_max()) {
2940 unix_to_nt_time_abs(&force_password_change, must_change_time);
2942 unix_to_nt_time(&force_password_change, must_change_time);
2945 munged_dial = pdb_get_munged_dial(pw);
2947 blob = base64_decode_data_blob(munged_dial);
2949 blob = data_blob_string_const_null("");
2952 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2953 data_blob_free(&blob);
2954 if (!NT_STATUS_IS_OK(status)) {
2958 r->force_password_change = force_password_change;
2960 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2961 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2962 r->home_directory.string = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2963 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2964 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2965 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2966 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2967 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2968 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2970 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2971 r->parameters = *parameters;
2973 r->primary_gid = primary_gid;
2974 r->acct_flags = pdb_get_acct_ctrl(pw);
2975 r->bad_password_count = pdb_get_bad_password_count(pw);
2976 r->logon_count = pdb_get_logon_count(pw);
2977 r->fields_present = pdb_build_fields_present(pw);
2978 r->password_expired = (pdb_get_pass_must_change_time(pw) == 0) ?
2979 PASS_MUST_CHANGE_AT_NEXT_LOGON : 0;
2980 r->country_code = pdb_get_country_code(pw);
2981 r->code_page = pdb_get_code_page(pw);
2982 r->lm_password_set = 0;
2983 r->nt_password_set = 0;
2988 Look at a user on a real NT4 PDC with usrmgr, press
2989 'ok'. Then you will see that fields_present is set to
2990 0x08f827fa. Look at the user immediately after that again,
2991 and you will see that 0x00fffff is returned. This solves
2992 the problem that you get access denied after having looked
3000 return NT_STATUS_OK;
3003 /*******************************************************************
3005 ********************************************************************/
3007 NTSTATUS _samr_QueryUserInfo(struct pipes_struct *p,
3008 struct samr_QueryUserInfo *r)
3011 union samr_UserInfo *user_info = NULL;
3012 struct samr_user_info *uinfo;
3013 struct dom_sid domain_sid;
3016 struct samu *pwd = NULL;
3017 uint32_t acc_required, acc_granted;
3019 switch (r->in.level) {
3020 case 1: /* UserGeneralInformation */
3021 /* USER_READ_GENERAL */
3022 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC;
3024 case 2: /* UserPreferencesInformation */
3025 /* USER_READ_PREFERENCES | USER_READ_GENERAL */
3026 acc_required = SAMR_USER_ACCESS_GET_LOCALE |
3027 SAMR_USER_ACCESS_GET_NAME_ETC;
3029 case 3: /* UserLogonInformation */
3030 /* USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT */
3031 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC |
3032 SAMR_USER_ACCESS_GET_LOCALE |
3033 SAMR_USER_ACCESS_GET_LOGONINFO |
3034 SAMR_USER_ACCESS_GET_ATTRIBUTES;
3036 case 4: /* UserLogonHoursInformation */
3037 /* USER_READ_LOGON */
3038 acc_required = SAMR_USER_ACCESS_GET_LOGONINFO;
3040 case 5: /* UserAccountInformation */
3041 /* USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT */
3042 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC |
3043 SAMR_USER_ACCESS_GET_LOCALE |
3044 SAMR_USER_ACCESS_GET_LOGONINFO |
3045 SAMR_USER_ACCESS_GET_ATTRIBUTES;
3047 case 6: /* UserNameInformation */
3048 case 7: /* UserAccountNameInformation */
3049 case 8: /* UserFullNameInformation */
3050 case 9: /* UserPrimaryGroupInformation */
3051 case 13: /* UserAdminCommentInformation */
3052 /* USER_READ_GENERAL */
3053 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC;
3055 case 10: /* UserHomeInformation */
3056 case 11: /* UserScriptInformation */
3057 case 12: /* UserProfileInformation */
3058 case 14: /* UserWorkStationsInformation */
3059 /* USER_READ_LOGON */
3060 acc_required = SAMR_USER_ACCESS_GET_LOGONINFO;
3062 case 16: /* UserControlInformation */
3063 case 17: /* UserExpiresInformation */
3064 case 20: /* UserParametersInformation */
3065 /* USER_READ_ACCOUNT */
3066 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
3068 case 21: /* UserAllInformation */
3070 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
3072 case 18: /* UserInternal1Information */
3074 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
3076 case 23: /* UserInternal4Information */
3077 case 24: /* UserInternal4InformationNew */
3078 case 25: /* UserInternal4InformationNew */
3079 case 26: /* UserInternal5InformationNew */
3081 return NT_STATUS_INVALID_INFO_CLASS;
3085 uinfo = policy_handle_find(p, r->in.user_handle,
3086 acc_required, &acc_granted,
3087 struct samr_user_info, &status);
3088 if (!NT_STATUS_IS_OK(status)) {
3092 domain_sid = uinfo->sid;
3094 sid_split_rid(&domain_sid, &rid);
3096 if (!sid_check_is_in_our_domain(&uinfo->sid))
3097 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3099 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
3100 sid_string_dbg(&uinfo->sid)));
3102 user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
3104 return NT_STATUS_NO_MEMORY;
3107 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
3109 if (!(pwd = samu_new(p->mem_ctx))) {
3110 return NT_STATUS_NO_MEMORY;
3114 ret = pdb_getsampwsid(pwd, &uinfo->sid);
3118 DEBUG(4,("User %s not found\n", sid_string_dbg(&uinfo->sid)));
3120 return NT_STATUS_NO_SUCH_USER;
3123 DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
3125 samr_clear_sam_passwd(pwd);
3127 switch (r->in.level) {
3129 status = get_user_info_1(p->mem_ctx, &user_info->info1, pwd, &domain_sid);
3132 status = get_user_info_2(p->mem_ctx, &user_info->info2, pwd);
3135 status = get_user_info_3(p->mem_ctx, &user_info->info3, pwd, &domain_sid);
3138 status = get_user_info_4(p->mem_ctx, &user_info->info4, pwd);
3141 status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
3144 status = get_user_info_6(p->mem_ctx, &user_info->info6, pwd);
3147 status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
3150 status = get_user_info_8(p->mem_ctx, &user_info->info8, pwd);
3153 status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
3156 status = get_user_info_10(p->mem_ctx, &user_info->info10, pwd);
3159 status = get_user_info_11(p->mem_ctx, &user_info->info11, pwd);
3162 status = get_user_info_12(p->mem_ctx, &user_info->info12, pwd);
3165 status = get_user_info_13(p->mem_ctx, &user_info->info13, pwd);
3168 status = get_user_info_14(p->mem_ctx, &user_info->info14, pwd);
3171 status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
3174 status = get_user_info_17(p->mem_ctx, &user_info->info17, pwd);
3177 /* level 18 is special */
3178 status = get_user_info_18(p, p->mem_ctx, &user_info->info18,
3182 status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
3185 status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid, acc_granted);
3188 status = NT_STATUS_INVALID_INFO_CLASS;
3192 if (!NT_STATUS_IS_OK(status)) {
3196 *r->out.info = user_info;
3201 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
3206 /****************************************************************
3207 ****************************************************************/
3209 NTSTATUS _samr_QueryUserInfo2(struct pipes_struct *p,
3210 struct samr_QueryUserInfo2 *r)
3212 struct samr_QueryUserInfo u;
3214 u.in.user_handle = r->in.user_handle;
3215 u.in.level = r->in.level;
3216 u.out.info = r->out.info;
3218 return _samr_QueryUserInfo(p, &u);
3221 /*******************************************************************
3222 _samr_GetGroupsForUser
3223 ********************************************************************/
3225 NTSTATUS _samr_GetGroupsForUser(struct pipes_struct *p,
3226 struct samr_GetGroupsForUser *r)
3228 struct samr_user_info *uinfo;
3229 struct samu *sam_pass=NULL;
3230 struct dom_sid *sids;
3231 struct samr_RidWithAttribute dom_gid;
3232 struct samr_RidWithAttribute *gids = NULL;
3233 uint32 primary_group_rid;
3234 uint32_t num_groups = 0;
3236 uint32_t i, num_gids;
3239 bool success = False;
3241 struct samr_RidWithAttributeArray *rids = NULL;
3244 * from the SID in the request:
3245 * we should send back the list of DOMAIN GROUPS
3246 * the user is a member of
3248 * and only the DOMAIN GROUPS
3249 * no ALIASES !!! neither aliases of the domain
3250 * nor aliases of the builtin SID
3255 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
3257 uinfo = policy_handle_find(p, r->in.user_handle,
3258 SAMR_USER_ACCESS_GET_GROUPS, NULL,
3259 struct samr_user_info, &result);
3260 if (!NT_STATUS_IS_OK(result)) {
3264 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
3266 return NT_STATUS_NO_MEMORY;
3269 if (!sid_check_is_in_our_domain(&uinfo->sid))
3270 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3272 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
3273 return NT_STATUS_NO_MEMORY;
3277 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
3281 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
3282 sid_string_dbg(&uinfo->sid)));
3283 return NT_STATUS_NO_SUCH_USER;
3288 /* make both calls inside the root block */
3290 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
3291 &sids, &unix_gids, &num_groups);
3292 if ( NT_STATUS_IS_OK(result) ) {
3293 success = sid_peek_check_rid(get_global_sam_sid(),
3294 pdb_get_group_sid(sam_pass),
3295 &primary_group_rid);
3299 if (!NT_STATUS_IS_OK(result)) {
3300 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
3301 sid_string_dbg(&uinfo->sid)));
3306 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
3307 sid_string_dbg(pdb_get_group_sid(sam_pass)),
3308 pdb_get_username(sam_pass)));
3309 TALLOC_FREE(sam_pass);
3310 return NT_STATUS_INTERNAL_DB_CORRUPTION;
3316 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
3318 dom_gid.rid = primary_group_rid;
3319 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
3321 for (i=0; i<num_groups; i++) {
3323 if (!sid_peek_check_rid(get_global_sam_sid(),
3324 &(sids[i]), &dom_gid.rid)) {
3325 DEBUG(10, ("Found sid %s not in our domain\n",
3326 sid_string_dbg(&sids[i])));
3330 if (dom_gid.rid == primary_group_rid) {
3331 /* We added the primary group directly from the
3332 * sam_account. The other SIDs are unique from
3333 * enum_group_memberships */
3337 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
3340 rids->count = num_gids;
3343 *r->out.rids = rids;
3345 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
3350 /*******************************************************************
3351 ********************************************************************/
3353 static uint32_t samr_get_server_role(void)
3355 uint32_t role = ROLE_DOMAIN_PDC;
3357 if (lp_server_role() == ROLE_DOMAIN_BDC) {
3358 role = ROLE_DOMAIN_BDC;
3364 /*******************************************************************
3365 ********************************************************************/
3367 static NTSTATUS query_dom_info_1(TALLOC_CTX *mem_ctx,
3368 struct samr_DomInfo1 *r)
3370 uint32_t account_policy_temp;
3371 time_t u_expire, u_min_age;
3377 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN, &account_policy_temp);
3378 r->min_password_length = account_policy_temp;
3380 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &account_policy_temp);
3381 r->password_history_length = account_policy_temp;
3383 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
3384 &r->password_properties);
3386 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &account_policy_temp);
3387 u_expire = account_policy_temp;
3389 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &account_policy_temp);
3390 u_min_age = account_policy_temp;
3396 unix_to_nt_time_abs((NTTIME *)&r->max_password_age, u_expire);
3397 unix_to_nt_time_abs((NTTIME *)&r->min_password_age, u_min_age);
3399 if (lp_check_password_script() && *lp_check_password_script()) {
3400 r->password_properties |= DOMAIN_PASSWORD_COMPLEX;
3403 return NT_STATUS_OK;
3406 /*******************************************************************
3407 ********************************************************************/
3409 static NTSTATUS query_dom_info_2(TALLOC_CTX *mem_ctx,
3410 struct samr_DomGeneralInformation *r,
3411 struct samr_domain_info *dinfo)
3420 r->num_users = count_sam_users(dinfo->disp_info, ACB_NORMAL);
3421 r->num_groups = count_sam_groups(dinfo->disp_info);
3422 r->num_aliases = count_sam_aliases(dinfo->disp_info);
3424 pdb_get_account_policy(PDB_POLICY_TIME_TO_LOGOUT, &u_logout);
3426 unix_to_nt_time_abs(&r->force_logoff_time, u_logout);
3428 if (!pdb_get_seq_num(&seq_num)) {
3429 seq_num = time(NULL);
3436 r->oem_information.string = lp_serverstring();
3437 r->domain_name.string = lp_workgroup();
3438 r->primary.string = global_myname();
3439 r->sequence_num = seq_num;
3440 r->domain_server_state = DOMAIN_SERVER_ENABLED;
3441 r->role = (enum samr_Role) samr_get_server_role();
3444 return NT_STATUS_OK;
3447 /*******************************************************************
3448 ********************************************************************/
3450 static NTSTATUS query_dom_info_3(TALLOC_CTX *mem_ctx,
3451 struct samr_DomInfo3 *r)
3461 pdb_get_account_policy(PDB_POLICY_TIME_TO_LOGOUT, &ul);
3462 u_logout = (time_t)ul;
3469 unix_to_nt_time_abs(&r->force_logoff_time, u_logout);
3471 return NT_STATUS_OK;
3474 /*******************************************************************
3475 ********************************************************************/
3477 static NTSTATUS query_dom_info_4(TALLOC_CTX *mem_ctx,
3478 struct samr_DomOEMInformation *r)
3480 r->oem_information.string = lp_serverstring();
3482 return NT_STATUS_OK;
3485 /*******************************************************************
3486 ********************************************************************/
3488 static NTSTATUS query_dom_info_5(TALLOC_CTX *mem_ctx,
3489 struct samr_DomInfo5 *r)
3491 r->domain_name.string = get_global_sam_name();
3493 return NT_STATUS_OK;
3496 /*******************************************************************
3497 ********************************************************************/
3499 static NTSTATUS query_dom_info_6(TALLOC_CTX *mem_ctx,
3500 struct samr_DomInfo6 *r)
3502 /* NT returns its own name when a PDC. win2k and later
3503 * only the name of the PDC if itself is a BDC (samba4
3505 r->primary.string = global_myname();
3507 return NT_STATUS_OK;
3510 /*******************************************************************
3511 ********************************************************************/
3513 static NTSTATUS query_dom_info_7(TALLOC_CTX *mem_ctx,
3514 struct samr_DomInfo7 *r)
3516 r->role = (enum samr_Role) samr_get_server_role();
3518 return NT_STATUS_OK;
3521 /*******************************************************************
3522 ********************************************************************/
3524 static NTSTATUS query_dom_info_8(TALLOC_CTX *mem_ctx,
3525 struct samr_DomInfo8 *r)
3533 if (!pdb_get_seq_num(&seq_num)) {
3534 seq_num = time(NULL);
3541 r->sequence_num = seq_num;
3542 r->domain_create_time = 0;
3544 return NT_STATUS_OK;
3547 /*******************************************************************
3548 ********************************************************************/
3550 static NTSTATUS query_dom_info_9(TALLOC_CTX *mem_ctx,
3551 struct samr_DomInfo9 *r)
3553 r->domain_server_state = DOMAIN_SERVER_ENABLED;
3555 return NT_STATUS_OK;
3558 /*******************************************************************
3559 ********************************************************************/
3561 static NTSTATUS query_dom_info_11(TALLOC_CTX *mem_ctx,
3562 struct samr_DomGeneralInformation2 *r,
3563 struct samr_domain_info *dinfo)
3566 uint32_t account_policy_temp;
3567 time_t u_lock_duration, u_reset_time;
3569 status = query_dom_info_2(mem_ctx, &r->general, dinfo);
3570 if (!NT_STATUS_IS_OK(status)) {
3578 pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3579 u_lock_duration = account_policy_temp;
3580 if (u_lock_duration != -1) {
3581 u_lock_duration *= 60;
3584 pdb_get_account_policy(PDB_POLICY_RESET_COUNT_TIME, &account_policy_temp);
3585 u_reset_time = account_policy_temp * 60;
3587 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3588 r->lockout_threshold = account_policy_temp;
3594 unix_to_nt_time_abs(&r->lockout_duration, u_lock_duration);
3595 unix_to_nt_time_abs(&r->lockout_window, u_reset_time);
3597 return NT_STATUS_OK;
3600 /*******************************************************************
3601 ********************************************************************/
3603 static NTSTATUS query_dom_info_12(TALLOC_CTX *mem_ctx,
3604 struct samr_DomInfo12 *r)
3606 uint32_t account_policy_temp;
3607 time_t u_lock_duration, u_reset_time;
3613 pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3614 u_lock_duration = account_policy_temp;
3615 if (u_lock_duration != -1) {
3616 u_lock_duration *= 60;
3619 pdb_get_account_policy(PDB_POLICY_RESET_COUNT_TIME, &account_policy_temp);
3620 u_reset_time = account_policy_temp * 60;
3622 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3623 r->lockout_threshold = account_policy_temp;
3629 unix_to_nt_time_abs(&r->lockout_duration, u_lock_duration);
3630 unix_to_nt_time_abs(&r->lockout_window, u_reset_time);
3632 return NT_STATUS_OK;
3635 /*******************************************************************
3636 ********************************************************************/
3638 static NTSTATUS query_dom_info_13(TALLOC_CTX *mem_ctx,
3639 struct samr_DomInfo13 *r)
3647 if (!pdb_get_seq_num(&seq_num)) {
3648 seq_num = time(NULL);
3655 r->sequence_num = seq_num;
3656 r->domain_create_time = 0;
3657 r->modified_count_at_last_promotion = 0;
3659 return NT_STATUS_OK;
3662 /*******************************************************************
3663 _samr_QueryDomainInfo
3664 ********************************************************************/
3666 NTSTATUS _samr_QueryDomainInfo(struct pipes_struct *p,
3667 struct samr_QueryDomainInfo *r)
3669 NTSTATUS status = NT_STATUS_OK;
3670 struct samr_domain_info *dinfo;
3671 union samr_DomainInfo *dom_info;
3673 uint32_t acc_required;
3675 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3677 switch (r->in.level) {
3678 case 1: /* DomainPasswordInformation */
3679 case 12: /* DomainLockoutInformation */
3680 /* DOMAIN_READ_PASSWORD_PARAMETERS */
3681 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1;
3683 case 11: /* DomainGeneralInformation2 */
3684 /* DOMAIN_READ_PASSWORD_PARAMETERS |
3685 * DOMAIN_READ_OTHER_PARAMETERS */
3686 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1 |
3687 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2;
3689 case 2: /* DomainGeneralInformation */
3690 case 3: /* DomainLogoffInformation */
3691 case 4: /* DomainOemInformation */
3692 case 5: /* DomainReplicationInformation */
3693 case 6: /* DomainReplicationInformation */
3694 case 7: /* DomainServerRoleInformation */
3695 case 8: /* DomainModifiedInformation */
3696 case 9: /* DomainStateInformation */
3697 case 10: /* DomainUasInformation */
3698 case 13: /* DomainModifiedInformation2 */
3699 /* DOMAIN_READ_OTHER_PARAMETERS */
3700 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2;
3703 return NT_STATUS_INVALID_INFO_CLASS;
3706 dinfo = policy_handle_find(p, r->in.domain_handle,
3708 struct samr_domain_info, &status);
3709 if (!NT_STATUS_IS_OK(status)) {
3713 dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
3715 return NT_STATUS_NO_MEMORY;
3718 switch (r->in.level) {
3720 status = query_dom_info_1(p->mem_ctx, &dom_info->info1);
3723 status = query_dom_info_2(p->mem_ctx, &dom_info->general, dinfo);
3726 status = query_dom_info_3(p->mem_ctx, &dom_info->info3);
3729 status = query_dom_info_4(p->mem_ctx, &dom_info->oem);
3732 status = query_dom_info_5(p->mem_ctx, &dom_info->info5);
3735 status = query_dom_info_6(p->mem_ctx, &dom_info->info6);
3738 status = query_dom_info_7(p->mem_ctx, &dom_info->info7);
3741 status = query_dom_info_8(p->mem_ctx, &dom_info->info8);
3744 status = query_dom_info_9(p->mem_ctx, &dom_info->info9);
3747 status = query_dom_info_11(p->mem_ctx, &dom_info->general2, dinfo);
3750 status = query_dom_info_12(p->mem_ctx, &dom_info->info12);
3753 status = query_dom_info_13(p->mem_ctx, &dom_info->info13);
3756 return NT_STATUS_INVALID_INFO_CLASS;
3759 if (!NT_STATUS_IS_OK(status)) {
3763 *r->out.info = dom_info;
3765 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3770 /* W2k3 seems to use the same check for all 3 objects that can be created via
3771 * SAMR, if you try to create for example "Dialup" as an alias it says
3772 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3775 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3777 enum lsa_SidType type;
3780 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3783 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3784 * whether the name already exists */
3785 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3786 NULL, NULL, NULL, &type);
3790 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3791 return NT_STATUS_OK;
3794 DEBUG(5, ("trying to create %s, exists as %s\n",
3795 new_name, sid_type_lookup(type)));
3797 if (type == SID_NAME_DOM_GRP) {
3798 return NT_STATUS_GROUP_EXISTS;
3800 if (type == SID_NAME_ALIAS) {
3801 return NT_STATUS_ALIAS_EXISTS;
3804 /* Yes, the default is NT_STATUS_USER_EXISTS */
3805 return NT_STATUS_USER_EXISTS;
3808 /*******************************************************************
3810 ********************************************************************/
3812 NTSTATUS _samr_CreateUser2(struct pipes_struct *p,
3813 struct samr_CreateUser2 *r)
3815 const char *account = NULL;
3817 uint32_t acb_info = r->in.acct_flags;
3818 struct samr_domain_info *dinfo;
3819 struct samr_user_info *uinfo;
3822 struct security_descriptor *psd;
3824 /* check this, when giving away 'add computer to domain' privs */
3825 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3826 bool can_add_account = False;
3828 /* Which privilege is needed to override the ACL? */
3829 enum sec_privilege needed_priv = SEC_PRIV_INVALID;
3831 dinfo = policy_handle_find(p, r->in.domain_handle,
3832 SAMR_DOMAIN_ACCESS_CREATE_USER, NULL,
3833 struct samr_domain_info, &nt_status);
3834 if (!NT_STATUS_IS_OK(nt_status)) {
3838 if (sid_check_is_builtin(&dinfo->sid)) {
3839 DEBUG(5,("_samr_CreateUser2: Refusing user create in BUILTIN\n"));
3840 return NT_STATUS_ACCESS_DENIED;
3843 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3844 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3845 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3846 this parameter is not an account type */
3847 return NT_STATUS_INVALID_PARAMETER;
3850 account = r->in.account_name->string;
3851 if (account == NULL) {
3852 return NT_STATUS_NO_MEMORY;
3855 nt_status = can_create(p->mem_ctx, account);
3856 if (!NT_STATUS_IS_OK(nt_status)) {
3860 /* determine which user right we need to check based on the acb_info */
3862 if (geteuid() == sec_initial_uid()) {
3863 can_add_account = true;
3864 } else if (acb_info & ACB_WSTRUST) {
3865 needed_priv = SEC_PRIV_MACHINE_ACCOUNT;
3866 can_add_account = security_token_has_privilege(p->session_info->security_token, SEC_PRIV_MACHINE_ACCOUNT);
3867 } else if (acb_info & ACB_NORMAL &&
3868 (account[strlen(account)-1] != '$')) {
3869 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3870 account for domain trusts and changes the ACB flags later */
3871 needed_priv = SEC_PRIV_ADD_USERS;
3872 can_add_account = security_token_has_privilege(p->session_info->security_token, SEC_PRIV_ADD_USERS);
3873 } else if (lp_enable_privileges()) {
3874 /* implicit assumption of a BDC or domain trust account here
3875 * (we already check the flags earlier) */
3876 /* only Domain Admins can add a BDC or domain trust */
3877 can_add_account = nt_token_check_domain_rid(
3878 p->session_info->security_token,
3879 DOMAIN_RID_ADMINS );
3882 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3883 uidtoname(p->session_info->utok.uid),
3884 can_add_account ? "True":"False" ));
3886 if (!can_add_account) {
3887 return NT_STATUS_ACCESS_DENIED;
3890 /********** BEGIN Admin BLOCK **********/
3893 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3897 /********** END Admin BLOCK **********/
3899 /* now check for failure */
3901 if ( !NT_STATUS_IS_OK(nt_status) )
3904 /* Get the user's SID */
3906 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3908 map_max_allowed_access(p->session_info->security_token,
3909 &p->session_info->utok,
3912 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3913 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3914 se_map_generic(&des_access, &usr_generic_mapping);
3917 * JRA - TESTME. We just created this user so we
3918 * had rights to create them. Do we need to check
3919 * any further access on this object ? Can't we
3920 * just assume we have all the rights we need ?
3923 nt_status = access_check_object(psd, p->session_info->security_token,
3924 needed_priv, SEC_PRIV_INVALID,
3925 GENERIC_RIGHTS_USER_WRITE, des_access,
3926 &acc_granted, "_samr_CreateUser2");
3928 if ( !NT_STATUS_IS_OK(nt_status) ) {
3932 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
3933 struct samr_user_info, &nt_status);
3934 if (!NT_STATUS_IS_OK(nt_status)) {
3939 /* After a "set" ensure we have no cached display info. */
3940 force_flush_samr_cache(&sid);
3942 *r->out.access_granted = acc_granted;
3944 return NT_STATUS_OK;
3947 /****************************************************************
3948 ****************************************************************/
3950 NTSTATUS _samr_CreateUser(struct pipes_struct *p,
3951 struct samr_CreateUser *r)
3953 struct samr_CreateUser2 c;
3954 uint32_t access_granted;
3956 c.in.domain_handle = r->in.domain_handle;
3957 c.in.account_name = r->in.account_name;
3958 c.in.acct_flags = ACB_NORMAL;
3959 c.in.access_mask = r->in.access_mask;
3960 c.out.user_handle = r->out.user_handle;
3961 c.out.access_granted = &access_granted;
3962 c.out.rid = r->out.rid;
3964 return _samr_CreateUser2(p, &c);
3967 /*******************************************************************
3969 ********************************************************************/
3971 NTSTATUS _samr_Connect(struct pipes_struct *p,
3972 struct samr_Connect *r)
3974 struct samr_connect_info *info;
3975 uint32_t acc_granted;
3976 struct policy_handle hnd;
3977 uint32 des_access = r->in.access_mask;
3982 if (!pipe_access_check(p)) {
3983 DEBUG(3, ("access denied to _samr_Connect\n"));
3984 return NT_STATUS_ACCESS_DENIED;
3987 /* don't give away the farm but this is probably ok. The SAMR_ACCESS_ENUM_DOMAINS
3988 was observed from a win98 client trying to enumerate users (when configured
3989 user level access control on shares) --jerry */
3991 map_max_allowed_access(p->session_info->security_token,
3992 &p->session_info->utok,
3995 se_map_generic( &des_access, &sam_generic_mapping );
3997 acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS
3998 |SAMR_ACCESS_LOOKUP_DOMAIN);
4000 /* set up the SAMR connect_anon response */
4002 info = policy_handle_create(p, &hnd, acc_granted,
4003 struct samr_connect_info,
4005 if (!NT_STATUS_IS_OK(status)) {
4009 *r->out.connect_handle = hnd;
4010 return NT_STATUS_OK;
4013 /*******************************************************************
4015 ********************************************************************/
4017 NTSTATUS _samr_Connect2(struct pipes_struct *p,
4018 struct samr_Connect2 *r)
4020 struct samr_connect_info *info = NULL;
4021 struct policy_handle hnd;
4022 struct security_descriptor *psd = NULL;
4024 uint32 des_access = r->in.access_mask;
4027 const char *fn = "_samr_Connect2";
4030 case NDR_SAMR_CONNECT2:
4031 fn = "_samr_Connect2";
4033 case NDR_SAMR_CONNECT3:
4034 fn = "_samr_Connect3";
4036 case NDR_SAMR_CONNECT4:
4037 fn = "_samr_Connect4";
4039 case NDR_SAMR_CONNECT5:
4040 fn = "_samr_Connect5";
4044 DEBUG(5,("%s: %d\n", fn, __LINE__));
4048 if (!pipe_access_check(p)) {
4049 DEBUG(3, ("access denied to %s\n", fn));
4050 return NT_STATUS_ACCESS_DENIED;
4053 map_max_allowed_access(p->session_info->security_token,
4054 &p->session_info->utok,
4057 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
4058 se_map_generic(&des_access, &sam_generic_mapping);
4060 nt_status = access_check_object(psd, p->session_info->security_token,
4061 SEC_PRIV_INVALID, SEC_PRIV_INVALID,
4062 0, des_access, &acc_granted, fn);
4064 if ( !NT_STATUS_IS_OK(nt_status) )
4067 info = policy_handle_create(p, &hnd, acc_granted,
4068 struct samr_connect_info, &nt_status);
4069 if (!NT_STATUS_IS_OK(nt_status)) {
4073 DEBUG(5,("%s: %d\n", fn, __LINE__));
4075 *r->out.connect_handle = hnd;
4076 return NT_STATUS_OK;
4079 /****************************************************************
4081 ****************************************************************/
4083 NTSTATUS _samr_Connect3(struct pipes_struct *p,
4084 struct samr_Connect3 *r)
4086 struct samr_Connect2 c;
4088 c.in.system_name = r->in.system_name;
4089 c.in.access_mask = r->in.access_mask;
4090 c.out.connect_handle = r->out.connect_handle;
4092 return _samr_Connect2(p, &c);
4095 /*******************************************************************
4097 ********************************************************************/
4099 NTSTATUS _samr_Connect4(struct pipes_struct *p,
4100 struct samr_Connect4 *r)
4102 struct samr_Connect2 c;
4104 c.in.system_name = r->in.system_name;
4105 c.in.access_mask = r->in.access_mask;
4106 c.out.connect_handle = r->out.connect_handle;
4108 return _samr_Connect2(p, &c);
4111 /*******************************************************************
4113 ********************************************************************/
4115 NTSTATUS _samr_Connect5(struct pipes_struct *p,
4116 struct samr_Connect5 *r)
4119 struct samr_Connect2 c;
4120 struct samr_ConnectInfo1 info1;
4122 info1.client_version = SAMR_CONNECT_AFTER_W2K;
4125 c.in.system_name = r->in.system_name;
4126 c.in.access_mask = r->in.access_mask;
4127 c.out.connect_handle = r->out.connect_handle;
4129 *r->out.level_out = 1;
4131 status = _samr_Connect2(p, &c);
4132 if (!NT_STATUS_IS_OK(status)) {
4136 r->out.info_out->info1 = info1;
4138 return NT_STATUS_OK;
4141 /**********************************************************************
4143 **********************************************************************/
4145 NTSTATUS _samr_LookupDomain(struct pipes_struct *p,
4146 struct samr_LookupDomain *r)
4149 struct samr_connect_info *info;
4150 const char *domain_name;
4151 struct dom_sid *sid = NULL;
4153 /* win9x user manager likes to use SAMR_ACCESS_ENUM_DOMAINS here.
4154 Reverted that change so we will work with RAS servers again */
4156 info = policy_handle_find(p, r->in.connect_handle,
4157 SAMR_ACCESS_LOOKUP_DOMAIN, NULL,
4158 struct samr_connect_info,
4160 if (!NT_STATUS_IS_OK(status)) {
4164 domain_name = r->in.domain_name->string;
4166 return NT_STATUS_INVALID_PARAMETER;
4169 sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
4171 return NT_STATUS_NO_MEMORY;
4174 if (strequal(domain_name, builtin_domain_name())) {
4175 sid_copy(sid, &global_sid_Builtin);
4177 if (!secrets_fetch_domain_sid(domain_name, sid)) {
4178 status = NT_STATUS_NO_SUCH_DOMAIN;
4182 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
4183 sid_string_dbg(sid)));
4190 /**********************************************************************
4192 **********************************************************************/
4194 NTSTATUS _samr_EnumDomains(struct pipes_struct *p,
4195 struct samr_EnumDomains *r)
4198 struct samr_connect_info *info;
4199 uint32_t num_entries = 2;
4200 struct samr_SamEntry *entry_array = NULL;
4201 struct samr_SamArray *sam;
4203 info = policy_handle_find(p, r->in.connect_handle,
4204 SAMR_ACCESS_ENUM_DOMAINS, NULL,
4205 struct samr_connect_info, &status);
4206 if (!NT_STATUS_IS_OK(status)) {
4210 sam = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
4212 return NT_STATUS_NO_MEMORY;
4215 entry_array = TALLOC_ZERO_ARRAY(p->mem_ctx,
4216 struct samr_SamEntry,
4219 return NT_STATUS_NO_MEMORY;
4222 entry_array[0].idx = 0;
4223 init_lsa_String(&entry_array[0].name, get_global_sam_name());
4225 entry_array[1].idx = 1;
4226 init_lsa_String(&entry_array[1].name, "Builtin");
4228 sam->count = num_entries;
4229 sam->entries = entry_array;
4232 *r->out.num_entries = num_entries;
4237 /*******************************************************************
4239 ********************************************************************/
4241 NTSTATUS _samr_OpenAlias(struct pipes_struct *p,
4242 struct samr_OpenAlias *r)
4245 uint32 alias_rid = r->in.rid;
4246 struct samr_alias_info *ainfo;
4247 struct samr_domain_info *dinfo;
4248 struct security_descriptor *psd = NULL;
4250 uint32 des_access = r->in.access_mask;
4254 dinfo = policy_handle_find(p, r->in.domain_handle,
4255 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
4256 struct samr_domain_info, &status);
4257 if (!NT_STATUS_IS_OK(status)) {
4261 /* append the alias' RID to it */
4263 if (!sid_compose(&sid, &dinfo->sid, alias_rid))
4264 return NT_STATUS_NO_SUCH_ALIAS;
4266 /*check if access can be granted as requested by client. */
4268 map_max_allowed_access(p->session_info->security_token,
4269 &p->session_info->utok,
4272 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
4273 se_map_generic(&des_access,&ali_generic_mapping);
4275 status = access_check_object(psd, p->session_info->security_token,
4276 SEC_PRIV_ADD_USERS, SEC_PRIV_INVALID,
4277 GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
4278 des_access, &acc_granted, "_samr_OpenAlias");
4280 if ( !NT_STATUS_IS_OK(status) )
4284 /* Check we actually have the requested alias */
4285 enum lsa_SidType type;
4290 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
4293 if (!result || (type != SID_NAME_ALIAS)) {
4294 return NT_STATUS_NO_SUCH_ALIAS;
4297 /* make sure there is a mapping */
4299 if ( !sid_to_gid( &sid, &gid ) ) {
4300 return NT_STATUS_NO_SUCH_ALIAS;
4305 ainfo = policy_handle_create(p, r->out.alias_handle, acc_granted,
4306 struct samr_alias_info, &status);
4307 if (!NT_STATUS_IS_OK(status)) {
4312 return NT_STATUS_OK;
4315 /*******************************************************************
4317 ********************************************************************/
4319 static NTSTATUS set_user_info_2(TALLOC_CTX *mem_ctx,
4320 struct samr_UserInfo2 *id2,
4324 DEBUG(5,("set_user_info_2: NULL id2\n"));
4325 return NT_STATUS_ACCESS_DENIED;
4328 copy_id2_to_sam_passwd(pwd, id2);
4330 return pdb_update_sam_account(pwd);
4333 /*******************************************************************
4335 ********************************************************************/
4337 static NTSTATUS set_user_info_4(TALLOC_CTX *mem_ctx,
4338 struct samr_UserInfo4 *id4,
4342 DEBUG(5,("set_user_info_2: NULL id4\n"));
4343 return NT_STATUS_ACCESS_DENIED;
4346 copy_id4_to_sam_passwd(pwd, id4);
4348 return pdb_update_sam_account(pwd);
4351 /*******************************************************************
4353 ********************************************************************/
4355 static NTSTATUS set_user_info_6(TALLOC_CTX *mem_ctx,
4356 struct samr_UserInfo6 *id6,
4360 DEBUG(5,("set_user_info_6: NULL id6\n"));
4361 return NT_STATUS_ACCESS_DENIED;
4364 copy_id6_to_sam_passwd(pwd, id6);
4366 return pdb_update_sam_account(pwd);
4369 /*******************************************************************
4371 ********************************************************************/
4373 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
4374 struct samr_UserInfo7 *id7,
4380 DEBUG(5, ("set_user_info_7: NULL id7\n"));
4381 return NT_STATUS_ACCESS_DENIED;
4384 if (!id7->account_name.string) {
4385 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
4386 return NT_STATUS_ACCESS_DENIED;
4389 /* check to see if the new username already exists. Note: we can't
4390 reliably lock all backends, so there is potentially the
4391 possibility that a user can be created in between this check and
4392 the rename. The rename should fail, but may not get the
4393 exact same failure status code. I think this is small enough
4394 of a window for this type of operation and the results are
4395 simply that the rename fails with a slightly different status
4396 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
4398 rc = can_create(mem_ctx, id7->account_name.string);
4400 /* when there is nothing to change, we're done here */
4401 if (NT_STATUS_EQUAL(rc, NT_STATUS_USER_EXISTS) &&
4402 strequal(id7->account_name.string, pdb_get_username(pwd))) {
4403 return NT_STATUS_OK;
4405 if (!NT_STATUS_IS_OK(rc)) {
4409 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
4414 /*******************************************************************
4416 ********************************************************************/
4418 static NTSTATUS set_user_info_8(TALLOC_CTX *mem_ctx,
4419 struct samr_UserInfo8 *id8,
4423 DEBUG(5,("set_user_info_8: NULL id8\n"));
4424 return NT_STATUS_ACCESS_DENIED;
4427 copy_id8_to_sam_passwd(pwd, id8);
4429 return pdb_update_sam_account(pwd);
4432 /*******************************************************************
4434 ********************************************************************/
4436 static NTSTATUS set_user_info_10(TALLOC_CTX *mem_ctx,
4437 struct samr_UserInfo10 *id10,
4441 DEBUG(5,("set_user_info_8: NULL id10\n"));
4442 return NT_STATUS_ACCESS_DENIED;
4445 copy_id10_to_sam_passwd(pwd, id10);
4447 return pdb_update_sam_account(pwd);
4450 /*******************************************************************
4452 ********************************************************************/
4454 static NTSTATUS set_user_info_11(TALLOC_CTX *mem_ctx,
4455 struct samr_UserInfo11 *id11,
4459 DEBUG(5,("set_user_info_11: NULL id11\n"));
4460 return NT_STATUS_ACCESS_DENIED;
4463 copy_id11_to_sam_passwd(pwd, id11);
4465 return pdb_update_sam_account(pwd);
4468 /*******************************************************************
4470 ********************************************************************/
4472 static NTSTATUS set_user_info_12(TALLOC_CTX *mem_ctx,
4473 struct samr_UserInfo12 *id12,
4477 DEBUG(5,("set_user_info_12: NULL id12\n"));
4478 return NT_STATUS_ACCESS_DENIED;
4481 copy_id12_to_sam_passwd(pwd, id12);
4483 return pdb_update_sam_account(pwd);
4486 /*******************************************************************
4488 ********************************************************************/
4490 static NTSTATUS set_user_info_13(TALLOC_CTX *mem_ctx,
4491 struct samr_UserInfo13 *id13,
4495 DEBUG(5,("set_user_info_13: NULL id13\n"));
4496 return NT_STATUS_ACCESS_DENIED;
4499 copy_id13_to_sam_passwd(pwd, id13);
4501 return pdb_update_sam_account(pwd);
4504 /*******************************************************************
4506 ********************************************************************/
4508 static NTSTATUS set_user_info_14(TALLOC_CTX *mem_ctx,
4509 struct samr_UserInfo14 *id14,
4513 DEBUG(5,("set_user_info_14: NULL id14\n"));
4514 return NT_STATUS_ACCESS_DENIED;
4517 copy_id14_to_sam_passwd(pwd, id14);
4519 return pdb_update_sam_account(pwd);
4522 /*******************************************************************
4524 ********************************************************************/
4526 static NTSTATUS set_user_info_16(TALLOC_CTX *mem_ctx,
4527 struct samr_UserInfo16 *id16,
4531 DEBUG(5,("set_user_info_16: NULL id16\n"));
4532 return NT_STATUS_ACCESS_DENIED;
4535 copy_id16_to_sam_passwd(pwd, id16);
4537 return pdb_update_sam_account(pwd);
4540 /*******************************************************************
4542 ********************************************************************/
4544 static NTSTATUS set_user_info_17(TALLOC_CTX *mem_ctx,
4545 struct samr_UserInfo17 *id17,
4549 DEBUG(5,("set_user_info_17: NULL id17\n"));
4550 return NT_STATUS_ACCESS_DENIED;
4553 copy_id17_to_sam_passwd(pwd, id17);
4555 return pdb_update_sam_account(pwd);
4558 /*******************************************************************
4560 ********************************************************************/
4562 static NTSTATUS set_user_info_18(struct samr_UserInfo18 *id18,
4563 TALLOC_CTX *mem_ctx,
4564 DATA_BLOB *session_key,
4568 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
4569 return NT_STATUS_INVALID_PARAMETER;
4572 if (id18->nt_pwd_active || id18->lm_pwd_active) {
4573 if (!session_key->length) {
4574 return NT_STATUS_NO_USER_SESSION_KEY;
4578 if (id18->nt_pwd_active) {
4582 in = data_blob_const(id18->nt_pwd.hash, 16);
4583 out = data_blob_talloc_zero(mem_ctx, 16);
4585 sess_crypt_blob(&out, &in, session_key, false);
4587 if (!pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED)) {
4588 return NT_STATUS_ACCESS_DENIED;
4591 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4594 if (id18->lm_pwd_active) {
4598 in = data_blob_const(id18->lm_pwd.hash, 16);
4599 out = data_blob_talloc_zero(mem_ctx, 16);
4601 sess_crypt_blob(&out, &in, session_key, false);
4603 if (!pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED)) {
4604 return NT_STATUS_ACCESS_DENIED;
4607 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4610 copy_id18_to_sam_passwd(pwd, id18);
4612 return pdb_update_sam_account(pwd);
4615 /*******************************************************************
4617 ********************************************************************/
4619 static NTSTATUS set_user_info_20(TALLOC_CTX *mem_ctx,
4620 struct samr_UserInfo20 *id20,
4624 DEBUG(5,("set_user_info_20: NULL id20\n"));
4625 return NT_STATUS_ACCESS_DENIED;
4628 copy_id20_to_sam_passwd(pwd, id20);
4630 return pdb_update_sam_account(pwd);
4633 /*******************************************************************
4635 ********************************************************************/
4637 static NTSTATUS set_user_info_21(struct samr_UserInfo21 *id21,
4638 TALLOC_CTX *mem_ctx,
4639 DATA_BLOB *session_key,
4645 DEBUG(5, ("set_user_info_21: NULL id21\n"));
4646 return NT_STATUS_INVALID_PARAMETER;
4649 if (id21->fields_present == 0) {
4650 return NT_STATUS_INVALID_PARAMETER;
4653 if (id21->fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4654 return NT_STATUS_ACCESS_DENIED;
4657 if (id21->fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) {
4658 if (id21->nt_password_set) {
4661 if ((id21->nt_owf_password.length != 16) ||
4662 (id21->nt_owf_password.size != 16)) {
4663 return NT_STATUS_INVALID_PARAMETER;
4666 if (!session_key->length) {
4667 return NT_STATUS_NO_USER_SESSION_KEY;
4670 in = data_blob_const(id21->nt_owf_password.array, 16);
4671 out = data_blob_talloc_zero(mem_ctx, 16);
4673 sess_crypt_blob(&out, &in, session_key, false);
4675 pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED);
4676 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4680 if (id21->fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT) {
4681 if (id21->lm_password_set) {
4684 if ((id21->lm_owf_password.length != 16) ||
4685 (id21->lm_owf_password.size != 16)) {
4686 return NT_STATUS_INVALID_PARAMETER;
4689 if (!session_key->length) {
4690 return NT_STATUS_NO_USER_SESSION_KEY;
4693 in = data_blob_const(id21->lm_owf_password.array, 16);
4694 out = data_blob_talloc_zero(mem_ctx, 16);
4696 sess_crypt_blob(&out, &in, session_key, false);
4698 pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED);
4699 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4703 /* we need to separately check for an account rename first */
4705 if (id21->account_name.string &&
4706 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
4709 /* check to see if the new username already exists. Note: we can't
4710 reliably lock all backends, so there is potentially the
4711 possibility that a user can be created in between this check and
4712 the rename. The rename should fail, but may not get the
4713 exact same failure status code. I think this is small enough
4714 of a window for this type of operation and the results are
4715 simply that the rename fails with a slightly different status
4716 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
4718 status = can_create(mem_ctx, id21->account_name.string);
4719 if (!NT_STATUS_IS_OK(status)) {
4723 status = pdb_rename_sam_account(pwd, id21->account_name.string);
4725 if (!NT_STATUS_IS_OK(status)) {
4726 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
4727 nt_errstr(status)));
4731 /* set the new username so that later
4732 functions can work on the new account */
4733 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
4736 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
4739 * The funny part about the previous two calls is
4740 * that pwd still has the password hashes from the
4741 * passdb entry. These have not been updated from
4742 * id21. I don't know if they need to be set. --jerry
4745 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4746 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4747 if ( !NT_STATUS_IS_OK(status) ) {
4752 /* Don't worry about writing out the user account since the
4753 primary group SID is generated solely from the user's Unix
4756 /* write the change out */
4757 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4761 return NT_STATUS_OK;
4764 /*******************************************************************
4766 ********************************************************************/
4768 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
4769 struct samr_UserInfo23 *id23,
4773 char *plaintext_buf = NULL;
4779 DEBUG(5, ("set_user_info_23: NULL id23\n"));
4780 return NT_STATUS_INVALID_PARAMETER;
4783 if (id23->info.fields_present == 0) {
4784 return NT_STATUS_INVALID_PARAMETER;
4787 if (id23->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4788 return NT_STATUS_ACCESS_DENIED;
4791 if ((id23->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4792 (id23->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4794 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
4795 pdb_get_username(pwd)));
4797 if (!decode_pw_buffer(mem_ctx,
4798 id23->password.data,
4802 return NT_STATUS_WRONG_PASSWORD;
4805 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4806 return NT_STATUS_ACCESS_DENIED;
4810 copy_id23_to_sam_passwd(pwd, id23);
4812 acct_ctrl = pdb_get_acct_ctrl(pwd);
4814 /* if it's a trust account, don't update /etc/passwd */
4815 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4816 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4817 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4818 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
4819 } else if (plaintext_buf) {
4820 /* update the UNIX password */
4821 if (lp_unix_password_sync() ) {
4822 struct passwd *passwd;
4823 if (pdb_get_username(pwd) == NULL) {
4824 DEBUG(1, ("chgpasswd: User without name???\n"));
4825 return NT_STATUS_ACCESS_DENIED;
4828 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4829 if (passwd == NULL) {
4830 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4833 if(!chgpasswd(pdb_get_username(pwd), rhost,
4834 passwd, "", plaintext_buf, True)) {
4835 return NT_STATUS_ACCESS_DENIED;
4837 TALLOC_FREE(passwd);
4841 if (plaintext_buf) {
4842 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4845 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
4846 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
4851 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4855 return NT_STATUS_OK;
4858 /*******************************************************************
4860 ********************************************************************/
4862 static bool set_user_info_pw(uint8 *pass, const char *rhost, struct samu *pwd)
4865 char *plaintext_buf = NULL;
4868 DEBUG(5, ("Attempting administrator password change for user %s\n",
4869 pdb_get_username(pwd)));
4871 acct_ctrl = pdb_get_acct_ctrl(pwd);
4873 if (!decode_pw_buffer(talloc_tos(),
4881 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4885 /* if it's a trust account, don't update /etc/passwd */
4886 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4887 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4888 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4889 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
4891 /* update the UNIX password */
4892 if (lp_unix_password_sync()) {
4893 struct passwd *passwd;
4895 if (pdb_get_username(pwd) == NULL) {
4896 DEBUG(1, ("chgpasswd: User without name???\n"));
4900 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4901 if (passwd == NULL) {
4902 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4905 if(!chgpasswd(pdb_get_username(pwd), rhost, passwd,
4906 "", plaintext_buf, True)) {
4909 TALLOC_FREE(passwd);
4913 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4915 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
4920 /*******************************************************************
4922 ********************************************************************/
4924 static NTSTATUS set_user_info_24(TALLOC_CTX *mem_ctx,
4926 struct samr_UserInfo24 *id24,
4932 DEBUG(5, ("set_user_info_24: NULL id24\n"));
4933 return NT_STATUS_INVALID_PARAMETER;
4936 if (!set_user_info_pw(id24->password.data, rhost, pwd)) {
4937 return NT_STATUS_WRONG_PASSWORD;
4940 copy_id24_to_sam_passwd(pwd, id24);
4942 status = pdb_update_sam_account(pwd);
4943 if (!NT_STATUS_IS_OK(status)) {
4947 return NT_STATUS_OK;
4950 /*******************************************************************
4952 ********************************************************************/
4954 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
4956 struct samr_UserInfo25 *id25,
4962 DEBUG(5, ("set_user_info_25: NULL id25\n"));
4963 return NT_STATUS_INVALID_PARAMETER;
4966 if (id25->info.fields_present == 0) {
4967 return NT_STATUS_INVALID_PARAMETER;
4970 if (id25->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4971 return NT_STATUS_ACCESS_DENIED;
4974 if ((id25->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4975 (id25->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4977 if (!set_user_info_pw(id25->password.data, rhost, pwd)) {
4978 return NT_STATUS_WRONG_PASSWORD;
4982 copy_id25_to_sam_passwd(pwd, id25);
4984 /* write the change out */
4985 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4990 * We need to "pdb_update_sam_account" before the unix primary group
4991 * is set, because the idealx scripts would also change the
4992 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
4993 * the delete explicit / add explicit, which would then fail to find
4994 * the previous primaryGroupSid value.
4997 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4998 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4999 if ( !NT_STATUS_IS_OK(status) ) {
5004 return NT_STATUS_OK;
5007 /*******************************************************************
5009 ********************************************************************/
5011 static NTSTATUS set_user_info_26(TALLOC_CTX *mem_ctx,
5013 struct samr_UserInfo26 *id26,
5019 DEBUG(5, ("set_user_info_26: NULL id26\n"));
5020 return NT_STATUS_INVALID_PARAMETER;
5023 if (!set_user_info_pw(id26->password.data, rhost, pwd)) {
5024 return NT_STATUS_WRONG_PASSWORD;
5027 copy_id26_to_sam_passwd(pwd, id26);
5029 status = pdb_update_sam_account(pwd);
5030 if (!NT_STATUS_IS_OK(status)) {
5034 return NT_STATUS_OK;
5037 /*************************************************************
5038 **************************************************************/
5040 static uint32_t samr_set_user_info_map_fields_to_access_mask(uint32_t fields)
5042 uint32_t acc_required = 0;
5044 /* USER_ALL_USERNAME */
5045 if (fields & SAMR_FIELD_ACCOUNT_NAME)
5046 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5047 /* USER_ALL_FULLNAME */
5048 if (fields & SAMR_FIELD_FULL_NAME)
5049 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5050 /* USER_ALL_PRIMARYGROUPID */
5051 if (fields & SAMR_FIELD_PRIMARY_GID)
5052 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5053 /* USER_ALL_HOMEDIRECTORY */
5054 if (fields & SAMR_FIELD_HOME_DIRECTORY)
5055 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5056 /* USER_ALL_HOMEDIRECTORYDRIVE */
5057 if (fields & SAMR_FIELD_HOME_DRIVE)
5058 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5059 /* USER_ALL_SCRIPTPATH */
5060 if (fields & SAMR_FIELD_LOGON_SCRIPT)
5061 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5062 /* USER_ALL_PROFILEPATH */
5063 if (fields & SAMR_FIELD_PROFILE_PATH)
5064 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5065 /* USER_ALL_ADMINCOMMENT */
5066 if (fields & SAMR_FIELD_COMMENT)
5067 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5068 /* USER_ALL_WORKSTATIONS */
5069 if (fields & SAMR_FIELD_WORKSTATIONS)
5070 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5071 /* USER_ALL_LOGONHOURS */
5072 if (fields & SAMR_FIELD_LOGON_HOURS)
5073 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5074 /* USER_ALL_ACCOUNTEXPIRES */
5075 if (fields & SAMR_FIELD_ACCT_EXPIRY)
5076 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5077 /* USER_ALL_USERACCOUNTCONTROL */
5078 if (fields & SAMR_FIELD_ACCT_FLAGS)
5079 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5080 /* USER_ALL_PARAMETERS */
5081 if (fields & SAMR_FIELD_PARAMETERS)
5082 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5083 /* USER_ALL_USERCOMMENT */
5084 if (fields & SAMR_FIELD_COMMENT)
5085 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5086 /* USER_ALL_COUNTRYCODE */
5087 if (fields & SAMR_FIELD_COUNTRY_CODE)
5088 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5089 /* USER_ALL_CODEPAGE */
5090 if (fields & SAMR_FIELD_CODE_PAGE)
5091 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5092 /* USER_ALL_NTPASSWORDPRESENT */
5093 if (fields & SAMR_FIELD_NT_PASSWORD_PRESENT)
5094 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5095 /* USER_ALL_LMPASSWORDPRESENT */
5096 if (fields & SAMR_FIELD_LM_PASSWORD_PRESENT)
5097 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5098 /* USER_ALL_PASSWORDEXPIRED */
5099 if (fields & SAMR_FIELD_EXPIRED_FLAG)
5100 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5102 return acc_required;
5105 /*******************************************************************
5107 ********************************************************************/
5109 NTSTATUS _samr_SetUserInfo(struct pipes_struct *p,
5110 struct samr_SetUserInfo *r)
5112 struct samr_user_info *uinfo;
5114 struct samu *pwd = NULL;
5115 union samr_UserInfo *info = r->in.info;
5116 uint32_t acc_required = 0;
5117 uint32_t fields = 0;
5120 DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
5122 /* This is tricky. A WinXP domain join sets
5123 (SAMR_USER_ACCESS_SET_PASSWORD|SAMR_USER_ACCESS_SET_ATTRIBUTES|SAMR_USER_ACCESS_GET_ATTRIBUTES)
5124 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
5125 standard Win32 API calls just ask for SAMR_USER_ACCESS_SET_PASSWORD in the SamrOpenUser().
5126 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
5127 we'll use the set from the WinXP join as the basis. */
5129 switch (r->in.level) {
5130 case 2: /* UserPreferencesInformation */
5131 /* USER_WRITE_ACCOUNT | USER_WRITE_PREFERENCES */
5132 acc_required = SAMR_USER_ACCESS_SET_ATTRIBUTES | SAMR_USER_ACCESS_SET_LOC_COM;
5134 case 4: /* UserLogonHoursInformation */
5135 case 6: /* UserNameInformation */
5136 case 7: /* UserAccountNameInformation */
5137 case 8: /* UserFullNameInformation */
5138 case 9: /* UserPrimaryGroupInformation */
5139 case 10: /* UserHomeInformation */
5140 case 11: /* UserScriptInformation */
5141 case 12: /* UserProfileInformation */
5142 case 13: /* UserAdminCommentInformation */
5143 case 14: /* UserWorkStationsInformation */
5144 case 16: /* UserControlInformation */
5145 case 17: /* UserExpiresInformation */
5146 case 20: /* UserParametersInformation */
5147 /* USER_WRITE_ACCOUNT */
5148 acc_required = SAMR_USER_ACCESS_SET_ATTRIBUTES;
5150 case 18: /* UserInternal1Information */
5151 /* FIXME: gd, this is a guess */
5152 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
5154 case 21: /* UserAllInformation */
5155 fields = info->info21.fields_present;
5156 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5158 case 23: /* UserInternal4Information */
5159 fields = info->info23.info.fields_present;
5160 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5162 case 25: /* UserInternal4InformationNew */
5163 fields = info->info25.info.fields_present;
5164 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5166 case 24: /* UserInternal5Information */
5167 case 26: /* UserInternal5InformationNew */
5168 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
5171 return NT_STATUS_INVALID_INFO_CLASS;
5174 uinfo = policy_handle_find(p, r->in.user_handle, acc_required, NULL,
5175 struct samr_user_info, &status);
5176 if (!NT_STATUS_IS_OK(status)) {
5180 DEBUG(5, ("_samr_SetUserInfo: sid:%s, level:%d\n",
5181 sid_string_dbg(&uinfo->sid), r->in.level));
5184 DEBUG(5, ("_samr_SetUserInfo: NULL info level\n"));
5185 return NT_STATUS_INVALID_INFO_CLASS;
5188 if (!(pwd = samu_new(NULL))) {
5189 return NT_STATUS_NO_MEMORY;
5193 ret = pdb_getsampwsid(pwd, &uinfo->sid);
5198 return NT_STATUS_NO_SUCH_USER;
5201 /* ================ BEGIN Privilege BLOCK ================ */
5205 /* ok! user info levels (lots: see MSDEV help), off we go... */
5207 switch (r->in.level) {
5210 status = set_user_info_2(p->mem_ctx,
5215 status = set_user_info_4(p->mem_ctx,
5220 status = set_user_info_6(p->mem_ctx,
5225 status = set_user_info_7(p->mem_ctx,
5230 status = set_user_info_8(p->mem_ctx,
5235 status = set_user_info_10(p->mem_ctx,
5236 &info->info10, pwd);
5240 status = set_user_info_11(p->mem_ctx,
5241 &info->info11, pwd);
5245 status = set_user_info_12(p->mem_ctx,
5246 &info->info12, pwd);
5250 status = set_user_info_13(p->mem_ctx,
5251 &info->info13, pwd);
5255 status = set_user_info_14(p->mem_ctx,
5256 &info->info14, pwd);
5260 status = set_user_info_16(p->mem_ctx,
5261 &info->info16, pwd);
5265 status = set_user_info_17(p->mem_ctx,
5266 &info->info17, pwd);
5270 /* Used by AS/U JRA. */
5271 status = set_user_info_18(&info->info18,
5273 &p->session_info->user_session_key,
5278 status = set_user_info_20(p->mem_ctx,
5279 &info->info20, pwd);
5283 status = set_user_info_21(&info->info21,
5285 &p->session_info->user_session_key,
5290 if (!p->session_info->user_session_key.length) {
5291 status = NT_STATUS_NO_USER_SESSION_KEY;
5293 arcfour_crypt_blob(info->info23.password.data, 516,
5294 &p->session_info->user_session_key);
5296 dump_data(100, info->info23.password.data, 516);
5298 status = set_user_info_23(p->mem_ctx,
5305 if (!p->session_info->user_session_key.length) {
5306 status = NT_STATUS_NO_USER_SESSION_KEY;
5308 arcfour_crypt_blob(info->info24.password.data,
5310 &p->session_info->user_session_key);
5312 dump_data(100, info->info24.password.data, 516);
5314 status = set_user_info_24(p->mem_ctx,
5316 &info->info24, pwd);
5320 if (!p->session_info->user_session_key.length) {
5321 status = NT_STATUS_NO_USER_SESSION_KEY;
5323 encode_or_decode_arc4_passwd_buffer(
5324 info->info25.password.data,
5325 &p->session_info->user_session_key);
5327 dump_data(100, info->info25.password.data, 532);
5329 status = set_user_info_25(p->mem_ctx,
5331 &info->info25, pwd);
5335 if (!p->session_info->user_session_key.length) {
5336 status = NT_STATUS_NO_USER_SESSION_KEY;
5338 encode_or_decode_arc4_passwd_buffer(
5339 info->info26.password.data,
5340 &p->session_info->user_session_key);
5342 dump_data(100, info->info26.password.data, 516);
5344 status = set_user_info_26(p->mem_ctx,
5346 &info->info26, pwd);
5350 status = NT_STATUS_INVALID_INFO_CLASS;
5357 /* ================ END Privilege BLOCK ================ */
5359 if (NT_STATUS_IS_OK(status)) {
5360 force_flush_samr_cache(&uinfo->sid);
5366 /*******************************************************************
5368 ********************************************************************/
5370 NTSTATUS _samr_SetUserInfo2(struct pipes_struct *p,
5371 struct samr_SetUserInfo2 *r)
5373 struct samr_SetUserInfo q;
5375 q.in.user_handle = r->in.user_handle;
5376 q.in.level = r->in.level;
5377 q.in.info = r->in.info;
5379 return _samr_SetUserInfo(p, &q);
5382 /*********************************************************************
5383 _samr_GetAliasMembership
5384 *********************************************************************/
5386 NTSTATUS _samr_GetAliasMembership(struct pipes_struct *p,
5387 struct samr_GetAliasMembership *r)
5389 size_t num_alias_rids;
5391 struct samr_domain_info *dinfo;
5396 struct dom_sid *members;
5398 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__));
5400 dinfo = policy_handle_find(p, r->in.domain_handle,
5401 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS
5402 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
5403 struct samr_domain_info, &status);
5404 if (!NT_STATUS_IS_OK(status)) {
5408 if (!sid_check_is_domain(&dinfo->sid) &&
5409 !sid_check_is_builtin(&dinfo->sid))
5410 return NT_STATUS_OBJECT_TYPE_MISMATCH;
5412 if (r->in.sids->num_sids) {
5413 members = TALLOC_ARRAY(p->mem_ctx, struct dom_sid, r->in.sids->num_sids);
5415 if (members == NULL)
5416 return NT_STATUS_NO_MEMORY;
5421 for (i=0; i<r->in.sids->num_sids; i++)
5422 sid_copy(&members[i], r->in.sids->sids[i].sid);
5428 status = pdb_enum_alias_memberships(p->mem_ctx, &dinfo->sid, members,
5429 r->in.sids->num_sids,
5430 &alias_rids, &num_alias_rids);
5433 if (!NT_STATUS_IS_OK(status)) {
5437 r->out.rids->count = num_alias_rids;
5438 r->out.rids->ids = alias_rids;
5440 if (r->out.rids->ids == NULL) {
5441 /* Windows domain clients don't accept a NULL ptr here */
5442 r->out.rids->ids = talloc_zero(p->mem_ctx, uint32_t);
5444 if (r->out.rids->ids == NULL) {
5445 return NT_STATUS_NO_MEMORY;
5448 return NT_STATUS_OK;
5451 /*********************************************************************
5452 _samr_GetMembersInAlias
5453 *********************************************************************/
5455 NTSTATUS _samr_GetMembersInAlias(struct pipes_struct *p,
5456 struct samr_GetMembersInAlias *r)
5458 struct samr_alias_info *ainfo;
5461 size_t num_sids = 0;
5462 struct lsa_SidPtr *sids = NULL;
5463 struct dom_sid *pdb_sids = NULL;
5465 ainfo = policy_handle_find(p, r->in.alias_handle,
5466 SAMR_ALIAS_ACCESS_GET_MEMBERS, NULL,
5467 struct samr_alias_info, &status);
5468 if (!NT_STATUS_IS_OK(status)) {
5472 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5475 status = pdb_enum_aliasmem(&ainfo->sid, talloc_tos(), &pdb_sids,
5479 if (!NT_STATUS_IS_OK(status)) {
5484 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr, num_sids);
5486 TALLOC_FREE(pdb_sids);
5487 return NT_STATUS_NO_MEMORY;
5491 for (i = 0; i < num_sids; i++) {
5492 sids[i].sid = dom_sid_dup(p->mem_ctx, &pdb_sids[i]);
5494 TALLOC_FREE(pdb_sids);
5495 return NT_STATUS_NO_MEMORY;
5499 r->out.sids->num_sids = num_sids;
5500 r->out.sids->sids = sids;
5502 TALLOC_FREE(pdb_sids);
5504 return NT_STATUS_OK;
5507 /*********************************************************************
5508 _samr_QueryGroupMember
5509 *********************************************************************/
5511 NTSTATUS _samr_QueryGroupMember(struct pipes_struct *p,
5512 struct samr_QueryGroupMember *r)
5514 struct samr_group_info *ginfo;
5515 size_t i, num_members;
5521 struct samr_RidAttrArray *rids = NULL;
5523 ginfo = policy_handle_find(p, r->in.group_handle,
5524 SAMR_GROUP_ACCESS_GET_MEMBERS, NULL,
5525 struct samr_group_info, &status);
5526 if (!NT_STATUS_IS_OK(status)) {
5530 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidAttrArray);
5532 return NT_STATUS_NO_MEMORY;
5535 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5537 if (!sid_check_is_in_our_domain(&ginfo->sid)) {
5538 DEBUG(3, ("sid %s is not in our domain\n",
5539 sid_string_dbg(&ginfo->sid)));
5540 return NT_STATUS_NO_SUCH_GROUP;
5543 DEBUG(10, ("lookup on Domain SID\n"));
5546 status = pdb_enum_group_members(p->mem_ctx, &ginfo->sid,
5547 &rid, &num_members);
5550 if (!NT_STATUS_IS_OK(status))
5554 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
5556 return NT_STATUS_NO_MEMORY;
5562 for (i=0; i<num_members; i++) {
5563 attr[i] = SE_GROUP_MANDATORY |
5564 SE_GROUP_ENABLED_BY_DEFAULT |
5568 rids->count = num_members;
5569 rids->attributes = attr;
5572 *r->out.rids = rids;
5574 return NT_STATUS_OK;
5577 /*********************************************************************
5578 _samr_AddAliasMember
5579 *********************************************************************/
5581 NTSTATUS _samr_AddAliasMember(struct pipes_struct *p,
5582 struct samr_AddAliasMember *r)
5584 struct samr_alias_info *ainfo;
5587 ainfo = policy_handle_find(p, r->in.alias_handle,
5588 SAMR_ALIAS_ACCESS_ADD_MEMBER, NULL,
5589 struct samr_alias_info, &status);
5590 if (!NT_STATUS_IS_OK(status)) {
5594 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5596 /******** BEGIN SeAddUsers BLOCK *********/
5599 status = pdb_add_aliasmem(&ainfo->sid, r->in.sid);
5602 /******** END SeAddUsers BLOCK *********/
5604 if (NT_STATUS_IS_OK(status)) {
5605 force_flush_samr_cache(&ainfo->sid);
5611 /*********************************************************************
5612 _samr_DeleteAliasMember
5613 *********************************************************************/
5615 NTSTATUS _samr_DeleteAliasMember(struct pipes_struct *p,
5616 struct samr_DeleteAliasMember *r)
5618 struct samr_alias_info *ainfo;
5621 ainfo = policy_handle_find(p, r->in.alias_handle,
5622 SAMR_ALIAS_ACCESS_REMOVE_MEMBER, NULL,
5623 struct samr_alias_info, &status);
5624 if (!NT_STATUS_IS_OK(status)) {
5628 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
5629 sid_string_dbg(&ainfo->sid)));
5631 /******** BEGIN SeAddUsers BLOCK *********/
5634 status = pdb_del_aliasmem(&ainfo->sid, r->in.sid);
5637 /******** END SeAddUsers BLOCK *********/
5639 if (NT_STATUS_IS_OK(status)) {
5640 force_flush_samr_cache(&ainfo->sid);
5646 /*********************************************************************
5647 _samr_AddGroupMember
5648 *********************************************************************/
5650 NTSTATUS _samr_AddGroupMember(struct pipes_struct *p,
5651 struct samr_AddGroupMember *r)
5653 struct samr_group_info *ginfo;
5657 ginfo = policy_handle_find(p, r->in.group_handle,
5658 SAMR_GROUP_ACCESS_ADD_MEMBER, NULL,
5659 struct samr_group_info, &status);
5660 if (!NT_STATUS_IS_OK(status)) {
5664 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5666 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5668 return NT_STATUS_INVALID_HANDLE;
5671 /******** BEGIN SeAddUsers BLOCK *********/
5674 status = pdb_add_groupmem(p->mem_ctx, group_rid, r->in.rid);
5677 /******** END SeAddUsers BLOCK *********/
5679 force_flush_samr_cache(&ginfo->sid);
5684 /*********************************************************************
5685 _samr_DeleteGroupMember
5686 *********************************************************************/
5688 NTSTATUS _samr_DeleteGroupMember(struct pipes_struct *p,
5689 struct samr_DeleteGroupMember *r)
5692 struct samr_group_info *ginfo;
5697 * delete the group member named r->in.rid
5698 * who is a member of the sid associated with the handle
5699 * the rid is a user's rid as the group is a domain group.
5702 ginfo = policy_handle_find(p, r->in.group_handle,
5703 SAMR_GROUP_ACCESS_REMOVE_MEMBER, NULL,
5704 struct samr_group_info, &status);
5705 if (!NT_STATUS_IS_OK(status)) {
5709 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5711 return NT_STATUS_INVALID_HANDLE;
5714 /******** BEGIN SeAddUsers BLOCK *********/
5717 status = pdb_del_groupmem(p->mem_ctx, group_rid, r->in.rid);
5720 /******** END SeAddUsers BLOCK *********/
5722 force_flush_samr_cache(&ginfo->sid);
5727 /*********************************************************************
5729 *********************************************************************/
5731 NTSTATUS _samr_DeleteUser(struct pipes_struct *p,
5732 struct samr_DeleteUser *r)
5734 struct samr_user_info *uinfo;
5736 struct samu *sam_pass=NULL;
5739 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__));
5741 uinfo = policy_handle_find(p, r->in.user_handle,
5742 SEC_STD_DELETE, NULL,
5743 struct samr_user_info, &status);
5744 if (!NT_STATUS_IS_OK(status)) {
5748 if (!sid_check_is_in_our_domain(&uinfo->sid))
5749 return NT_STATUS_CANNOT_DELETE;
5751 /* check if the user exists before trying to delete */
5752 if ( !(sam_pass = samu_new( NULL )) ) {
5753 return NT_STATUS_NO_MEMORY;
5757 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
5761 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
5762 sid_string_dbg(&uinfo->sid)));
5763 TALLOC_FREE(sam_pass);
5764 return NT_STATUS_NO_SUCH_USER;
5767 /******** BEGIN SeAddUsers BLOCK *********/
5770 status = pdb_delete_user(p->mem_ctx, sam_pass);
5773 /******** END SeAddUsers BLOCK *********/
5775 if ( !NT_STATUS_IS_OK(status) ) {
5776 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
5777 "user %s: %s.\n", pdb_get_username(sam_pass),
5778 nt_errstr(status)));
5779 TALLOC_FREE(sam_pass);
5784 TALLOC_FREE(sam_pass);
5786 force_flush_samr_cache(&uinfo->sid);
5788 if (!close_policy_hnd(p, r->in.user_handle))
5789 return NT_STATUS_OBJECT_NAME_INVALID;
5791 ZERO_STRUCTP(r->out.user_handle);
5793 return NT_STATUS_OK;
5796 /*********************************************************************
5797 _samr_DeleteDomainGroup
5798 *********************************************************************/
5800 NTSTATUS _samr_DeleteDomainGroup(struct pipes_struct *p,
5801 struct samr_DeleteDomainGroup *r)
5803 struct samr_group_info *ginfo;
5807 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__));
5809 ginfo = policy_handle_find(p, r->in.group_handle,
5810 SEC_STD_DELETE, NULL,
5811 struct samr_group_info, &status);
5812 if (!NT_STATUS_IS_OK(status)) {
5816 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5818 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5820 return NT_STATUS_NO_SUCH_GROUP;
5823 /******** BEGIN SeAddUsers BLOCK *********/
5826 status = pdb_delete_dom_group(p->mem_ctx, group_rid);
5829 /******** END SeAddUsers BLOCK *********/
5831 if ( !NT_STATUS_IS_OK(status) ) {
5832 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
5833 "entry for group %s: %s\n",
5834 sid_string_dbg(&ginfo->sid),
5835 nt_errstr(status)));
5839 force_flush_samr_cache(&ginfo->sid);
5841 if (!close_policy_hnd(p, r->in.group_handle))
5842 return NT_STATUS_OBJECT_NAME_INVALID;
5844 return NT_STATUS_OK;
5847 /*********************************************************************
5848 _samr_DeleteDomAlias
5849 *********************************************************************/
5851 NTSTATUS _samr_DeleteDomAlias(struct pipes_struct *p,
5852 struct samr_DeleteDomAlias *r)
5854 struct samr_alias_info *ainfo;
5857 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__));
5859 ainfo = policy_handle_find(p, r->in.alias_handle,
5860 SEC_STD_DELETE, NULL,
5861 struct samr_alias_info, &status);
5862 if (!NT_STATUS_IS_OK(status)) {
5866 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5868 /* Don't let Windows delete builtin groups */
5870 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
5871 return NT_STATUS_SPECIAL_ACCOUNT;
5874 if (!sid_check_is_in_our_domain(&ainfo->sid))
5875 return NT_STATUS_NO_SUCH_ALIAS;
5877 DEBUG(10, ("lookup on Local SID\n"));
5879 /******** BEGIN SeAddUsers BLOCK *********/
5882 /* Have passdb delete the alias */
5883 status = pdb_delete_alias(&ainfo->sid);
5886 /******** END SeAddUsers BLOCK *********/
5888 if ( !NT_STATUS_IS_OK(status))
5891 force_flush_samr_cache(&ainfo->sid);
5893 if (!close_policy_hnd(p, r->in.alias_handle))
5894 return NT_STATUS_OBJECT_NAME_INVALID;
5896 return NT_STATUS_OK;
5899 /*********************************************************************
5900 _samr_CreateDomainGroup
5901 *********************************************************************/
5903 NTSTATUS _samr_CreateDomainGroup(struct pipes_struct *p,
5904 struct samr_CreateDomainGroup *r)
5909 struct samr_domain_info *dinfo;
5910 struct samr_group_info *ginfo;
5912 dinfo = policy_handle_find(p, r->in.domain_handle,
5913 SAMR_DOMAIN_ACCESS_CREATE_GROUP, NULL,
5914 struct samr_domain_info, &status);
5915 if (!NT_STATUS_IS_OK(status)) {
5919 if (!sid_check_is_domain(&dinfo->sid)) {
5920 return NT_STATUS_ACCESS_DENIED;
5923 name = r->in.name->string;
5925 return NT_STATUS_NO_MEMORY;
5928 status = can_create(p->mem_ctx, name);
5929 if (!NT_STATUS_IS_OK(status)) {
5933 /******** BEGIN SeAddUsers BLOCK *********/
5936 /* check that we successfully create the UNIX group */
5937 status = pdb_create_dom_group(p->mem_ctx, name, r->out.rid);
5940 /******** END SeAddUsers BLOCK *********/
5942 /* check if we should bail out here */
5944 if ( !NT_STATUS_IS_OK(status) )
5947 ginfo = policy_handle_create(p, r->out.group_handle,
5948 GENERIC_RIGHTS_GROUP_ALL_ACCESS,
5949 struct samr_group_info, &status);
5950 if (!NT_STATUS_IS_OK(status)) {
5953 sid_compose(&ginfo->sid, &dinfo->sid, *r->out.rid);
5955 force_flush_samr_cache(&dinfo->sid);
5957 return NT_STATUS_OK;
5960 /*********************************************************************
5961 _samr_CreateDomAlias
5962 *********************************************************************/
5964 NTSTATUS _samr_CreateDomAlias(struct pipes_struct *p,
5965 struct samr_CreateDomAlias *r)
5967 struct dom_sid info_sid;
5968 const char *name = NULL;
5969 struct samr_domain_info *dinfo;
5970 struct samr_alias_info *ainfo;
5974 dinfo = policy_handle_find(p, r->in.domain_handle,
5975 SAMR_DOMAIN_ACCESS_CREATE_ALIAS, NULL,
5976 struct samr_domain_info, &result);
5977 if (!NT_STATUS_IS_OK(result)) {
5981 if (!sid_check_is_domain(&dinfo->sid)) {
5982 return NT_STATUS_ACCESS_DENIED;
5985 name = r->in.alias_name->string;
5987 result = can_create(p->mem_ctx, name);
5988 if (!NT_STATUS_IS_OK(result)) {
5992 /******** BEGIN SeAddUsers BLOCK *********/
5995 /* Have passdb create the alias */
5996 result = pdb_create_alias(name, r->out.rid);
5999 /******** END SeAddUsers BLOCK *********/
6001 if (!NT_STATUS_IS_OK(result)) {
6002 DEBUG(10, ("pdb_create_alias failed: %s\n",
6003 nt_errstr(result)));
6007 sid_compose(&info_sid, &dinfo->sid, *r->out.rid);
6009 if (!sid_to_gid(&info_sid, &gid)) {
6010 DEBUG(10, ("Could not find alias just created\n"));
6011 return NT_STATUS_ACCESS_DENIED;
6014 /* check if the group has been successfully created */
6015 if ( getgrgid(gid) == NULL ) {
6016 DEBUG(10, ("getgrgid(%u) of just created alias failed\n",
6017 (unsigned int)gid));
6018 return NT_STATUS_ACCESS_DENIED;
6021 ainfo = policy_handle_create(p, r->out.alias_handle,
6022 GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
6023 struct samr_alias_info, &result);
6024 if (!NT_STATUS_IS_OK(result)) {
6027 ainfo->sid = info_sid;
6029 force_flush_samr_cache(&info_sid);
6031 return NT_STATUS_OK;
6034 /*********************************************************************
6035 _samr_QueryGroupInfo
6036 *********************************************************************/
6038 NTSTATUS _samr_QueryGroupInfo(struct pipes_struct *p,
6039 struct samr_QueryGroupInfo *r)
6041 struct samr_group_info *ginfo;
6044 union samr_GroupInfo *info = NULL;
6046 uint32_t attributes = SE_GROUP_MANDATORY |
6047 SE_GROUP_ENABLED_BY_DEFAULT |
6049 const char *group_name = NULL;
6050 const char *group_description = NULL;
6052 ginfo = policy_handle_find(p, r->in.group_handle,
6053 SAMR_GROUP_ACCESS_LOOKUP_INFO, NULL,
6054 struct samr_group_info, &status);
6055 if (!NT_STATUS_IS_OK(status)) {
6060 ret = get_domain_group_from_sid(ginfo->sid, &map);
6063 return NT_STATUS_INVALID_HANDLE;
6065 /* FIXME: map contains fstrings */
6066 group_name = talloc_strdup(r, map.nt_name);
6067 group_description = talloc_strdup(r, map.comment);
6069 info = TALLOC_ZERO_P(p->mem_ctx, union samr_GroupInfo);
6071 return NT_STATUS_NO_MEMORY;
6074 switch (r->in.level) {
6080 status = pdb_enum_group_members(
6081 p->mem_ctx, &ginfo->sid, &members,
6085 if (!NT_STATUS_IS_OK(status)) {
6089 info->all.name.string = group_name;
6090 info->all.attributes = attributes;
6091 info->all.num_members = num_members;
6092 info->all.description.string = group_description;
6096 info->name.string = group_name;
6099 info->attributes.attributes = attributes;
6102 info->description.string = group_description;
6112 status = pdb_enum_group_members(
6113 p->mem_ctx, &ginfo->sid, &members,
6117 if (!NT_STATUS_IS_OK(status)) {
6121 info->all2.name.string = group_name;
6122 info->all2.attributes = attributes;
6123 info->all2.num_members = 0; /* num_members - in w2k3 this is always 0 */
6124 info->all2.description.string = group_description;
6129 return NT_STATUS_INVALID_INFO_CLASS;
6132 *r->out.info = info;
6134 return NT_STATUS_OK;
6137 /*********************************************************************
6139 *********************************************************************/
6141 NTSTATUS _samr_SetGroupInfo(struct pipes_struct *p,
6142 struct samr_SetGroupInfo *r)
6144 struct samr_group_info *ginfo;
6149 ginfo = policy_handle_find(p, r->in.group_handle,
6150 SAMR_GROUP_ACCESS_SET_INFO, NULL,
6151 struct samr_group_info, &status);
6152 if (!NT_STATUS_IS_OK(status)) {
6157 ret = get_domain_group_from_sid(ginfo->sid, &map);
6160 return NT_STATUS_NO_SUCH_GROUP;
6162 switch (r->in.level) {
6164 fstrcpy(map.nt_name, r->in.info->name.string);
6169 fstrcpy(map.comment, r->in.info->description.string);
6172 return NT_STATUS_INVALID_INFO_CLASS;
6175 /******** BEGIN SeAddUsers BLOCK *********/
6178 status = pdb_update_group_mapping_entry(&map);
6181 /******** End SeAddUsers BLOCK *********/
6183 if (NT_STATUS_IS_OK(status)) {
6184 force_flush_samr_cache(&ginfo->sid);
6190 /*********************************************************************
6192 *********************************************************************/
6194 NTSTATUS _samr_SetAliasInfo(struct pipes_struct *p,
6195 struct samr_SetAliasInfo *r)
6197 struct samr_alias_info *ainfo;
6198 struct acct_info info;
6201 ainfo = policy_handle_find(p, r->in.alias_handle,
6202 SAMR_ALIAS_ACCESS_SET_INFO, NULL,
6203 struct samr_alias_info, &status);
6204 if (!NT_STATUS_IS_OK(status)) {
6208 /* get the current group information */
6211 status = pdb_get_aliasinfo( &ainfo->sid, &info );
6214 if ( !NT_STATUS_IS_OK(status))
6217 switch (r->in.level) {
6222 /* We currently do not support renaming groups in the
6223 the BUILTIN domain. Refer to util_builtin.c to understand
6224 why. The eventually needs to be fixed to be like Windows
6225 where you can rename builtin groups, just not delete them */
6227 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
6228 return NT_STATUS_SPECIAL_ACCOUNT;
6231 /* There has to be a valid name (and it has to be different) */
6233 if ( !r->in.info->name.string )
6234 return NT_STATUS_INVALID_PARAMETER;
6236 /* If the name is the same just reply "ok". Yes this
6237 doesn't allow you to change the case of a group name. */
6239 if ( strequal( r->in.info->name.string, info.acct_name ) )
6240 return NT_STATUS_OK;
6242 fstrcpy( info.acct_name, r->in.info->name.string);
6244 /* make sure the name doesn't already exist as a user
6247 fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
6248 status = can_create( p->mem_ctx, group_name );
6249 if ( !NT_STATUS_IS_OK( status ) )
6253 case ALIASINFODESCRIPTION:
6254 if (r->in.info->description.string) {
6255 fstrcpy(info.acct_desc,
6256 r->in.info->description.string);
6258 fstrcpy( info.acct_desc, "" );
6262 return NT_STATUS_INVALID_INFO_CLASS;
6265 /******** BEGIN SeAddUsers BLOCK *********/
6268 status = pdb_set_aliasinfo( &ainfo->sid, &info );
6271 /******** End SeAddUsers BLOCK *********/
6273 if (NT_STATUS_IS_OK(status))
6274 force_flush_samr_cache(&ainfo->sid);
6279 /****************************************************************
6281 ****************************************************************/
6283 NTSTATUS _samr_GetDomPwInfo(struct pipes_struct *p,
6284 struct samr_GetDomPwInfo *r)
6286 uint32_t min_password_length = 0;
6287 uint32_t password_properties = 0;
6289 /* Perform access check. Since this rpc does not require a
6290 policy handle it will not be caught by the access checks on
6291 SAMR_CONNECT or SAMR_CONNECT_ANON. */
6293 if (!pipe_access_check(p)) {
6294 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
6295 return NT_STATUS_ACCESS_DENIED;
6299 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
6300 &min_password_length);
6301 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
6302 &password_properties);
6305 if (lp_check_password_script() && *lp_check_password_script()) {
6306 password_properties |= DOMAIN_PASSWORD_COMPLEX;
6309 r->out.info->min_password_length = min_password_length;
6310 r->out.info->password_properties = password_properties;
6312 return NT_STATUS_OK;
6315 /*********************************************************************
6317 *********************************************************************/
6319 NTSTATUS _samr_OpenGroup(struct pipes_struct *p,
6320 struct samr_OpenGroup *r)
6323 struct dom_sid info_sid;
6325 struct samr_domain_info *dinfo;
6326 struct samr_group_info *ginfo;
6327 struct security_descriptor *psd = NULL;
6329 uint32 des_access = r->in.access_mask;
6334 dinfo = policy_handle_find(p, r->in.domain_handle,
6335 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
6336 struct samr_domain_info, &status);
6337 if (!NT_STATUS_IS_OK(status)) {
6341 /*check if access can be granted as requested by client. */
6342 map_max_allowed_access(p->session_info->security_token,
6343 &p->session_info->utok,
6346 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
6347 se_map_generic(&des_access,&grp_generic_mapping);
6349 status = access_check_object(psd, p->session_info->security_token,
6350 SEC_PRIV_ADD_USERS, SEC_PRIV_INVALID, GENERIC_RIGHTS_GROUP_ALL_ACCESS,
6351 des_access, &acc_granted, "_samr_OpenGroup");
6353 if ( !NT_STATUS_IS_OK(status) )
6356 /* this should not be hard-coded like this */
6358 if (!sid_check_is_domain(&dinfo->sid)) {
6359 return NT_STATUS_ACCESS_DENIED;
6362 sid_compose(&info_sid, &dinfo->sid, r->in.rid);
6364 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n",
6365 sid_string_dbg(&info_sid)));
6367 /* check if that group really exists */
6369 ret = get_domain_group_from_sid(info_sid, &map);
6372 return NT_STATUS_NO_SUCH_GROUP;
6374 ginfo = policy_handle_create(p, r->out.group_handle,
6376 struct samr_group_info, &status);
6377 if (!NT_STATUS_IS_OK(status)) {
6380 ginfo->sid = info_sid;
6382 return NT_STATUS_OK;
6385 /*********************************************************************
6386 _samr_RemoveMemberFromForeignDomain
6387 *********************************************************************/
6389 NTSTATUS _samr_RemoveMemberFromForeignDomain(struct pipes_struct *p,
6390 struct samr_RemoveMemberFromForeignDomain *r)
6392 struct samr_domain_info *dinfo;
6395 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
6396 sid_string_dbg(r->in.sid)));
6398 /* Find the policy handle. Open a policy on it. */
6400 dinfo = policy_handle_find(p, r->in.domain_handle,
6401 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
6402 struct samr_domain_info, &result);
6403 if (!NT_STATUS_IS_OK(result)) {
6407 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
6408 sid_string_dbg(&dinfo->sid)));
6410 /* we can only delete a user from a group since we don't have
6411 nested groups anyways. So in the latter case, just say OK */
6413 /* TODO: The above comment nowadays is bogus. Since we have nested
6414 * groups now, and aliases members are never reported out of the unix
6415 * group membership, the "just say OK" makes this call a no-op. For
6416 * us. This needs fixing however. */
6418 /* I've only ever seen this in the wild when deleting a user from
6419 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
6420 * is the user about to be deleted. I very much suspect this is the
6421 * only application of this call. To verify this, let people report
6424 if (!sid_check_is_builtin(&dinfo->sid)) {
6425 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
6426 "global_sam_sid() = %s\n",
6427 sid_string_dbg(&dinfo->sid),
6428 sid_string_dbg(get_global_sam_sid())));
6429 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
6430 return NT_STATUS_OK;
6433 force_flush_samr_cache(&dinfo->sid);
6435 result = NT_STATUS_OK;
6440 /*******************************************************************
6441 _samr_QueryDomainInfo2
6442 ********************************************************************/
6444 NTSTATUS _samr_QueryDomainInfo2(struct pipes_struct *p,
6445 struct samr_QueryDomainInfo2 *r)
6447 struct samr_QueryDomainInfo q;
6449 q.in.domain_handle = r->in.domain_handle;
6450 q.in.level = r->in.level;
6452 q.out.info = r->out.info;
6454 return _samr_QueryDomainInfo(p, &q);
6457 /*******************************************************************
6458 ********************************************************************/
6460 static NTSTATUS set_dom_info_1(TALLOC_CTX *mem_ctx,
6461 struct samr_DomInfo1 *r)
6463 time_t u_expire, u_min_age;
6465 u_expire = nt_time_to_unix_abs((NTTIME *)&r->max_password_age);
6466 u_min_age = nt_time_to_unix_abs((NTTIME *)&r->min_password_age);
6468 pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
6469 (uint32_t)r->min_password_length);
6470 pdb_set_account_policy(PDB_POLICY_PASSWORD_HISTORY,
6471 (uint32_t)r->password_history_length);
6472 pdb_set_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
6473 (uint32_t)r->password_properties);
6474 pdb_set_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, (int)u_expire);
6475 pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, (int)u_min_age);
6477 return NT_STATUS_OK;
6480 /*******************************************************************
6481 ********************************************************************/
6483 static NTSTATUS set_dom_info_3(TALLOC_CTX *mem_ctx,
6484 struct samr_DomInfo3 *r)
6488 u_logout = nt_time_to_unix_abs((NTTIME *)&r->force_logoff_time);
6490 pdb_set_account_policy(PDB_POLICY_TIME_TO_LOGOUT, (int)u_logout);
6492 return NT_STATUS_OK;
6495 /*******************************************************************
6496 ********************************************************************/
6498 static NTSTATUS set_dom_info_12(TALLOC_CTX *mem_ctx,
6499 struct samr_DomInfo12 *r)
6501 time_t u_lock_duration, u_reset_time;
6503 u_lock_duration = nt_time_to_unix_abs((NTTIME *)&r->lockout_duration);
6504 if (u_lock_duration != -1) {
6505 u_lock_duration /= 60;
6508 u_reset_time = nt_time_to_unix_abs((NTTIME *)&r->lockout_window)/60;
6510 pdb_set_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
6511 pdb_set_account_policy(PDB_POLICY_RESET_COUNT_TIME, (int)u_reset_time);
6512 pdb_set_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT,
6513 (uint32_t)r->lockout_threshold);
6515 return NT_STATUS_OK;
6518 /*******************************************************************
6520 ********************************************************************/
6522 NTSTATUS _samr_SetDomainInfo(struct pipes_struct *p,
6523 struct samr_SetDomainInfo *r)
6525 struct samr_domain_info *dinfo;
6527 uint32_t acc_required = 0;
6529 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
6531 switch (r->in.level) {
6532 case 1: /* DomainPasswordInformation */
6533 case 12: /* DomainLockoutInformation */
6534 /* DOMAIN_WRITE_PASSWORD_PARAMETERS */
6535 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_1;
6537 case 3: /* DomainLogoffInformation */
6538 case 4: /* DomainOemInformation */
6539 /* DOMAIN_WRITE_OTHER_PARAMETERS */
6540 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_2;
6542 case 6: /* DomainReplicationInformation */
6543 case 9: /* DomainStateInformation */
6544 case 7: /* DomainServerRoleInformation */
6545 /* DOMAIN_ADMINISTER_SERVER */
6546 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_3;
6549 return NT_STATUS_INVALID_INFO_CLASS;
6552 dinfo = policy_handle_find(p, r->in.domain_handle,
6554 struct samr_domain_info, &status);
6555 if (!NT_STATUS_IS_OK(status)) {
6559 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r->in.level));
6561 switch (r->in.level) {
6563 status = set_dom_info_1(p->mem_ctx, &r->in.info->info1);
6566 status = set_dom_info_3(p->mem_ctx, &r->in.info->info3);
6577 status = set_dom_info_12(p->mem_ctx, &r->in.info->info12);
6580 return NT_STATUS_INVALID_INFO_CLASS;
6583 if (!NT_STATUS_IS_OK(status)) {
6587 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
6589 return NT_STATUS_OK;
6592 /****************************************************************
6593 _samr_GetDisplayEnumerationIndex
6594 ****************************************************************/
6596 NTSTATUS _samr_GetDisplayEnumerationIndex(struct pipes_struct *p,
6597 struct samr_GetDisplayEnumerationIndex *r)
6599 struct samr_domain_info *dinfo;
6600 uint32_t max_entries = (uint32_t) -1;
6601 uint32_t enum_context = 0;
6603 uint32_t num_account = 0;
6604 struct samr_displayentry *entries = NULL;
6607 DEBUG(5,("_samr_GetDisplayEnumerationIndex: %d\n", __LINE__));
6609 dinfo = policy_handle_find(p, r->in.domain_handle,
6610 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
6611 struct samr_domain_info, &status);
6612 if (!NT_STATUS_IS_OK(status)) {
6616 if ((r->in.level < 1) || (r->in.level > 3)) {
6617 DEBUG(0,("_samr_GetDisplayEnumerationIndex: "
6618 "Unknown info level (%u)\n",
6620 return NT_STATUS_INVALID_INFO_CLASS;
6625 /* The following done as ROOT. Don't return without unbecome_root(). */
6627 switch (r->in.level) {
6629 if (dinfo->disp_info->users == NULL) {
6630 dinfo->disp_info->users = pdb_search_users(
6631 dinfo->disp_info, ACB_NORMAL);
6632 if (dinfo->disp_info->users == NULL) {
6634 return NT_STATUS_ACCESS_DENIED;
6636 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6637 "starting user enumeration at index %u\n",
6638 (unsigned int)enum_context));
6640 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6641 "using cached user enumeration at index %u\n",
6642 (unsigned int)enum_context));
6644 num_account = pdb_search_entries(dinfo->disp_info->users,
6645 enum_context, max_entries,
6649 if (dinfo->disp_info->machines == NULL) {
6650 dinfo->disp_info->machines = pdb_search_users(
6651 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
6652 if (dinfo->disp_info->machines == NULL) {
6654 return NT_STATUS_ACCESS_DENIED;
6656 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6657 "starting machine enumeration at index %u\n",
6658 (unsigned int)enum_context));
6660 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6661 "using cached machine enumeration at index %u\n",
6662 (unsigned int)enum_context));
6664 num_account = pdb_search_entries(dinfo->disp_info->machines,
6665 enum_context, max_entries,
6669 if (dinfo->disp_info->groups == NULL) {
6670 dinfo->disp_info->groups = pdb_search_groups(
6672 if (dinfo->disp_info->groups == NULL) {
6674 return NT_STATUS_ACCESS_DENIED;
6676 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6677 "starting group enumeration at index %u\n",
6678 (unsigned int)enum_context));
6680 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6681 "using cached group enumeration at index %u\n",
6682 (unsigned int)enum_context));
6684 num_account = pdb_search_entries(dinfo->disp_info->groups,
6685 enum_context, max_entries,
6690 smb_panic("info class changed");
6696 /* Ensure we cache this enumeration. */
6697 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
6699 DEBUG(10,("_samr_GetDisplayEnumerationIndex: looking for :%s\n",
6700 r->in.name->string));
6702 for (i=0; i<num_account; i++) {
6703 if (strequal(entries[i].account_name, r->in.name->string)) {
6704 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6705 "found %s at idx %d\n",
6706 r->in.name->string, i));
6708 return NT_STATUS_OK;
6712 /* assuming account_name lives at the very end */
6713 *r->out.idx = num_account;
6715 return NT_STATUS_NO_MORE_ENTRIES;
6718 /****************************************************************
6719 _samr_GetDisplayEnumerationIndex2
6720 ****************************************************************/
6722 NTSTATUS _samr_GetDisplayEnumerationIndex2(struct pipes_struct *p,
6723 struct samr_GetDisplayEnumerationIndex2 *r)
6725 struct samr_GetDisplayEnumerationIndex q;
6727 q.in.domain_handle = r->in.domain_handle;
6728 q.in.level = r->in.level;
6729 q.in.name = r->in.name;
6731 q.out.idx = r->out.idx;
6733 return _samr_GetDisplayEnumerationIndex(p, &q);
6736 /****************************************************************
6738 ****************************************************************/
6740 NTSTATUS _samr_RidToSid(struct pipes_struct *p,
6741 struct samr_RidToSid *r)
6743 struct samr_domain_info *dinfo;
6747 dinfo = policy_handle_find(p, r->in.domain_handle,
6749 struct samr_domain_info, &status);
6750 if (!NT_STATUS_IS_OK(status)) {
6754 if (!sid_compose(&sid, &dinfo->sid, r->in.rid)) {
6755 return NT_STATUS_NO_MEMORY;
6758 *r->out.sid = dom_sid_dup(p->mem_ctx, &sid);
6760 return NT_STATUS_NO_MEMORY;
6763 return NT_STATUS_OK;
6766 /****************************************************************
6767 ****************************************************************/
6769 static enum samr_ValidationStatus samr_ValidatePassword_Change(TALLOC_CTX *mem_ctx,
6770 const struct samr_PwInfo *dom_pw_info,
6771 const struct samr_ValidatePasswordReq2 *req,
6772 struct samr_ValidatePasswordRepCtr *rep)
6776 if (req->password.string == NULL) {
6777 return SAMR_VALIDATION_STATUS_SUCCESS;
6779 if (strlen(req->password.string) < dom_pw_info->min_password_length) {
6780 ZERO_STRUCT(rep->info);
6781 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
6783 if (dom_pw_info->password_properties & DOMAIN_PASSWORD_COMPLEX) {
6784 status = check_password_complexity(req->account.string,
6785 req->password.string,
6787 if (!NT_STATUS_IS_OK(status)) {
6788 ZERO_STRUCT(rep->info);
6789 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
6793 return SAMR_VALIDATION_STATUS_SUCCESS;
6796 /****************************************************************
6797 ****************************************************************/
6799 static enum samr_ValidationStatus samr_ValidatePassword_Reset(TALLOC_CTX *mem_ctx,
6800 const struct samr_PwInfo *dom_pw_info,
6801 const struct samr_ValidatePasswordReq3 *req,
6802 struct samr_ValidatePasswordRepCtr *rep)
6806 if (req->password.string == NULL) {
6807 return SAMR_VALIDATION_STATUS_SUCCESS;
6809 if (strlen(req->password.string) < dom_pw_info->min_password_length) {
6810 ZERO_STRUCT(rep->info);
6811 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
6813 if (dom_pw_info->password_properties & DOMAIN_PASSWORD_COMPLEX) {
6814 status = check_password_complexity(req->account.string,
6815 req->password.string,
6817 if (!NT_STATUS_IS_OK(status)) {
6818 ZERO_STRUCT(rep->info);
6819 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
6823 return SAMR_VALIDATION_STATUS_SUCCESS;
6826 /****************************************************************
6827 _samr_ValidatePassword
6828 ****************************************************************/
6830 NTSTATUS _samr_ValidatePassword(struct pipes_struct *p,
6831 struct samr_ValidatePassword *r)
6833 union samr_ValidatePasswordRep *rep;
6835 struct samr_GetDomPwInfo pw;
6836 struct samr_PwInfo dom_pw_info;
6838 if (r->in.level < 1 || r->in.level > 3) {
6839 return NT_STATUS_INVALID_INFO_CLASS;
6842 pw.in.domain_name = NULL;
6843 pw.out.info = &dom_pw_info;
6845 status = _samr_GetDomPwInfo(p, &pw);
6846 if (!NT_STATUS_IS_OK(status)) {
6850 rep = talloc_zero(p->mem_ctx, union samr_ValidatePasswordRep);
6852 return NT_STATUS_NO_MEMORY;
6855 switch (r->in.level) {
6857 status = NT_STATUS_NOT_SUPPORTED;
6860 rep->ctr2.status = samr_ValidatePassword_Change(p->mem_ctx,
6866 rep->ctr3.status = samr_ValidatePassword_Reset(p->mem_ctx,
6872 status = NT_STATUS_INVALID_INFO_CLASS;
6876 if (!NT_STATUS_IS_OK(status)) {
6883 return NT_STATUS_OK;
6886 /****************************************************************
6887 ****************************************************************/
6889 NTSTATUS _samr_Shutdown(struct pipes_struct *p,
6890 struct samr_Shutdown *r)
6892 p->rng_fault_state = true;
6893 return NT_STATUS_NOT_IMPLEMENTED;
6896 /****************************************************************
6897 ****************************************************************/
6899 NTSTATUS _samr_SetMemberAttributesOfGroup(struct pipes_struct *p,
6900 struct samr_SetMemberAttributesOfGroup *r)
6902 p->rng_fault_state = true;
6903 return NT_STATUS_NOT_IMPLEMENTED;
6906 /****************************************************************
6907 ****************************************************************/
6909 NTSTATUS _samr_TestPrivateFunctionsDomain(struct pipes_struct *p,
6910 struct samr_TestPrivateFunctionsDomain *r)
6912 return NT_STATUS_NOT_IMPLEMENTED;
6915 /****************************************************************
6916 ****************************************************************/
6918 NTSTATUS _samr_TestPrivateFunctionsUser(struct pipes_struct *p,
6919 struct samr_TestPrivateFunctionsUser *r)
6921 return NT_STATUS_NOT_IMPLEMENTED;
6924 /****************************************************************
6925 ****************************************************************/
6927 NTSTATUS _samr_AddMultipleMembersToAlias(struct pipes_struct *p,
6928 struct samr_AddMultipleMembersToAlias *r)
6930 p->rng_fault_state = true;
6931 return NT_STATUS_NOT_IMPLEMENTED;
6934 /****************************************************************
6935 ****************************************************************/
6937 NTSTATUS _samr_RemoveMultipleMembersFromAlias(struct pipes_struct *p,
6938 struct samr_RemoveMultipleMembersFromAlias *r)
6940 p->rng_fault_state = true;
6941 return NT_STATUS_NOT_IMPLEMENTED;
6944 /****************************************************************
6945 ****************************************************************/
6947 NTSTATUS _samr_SetBootKeyInformation(struct pipes_struct *p,
6948 struct samr_SetBootKeyInformation *r)
6950 p->rng_fault_state = true;
6951 return NT_STATUS_NOT_IMPLEMENTED;
6954 /****************************************************************
6955 ****************************************************************/
6957 NTSTATUS _samr_GetBootKeyInformation(struct pipes_struct *p,
6958 struct samr_GetBootKeyInformation *r)
6960 p->rng_fault_state = true;
6961 return NT_STATUS_NOT_IMPLEMENTED;
6964 /****************************************************************
6965 ****************************************************************/
6967 NTSTATUS _samr_SetDsrmPassword(struct pipes_struct *p,
6968 struct samr_SetDsrmPassword *r)
6970 p->rng_fault_state = true;
6971 return NT_STATUS_NOT_IMPLEMENTED;