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"
46 #define DBGC_CLASS DBGC_RPC_SRV
48 #define SAMR_USR_RIGHTS_WRITE_PW \
49 ( READ_CONTROL_ACCESS | \
50 SAMR_USER_ACCESS_CHANGE_PASSWORD | \
51 SAMR_USER_ACCESS_SET_LOC_COM)
52 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
53 ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
55 #define DISP_INFO_CACHE_TIMEOUT 10
57 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
58 #define MAX_SAM_ENTRIES_W95 50
60 struct samr_connect_info {
64 struct samr_domain_info {
66 struct disp_info *disp_info;
69 struct samr_user_info {
73 struct samr_group_info {
77 struct samr_alias_info {
81 typedef struct disp_info {
82 struct dom_sid sid; /* identify which domain this is. */
83 struct pdb_search *users; /* querydispinfo 1 and 4 */
84 struct pdb_search *machines; /* querydispinfo 2 */
85 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
86 struct pdb_search *aliases; /* enumaliases */
88 uint32_t enum_acb_mask;
89 struct pdb_search *enum_users; /* enumusers with a mask */
91 struct timed_event *cache_timeout_event; /* cache idle timeout
95 static const struct generic_mapping sam_generic_mapping = {
96 GENERIC_RIGHTS_SAM_READ,
97 GENERIC_RIGHTS_SAM_WRITE,
98 GENERIC_RIGHTS_SAM_EXECUTE,
99 GENERIC_RIGHTS_SAM_ALL_ACCESS};
100 static const struct generic_mapping dom_generic_mapping = {
101 GENERIC_RIGHTS_DOMAIN_READ,
102 GENERIC_RIGHTS_DOMAIN_WRITE,
103 GENERIC_RIGHTS_DOMAIN_EXECUTE,
104 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
105 static const struct generic_mapping usr_generic_mapping = {
106 GENERIC_RIGHTS_USER_READ,
107 GENERIC_RIGHTS_USER_WRITE,
108 GENERIC_RIGHTS_USER_EXECUTE,
109 GENERIC_RIGHTS_USER_ALL_ACCESS};
110 static const struct generic_mapping usr_nopwchange_generic_mapping = {
111 GENERIC_RIGHTS_USER_READ,
112 GENERIC_RIGHTS_USER_WRITE,
113 GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
114 GENERIC_RIGHTS_USER_ALL_ACCESS};
115 static const struct generic_mapping grp_generic_mapping = {
116 GENERIC_RIGHTS_GROUP_READ,
117 GENERIC_RIGHTS_GROUP_WRITE,
118 GENERIC_RIGHTS_GROUP_EXECUTE,
119 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
120 static const struct generic_mapping ali_generic_mapping = {
121 GENERIC_RIGHTS_ALIAS_READ,
122 GENERIC_RIGHTS_ALIAS_WRITE,
123 GENERIC_RIGHTS_ALIAS_EXECUTE,
124 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
126 /*******************************************************************
127 *******************************************************************/
129 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, struct security_descriptor **psd, size_t *sd_size,
130 const struct generic_mapping *map,
131 struct dom_sid *sid, uint32 sid_access )
133 struct dom_sid domadmin_sid;
134 struct security_ace ace[5]; /* at most 5 entries */
137 struct security_acl *psa = NULL;
139 /* basic access for Everyone */
141 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
142 map->generic_execute | map->generic_read, 0);
144 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
146 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
147 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
148 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
149 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
151 /* Add Full Access for Domain Admins if we are a DC */
154 sid_compose(&domadmin_sid, get_global_sam_sid(),
156 init_sec_ace(&ace[i++], &domadmin_sid,
157 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
160 /* if we have a sid, give it some special access */
163 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sid_access, 0);
166 /* create the security descriptor */
168 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
169 return NT_STATUS_NO_MEMORY;
171 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
172 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
173 psa, sd_size)) == NULL)
174 return NT_STATUS_NO_MEMORY;
179 /*******************************************************************
180 Checks if access to an object should be granted, and returns that
181 level of access for further checks.
183 If the user has either of needed_priv_1 or needed_priv_2 then they
184 get the rights in rights_mask in addition to any calulated rights.
186 This handles the unusual case where we need to allow two different
187 privileges to obtain exactly the same rights, which occours only in
189 ********************************************************************/
191 NTSTATUS access_check_object( struct security_descriptor *psd, struct security_token *token,
192 enum sec_privilege needed_priv_1, enum sec_privilege needed_priv_2,
194 uint32 des_access, uint32 *acc_granted,
197 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
198 uint32 saved_mask = 0;
200 /* check privileges; certain SAM access bits should be overridden
201 by privileges (mostly having to do with creating/modifying/deleting
204 if ((needed_priv_1 != SEC_PRIV_INVALID && security_token_has_privilege(token, needed_priv_1)) ||
205 (needed_priv_2 != SEC_PRIV_INVALID && security_token_has_privilege(token, needed_priv_2))) {
206 saved_mask = (des_access & rights_mask);
207 des_access &= ~saved_mask;
209 DEBUG(4,("access_check_object: user rights access mask [0x%x]\n",
214 /* check the security descriptor first */
216 status = se_access_check(psd, token, des_access, acc_granted);
217 if (NT_STATUS_IS_OK(status)) {
221 /* give root a free pass */
223 if ( geteuid() == sec_initial_uid() ) {
225 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
226 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
228 *acc_granted = des_access;
230 status = NT_STATUS_OK;
236 /* add in any bits saved during the privilege check (only
237 matters is status is ok) */
239 *acc_granted |= rights_mask;
241 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
242 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
243 des_access, *acc_granted));
249 /*******************************************************************
250 Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
251 ********************************************************************/
253 void map_max_allowed_access(const struct security_token *nt_token,
254 const struct security_unix_token *unix_token,
255 uint32_t *pacc_requested)
257 if (!((*pacc_requested) & MAXIMUM_ALLOWED_ACCESS)) {
260 *pacc_requested &= ~MAXIMUM_ALLOWED_ACCESS;
262 /* At least try for generic read|execute - Everyone gets that. */
263 *pacc_requested = GENERIC_READ_ACCESS|GENERIC_EXECUTE_ACCESS;
265 /* root gets anything. */
266 if (unix_token->uid == sec_initial_uid()) {
267 *pacc_requested |= GENERIC_ALL_ACCESS;
271 /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
273 if (security_token_has_sid(nt_token, &global_sid_Builtin_Administrators) ||
274 security_token_has_sid(nt_token, &global_sid_Builtin_Account_Operators)) {
275 *pacc_requested |= GENERIC_ALL_ACCESS;
279 /* Full access for DOMAIN\Domain Admins. */
281 struct dom_sid domadmin_sid;
282 sid_compose(&domadmin_sid, get_global_sam_sid(),
284 if (security_token_has_sid(nt_token, &domadmin_sid)) {
285 *pacc_requested |= GENERIC_ALL_ACCESS;
289 /* TODO ! Check privileges. */
292 /*******************************************************************
293 Fetch or create a dispinfo struct.
294 ********************************************************************/
296 static DISP_INFO *get_samr_dispinfo_by_sid(const struct dom_sid *psid)
299 * We do a static cache for DISP_INFO's here. Explanation can be found
300 * in Jeremy's checkin message to r11793:
302 * Fix the SAMR cache so it works across completely insane
303 * client behaviour (ie.:
304 * open pipe/open SAMR handle/enumerate 0 - 1024
305 * close SAMR handle, close pipe.
306 * open pipe/open SAMR handle/enumerate 1024 - 2048...
307 * close SAMR handle, close pipe.
308 * And on ad-nausium. Amazing.... probably object-oriented
309 * client side programming in action yet again.
310 * This change should *massively* improve performance when
311 * enumerating users from an LDAP database.
314 * "Our" and the builtin domain are the only ones where we ever
315 * enumerate stuff, so just cache 2 entries.
318 static struct disp_info *builtin_dispinfo;
319 static struct disp_info *domain_dispinfo;
321 /* There are two cases to consider here:
322 1) The SID is a domain SID and we look for an equality match, or
323 2) This is an account SID and so we return the DISP_INFO* for our
330 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
332 * Necessary only once, but it does not really hurt.
334 if (builtin_dispinfo == NULL) {
335 builtin_dispinfo = talloc_zero(NULL, struct disp_info);
336 if (builtin_dispinfo == NULL) {
340 sid_copy(&builtin_dispinfo->sid, &global_sid_Builtin);
342 return builtin_dispinfo;
345 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
347 * Necessary only once, but it does not really hurt.
349 if (domain_dispinfo == NULL) {
350 domain_dispinfo = talloc_zero(NULL, struct disp_info);
351 if (domain_dispinfo == NULL) {
355 sid_copy(&domain_dispinfo->sid, get_global_sam_sid());
357 return domain_dispinfo;
363 /*******************************************************************
364 Function to free the per SID data.
365 ********************************************************************/
367 static void free_samr_cache(DISP_INFO *disp_info)
369 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
370 sid_string_dbg(&disp_info->sid)));
372 /* We need to become root here because the paged search might have to
373 * tell the LDAP server we're not interested in the rest anymore. */
377 TALLOC_FREE(disp_info->users);
378 TALLOC_FREE(disp_info->machines);
379 TALLOC_FREE(disp_info->groups);
380 TALLOC_FREE(disp_info->aliases);
381 TALLOC_FREE(disp_info->enum_users);
386 /*******************************************************************
387 Idle event handler. Throw away the disp info cache.
388 ********************************************************************/
390 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
391 struct timed_event *te,
395 DISP_INFO *disp_info = (DISP_INFO *)private_data;
397 TALLOC_FREE(disp_info->cache_timeout_event);
399 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
401 free_samr_cache(disp_info);
404 /*******************************************************************
405 Setup cache removal idle event handler.
406 ********************************************************************/
408 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
410 /* Remove any pending timeout and update. */
412 TALLOC_FREE(disp_info->cache_timeout_event);
414 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
415 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
416 (unsigned int)secs_fromnow ));
418 disp_info->cache_timeout_event = event_add_timed(
419 server_event_context(), NULL,
420 timeval_current_ofs(secs_fromnow, 0),
421 disp_info_cache_idle_timeout_handler, (void *)disp_info);
424 /*******************************************************************
425 Force flush any cache. We do this on any samr_set_xxx call.
426 We must also remove the timeout handler.
427 ********************************************************************/
429 static void force_flush_samr_cache(const struct dom_sid *sid)
431 struct disp_info *disp_info = get_samr_dispinfo_by_sid(sid);
433 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
437 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
438 TALLOC_FREE(disp_info->cache_timeout_event);
439 free_samr_cache(disp_info);
442 /*******************************************************************
443 Ensure password info is never given out. Paranioa... JRA.
444 ********************************************************************/
446 static void samr_clear_sam_passwd(struct samu *sam_pass)
452 /* These now zero out the old password */
454 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
455 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
458 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
460 struct samr_displayentry *entry;
462 if (sid_check_is_builtin(&info->sid)) {
463 /* No users in builtin. */
467 if (info->users == NULL) {
468 info->users = pdb_search_users(info, acct_flags);
469 if (info->users == NULL) {
473 /* Fetch the last possible entry, thus trigger an enumeration */
474 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
476 /* Ensure we cache this enumeration. */
477 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
479 return info->users->num_entries;
482 static uint32 count_sam_groups(struct disp_info *info)
484 struct samr_displayentry *entry;
486 if (sid_check_is_builtin(&info->sid)) {
487 /* No groups in builtin. */
491 if (info->groups == NULL) {
492 info->groups = pdb_search_groups(info);
493 if (info->groups == NULL) {
497 /* Fetch the last possible entry, thus trigger an enumeration */
498 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
500 /* Ensure we cache this enumeration. */
501 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
503 return info->groups->num_entries;
506 static uint32 count_sam_aliases(struct disp_info *info)
508 struct samr_displayentry *entry;
510 if (info->aliases == NULL) {
511 info->aliases = pdb_search_aliases(info, &info->sid);
512 if (info->aliases == NULL) {
516 /* Fetch the last possible entry, thus trigger an enumeration */
517 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
519 /* Ensure we cache this enumeration. */
520 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
522 return info->aliases->num_entries;
525 /*******************************************************************
527 ********************************************************************/
529 NTSTATUS _samr_Close(struct pipes_struct *p, struct samr_Close *r)
531 if (!close_policy_hnd(p, r->in.handle)) {
532 return NT_STATUS_INVALID_HANDLE;
535 ZERO_STRUCTP(r->out.handle);
540 /*******************************************************************
542 ********************************************************************/
544 NTSTATUS _samr_OpenDomain(struct pipes_struct *p,
545 struct samr_OpenDomain *r)
547 struct samr_connect_info *cinfo;
548 struct samr_domain_info *dinfo;
549 struct security_descriptor *psd = NULL;
551 uint32 des_access = r->in.access_mask;
554 uint32_t extra_access = SAMR_DOMAIN_ACCESS_CREATE_USER;
556 /* find the connection policy handle. */
558 cinfo = policy_handle_find(p, r->in.connect_handle, 0, NULL,
559 struct samr_connect_info, &status);
560 if (!NT_STATUS_IS_OK(status)) {
564 /*check if access can be granted as requested by client. */
565 map_max_allowed_access(p->session_info->security_token,
566 &p->session_info->utok,
569 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
570 se_map_generic( &des_access, &dom_generic_mapping );
573 * Users with SeAddUser get the ability to manipulate groups
576 if (security_token_has_privilege(p->session_info->security_token, SEC_PRIV_ADD_USERS)) {
577 extra_access |= (SAMR_DOMAIN_ACCESS_CREATE_GROUP |
578 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS |
579 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT |
580 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS |
581 SAMR_DOMAIN_ACCESS_CREATE_ALIAS);
585 * Users with SeMachineAccount or SeAddUser get additional
586 * SAMR_DOMAIN_ACCESS_CREATE_USER access.
589 status = access_check_object( psd, p->session_info->security_token,
590 SEC_PRIV_MACHINE_ACCOUNT, SEC_PRIV_ADD_USERS,
591 extra_access, des_access,
592 &acc_granted, "_samr_OpenDomain" );
594 if ( !NT_STATUS_IS_OK(status) )
597 if (!sid_check_is_domain(r->in.sid) &&
598 !sid_check_is_builtin(r->in.sid)) {
599 return NT_STATUS_NO_SUCH_DOMAIN;
602 dinfo = policy_handle_create(p, r->out.domain_handle, acc_granted,
603 struct samr_domain_info, &status);
604 if (!NT_STATUS_IS_OK(status)) {
607 dinfo->sid = *r->in.sid;
608 dinfo->disp_info = get_samr_dispinfo_by_sid(r->in.sid);
610 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
615 /*******************************************************************
617 ********************************************************************/
619 NTSTATUS _samr_GetUserPwInfo(struct pipes_struct *p,
620 struct samr_GetUserPwInfo *r)
622 struct samr_user_info *uinfo;
623 enum lsa_SidType sid_type;
624 uint32_t min_password_length = 0;
625 uint32_t password_properties = 0;
629 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
631 uinfo = policy_handle_find(p, r->in.user_handle,
632 SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
633 struct samr_user_info, &status);
634 if (!NT_STATUS_IS_OK(status)) {
638 if (!sid_check_is_in_our_domain(&uinfo->sid)) {
639 return NT_STATUS_OBJECT_TYPE_MISMATCH;
643 ret = lookup_sid(p->mem_ctx, &uinfo->sid, NULL, NULL, &sid_type);
646 return NT_STATUS_NO_SUCH_USER;
652 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
653 &min_password_length);
654 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
655 &password_properties);
658 if (lp_check_password_script() && *lp_check_password_script()) {
659 password_properties |= DOMAIN_PASSWORD_COMPLEX;
667 r->out.info->min_password_length = min_password_length;
668 r->out.info->password_properties = password_properties;
670 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
675 /*******************************************************************
677 ********************************************************************/
679 NTSTATUS _samr_SetSecurity(struct pipes_struct *p,
680 struct samr_SetSecurity *r)
682 struct samr_user_info *uinfo;
684 struct security_acl *dacl;
686 struct samu *sampass=NULL;
689 uinfo = policy_handle_find(p, r->in.handle,
690 SAMR_USER_ACCESS_SET_ATTRIBUTES, NULL,
691 struct samr_user_info, &status);
692 if (!NT_STATUS_IS_OK(status)) {
696 if (!(sampass = samu_new( p->mem_ctx))) {
697 DEBUG(0,("No memory!\n"));
698 return NT_STATUS_NO_MEMORY;
701 /* get the user record */
703 ret = pdb_getsampwsid(sampass, &uinfo->sid);
707 DEBUG(4, ("User %s not found\n",
708 sid_string_dbg(&uinfo->sid)));
709 TALLOC_FREE(sampass);
710 return NT_STATUS_INVALID_HANDLE;
713 dacl = r->in.sdbuf->sd->dacl;
714 for (i=0; i < dacl->num_aces; i++) {
715 if (dom_sid_equal(&uinfo->sid, &dacl->aces[i].trustee)) {
716 ret = pdb_set_pass_can_change(sampass,
717 (dacl->aces[i].access_mask &
718 SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
725 TALLOC_FREE(sampass);
726 return NT_STATUS_ACCESS_DENIED;
730 status = pdb_update_sam_account(sampass);
733 TALLOC_FREE(sampass);
738 /*******************************************************************
739 build correct perms based on policies and password times for _samr_query_sec_obj
740 *******************************************************************/
741 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, struct dom_sid *user_sid)
743 struct samu *sampass=NULL;
746 if ( !(sampass = samu_new( mem_ctx )) ) {
747 DEBUG(0,("No memory!\n"));
752 ret = pdb_getsampwsid(sampass, user_sid);
756 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
757 TALLOC_FREE(sampass);
761 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
763 if (pdb_get_pass_can_change(sampass)) {
764 TALLOC_FREE(sampass);
767 TALLOC_FREE(sampass);
772 /*******************************************************************
774 ********************************************************************/
776 NTSTATUS _samr_QuerySecurity(struct pipes_struct *p,
777 struct samr_QuerySecurity *r)
779 struct samr_connect_info *cinfo;
780 struct samr_domain_info *dinfo;
781 struct samr_user_info *uinfo;
782 struct samr_group_info *ginfo;
783 struct samr_alias_info *ainfo;
785 struct security_descriptor * psd = NULL;
788 cinfo = policy_handle_find(p, r->in.handle,
789 SEC_STD_READ_CONTROL, NULL,
790 struct samr_connect_info, &status);
791 if (NT_STATUS_IS_OK(status)) {
792 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
793 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
794 &sam_generic_mapping, NULL, 0);
798 dinfo = policy_handle_find(p, r->in.handle,
799 SEC_STD_READ_CONTROL, NULL,
800 struct samr_domain_info, &status);
801 if (NT_STATUS_IS_OK(status)) {
802 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
803 "with SID: %s\n", sid_string_dbg(&dinfo->sid)));
805 * TODO: Builtin probably needs a different SD with restricted
808 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
809 &dom_generic_mapping, NULL, 0);
813 uinfo = policy_handle_find(p, r->in.handle,
814 SEC_STD_READ_CONTROL, NULL,
815 struct samr_user_info, &status);
816 if (NT_STATUS_IS_OK(status)) {
817 DEBUG(10,("_samr_QuerySecurity: querying security on user "
818 "Object with SID: %s\n",
819 sid_string_dbg(&uinfo->sid)));
820 if (check_change_pw_access(p->mem_ctx, &uinfo->sid)) {
821 status = make_samr_object_sd(
822 p->mem_ctx, &psd, &sd_size,
823 &usr_generic_mapping,
824 &uinfo->sid, SAMR_USR_RIGHTS_WRITE_PW);
826 status = make_samr_object_sd(
827 p->mem_ctx, &psd, &sd_size,
828 &usr_nopwchange_generic_mapping,
829 &uinfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
834 ginfo = policy_handle_find(p, r->in.handle,
835 SEC_STD_READ_CONTROL, NULL,
836 struct samr_group_info, &status);
837 if (NT_STATUS_IS_OK(status)) {
839 * TODO: different SDs have to be generated for aliases groups
840 * and users. Currently all three get a default user SD
842 DEBUG(10,("_samr_QuerySecurity: querying security on group "
843 "Object with SID: %s\n",
844 sid_string_dbg(&ginfo->sid)));
845 status = make_samr_object_sd(
846 p->mem_ctx, &psd, &sd_size,
847 &usr_nopwchange_generic_mapping,
848 &ginfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
852 ainfo = policy_handle_find(p, r->in.handle,
853 SEC_STD_READ_CONTROL, NULL,
854 struct samr_alias_info, &status);
855 if (NT_STATUS_IS_OK(status)) {
857 * TODO: different SDs have to be generated for aliases groups
858 * and users. Currently all three get a default user SD
860 DEBUG(10,("_samr_QuerySecurity: querying security on alias "
861 "Object with SID: %s\n",
862 sid_string_dbg(&ainfo->sid)));
863 status = make_samr_object_sd(
864 p->mem_ctx, &psd, &sd_size,
865 &usr_nopwchange_generic_mapping,
866 &ainfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
870 return NT_STATUS_OBJECT_TYPE_MISMATCH;
872 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
873 return NT_STATUS_NO_MEMORY;
878 /*******************************************************************
879 makes a SAM_ENTRY / UNISTR2* structure from a user list.
880 ********************************************************************/
882 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
883 struct samr_SamEntry **sam_pp,
884 uint32_t num_entries,
886 struct samr_displayentry *entries)
889 struct samr_SamEntry *sam;
893 if (num_entries == 0) {
897 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
899 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
900 return NT_STATUS_NO_MEMORY;
903 for (i = 0; i < num_entries; i++) {
906 * usrmgr expects a non-NULL terminated string with
907 * trust relationships
909 if (entries[i].acct_flags & ACB_DOMTRUST) {
910 init_unistr2(&uni_temp_name, entries[i].account_name,
913 init_unistr2(&uni_temp_name, entries[i].account_name,
917 init_lsa_String(&sam[i].name, entries[i].account_name);
918 sam[i].idx = entries[i].rid;
926 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
928 /*******************************************************************
929 _samr_EnumDomainUsers
930 ********************************************************************/
932 NTSTATUS _samr_EnumDomainUsers(struct pipes_struct *p,
933 struct samr_EnumDomainUsers *r)
936 struct samr_domain_info *dinfo;
938 uint32 enum_context = *r->in.resume_handle;
939 enum remote_arch_types ra_type = get_remote_arch();
940 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
941 uint32 max_entries = max_sam_entries;
942 struct samr_displayentry *entries = NULL;
943 struct samr_SamArray *samr_array = NULL;
944 struct samr_SamEntry *samr_entries = NULL;
946 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
948 dinfo = policy_handle_find(p, r->in.domain_handle,
949 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
950 struct samr_domain_info, &status);
951 if (!NT_STATUS_IS_OK(status)) {
955 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
957 return NT_STATUS_NO_MEMORY;
959 *r->out.sam = samr_array;
961 if (sid_check_is_builtin(&dinfo->sid)) {
962 /* No users in builtin. */
963 *r->out.resume_handle = *r->in.resume_handle;
964 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
972 if ((dinfo->disp_info->enum_users != NULL) &&
973 (dinfo->disp_info->enum_acb_mask != r->in.acct_flags)) {
974 TALLOC_FREE(dinfo->disp_info->enum_users);
977 if (dinfo->disp_info->enum_users == NULL) {
978 dinfo->disp_info->enum_users = pdb_search_users(
979 dinfo->disp_info, r->in.acct_flags);
980 dinfo->disp_info->enum_acb_mask = r->in.acct_flags;
983 if (dinfo->disp_info->enum_users == NULL) {
984 /* END AS ROOT !!!! */
986 return NT_STATUS_ACCESS_DENIED;
989 num_account = pdb_search_entries(dinfo->disp_info->enum_users,
990 enum_context, max_entries,
993 /* END AS ROOT !!!! */
997 if (num_account == 0) {
998 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
1000 *r->out.resume_handle = *r->in.resume_handle;
1001 return NT_STATUS_OK;
1004 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
1005 num_account, enum_context,
1007 if (!NT_STATUS_IS_OK(status)) {
1011 if (max_entries <= num_account) {
1012 status = STATUS_MORE_ENTRIES;
1014 status = NT_STATUS_OK;
1017 /* Ensure we cache this enumeration. */
1018 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1020 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
1022 samr_array->count = num_account;
1023 samr_array->entries = samr_entries;
1025 *r->out.resume_handle = *r->in.resume_handle + num_account;
1026 *r->out.num_entries = num_account;
1028 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1033 /*******************************************************************
1034 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1035 ********************************************************************/
1037 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
1038 struct samr_SamEntry **sam_pp,
1039 uint32_t num_sam_entries,
1040 struct samr_displayentry *entries)
1042 struct samr_SamEntry *sam;
1047 if (num_sam_entries == 0) {
1051 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1056 for (i = 0; i < num_sam_entries; i++) {
1058 * JRA. I think this should include the null. TNG does not.
1060 init_lsa_String(&sam[i].name, entries[i].account_name);
1061 sam[i].idx = entries[i].rid;
1067 /*******************************************************************
1068 _samr_EnumDomainGroups
1069 ********************************************************************/
1071 NTSTATUS _samr_EnumDomainGroups(struct pipes_struct *p,
1072 struct samr_EnumDomainGroups *r)
1075 struct samr_domain_info *dinfo;
1076 struct samr_displayentry *groups;
1078 struct samr_SamArray *samr_array = NULL;
1079 struct samr_SamEntry *samr_entries = NULL;
1081 dinfo = policy_handle_find(p, r->in.domain_handle,
1082 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1083 struct samr_domain_info, &status);
1084 if (!NT_STATUS_IS_OK(status)) {
1088 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1090 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1092 return NT_STATUS_NO_MEMORY;
1094 *r->out.sam = samr_array;
1096 if (sid_check_is_builtin(&dinfo->sid)) {
1097 /* No groups in builtin. */
1098 *r->out.resume_handle = *r->in.resume_handle;
1099 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1103 /* the domain group array is being allocated in the function below */
1107 if (dinfo->disp_info->groups == NULL) {
1108 dinfo->disp_info->groups = pdb_search_groups(dinfo->disp_info);
1110 if (dinfo->disp_info->groups == NULL) {
1112 return NT_STATUS_ACCESS_DENIED;
1116 num_groups = pdb_search_entries(dinfo->disp_info->groups,
1117 *r->in.resume_handle,
1118 MAX_SAM_ENTRIES, &groups);
1121 /* Ensure we cache this enumeration. */
1122 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1124 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1125 num_groups, groups);
1127 if (MAX_SAM_ENTRIES <= num_groups) {
1128 status = STATUS_MORE_ENTRIES;
1130 status = NT_STATUS_OK;
1133 samr_array->count = num_groups;
1134 samr_array->entries = samr_entries;
1136 *r->out.num_entries = num_groups;
1137 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1139 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1144 /*******************************************************************
1145 _samr_EnumDomainAliases
1146 ********************************************************************/
1148 NTSTATUS _samr_EnumDomainAliases(struct pipes_struct *p,
1149 struct samr_EnumDomainAliases *r)
1152 struct samr_domain_info *dinfo;
1153 struct samr_displayentry *aliases;
1154 uint32 num_aliases = 0;
1155 struct samr_SamArray *samr_array = NULL;
1156 struct samr_SamEntry *samr_entries = NULL;
1158 dinfo = policy_handle_find(p, r->in.domain_handle,
1159 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1160 struct samr_domain_info, &status);
1161 if (!NT_STATUS_IS_OK(status)) {
1165 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1166 sid_string_dbg(&dinfo->sid)));
1168 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1170 return NT_STATUS_NO_MEMORY;
1175 if (dinfo->disp_info->aliases == NULL) {
1176 dinfo->disp_info->aliases = pdb_search_aliases(
1177 dinfo->disp_info, &dinfo->sid);
1178 if (dinfo->disp_info->aliases == NULL) {
1180 return NT_STATUS_ACCESS_DENIED;
1184 num_aliases = pdb_search_entries(dinfo->disp_info->aliases,
1185 *r->in.resume_handle,
1186 MAX_SAM_ENTRIES, &aliases);
1189 /* Ensure we cache this enumeration. */
1190 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1192 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1193 num_aliases, aliases);
1195 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1197 if (MAX_SAM_ENTRIES <= num_aliases) {
1198 status = STATUS_MORE_ENTRIES;
1200 status = NT_STATUS_OK;
1203 samr_array->count = num_aliases;
1204 samr_array->entries = samr_entries;
1206 *r->out.sam = samr_array;
1207 *r->out.num_entries = num_aliases;
1208 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1213 /*******************************************************************
1214 inits a samr_DispInfoGeneral structure.
1215 ********************************************************************/
1217 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1218 struct samr_DispInfoGeneral *r,
1219 uint32_t num_entries,
1221 struct samr_displayentry *entries)
1225 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1227 if (num_entries == 0) {
1228 return NT_STATUS_OK;
1231 r->count = num_entries;
1233 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1235 return NT_STATUS_NO_MEMORY;
1238 for (i = 0; i < num_entries ; i++) {
1240 init_lsa_String(&r->entries[i].account_name,
1241 entries[i].account_name);
1243 init_lsa_String(&r->entries[i].description,
1244 entries[i].description);
1246 init_lsa_String(&r->entries[i].full_name,
1247 entries[i].fullname);
1249 r->entries[i].rid = entries[i].rid;
1250 r->entries[i].acct_flags = entries[i].acct_flags;
1251 r->entries[i].idx = start_idx+i+1;
1254 return NT_STATUS_OK;
1257 /*******************************************************************
1258 inits a samr_DispInfoFull structure.
1259 ********************************************************************/
1261 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1262 struct samr_DispInfoFull *r,
1263 uint32_t num_entries,
1265 struct samr_displayentry *entries)
1269 DEBUG(10, ("init_samr_dispinfo_2: num_entries: %d\n", num_entries));
1271 if (num_entries == 0) {
1272 return NT_STATUS_OK;
1275 r->count = num_entries;
1277 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFull, num_entries);
1279 return NT_STATUS_NO_MEMORY;
1282 for (i = 0; i < num_entries ; i++) {
1284 init_lsa_String(&r->entries[i].account_name,
1285 entries[i].account_name);
1287 init_lsa_String(&r->entries[i].description,
1288 entries[i].description);
1290 r->entries[i].rid = entries[i].rid;
1291 r->entries[i].acct_flags = entries[i].acct_flags;
1292 r->entries[i].idx = start_idx+i+1;
1295 return NT_STATUS_OK;
1298 /*******************************************************************
1299 inits a samr_DispInfoFullGroups structure.
1300 ********************************************************************/
1302 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1303 struct samr_DispInfoFullGroups *r,
1304 uint32_t num_entries,
1306 struct samr_displayentry *entries)
1310 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1312 if (num_entries == 0) {
1313 return NT_STATUS_OK;
1316 r->count = num_entries;
1318 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1320 return NT_STATUS_NO_MEMORY;
1323 for (i = 0; i < num_entries ; i++) {
1325 init_lsa_String(&r->entries[i].account_name,
1326 entries[i].account_name);
1328 init_lsa_String(&r->entries[i].description,
1329 entries[i].description);
1331 r->entries[i].rid = entries[i].rid;
1332 r->entries[i].acct_flags = entries[i].acct_flags;
1333 r->entries[i].idx = start_idx+i+1;
1336 return NT_STATUS_OK;
1339 /*******************************************************************
1340 inits a samr_DispInfoAscii structure.
1341 ********************************************************************/
1343 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1344 struct samr_DispInfoAscii *r,
1345 uint32_t num_entries,
1347 struct samr_displayentry *entries)
1351 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1353 if (num_entries == 0) {
1354 return NT_STATUS_OK;
1357 r->count = num_entries;
1359 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1361 return NT_STATUS_NO_MEMORY;
1364 for (i = 0; i < num_entries ; i++) {
1366 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1367 entries[i].account_name);
1369 r->entries[i].idx = start_idx+i+1;
1372 return NT_STATUS_OK;
1375 /*******************************************************************
1376 inits a samr_DispInfoAscii structure.
1377 ********************************************************************/
1379 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1380 struct samr_DispInfoAscii *r,
1381 uint32_t num_entries,
1383 struct samr_displayentry *entries)
1387 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1389 if (num_entries == 0) {
1390 return NT_STATUS_OK;
1393 r->count = num_entries;
1395 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1397 return NT_STATUS_NO_MEMORY;
1400 for (i = 0; i < num_entries ; i++) {
1402 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1403 entries[i].account_name);
1405 r->entries[i].idx = start_idx+i+1;
1408 return NT_STATUS_OK;
1411 /*******************************************************************
1412 _samr_QueryDisplayInfo
1413 ********************************************************************/
1415 NTSTATUS _samr_QueryDisplayInfo(struct pipes_struct *p,
1416 struct samr_QueryDisplayInfo *r)
1419 struct samr_domain_info *dinfo;
1420 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1422 uint32 max_entries = r->in.max_entries;
1424 union samr_DispInfo *disp_info = r->out.info;
1427 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1428 uint32 num_account = 0;
1429 enum remote_arch_types ra_type = get_remote_arch();
1430 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1431 struct samr_displayentry *entries = NULL;
1433 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1435 dinfo = policy_handle_find(p, r->in.domain_handle,
1436 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1437 struct samr_domain_info, &status);
1438 if (!NT_STATUS_IS_OK(status)) {
1442 if (sid_check_is_builtin(&dinfo->sid)) {
1443 DEBUG(5,("_samr_QueryDisplayInfo: no users in BUILTIN\n"));
1444 return NT_STATUS_OK;
1448 * calculate how many entries we will return.
1450 * - the number of entries the client asked
1451 * - our limit on that
1452 * - the starting point (enumeration context)
1453 * - the buffer size the client will accept
1457 * We are a lot more like W2K. Instead of reading the SAM
1458 * each time to find the records we need to send back,
1459 * we read it once and link that copy to the sam handle.
1460 * For large user list (over the MAX_SAM_ENTRIES)
1461 * it's a definitive win.
1462 * second point to notice: between enumerations
1463 * our sam is now the same as it's a snapshoot.
1464 * third point: got rid of the static SAM_USER_21 struct
1465 * no more intermediate.
1466 * con: it uses much more memory, as a full copy is stored
1469 * If you want to change it, think twice and think
1470 * of the second point , that's really important.
1475 if ((r->in.level < 1) || (r->in.level > 5)) {
1476 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1477 (unsigned int)r->in.level ));
1478 return NT_STATUS_INVALID_INFO_CLASS;
1481 /* first limit the number of entries we will return */
1482 if (r->in.max_entries > max_sam_entries) {
1483 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1484 "entries, limiting to %d\n", r->in.max_entries,
1486 max_entries = max_sam_entries;
1489 /* calculate the size and limit on the number of entries we will
1492 temp_size=max_entries*struct_size;
1494 if (temp_size > r->in.buf_size) {
1495 max_entries = MIN((r->in.buf_size / struct_size),max_entries);
1496 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1497 "only %d entries\n", max_entries));
1502 /* THe following done as ROOT. Don't return without unbecome_root(). */
1504 switch (r->in.level) {
1507 if (dinfo->disp_info->users == NULL) {
1508 dinfo->disp_info->users = pdb_search_users(
1509 dinfo->disp_info, ACB_NORMAL);
1510 if (dinfo->disp_info->users == NULL) {
1512 return NT_STATUS_ACCESS_DENIED;
1514 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1515 (unsigned int)r->in.start_idx));
1517 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1518 (unsigned int)r->in.start_idx));
1521 num_account = pdb_search_entries(dinfo->disp_info->users,
1522 r->in.start_idx, max_entries,
1526 if (dinfo->disp_info->machines == NULL) {
1527 dinfo->disp_info->machines = pdb_search_users(
1528 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
1529 if (dinfo->disp_info->machines == NULL) {
1531 return NT_STATUS_ACCESS_DENIED;
1533 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1534 (unsigned int)r->in.start_idx));
1536 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1537 (unsigned int)r->in.start_idx));
1540 num_account = pdb_search_entries(dinfo->disp_info->machines,
1541 r->in.start_idx, max_entries,
1546 if (dinfo->disp_info->groups == NULL) {
1547 dinfo->disp_info->groups = pdb_search_groups(
1549 if (dinfo->disp_info->groups == NULL) {
1551 return NT_STATUS_ACCESS_DENIED;
1553 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1554 (unsigned int)r->in.start_idx));
1556 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1557 (unsigned int)r->in.start_idx));
1560 num_account = pdb_search_entries(dinfo->disp_info->groups,
1561 r->in.start_idx, max_entries,
1566 smb_panic("info class changed");
1572 /* Now create reply structure */
1573 switch (r->in.level) {
1575 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1576 num_account, r->in.start_idx,
1580 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1581 num_account, r->in.start_idx,
1585 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1586 num_account, r->in.start_idx,
1590 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1591 num_account, r->in.start_idx,
1595 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1596 num_account, r->in.start_idx,
1600 smb_panic("info class changed");
1604 if (!NT_STATUS_IS_OK(disp_ret))
1607 if (max_entries <= num_account) {
1608 status = STATUS_MORE_ENTRIES;
1610 status = NT_STATUS_OK;
1613 /* Ensure we cache this enumeration. */
1614 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1616 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1618 *r->out.total_size = num_account * struct_size;
1619 *r->out.returned_size = num_account ? temp_size : 0;
1624 /****************************************************************
1625 _samr_QueryDisplayInfo2
1626 ****************************************************************/
1628 NTSTATUS _samr_QueryDisplayInfo2(struct pipes_struct *p,
1629 struct samr_QueryDisplayInfo2 *r)
1631 struct samr_QueryDisplayInfo q;
1633 q.in.domain_handle = r->in.domain_handle;
1634 q.in.level = r->in.level;
1635 q.in.start_idx = r->in.start_idx;
1636 q.in.max_entries = r->in.max_entries;
1637 q.in.buf_size = r->in.buf_size;
1639 q.out.total_size = r->out.total_size;
1640 q.out.returned_size = r->out.returned_size;
1641 q.out.info = r->out.info;
1643 return _samr_QueryDisplayInfo(p, &q);
1646 /****************************************************************
1647 _samr_QueryDisplayInfo3
1648 ****************************************************************/
1650 NTSTATUS _samr_QueryDisplayInfo3(struct pipes_struct *p,
1651 struct samr_QueryDisplayInfo3 *r)
1653 struct samr_QueryDisplayInfo q;
1655 q.in.domain_handle = r->in.domain_handle;
1656 q.in.level = r->in.level;
1657 q.in.start_idx = r->in.start_idx;
1658 q.in.max_entries = r->in.max_entries;
1659 q.in.buf_size = r->in.buf_size;
1661 q.out.total_size = r->out.total_size;
1662 q.out.returned_size = r->out.returned_size;
1663 q.out.info = r->out.info;
1665 return _samr_QueryDisplayInfo(p, &q);
1668 /*******************************************************************
1669 _samr_QueryAliasInfo
1670 ********************************************************************/
1672 NTSTATUS _samr_QueryAliasInfo(struct pipes_struct *p,
1673 struct samr_QueryAliasInfo *r)
1675 struct samr_alias_info *ainfo;
1676 struct acct_info info;
1678 union samr_AliasInfo *alias_info = NULL;
1679 const char *alias_name = NULL;
1680 const char *alias_description = NULL;
1682 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1684 ainfo = policy_handle_find(p, r->in.alias_handle,
1685 SAMR_ALIAS_ACCESS_LOOKUP_INFO, NULL,
1686 struct samr_alias_info, &status);
1687 if (!NT_STATUS_IS_OK(status)) {
1691 alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1693 return NT_STATUS_NO_MEMORY;
1697 status = pdb_get_aliasinfo(&ainfo->sid, &info);
1700 if ( !NT_STATUS_IS_OK(status))
1703 /* FIXME: info contains fstrings */
1704 alias_name = talloc_strdup(r, info.acct_name);
1705 alias_description = talloc_strdup(r, info.acct_desc);
1707 switch (r->in.level) {
1709 alias_info->all.name.string = alias_name;
1710 alias_info->all.num_members = 1; /* ??? */
1711 alias_info->all.description.string = alias_description;
1714 alias_info->name.string = alias_name;
1716 case ALIASINFODESCRIPTION:
1717 alias_info->description.string = alias_description;
1720 return NT_STATUS_INVALID_INFO_CLASS;
1723 *r->out.info = alias_info;
1725 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1727 return NT_STATUS_OK;
1730 /*******************************************************************
1732 ********************************************************************/
1734 NTSTATUS _samr_LookupNames(struct pipes_struct *p,
1735 struct samr_LookupNames *r)
1737 struct samr_domain_info *dinfo;
1740 enum lsa_SidType *type;
1742 int num_rids = r->in.num_names;
1743 struct samr_Ids rids, types;
1744 uint32_t num_mapped = 0;
1746 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1748 dinfo = policy_handle_find(p, r->in.domain_handle,
1749 0 /* Don't know the acc_bits yet */, NULL,
1750 struct samr_domain_info, &status);
1751 if (!NT_STATUS_IS_OK(status)) {
1755 if (num_rids > MAX_SAM_ENTRIES) {
1756 num_rids = MAX_SAM_ENTRIES;
1757 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1760 rid = talloc_array(p->mem_ctx, uint32, num_rids);
1761 NT_STATUS_HAVE_NO_MEMORY(rid);
1763 type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1764 NT_STATUS_HAVE_NO_MEMORY(type);
1766 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1767 sid_string_dbg(&dinfo->sid)));
1769 for (i = 0; i < num_rids; i++) {
1771 status = NT_STATUS_NONE_MAPPED;
1772 type[i] = SID_NAME_UNKNOWN;
1774 rid[i] = 0xffffffff;
1776 if (sid_check_is_builtin(&dinfo->sid)) {
1777 if (lookup_builtin_name(r->in.names[i].string,
1780 type[i] = SID_NAME_ALIAS;
1783 lookup_global_sam_name(r->in.names[i].string, 0,
1787 if (type[i] != SID_NAME_UNKNOWN) {
1792 if (num_mapped == num_rids) {
1793 status = NT_STATUS_OK;
1794 } else if (num_mapped == 0) {
1795 status = NT_STATUS_NONE_MAPPED;
1797 status = STATUS_SOME_UNMAPPED;
1800 rids.count = num_rids;
1803 types.count = num_rids;
1804 types.ids = talloc_array(p->mem_ctx, uint32_t, num_rids);
1805 NT_STATUS_HAVE_NO_MEMORY(type);
1806 for (i = 0; i < num_rids; i++) {
1807 types.ids[i] = (type[i] & 0xffffffff);
1810 *r->out.rids = rids;
1811 *r->out.types = types;
1813 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1818 /****************************************************************
1819 _samr_ChangePasswordUser
1820 ****************************************************************/
1822 NTSTATUS _samr_ChangePasswordUser(struct pipes_struct *p,
1823 struct samr_ChangePasswordUser *r)
1827 struct samr_user_info *uinfo;
1829 struct samr_Password new_lmPwdHash, new_ntPwdHash, checkHash;
1830 struct samr_Password lm_pwd, nt_pwd;
1832 uinfo = policy_handle_find(p, r->in.user_handle,
1833 SAMR_USER_ACCESS_SET_PASSWORD, NULL,
1834 struct samr_user_info, &status);
1835 if (!NT_STATUS_IS_OK(status)) {
1839 DEBUG(5,("_samr_ChangePasswordUser: sid:%s\n",
1840 sid_string_dbg(&uinfo->sid)));
1842 if (!(pwd = samu_new(NULL))) {
1843 return NT_STATUS_NO_MEMORY;
1847 ret = pdb_getsampwsid(pwd, &uinfo->sid);
1852 return NT_STATUS_WRONG_PASSWORD;
1856 const uint8_t *lm_pass, *nt_pass;
1858 lm_pass = pdb_get_lanman_passwd(pwd);
1859 nt_pass = pdb_get_nt_passwd(pwd);
1861 if (!lm_pass || !nt_pass) {
1862 status = NT_STATUS_WRONG_PASSWORD;
1866 memcpy(&lm_pwd.hash, lm_pass, sizeof(lm_pwd.hash));
1867 memcpy(&nt_pwd.hash, nt_pass, sizeof(nt_pwd.hash));
1870 /* basic sanity checking on parameters. Do this before any database ops */
1871 if (!r->in.lm_present || !r->in.nt_present ||
1872 !r->in.old_lm_crypted || !r->in.new_lm_crypted ||
1873 !r->in.old_nt_crypted || !r->in.new_nt_crypted) {
1874 /* we should really handle a change with lm not
1876 status = NT_STATUS_INVALID_PARAMETER_MIX;
1880 /* decrypt and check the new lm hash */
1881 D_P16(lm_pwd.hash, r->in.new_lm_crypted->hash, new_lmPwdHash.hash);
1882 D_P16(new_lmPwdHash.hash, r->in.old_lm_crypted->hash, checkHash.hash);
1883 if (memcmp(checkHash.hash, lm_pwd.hash, 16) != 0) {
1884 status = NT_STATUS_WRONG_PASSWORD;
1888 /* decrypt and check the new nt hash */
1889 D_P16(nt_pwd.hash, r->in.new_nt_crypted->hash, new_ntPwdHash.hash);
1890 D_P16(new_ntPwdHash.hash, r->in.old_nt_crypted->hash, checkHash.hash);
1891 if (memcmp(checkHash.hash, nt_pwd.hash, 16) != 0) {
1892 status = NT_STATUS_WRONG_PASSWORD;
1896 /* The NT Cross is not required by Win2k3 R2, but if present
1897 check the nt cross hash */
1898 if (r->in.cross1_present && r->in.nt_cross) {
1899 D_P16(lm_pwd.hash, r->in.nt_cross->hash, checkHash.hash);
1900 if (memcmp(checkHash.hash, new_ntPwdHash.hash, 16) != 0) {
1901 status = NT_STATUS_WRONG_PASSWORD;
1906 /* The LM Cross is not required by Win2k3 R2, but if present
1907 check the lm cross hash */
1908 if (r->in.cross2_present && r->in.lm_cross) {
1909 D_P16(nt_pwd.hash, r->in.lm_cross->hash, checkHash.hash);
1910 if (memcmp(checkHash.hash, new_lmPwdHash.hash, 16) != 0) {
1911 status = NT_STATUS_WRONG_PASSWORD;
1916 if (!pdb_set_nt_passwd(pwd, new_ntPwdHash.hash, PDB_CHANGED) ||
1917 !pdb_set_lanman_passwd(pwd, new_lmPwdHash.hash, PDB_CHANGED)) {
1918 status = NT_STATUS_ACCESS_DENIED;
1922 status = pdb_update_sam_account(pwd);
1929 /*******************************************************************
1930 _samr_ChangePasswordUser2
1931 ********************************************************************/
1933 NTSTATUS _samr_ChangePasswordUser2(struct pipes_struct *p,
1934 struct samr_ChangePasswordUser2 *r)
1937 char *user_name = NULL;
1940 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1942 if (!r->in.account->string) {
1943 return NT_STATUS_INVALID_PARAMETER;
1945 fstrcpy(wks, r->in.server->string);
1947 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1950 * Pass the user through the NT -> unix user mapping
1954 (void)map_username(talloc_tos(), r->in.account->string, &user_name);
1956 return NT_STATUS_NO_MEMORY;
1960 * UNIX username case mangling not required, pass_oem_change
1961 * is case insensitive.
1964 status = pass_oem_change(user_name,
1966 r->in.lm_password->data,
1967 r->in.lm_verifier->hash,
1968 r->in.nt_password->data,
1969 r->in.nt_verifier->hash,
1972 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1974 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1975 return NT_STATUS_WRONG_PASSWORD;
1981 /****************************************************************
1982 _samr_OemChangePasswordUser2
1983 ****************************************************************/
1985 NTSTATUS _samr_OemChangePasswordUser2(struct pipes_struct *p,
1986 struct samr_OemChangePasswordUser2 *r)
1989 char *user_name = NULL;
1990 const char *wks = NULL;
1992 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
1994 if (!r->in.account->string) {
1995 return NT_STATUS_INVALID_PARAMETER;
1997 if (r->in.server && r->in.server->string) {
1998 wks = r->in.server->string;
2001 DEBUG(5,("_samr_OemChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
2004 * Pass the user through the NT -> unix user mapping
2008 (void)map_username(talloc_tos(), r->in.account->string, &user_name);
2010 return NT_STATUS_NO_MEMORY;
2014 * UNIX username case mangling not required, pass_oem_change
2015 * is case insensitive.
2018 if (!r->in.hash || !r->in.password) {
2019 return NT_STATUS_INVALID_PARAMETER;
2022 status = pass_oem_change(user_name,
2024 r->in.password->data,
2030 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2031 return NT_STATUS_WRONG_PASSWORD;
2034 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
2039 /*******************************************************************
2040 _samr_ChangePasswordUser3
2041 ********************************************************************/
2043 NTSTATUS _samr_ChangePasswordUser3(struct pipes_struct *p,
2044 struct samr_ChangePasswordUser3 *r)
2047 char *user_name = NULL;
2048 const char *wks = NULL;
2049 enum samPwdChangeReason reject_reason;
2050 struct samr_DomInfo1 *dominfo = NULL;
2051 struct userPwdChangeFailureInformation *reject = NULL;
2054 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2056 if (!r->in.account->string) {
2057 return NT_STATUS_INVALID_PARAMETER;
2059 if (r->in.server && r->in.server->string) {
2060 wks = r->in.server->string;
2063 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
2066 * Pass the user through the NT -> unix user mapping
2070 (void)map_username(talloc_tos(), r->in.account->string, &user_name);
2072 return NT_STATUS_NO_MEMORY;
2076 * UNIX username case mangling not required, pass_oem_change
2077 * is case insensitive.
2080 status = pass_oem_change(user_name,
2082 r->in.lm_password->data,
2083 r->in.lm_verifier->hash,
2084 r->in.nt_password->data,
2085 r->in.nt_verifier->hash,
2087 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
2088 return NT_STATUS_WRONG_PASSWORD;
2091 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
2092 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
2094 time_t u_expire, u_min_age;
2095 uint32 account_policy_temp;
2097 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
2099 return NT_STATUS_NO_MEMORY;
2102 reject = TALLOC_ZERO_P(p->mem_ctx,
2103 struct userPwdChangeFailureInformation);
2105 return NT_STATUS_NO_MEMORY;
2112 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN, &tmp);
2113 dominfo->min_password_length = tmp;
2115 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &tmp);
2116 dominfo->password_history_length = tmp;
2118 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
2119 &dominfo->password_properties);
2121 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &account_policy_temp);
2122 u_expire = account_policy_temp;
2124 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &account_policy_temp);
2125 u_min_age = account_policy_temp;
2131 unix_to_nt_time_abs((NTTIME *)&dominfo->max_password_age, u_expire);
2132 unix_to_nt_time_abs((NTTIME *)&dominfo->min_password_age, u_min_age);
2134 if (lp_check_password_script() && *lp_check_password_script()) {
2135 dominfo->password_properties |= DOMAIN_PASSWORD_COMPLEX;
2138 reject->extendedFailureReason = reject_reason;
2140 *r->out.dominfo = dominfo;
2141 *r->out.reject = reject;
2144 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
2149 /*******************************************************************
2150 makes a SAMR_R_LOOKUP_RIDS structure.
2151 ********************************************************************/
2153 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
2155 struct lsa_String **lsa_name_array_p)
2157 struct lsa_String *lsa_name_array = NULL;
2160 *lsa_name_array_p = NULL;
2162 if (num_names != 0) {
2163 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
2164 if (!lsa_name_array) {
2169 for (i = 0; i < num_names; i++) {
2170 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
2171 init_lsa_String(&lsa_name_array[i], names[i]);
2174 *lsa_name_array_p = lsa_name_array;
2179 /*******************************************************************
2181 ********************************************************************/
2183 NTSTATUS _samr_LookupRids(struct pipes_struct *p,
2184 struct samr_LookupRids *r)
2186 struct samr_domain_info *dinfo;
2189 enum lsa_SidType *attrs = NULL;
2190 uint32 *wire_attrs = NULL;
2191 int num_rids = (int)r->in.num_rids;
2193 struct lsa_Strings names_array;
2194 struct samr_Ids types_array;
2195 struct lsa_String *lsa_names = NULL;
2197 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2199 dinfo = policy_handle_find(p, r->in.domain_handle,
2200 0 /* Don't know the acc_bits yet */, NULL,
2201 struct samr_domain_info, &status);
2202 if (!NT_STATUS_IS_OK(status)) {
2206 if (num_rids > 1000) {
2207 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2208 "to samba4 idl this is not possible\n", num_rids));
2209 return NT_STATUS_UNSUCCESSFUL;
2213 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
2214 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
2215 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
2217 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2218 return NT_STATUS_NO_MEMORY;
2225 become_root(); /* lookup_sid can require root privs */
2226 status = pdb_lookup_rids(&dinfo->sid, num_rids, r->in.rids,
2230 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2231 status = NT_STATUS_OK;
2234 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2236 return NT_STATUS_NO_MEMORY;
2239 /* Convert from enum lsa_SidType to uint32 for wire format. */
2240 for (i = 0; i < num_rids; i++) {
2241 wire_attrs[i] = (uint32)attrs[i];
2244 names_array.count = num_rids;
2245 names_array.names = lsa_names;
2247 types_array.count = num_rids;
2248 types_array.ids = wire_attrs;
2250 *r->out.names = names_array;
2251 *r->out.types = types_array;
2253 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2258 /*******************************************************************
2260 ********************************************************************/
2262 NTSTATUS _samr_OpenUser(struct pipes_struct *p,
2263 struct samr_OpenUser *r)
2265 struct samu *sampass=NULL;
2267 struct samr_domain_info *dinfo;
2268 struct samr_user_info *uinfo;
2269 struct security_descriptor *psd = NULL;
2271 uint32 des_access = r->in.access_mask;
2272 uint32_t extra_access = 0;
2277 /* These two privileges, if != SEC_PRIV_INVALID, indicate
2278 * privileges that the user must have to complete this
2279 * operation in defience of the fixed ACL */
2280 enum sec_privilege needed_priv_1, needed_priv_2;
2283 dinfo = policy_handle_find(p, r->in.domain_handle,
2284 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
2285 struct samr_domain_info, &status);
2286 if (!NT_STATUS_IS_OK(status)) {
2290 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2291 return NT_STATUS_NO_MEMORY;
2294 /* append the user's RID to it */
2296 if (!sid_compose(&sid, &dinfo->sid, r->in.rid))
2297 return NT_STATUS_NO_SUCH_USER;
2299 /* check if access can be granted as requested by client. */
2300 map_max_allowed_access(p->session_info->security_token,
2301 &p->session_info->utok,
2304 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2305 se_map_generic(&des_access, &usr_generic_mapping);
2308 * Get the sampass first as we need to check privileges
2309 * based on what kind of user object this is.
2310 * But don't reveal info too early if it didn't exist.
2314 ret=pdb_getsampwsid(sampass, &sid);
2317 needed_priv_1 = SEC_PRIV_INVALID;
2318 needed_priv_2 = SEC_PRIV_INVALID;
2320 * We do the override access checks on *open*, not at
2324 uint32_t acb_info = pdb_get_acct_ctrl(sampass);
2326 if (acb_info & ACB_WSTRUST) {
2328 * SeMachineAccount is needed to add
2329 * GENERIC_RIGHTS_USER_WRITE to a machine
2332 needed_priv_1 = SEC_PRIV_MACHINE_ACCOUNT;
2334 if (acb_info & ACB_NORMAL) {
2336 * SeAddUsers is needed to add
2337 * GENERIC_RIGHTS_USER_WRITE to a normal
2340 needed_priv_1 = SEC_PRIV_ADD_USERS;
2343 * Cheat - we have not set a specific privilege for
2344 * server (BDC) or domain trust account, so allow
2345 * GENERIC_RIGHTS_USER_WRITE if pipe user is in
2346 * DOMAIN_RID_ADMINS.
2348 if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
2349 if (lp_enable_privileges() && nt_token_check_domain_rid(p->session_info->security_token,
2350 DOMAIN_RID_ADMINS)) {
2351 des_access &= ~GENERIC_RIGHTS_USER_WRITE;
2352 extra_access = GENERIC_RIGHTS_USER_WRITE;
2353 DEBUG(4,("_samr_OpenUser: Allowing "
2354 "GENERIC_RIGHTS_USER_WRITE for "
2360 TALLOC_FREE(sampass);
2362 nt_status = access_check_object(psd, p->session_info->security_token,
2363 needed_priv_1, needed_priv_2,
2364 GENERIC_RIGHTS_USER_WRITE, des_access,
2365 &acc_granted, "_samr_OpenUser");
2367 if ( !NT_STATUS_IS_OK(nt_status) )
2370 /* check that the SID exists in our domain. */
2372 return NT_STATUS_NO_SUCH_USER;
2375 /* If we did the rid admins hack above, allow access. */
2376 acc_granted |= extra_access;
2378 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
2379 struct samr_user_info, &nt_status);
2380 if (!NT_STATUS_IS_OK(nt_status)) {
2385 return NT_STATUS_OK;
2388 /*************************************************************************
2389 *************************************************************************/
2391 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2393 struct lsa_BinaryString **_r)
2395 struct lsa_BinaryString *r;
2398 return NT_STATUS_INVALID_PARAMETER;
2401 r = TALLOC_ZERO_P(mem_ctx, struct lsa_BinaryString);
2403 return NT_STATUS_NO_MEMORY;
2406 r->array = TALLOC_ZERO_ARRAY(mem_ctx, uint16_t, blob->length/2);
2408 return NT_STATUS_NO_MEMORY;
2410 memcpy(r->array, blob->data, blob->length);
2411 r->size = blob->length;
2412 r->length = blob->length;
2415 return NT_STATUS_NO_MEMORY;
2420 return NT_STATUS_OK;
2423 /*************************************************************************
2424 *************************************************************************/
2426 static struct samr_LogonHours get_logon_hours_from_pdb(TALLOC_CTX *mem_ctx,
2429 struct samr_LogonHours hours;
2430 const int units_per_week = 168;
2433 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
2438 hours.units_per_week = units_per_week;
2439 memset(hours.bits, 0xFF, units_per_week);
2441 if (pdb_get_hours(pw)) {
2442 memcpy(hours.bits, pdb_get_hours(pw),
2443 MIN(pdb_get_hours_len(pw), units_per_week));
2449 /*************************************************************************
2451 *************************************************************************/
2453 static NTSTATUS get_user_info_1(TALLOC_CTX *mem_ctx,
2454 struct samr_UserInfo1 *r,
2456 struct dom_sid *domain_sid)
2458 const struct dom_sid *sid_group;
2459 uint32_t primary_gid;
2462 sid_group = pdb_get_group_sid(pw);
2465 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2466 DEBUG(0, ("get_user_info_1: User %s has Primary Group SID %s, \n"
2467 "which conflicts with the domain sid %s. Failing operation.\n",
2468 pdb_get_username(pw), sid_string_dbg(sid_group),
2469 sid_string_dbg(domain_sid)));
2470 return NT_STATUS_UNSUCCESSFUL;
2473 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2474 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2475 r->primary_gid = primary_gid;
2476 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2477 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2479 return NT_STATUS_OK;
2482 /*************************************************************************
2484 *************************************************************************/
2486 static NTSTATUS get_user_info_2(TALLOC_CTX *mem_ctx,
2487 struct samr_UserInfo2 *r,
2490 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2491 r->reserved.string = NULL;
2492 r->country_code = pdb_get_country_code(pw);
2493 r->code_page = pdb_get_code_page(pw);
2495 return NT_STATUS_OK;
2498 /*************************************************************************
2500 *************************************************************************/
2502 static NTSTATUS get_user_info_3(TALLOC_CTX *mem_ctx,
2503 struct samr_UserInfo3 *r,
2505 struct dom_sid *domain_sid)
2507 const struct dom_sid *sid_user, *sid_group;
2508 uint32_t rid, primary_gid;
2510 sid_user = pdb_get_user_sid(pw);
2512 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2513 DEBUG(0, ("get_user_info_3: User %s has SID %s, \nwhich conflicts with "
2514 "the domain sid %s. Failing operation.\n",
2515 pdb_get_username(pw), sid_string_dbg(sid_user),
2516 sid_string_dbg(domain_sid)));
2517 return NT_STATUS_UNSUCCESSFUL;
2521 sid_group = pdb_get_group_sid(pw);
2524 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2525 DEBUG(0, ("get_user_info_3: User %s has Primary Group SID %s, \n"
2526 "which conflicts with the domain sid %s. Failing operation.\n",
2527 pdb_get_username(pw), sid_string_dbg(sid_group),
2528 sid_string_dbg(domain_sid)));
2529 return NT_STATUS_UNSUCCESSFUL;
2532 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2533 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2534 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2535 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2536 unix_to_nt_time(&r->force_password_change, pdb_get_pass_must_change_time(pw));
2538 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2539 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2540 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2541 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2542 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2543 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2544 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2546 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2548 r->primary_gid = primary_gid;
2549 r->acct_flags = pdb_get_acct_ctrl(pw);
2550 r->bad_password_count = pdb_get_bad_password_count(pw);
2551 r->logon_count = pdb_get_logon_count(pw);
2553 return NT_STATUS_OK;
2556 /*************************************************************************
2558 *************************************************************************/
2560 static NTSTATUS get_user_info_4(TALLOC_CTX *mem_ctx,
2561 struct samr_UserInfo4 *r,
2564 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2566 return NT_STATUS_OK;
2569 /*************************************************************************
2571 *************************************************************************/
2573 static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
2574 struct samr_UserInfo5 *r,
2576 struct dom_sid *domain_sid)
2578 const struct dom_sid *sid_user, *sid_group;
2579 uint32_t rid, primary_gid;
2581 sid_user = pdb_get_user_sid(pw);
2583 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2584 DEBUG(0, ("get_user_info_5: User %s has SID %s, \nwhich conflicts with "
2585 "the domain sid %s. Failing operation.\n",
2586 pdb_get_username(pw), sid_string_dbg(sid_user),
2587 sid_string_dbg(domain_sid)));
2588 return NT_STATUS_UNSUCCESSFUL;
2592 sid_group = pdb_get_group_sid(pw);
2595 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2596 DEBUG(0, ("get_user_info_5: User %s has Primary Group SID %s, \n"
2597 "which conflicts with the domain sid %s. Failing operation.\n",
2598 pdb_get_username(pw), sid_string_dbg(sid_group),
2599 sid_string_dbg(domain_sid)));
2600 return NT_STATUS_UNSUCCESSFUL;
2603 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2604 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2605 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2606 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2608 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2609 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2610 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2611 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2612 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2613 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2614 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2615 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2617 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2619 r->primary_gid = primary_gid;
2620 r->acct_flags = pdb_get_acct_ctrl(pw);
2621 r->bad_password_count = pdb_get_bad_password_count(pw);
2622 r->logon_count = pdb_get_logon_count(pw);
2624 return NT_STATUS_OK;
2627 /*************************************************************************
2629 *************************************************************************/
2631 static NTSTATUS get_user_info_6(TALLOC_CTX *mem_ctx,
2632 struct samr_UserInfo6 *r,
2635 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2636 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2638 return NT_STATUS_OK;
2641 /*************************************************************************
2642 get_user_info_7. Safe. Only gives out account_name.
2643 *************************************************************************/
2645 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2646 struct samr_UserInfo7 *r,
2647 struct samu *smbpass)
2649 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2650 if (!r->account_name.string) {
2651 return NT_STATUS_NO_MEMORY;
2654 return NT_STATUS_OK;
2657 /*************************************************************************
2659 *************************************************************************/
2661 static NTSTATUS get_user_info_8(TALLOC_CTX *mem_ctx,
2662 struct samr_UserInfo8 *r,
2665 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2667 return NT_STATUS_OK;
2670 /*************************************************************************
2671 get_user_info_9. Only gives out primary group SID.
2672 *************************************************************************/
2674 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2675 struct samr_UserInfo9 *r,
2676 struct samu *smbpass)
2678 r->primary_gid = pdb_get_group_rid(smbpass);
2680 return NT_STATUS_OK;
2683 /*************************************************************************
2685 *************************************************************************/
2687 static NTSTATUS get_user_info_10(TALLOC_CTX *mem_ctx,
2688 struct samr_UserInfo10 *r,
2691 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2692 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2694 return NT_STATUS_OK;
2697 /*************************************************************************
2699 *************************************************************************/
2701 static NTSTATUS get_user_info_11(TALLOC_CTX *mem_ctx,
2702 struct samr_UserInfo11 *r,
2705 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2707 return NT_STATUS_OK;
2710 /*************************************************************************
2712 *************************************************************************/
2714 static NTSTATUS get_user_info_12(TALLOC_CTX *mem_ctx,
2715 struct samr_UserInfo12 *r,
2718 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2720 return NT_STATUS_OK;
2723 /*************************************************************************
2725 *************************************************************************/
2727 static NTSTATUS get_user_info_13(TALLOC_CTX *mem_ctx,
2728 struct samr_UserInfo13 *r,
2731 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2733 return NT_STATUS_OK;
2736 /*************************************************************************
2738 *************************************************************************/
2740 static NTSTATUS get_user_info_14(TALLOC_CTX *mem_ctx,
2741 struct samr_UserInfo14 *r,
2744 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2746 return NT_STATUS_OK;
2749 /*************************************************************************
2750 get_user_info_16. Safe. Only gives out acb bits.
2751 *************************************************************************/
2753 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2754 struct samr_UserInfo16 *r,
2755 struct samu *smbpass)
2757 r->acct_flags = pdb_get_acct_ctrl(smbpass);
2759 return NT_STATUS_OK;
2762 /*************************************************************************
2764 *************************************************************************/
2766 static NTSTATUS get_user_info_17(TALLOC_CTX *mem_ctx,
2767 struct samr_UserInfo17 *r,
2770 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2772 return NT_STATUS_OK;
2775 /*************************************************************************
2776 get_user_info_18. OK - this is the killer as it gives out password info.
2777 Ensure that this is only allowed on an encrypted connection with a root
2779 *************************************************************************/
2781 static NTSTATUS get_user_info_18(struct pipes_struct *p,
2782 TALLOC_CTX *mem_ctx,
2783 struct samr_UserInfo18 *r,
2784 struct dom_sid *user_sid)
2786 struct samu *smbpass=NULL;
2788 const uint8_t *nt_pass = NULL;
2789 const uint8_t *lm_pass = NULL;
2793 if (p->session_info->system) {
2797 if ((p->auth.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) ||
2798 (p->auth.auth_type != DCERPC_AUTH_TYPE_KRB5) ||
2799 (p->auth.auth_type != DCERPC_AUTH_TYPE_SPNEGO)) {
2800 return NT_STATUS_ACCESS_DENIED;
2803 if (p->auth.auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
2804 return NT_STATUS_ACCESS_DENIED;
2809 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2812 if ( !(smbpass = samu_new( mem_ctx )) ) {
2813 return NT_STATUS_NO_MEMORY;
2816 ret = pdb_getsampwsid(smbpass, user_sid);
2819 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2820 TALLOC_FREE(smbpass);
2821 return (geteuid() == sec_initial_uid()) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2824 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2826 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2827 TALLOC_FREE(smbpass);
2828 return NT_STATUS_ACCOUNT_DISABLED;
2831 lm_pass = pdb_get_lanman_passwd(smbpass);
2832 if (lm_pass != NULL) {
2833 memcpy(r->lm_pwd.hash, lm_pass, 16);
2834 r->lm_pwd_active = true;
2837 nt_pass = pdb_get_nt_passwd(smbpass);
2838 if (nt_pass != NULL) {
2839 memcpy(r->nt_pwd.hash, nt_pass, 16);
2840 r->nt_pwd_active = true;
2842 r->password_expired = 0; /* FIXME */
2844 TALLOC_FREE(smbpass);
2846 return NT_STATUS_OK;
2849 /*************************************************************************
2851 *************************************************************************/
2853 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2854 struct samr_UserInfo20 *r,
2855 struct samu *sampass)
2857 const char *munged_dial = NULL;
2860 struct lsa_BinaryString *parameters = NULL;
2864 munged_dial = pdb_get_munged_dial(sampass);
2866 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2867 munged_dial, (int)strlen(munged_dial)));
2870 blob = base64_decode_data_blob(munged_dial);
2872 blob = data_blob_string_const_null("");
2875 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2876 data_blob_free(&blob);
2877 if (!NT_STATUS_IS_OK(status)) {
2881 r->parameters = *parameters;
2883 return NT_STATUS_OK;
2887 /*************************************************************************
2889 *************************************************************************/
2891 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2892 struct samr_UserInfo21 *r,
2894 struct dom_sid *domain_sid,
2895 uint32_t acc_granted)
2898 const struct dom_sid *sid_user, *sid_group;
2899 uint32_t rid, primary_gid;
2900 NTTIME force_password_change;
2901 time_t must_change_time;
2902 struct lsa_BinaryString *parameters = NULL;
2903 const char *munged_dial = NULL;
2908 sid_user = pdb_get_user_sid(pw);
2910 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2911 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2912 "the domain sid %s. Failing operation.\n",
2913 pdb_get_username(pw), sid_string_dbg(sid_user),
2914 sid_string_dbg(domain_sid)));
2915 return NT_STATUS_UNSUCCESSFUL;
2919 sid_group = pdb_get_group_sid(pw);
2922 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2923 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2924 "which conflicts with the domain sid %s. Failing operation.\n",
2925 pdb_get_username(pw), sid_string_dbg(sid_group),
2926 sid_string_dbg(domain_sid)));
2927 return NT_STATUS_UNSUCCESSFUL;
2930 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2931 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2932 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2933 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2934 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2936 must_change_time = pdb_get_pass_must_change_time(pw);
2937 if (must_change_time == get_time_t_max()) {
2938 unix_to_nt_time_abs(&force_password_change, must_change_time);
2940 unix_to_nt_time(&force_password_change, must_change_time);
2943 munged_dial = pdb_get_munged_dial(pw);
2945 blob = base64_decode_data_blob(munged_dial);
2947 blob = data_blob_string_const_null("");
2950 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2951 data_blob_free(&blob);
2952 if (!NT_STATUS_IS_OK(status)) {
2956 r->force_password_change = force_password_change;
2958 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2959 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2960 r->home_directory.string = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2961 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2962 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2963 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2964 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2965 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2966 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2968 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2969 r->parameters = *parameters;
2971 r->primary_gid = primary_gid;
2972 r->acct_flags = pdb_get_acct_ctrl(pw);
2973 r->bad_password_count = pdb_get_bad_password_count(pw);
2974 r->logon_count = pdb_get_logon_count(pw);
2975 r->fields_present = pdb_build_fields_present(pw);
2976 r->password_expired = (pdb_get_pass_must_change_time(pw) == 0) ?
2977 PASS_MUST_CHANGE_AT_NEXT_LOGON : 0;
2978 r->country_code = pdb_get_country_code(pw);
2979 r->code_page = pdb_get_code_page(pw);
2980 r->lm_password_set = 0;
2981 r->nt_password_set = 0;
2986 Look at a user on a real NT4 PDC with usrmgr, press
2987 'ok'. Then you will see that fields_present is set to
2988 0x08f827fa. Look at the user immediately after that again,
2989 and you will see that 0x00fffff is returned. This solves
2990 the problem that you get access denied after having looked
2998 return NT_STATUS_OK;
3001 /*******************************************************************
3003 ********************************************************************/
3005 NTSTATUS _samr_QueryUserInfo(struct pipes_struct *p,
3006 struct samr_QueryUserInfo *r)
3009 union samr_UserInfo *user_info = NULL;
3010 struct samr_user_info *uinfo;
3011 struct dom_sid domain_sid;
3014 struct samu *pwd = NULL;
3015 uint32_t acc_required, acc_granted;
3017 switch (r->in.level) {
3018 case 1: /* UserGeneralInformation */
3019 /* USER_READ_GENERAL */
3020 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC;
3022 case 2: /* UserPreferencesInformation */
3023 /* USER_READ_PREFERENCES | USER_READ_GENERAL */
3024 acc_required = SAMR_USER_ACCESS_GET_LOCALE |
3025 SAMR_USER_ACCESS_GET_NAME_ETC;
3027 case 3: /* UserLogonInformation */
3028 /* USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT */
3029 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC |
3030 SAMR_USER_ACCESS_GET_LOCALE |
3031 SAMR_USER_ACCESS_GET_LOGONINFO |
3032 SAMR_USER_ACCESS_GET_ATTRIBUTES;
3034 case 4: /* UserLogonHoursInformation */
3035 /* USER_READ_LOGON */
3036 acc_required = SAMR_USER_ACCESS_GET_LOGONINFO;
3038 case 5: /* UserAccountInformation */
3039 /* USER_READ_GENERAL | USER_READ_PREFERENCES | USER_READ_LOGON | USER_READ_ACCOUNT */
3040 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC |
3041 SAMR_USER_ACCESS_GET_LOCALE |
3042 SAMR_USER_ACCESS_GET_LOGONINFO |
3043 SAMR_USER_ACCESS_GET_ATTRIBUTES;
3045 case 6: /* UserNameInformation */
3046 case 7: /* UserAccountNameInformation */
3047 case 8: /* UserFullNameInformation */
3048 case 9: /* UserPrimaryGroupInformation */
3049 case 13: /* UserAdminCommentInformation */
3050 /* USER_READ_GENERAL */
3051 acc_required = SAMR_USER_ACCESS_GET_NAME_ETC;
3053 case 10: /* UserHomeInformation */
3054 case 11: /* UserScriptInformation */
3055 case 12: /* UserProfileInformation */
3056 case 14: /* UserWorkStationsInformation */
3057 /* USER_READ_LOGON */
3058 acc_required = SAMR_USER_ACCESS_GET_LOGONINFO;
3060 case 16: /* UserControlInformation */
3061 case 17: /* UserExpiresInformation */
3062 case 20: /* UserParametersInformation */
3063 /* USER_READ_ACCOUNT */
3064 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
3066 case 21: /* UserAllInformation */
3068 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
3070 case 18: /* UserInternal1Information */
3072 acc_required = SAMR_USER_ACCESS_GET_ATTRIBUTES;
3074 case 23: /* UserInternal4Information */
3075 case 24: /* UserInternal4InformationNew */
3076 case 25: /* UserInternal4InformationNew */
3077 case 26: /* UserInternal5InformationNew */
3079 return NT_STATUS_INVALID_INFO_CLASS;
3083 uinfo = policy_handle_find(p, r->in.user_handle,
3084 acc_required, &acc_granted,
3085 struct samr_user_info, &status);
3086 if (!NT_STATUS_IS_OK(status)) {
3090 domain_sid = uinfo->sid;
3092 sid_split_rid(&domain_sid, &rid);
3094 if (!sid_check_is_in_our_domain(&uinfo->sid))
3095 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3097 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
3098 sid_string_dbg(&uinfo->sid)));
3100 user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
3102 return NT_STATUS_NO_MEMORY;
3105 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
3107 if (!(pwd = samu_new(p->mem_ctx))) {
3108 return NT_STATUS_NO_MEMORY;
3112 ret = pdb_getsampwsid(pwd, &uinfo->sid);
3116 DEBUG(4,("User %s not found\n", sid_string_dbg(&uinfo->sid)));
3118 return NT_STATUS_NO_SUCH_USER;
3121 DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
3123 samr_clear_sam_passwd(pwd);
3125 switch (r->in.level) {
3127 status = get_user_info_1(p->mem_ctx, &user_info->info1, pwd, &domain_sid);
3130 status = get_user_info_2(p->mem_ctx, &user_info->info2, pwd);
3133 status = get_user_info_3(p->mem_ctx, &user_info->info3, pwd, &domain_sid);
3136 status = get_user_info_4(p->mem_ctx, &user_info->info4, pwd);
3139 status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
3142 status = get_user_info_6(p->mem_ctx, &user_info->info6, pwd);
3145 status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
3148 status = get_user_info_8(p->mem_ctx, &user_info->info8, pwd);
3151 status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
3154 status = get_user_info_10(p->mem_ctx, &user_info->info10, pwd);
3157 status = get_user_info_11(p->mem_ctx, &user_info->info11, pwd);
3160 status = get_user_info_12(p->mem_ctx, &user_info->info12, pwd);
3163 status = get_user_info_13(p->mem_ctx, &user_info->info13, pwd);
3166 status = get_user_info_14(p->mem_ctx, &user_info->info14, pwd);
3169 status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
3172 status = get_user_info_17(p->mem_ctx, &user_info->info17, pwd);
3175 /* level 18 is special */
3176 status = get_user_info_18(p, p->mem_ctx, &user_info->info18,
3180 status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
3183 status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid, acc_granted);
3186 status = NT_STATUS_INVALID_INFO_CLASS;
3190 if (!NT_STATUS_IS_OK(status)) {
3194 *r->out.info = user_info;
3199 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
3204 /****************************************************************
3205 ****************************************************************/
3207 NTSTATUS _samr_QueryUserInfo2(struct pipes_struct *p,
3208 struct samr_QueryUserInfo2 *r)
3210 struct samr_QueryUserInfo u;
3212 u.in.user_handle = r->in.user_handle;
3213 u.in.level = r->in.level;
3214 u.out.info = r->out.info;
3216 return _samr_QueryUserInfo(p, &u);
3219 /*******************************************************************
3220 _samr_GetGroupsForUser
3221 ********************************************************************/
3223 NTSTATUS _samr_GetGroupsForUser(struct pipes_struct *p,
3224 struct samr_GetGroupsForUser *r)
3226 struct samr_user_info *uinfo;
3227 struct samu *sam_pass=NULL;
3228 struct dom_sid *sids;
3229 struct samr_RidWithAttribute dom_gid;
3230 struct samr_RidWithAttribute *gids = NULL;
3231 uint32 primary_group_rid;
3232 uint32_t num_groups = 0;
3234 uint32_t i, num_gids;
3237 bool success = False;
3239 struct samr_RidWithAttributeArray *rids = NULL;
3242 * from the SID in the request:
3243 * we should send back the list of DOMAIN GROUPS
3244 * the user is a member of
3246 * and only the DOMAIN GROUPS
3247 * no ALIASES !!! neither aliases of the domain
3248 * nor aliases of the builtin SID
3253 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
3255 uinfo = policy_handle_find(p, r->in.user_handle,
3256 SAMR_USER_ACCESS_GET_GROUPS, NULL,
3257 struct samr_user_info, &result);
3258 if (!NT_STATUS_IS_OK(result)) {
3262 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
3264 return NT_STATUS_NO_MEMORY;
3267 if (!sid_check_is_in_our_domain(&uinfo->sid))
3268 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3270 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
3271 return NT_STATUS_NO_MEMORY;
3275 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
3279 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
3280 sid_string_dbg(&uinfo->sid)));
3281 return NT_STATUS_NO_SUCH_USER;
3286 /* make both calls inside the root block */
3288 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
3289 &sids, &unix_gids, &num_groups);
3290 if ( NT_STATUS_IS_OK(result) ) {
3291 success = sid_peek_check_rid(get_global_sam_sid(),
3292 pdb_get_group_sid(sam_pass),
3293 &primary_group_rid);
3297 if (!NT_STATUS_IS_OK(result)) {
3298 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
3299 sid_string_dbg(&uinfo->sid)));
3304 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
3305 sid_string_dbg(pdb_get_group_sid(sam_pass)),
3306 pdb_get_username(sam_pass)));
3307 TALLOC_FREE(sam_pass);
3308 return NT_STATUS_INTERNAL_DB_CORRUPTION;
3314 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
3316 dom_gid.rid = primary_group_rid;
3317 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
3319 for (i=0; i<num_groups; i++) {
3321 if (!sid_peek_check_rid(get_global_sam_sid(),
3322 &(sids[i]), &dom_gid.rid)) {
3323 DEBUG(10, ("Found sid %s not in our domain\n",
3324 sid_string_dbg(&sids[i])));
3328 if (dom_gid.rid == primary_group_rid) {
3329 /* We added the primary group directly from the
3330 * sam_account. The other SIDs are unique from
3331 * enum_group_memberships */
3335 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
3338 rids->count = num_gids;
3341 *r->out.rids = rids;
3343 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
3348 /*******************************************************************
3349 ********************************************************************/
3351 static uint32_t samr_get_server_role(void)
3353 uint32_t role = ROLE_DOMAIN_PDC;
3355 if (lp_server_role() == ROLE_DOMAIN_BDC) {
3356 role = ROLE_DOMAIN_BDC;
3362 /*******************************************************************
3363 ********************************************************************/
3365 static NTSTATUS query_dom_info_1(TALLOC_CTX *mem_ctx,
3366 struct samr_DomInfo1 *r)
3368 uint32_t account_policy_temp;
3369 time_t u_expire, u_min_age;
3375 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN, &account_policy_temp);
3376 r->min_password_length = account_policy_temp;
3378 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &account_policy_temp);
3379 r->password_history_length = account_policy_temp;
3381 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
3382 &r->password_properties);
3384 pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &account_policy_temp);
3385 u_expire = account_policy_temp;
3387 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &account_policy_temp);
3388 u_min_age = account_policy_temp;
3394 unix_to_nt_time_abs((NTTIME *)&r->max_password_age, u_expire);
3395 unix_to_nt_time_abs((NTTIME *)&r->min_password_age, u_min_age);
3397 if (lp_check_password_script() && *lp_check_password_script()) {
3398 r->password_properties |= DOMAIN_PASSWORD_COMPLEX;
3401 return NT_STATUS_OK;
3404 /*******************************************************************
3405 ********************************************************************/
3407 static NTSTATUS query_dom_info_2(TALLOC_CTX *mem_ctx,
3408 struct samr_DomGeneralInformation *r,
3409 struct samr_domain_info *dinfo)
3418 r->num_users = count_sam_users(dinfo->disp_info, ACB_NORMAL);
3419 r->num_groups = count_sam_groups(dinfo->disp_info);
3420 r->num_aliases = count_sam_aliases(dinfo->disp_info);
3422 pdb_get_account_policy(PDB_POLICY_TIME_TO_LOGOUT, &u_logout);
3424 unix_to_nt_time_abs(&r->force_logoff_time, u_logout);
3426 if (!pdb_get_seq_num(&seq_num)) {
3427 seq_num = time(NULL);
3434 r->oem_information.string = lp_serverstring();
3435 r->domain_name.string = lp_workgroup();
3436 r->primary.string = global_myname();
3437 r->sequence_num = seq_num;
3438 r->domain_server_state = DOMAIN_SERVER_ENABLED;
3439 r->role = (enum samr_Role) samr_get_server_role();
3442 return NT_STATUS_OK;
3445 /*******************************************************************
3446 ********************************************************************/
3448 static NTSTATUS query_dom_info_3(TALLOC_CTX *mem_ctx,
3449 struct samr_DomInfo3 *r)
3459 pdb_get_account_policy(PDB_POLICY_TIME_TO_LOGOUT, &ul);
3460 u_logout = (time_t)ul;
3467 unix_to_nt_time_abs(&r->force_logoff_time, u_logout);
3469 return NT_STATUS_OK;
3472 /*******************************************************************
3473 ********************************************************************/
3475 static NTSTATUS query_dom_info_4(TALLOC_CTX *mem_ctx,
3476 struct samr_DomOEMInformation *r)
3478 r->oem_information.string = lp_serverstring();
3480 return NT_STATUS_OK;
3483 /*******************************************************************
3484 ********************************************************************/
3486 static NTSTATUS query_dom_info_5(TALLOC_CTX *mem_ctx,
3487 struct samr_DomInfo5 *r)
3489 r->domain_name.string = get_global_sam_name();
3491 return NT_STATUS_OK;
3494 /*******************************************************************
3495 ********************************************************************/
3497 static NTSTATUS query_dom_info_6(TALLOC_CTX *mem_ctx,
3498 struct samr_DomInfo6 *r)
3500 /* NT returns its own name when a PDC. win2k and later
3501 * only the name of the PDC if itself is a BDC (samba4
3503 r->primary.string = global_myname();
3505 return NT_STATUS_OK;
3508 /*******************************************************************
3509 ********************************************************************/
3511 static NTSTATUS query_dom_info_7(TALLOC_CTX *mem_ctx,
3512 struct samr_DomInfo7 *r)
3514 r->role = (enum samr_Role) samr_get_server_role();
3516 return NT_STATUS_OK;
3519 /*******************************************************************
3520 ********************************************************************/
3522 static NTSTATUS query_dom_info_8(TALLOC_CTX *mem_ctx,
3523 struct samr_DomInfo8 *r)
3531 if (!pdb_get_seq_num(&seq_num)) {
3532 seq_num = time(NULL);
3539 r->sequence_num = seq_num;
3540 r->domain_create_time = 0;
3542 return NT_STATUS_OK;
3545 /*******************************************************************
3546 ********************************************************************/
3548 static NTSTATUS query_dom_info_9(TALLOC_CTX *mem_ctx,
3549 struct samr_DomInfo9 *r)
3551 r->domain_server_state = DOMAIN_SERVER_ENABLED;
3553 return NT_STATUS_OK;
3556 /*******************************************************************
3557 ********************************************************************/
3559 static NTSTATUS query_dom_info_11(TALLOC_CTX *mem_ctx,
3560 struct samr_DomGeneralInformation2 *r,
3561 struct samr_domain_info *dinfo)
3564 uint32_t account_policy_temp;
3565 time_t u_lock_duration, u_reset_time;
3567 status = query_dom_info_2(mem_ctx, &r->general, dinfo);
3568 if (!NT_STATUS_IS_OK(status)) {
3576 pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3577 u_lock_duration = account_policy_temp;
3578 if (u_lock_duration != -1) {
3579 u_lock_duration *= 60;
3582 pdb_get_account_policy(PDB_POLICY_RESET_COUNT_TIME, &account_policy_temp);
3583 u_reset_time = account_policy_temp * 60;
3585 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3586 r->lockout_threshold = account_policy_temp;
3592 unix_to_nt_time_abs(&r->lockout_duration, u_lock_duration);
3593 unix_to_nt_time_abs(&r->lockout_window, u_reset_time);
3595 return NT_STATUS_OK;
3598 /*******************************************************************
3599 ********************************************************************/
3601 static NTSTATUS query_dom_info_12(TALLOC_CTX *mem_ctx,
3602 struct samr_DomInfo12 *r)
3604 uint32_t account_policy_temp;
3605 time_t u_lock_duration, u_reset_time;
3611 pdb_get_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3612 u_lock_duration = account_policy_temp;
3613 if (u_lock_duration != -1) {
3614 u_lock_duration *= 60;
3617 pdb_get_account_policy(PDB_POLICY_RESET_COUNT_TIME, &account_policy_temp);
3618 u_reset_time = account_policy_temp * 60;
3620 pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
3621 r->lockout_threshold = account_policy_temp;
3627 unix_to_nt_time_abs(&r->lockout_duration, u_lock_duration);
3628 unix_to_nt_time_abs(&r->lockout_window, u_reset_time);
3630 return NT_STATUS_OK;
3633 /*******************************************************************
3634 ********************************************************************/
3636 static NTSTATUS query_dom_info_13(TALLOC_CTX *mem_ctx,
3637 struct samr_DomInfo13 *r)
3645 if (!pdb_get_seq_num(&seq_num)) {
3646 seq_num = time(NULL);
3653 r->sequence_num = seq_num;
3654 r->domain_create_time = 0;
3655 r->modified_count_at_last_promotion = 0;
3657 return NT_STATUS_OK;
3660 /*******************************************************************
3661 _samr_QueryDomainInfo
3662 ********************************************************************/
3664 NTSTATUS _samr_QueryDomainInfo(struct pipes_struct *p,
3665 struct samr_QueryDomainInfo *r)
3667 NTSTATUS status = NT_STATUS_OK;
3668 struct samr_domain_info *dinfo;
3669 union samr_DomainInfo *dom_info;
3671 uint32_t acc_required;
3673 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3675 switch (r->in.level) {
3676 case 1: /* DomainPasswordInformation */
3677 case 12: /* DomainLockoutInformation */
3678 /* DOMAIN_READ_PASSWORD_PARAMETERS */
3679 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1;
3681 case 11: /* DomainGeneralInformation2 */
3682 /* DOMAIN_READ_PASSWORD_PARAMETERS |
3683 * DOMAIN_READ_OTHER_PARAMETERS */
3684 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1 |
3685 SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2;
3687 case 2: /* DomainGeneralInformation */
3688 case 3: /* DomainLogoffInformation */
3689 case 4: /* DomainOemInformation */
3690 case 5: /* DomainReplicationInformation */
3691 case 6: /* DomainReplicationInformation */
3692 case 7: /* DomainServerRoleInformation */
3693 case 8: /* DomainModifiedInformation */
3694 case 9: /* DomainStateInformation */
3695 case 10: /* DomainUasInformation */
3696 case 13: /* DomainModifiedInformation2 */
3697 /* DOMAIN_READ_OTHER_PARAMETERS */
3698 acc_required = SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2;
3701 return NT_STATUS_INVALID_INFO_CLASS;
3704 dinfo = policy_handle_find(p, r->in.domain_handle,
3706 struct samr_domain_info, &status);
3707 if (!NT_STATUS_IS_OK(status)) {
3711 dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
3713 return NT_STATUS_NO_MEMORY;
3716 switch (r->in.level) {
3718 status = query_dom_info_1(p->mem_ctx, &dom_info->info1);
3721 status = query_dom_info_2(p->mem_ctx, &dom_info->general, dinfo);
3724 status = query_dom_info_3(p->mem_ctx, &dom_info->info3);
3727 status = query_dom_info_4(p->mem_ctx, &dom_info->oem);
3730 status = query_dom_info_5(p->mem_ctx, &dom_info->info5);
3733 status = query_dom_info_6(p->mem_ctx, &dom_info->info6);
3736 status = query_dom_info_7(p->mem_ctx, &dom_info->info7);
3739 status = query_dom_info_8(p->mem_ctx, &dom_info->info8);
3742 status = query_dom_info_9(p->mem_ctx, &dom_info->info9);
3745 status = query_dom_info_11(p->mem_ctx, &dom_info->general2, dinfo);
3748 status = query_dom_info_12(p->mem_ctx, &dom_info->info12);
3751 status = query_dom_info_13(p->mem_ctx, &dom_info->info13);
3754 return NT_STATUS_INVALID_INFO_CLASS;
3757 if (!NT_STATUS_IS_OK(status)) {
3761 *r->out.info = dom_info;
3763 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3768 /* W2k3 seems to use the same check for all 3 objects that can be created via
3769 * SAMR, if you try to create for example "Dialup" as an alias it says
3770 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3773 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3775 enum lsa_SidType type;
3778 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3781 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3782 * whether the name already exists */
3783 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3784 NULL, NULL, NULL, &type);
3788 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3789 return NT_STATUS_OK;
3792 DEBUG(5, ("trying to create %s, exists as %s\n",
3793 new_name, sid_type_lookup(type)));
3795 if (type == SID_NAME_DOM_GRP) {
3796 return NT_STATUS_GROUP_EXISTS;
3798 if (type == SID_NAME_ALIAS) {
3799 return NT_STATUS_ALIAS_EXISTS;
3802 /* Yes, the default is NT_STATUS_USER_EXISTS */
3803 return NT_STATUS_USER_EXISTS;
3806 /*******************************************************************
3808 ********************************************************************/
3810 NTSTATUS _samr_CreateUser2(struct pipes_struct *p,
3811 struct samr_CreateUser2 *r)
3813 const char *account = NULL;
3815 uint32_t acb_info = r->in.acct_flags;
3816 struct samr_domain_info *dinfo;
3817 struct samr_user_info *uinfo;
3820 struct security_descriptor *psd;
3822 /* check this, when giving away 'add computer to domain' privs */
3823 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3824 bool can_add_account = False;
3826 /* Which privilege is needed to override the ACL? */
3827 enum sec_privilege needed_priv = SEC_PRIV_INVALID;
3829 dinfo = policy_handle_find(p, r->in.domain_handle,
3830 SAMR_DOMAIN_ACCESS_CREATE_USER, NULL,
3831 struct samr_domain_info, &nt_status);
3832 if (!NT_STATUS_IS_OK(nt_status)) {
3836 if (sid_check_is_builtin(&dinfo->sid)) {
3837 DEBUG(5,("_samr_CreateUser2: Refusing user create in BUILTIN\n"));
3838 return NT_STATUS_ACCESS_DENIED;
3841 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3842 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3843 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3844 this parameter is not an account type */
3845 return NT_STATUS_INVALID_PARAMETER;
3848 account = r->in.account_name->string;
3849 if (account == NULL) {
3850 return NT_STATUS_NO_MEMORY;
3853 nt_status = can_create(p->mem_ctx, account);
3854 if (!NT_STATUS_IS_OK(nt_status)) {
3858 /* determine which user right we need to check based on the acb_info */
3860 if (geteuid() == sec_initial_uid()) {
3861 can_add_account = true;
3862 } else if (acb_info & ACB_WSTRUST) {
3863 needed_priv = SEC_PRIV_MACHINE_ACCOUNT;
3864 can_add_account = security_token_has_privilege(p->session_info->security_token, SEC_PRIV_MACHINE_ACCOUNT);
3865 } else if (acb_info & ACB_NORMAL &&
3866 (account[strlen(account)-1] != '$')) {
3867 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3868 account for domain trusts and changes the ACB flags later */
3869 needed_priv = SEC_PRIV_ADD_USERS;
3870 can_add_account = security_token_has_privilege(p->session_info->security_token, SEC_PRIV_ADD_USERS);
3871 } else if (lp_enable_privileges()) {
3872 /* implicit assumption of a BDC or domain trust account here
3873 * (we already check the flags earlier) */
3874 /* only Domain Admins can add a BDC or domain trust */
3875 can_add_account = nt_token_check_domain_rid(
3876 p->session_info->security_token,
3877 DOMAIN_RID_ADMINS );
3880 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3881 uidtoname(p->session_info->utok.uid),
3882 can_add_account ? "True":"False" ));
3884 if (!can_add_account) {
3885 return NT_STATUS_ACCESS_DENIED;
3888 /********** BEGIN Admin BLOCK **********/
3891 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3895 /********** END Admin BLOCK **********/
3897 /* now check for failure */
3899 if ( !NT_STATUS_IS_OK(nt_status) )
3902 /* Get the user's SID */
3904 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3906 map_max_allowed_access(p->session_info->security_token,
3907 &p->session_info->utok,
3910 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3911 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3912 se_map_generic(&des_access, &usr_generic_mapping);
3915 * JRA - TESTME. We just created this user so we
3916 * had rights to create them. Do we need to check
3917 * any further access on this object ? Can't we
3918 * just assume we have all the rights we need ?
3921 nt_status = access_check_object(psd, p->session_info->security_token,
3922 needed_priv, SEC_PRIV_INVALID,
3923 GENERIC_RIGHTS_USER_WRITE, des_access,
3924 &acc_granted, "_samr_CreateUser2");
3926 if ( !NT_STATUS_IS_OK(nt_status) ) {
3930 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
3931 struct samr_user_info, &nt_status);
3932 if (!NT_STATUS_IS_OK(nt_status)) {
3937 /* After a "set" ensure we have no cached display info. */
3938 force_flush_samr_cache(&sid);
3940 *r->out.access_granted = acc_granted;
3942 return NT_STATUS_OK;
3945 /****************************************************************
3946 ****************************************************************/
3948 NTSTATUS _samr_CreateUser(struct pipes_struct *p,
3949 struct samr_CreateUser *r)
3951 struct samr_CreateUser2 c;
3952 uint32_t access_granted;
3954 c.in.domain_handle = r->in.domain_handle;
3955 c.in.account_name = r->in.account_name;
3956 c.in.acct_flags = ACB_NORMAL;
3957 c.in.access_mask = r->in.access_mask;
3958 c.out.user_handle = r->out.user_handle;
3959 c.out.access_granted = &access_granted;
3960 c.out.rid = r->out.rid;
3962 return _samr_CreateUser2(p, &c);
3965 /*******************************************************************
3967 ********************************************************************/
3969 NTSTATUS _samr_Connect(struct pipes_struct *p,
3970 struct samr_Connect *r)
3972 struct samr_connect_info *info;
3973 uint32_t acc_granted;
3974 struct policy_handle hnd;
3975 uint32 des_access = r->in.access_mask;
3980 if (!pipe_access_check(p)) {
3981 DEBUG(3, ("access denied to _samr_Connect\n"));
3982 return NT_STATUS_ACCESS_DENIED;
3985 /* don't give away the farm but this is probably ok. The SAMR_ACCESS_ENUM_DOMAINS
3986 was observed from a win98 client trying to enumerate users (when configured
3987 user level access control on shares) --jerry */
3989 map_max_allowed_access(p->session_info->security_token,
3990 &p->session_info->utok,
3993 se_map_generic( &des_access, &sam_generic_mapping );
3995 acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS
3996 |SAMR_ACCESS_LOOKUP_DOMAIN);
3998 /* set up the SAMR connect_anon response */
4000 info = policy_handle_create(p, &hnd, acc_granted,
4001 struct samr_connect_info,
4003 if (!NT_STATUS_IS_OK(status)) {
4007 *r->out.connect_handle = hnd;
4008 return NT_STATUS_OK;
4011 /*******************************************************************
4013 ********************************************************************/
4015 NTSTATUS _samr_Connect2(struct pipes_struct *p,
4016 struct samr_Connect2 *r)
4018 struct samr_connect_info *info = NULL;
4019 struct policy_handle hnd;
4020 struct security_descriptor *psd = NULL;
4022 uint32 des_access = r->in.access_mask;
4025 const char *fn = "_samr_Connect2";
4028 case NDR_SAMR_CONNECT2:
4029 fn = "_samr_Connect2";
4031 case NDR_SAMR_CONNECT3:
4032 fn = "_samr_Connect3";
4034 case NDR_SAMR_CONNECT4:
4035 fn = "_samr_Connect4";
4037 case NDR_SAMR_CONNECT5:
4038 fn = "_samr_Connect5";
4042 DEBUG(5,("%s: %d\n", fn, __LINE__));
4046 if (!pipe_access_check(p)) {
4047 DEBUG(3, ("access denied to %s\n", fn));
4048 return NT_STATUS_ACCESS_DENIED;
4051 map_max_allowed_access(p->session_info->security_token,
4052 &p->session_info->utok,
4055 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
4056 se_map_generic(&des_access, &sam_generic_mapping);
4058 nt_status = access_check_object(psd, p->session_info->security_token,
4059 SEC_PRIV_INVALID, SEC_PRIV_INVALID,
4060 0, des_access, &acc_granted, fn);
4062 if ( !NT_STATUS_IS_OK(nt_status) )
4065 info = policy_handle_create(p, &hnd, acc_granted,
4066 struct samr_connect_info, &nt_status);
4067 if (!NT_STATUS_IS_OK(nt_status)) {
4071 DEBUG(5,("%s: %d\n", fn, __LINE__));
4073 *r->out.connect_handle = hnd;
4074 return NT_STATUS_OK;
4077 /****************************************************************
4079 ****************************************************************/
4081 NTSTATUS _samr_Connect3(struct pipes_struct *p,
4082 struct samr_Connect3 *r)
4084 struct samr_Connect2 c;
4086 c.in.system_name = r->in.system_name;
4087 c.in.access_mask = r->in.access_mask;
4088 c.out.connect_handle = r->out.connect_handle;
4090 return _samr_Connect2(p, &c);
4093 /*******************************************************************
4095 ********************************************************************/
4097 NTSTATUS _samr_Connect4(struct pipes_struct *p,
4098 struct samr_Connect4 *r)
4100 struct samr_Connect2 c;
4102 c.in.system_name = r->in.system_name;
4103 c.in.access_mask = r->in.access_mask;
4104 c.out.connect_handle = r->out.connect_handle;
4106 return _samr_Connect2(p, &c);
4109 /*******************************************************************
4111 ********************************************************************/
4113 NTSTATUS _samr_Connect5(struct pipes_struct *p,
4114 struct samr_Connect5 *r)
4117 struct samr_Connect2 c;
4118 struct samr_ConnectInfo1 info1;
4120 info1.client_version = SAMR_CONNECT_AFTER_W2K;
4123 c.in.system_name = r->in.system_name;
4124 c.in.access_mask = r->in.access_mask;
4125 c.out.connect_handle = r->out.connect_handle;
4127 *r->out.level_out = 1;
4129 status = _samr_Connect2(p, &c);
4130 if (!NT_STATUS_IS_OK(status)) {
4134 r->out.info_out->info1 = info1;
4136 return NT_STATUS_OK;
4139 /**********************************************************************
4141 **********************************************************************/
4143 NTSTATUS _samr_LookupDomain(struct pipes_struct *p,
4144 struct samr_LookupDomain *r)
4147 struct samr_connect_info *info;
4148 const char *domain_name;
4149 struct dom_sid *sid = NULL;
4151 /* win9x user manager likes to use SAMR_ACCESS_ENUM_DOMAINS here.
4152 Reverted that change so we will work with RAS servers again */
4154 info = policy_handle_find(p, r->in.connect_handle,
4155 SAMR_ACCESS_LOOKUP_DOMAIN, NULL,
4156 struct samr_connect_info,
4158 if (!NT_STATUS_IS_OK(status)) {
4162 domain_name = r->in.domain_name->string;
4164 return NT_STATUS_INVALID_PARAMETER;
4167 sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
4169 return NT_STATUS_NO_MEMORY;
4172 if (strequal(domain_name, builtin_domain_name())) {
4173 sid_copy(sid, &global_sid_Builtin);
4175 if (!secrets_fetch_domain_sid(domain_name, sid)) {
4176 status = NT_STATUS_NO_SUCH_DOMAIN;
4180 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
4181 sid_string_dbg(sid)));
4188 /**********************************************************************
4190 **********************************************************************/
4192 NTSTATUS _samr_EnumDomains(struct pipes_struct *p,
4193 struct samr_EnumDomains *r)
4196 struct samr_connect_info *info;
4197 uint32_t num_entries = 2;
4198 struct samr_SamEntry *entry_array = NULL;
4199 struct samr_SamArray *sam;
4201 info = policy_handle_find(p, r->in.connect_handle,
4202 SAMR_ACCESS_ENUM_DOMAINS, NULL,
4203 struct samr_connect_info, &status);
4204 if (!NT_STATUS_IS_OK(status)) {
4208 sam = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
4210 return NT_STATUS_NO_MEMORY;
4213 entry_array = TALLOC_ZERO_ARRAY(p->mem_ctx,
4214 struct samr_SamEntry,
4217 return NT_STATUS_NO_MEMORY;
4220 entry_array[0].idx = 0;
4221 init_lsa_String(&entry_array[0].name, get_global_sam_name());
4223 entry_array[1].idx = 1;
4224 init_lsa_String(&entry_array[1].name, "Builtin");
4226 sam->count = num_entries;
4227 sam->entries = entry_array;
4230 *r->out.num_entries = num_entries;
4235 /*******************************************************************
4237 ********************************************************************/
4239 NTSTATUS _samr_OpenAlias(struct pipes_struct *p,
4240 struct samr_OpenAlias *r)
4243 uint32 alias_rid = r->in.rid;
4244 struct samr_alias_info *ainfo;
4245 struct samr_domain_info *dinfo;
4246 struct security_descriptor *psd = NULL;
4248 uint32 des_access = r->in.access_mask;
4252 dinfo = policy_handle_find(p, r->in.domain_handle,
4253 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
4254 struct samr_domain_info, &status);
4255 if (!NT_STATUS_IS_OK(status)) {
4259 /* append the alias' RID to it */
4261 if (!sid_compose(&sid, &dinfo->sid, alias_rid))
4262 return NT_STATUS_NO_SUCH_ALIAS;
4264 /*check if access can be granted as requested by client. */
4266 map_max_allowed_access(p->session_info->security_token,
4267 &p->session_info->utok,
4270 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
4271 se_map_generic(&des_access,&ali_generic_mapping);
4273 status = access_check_object(psd, p->session_info->security_token,
4274 SEC_PRIV_ADD_USERS, SEC_PRIV_INVALID,
4275 GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
4276 des_access, &acc_granted, "_samr_OpenAlias");
4278 if ( !NT_STATUS_IS_OK(status) )
4282 /* Check we actually have the requested alias */
4283 enum lsa_SidType type;
4288 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
4291 if (!result || (type != SID_NAME_ALIAS)) {
4292 return NT_STATUS_NO_SUCH_ALIAS;
4295 /* make sure there is a mapping */
4297 if ( !sid_to_gid( &sid, &gid ) ) {
4298 return NT_STATUS_NO_SUCH_ALIAS;
4303 ainfo = policy_handle_create(p, r->out.alias_handle, acc_granted,
4304 struct samr_alias_info, &status);
4305 if (!NT_STATUS_IS_OK(status)) {
4310 return NT_STATUS_OK;
4313 /*******************************************************************
4315 ********************************************************************/
4317 static NTSTATUS set_user_info_2(TALLOC_CTX *mem_ctx,
4318 struct samr_UserInfo2 *id2,
4322 DEBUG(5,("set_user_info_2: NULL id2\n"));
4323 return NT_STATUS_ACCESS_DENIED;
4326 copy_id2_to_sam_passwd(pwd, id2);
4328 return pdb_update_sam_account(pwd);
4331 /*******************************************************************
4333 ********************************************************************/
4335 static NTSTATUS set_user_info_4(TALLOC_CTX *mem_ctx,
4336 struct samr_UserInfo4 *id4,
4340 DEBUG(5,("set_user_info_2: NULL id4\n"));
4341 return NT_STATUS_ACCESS_DENIED;
4344 copy_id4_to_sam_passwd(pwd, id4);
4346 return pdb_update_sam_account(pwd);
4349 /*******************************************************************
4351 ********************************************************************/
4353 static NTSTATUS set_user_info_6(TALLOC_CTX *mem_ctx,
4354 struct samr_UserInfo6 *id6,
4358 DEBUG(5,("set_user_info_6: NULL id6\n"));
4359 return NT_STATUS_ACCESS_DENIED;
4362 copy_id6_to_sam_passwd(pwd, id6);
4364 return pdb_update_sam_account(pwd);
4367 /*******************************************************************
4369 ********************************************************************/
4371 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
4372 struct samr_UserInfo7 *id7,
4378 DEBUG(5, ("set_user_info_7: NULL id7\n"));
4379 return NT_STATUS_ACCESS_DENIED;
4382 if (!id7->account_name.string) {
4383 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
4384 return NT_STATUS_ACCESS_DENIED;
4387 /* check to see if the new username already exists. Note: we can't
4388 reliably lock all backends, so there is potentially the
4389 possibility that a user can be created in between this check and
4390 the rename. The rename should fail, but may not get the
4391 exact same failure status code. I think this is small enough
4392 of a window for this type of operation and the results are
4393 simply that the rename fails with a slightly different status
4394 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
4396 rc = can_create(mem_ctx, id7->account_name.string);
4398 /* when there is nothing to change, we're done here */
4399 if (NT_STATUS_EQUAL(rc, NT_STATUS_USER_EXISTS) &&
4400 strequal(id7->account_name.string, pdb_get_username(pwd))) {
4401 return NT_STATUS_OK;
4403 if (!NT_STATUS_IS_OK(rc)) {
4407 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
4412 /*******************************************************************
4414 ********************************************************************/
4416 static NTSTATUS set_user_info_8(TALLOC_CTX *mem_ctx,
4417 struct samr_UserInfo8 *id8,
4421 DEBUG(5,("set_user_info_8: NULL id8\n"));
4422 return NT_STATUS_ACCESS_DENIED;
4425 copy_id8_to_sam_passwd(pwd, id8);
4427 return pdb_update_sam_account(pwd);
4430 /*******************************************************************
4432 ********************************************************************/
4434 static NTSTATUS set_user_info_10(TALLOC_CTX *mem_ctx,
4435 struct samr_UserInfo10 *id10,
4439 DEBUG(5,("set_user_info_8: NULL id10\n"));
4440 return NT_STATUS_ACCESS_DENIED;
4443 copy_id10_to_sam_passwd(pwd, id10);
4445 return pdb_update_sam_account(pwd);
4448 /*******************************************************************
4450 ********************************************************************/
4452 static NTSTATUS set_user_info_11(TALLOC_CTX *mem_ctx,
4453 struct samr_UserInfo11 *id11,
4457 DEBUG(5,("set_user_info_11: NULL id11\n"));
4458 return NT_STATUS_ACCESS_DENIED;
4461 copy_id11_to_sam_passwd(pwd, id11);
4463 return pdb_update_sam_account(pwd);
4466 /*******************************************************************
4468 ********************************************************************/
4470 static NTSTATUS set_user_info_12(TALLOC_CTX *mem_ctx,
4471 struct samr_UserInfo12 *id12,
4475 DEBUG(5,("set_user_info_12: NULL id12\n"));
4476 return NT_STATUS_ACCESS_DENIED;
4479 copy_id12_to_sam_passwd(pwd, id12);
4481 return pdb_update_sam_account(pwd);
4484 /*******************************************************************
4486 ********************************************************************/
4488 static NTSTATUS set_user_info_13(TALLOC_CTX *mem_ctx,
4489 struct samr_UserInfo13 *id13,
4493 DEBUG(5,("set_user_info_13: NULL id13\n"));
4494 return NT_STATUS_ACCESS_DENIED;
4497 copy_id13_to_sam_passwd(pwd, id13);
4499 return pdb_update_sam_account(pwd);
4502 /*******************************************************************
4504 ********************************************************************/
4506 static NTSTATUS set_user_info_14(TALLOC_CTX *mem_ctx,
4507 struct samr_UserInfo14 *id14,
4511 DEBUG(5,("set_user_info_14: NULL id14\n"));
4512 return NT_STATUS_ACCESS_DENIED;
4515 copy_id14_to_sam_passwd(pwd, id14);
4517 return pdb_update_sam_account(pwd);
4520 /*******************************************************************
4522 ********************************************************************/
4524 static NTSTATUS set_user_info_16(TALLOC_CTX *mem_ctx,
4525 struct samr_UserInfo16 *id16,
4529 DEBUG(5,("set_user_info_16: NULL id16\n"));
4530 return NT_STATUS_ACCESS_DENIED;
4533 copy_id16_to_sam_passwd(pwd, id16);
4535 return pdb_update_sam_account(pwd);
4538 /*******************************************************************
4540 ********************************************************************/
4542 static NTSTATUS set_user_info_17(TALLOC_CTX *mem_ctx,
4543 struct samr_UserInfo17 *id17,
4547 DEBUG(5,("set_user_info_17: NULL id17\n"));
4548 return NT_STATUS_ACCESS_DENIED;
4551 copy_id17_to_sam_passwd(pwd, id17);
4553 return pdb_update_sam_account(pwd);
4556 /*******************************************************************
4558 ********************************************************************/
4560 static NTSTATUS set_user_info_18(struct samr_UserInfo18 *id18,
4561 TALLOC_CTX *mem_ctx,
4562 DATA_BLOB *session_key,
4566 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
4567 return NT_STATUS_INVALID_PARAMETER;
4570 if (id18->nt_pwd_active || id18->lm_pwd_active) {
4571 if (!session_key->length) {
4572 return NT_STATUS_NO_USER_SESSION_KEY;
4576 if (id18->nt_pwd_active) {
4580 in = data_blob_const(id18->nt_pwd.hash, 16);
4581 out = data_blob_talloc_zero(mem_ctx, 16);
4583 sess_crypt_blob(&out, &in, session_key, false);
4585 if (!pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED)) {
4586 return NT_STATUS_ACCESS_DENIED;
4589 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4592 if (id18->lm_pwd_active) {
4596 in = data_blob_const(id18->lm_pwd.hash, 16);
4597 out = data_blob_talloc_zero(mem_ctx, 16);
4599 sess_crypt_blob(&out, &in, session_key, false);
4601 if (!pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED)) {
4602 return NT_STATUS_ACCESS_DENIED;
4605 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4608 copy_id18_to_sam_passwd(pwd, id18);
4610 return pdb_update_sam_account(pwd);
4613 /*******************************************************************
4615 ********************************************************************/
4617 static NTSTATUS set_user_info_20(TALLOC_CTX *mem_ctx,
4618 struct samr_UserInfo20 *id20,
4622 DEBUG(5,("set_user_info_20: NULL id20\n"));
4623 return NT_STATUS_ACCESS_DENIED;
4626 copy_id20_to_sam_passwd(pwd, id20);
4628 return pdb_update_sam_account(pwd);
4631 /*******************************************************************
4633 ********************************************************************/
4635 static NTSTATUS set_user_info_21(struct samr_UserInfo21 *id21,
4636 TALLOC_CTX *mem_ctx,
4637 DATA_BLOB *session_key,
4643 DEBUG(5, ("set_user_info_21: NULL id21\n"));
4644 return NT_STATUS_INVALID_PARAMETER;
4647 if (id21->fields_present == 0) {
4648 return NT_STATUS_INVALID_PARAMETER;
4651 if (id21->fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4652 return NT_STATUS_ACCESS_DENIED;
4655 if (id21->fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) {
4656 if (id21->nt_password_set) {
4659 if ((id21->nt_owf_password.length != 16) ||
4660 (id21->nt_owf_password.size != 16)) {
4661 return NT_STATUS_INVALID_PARAMETER;
4664 if (!session_key->length) {
4665 return NT_STATUS_NO_USER_SESSION_KEY;
4668 in = data_blob_const(id21->nt_owf_password.array, 16);
4669 out = data_blob_talloc_zero(mem_ctx, 16);
4671 sess_crypt_blob(&out, &in, session_key, false);
4673 pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED);
4674 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4678 if (id21->fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT) {
4679 if (id21->lm_password_set) {
4682 if ((id21->lm_owf_password.length != 16) ||
4683 (id21->lm_owf_password.size != 16)) {
4684 return NT_STATUS_INVALID_PARAMETER;
4687 if (!session_key->length) {
4688 return NT_STATUS_NO_USER_SESSION_KEY;
4691 in = data_blob_const(id21->lm_owf_password.array, 16);
4692 out = data_blob_talloc_zero(mem_ctx, 16);
4694 sess_crypt_blob(&out, &in, session_key, false);
4696 pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED);
4697 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4701 /* we need to separately check for an account rename first */
4703 if (id21->account_name.string &&
4704 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
4707 /* check to see if the new username already exists. Note: we can't
4708 reliably lock all backends, so there is potentially the
4709 possibility that a user can be created in between this check and
4710 the rename. The rename should fail, but may not get the
4711 exact same failure status code. I think this is small enough
4712 of a window for this type of operation and the results are
4713 simply that the rename fails with a slightly different status
4714 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
4716 status = can_create(mem_ctx, id21->account_name.string);
4717 if (!NT_STATUS_IS_OK(status)) {
4721 status = pdb_rename_sam_account(pwd, id21->account_name.string);
4723 if (!NT_STATUS_IS_OK(status)) {
4724 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
4725 nt_errstr(status)));
4729 /* set the new username so that later
4730 functions can work on the new account */
4731 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
4734 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
4737 * The funny part about the previous two calls is
4738 * that pwd still has the password hashes from the
4739 * passdb entry. These have not been updated from
4740 * id21. I don't know if they need to be set. --jerry
4743 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4744 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4745 if ( !NT_STATUS_IS_OK(status) ) {
4750 /* Don't worry about writing out the user account since the
4751 primary group SID is generated solely from the user's Unix
4754 /* write the change out */
4755 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4759 return NT_STATUS_OK;
4762 /*******************************************************************
4764 ********************************************************************/
4766 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
4767 struct samr_UserInfo23 *id23,
4771 char *plaintext_buf = NULL;
4777 DEBUG(5, ("set_user_info_23: NULL id23\n"));
4778 return NT_STATUS_INVALID_PARAMETER;
4781 if (id23->info.fields_present == 0) {
4782 return NT_STATUS_INVALID_PARAMETER;
4785 if (id23->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4786 return NT_STATUS_ACCESS_DENIED;
4789 if ((id23->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4790 (id23->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4792 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
4793 pdb_get_username(pwd)));
4795 if (!decode_pw_buffer(mem_ctx,
4796 id23->password.data,
4800 return NT_STATUS_WRONG_PASSWORD;
4803 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4804 return NT_STATUS_ACCESS_DENIED;
4808 copy_id23_to_sam_passwd(pwd, id23);
4810 acct_ctrl = pdb_get_acct_ctrl(pwd);
4812 /* if it's a trust account, don't update /etc/passwd */
4813 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4814 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4815 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4816 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
4817 } else if (plaintext_buf) {
4818 /* update the UNIX password */
4819 if (lp_unix_password_sync() ) {
4820 struct passwd *passwd;
4821 if (pdb_get_username(pwd) == NULL) {
4822 DEBUG(1, ("chgpasswd: User without name???\n"));
4823 return NT_STATUS_ACCESS_DENIED;
4826 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4827 if (passwd == NULL) {
4828 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4831 if(!chgpasswd(pdb_get_username(pwd), rhost,
4832 passwd, "", plaintext_buf, True)) {
4833 return NT_STATUS_ACCESS_DENIED;
4835 TALLOC_FREE(passwd);
4839 if (plaintext_buf) {
4840 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4843 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
4844 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
4849 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4853 return NT_STATUS_OK;
4856 /*******************************************************************
4858 ********************************************************************/
4860 static bool set_user_info_pw(uint8 *pass, const char *rhost, struct samu *pwd)
4863 char *plaintext_buf = NULL;
4866 DEBUG(5, ("Attempting administrator password change for user %s\n",
4867 pdb_get_username(pwd)));
4869 acct_ctrl = pdb_get_acct_ctrl(pwd);
4871 if (!decode_pw_buffer(talloc_tos(),
4879 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4883 /* if it's a trust account, don't update /etc/passwd */
4884 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4885 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4886 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4887 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
4889 /* update the UNIX password */
4890 if (lp_unix_password_sync()) {
4891 struct passwd *passwd;
4893 if (pdb_get_username(pwd) == NULL) {
4894 DEBUG(1, ("chgpasswd: User without name???\n"));
4898 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4899 if (passwd == NULL) {
4900 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4903 if(!chgpasswd(pdb_get_username(pwd), rhost, passwd,
4904 "", plaintext_buf, True)) {
4907 TALLOC_FREE(passwd);
4911 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4913 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
4918 /*******************************************************************
4920 ********************************************************************/
4922 static NTSTATUS set_user_info_24(TALLOC_CTX *mem_ctx,
4924 struct samr_UserInfo24 *id24,
4930 DEBUG(5, ("set_user_info_24: NULL id24\n"));
4931 return NT_STATUS_INVALID_PARAMETER;
4934 if (!set_user_info_pw(id24->password.data, rhost, pwd)) {
4935 return NT_STATUS_WRONG_PASSWORD;
4938 copy_id24_to_sam_passwd(pwd, id24);
4940 status = pdb_update_sam_account(pwd);
4941 if (!NT_STATUS_IS_OK(status)) {
4945 return NT_STATUS_OK;
4948 /*******************************************************************
4950 ********************************************************************/
4952 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
4954 struct samr_UserInfo25 *id25,
4960 DEBUG(5, ("set_user_info_25: NULL id25\n"));
4961 return NT_STATUS_INVALID_PARAMETER;
4964 if (id25->info.fields_present == 0) {
4965 return NT_STATUS_INVALID_PARAMETER;
4968 if (id25->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4969 return NT_STATUS_ACCESS_DENIED;
4972 if ((id25->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4973 (id25->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4975 if (!set_user_info_pw(id25->password.data, rhost, pwd)) {
4976 return NT_STATUS_WRONG_PASSWORD;
4980 copy_id25_to_sam_passwd(pwd, id25);
4982 /* write the change out */
4983 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4988 * We need to "pdb_update_sam_account" before the unix primary group
4989 * is set, because the idealx scripts would also change the
4990 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
4991 * the delete explicit / add explicit, which would then fail to find
4992 * the previous primaryGroupSid value.
4995 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4996 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4997 if ( !NT_STATUS_IS_OK(status) ) {
5002 return NT_STATUS_OK;
5005 /*******************************************************************
5007 ********************************************************************/
5009 static NTSTATUS set_user_info_26(TALLOC_CTX *mem_ctx,
5011 struct samr_UserInfo26 *id26,
5017 DEBUG(5, ("set_user_info_26: NULL id26\n"));
5018 return NT_STATUS_INVALID_PARAMETER;
5021 if (!set_user_info_pw(id26->password.data, rhost, pwd)) {
5022 return NT_STATUS_WRONG_PASSWORD;
5025 copy_id26_to_sam_passwd(pwd, id26);
5027 status = pdb_update_sam_account(pwd);
5028 if (!NT_STATUS_IS_OK(status)) {
5032 return NT_STATUS_OK;
5035 /*************************************************************
5036 **************************************************************/
5038 static uint32_t samr_set_user_info_map_fields_to_access_mask(uint32_t fields)
5040 uint32_t acc_required = 0;
5042 /* USER_ALL_USERNAME */
5043 if (fields & SAMR_FIELD_ACCOUNT_NAME)
5044 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5045 /* USER_ALL_FULLNAME */
5046 if (fields & SAMR_FIELD_FULL_NAME)
5047 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5048 /* USER_ALL_PRIMARYGROUPID */
5049 if (fields & SAMR_FIELD_PRIMARY_GID)
5050 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5051 /* USER_ALL_HOMEDIRECTORY */
5052 if (fields & SAMR_FIELD_HOME_DIRECTORY)
5053 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5054 /* USER_ALL_HOMEDIRECTORYDRIVE */
5055 if (fields & SAMR_FIELD_HOME_DRIVE)
5056 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5057 /* USER_ALL_SCRIPTPATH */
5058 if (fields & SAMR_FIELD_LOGON_SCRIPT)
5059 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5060 /* USER_ALL_PROFILEPATH */
5061 if (fields & SAMR_FIELD_PROFILE_PATH)
5062 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5063 /* USER_ALL_ADMINCOMMENT */
5064 if (fields & SAMR_FIELD_COMMENT)
5065 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5066 /* USER_ALL_WORKSTATIONS */
5067 if (fields & SAMR_FIELD_WORKSTATIONS)
5068 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5069 /* USER_ALL_LOGONHOURS */
5070 if (fields & SAMR_FIELD_LOGON_HOURS)
5071 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5072 /* USER_ALL_ACCOUNTEXPIRES */
5073 if (fields & SAMR_FIELD_ACCT_EXPIRY)
5074 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5075 /* USER_ALL_USERACCOUNTCONTROL */
5076 if (fields & SAMR_FIELD_ACCT_FLAGS)
5077 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5078 /* USER_ALL_PARAMETERS */
5079 if (fields & SAMR_FIELD_PARAMETERS)
5080 acc_required |= SAMR_USER_ACCESS_SET_ATTRIBUTES;
5081 /* USER_ALL_USERCOMMENT */
5082 if (fields & SAMR_FIELD_COMMENT)
5083 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5084 /* USER_ALL_COUNTRYCODE */
5085 if (fields & SAMR_FIELD_COUNTRY_CODE)
5086 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5087 /* USER_ALL_CODEPAGE */
5088 if (fields & SAMR_FIELD_CODE_PAGE)
5089 acc_required |= SAMR_USER_ACCESS_SET_LOC_COM;
5090 /* USER_ALL_NTPASSWORDPRESENT */
5091 if (fields & SAMR_FIELD_NT_PASSWORD_PRESENT)
5092 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5093 /* USER_ALL_LMPASSWORDPRESENT */
5094 if (fields & SAMR_FIELD_LM_PASSWORD_PRESENT)
5095 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5096 /* USER_ALL_PASSWORDEXPIRED */
5097 if (fields & SAMR_FIELD_EXPIRED_FLAG)
5098 acc_required |= SAMR_USER_ACCESS_SET_PASSWORD;
5100 return acc_required;
5103 /*******************************************************************
5105 ********************************************************************/
5107 NTSTATUS _samr_SetUserInfo(struct pipes_struct *p,
5108 struct samr_SetUserInfo *r)
5110 struct samr_user_info *uinfo;
5112 struct samu *pwd = NULL;
5113 union samr_UserInfo *info = r->in.info;
5114 uint32_t acc_required = 0;
5115 uint32_t fields = 0;
5118 DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
5120 /* This is tricky. A WinXP domain join sets
5121 (SAMR_USER_ACCESS_SET_PASSWORD|SAMR_USER_ACCESS_SET_ATTRIBUTES|SAMR_USER_ACCESS_GET_ATTRIBUTES)
5122 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
5123 standard Win32 API calls just ask for SAMR_USER_ACCESS_SET_PASSWORD in the SamrOpenUser().
5124 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
5125 we'll use the set from the WinXP join as the basis. */
5127 switch (r->in.level) {
5128 case 2: /* UserPreferencesInformation */
5129 /* USER_WRITE_ACCOUNT | USER_WRITE_PREFERENCES */
5130 acc_required = SAMR_USER_ACCESS_SET_ATTRIBUTES | SAMR_USER_ACCESS_SET_LOC_COM;
5132 case 4: /* UserLogonHoursInformation */
5133 case 6: /* UserNameInformation */
5134 case 7: /* UserAccountNameInformation */
5135 case 8: /* UserFullNameInformation */
5136 case 9: /* UserPrimaryGroupInformation */
5137 case 10: /* UserHomeInformation */
5138 case 11: /* UserScriptInformation */
5139 case 12: /* UserProfileInformation */
5140 case 13: /* UserAdminCommentInformation */
5141 case 14: /* UserWorkStationsInformation */
5142 case 16: /* UserControlInformation */
5143 case 17: /* UserExpiresInformation */
5144 case 20: /* UserParametersInformation */
5145 /* USER_WRITE_ACCOUNT */
5146 acc_required = SAMR_USER_ACCESS_SET_ATTRIBUTES;
5148 case 18: /* UserInternal1Information */
5149 /* FIXME: gd, this is a guess */
5150 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
5152 case 21: /* UserAllInformation */
5153 fields = info->info21.fields_present;
5154 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5156 case 23: /* UserInternal4Information */
5157 fields = info->info23.info.fields_present;
5158 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5160 case 25: /* UserInternal4InformationNew */
5161 fields = info->info25.info.fields_present;
5162 acc_required = samr_set_user_info_map_fields_to_access_mask(fields);
5164 case 24: /* UserInternal5Information */
5165 case 26: /* UserInternal5InformationNew */
5166 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
5169 return NT_STATUS_INVALID_INFO_CLASS;
5172 uinfo = policy_handle_find(p, r->in.user_handle, acc_required, NULL,
5173 struct samr_user_info, &status);
5174 if (!NT_STATUS_IS_OK(status)) {
5178 DEBUG(5, ("_samr_SetUserInfo: sid:%s, level:%d\n",
5179 sid_string_dbg(&uinfo->sid), r->in.level));
5182 DEBUG(5, ("_samr_SetUserInfo: NULL info level\n"));
5183 return NT_STATUS_INVALID_INFO_CLASS;
5186 if (!(pwd = samu_new(NULL))) {
5187 return NT_STATUS_NO_MEMORY;
5191 ret = pdb_getsampwsid(pwd, &uinfo->sid);
5196 return NT_STATUS_NO_SUCH_USER;
5199 /* ================ BEGIN Privilege BLOCK ================ */
5203 /* ok! user info levels (lots: see MSDEV help), off we go... */
5205 switch (r->in.level) {
5208 status = set_user_info_2(p->mem_ctx,
5213 status = set_user_info_4(p->mem_ctx,
5218 status = set_user_info_6(p->mem_ctx,
5223 status = set_user_info_7(p->mem_ctx,
5228 status = set_user_info_8(p->mem_ctx,
5233 status = set_user_info_10(p->mem_ctx,
5234 &info->info10, pwd);
5238 status = set_user_info_11(p->mem_ctx,
5239 &info->info11, pwd);
5243 status = set_user_info_12(p->mem_ctx,
5244 &info->info12, pwd);
5248 status = set_user_info_13(p->mem_ctx,
5249 &info->info13, pwd);
5253 status = set_user_info_14(p->mem_ctx,
5254 &info->info14, pwd);
5258 status = set_user_info_16(p->mem_ctx,
5259 &info->info16, pwd);
5263 status = set_user_info_17(p->mem_ctx,
5264 &info->info17, pwd);
5268 /* Used by AS/U JRA. */
5269 status = set_user_info_18(&info->info18,
5271 &p->session_info->user_session_key,
5276 status = set_user_info_20(p->mem_ctx,
5277 &info->info20, pwd);
5281 status = set_user_info_21(&info->info21,
5283 &p->session_info->user_session_key,
5288 if (!p->session_info->user_session_key.length) {
5289 status = NT_STATUS_NO_USER_SESSION_KEY;
5291 arcfour_crypt_blob(info->info23.password.data, 516,
5292 &p->session_info->user_session_key);
5294 dump_data(100, info->info23.password.data, 516);
5296 status = set_user_info_23(p->mem_ctx,
5303 if (!p->session_info->user_session_key.length) {
5304 status = NT_STATUS_NO_USER_SESSION_KEY;
5306 arcfour_crypt_blob(info->info24.password.data,
5308 &p->session_info->user_session_key);
5310 dump_data(100, info->info24.password.data, 516);
5312 status = set_user_info_24(p->mem_ctx,
5314 &info->info24, pwd);
5318 if (!p->session_info->user_session_key.length) {
5319 status = NT_STATUS_NO_USER_SESSION_KEY;
5321 encode_or_decode_arc4_passwd_buffer(
5322 info->info25.password.data,
5323 &p->session_info->user_session_key);
5325 dump_data(100, info->info25.password.data, 532);
5327 status = set_user_info_25(p->mem_ctx,
5329 &info->info25, pwd);
5333 if (!p->session_info->user_session_key.length) {
5334 status = NT_STATUS_NO_USER_SESSION_KEY;
5336 encode_or_decode_arc4_passwd_buffer(
5337 info->info26.password.data,
5338 &p->session_info->user_session_key);
5340 dump_data(100, info->info26.password.data, 516);
5342 status = set_user_info_26(p->mem_ctx,
5344 &info->info26, pwd);
5348 status = NT_STATUS_INVALID_INFO_CLASS;
5355 /* ================ END Privilege BLOCK ================ */
5357 if (NT_STATUS_IS_OK(status)) {
5358 force_flush_samr_cache(&uinfo->sid);
5364 /*******************************************************************
5366 ********************************************************************/
5368 NTSTATUS _samr_SetUserInfo2(struct pipes_struct *p,
5369 struct samr_SetUserInfo2 *r)
5371 struct samr_SetUserInfo q;
5373 q.in.user_handle = r->in.user_handle;
5374 q.in.level = r->in.level;
5375 q.in.info = r->in.info;
5377 return _samr_SetUserInfo(p, &q);
5380 /*********************************************************************
5381 _samr_GetAliasMembership
5382 *********************************************************************/
5384 NTSTATUS _samr_GetAliasMembership(struct pipes_struct *p,
5385 struct samr_GetAliasMembership *r)
5387 size_t num_alias_rids;
5389 struct samr_domain_info *dinfo;
5394 struct dom_sid *members;
5396 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__));
5398 dinfo = policy_handle_find(p, r->in.domain_handle,
5399 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS
5400 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
5401 struct samr_domain_info, &status);
5402 if (!NT_STATUS_IS_OK(status)) {
5406 if (!sid_check_is_domain(&dinfo->sid) &&
5407 !sid_check_is_builtin(&dinfo->sid))
5408 return NT_STATUS_OBJECT_TYPE_MISMATCH;
5410 if (r->in.sids->num_sids) {
5411 members = TALLOC_ARRAY(p->mem_ctx, struct dom_sid, r->in.sids->num_sids);
5413 if (members == NULL)
5414 return NT_STATUS_NO_MEMORY;
5419 for (i=0; i<r->in.sids->num_sids; i++)
5420 sid_copy(&members[i], r->in.sids->sids[i].sid);
5426 status = pdb_enum_alias_memberships(p->mem_ctx, &dinfo->sid, members,
5427 r->in.sids->num_sids,
5428 &alias_rids, &num_alias_rids);
5431 if (!NT_STATUS_IS_OK(status)) {
5435 r->out.rids->count = num_alias_rids;
5436 r->out.rids->ids = alias_rids;
5438 if (r->out.rids->ids == NULL) {
5439 /* Windows domain clients don't accept a NULL ptr here */
5440 r->out.rids->ids = talloc_zero(p->mem_ctx, uint32_t);
5442 if (r->out.rids->ids == NULL) {
5443 return NT_STATUS_NO_MEMORY;
5446 return NT_STATUS_OK;
5449 /*********************************************************************
5450 _samr_GetMembersInAlias
5451 *********************************************************************/
5453 NTSTATUS _samr_GetMembersInAlias(struct pipes_struct *p,
5454 struct samr_GetMembersInAlias *r)
5456 struct samr_alias_info *ainfo;
5459 size_t num_sids = 0;
5460 struct lsa_SidPtr *sids = NULL;
5461 struct dom_sid *pdb_sids = NULL;
5463 ainfo = policy_handle_find(p, r->in.alias_handle,
5464 SAMR_ALIAS_ACCESS_GET_MEMBERS, NULL,
5465 struct samr_alias_info, &status);
5466 if (!NT_STATUS_IS_OK(status)) {
5470 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5473 status = pdb_enum_aliasmem(&ainfo->sid, talloc_tos(), &pdb_sids,
5477 if (!NT_STATUS_IS_OK(status)) {
5482 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr, num_sids);
5484 TALLOC_FREE(pdb_sids);
5485 return NT_STATUS_NO_MEMORY;
5489 for (i = 0; i < num_sids; i++) {
5490 sids[i].sid = dom_sid_dup(p->mem_ctx, &pdb_sids[i]);
5492 TALLOC_FREE(pdb_sids);
5493 return NT_STATUS_NO_MEMORY;
5497 r->out.sids->num_sids = num_sids;
5498 r->out.sids->sids = sids;
5500 TALLOC_FREE(pdb_sids);
5502 return NT_STATUS_OK;
5505 /*********************************************************************
5506 _samr_QueryGroupMember
5507 *********************************************************************/
5509 NTSTATUS _samr_QueryGroupMember(struct pipes_struct *p,
5510 struct samr_QueryGroupMember *r)
5512 struct samr_group_info *ginfo;
5513 size_t i, num_members;
5519 struct samr_RidAttrArray *rids = NULL;
5521 ginfo = policy_handle_find(p, r->in.group_handle,
5522 SAMR_GROUP_ACCESS_GET_MEMBERS, NULL,
5523 struct samr_group_info, &status);
5524 if (!NT_STATUS_IS_OK(status)) {
5528 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidAttrArray);
5530 return NT_STATUS_NO_MEMORY;
5533 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5535 if (!sid_check_is_in_our_domain(&ginfo->sid)) {
5536 DEBUG(3, ("sid %s is not in our domain\n",
5537 sid_string_dbg(&ginfo->sid)));
5538 return NT_STATUS_NO_SUCH_GROUP;
5541 DEBUG(10, ("lookup on Domain SID\n"));
5544 status = pdb_enum_group_members(p->mem_ctx, &ginfo->sid,
5545 &rid, &num_members);
5548 if (!NT_STATUS_IS_OK(status))
5552 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
5554 return NT_STATUS_NO_MEMORY;
5560 for (i=0; i<num_members; i++) {
5561 attr[i] = SE_GROUP_MANDATORY |
5562 SE_GROUP_ENABLED_BY_DEFAULT |
5566 rids->count = num_members;
5567 rids->attributes = attr;
5570 *r->out.rids = rids;
5572 return NT_STATUS_OK;
5575 /*********************************************************************
5576 _samr_AddAliasMember
5577 *********************************************************************/
5579 NTSTATUS _samr_AddAliasMember(struct pipes_struct *p,
5580 struct samr_AddAliasMember *r)
5582 struct samr_alias_info *ainfo;
5585 ainfo = policy_handle_find(p, r->in.alias_handle,
5586 SAMR_ALIAS_ACCESS_ADD_MEMBER, NULL,
5587 struct samr_alias_info, &status);
5588 if (!NT_STATUS_IS_OK(status)) {
5592 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5594 /******** BEGIN SeAddUsers BLOCK *********/
5597 status = pdb_add_aliasmem(&ainfo->sid, r->in.sid);
5600 /******** END SeAddUsers BLOCK *********/
5602 if (NT_STATUS_IS_OK(status)) {
5603 force_flush_samr_cache(&ainfo->sid);
5609 /*********************************************************************
5610 _samr_DeleteAliasMember
5611 *********************************************************************/
5613 NTSTATUS _samr_DeleteAliasMember(struct pipes_struct *p,
5614 struct samr_DeleteAliasMember *r)
5616 struct samr_alias_info *ainfo;
5619 ainfo = policy_handle_find(p, r->in.alias_handle,
5620 SAMR_ALIAS_ACCESS_REMOVE_MEMBER, NULL,
5621 struct samr_alias_info, &status);
5622 if (!NT_STATUS_IS_OK(status)) {
5626 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
5627 sid_string_dbg(&ainfo->sid)));
5629 /******** BEGIN SeAddUsers BLOCK *********/
5632 status = pdb_del_aliasmem(&ainfo->sid, r->in.sid);
5635 /******** END SeAddUsers BLOCK *********/
5637 if (NT_STATUS_IS_OK(status)) {
5638 force_flush_samr_cache(&ainfo->sid);
5644 /*********************************************************************
5645 _samr_AddGroupMember
5646 *********************************************************************/
5648 NTSTATUS _samr_AddGroupMember(struct pipes_struct *p,
5649 struct samr_AddGroupMember *r)
5651 struct samr_group_info *ginfo;
5655 ginfo = policy_handle_find(p, r->in.group_handle,
5656 SAMR_GROUP_ACCESS_ADD_MEMBER, NULL,
5657 struct samr_group_info, &status);
5658 if (!NT_STATUS_IS_OK(status)) {
5662 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5664 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5666 return NT_STATUS_INVALID_HANDLE;
5669 /******** BEGIN SeAddUsers BLOCK *********/
5672 status = pdb_add_groupmem(p->mem_ctx, group_rid, r->in.rid);
5675 /******** END SeAddUsers BLOCK *********/
5677 force_flush_samr_cache(&ginfo->sid);
5682 /*********************************************************************
5683 _samr_DeleteGroupMember
5684 *********************************************************************/
5686 NTSTATUS _samr_DeleteGroupMember(struct pipes_struct *p,
5687 struct samr_DeleteGroupMember *r)
5690 struct samr_group_info *ginfo;
5695 * delete the group member named r->in.rid
5696 * who is a member of the sid associated with the handle
5697 * the rid is a user's rid as the group is a domain group.
5700 ginfo = policy_handle_find(p, r->in.group_handle,
5701 SAMR_GROUP_ACCESS_REMOVE_MEMBER, NULL,
5702 struct samr_group_info, &status);
5703 if (!NT_STATUS_IS_OK(status)) {
5707 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5709 return NT_STATUS_INVALID_HANDLE;
5712 /******** BEGIN SeAddUsers BLOCK *********/
5715 status = pdb_del_groupmem(p->mem_ctx, group_rid, r->in.rid);
5718 /******** END SeAddUsers BLOCK *********/
5720 force_flush_samr_cache(&ginfo->sid);
5725 /*********************************************************************
5727 *********************************************************************/
5729 NTSTATUS _samr_DeleteUser(struct pipes_struct *p,
5730 struct samr_DeleteUser *r)
5732 struct samr_user_info *uinfo;
5734 struct samu *sam_pass=NULL;
5737 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__));
5739 uinfo = policy_handle_find(p, r->in.user_handle,
5740 SEC_STD_DELETE, NULL,
5741 struct samr_user_info, &status);
5742 if (!NT_STATUS_IS_OK(status)) {
5746 if (!sid_check_is_in_our_domain(&uinfo->sid))
5747 return NT_STATUS_CANNOT_DELETE;
5749 /* check if the user exists before trying to delete */
5750 if ( !(sam_pass = samu_new( NULL )) ) {
5751 return NT_STATUS_NO_MEMORY;
5755 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
5759 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
5760 sid_string_dbg(&uinfo->sid)));
5761 TALLOC_FREE(sam_pass);
5762 return NT_STATUS_NO_SUCH_USER;
5765 /******** BEGIN SeAddUsers BLOCK *********/
5768 status = pdb_delete_user(p->mem_ctx, sam_pass);
5771 /******** END SeAddUsers BLOCK *********/
5773 if ( !NT_STATUS_IS_OK(status) ) {
5774 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
5775 "user %s: %s.\n", pdb_get_username(sam_pass),
5776 nt_errstr(status)));
5777 TALLOC_FREE(sam_pass);
5782 TALLOC_FREE(sam_pass);
5784 force_flush_samr_cache(&uinfo->sid);
5786 if (!close_policy_hnd(p, r->in.user_handle))
5787 return NT_STATUS_OBJECT_NAME_INVALID;
5789 ZERO_STRUCTP(r->out.user_handle);
5791 return NT_STATUS_OK;
5794 /*********************************************************************
5795 _samr_DeleteDomainGroup
5796 *********************************************************************/
5798 NTSTATUS _samr_DeleteDomainGroup(struct pipes_struct *p,
5799 struct samr_DeleteDomainGroup *r)
5801 struct samr_group_info *ginfo;
5805 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__));
5807 ginfo = policy_handle_find(p, r->in.group_handle,
5808 SEC_STD_DELETE, NULL,
5809 struct samr_group_info, &status);
5810 if (!NT_STATUS_IS_OK(status)) {
5814 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5816 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5818 return NT_STATUS_NO_SUCH_GROUP;
5821 /******** BEGIN SeAddUsers BLOCK *********/
5824 status = pdb_delete_dom_group(p->mem_ctx, group_rid);
5827 /******** END SeAddUsers BLOCK *********/
5829 if ( !NT_STATUS_IS_OK(status) ) {
5830 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
5831 "entry for group %s: %s\n",
5832 sid_string_dbg(&ginfo->sid),
5833 nt_errstr(status)));
5837 force_flush_samr_cache(&ginfo->sid);
5839 if (!close_policy_hnd(p, r->in.group_handle))
5840 return NT_STATUS_OBJECT_NAME_INVALID;
5842 return NT_STATUS_OK;
5845 /*********************************************************************
5846 _samr_DeleteDomAlias
5847 *********************************************************************/
5849 NTSTATUS _samr_DeleteDomAlias(struct pipes_struct *p,
5850 struct samr_DeleteDomAlias *r)
5852 struct samr_alias_info *ainfo;
5855 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__));
5857 ainfo = policy_handle_find(p, r->in.alias_handle,
5858 SEC_STD_DELETE, NULL,
5859 struct samr_alias_info, &status);
5860 if (!NT_STATUS_IS_OK(status)) {
5864 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5866 /* Don't let Windows delete builtin groups */
5868 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
5869 return NT_STATUS_SPECIAL_ACCOUNT;
5872 if (!sid_check_is_in_our_domain(&ainfo->sid))
5873 return NT_STATUS_NO_SUCH_ALIAS;
5875 DEBUG(10, ("lookup on Local SID\n"));
5877 /******** BEGIN SeAddUsers BLOCK *********/
5880 /* Have passdb delete the alias */
5881 status = pdb_delete_alias(&ainfo->sid);
5884 /******** END SeAddUsers BLOCK *********/
5886 if ( !NT_STATUS_IS_OK(status))
5889 force_flush_samr_cache(&ainfo->sid);
5891 if (!close_policy_hnd(p, r->in.alias_handle))
5892 return NT_STATUS_OBJECT_NAME_INVALID;
5894 return NT_STATUS_OK;
5897 /*********************************************************************
5898 _samr_CreateDomainGroup
5899 *********************************************************************/
5901 NTSTATUS _samr_CreateDomainGroup(struct pipes_struct *p,
5902 struct samr_CreateDomainGroup *r)
5907 struct samr_domain_info *dinfo;
5908 struct samr_group_info *ginfo;
5910 dinfo = policy_handle_find(p, r->in.domain_handle,
5911 SAMR_DOMAIN_ACCESS_CREATE_GROUP, NULL,
5912 struct samr_domain_info, &status);
5913 if (!NT_STATUS_IS_OK(status)) {
5917 if (!sid_check_is_domain(&dinfo->sid)) {
5918 return NT_STATUS_ACCESS_DENIED;
5921 name = r->in.name->string;
5923 return NT_STATUS_NO_MEMORY;
5926 status = can_create(p->mem_ctx, name);
5927 if (!NT_STATUS_IS_OK(status)) {
5931 /******** BEGIN SeAddUsers BLOCK *********/
5934 /* check that we successfully create the UNIX group */
5935 status = pdb_create_dom_group(p->mem_ctx, name, r->out.rid);
5938 /******** END SeAddUsers BLOCK *********/
5940 /* check if we should bail out here */
5942 if ( !NT_STATUS_IS_OK(status) )
5945 ginfo = policy_handle_create(p, r->out.group_handle,
5946 GENERIC_RIGHTS_GROUP_ALL_ACCESS,
5947 struct samr_group_info, &status);
5948 if (!NT_STATUS_IS_OK(status)) {
5951 sid_compose(&ginfo->sid, &dinfo->sid, *r->out.rid);
5953 force_flush_samr_cache(&dinfo->sid);
5955 return NT_STATUS_OK;
5958 /*********************************************************************
5959 _samr_CreateDomAlias
5960 *********************************************************************/
5962 NTSTATUS _samr_CreateDomAlias(struct pipes_struct *p,
5963 struct samr_CreateDomAlias *r)
5965 struct dom_sid info_sid;
5966 const char *name = NULL;
5967 struct samr_domain_info *dinfo;
5968 struct samr_alias_info *ainfo;
5972 dinfo = policy_handle_find(p, r->in.domain_handle,
5973 SAMR_DOMAIN_ACCESS_CREATE_ALIAS, NULL,
5974 struct samr_domain_info, &result);
5975 if (!NT_STATUS_IS_OK(result)) {
5979 if (!sid_check_is_domain(&dinfo->sid)) {
5980 return NT_STATUS_ACCESS_DENIED;
5983 name = r->in.alias_name->string;
5985 result = can_create(p->mem_ctx, name);
5986 if (!NT_STATUS_IS_OK(result)) {
5990 /******** BEGIN SeAddUsers BLOCK *********/
5993 /* Have passdb create the alias */
5994 result = pdb_create_alias(name, r->out.rid);
5997 /******** END SeAddUsers BLOCK *********/
5999 if (!NT_STATUS_IS_OK(result)) {
6000 DEBUG(10, ("pdb_create_alias failed: %s\n",
6001 nt_errstr(result)));
6005 sid_compose(&info_sid, &dinfo->sid, *r->out.rid);
6007 if (!sid_to_gid(&info_sid, &gid)) {
6008 DEBUG(10, ("Could not find alias just created\n"));
6009 return NT_STATUS_ACCESS_DENIED;
6012 /* check if the group has been successfully created */
6013 if ( getgrgid(gid) == NULL ) {
6014 DEBUG(10, ("getgrgid(%u) of just created alias failed\n",
6015 (unsigned int)gid));
6016 return NT_STATUS_ACCESS_DENIED;
6019 ainfo = policy_handle_create(p, r->out.alias_handle,
6020 GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
6021 struct samr_alias_info, &result);
6022 if (!NT_STATUS_IS_OK(result)) {
6025 ainfo->sid = info_sid;
6027 force_flush_samr_cache(&info_sid);
6029 return NT_STATUS_OK;
6032 /*********************************************************************
6033 _samr_QueryGroupInfo
6034 *********************************************************************/
6036 NTSTATUS _samr_QueryGroupInfo(struct pipes_struct *p,
6037 struct samr_QueryGroupInfo *r)
6039 struct samr_group_info *ginfo;
6042 union samr_GroupInfo *info = NULL;
6044 uint32_t attributes = SE_GROUP_MANDATORY |
6045 SE_GROUP_ENABLED_BY_DEFAULT |
6047 const char *group_name = NULL;
6048 const char *group_description = NULL;
6050 ginfo = policy_handle_find(p, r->in.group_handle,
6051 SAMR_GROUP_ACCESS_LOOKUP_INFO, NULL,
6052 struct samr_group_info, &status);
6053 if (!NT_STATUS_IS_OK(status)) {
6058 ret = get_domain_group_from_sid(ginfo->sid, &map);
6061 return NT_STATUS_INVALID_HANDLE;
6063 /* FIXME: map contains fstrings */
6064 group_name = talloc_strdup(r, map.nt_name);
6065 group_description = talloc_strdup(r, map.comment);
6067 info = TALLOC_ZERO_P(p->mem_ctx, union samr_GroupInfo);
6069 return NT_STATUS_NO_MEMORY;
6072 switch (r->in.level) {
6078 status = pdb_enum_group_members(
6079 p->mem_ctx, &ginfo->sid, &members,
6083 if (!NT_STATUS_IS_OK(status)) {
6087 info->all.name.string = group_name;
6088 info->all.attributes = attributes;
6089 info->all.num_members = num_members;
6090 info->all.description.string = group_description;
6094 info->name.string = group_name;
6097 info->attributes.attributes = attributes;
6100 info->description.string = group_description;
6110 status = pdb_enum_group_members(
6111 p->mem_ctx, &ginfo->sid, &members,
6115 if (!NT_STATUS_IS_OK(status)) {
6119 info->all2.name.string = group_name;
6120 info->all2.attributes = attributes;
6121 info->all2.num_members = 0; /* num_members - in w2k3 this is always 0 */
6122 info->all2.description.string = group_description;
6127 return NT_STATUS_INVALID_INFO_CLASS;
6130 *r->out.info = info;
6132 return NT_STATUS_OK;
6135 /*********************************************************************
6137 *********************************************************************/
6139 NTSTATUS _samr_SetGroupInfo(struct pipes_struct *p,
6140 struct samr_SetGroupInfo *r)
6142 struct samr_group_info *ginfo;
6147 ginfo = policy_handle_find(p, r->in.group_handle,
6148 SAMR_GROUP_ACCESS_SET_INFO, NULL,
6149 struct samr_group_info, &status);
6150 if (!NT_STATUS_IS_OK(status)) {
6155 ret = get_domain_group_from_sid(ginfo->sid, &map);
6158 return NT_STATUS_NO_SUCH_GROUP;
6160 switch (r->in.level) {
6162 fstrcpy(map.nt_name, r->in.info->name.string);
6167 fstrcpy(map.comment, r->in.info->description.string);
6170 return NT_STATUS_INVALID_INFO_CLASS;
6173 /******** BEGIN SeAddUsers BLOCK *********/
6176 status = pdb_update_group_mapping_entry(&map);
6179 /******** End SeAddUsers BLOCK *********/
6181 if (NT_STATUS_IS_OK(status)) {
6182 force_flush_samr_cache(&ginfo->sid);
6188 /*********************************************************************
6190 *********************************************************************/
6192 NTSTATUS _samr_SetAliasInfo(struct pipes_struct *p,
6193 struct samr_SetAliasInfo *r)
6195 struct samr_alias_info *ainfo;
6196 struct acct_info info;
6199 ainfo = policy_handle_find(p, r->in.alias_handle,
6200 SAMR_ALIAS_ACCESS_SET_INFO, NULL,
6201 struct samr_alias_info, &status);
6202 if (!NT_STATUS_IS_OK(status)) {
6206 /* get the current group information */
6209 status = pdb_get_aliasinfo( &ainfo->sid, &info );
6212 if ( !NT_STATUS_IS_OK(status))
6215 switch (r->in.level) {
6220 /* We currently do not support renaming groups in the
6221 the BUILTIN domain. Refer to util_builtin.c to understand
6222 why. The eventually needs to be fixed to be like Windows
6223 where you can rename builtin groups, just not delete them */
6225 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
6226 return NT_STATUS_SPECIAL_ACCOUNT;
6229 /* There has to be a valid name (and it has to be different) */
6231 if ( !r->in.info->name.string )
6232 return NT_STATUS_INVALID_PARAMETER;
6234 /* If the name is the same just reply "ok". Yes this
6235 doesn't allow you to change the case of a group name. */
6237 if ( strequal( r->in.info->name.string, info.acct_name ) )
6238 return NT_STATUS_OK;
6240 fstrcpy( info.acct_name, r->in.info->name.string);
6242 /* make sure the name doesn't already exist as a user
6245 fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
6246 status = can_create( p->mem_ctx, group_name );
6247 if ( !NT_STATUS_IS_OK( status ) )
6251 case ALIASINFODESCRIPTION:
6252 if (r->in.info->description.string) {
6253 fstrcpy(info.acct_desc,
6254 r->in.info->description.string);
6256 fstrcpy( info.acct_desc, "" );
6260 return NT_STATUS_INVALID_INFO_CLASS;
6263 /******** BEGIN SeAddUsers BLOCK *********/
6266 status = pdb_set_aliasinfo( &ainfo->sid, &info );
6269 /******** End SeAddUsers BLOCK *********/
6271 if (NT_STATUS_IS_OK(status))
6272 force_flush_samr_cache(&ainfo->sid);
6277 /****************************************************************
6279 ****************************************************************/
6281 NTSTATUS _samr_GetDomPwInfo(struct pipes_struct *p,
6282 struct samr_GetDomPwInfo *r)
6284 uint32_t min_password_length = 0;
6285 uint32_t password_properties = 0;
6287 /* Perform access check. Since this rpc does not require a
6288 policy handle it will not be caught by the access checks on
6289 SAMR_CONNECT or SAMR_CONNECT_ANON. */
6291 if (!pipe_access_check(p)) {
6292 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
6293 return NT_STATUS_ACCESS_DENIED;
6297 pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
6298 &min_password_length);
6299 pdb_get_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
6300 &password_properties);
6303 if (lp_check_password_script() && *lp_check_password_script()) {
6304 password_properties |= DOMAIN_PASSWORD_COMPLEX;
6307 r->out.info->min_password_length = min_password_length;
6308 r->out.info->password_properties = password_properties;
6310 return NT_STATUS_OK;
6313 /*********************************************************************
6315 *********************************************************************/
6317 NTSTATUS _samr_OpenGroup(struct pipes_struct *p,
6318 struct samr_OpenGroup *r)
6321 struct dom_sid info_sid;
6323 struct samr_domain_info *dinfo;
6324 struct samr_group_info *ginfo;
6325 struct security_descriptor *psd = NULL;
6327 uint32 des_access = r->in.access_mask;
6332 dinfo = policy_handle_find(p, r->in.domain_handle,
6333 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
6334 struct samr_domain_info, &status);
6335 if (!NT_STATUS_IS_OK(status)) {
6339 /*check if access can be granted as requested by client. */
6340 map_max_allowed_access(p->session_info->security_token,
6341 &p->session_info->utok,
6344 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
6345 se_map_generic(&des_access,&grp_generic_mapping);
6347 status = access_check_object(psd, p->session_info->security_token,
6348 SEC_PRIV_ADD_USERS, SEC_PRIV_INVALID, GENERIC_RIGHTS_GROUP_ALL_ACCESS,
6349 des_access, &acc_granted, "_samr_OpenGroup");
6351 if ( !NT_STATUS_IS_OK(status) )
6354 /* this should not be hard-coded like this */
6356 if (!sid_check_is_domain(&dinfo->sid)) {
6357 return NT_STATUS_ACCESS_DENIED;
6360 sid_compose(&info_sid, &dinfo->sid, r->in.rid);
6362 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n",
6363 sid_string_dbg(&info_sid)));
6365 /* check if that group really exists */
6367 ret = get_domain_group_from_sid(info_sid, &map);
6370 return NT_STATUS_NO_SUCH_GROUP;
6372 ginfo = policy_handle_create(p, r->out.group_handle,
6374 struct samr_group_info, &status);
6375 if (!NT_STATUS_IS_OK(status)) {
6378 ginfo->sid = info_sid;
6380 return NT_STATUS_OK;
6383 /*********************************************************************
6384 _samr_RemoveMemberFromForeignDomain
6385 *********************************************************************/
6387 NTSTATUS _samr_RemoveMemberFromForeignDomain(struct pipes_struct *p,
6388 struct samr_RemoveMemberFromForeignDomain *r)
6390 struct samr_domain_info *dinfo;
6393 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
6394 sid_string_dbg(r->in.sid)));
6396 /* Find the policy handle. Open a policy on it. */
6398 dinfo = policy_handle_find(p, r->in.domain_handle,
6399 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
6400 struct samr_domain_info, &result);
6401 if (!NT_STATUS_IS_OK(result)) {
6405 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
6406 sid_string_dbg(&dinfo->sid)));
6408 /* we can only delete a user from a group since we don't have
6409 nested groups anyways. So in the latter case, just say OK */
6411 /* TODO: The above comment nowadays is bogus. Since we have nested
6412 * groups now, and aliases members are never reported out of the unix
6413 * group membership, the "just say OK" makes this call a no-op. For
6414 * us. This needs fixing however. */
6416 /* I've only ever seen this in the wild when deleting a user from
6417 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
6418 * is the user about to be deleted. I very much suspect this is the
6419 * only application of this call. To verify this, let people report
6422 if (!sid_check_is_builtin(&dinfo->sid)) {
6423 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
6424 "global_sam_sid() = %s\n",
6425 sid_string_dbg(&dinfo->sid),
6426 sid_string_dbg(get_global_sam_sid())));
6427 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
6428 return NT_STATUS_OK;
6431 force_flush_samr_cache(&dinfo->sid);
6433 result = NT_STATUS_OK;
6438 /*******************************************************************
6439 _samr_QueryDomainInfo2
6440 ********************************************************************/
6442 NTSTATUS _samr_QueryDomainInfo2(struct pipes_struct *p,
6443 struct samr_QueryDomainInfo2 *r)
6445 struct samr_QueryDomainInfo q;
6447 q.in.domain_handle = r->in.domain_handle;
6448 q.in.level = r->in.level;
6450 q.out.info = r->out.info;
6452 return _samr_QueryDomainInfo(p, &q);
6455 /*******************************************************************
6456 ********************************************************************/
6458 static NTSTATUS set_dom_info_1(TALLOC_CTX *mem_ctx,
6459 struct samr_DomInfo1 *r)
6461 time_t u_expire, u_min_age;
6463 u_expire = nt_time_to_unix_abs((NTTIME *)&r->max_password_age);
6464 u_min_age = nt_time_to_unix_abs((NTTIME *)&r->min_password_age);
6466 pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_LEN,
6467 (uint32_t)r->min_password_length);
6468 pdb_set_account_policy(PDB_POLICY_PASSWORD_HISTORY,
6469 (uint32_t)r->password_history_length);
6470 pdb_set_account_policy(PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS,
6471 (uint32_t)r->password_properties);
6472 pdb_set_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, (int)u_expire);
6473 pdb_set_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, (int)u_min_age);
6475 return NT_STATUS_OK;
6478 /*******************************************************************
6479 ********************************************************************/
6481 static NTSTATUS set_dom_info_3(TALLOC_CTX *mem_ctx,
6482 struct samr_DomInfo3 *r)
6486 u_logout = nt_time_to_unix_abs((NTTIME *)&r->force_logoff_time);
6488 pdb_set_account_policy(PDB_POLICY_TIME_TO_LOGOUT, (int)u_logout);
6490 return NT_STATUS_OK;
6493 /*******************************************************************
6494 ********************************************************************/
6496 static NTSTATUS set_dom_info_12(TALLOC_CTX *mem_ctx,
6497 struct samr_DomInfo12 *r)
6499 time_t u_lock_duration, u_reset_time;
6501 u_lock_duration = nt_time_to_unix_abs((NTTIME *)&r->lockout_duration);
6502 if (u_lock_duration != -1) {
6503 u_lock_duration /= 60;
6506 u_reset_time = nt_time_to_unix_abs((NTTIME *)&r->lockout_window)/60;
6508 pdb_set_account_policy(PDB_POLICY_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
6509 pdb_set_account_policy(PDB_POLICY_RESET_COUNT_TIME, (int)u_reset_time);
6510 pdb_set_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT,
6511 (uint32_t)r->lockout_threshold);
6513 return NT_STATUS_OK;
6516 /*******************************************************************
6518 ********************************************************************/
6520 NTSTATUS _samr_SetDomainInfo(struct pipes_struct *p,
6521 struct samr_SetDomainInfo *r)
6523 struct samr_domain_info *dinfo;
6525 uint32_t acc_required = 0;
6527 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
6529 switch (r->in.level) {
6530 case 1: /* DomainPasswordInformation */
6531 case 12: /* DomainLockoutInformation */
6532 /* DOMAIN_WRITE_PASSWORD_PARAMETERS */
6533 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_1;
6535 case 3: /* DomainLogoffInformation */
6536 case 4: /* DomainOemInformation */
6537 /* DOMAIN_WRITE_OTHER_PARAMETERS */
6538 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_2;
6540 case 6: /* DomainReplicationInformation */
6541 case 9: /* DomainStateInformation */
6542 case 7: /* DomainServerRoleInformation */
6543 /* DOMAIN_ADMINISTER_SERVER */
6544 acc_required = SAMR_DOMAIN_ACCESS_SET_INFO_3;
6547 return NT_STATUS_INVALID_INFO_CLASS;
6550 dinfo = policy_handle_find(p, r->in.domain_handle,
6552 struct samr_domain_info, &status);
6553 if (!NT_STATUS_IS_OK(status)) {
6557 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r->in.level));
6559 switch (r->in.level) {
6561 status = set_dom_info_1(p->mem_ctx, &r->in.info->info1);
6564 status = set_dom_info_3(p->mem_ctx, &r->in.info->info3);
6575 status = set_dom_info_12(p->mem_ctx, &r->in.info->info12);
6578 return NT_STATUS_INVALID_INFO_CLASS;
6581 if (!NT_STATUS_IS_OK(status)) {
6585 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
6587 return NT_STATUS_OK;
6590 /****************************************************************
6591 _samr_GetDisplayEnumerationIndex
6592 ****************************************************************/
6594 NTSTATUS _samr_GetDisplayEnumerationIndex(struct pipes_struct *p,
6595 struct samr_GetDisplayEnumerationIndex *r)
6597 struct samr_domain_info *dinfo;
6598 uint32_t max_entries = (uint32_t) -1;
6599 uint32_t enum_context = 0;
6601 uint32_t num_account = 0;
6602 struct samr_displayentry *entries = NULL;
6605 DEBUG(5,("_samr_GetDisplayEnumerationIndex: %d\n", __LINE__));
6607 dinfo = policy_handle_find(p, r->in.domain_handle,
6608 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
6609 struct samr_domain_info, &status);
6610 if (!NT_STATUS_IS_OK(status)) {
6614 if ((r->in.level < 1) || (r->in.level > 3)) {
6615 DEBUG(0,("_samr_GetDisplayEnumerationIndex: "
6616 "Unknown info level (%u)\n",
6618 return NT_STATUS_INVALID_INFO_CLASS;
6623 /* The following done as ROOT. Don't return without unbecome_root(). */
6625 switch (r->in.level) {
6627 if (dinfo->disp_info->users == NULL) {
6628 dinfo->disp_info->users = pdb_search_users(
6629 dinfo->disp_info, ACB_NORMAL);
6630 if (dinfo->disp_info->users == NULL) {
6632 return NT_STATUS_ACCESS_DENIED;
6634 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6635 "starting user enumeration at index %u\n",
6636 (unsigned int)enum_context));
6638 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6639 "using cached user enumeration at index %u\n",
6640 (unsigned int)enum_context));
6642 num_account = pdb_search_entries(dinfo->disp_info->users,
6643 enum_context, max_entries,
6647 if (dinfo->disp_info->machines == NULL) {
6648 dinfo->disp_info->machines = pdb_search_users(
6649 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
6650 if (dinfo->disp_info->machines == NULL) {
6652 return NT_STATUS_ACCESS_DENIED;
6654 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6655 "starting machine enumeration at index %u\n",
6656 (unsigned int)enum_context));
6658 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6659 "using cached machine enumeration at index %u\n",
6660 (unsigned int)enum_context));
6662 num_account = pdb_search_entries(dinfo->disp_info->machines,
6663 enum_context, max_entries,
6667 if (dinfo->disp_info->groups == NULL) {
6668 dinfo->disp_info->groups = pdb_search_groups(
6670 if (dinfo->disp_info->groups == NULL) {
6672 return NT_STATUS_ACCESS_DENIED;
6674 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6675 "starting group enumeration at index %u\n",
6676 (unsigned int)enum_context));
6678 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6679 "using cached group enumeration at index %u\n",
6680 (unsigned int)enum_context));
6682 num_account = pdb_search_entries(dinfo->disp_info->groups,
6683 enum_context, max_entries,
6688 smb_panic("info class changed");
6694 /* Ensure we cache this enumeration. */
6695 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
6697 DEBUG(10,("_samr_GetDisplayEnumerationIndex: looking for :%s\n",
6698 r->in.name->string));
6700 for (i=0; i<num_account; i++) {
6701 if (strequal(entries[i].account_name, r->in.name->string)) {
6702 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6703 "found %s at idx %d\n",
6704 r->in.name->string, i));
6706 return NT_STATUS_OK;
6710 /* assuming account_name lives at the very end */
6711 *r->out.idx = num_account;
6713 return NT_STATUS_NO_MORE_ENTRIES;
6716 /****************************************************************
6717 _samr_GetDisplayEnumerationIndex2
6718 ****************************************************************/
6720 NTSTATUS _samr_GetDisplayEnumerationIndex2(struct pipes_struct *p,
6721 struct samr_GetDisplayEnumerationIndex2 *r)
6723 struct samr_GetDisplayEnumerationIndex q;
6725 q.in.domain_handle = r->in.domain_handle;
6726 q.in.level = r->in.level;
6727 q.in.name = r->in.name;
6729 q.out.idx = r->out.idx;
6731 return _samr_GetDisplayEnumerationIndex(p, &q);
6734 /****************************************************************
6736 ****************************************************************/
6738 NTSTATUS _samr_RidToSid(struct pipes_struct *p,
6739 struct samr_RidToSid *r)
6741 struct samr_domain_info *dinfo;
6745 dinfo = policy_handle_find(p, r->in.domain_handle,
6747 struct samr_domain_info, &status);
6748 if (!NT_STATUS_IS_OK(status)) {
6752 if (!sid_compose(&sid, &dinfo->sid, r->in.rid)) {
6753 return NT_STATUS_NO_MEMORY;
6756 *r->out.sid = dom_sid_dup(p->mem_ctx, &sid);
6758 return NT_STATUS_NO_MEMORY;
6761 return NT_STATUS_OK;
6764 /****************************************************************
6765 ****************************************************************/
6767 static enum samr_ValidationStatus samr_ValidatePassword_Change(TALLOC_CTX *mem_ctx,
6768 const struct samr_PwInfo *dom_pw_info,
6769 const struct samr_ValidatePasswordReq2 *req,
6770 struct samr_ValidatePasswordRepCtr *rep)
6774 if (req->password.string == NULL) {
6775 return SAMR_VALIDATION_STATUS_SUCCESS;
6777 if (strlen(req->password.string) < dom_pw_info->min_password_length) {
6778 ZERO_STRUCT(rep->info);
6779 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
6781 if (dom_pw_info->password_properties & DOMAIN_PASSWORD_COMPLEX) {
6782 status = check_password_complexity(req->account.string,
6783 req->password.string,
6785 if (!NT_STATUS_IS_OK(status)) {
6786 ZERO_STRUCT(rep->info);
6787 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
6791 return SAMR_VALIDATION_STATUS_SUCCESS;
6794 /****************************************************************
6795 ****************************************************************/
6797 static enum samr_ValidationStatus samr_ValidatePassword_Reset(TALLOC_CTX *mem_ctx,
6798 const struct samr_PwInfo *dom_pw_info,
6799 const struct samr_ValidatePasswordReq3 *req,
6800 struct samr_ValidatePasswordRepCtr *rep)
6804 if (req->password.string == NULL) {
6805 return SAMR_VALIDATION_STATUS_SUCCESS;
6807 if (strlen(req->password.string) < dom_pw_info->min_password_length) {
6808 ZERO_STRUCT(rep->info);
6809 return SAMR_VALIDATION_STATUS_PWD_TOO_SHORT;
6811 if (dom_pw_info->password_properties & DOMAIN_PASSWORD_COMPLEX) {
6812 status = check_password_complexity(req->account.string,
6813 req->password.string,
6815 if (!NT_STATUS_IS_OK(status)) {
6816 ZERO_STRUCT(rep->info);
6817 return SAMR_VALIDATION_STATUS_NOT_COMPLEX_ENOUGH;
6821 return SAMR_VALIDATION_STATUS_SUCCESS;
6824 /****************************************************************
6825 _samr_ValidatePassword
6826 ****************************************************************/
6828 NTSTATUS _samr_ValidatePassword(struct pipes_struct *p,
6829 struct samr_ValidatePassword *r)
6831 union samr_ValidatePasswordRep *rep;
6833 struct samr_GetDomPwInfo pw;
6834 struct samr_PwInfo dom_pw_info;
6836 if (r->in.level < 1 || r->in.level > 3) {
6837 return NT_STATUS_INVALID_INFO_CLASS;
6840 pw.in.domain_name = NULL;
6841 pw.out.info = &dom_pw_info;
6843 status = _samr_GetDomPwInfo(p, &pw);
6844 if (!NT_STATUS_IS_OK(status)) {
6848 rep = talloc_zero(p->mem_ctx, union samr_ValidatePasswordRep);
6850 return NT_STATUS_NO_MEMORY;
6853 switch (r->in.level) {
6855 status = NT_STATUS_NOT_SUPPORTED;
6858 rep->ctr2.status = samr_ValidatePassword_Change(p->mem_ctx,
6864 rep->ctr3.status = samr_ValidatePassword_Reset(p->mem_ctx,
6870 status = NT_STATUS_INVALID_INFO_CLASS;
6874 if (!NT_STATUS_IS_OK(status)) {
6881 return NT_STATUS_OK;
6884 /****************************************************************
6885 ****************************************************************/
6887 NTSTATUS _samr_Shutdown(struct pipes_struct *p,
6888 struct samr_Shutdown *r)
6890 p->rng_fault_state = true;
6891 return NT_STATUS_NOT_IMPLEMENTED;
6894 /****************************************************************
6895 ****************************************************************/
6897 NTSTATUS _samr_SetMemberAttributesOfGroup(struct pipes_struct *p,
6898 struct samr_SetMemberAttributesOfGroup *r)
6900 p->rng_fault_state = true;
6901 return NT_STATUS_NOT_IMPLEMENTED;
6904 /****************************************************************
6905 ****************************************************************/
6907 NTSTATUS _samr_TestPrivateFunctionsDomain(struct pipes_struct *p,
6908 struct samr_TestPrivateFunctionsDomain *r)
6910 return NT_STATUS_NOT_IMPLEMENTED;
6913 /****************************************************************
6914 ****************************************************************/
6916 NTSTATUS _samr_TestPrivateFunctionsUser(struct pipes_struct *p,
6917 struct samr_TestPrivateFunctionsUser *r)
6919 return NT_STATUS_NOT_IMPLEMENTED;
6922 /****************************************************************
6923 ****************************************************************/
6925 NTSTATUS _samr_AddMultipleMembersToAlias(struct pipes_struct *p,
6926 struct samr_AddMultipleMembersToAlias *r)
6928 p->rng_fault_state = true;
6929 return NT_STATUS_NOT_IMPLEMENTED;
6932 /****************************************************************
6933 ****************************************************************/
6935 NTSTATUS _samr_RemoveMultipleMembersFromAlias(struct pipes_struct *p,
6936 struct samr_RemoveMultipleMembersFromAlias *r)
6938 p->rng_fault_state = true;
6939 return NT_STATUS_NOT_IMPLEMENTED;
6942 /****************************************************************
6943 ****************************************************************/
6945 NTSTATUS _samr_SetBootKeyInformation(struct pipes_struct *p,
6946 struct samr_SetBootKeyInformation *r)
6948 p->rng_fault_state = true;
6949 return NT_STATUS_NOT_IMPLEMENTED;
6952 /****************************************************************
6953 ****************************************************************/
6955 NTSTATUS _samr_GetBootKeyInformation(struct pipes_struct *p,
6956 struct samr_GetBootKeyInformation *r)
6958 p->rng_fault_state = true;
6959 return NT_STATUS_NOT_IMPLEMENTED;
6962 /****************************************************************
6963 ****************************************************************/
6965 NTSTATUS _samr_SetDsrmPassword(struct pipes_struct *p,
6966 struct samr_SetDsrmPassword *r)
6968 p->rng_fault_state = true;
6969 return NT_STATUS_NOT_IMPLEMENTED;