2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Paul Ashton 1997,
7 * Copyright (C) Marc Jacobsen 1999,
8 * Copyright (C) Jeremy Allison 2001-2008,
9 * Copyright (C) Jean François Micouleau 1998-2001,
10 * Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002,
11 * Copyright (C) Gerald (Jerry) Carter 2003-2004,
12 * Copyright (C) Simo Sorce 2003.
13 * Copyright (C) Volker Lendecke 2005.
14 * Copyright (C) Guenther Deschner 2008.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 * This is the implementation of the SAMR code.
35 #include "../libcli/auth/libcli_auth.h"
38 #define DBGC_CLASS DBGC_RPC_SRV
40 #define SAMR_USR_RIGHTS_WRITE_PW \
41 ( READ_CONTROL_ACCESS | \
42 SAMR_USER_ACCESS_CHANGE_PASSWORD | \
43 SAMR_USER_ACCESS_SET_LOC_COM)
44 #define SAMR_USR_RIGHTS_CANT_WRITE_PW \
45 ( READ_CONTROL_ACCESS | SAMR_USER_ACCESS_SET_LOC_COM )
47 #define DISP_INFO_CACHE_TIMEOUT 10
49 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
50 #define MAX_SAM_ENTRIES_W95 50
52 struct samr_connect_info {
56 struct samr_domain_info {
58 struct disp_info *disp_info;
61 struct samr_user_info {
65 struct samr_group_info {
69 struct samr_alias_info {
73 typedef struct disp_info {
74 DOM_SID sid; /* identify which domain this is. */
75 struct pdb_search *users; /* querydispinfo 1 and 4 */
76 struct pdb_search *machines; /* querydispinfo 2 */
77 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
78 struct pdb_search *aliases; /* enumaliases */
81 struct pdb_search *enum_users; /* enumusers with a mask */
83 struct timed_event *cache_timeout_event; /* cache idle timeout
87 static const struct generic_mapping sam_generic_mapping = {
88 GENERIC_RIGHTS_SAM_READ,
89 GENERIC_RIGHTS_SAM_WRITE,
90 GENERIC_RIGHTS_SAM_EXECUTE,
91 GENERIC_RIGHTS_SAM_ALL_ACCESS};
92 static const struct generic_mapping dom_generic_mapping = {
93 GENERIC_RIGHTS_DOMAIN_READ,
94 GENERIC_RIGHTS_DOMAIN_WRITE,
95 GENERIC_RIGHTS_DOMAIN_EXECUTE,
96 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
97 static const struct generic_mapping usr_generic_mapping = {
98 GENERIC_RIGHTS_USER_READ,
99 GENERIC_RIGHTS_USER_WRITE,
100 GENERIC_RIGHTS_USER_EXECUTE,
101 GENERIC_RIGHTS_USER_ALL_ACCESS};
102 static const struct generic_mapping usr_nopwchange_generic_mapping = {
103 GENERIC_RIGHTS_USER_READ,
104 GENERIC_RIGHTS_USER_WRITE,
105 GENERIC_RIGHTS_USER_EXECUTE & ~SAMR_USER_ACCESS_CHANGE_PASSWORD,
106 GENERIC_RIGHTS_USER_ALL_ACCESS};
107 static const struct generic_mapping grp_generic_mapping = {
108 GENERIC_RIGHTS_GROUP_READ,
109 GENERIC_RIGHTS_GROUP_WRITE,
110 GENERIC_RIGHTS_GROUP_EXECUTE,
111 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
112 static const struct generic_mapping ali_generic_mapping = {
113 GENERIC_RIGHTS_ALIAS_READ,
114 GENERIC_RIGHTS_ALIAS_WRITE,
115 GENERIC_RIGHTS_ALIAS_EXECUTE,
116 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
118 /*******************************************************************
119 *******************************************************************/
121 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
122 const struct generic_mapping *map,
123 DOM_SID *sid, uint32 sid_access )
125 DOM_SID domadmin_sid;
126 SEC_ACE ace[5]; /* at most 5 entries */
131 /* basic access for Everyone */
133 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED,
134 map->generic_execute | map->generic_read, 0);
136 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
138 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
139 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
140 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators,
141 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
143 /* Add Full Access for Domain Admins if we are a DC */
146 sid_copy( &domadmin_sid, get_global_sam_sid() );
147 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
148 init_sec_ace(&ace[i++], &domadmin_sid,
149 SEC_ACE_TYPE_ACCESS_ALLOWED, map->generic_all, 0);
152 /* if we have a sid, give it some special access */
155 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sid_access, 0);
158 /* create the security descriptor */
160 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
161 return NT_STATUS_NO_MEMORY;
163 if ((*psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
164 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
165 psa, sd_size)) == NULL)
166 return NT_STATUS_NO_MEMORY;
171 /*******************************************************************
172 Checks if access to an object should be granted, and returns that
173 level of access for further checks.
174 ********************************************************************/
176 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
177 SE_PRIV *rights, uint32 rights_mask,
178 uint32 des_access, uint32 *acc_granted,
181 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
182 uint32 saved_mask = 0;
184 /* check privileges; certain SAM access bits should be overridden
185 by privileges (mostly having to do with creating/modifying/deleting
188 if ( rights && user_has_any_privilege( token, rights ) ) {
190 saved_mask = (des_access & rights_mask);
191 des_access &= ~saved_mask;
193 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
198 /* check the security descriptor first */
200 status = se_access_check(psd, token, des_access, acc_granted);
201 if (NT_STATUS_IS_OK(status)) {
205 /* give root a free pass */
207 if ( geteuid() == sec_initial_uid() ) {
209 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
210 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
212 *acc_granted = des_access;
214 status = NT_STATUS_OK;
220 /* add in any bits saved during the privilege check (only
221 matters is status is ok) */
223 *acc_granted |= rights_mask;
225 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
226 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
227 des_access, *acc_granted));
233 /*******************************************************************
234 Map any MAXIMUM_ALLOWED_ACCESS request to a valid access set.
235 ********************************************************************/
237 static void map_max_allowed_access(const NT_USER_TOKEN *token,
238 uint32_t *pacc_requested)
240 if (!((*pacc_requested) & MAXIMUM_ALLOWED_ACCESS)) {
243 *pacc_requested &= ~MAXIMUM_ALLOWED_ACCESS;
245 /* At least try for generic read. */
246 *pacc_requested = GENERIC_READ_ACCESS;
248 /* root gets anything. */
249 if (geteuid() == sec_initial_uid()) {
250 *pacc_requested |= GENERIC_ALL_ACCESS;
254 /* Full Access for 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
256 if (is_sid_in_token(token, &global_sid_Builtin_Administrators) ||
257 is_sid_in_token(token, &global_sid_Builtin_Account_Operators)) {
258 *pacc_requested |= GENERIC_ALL_ACCESS;
262 /* Full access for DOMAIN\Domain Admins. */
264 DOM_SID domadmin_sid;
265 sid_copy( &domadmin_sid, get_global_sam_sid() );
266 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
267 if (is_sid_in_token(token, &domadmin_sid)) {
268 *pacc_requested |= GENERIC_ALL_ACCESS;
272 /* TODO ! Check privileges. */
275 /*******************************************************************
276 Fetch or create a dispinfo struct.
277 ********************************************************************/
279 static DISP_INFO *get_samr_dispinfo_by_sid(const struct dom_sid *psid)
282 * We do a static cache for DISP_INFO's here. Explanation can be found
283 * in Jeremy's checkin message to r11793:
285 * Fix the SAMR cache so it works across completely insane
286 * client behaviour (ie.:
287 * open pipe/open SAMR handle/enumerate 0 - 1024
288 * close SAMR handle, close pipe.
289 * open pipe/open SAMR handle/enumerate 1024 - 2048...
290 * close SAMR handle, close pipe.
291 * And on ad-nausium. Amazing.... probably object-oriented
292 * client side programming in action yet again.
293 * This change should *massively* improve performance when
294 * enumerating users from an LDAP database.
297 * "Our" and the builtin domain are the only ones where we ever
298 * enumerate stuff, so just cache 2 entries.
301 static struct disp_info *builtin_dispinfo;
302 static struct disp_info *domain_dispinfo;
304 /* There are two cases to consider here:
305 1) The SID is a domain SID and we look for an equality match, or
306 2) This is an account SID and so we return the DISP_INFO* for our
313 if (sid_check_is_builtin(psid) || sid_check_is_in_builtin(psid)) {
315 * Necessary only once, but it does not really hurt.
317 if (builtin_dispinfo == NULL) {
318 builtin_dispinfo = talloc_zero(
319 talloc_autofree_context(), struct disp_info);
320 if (builtin_dispinfo == NULL) {
324 sid_copy(&builtin_dispinfo->sid, &global_sid_Builtin);
326 return builtin_dispinfo;
329 if (sid_check_is_domain(psid) || sid_check_is_in_our_domain(psid)) {
331 * Necessary only once, but it does not really hurt.
333 if (domain_dispinfo == NULL) {
334 domain_dispinfo = talloc_zero(
335 talloc_autofree_context(), struct disp_info);
336 if (domain_dispinfo == NULL) {
340 sid_copy(&domain_dispinfo->sid, get_global_sam_sid());
342 return domain_dispinfo;
348 /*******************************************************************
349 Function to free the per SID data.
350 ********************************************************************/
352 static void free_samr_cache(DISP_INFO *disp_info)
354 DEBUG(10, ("free_samr_cache: deleting cache for SID %s\n",
355 sid_string_dbg(&disp_info->sid)));
357 /* We need to become root here because the paged search might have to
358 * tell the LDAP server we're not interested in the rest anymore. */
362 TALLOC_FREE(disp_info->users);
363 TALLOC_FREE(disp_info->machines);
364 TALLOC_FREE(disp_info->groups);
365 TALLOC_FREE(disp_info->aliases);
366 TALLOC_FREE(disp_info->enum_users);
371 /*******************************************************************
372 Idle event handler. Throw away the disp info cache.
373 ********************************************************************/
375 static void disp_info_cache_idle_timeout_handler(struct event_context *ev_ctx,
376 struct timed_event *te,
380 DISP_INFO *disp_info = (DISP_INFO *)private_data;
382 TALLOC_FREE(disp_info->cache_timeout_event);
384 DEBUG(10, ("disp_info_cache_idle_timeout_handler: caching timed "
386 free_samr_cache(disp_info);
389 /*******************************************************************
390 Setup cache removal idle event handler.
391 ********************************************************************/
393 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
395 /* Remove any pending timeout and update. */
397 TALLOC_FREE(disp_info->cache_timeout_event);
399 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for "
400 "SID %s for %u seconds\n", sid_string_dbg(&disp_info->sid),
401 (unsigned int)secs_fromnow ));
403 disp_info->cache_timeout_event = event_add_timed(
404 smbd_event_context(), NULL,
405 timeval_current_ofs(secs_fromnow, 0),
406 disp_info_cache_idle_timeout_handler, (void *)disp_info);
409 /*******************************************************************
410 Force flush any cache. We do this on any samr_set_xxx call.
411 We must also remove the timeout handler.
412 ********************************************************************/
414 static void force_flush_samr_cache(const struct dom_sid *sid)
416 struct disp_info *disp_info = get_samr_dispinfo_by_sid(sid);
418 if ((disp_info == NULL) || (disp_info->cache_timeout_event == NULL)) {
422 DEBUG(10,("force_flush_samr_cache: clearing idle event\n"));
423 TALLOC_FREE(disp_info->cache_timeout_event);
424 free_samr_cache(disp_info);
427 /*******************************************************************
428 Ensure password info is never given out. Paranioa... JRA.
429 ********************************************************************/
431 static void samr_clear_sam_passwd(struct samu *sam_pass)
437 /* These now zero out the old password */
439 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
440 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
443 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
445 struct samr_displayentry *entry;
447 if (sid_check_is_builtin(&info->sid)) {
448 /* No users in builtin. */
452 if (info->users == NULL) {
453 info->users = pdb_search_users(info, acct_flags);
454 if (info->users == NULL) {
458 /* Fetch the last possible entry, thus trigger an enumeration */
459 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
461 /* Ensure we cache this enumeration. */
462 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
464 return info->users->num_entries;
467 static uint32 count_sam_groups(struct disp_info *info)
469 struct samr_displayentry *entry;
471 if (sid_check_is_builtin(&info->sid)) {
472 /* No groups in builtin. */
476 if (info->groups == NULL) {
477 info->groups = pdb_search_groups(info);
478 if (info->groups == NULL) {
482 /* Fetch the last possible entry, thus trigger an enumeration */
483 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
485 /* Ensure we cache this enumeration. */
486 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
488 return info->groups->num_entries;
491 static uint32 count_sam_aliases(struct disp_info *info)
493 struct samr_displayentry *entry;
495 if (info->aliases == NULL) {
496 info->aliases = pdb_search_aliases(info, &info->sid);
497 if (info->aliases == NULL) {
501 /* Fetch the last possible entry, thus trigger an enumeration */
502 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
504 /* Ensure we cache this enumeration. */
505 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
507 return info->aliases->num_entries;
510 /*******************************************************************
512 ********************************************************************/
514 NTSTATUS _samr_Close(pipes_struct *p, struct samr_Close *r)
516 if (!close_policy_hnd(p, r->in.handle)) {
517 return NT_STATUS_INVALID_HANDLE;
520 ZERO_STRUCTP(r->out.handle);
525 /*******************************************************************
527 ********************************************************************/
529 NTSTATUS _samr_OpenDomain(pipes_struct *p,
530 struct samr_OpenDomain *r)
532 struct samr_connect_info *cinfo;
533 struct samr_domain_info *dinfo;
534 SEC_DESC *psd = NULL;
536 uint32 des_access = r->in.access_mask;
541 /* find the connection policy handle. */
543 cinfo = policy_handle_find(p, r->in.connect_handle, 0, NULL,
544 struct samr_connect_info, &status);
545 if (!NT_STATUS_IS_OK(status)) {
549 /*check if access can be granted as requested by client. */
550 map_max_allowed_access(p->server_info->ptok, &des_access);
552 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
553 se_map_generic( &des_access, &dom_generic_mapping );
555 se_priv_copy( &se_rights, &se_machine_account );
556 se_priv_add( &se_rights, &se_add_users );
558 status = access_check_samr_object( psd, p->server_info->ptok,
559 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
560 &acc_granted, "_samr_OpenDomain" );
562 if ( !NT_STATUS_IS_OK(status) )
565 if (!sid_check_is_domain(r->in.sid) &&
566 !sid_check_is_builtin(r->in.sid)) {
567 return NT_STATUS_NO_SUCH_DOMAIN;
570 dinfo = policy_handle_create(p, r->out.domain_handle, acc_granted,
571 struct samr_domain_info, &status);
572 if (!NT_STATUS_IS_OK(status)) {
575 dinfo->sid = *r->in.sid;
576 dinfo->disp_info = get_samr_dispinfo_by_sid(r->in.sid);
578 DEBUG(5,("_samr_OpenDomain: %d\n", __LINE__));
583 /*******************************************************************
585 ********************************************************************/
587 NTSTATUS _samr_GetUserPwInfo(pipes_struct *p,
588 struct samr_GetUserPwInfo *r)
590 struct samr_user_info *uinfo;
591 enum lsa_SidType sid_type;
592 uint32_t min_password_length = 0;
593 uint32_t password_properties = 0;
597 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
599 uinfo = policy_handle_find(p, r->in.user_handle,
600 SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
601 struct samr_user_info, &status);
602 if (!NT_STATUS_IS_OK(status)) {
606 if (!sid_check_is_in_our_domain(&uinfo->sid)) {
607 return NT_STATUS_OBJECT_TYPE_MISMATCH;
611 ret = lookup_sid(p->mem_ctx, &uinfo->sid, NULL, NULL, &sid_type);
614 return NT_STATUS_NO_SUCH_USER;
620 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
621 &min_password_length);
622 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
623 &password_properties);
626 if (lp_check_password_script() && *lp_check_password_script()) {
627 password_properties |= DOMAIN_PASSWORD_COMPLEX;
635 r->out.info->min_password_length = min_password_length;
636 r->out.info->password_properties = password_properties;
638 DEBUG(5,("_samr_GetUserPwInfo: %d\n", __LINE__));
643 /*******************************************************************
645 ********************************************************************/
647 NTSTATUS _samr_SetSecurity(pipes_struct *p,
648 struct samr_SetSecurity *r)
650 struct samr_user_info *uinfo;
654 struct samu *sampass=NULL;
657 uinfo = policy_handle_find(p, r->in.handle,
658 SAMR_USER_ACCESS_SET_ATTRIBUTES, NULL,
659 struct samr_user_info, &status);
660 if (!NT_STATUS_IS_OK(status)) {
664 if (!(sampass = samu_new( p->mem_ctx))) {
665 DEBUG(0,("No memory!\n"));
666 return NT_STATUS_NO_MEMORY;
669 /* get the user record */
671 ret = pdb_getsampwsid(sampass, &uinfo->sid);
675 DEBUG(4, ("User %s not found\n",
676 sid_string_dbg(&uinfo->sid)));
677 TALLOC_FREE(sampass);
678 return NT_STATUS_INVALID_HANDLE;
681 dacl = r->in.sdbuf->sd->dacl;
682 for (i=0; i < dacl->num_aces; i++) {
683 if (sid_equal(&uinfo->sid, &dacl->aces[i].trustee)) {
684 ret = pdb_set_pass_can_change(sampass,
685 (dacl->aces[i].access_mask &
686 SAMR_USER_ACCESS_CHANGE_PASSWORD) ?
693 TALLOC_FREE(sampass);
694 return NT_STATUS_ACCESS_DENIED;
698 status = pdb_update_sam_account(sampass);
701 TALLOC_FREE(sampass);
706 /*******************************************************************
707 build correct perms based on policies and password times for _samr_query_sec_obj
708 *******************************************************************/
709 static bool check_change_pw_access(TALLOC_CTX *mem_ctx, DOM_SID *user_sid)
711 struct samu *sampass=NULL;
714 if ( !(sampass = samu_new( mem_ctx )) ) {
715 DEBUG(0,("No memory!\n"));
720 ret = pdb_getsampwsid(sampass, user_sid);
724 DEBUG(4,("User %s not found\n", sid_string_dbg(user_sid)));
725 TALLOC_FREE(sampass);
729 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
731 if (pdb_get_pass_can_change(sampass)) {
732 TALLOC_FREE(sampass);
735 TALLOC_FREE(sampass);
740 /*******************************************************************
742 ********************************************************************/
744 NTSTATUS _samr_QuerySecurity(pipes_struct *p,
745 struct samr_QuerySecurity *r)
747 struct samr_connect_info *cinfo;
748 struct samr_domain_info *dinfo;
749 struct samr_user_info *uinfo;
750 struct samr_group_info *ginfo;
751 struct samr_alias_info *ainfo;
753 SEC_DESC * psd = NULL;
756 cinfo = policy_handle_find(p, r->in.handle,
757 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
758 struct samr_connect_info, &status);
759 if (NT_STATUS_IS_OK(status)) {
760 DEBUG(5,("_samr_QuerySecurity: querying security on SAM\n"));
761 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
762 &sam_generic_mapping, NULL, 0);
766 dinfo = policy_handle_find(p, r->in.handle,
767 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
768 struct samr_domain_info, &status);
769 if (NT_STATUS_IS_OK(status)) {
770 DEBUG(5,("_samr_QuerySecurity: querying security on Domain "
771 "with SID: %s\n", sid_string_dbg(&dinfo->sid)));
773 * TODO: Builtin probably needs a different SD with restricted
776 status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size,
777 &dom_generic_mapping, NULL, 0);
781 uinfo = policy_handle_find(p, r->in.handle,
782 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
783 struct samr_user_info, &status);
784 if (NT_STATUS_IS_OK(status)) {
785 DEBUG(10,("_samr_QuerySecurity: querying security on user "
786 "Object with SID: %s\n",
787 sid_string_dbg(&uinfo->sid)));
788 if (check_change_pw_access(p->mem_ctx, &uinfo->sid)) {
789 status = make_samr_object_sd(
790 p->mem_ctx, &psd, &sd_size,
791 &usr_generic_mapping,
792 &uinfo->sid, SAMR_USR_RIGHTS_WRITE_PW);
794 status = make_samr_object_sd(
795 p->mem_ctx, &psd, &sd_size,
796 &usr_nopwchange_generic_mapping,
797 &uinfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
802 ginfo = policy_handle_find(p, r->in.handle,
803 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
804 struct samr_group_info, &status);
805 if (NT_STATUS_IS_OK(status)) {
807 * TODO: different SDs have to be generated for aliases groups
808 * and users. Currently all three get a default user SD
810 DEBUG(10,("_samr_QuerySecurity: querying security on group "
811 "Object with SID: %s\n",
812 sid_string_dbg(&ginfo->sid)));
813 status = make_samr_object_sd(
814 p->mem_ctx, &psd, &sd_size,
815 &usr_nopwchange_generic_mapping,
816 &ginfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
820 ainfo = policy_handle_find(p, r->in.handle,
821 STD_RIGHT_READ_CONTROL_ACCESS, NULL,
822 struct samr_alias_info, &status);
823 if (NT_STATUS_IS_OK(status)) {
825 * TODO: different SDs have to be generated for aliases groups
826 * and users. Currently all three get a default user SD
828 DEBUG(10,("_samr_QuerySecurity: querying security on alias "
829 "Object with SID: %s\n",
830 sid_string_dbg(&ainfo->sid)));
831 status = make_samr_object_sd(
832 p->mem_ctx, &psd, &sd_size,
833 &usr_nopwchange_generic_mapping,
834 &ainfo->sid, SAMR_USR_RIGHTS_CANT_WRITE_PW);
838 return NT_STATUS_OBJECT_TYPE_MISMATCH;
840 if ((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
841 return NT_STATUS_NO_MEMORY;
846 /*******************************************************************
847 makes a SAM_ENTRY / UNISTR2* structure from a user list.
848 ********************************************************************/
850 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx,
851 struct samr_SamEntry **sam_pp,
852 uint32_t num_entries,
854 struct samr_displayentry *entries)
857 struct samr_SamEntry *sam;
861 if (num_entries == 0) {
865 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_entries);
867 DEBUG(0, ("make_user_sam_entry_list: TALLOC_ZERO failed!\n"));
868 return NT_STATUS_NO_MEMORY;
871 for (i = 0; i < num_entries; i++) {
874 * usrmgr expects a non-NULL terminated string with
875 * trust relationships
877 if (entries[i].acct_flags & ACB_DOMTRUST) {
878 init_unistr2(&uni_temp_name, entries[i].account_name,
881 init_unistr2(&uni_temp_name, entries[i].account_name,
885 init_lsa_String(&sam[i].name, entries[i].account_name);
886 sam[i].idx = entries[i].rid;
894 #define MAX_SAM_ENTRIES MAX_SAM_ENTRIES_W2K
896 /*******************************************************************
897 _samr_EnumDomainUsers
898 ********************************************************************/
900 NTSTATUS _samr_EnumDomainUsers(pipes_struct *p,
901 struct samr_EnumDomainUsers *r)
904 struct samr_domain_info *dinfo;
906 uint32 enum_context = *r->in.resume_handle;
907 enum remote_arch_types ra_type = get_remote_arch();
908 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
909 uint32 max_entries = max_sam_entries;
910 struct samr_displayentry *entries = NULL;
911 struct samr_SamArray *samr_array = NULL;
912 struct samr_SamEntry *samr_entries = NULL;
914 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
916 dinfo = policy_handle_find(p, r->in.domain_handle,
917 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
918 struct samr_domain_info, &status);
919 if (!NT_STATUS_IS_OK(status)) {
923 if (sid_check_is_builtin(&dinfo->sid)) {
924 /* No users in builtin. */
925 *r->out.resume_handle = *r->in.resume_handle;
926 DEBUG(5,("_samr_EnumDomainUsers: No users in BUILTIN\n"));
930 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
932 return NT_STATUS_NO_MEMORY;
934 *r->out.sam = samr_array;
940 if ((dinfo->disp_info->enum_users != NULL) &&
941 (dinfo->disp_info->enum_acb_mask != r->in.acct_flags)) {
942 TALLOC_FREE(dinfo->disp_info->enum_users);
945 if (dinfo->disp_info->enum_users == NULL) {
946 dinfo->disp_info->enum_users = pdb_search_users(
947 dinfo->disp_info, r->in.acct_flags);
948 dinfo->disp_info->enum_acb_mask = r->in.acct_flags;
951 if (dinfo->disp_info->enum_users == NULL) {
952 /* END AS ROOT !!!! */
954 return NT_STATUS_ACCESS_DENIED;
957 num_account = pdb_search_entries(dinfo->disp_info->enum_users,
958 enum_context, max_entries,
961 /* END AS ROOT !!!! */
965 if (num_account == 0) {
966 DEBUG(5, ("_samr_EnumDomainUsers: enumeration handle over "
968 *r->out.resume_handle = *r->in.resume_handle;
972 status = make_user_sam_entry_list(p->mem_ctx, &samr_entries,
973 num_account, enum_context,
975 if (!NT_STATUS_IS_OK(status)) {
979 if (max_entries <= num_account) {
980 status = STATUS_MORE_ENTRIES;
982 status = NT_STATUS_OK;
985 /* Ensure we cache this enumeration. */
986 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
988 DEBUG(5, ("_samr_EnumDomainUsers: %d\n", __LINE__));
990 samr_array->count = num_account;
991 samr_array->entries = samr_entries;
993 *r->out.resume_handle = *r->in.resume_handle + num_account;
994 *r->out.num_entries = num_account;
996 DEBUG(5,("_samr_EnumDomainUsers: %d\n", __LINE__));
1001 /*******************************************************************
1002 makes a SAM_ENTRY / UNISTR2* structure from a group list.
1003 ********************************************************************/
1005 static void make_group_sam_entry_list(TALLOC_CTX *ctx,
1006 struct samr_SamEntry **sam_pp,
1007 uint32_t num_sam_entries,
1008 struct samr_displayentry *entries)
1010 struct samr_SamEntry *sam;
1015 if (num_sam_entries == 0) {
1019 sam = TALLOC_ZERO_ARRAY(ctx, struct samr_SamEntry, num_sam_entries);
1024 for (i = 0; i < num_sam_entries; i++) {
1026 * JRA. I think this should include the null. TNG does not.
1028 init_lsa_String(&sam[i].name, entries[i].account_name);
1029 sam[i].idx = entries[i].rid;
1035 /*******************************************************************
1036 _samr_EnumDomainGroups
1037 ********************************************************************/
1039 NTSTATUS _samr_EnumDomainGroups(pipes_struct *p,
1040 struct samr_EnumDomainGroups *r)
1043 struct samr_domain_info *dinfo;
1044 struct samr_displayentry *groups;
1046 struct samr_SamArray *samr_array = NULL;
1047 struct samr_SamEntry *samr_entries = NULL;
1049 dinfo = policy_handle_find(p, r->in.domain_handle,
1050 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1051 struct samr_domain_info, &status);
1052 if (!NT_STATUS_IS_OK(status)) {
1056 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1058 if (sid_check_is_builtin(&dinfo->sid)) {
1059 /* No groups in builtin. */
1060 *r->out.resume_handle = *r->in.resume_handle;
1061 DEBUG(5,("_samr_EnumDomainGroups: No groups in BUILTIN\n"));
1065 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1067 return NT_STATUS_NO_MEMORY;
1070 /* the domain group array is being allocated in the function below */
1074 if (dinfo->disp_info->groups == NULL) {
1075 dinfo->disp_info->groups = pdb_search_groups(dinfo->disp_info);
1077 if (dinfo->disp_info->groups == NULL) {
1079 return NT_STATUS_ACCESS_DENIED;
1083 num_groups = pdb_search_entries(dinfo->disp_info->groups,
1084 *r->in.resume_handle,
1085 MAX_SAM_ENTRIES, &groups);
1088 /* Ensure we cache this enumeration. */
1089 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1091 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1092 num_groups, groups);
1094 samr_array->count = num_groups;
1095 samr_array->entries = samr_entries;
1097 *r->out.sam = samr_array;
1098 *r->out.num_entries = num_groups;
1099 *r->out.resume_handle = num_groups + *r->in.resume_handle;
1101 DEBUG(5,("_samr_EnumDomainGroups: %d\n", __LINE__));
1106 /*******************************************************************
1107 _samr_EnumDomainAliases
1108 ********************************************************************/
1110 NTSTATUS _samr_EnumDomainAliases(pipes_struct *p,
1111 struct samr_EnumDomainAliases *r)
1114 struct samr_domain_info *dinfo;
1115 struct samr_displayentry *aliases;
1116 uint32 num_aliases = 0;
1117 struct samr_SamArray *samr_array = NULL;
1118 struct samr_SamEntry *samr_entries = NULL;
1120 dinfo = policy_handle_find(p, r->in.domain_handle,
1121 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1122 struct samr_domain_info, &status);
1123 if (!NT_STATUS_IS_OK(status)) {
1127 DEBUG(5,("_samr_EnumDomainAliases: sid %s\n",
1128 sid_string_dbg(&dinfo->sid)));
1130 samr_array = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
1132 return NT_STATUS_NO_MEMORY;
1137 if (dinfo->disp_info->aliases == NULL) {
1138 dinfo->disp_info->aliases = pdb_search_aliases(
1139 dinfo->disp_info, &dinfo->sid);
1140 if (dinfo->disp_info->aliases == NULL) {
1142 return NT_STATUS_ACCESS_DENIED;
1146 num_aliases = pdb_search_entries(dinfo->disp_info->aliases,
1147 *r->in.resume_handle,
1148 MAX_SAM_ENTRIES, &aliases);
1151 /* Ensure we cache this enumeration. */
1152 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1154 make_group_sam_entry_list(p->mem_ctx, &samr_entries,
1155 num_aliases, aliases);
1157 DEBUG(5,("_samr_EnumDomainAliases: %d\n", __LINE__));
1159 samr_array->count = num_aliases;
1160 samr_array->entries = samr_entries;
1162 *r->out.sam = samr_array;
1163 *r->out.num_entries = num_aliases;
1164 *r->out.resume_handle = num_aliases + *r->in.resume_handle;
1169 /*******************************************************************
1170 inits a samr_DispInfoGeneral structure.
1171 ********************************************************************/
1173 static NTSTATUS init_samr_dispinfo_1(TALLOC_CTX *ctx,
1174 struct samr_DispInfoGeneral *r,
1175 uint32_t num_entries,
1177 struct samr_displayentry *entries)
1181 DEBUG(10, ("init_samr_dispinfo_1: num_entries: %d\n", num_entries));
1183 if (num_entries == 0) {
1184 return NT_STATUS_OK;
1187 r->count = num_entries;
1189 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryGeneral, num_entries);
1191 return NT_STATUS_NO_MEMORY;
1194 for (i = 0; i < num_entries ; i++) {
1196 init_lsa_String(&r->entries[i].account_name,
1197 entries[i].account_name);
1199 init_lsa_String(&r->entries[i].description,
1200 entries[i].description);
1202 init_lsa_String(&r->entries[i].full_name,
1203 entries[i].fullname);
1205 r->entries[i].rid = entries[i].rid;
1206 r->entries[i].acct_flags = entries[i].acct_flags;
1207 r->entries[i].idx = start_idx+i+1;
1210 return NT_STATUS_OK;
1213 /*******************************************************************
1214 inits a samr_DispInfoFull structure.
1215 ********************************************************************/
1217 static NTSTATUS init_samr_dispinfo_2(TALLOC_CTX *ctx,
1218 struct samr_DispInfoFull *r,
1219 uint32_t num_entries,
1221 struct samr_displayentry *entries)
1225 DEBUG(10, ("init_samr_dispinfo_2: 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_DispEntryFull, 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 r->entries[i].rid = entries[i].rid;
1247 r->entries[i].acct_flags = entries[i].acct_flags;
1248 r->entries[i].idx = start_idx+i+1;
1251 return NT_STATUS_OK;
1254 /*******************************************************************
1255 inits a samr_DispInfoFullGroups structure.
1256 ********************************************************************/
1258 static NTSTATUS init_samr_dispinfo_3(TALLOC_CTX *ctx,
1259 struct samr_DispInfoFullGroups *r,
1260 uint32_t num_entries,
1262 struct samr_displayentry *entries)
1266 DEBUG(5, ("init_samr_dispinfo_3: num_entries: %d\n", num_entries));
1268 if (num_entries == 0) {
1269 return NT_STATUS_OK;
1272 r->count = num_entries;
1274 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryFullGroup, num_entries);
1276 return NT_STATUS_NO_MEMORY;
1279 for (i = 0; i < num_entries ; i++) {
1281 init_lsa_String(&r->entries[i].account_name,
1282 entries[i].account_name);
1284 init_lsa_String(&r->entries[i].description,
1285 entries[i].description);
1287 r->entries[i].rid = entries[i].rid;
1288 r->entries[i].acct_flags = entries[i].acct_flags;
1289 r->entries[i].idx = start_idx+i+1;
1292 return NT_STATUS_OK;
1295 /*******************************************************************
1296 inits a samr_DispInfoAscii structure.
1297 ********************************************************************/
1299 static NTSTATUS init_samr_dispinfo_4(TALLOC_CTX *ctx,
1300 struct samr_DispInfoAscii *r,
1301 uint32_t num_entries,
1303 struct samr_displayentry *entries)
1307 DEBUG(5, ("init_samr_dispinfo_4: num_entries: %d\n", num_entries));
1309 if (num_entries == 0) {
1310 return NT_STATUS_OK;
1313 r->count = num_entries;
1315 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1317 return NT_STATUS_NO_MEMORY;
1320 for (i = 0; i < num_entries ; i++) {
1322 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1323 entries[i].account_name);
1325 r->entries[i].idx = start_idx+i+1;
1328 return NT_STATUS_OK;
1331 /*******************************************************************
1332 inits a samr_DispInfoAscii structure.
1333 ********************************************************************/
1335 static NTSTATUS init_samr_dispinfo_5(TALLOC_CTX *ctx,
1336 struct samr_DispInfoAscii *r,
1337 uint32_t num_entries,
1339 struct samr_displayentry *entries)
1343 DEBUG(5, ("init_samr_dispinfo_5: num_entries: %d\n", num_entries));
1345 if (num_entries == 0) {
1346 return NT_STATUS_OK;
1349 r->count = num_entries;
1351 r->entries = TALLOC_ZERO_ARRAY(ctx, struct samr_DispEntryAscii, num_entries);
1353 return NT_STATUS_NO_MEMORY;
1356 for (i = 0; i < num_entries ; i++) {
1358 init_lsa_AsciiStringLarge(&r->entries[i].account_name,
1359 entries[i].account_name);
1361 r->entries[i].idx = start_idx+i+1;
1364 return NT_STATUS_OK;
1367 /*******************************************************************
1368 _samr_QueryDisplayInfo
1369 ********************************************************************/
1371 NTSTATUS _samr_QueryDisplayInfo(pipes_struct *p,
1372 struct samr_QueryDisplayInfo *r)
1375 struct samr_domain_info *dinfo;
1376 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1378 uint32 max_entries = r->in.max_entries;
1379 uint32 enum_context = r->in.start_idx;
1380 uint32 max_size = r->in.buf_size;
1382 union samr_DispInfo *disp_info = r->out.info;
1384 uint32 temp_size=0, total_data_size=0;
1385 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1386 uint32 num_account = 0;
1387 enum remote_arch_types ra_type = get_remote_arch();
1388 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1389 struct samr_displayentry *entries = NULL;
1391 DEBUG(5,("_samr_QueryDisplayInfo: %d\n", __LINE__));
1393 dinfo = policy_handle_find(p, r->in.domain_handle,
1394 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
1395 struct samr_domain_info, &status);
1396 if (!NT_STATUS_IS_OK(status)) {
1400 if (sid_check_is_builtin(&dinfo->sid)) {
1401 DEBUG(5,("_samr_QueryDisplayInfo: no users in BUILTIN\n"));
1402 return NT_STATUS_OK;
1406 * calculate how many entries we will return.
1408 * - the number of entries the client asked
1409 * - our limit on that
1410 * - the starting point (enumeration context)
1411 * - the buffer size the client will accept
1415 * We are a lot more like W2K. Instead of reading the SAM
1416 * each time to find the records we need to send back,
1417 * we read it once and link that copy to the sam handle.
1418 * For large user list (over the MAX_SAM_ENTRIES)
1419 * it's a definitive win.
1420 * second point to notice: between enumerations
1421 * our sam is now the same as it's a snapshoot.
1422 * third point: got rid of the static SAM_USER_21 struct
1423 * no more intermediate.
1424 * con: it uses much more memory, as a full copy is stored
1427 * If you want to change it, think twice and think
1428 * of the second point , that's really important.
1433 if ((r->in.level < 1) || (r->in.level > 5)) {
1434 DEBUG(0,("_samr_QueryDisplayInfo: Unknown info level (%u)\n",
1435 (unsigned int)r->in.level ));
1436 return NT_STATUS_INVALID_INFO_CLASS;
1439 /* first limit the number of entries we will return */
1440 if(max_entries > max_sam_entries) {
1441 DEBUG(5, ("_samr_QueryDisplayInfo: client requested %d "
1442 "entries, limiting to %d\n", max_entries,
1444 max_entries = max_sam_entries;
1447 /* calculate the size and limit on the number of entries we will
1450 temp_size=max_entries*struct_size;
1452 if (temp_size>max_size) {
1453 max_entries=MIN((max_size/struct_size),max_entries);;
1454 DEBUG(5, ("_samr_QueryDisplayInfo: buffer size limits to "
1455 "only %d entries\n", max_entries));
1460 /* THe following done as ROOT. Don't return without unbecome_root(). */
1462 switch (r->in.level) {
1465 if (dinfo->disp_info->users == NULL) {
1466 dinfo->disp_info->users = pdb_search_users(
1467 dinfo->disp_info, ACB_NORMAL);
1468 if (dinfo->disp_info->users == NULL) {
1470 return NT_STATUS_ACCESS_DENIED;
1472 DEBUG(10,("_samr_QueryDisplayInfo: starting user enumeration at index %u\n",
1473 (unsigned int)enum_context ));
1475 DEBUG(10,("_samr_QueryDisplayInfo: using cached user enumeration at index %u\n",
1476 (unsigned int)enum_context ));
1479 num_account = pdb_search_entries(dinfo->disp_info->users,
1480 enum_context, max_entries,
1484 if (dinfo->disp_info->machines == NULL) {
1485 dinfo->disp_info->machines = pdb_search_users(
1486 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
1487 if (dinfo->disp_info->machines == NULL) {
1489 return NT_STATUS_ACCESS_DENIED;
1491 DEBUG(10,("_samr_QueryDisplayInfo: starting machine enumeration at index %u\n",
1492 (unsigned int)enum_context ));
1494 DEBUG(10,("_samr_QueryDisplayInfo: using cached machine enumeration at index %u\n",
1495 (unsigned int)enum_context ));
1498 num_account = pdb_search_entries(dinfo->disp_info->machines,
1499 enum_context, max_entries,
1504 if (dinfo->disp_info->groups == NULL) {
1505 dinfo->disp_info->groups = pdb_search_groups(
1507 if (dinfo->disp_info->groups == NULL) {
1509 return NT_STATUS_ACCESS_DENIED;
1511 DEBUG(10,("_samr_QueryDisplayInfo: starting group enumeration at index %u\n",
1512 (unsigned int)enum_context ));
1514 DEBUG(10,("_samr_QueryDisplayInfo: using cached group enumeration at index %u\n",
1515 (unsigned int)enum_context ));
1518 num_account = pdb_search_entries(dinfo->disp_info->groups,
1519 enum_context, max_entries,
1524 smb_panic("info class changed");
1530 /* Now create reply structure */
1531 switch (r->in.level) {
1533 disp_ret = init_samr_dispinfo_1(p->mem_ctx, &disp_info->info1,
1534 num_account, enum_context,
1538 disp_ret = init_samr_dispinfo_2(p->mem_ctx, &disp_info->info2,
1539 num_account, enum_context,
1543 disp_ret = init_samr_dispinfo_3(p->mem_ctx, &disp_info->info3,
1544 num_account, enum_context,
1548 disp_ret = init_samr_dispinfo_4(p->mem_ctx, &disp_info->info4,
1549 num_account, enum_context,
1553 disp_ret = init_samr_dispinfo_5(p->mem_ctx, &disp_info->info5,
1554 num_account, enum_context,
1558 smb_panic("info class changed");
1562 if (!NT_STATUS_IS_OK(disp_ret))
1565 /* calculate the total size */
1566 total_data_size=num_account*struct_size;
1568 if (max_entries <= num_account) {
1569 status = STATUS_MORE_ENTRIES;
1571 status = NT_STATUS_OK;
1574 /* Ensure we cache this enumeration. */
1575 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
1577 DEBUG(5, ("_samr_QueryDisplayInfo: %d\n", __LINE__));
1579 *r->out.total_size = total_data_size;
1580 *r->out.returned_size = temp_size;
1585 /****************************************************************
1586 _samr_QueryDisplayInfo2
1587 ****************************************************************/
1589 NTSTATUS _samr_QueryDisplayInfo2(pipes_struct *p,
1590 struct samr_QueryDisplayInfo2 *r)
1592 struct samr_QueryDisplayInfo q;
1594 q.in.domain_handle = r->in.domain_handle;
1595 q.in.level = r->in.level;
1596 q.in.start_idx = r->in.start_idx;
1597 q.in.max_entries = r->in.max_entries;
1598 q.in.buf_size = r->in.buf_size;
1600 q.out.total_size = r->out.total_size;
1601 q.out.returned_size = r->out.returned_size;
1602 q.out.info = r->out.info;
1604 return _samr_QueryDisplayInfo(p, &q);
1607 /****************************************************************
1608 _samr_QueryDisplayInfo3
1609 ****************************************************************/
1611 NTSTATUS _samr_QueryDisplayInfo3(pipes_struct *p,
1612 struct samr_QueryDisplayInfo3 *r)
1614 struct samr_QueryDisplayInfo q;
1616 q.in.domain_handle = r->in.domain_handle;
1617 q.in.level = r->in.level;
1618 q.in.start_idx = r->in.start_idx;
1619 q.in.max_entries = r->in.max_entries;
1620 q.in.buf_size = r->in.buf_size;
1622 q.out.total_size = r->out.total_size;
1623 q.out.returned_size = r->out.returned_size;
1624 q.out.info = r->out.info;
1626 return _samr_QueryDisplayInfo(p, &q);
1629 /*******************************************************************
1630 _samr_QueryAliasInfo
1631 ********************************************************************/
1633 NTSTATUS _samr_QueryAliasInfo(pipes_struct *p,
1634 struct samr_QueryAliasInfo *r)
1636 struct samr_alias_info *ainfo;
1637 struct acct_info info;
1639 union samr_AliasInfo *alias_info = NULL;
1640 const char *alias_name = NULL;
1641 const char *alias_description = NULL;
1643 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1645 ainfo = policy_handle_find(p, r->in.alias_handle,
1646 SAMR_ALIAS_ACCESS_LOOKUP_INFO, NULL,
1647 struct samr_alias_info, &status);
1648 if (!NT_STATUS_IS_OK(status)) {
1652 alias_info = TALLOC_ZERO_P(p->mem_ctx, union samr_AliasInfo);
1654 return NT_STATUS_NO_MEMORY;
1658 status = pdb_get_aliasinfo(&ainfo->sid, &info);
1661 if ( !NT_STATUS_IS_OK(status))
1664 /* FIXME: info contains fstrings */
1665 alias_name = talloc_strdup(r, info.acct_name);
1666 alias_description = talloc_strdup(r, info.acct_desc);
1668 switch (r->in.level) {
1670 alias_info->all.name.string = alias_name;
1671 alias_info->all.num_members = 1; /* ??? */
1672 alias_info->all.description.string = alias_description;
1674 case ALIASINFODESCRIPTION:
1675 alias_info->description.string = alias_description;
1678 return NT_STATUS_INVALID_INFO_CLASS;
1681 *r->out.info = alias_info;
1683 DEBUG(5,("_samr_QueryAliasInfo: %d\n", __LINE__));
1685 return NT_STATUS_OK;
1688 /*******************************************************************
1690 ********************************************************************/
1692 NTSTATUS _samr_LookupNames(pipes_struct *p,
1693 struct samr_LookupNames *r)
1695 struct samr_domain_info *dinfo;
1698 enum lsa_SidType *type;
1700 int num_rids = r->in.num_names;
1701 struct samr_Ids rids, types;
1702 uint32_t num_mapped = 0;
1704 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1706 dinfo = policy_handle_find(p, r->in.domain_handle,
1707 0 /* Don't know the acc_bits yet */, NULL,
1708 struct samr_domain_info, &status);
1709 if (!NT_STATUS_IS_OK(status)) {
1713 if (num_rids > MAX_SAM_ENTRIES) {
1714 num_rids = MAX_SAM_ENTRIES;
1715 DEBUG(5,("_samr_LookupNames: truncating entries to %d\n", num_rids));
1718 rid = talloc_array(p->mem_ctx, uint32, num_rids);
1719 NT_STATUS_HAVE_NO_MEMORY(rid);
1721 type = talloc_array(p->mem_ctx, enum lsa_SidType, num_rids);
1722 NT_STATUS_HAVE_NO_MEMORY(type);
1724 DEBUG(5,("_samr_LookupNames: looking name on SID %s\n",
1725 sid_string_dbg(&dinfo->sid)));
1727 for (i = 0; i < num_rids; i++) {
1729 status = NT_STATUS_NONE_MAPPED;
1730 type[i] = SID_NAME_UNKNOWN;
1732 rid[i] = 0xffffffff;
1734 if (sid_check_is_builtin(&dinfo->sid)) {
1735 if (lookup_builtin_name(r->in.names[i].string,
1738 type[i] = SID_NAME_ALIAS;
1741 lookup_global_sam_name(r->in.names[i].string, 0,
1745 if (type[i] != SID_NAME_UNKNOWN) {
1750 if (num_mapped == num_rids) {
1751 status = NT_STATUS_OK;
1752 } else if (num_mapped == 0) {
1753 status = NT_STATUS_NONE_MAPPED;
1755 status = STATUS_SOME_UNMAPPED;
1758 rids.count = num_rids;
1761 types.count = num_rids;
1764 *r->out.rids = rids;
1765 *r->out.types = types;
1767 DEBUG(5,("_samr_LookupNames: %d\n", __LINE__));
1772 /*******************************************************************
1773 _samr_ChangePasswordUser2
1774 ********************************************************************/
1776 NTSTATUS _samr_ChangePasswordUser2(pipes_struct *p,
1777 struct samr_ChangePasswordUser2 *r)
1783 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1785 fstrcpy(user_name, r->in.account->string);
1786 fstrcpy(wks, r->in.server->string);
1788 DEBUG(5,("_samr_ChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1791 * Pass the user through the NT -> unix user mapping
1795 (void)map_username(user_name);
1798 * UNIX username case mangling not required, pass_oem_change
1799 * is case insensitive.
1802 status = pass_oem_change(user_name,
1803 r->in.lm_password->data,
1804 r->in.lm_verifier->hash,
1805 r->in.nt_password->data,
1806 r->in.nt_verifier->hash,
1809 DEBUG(5,("_samr_ChangePasswordUser2: %d\n", __LINE__));
1814 /****************************************************************
1815 _samr_OemChangePasswordUser2
1816 ****************************************************************/
1818 NTSTATUS _samr_OemChangePasswordUser2(pipes_struct *p,
1819 struct samr_OemChangePasswordUser2 *r)
1823 const char *wks = NULL;
1825 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
1827 fstrcpy(user_name, r->in.account->string);
1828 if (r->in.server && r->in.server->string) {
1829 wks = r->in.server->string;
1832 DEBUG(5,("_samr_OemChangePasswordUser2: user: %s wks: %s\n", user_name, wks));
1835 * Pass the user through the NT -> unix user mapping
1839 (void)map_username(user_name);
1842 * UNIX username case mangling not required, pass_oem_change
1843 * is case insensitive.
1846 if (!r->in.hash || !r->in.password) {
1847 return NT_STATUS_INVALID_PARAMETER;
1850 status = pass_oem_change(user_name,
1851 r->in.password->data,
1857 DEBUG(5,("_samr_OemChangePasswordUser2: %d\n", __LINE__));
1862 /*******************************************************************
1863 _samr_ChangePasswordUser3
1864 ********************************************************************/
1866 NTSTATUS _samr_ChangePasswordUser3(pipes_struct *p,
1867 struct samr_ChangePasswordUser3 *r)
1871 const char *wks = NULL;
1872 uint32 reject_reason;
1873 struct samr_DomInfo1 *dominfo = NULL;
1874 struct samr_ChangeReject *reject = NULL;
1877 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1879 fstrcpy(user_name, r->in.account->string);
1880 if (r->in.server && r->in.server->string) {
1881 wks = r->in.server->string;
1884 DEBUG(5,("_samr_ChangePasswordUser3: user: %s wks: %s\n", user_name, wks));
1887 * Pass the user through the NT -> unix user mapping
1891 (void)map_username(user_name);
1894 * UNIX username case mangling not required, pass_oem_change
1895 * is case insensitive.
1898 status = pass_oem_change(user_name,
1899 r->in.lm_password->data,
1900 r->in.lm_verifier->hash,
1901 r->in.nt_password->data,
1902 r->in.nt_verifier->hash,
1905 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION) ||
1906 NT_STATUS_EQUAL(status, NT_STATUS_ACCOUNT_RESTRICTION)) {
1908 time_t u_expire, u_min_age;
1909 uint32 account_policy_temp;
1911 dominfo = TALLOC_ZERO_P(p->mem_ctx, struct samr_DomInfo1);
1913 return NT_STATUS_NO_MEMORY;
1916 reject = TALLOC_ZERO_P(p->mem_ctx, struct samr_ChangeReject);
1918 return NT_STATUS_NO_MEMORY;
1925 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &tmp);
1926 dominfo->min_password_length = tmp;
1928 pdb_get_account_policy(AP_PASSWORD_HISTORY, &tmp);
1929 dominfo->password_history_length = tmp;
1931 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
1932 &dominfo->password_properties);
1934 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
1935 u_expire = account_policy_temp;
1937 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
1938 u_min_age = account_policy_temp;
1944 unix_to_nt_time_abs((NTTIME *)&dominfo->max_password_age, u_expire);
1945 unix_to_nt_time_abs((NTTIME *)&dominfo->min_password_age, u_min_age);
1947 if (lp_check_password_script() && *lp_check_password_script()) {
1948 dominfo->password_properties |= DOMAIN_PASSWORD_COMPLEX;
1951 reject->reason = reject_reason;
1953 *r->out.dominfo = dominfo;
1954 *r->out.reject = reject;
1957 DEBUG(5,("_samr_ChangePasswordUser3: %d\n", __LINE__));
1962 /*******************************************************************
1963 makes a SAMR_R_LOOKUP_RIDS structure.
1964 ********************************************************************/
1966 static bool make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1968 struct lsa_String **lsa_name_array_p)
1970 struct lsa_String *lsa_name_array = NULL;
1973 *lsa_name_array_p = NULL;
1975 if (num_names != 0) {
1976 lsa_name_array = TALLOC_ZERO_ARRAY(ctx, struct lsa_String, num_names);
1977 if (!lsa_name_array) {
1982 for (i = 0; i < num_names; i++) {
1983 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1984 init_lsa_String(&lsa_name_array[i], names[i]);
1987 *lsa_name_array_p = lsa_name_array;
1992 /*******************************************************************
1994 ********************************************************************/
1996 NTSTATUS _samr_LookupRids(pipes_struct *p,
1997 struct samr_LookupRids *r)
1999 struct samr_domain_info *dinfo;
2002 enum lsa_SidType *attrs = NULL;
2003 uint32 *wire_attrs = NULL;
2004 int num_rids = (int)r->in.num_rids;
2006 struct lsa_Strings names_array;
2007 struct samr_Ids types_array;
2008 struct lsa_String *lsa_names = NULL;
2010 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2012 dinfo = policy_handle_find(p, r->in.domain_handle,
2013 0 /* Don't know the acc_bits yet */, NULL,
2014 struct samr_domain_info, &status);
2015 if (!NT_STATUS_IS_OK(status)) {
2019 if (num_rids > 1000) {
2020 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
2021 "to samba4 idl this is not possible\n", num_rids));
2022 return NT_STATUS_UNSUCCESSFUL;
2026 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
2027 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum lsa_SidType, num_rids);
2028 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
2030 if ((names == NULL) || (attrs == NULL) || (wire_attrs==NULL))
2031 return NT_STATUS_NO_MEMORY;
2038 become_root(); /* lookup_sid can require root privs */
2039 status = pdb_lookup_rids(&dinfo->sid, num_rids, r->in.rids,
2043 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED) && (num_rids == 0)) {
2044 status = NT_STATUS_OK;
2047 if (!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
2049 return NT_STATUS_NO_MEMORY;
2052 /* Convert from enum lsa_SidType to uint32 for wire format. */
2053 for (i = 0; i < num_rids; i++) {
2054 wire_attrs[i] = (uint32)attrs[i];
2057 names_array.count = num_rids;
2058 names_array.names = lsa_names;
2060 types_array.count = num_rids;
2061 types_array.ids = wire_attrs;
2063 *r->out.names = names_array;
2064 *r->out.types = types_array;
2066 DEBUG(5,("_samr_LookupRids: %d\n", __LINE__));
2071 /*******************************************************************
2073 ********************************************************************/
2075 NTSTATUS _samr_OpenUser(pipes_struct *p,
2076 struct samr_OpenUser *r)
2078 struct samu *sampass=NULL;
2080 struct samr_domain_info *dinfo;
2081 struct samr_user_info *uinfo;
2082 SEC_DESC *psd = NULL;
2084 uint32 des_access = r->in.access_mask;
2091 dinfo = policy_handle_find(p, r->in.domain_handle,
2092 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
2093 struct samr_domain_info, &status);
2094 if (!NT_STATUS_IS_OK(status)) {
2098 if ( !(sampass = samu_new( p->mem_ctx )) ) {
2099 return NT_STATUS_NO_MEMORY;
2102 /* append the user's RID to it */
2104 if (!sid_compose(&sid, &dinfo->sid, r->in.rid))
2105 return NT_STATUS_NO_SUCH_USER;
2107 /* check if access can be granted as requested by client. */
2109 map_max_allowed_access(p->server_info->ptok, &des_access);
2111 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2112 se_map_generic(&des_access, &usr_generic_mapping);
2114 se_priv_copy( &se_rights, &se_machine_account );
2115 se_priv_add( &se_rights, &se_add_users );
2117 nt_status = access_check_samr_object(psd, p->server_info->ptok,
2118 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2119 &acc_granted, "_samr_OpenUser");
2121 if ( !NT_STATUS_IS_OK(nt_status) )
2125 ret=pdb_getsampwsid(sampass, &sid);
2128 /* check that the SID exists in our domain. */
2130 return NT_STATUS_NO_SUCH_USER;
2133 TALLOC_FREE(sampass);
2135 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
2136 struct samr_user_info, &nt_status);
2137 if (!NT_STATUS_IS_OK(nt_status)) {
2142 return NT_STATUS_OK;
2145 /*************************************************************************
2146 *************************************************************************/
2148 static NTSTATUS init_samr_parameters_string(TALLOC_CTX *mem_ctx,
2150 struct lsa_BinaryString **_r)
2152 struct lsa_BinaryString *r;
2155 return NT_STATUS_INVALID_PARAMETER;
2158 r = TALLOC_ZERO_P(mem_ctx, struct lsa_BinaryString);
2160 return NT_STATUS_NO_MEMORY;
2163 r->array = TALLOC_ZERO_ARRAY(mem_ctx, uint16_t, blob->length/2);
2165 return NT_STATUS_NO_MEMORY;
2167 memcpy(r->array, blob->data, blob->length);
2168 r->size = blob->length;
2169 r->length = blob->length;
2172 return NT_STATUS_NO_MEMORY;
2177 return NT_STATUS_OK;
2180 /*************************************************************************
2182 *************************************************************************/
2184 static NTSTATUS get_user_info_1(TALLOC_CTX *mem_ctx,
2185 struct samr_UserInfo1 *r,
2187 DOM_SID *domain_sid)
2189 const DOM_SID *sid_group;
2190 uint32_t primary_gid;
2193 sid_group = pdb_get_group_sid(pw);
2196 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2197 DEBUG(0, ("get_user_info_1: User %s has Primary Group SID %s, \n"
2198 "which conflicts with the domain sid %s. Failing operation.\n",
2199 pdb_get_username(pw), sid_string_dbg(sid_group),
2200 sid_string_dbg(domain_sid)));
2201 return NT_STATUS_UNSUCCESSFUL;
2204 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2205 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2206 r->primary_gid = primary_gid;
2207 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2208 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2210 return NT_STATUS_OK;
2213 /*************************************************************************
2215 *************************************************************************/
2217 static NTSTATUS get_user_info_2(TALLOC_CTX *mem_ctx,
2218 struct samr_UserInfo2 *r,
2221 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2222 r->unknown.string = NULL;
2223 r->country_code = 0;
2226 return NT_STATUS_OK;
2229 /*************************************************************************
2231 *************************************************************************/
2233 static NTSTATUS get_user_info_3(TALLOC_CTX *mem_ctx,
2234 struct samr_UserInfo3 *r,
2236 DOM_SID *domain_sid)
2238 const DOM_SID *sid_user, *sid_group;
2239 uint32_t rid, primary_gid;
2241 sid_user = pdb_get_user_sid(pw);
2243 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2244 DEBUG(0, ("get_user_info_3: User %s has SID %s, \nwhich conflicts with "
2245 "the domain sid %s. Failing operation.\n",
2246 pdb_get_username(pw), sid_string_dbg(sid_user),
2247 sid_string_dbg(domain_sid)));
2248 return NT_STATUS_UNSUCCESSFUL;
2252 sid_group = pdb_get_group_sid(pw);
2255 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2256 DEBUG(0, ("get_user_info_3: User %s has Primary Group SID %s, \n"
2257 "which conflicts with the domain sid %s. Failing operation.\n",
2258 pdb_get_username(pw), sid_string_dbg(sid_group),
2259 sid_string_dbg(domain_sid)));
2260 return NT_STATUS_UNSUCCESSFUL;
2263 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2264 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2265 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2266 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2267 unix_to_nt_time(&r->force_password_change, pdb_get_pass_must_change_time(pw));
2269 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2270 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2271 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2272 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2273 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2274 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2275 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2277 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2279 r->primary_gid = primary_gid;
2280 r->acct_flags = pdb_get_acct_ctrl(pw);
2281 r->bad_password_count = pdb_get_bad_password_count(pw);
2282 r->logon_count = pdb_get_logon_count(pw);
2284 return NT_STATUS_OK;
2287 /*************************************************************************
2289 *************************************************************************/
2291 static NTSTATUS get_user_info_4(TALLOC_CTX *mem_ctx,
2292 struct samr_UserInfo4 *r,
2295 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2297 return NT_STATUS_OK;
2300 /*************************************************************************
2302 *************************************************************************/
2304 static NTSTATUS get_user_info_5(TALLOC_CTX *mem_ctx,
2305 struct samr_UserInfo5 *r,
2307 DOM_SID *domain_sid)
2309 const DOM_SID *sid_user, *sid_group;
2310 uint32_t rid, primary_gid;
2312 sid_user = pdb_get_user_sid(pw);
2314 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2315 DEBUG(0, ("get_user_info_5: User %s has SID %s, \nwhich conflicts with "
2316 "the domain sid %s. Failing operation.\n",
2317 pdb_get_username(pw), sid_string_dbg(sid_user),
2318 sid_string_dbg(domain_sid)));
2319 return NT_STATUS_UNSUCCESSFUL;
2323 sid_group = pdb_get_group_sid(pw);
2326 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2327 DEBUG(0, ("get_user_info_5: User %s has Primary Group SID %s, \n"
2328 "which conflicts with the domain sid %s. Failing operation.\n",
2329 pdb_get_username(pw), sid_string_dbg(sid_group),
2330 sid_string_dbg(domain_sid)));
2331 return NT_STATUS_UNSUCCESSFUL;
2334 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2335 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2336 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2337 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2339 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2340 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2341 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2342 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2343 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2344 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2345 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2346 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2348 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2350 r->primary_gid = primary_gid;
2351 r->acct_flags = pdb_get_acct_ctrl(pw);
2352 r->bad_password_count = pdb_get_bad_password_count(pw);
2353 r->logon_count = pdb_get_logon_count(pw);
2355 return NT_STATUS_OK;
2358 /*************************************************************************
2360 *************************************************************************/
2362 static NTSTATUS get_user_info_6(TALLOC_CTX *mem_ctx,
2363 struct samr_UserInfo6 *r,
2366 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2367 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2369 return NT_STATUS_OK;
2372 /*************************************************************************
2373 get_user_info_7. Safe. Only gives out account_name.
2374 *************************************************************************/
2376 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx,
2377 struct samr_UserInfo7 *r,
2378 struct samu *smbpass)
2380 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(smbpass));
2381 if (!r->account_name.string) {
2382 return NT_STATUS_NO_MEMORY;
2385 return NT_STATUS_OK;
2388 /*************************************************************************
2390 *************************************************************************/
2392 static NTSTATUS get_user_info_8(TALLOC_CTX *mem_ctx,
2393 struct samr_UserInfo8 *r,
2396 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2398 return NT_STATUS_OK;
2401 /*************************************************************************
2402 get_user_info_9. Only gives out primary group SID.
2403 *************************************************************************/
2405 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx,
2406 struct samr_UserInfo9 *r,
2407 struct samu *smbpass)
2409 r->primary_gid = pdb_get_group_rid(smbpass);
2411 return NT_STATUS_OK;
2414 /*************************************************************************
2416 *************************************************************************/
2418 static NTSTATUS get_user_info_10(TALLOC_CTX *mem_ctx,
2419 struct samr_UserInfo10 *r,
2422 r->home_directory.string= talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2423 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2425 return NT_STATUS_OK;
2428 /*************************************************************************
2430 *************************************************************************/
2432 static NTSTATUS get_user_info_11(TALLOC_CTX *mem_ctx,
2433 struct samr_UserInfo11 *r,
2436 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2438 return NT_STATUS_OK;
2441 /*************************************************************************
2443 *************************************************************************/
2445 static NTSTATUS get_user_info_12(TALLOC_CTX *mem_ctx,
2446 struct samr_UserInfo12 *r,
2449 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2451 return NT_STATUS_OK;
2454 /*************************************************************************
2456 *************************************************************************/
2458 static NTSTATUS get_user_info_13(TALLOC_CTX *mem_ctx,
2459 struct samr_UserInfo13 *r,
2462 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2464 return NT_STATUS_OK;
2467 /*************************************************************************
2469 *************************************************************************/
2471 static NTSTATUS get_user_info_14(TALLOC_CTX *mem_ctx,
2472 struct samr_UserInfo14 *r,
2475 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2477 return NT_STATUS_OK;
2480 /*************************************************************************
2481 get_user_info_16. Safe. Only gives out acb bits.
2482 *************************************************************************/
2484 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx,
2485 struct samr_UserInfo16 *r,
2486 struct samu *smbpass)
2488 r->acct_flags = pdb_get_acct_ctrl(smbpass);
2490 return NT_STATUS_OK;
2493 /*************************************************************************
2495 *************************************************************************/
2497 static NTSTATUS get_user_info_17(TALLOC_CTX *mem_ctx,
2498 struct samr_UserInfo17 *r,
2501 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2503 return NT_STATUS_OK;
2506 /*************************************************************************
2507 get_user_info_18. OK - this is the killer as it gives out password info.
2508 Ensure that this is only allowed on an encrypted connection with a root
2510 *************************************************************************/
2512 static NTSTATUS get_user_info_18(pipes_struct *p,
2513 TALLOC_CTX *mem_ctx,
2514 struct samr_UserInfo18 *r,
2517 struct samu *smbpass=NULL;
2522 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
2523 return NT_STATUS_ACCESS_DENIED;
2526 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
2527 return NT_STATUS_ACCESS_DENIED;
2531 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
2534 if ( !(smbpass = samu_new( mem_ctx )) ) {
2535 return NT_STATUS_NO_MEMORY;
2538 ret = pdb_getsampwsid(smbpass, user_sid);
2541 DEBUG(4, ("User %s not found\n", sid_string_dbg(user_sid)));
2542 TALLOC_FREE(smbpass);
2543 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
2546 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
2548 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
2549 TALLOC_FREE(smbpass);
2550 return NT_STATUS_ACCOUNT_DISABLED;
2553 r->lm_pwd_active = true;
2554 r->nt_pwd_active = true;
2555 memcpy(r->lm_pwd.hash, pdb_get_lanman_passwd(smbpass), 16);
2556 memcpy(r->nt_pwd.hash, pdb_get_nt_passwd(smbpass), 16);
2557 r->password_expired = 0; /* FIXME */
2559 TALLOC_FREE(smbpass);
2561 return NT_STATUS_OK;
2564 /*************************************************************************
2566 *************************************************************************/
2568 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx,
2569 struct samr_UserInfo20 *r,
2570 struct samu *sampass)
2572 const char *munged_dial = NULL;
2575 struct lsa_BinaryString *parameters = NULL;
2579 munged_dial = pdb_get_munged_dial(sampass);
2581 DEBUG(3,("User:[%s] has [%s] (length: %d)\n", pdb_get_username(sampass),
2582 munged_dial, (int)strlen(munged_dial)));
2585 blob = base64_decode_data_blob(munged_dial);
2587 blob = data_blob_string_const_null("");
2590 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2591 data_blob_free(&blob);
2592 if (!NT_STATUS_IS_OK(status)) {
2596 r->parameters = *parameters;
2598 return NT_STATUS_OK;
2602 /*************************************************************************
2604 *************************************************************************/
2606 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx,
2607 struct samr_UserInfo21 *r,
2609 DOM_SID *domain_sid)
2612 const DOM_SID *sid_user, *sid_group;
2613 uint32_t rid, primary_gid;
2614 NTTIME force_password_change;
2615 time_t must_change_time;
2616 struct lsa_BinaryString *parameters = NULL;
2617 const char *munged_dial = NULL;
2622 sid_user = pdb_get_user_sid(pw);
2624 if (!sid_peek_check_rid(domain_sid, sid_user, &rid)) {
2625 DEBUG(0, ("get_user_info_21: User %s has SID %s, \nwhich conflicts with "
2626 "the domain sid %s. Failing operation.\n",
2627 pdb_get_username(pw), sid_string_dbg(sid_user),
2628 sid_string_dbg(domain_sid)));
2629 return NT_STATUS_UNSUCCESSFUL;
2633 sid_group = pdb_get_group_sid(pw);
2636 if (!sid_peek_check_rid(domain_sid, sid_group, &primary_gid)) {
2637 DEBUG(0, ("get_user_info_21: User %s has Primary Group SID %s, \n"
2638 "which conflicts with the domain sid %s. Failing operation.\n",
2639 pdb_get_username(pw), sid_string_dbg(sid_group),
2640 sid_string_dbg(domain_sid)));
2641 return NT_STATUS_UNSUCCESSFUL;
2644 unix_to_nt_time(&r->last_logon, pdb_get_logon_time(pw));
2645 unix_to_nt_time(&r->last_logoff, pdb_get_logoff_time(pw));
2646 unix_to_nt_time(&r->acct_expiry, pdb_get_kickoff_time(pw));
2647 unix_to_nt_time(&r->last_password_change, pdb_get_pass_last_set_time(pw));
2648 unix_to_nt_time(&r->allow_password_change, pdb_get_pass_can_change_time(pw));
2650 must_change_time = pdb_get_pass_must_change_time(pw);
2651 if (must_change_time == get_time_t_max()) {
2652 unix_to_nt_time_abs(&force_password_change, must_change_time);
2654 unix_to_nt_time(&force_password_change, must_change_time);
2657 munged_dial = pdb_get_munged_dial(pw);
2659 blob = base64_decode_data_blob(munged_dial);
2661 blob = data_blob_string_const_null("");
2664 status = init_samr_parameters_string(mem_ctx, &blob, ¶meters);
2665 data_blob_free(&blob);
2666 if (!NT_STATUS_IS_OK(status)) {
2670 r->force_password_change = force_password_change;
2672 r->account_name.string = talloc_strdup(mem_ctx, pdb_get_username(pw));
2673 r->full_name.string = talloc_strdup(mem_ctx, pdb_get_fullname(pw));
2674 r->home_directory.string = talloc_strdup(mem_ctx, pdb_get_homedir(pw));
2675 r->home_drive.string = talloc_strdup(mem_ctx, pdb_get_dir_drive(pw));
2676 r->logon_script.string = talloc_strdup(mem_ctx, pdb_get_logon_script(pw));
2677 r->profile_path.string = talloc_strdup(mem_ctx, pdb_get_profile_path(pw));
2678 r->description.string = talloc_strdup(mem_ctx, pdb_get_acct_desc(pw));
2679 r->workstations.string = talloc_strdup(mem_ctx, pdb_get_workstations(pw));
2680 r->comment.string = talloc_strdup(mem_ctx, pdb_get_comment(pw));
2682 r->logon_hours = get_logon_hours_from_pdb(mem_ctx, pw);
2683 r->parameters = *parameters;
2685 r->primary_gid = primary_gid;
2686 r->acct_flags = pdb_get_acct_ctrl(pw);
2687 r->bad_password_count = pdb_get_bad_password_count(pw);
2688 r->logon_count = pdb_get_logon_count(pw);
2689 r->fields_present = pdb_build_fields_present(pw);
2690 r->password_expired = (pdb_get_pass_must_change_time(pw) == 0) ?
2691 PASS_MUST_CHANGE_AT_NEXT_LOGON : 0;
2692 r->country_code = 0;
2694 r->lm_password_set = 0;
2695 r->nt_password_set = 0;
2700 Look at a user on a real NT4 PDC with usrmgr, press
2701 'ok'. Then you will see that fields_present is set to
2702 0x08f827fa. Look at the user immediately after that again,
2703 and you will see that 0x00fffff is returned. This solves
2704 the problem that you get access denied after having looked
2712 return NT_STATUS_OK;
2715 /*******************************************************************
2717 ********************************************************************/
2719 NTSTATUS _samr_QueryUserInfo(pipes_struct *p,
2720 struct samr_QueryUserInfo *r)
2723 union samr_UserInfo *user_info = NULL;
2724 struct samr_user_info *uinfo;
2728 struct samu *pwd = NULL;
2730 uinfo = policy_handle_find(p, r->in.user_handle,
2731 SAMR_USER_ACCESS_GET_ATTRIBUTES, NULL,
2732 struct samr_user_info, &status);
2733 if (!NT_STATUS_IS_OK(status)) {
2737 domain_sid = uinfo->sid;
2739 sid_split_rid(&domain_sid, &rid);
2741 if (!sid_check_is_in_our_domain(&uinfo->sid))
2742 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2744 DEBUG(5,("_samr_QueryUserInfo: sid:%s\n",
2745 sid_string_dbg(&uinfo->sid)));
2747 user_info = TALLOC_ZERO_P(p->mem_ctx, union samr_UserInfo);
2749 return NT_STATUS_NO_MEMORY;
2752 DEBUG(5,("_samr_QueryUserInfo: user info level: %d\n", r->in.level));
2754 if (!(pwd = samu_new(p->mem_ctx))) {
2755 return NT_STATUS_NO_MEMORY;
2759 ret = pdb_getsampwsid(pwd, &uinfo->sid);
2763 DEBUG(4,("User %s not found\n", sid_string_dbg(&uinfo->sid)));
2765 return NT_STATUS_NO_SUCH_USER;
2768 DEBUG(3,("User:[%s]\n", pdb_get_username(pwd)));
2770 samr_clear_sam_passwd(pwd);
2772 switch (r->in.level) {
2774 status = get_user_info_1(p->mem_ctx, &user_info->info1, pwd, &domain_sid);
2777 status = get_user_info_2(p->mem_ctx, &user_info->info2, pwd);
2780 status = get_user_info_3(p->mem_ctx, &user_info->info3, pwd, &domain_sid);
2783 status = get_user_info_4(p->mem_ctx, &user_info->info4, pwd);
2786 status = get_user_info_5(p->mem_ctx, &user_info->info5, pwd, &domain_sid);
2789 status = get_user_info_6(p->mem_ctx, &user_info->info6, pwd);
2792 status = get_user_info_7(p->mem_ctx, &user_info->info7, pwd);
2795 status = get_user_info_8(p->mem_ctx, &user_info->info8, pwd);
2798 status = get_user_info_9(p->mem_ctx, &user_info->info9, pwd);
2801 status = get_user_info_10(p->mem_ctx, &user_info->info10, pwd);
2804 status = get_user_info_11(p->mem_ctx, &user_info->info11, pwd);
2807 status = get_user_info_12(p->mem_ctx, &user_info->info12, pwd);
2810 status = get_user_info_13(p->mem_ctx, &user_info->info13, pwd);
2813 status = get_user_info_14(p->mem_ctx, &user_info->info14, pwd);
2816 status = get_user_info_16(p->mem_ctx, &user_info->info16, pwd);
2819 status = get_user_info_17(p->mem_ctx, &user_info->info17, pwd);
2822 /* level 18 is special */
2823 status = get_user_info_18(p, p->mem_ctx, &user_info->info18,
2827 status = get_user_info_20(p->mem_ctx, &user_info->info20, pwd);
2830 status = get_user_info_21(p->mem_ctx, &user_info->info21, pwd, &domain_sid);
2833 status = NT_STATUS_INVALID_INFO_CLASS;
2839 *r->out.info = user_info;
2841 DEBUG(5,("_samr_QueryUserInfo: %d\n", __LINE__));
2846 /****************************************************************
2847 ****************************************************************/
2849 NTSTATUS _samr_QueryUserInfo2(pipes_struct *p,
2850 struct samr_QueryUserInfo2 *r)
2852 struct samr_QueryUserInfo u;
2854 u.in.user_handle = r->in.user_handle;
2855 u.in.level = r->in.level;
2856 u.out.info = r->out.info;
2858 return _samr_QueryUserInfo(p, &u);
2861 /*******************************************************************
2862 _samr_GetGroupsForUser
2863 ********************************************************************/
2865 NTSTATUS _samr_GetGroupsForUser(pipes_struct *p,
2866 struct samr_GetGroupsForUser *r)
2868 struct samr_user_info *uinfo;
2869 struct samu *sam_pass=NULL;
2871 struct samr_RidWithAttribute dom_gid;
2872 struct samr_RidWithAttribute *gids = NULL;
2873 uint32 primary_group_rid;
2874 size_t num_groups = 0;
2879 bool success = False;
2881 struct samr_RidWithAttributeArray *rids = NULL;
2884 * from the SID in the request:
2885 * we should send back the list of DOMAIN GROUPS
2886 * the user is a member of
2888 * and only the DOMAIN GROUPS
2889 * no ALIASES !!! neither aliases of the domain
2890 * nor aliases of the builtin SID
2895 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2897 uinfo = policy_handle_find(p, r->in.user_handle,
2898 SAMR_USER_ACCESS_GET_GROUPS, NULL,
2899 struct samr_user_info, &result);
2900 if (!NT_STATUS_IS_OK(result)) {
2904 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidWithAttributeArray);
2906 return NT_STATUS_NO_MEMORY;
2909 if (!sid_check_is_in_our_domain(&uinfo->sid))
2910 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2912 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2913 return NT_STATUS_NO_MEMORY;
2917 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
2921 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2922 sid_string_dbg(&uinfo->sid)));
2923 return NT_STATUS_NO_SUCH_USER;
2928 /* make both calls inside the root block */
2930 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2931 &sids, &unix_gids, &num_groups);
2932 if ( NT_STATUS_IS_OK(result) ) {
2933 success = sid_peek_check_rid(get_global_sam_sid(),
2934 pdb_get_group_sid(sam_pass),
2935 &primary_group_rid);
2939 if (!NT_STATUS_IS_OK(result)) {
2940 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2941 sid_string_dbg(&uinfo->sid)));
2946 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2947 sid_string_dbg(pdb_get_group_sid(sam_pass)),
2948 pdb_get_username(sam_pass)));
2949 TALLOC_FREE(sam_pass);
2950 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2956 dom_gid.attributes = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2958 dom_gid.rid = primary_group_rid;
2959 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2961 for (i=0; i<num_groups; i++) {
2963 if (!sid_peek_check_rid(get_global_sam_sid(),
2964 &(sids[i]), &dom_gid.rid)) {
2965 DEBUG(10, ("Found sid %s not in our domain\n",
2966 sid_string_dbg(&sids[i])));
2970 if (dom_gid.rid == primary_group_rid) {
2971 /* We added the primary group directly from the
2972 * sam_account. The other SIDs are unique from
2973 * enum_group_memberships */
2977 ADD_TO_ARRAY(p->mem_ctx, struct samr_RidWithAttribute, dom_gid, &gids, &num_gids);
2980 rids->count = num_gids;
2983 *r->out.rids = rids;
2985 DEBUG(5,("_samr_GetGroupsForUser: %d\n", __LINE__));
2990 /*******************************************************************
2991 _samr_QueryDomainInfo
2992 ********************************************************************/
2994 NTSTATUS _samr_QueryDomainInfo(pipes_struct *p,
2995 struct samr_QueryDomainInfo *r)
2997 NTSTATUS status = NT_STATUS_OK;
2998 struct samr_domain_info *dinfo;
2999 union samr_DomainInfo *dom_info;
3000 time_t u_expire, u_min_age;
3002 time_t u_lock_duration, u_reset_time;
3005 uint32 account_policy_temp;
3010 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3012 dinfo = policy_handle_find(p, r->in.domain_handle,
3013 SAMR_ACCESS_LOOKUP_DOMAIN, NULL,
3014 struct samr_domain_info, &status);
3015 if (!NT_STATUS_IS_OK(status)) {
3019 dom_info = TALLOC_ZERO_P(p->mem_ctx, union samr_DomainInfo);
3021 return NT_STATUS_NO_MEMORY;
3024 switch (r->in.level) {
3031 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
3032 &account_policy_temp);
3033 dom_info->info1.min_password_length = account_policy_temp;
3035 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
3036 dom_info->info1.password_history_length = account_policy_temp;
3038 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
3039 &dom_info->info1.password_properties);
3041 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
3042 u_expire = account_policy_temp;
3044 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
3045 u_min_age = account_policy_temp;
3051 unix_to_nt_time_abs((NTTIME *)&dom_info->info1.max_password_age, u_expire);
3052 unix_to_nt_time_abs((NTTIME *)&dom_info->info1.min_password_age, u_min_age);
3054 if (lp_check_password_script() && *lp_check_password_script()) {
3055 dom_info->info1.password_properties |= DOMAIN_PASSWORD_COMPLEX;
3065 dom_info->general.num_users = count_sam_users(
3066 dinfo->disp_info, ACB_NORMAL);
3067 dom_info->general.num_groups = count_sam_groups(
3069 dom_info->general.num_aliases = count_sam_aliases(
3072 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &u_logout);
3074 unix_to_nt_time_abs(&dom_info->general.force_logoff_time, u_logout);
3076 if (!pdb_get_seq_num(&seq_num))
3077 seq_num = time(NULL);
3083 server_role = ROLE_DOMAIN_PDC;
3084 if (lp_server_role() == ROLE_DOMAIN_BDC)
3085 server_role = ROLE_DOMAIN_BDC;
3087 dom_info->general.oem_information.string = lp_serverstring();
3088 dom_info->general.domain_name.string = lp_workgroup();
3089 dom_info->general.primary.string = global_myname();
3090 dom_info->general.sequence_num = seq_num;
3091 dom_info->general.domain_server_state = DOMAIN_SERVER_ENABLED;
3092 dom_info->general.role = server_role;
3093 dom_info->general.unknown3 = 1;
3104 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
3105 u_logout = (time_t)ul;
3112 unix_to_nt_time_abs(&dom_info->info3.force_logoff_time, u_logout);
3116 dom_info->oem.oem_information.string = lp_serverstring();
3119 dom_info->info5.domain_name.string = get_global_sam_name();
3122 /* NT returns its own name when a PDC. win2k and later
3123 * only the name of the PDC if itself is a BDC (samba4
3125 dom_info->info6.primary.string = global_myname();
3128 server_role = ROLE_DOMAIN_PDC;
3129 if (lp_server_role() == ROLE_DOMAIN_BDC)
3130 server_role = ROLE_DOMAIN_BDC;
3132 dom_info->info7.role = server_role;
3140 if (!pdb_get_seq_num(&seq_num)) {
3141 seq_num = time(NULL);
3148 dom_info->info8.sequence_num = seq_num;
3149 dom_info->info8.domain_create_time = 0;
3158 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
3159 u_lock_duration = account_policy_temp;
3160 if (u_lock_duration != -1) {
3161 u_lock_duration *= 60;
3164 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
3165 u_reset_time = account_policy_temp * 60;
3167 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT,
3168 &account_policy_temp);
3169 dom_info->info12.lockout_threshold = account_policy_temp;
3175 unix_to_nt_time_abs(&dom_info->info12.lockout_duration,
3177 unix_to_nt_time_abs(&dom_info->info12.lockout_window,
3182 return NT_STATUS_INVALID_INFO_CLASS;
3185 *r->out.info = dom_info;
3187 DEBUG(5,("_samr_QueryDomainInfo: %d\n", __LINE__));
3192 /* W2k3 seems to use the same check for all 3 objects that can be created via
3193 * SAMR, if you try to create for example "Dialup" as an alias it says
3194 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
3197 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
3199 enum lsa_SidType type;
3202 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
3205 /* Lookup in our local databases (LOOKUP_NAME_REMOTE not set)
3206 * whether the name already exists */
3207 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_LOCAL,
3208 NULL, NULL, NULL, &type);
3212 DEBUG(10, ("%s does not exist, can create it\n", new_name));
3213 return NT_STATUS_OK;
3216 DEBUG(5, ("trying to create %s, exists as %s\n",
3217 new_name, sid_type_lookup(type)));
3219 if (type == SID_NAME_DOM_GRP) {
3220 return NT_STATUS_GROUP_EXISTS;
3222 if (type == SID_NAME_ALIAS) {
3223 return NT_STATUS_ALIAS_EXISTS;
3226 /* Yes, the default is NT_STATUS_USER_EXISTS */
3227 return NT_STATUS_USER_EXISTS;
3230 /*******************************************************************
3232 ********************************************************************/
3234 NTSTATUS _samr_CreateUser2(pipes_struct *p,
3235 struct samr_CreateUser2 *r)
3237 const char *account = NULL;
3239 uint32_t acb_info = r->in.acct_flags;
3240 struct samr_domain_info *dinfo;
3241 struct samr_user_info *uinfo;
3246 /* check this, when giving away 'add computer to domain' privs */
3247 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
3248 bool can_add_account = False;
3251 dinfo = policy_handle_find(p, r->in.domain_handle,
3252 SAMR_DOMAIN_ACCESS_CREATE_USER, NULL,
3253 struct samr_domain_info, &nt_status);
3254 if (!NT_STATUS_IS_OK(nt_status)) {
3258 if (sid_check_is_builtin(&dinfo->sid)) {
3259 DEBUG(5,("_samr_CreateUser2: Refusing user create in BUILTIN\n"));
3260 return NT_STATUS_ACCESS_DENIED;
3263 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
3264 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
3265 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
3266 this parameter is not an account type */
3267 return NT_STATUS_INVALID_PARAMETER;
3270 account = r->in.account_name->string;
3271 if (account == NULL) {
3272 return NT_STATUS_NO_MEMORY;
3275 nt_status = can_create(p->mem_ctx, account);
3276 if (!NT_STATUS_IS_OK(nt_status)) {
3280 /* determine which user right we need to check based on the acb_info */
3282 if ( acb_info & ACB_WSTRUST )
3284 se_priv_copy( &se_rights, &se_machine_account );
3285 can_add_account = user_has_privileges(
3286 p->server_info->ptok, &se_rights );
3288 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
3289 account for domain trusts and changes the ACB flags later */
3290 else if ( acb_info & ACB_NORMAL &&
3291 (account[strlen(account)-1] != '$') )
3293 se_priv_copy( &se_rights, &se_add_users );
3294 can_add_account = user_has_privileges(
3295 p->server_info->ptok, &se_rights );
3297 else /* implicit assumption of a BDC or domain trust account here
3298 * (we already check the flags earlier) */
3300 if ( lp_enable_privileges() ) {
3301 /* only Domain Admins can add a BDC or domain trust */
3302 se_priv_copy( &se_rights, &se_priv_none );
3303 can_add_account = nt_token_check_domain_rid(
3304 p->server_info->ptok,
3305 DOMAIN_GROUP_RID_ADMINS );
3309 DEBUG(5, ("_samr_CreateUser2: %s can add this account : %s\n",
3310 uidtoname(p->server_info->utok.uid),
3311 can_add_account ? "True":"False" ));
3313 /********** BEGIN Admin BLOCK **********/
3315 if ( can_add_account )
3318 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
3321 if ( can_add_account )
3324 /********** END Admin BLOCK **********/
3326 /* now check for failure */
3328 if ( !NT_STATUS_IS_OK(nt_status) )
3331 /* Get the user's SID */
3333 sid_compose(&sid, get_global_sam_sid(), *r->out.rid);
3335 map_max_allowed_access(p->server_info->ptok, &des_access);
3337 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
3338 &sid, SAMR_USR_RIGHTS_WRITE_PW);
3339 se_map_generic(&des_access, &usr_generic_mapping);
3341 nt_status = access_check_samr_object(psd, p->server_info->ptok,
3342 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
3343 &acc_granted, "_samr_CreateUser2");
3345 if ( !NT_STATUS_IS_OK(nt_status) ) {
3349 uinfo = policy_handle_create(p, r->out.user_handle, acc_granted,
3350 struct samr_user_info, &nt_status);
3351 if (!NT_STATUS_IS_OK(nt_status)) {
3356 /* After a "set" ensure we have no cached display info. */
3357 force_flush_samr_cache(&sid);
3359 *r->out.access_granted = acc_granted;
3361 return NT_STATUS_OK;
3364 /****************************************************************
3365 ****************************************************************/
3367 NTSTATUS _samr_CreateUser(pipes_struct *p,
3368 struct samr_CreateUser *r)
3370 struct samr_CreateUser2 c;
3371 uint32_t access_granted;
3373 c.in.domain_handle = r->in.domain_handle;
3374 c.in.account_name = r->in.account_name;
3375 c.in.acct_flags = ACB_NORMAL;
3376 c.in.access_mask = r->in.access_mask;
3377 c.out.user_handle = r->out.user_handle;
3378 c.out.access_granted = &access_granted;
3379 c.out.rid = r->out.rid;
3381 return _samr_CreateUser2(p, &c);
3384 /*******************************************************************
3386 ********************************************************************/
3388 NTSTATUS _samr_Connect(pipes_struct *p,
3389 struct samr_Connect *r)
3391 struct samr_connect_info *info;
3392 uint32_t acc_granted;
3393 struct policy_handle hnd;
3394 uint32 des_access = r->in.access_mask;
3399 if (!pipe_access_check(p)) {
3400 DEBUG(3, ("access denied to _samr_Connect\n"));
3401 return NT_STATUS_ACCESS_DENIED;
3404 /* don't give away the farm but this is probably ok. The SAMR_ACCESS_ENUM_DOMAINS
3405 was observed from a win98 client trying to enumerate users (when configured
3406 user level access control on shares) --jerry */
3408 map_max_allowed_access(p->server_info->ptok, &des_access);
3410 se_map_generic( &des_access, &sam_generic_mapping );
3412 acc_granted = des_access & (SAMR_ACCESS_ENUM_DOMAINS
3413 |SAMR_ACCESS_LOOKUP_DOMAIN);
3415 /* set up the SAMR connect_anon response */
3417 info = policy_handle_create(p, &hnd, acc_granted,
3418 struct samr_connect_info,
3420 if (!NT_STATUS_IS_OK(status)) {
3424 *r->out.connect_handle = hnd;
3425 return NT_STATUS_OK;
3428 /*******************************************************************
3430 ********************************************************************/
3432 NTSTATUS _samr_Connect2(pipes_struct *p,
3433 struct samr_Connect2 *r)
3435 struct samr_connect_info *info = NULL;
3436 struct policy_handle hnd;
3437 SEC_DESC *psd = NULL;
3439 uint32 des_access = r->in.access_mask;
3442 const char *fn = "_samr_Connect2";
3444 switch (p->hdr_req.opnum) {
3445 case NDR_SAMR_CONNECT2:
3446 fn = "_samr_Connect2";
3448 case NDR_SAMR_CONNECT3:
3449 fn = "_samr_Connect3";
3451 case NDR_SAMR_CONNECT4:
3452 fn = "_samr_Connect4";
3454 case NDR_SAMR_CONNECT5:
3455 fn = "_samr_Connect5";
3459 DEBUG(5,("%s: %d\n", fn, __LINE__));
3463 if (!pipe_access_check(p)) {
3464 DEBUG(3, ("access denied to %s\n", fn));
3465 return NT_STATUS_ACCESS_DENIED;
3468 map_max_allowed_access(p->server_info->ptok, &des_access);
3470 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
3471 se_map_generic(&des_access, &sam_generic_mapping);
3473 nt_status = access_check_samr_object(psd, p->server_info->ptok,
3474 NULL, 0, des_access, &acc_granted, fn);
3476 if ( !NT_STATUS_IS_OK(nt_status) )
3479 info = policy_handle_create(p, &hnd, acc_granted,
3480 struct samr_connect_info, &nt_status);
3481 if (!NT_STATUS_IS_OK(nt_status)) {
3485 DEBUG(5,("%s: %d\n", fn, __LINE__));
3487 *r->out.connect_handle = hnd;
3488 return NT_STATUS_OK;
3491 /****************************************************************
3493 ****************************************************************/
3495 NTSTATUS _samr_Connect3(pipes_struct *p,
3496 struct samr_Connect3 *r)
3498 struct samr_Connect2 c;
3500 c.in.system_name = r->in.system_name;
3501 c.in.access_mask = r->in.access_mask;
3502 c.out.connect_handle = r->out.connect_handle;
3504 return _samr_Connect2(p, &c);
3507 /*******************************************************************
3509 ********************************************************************/
3511 NTSTATUS _samr_Connect4(pipes_struct *p,
3512 struct samr_Connect4 *r)
3514 struct samr_Connect2 c;
3516 c.in.system_name = r->in.system_name;
3517 c.in.access_mask = r->in.access_mask;
3518 c.out.connect_handle = r->out.connect_handle;
3520 return _samr_Connect2(p, &c);
3523 /*******************************************************************
3525 ********************************************************************/
3527 NTSTATUS _samr_Connect5(pipes_struct *p,
3528 struct samr_Connect5 *r)
3531 struct samr_Connect2 c;
3532 struct samr_ConnectInfo1 info1;
3534 info1.client_version = SAMR_CONNECT_AFTER_W2K;
3537 c.in.system_name = r->in.system_name;
3538 c.in.access_mask = r->in.access_mask;
3539 c.out.connect_handle = r->out.connect_handle;
3541 *r->out.level_out = 1;
3543 status = _samr_Connect2(p, &c);
3544 if (!NT_STATUS_IS_OK(status)) {
3548 r->out.info_out->info1 = info1;
3550 return NT_STATUS_OK;
3553 /**********************************************************************
3555 **********************************************************************/
3557 NTSTATUS _samr_LookupDomain(pipes_struct *p,
3558 struct samr_LookupDomain *r)
3561 struct samr_connect_info *info;
3562 const char *domain_name;
3563 DOM_SID *sid = NULL;
3565 /* win9x user manager likes to use SAMR_ACCESS_ENUM_DOMAINS here.
3566 Reverted that change so we will work with RAS servers again */
3568 info = policy_handle_find(p, r->in.connect_handle,
3569 SAMR_ACCESS_LOOKUP_DOMAIN, NULL,
3570 struct samr_connect_info,
3572 if (!NT_STATUS_IS_OK(status)) {
3576 domain_name = r->in.domain_name->string;
3578 return NT_STATUS_INVALID_PARAMETER;
3581 sid = TALLOC_ZERO_P(p->mem_ctx, struct dom_sid2);
3583 return NT_STATUS_NO_MEMORY;
3586 if (strequal(domain_name, builtin_domain_name())) {
3587 sid_copy(sid, &global_sid_Builtin);
3589 if (!secrets_fetch_domain_sid(domain_name, sid)) {
3590 status = NT_STATUS_NO_SUCH_DOMAIN;
3594 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name,
3595 sid_string_dbg(sid)));
3602 /**********************************************************************
3604 **********************************************************************/
3606 NTSTATUS _samr_EnumDomains(pipes_struct *p,
3607 struct samr_EnumDomains *r)
3610 struct samr_connect_info *info;
3611 uint32_t num_entries = 2;
3612 struct samr_SamEntry *entry_array = NULL;
3613 struct samr_SamArray *sam;
3615 info = policy_handle_find(p, r->in.connect_handle,
3616 SAMR_ACCESS_ENUM_DOMAINS, NULL,
3617 struct samr_connect_info, &status);
3618 if (!NT_STATUS_IS_OK(status)) {
3622 sam = TALLOC_ZERO_P(p->mem_ctx, struct samr_SamArray);
3624 return NT_STATUS_NO_MEMORY;
3627 entry_array = TALLOC_ZERO_ARRAY(p->mem_ctx,
3628 struct samr_SamEntry,
3631 return NT_STATUS_NO_MEMORY;
3634 entry_array[0].idx = 0;
3635 init_lsa_String(&entry_array[0].name, get_global_sam_name());
3637 entry_array[1].idx = 1;
3638 init_lsa_String(&entry_array[1].name, "Builtin");
3640 sam->count = num_entries;
3641 sam->entries = entry_array;
3644 *r->out.num_entries = num_entries;
3649 /*******************************************************************
3651 ********************************************************************/
3653 NTSTATUS _samr_OpenAlias(pipes_struct *p,
3654 struct samr_OpenAlias *r)
3657 uint32 alias_rid = r->in.rid;
3658 struct samr_alias_info *ainfo;
3659 struct samr_domain_info *dinfo;
3660 SEC_DESC *psd = NULL;
3662 uint32 des_access = r->in.access_mask;
3667 dinfo = policy_handle_find(p, r->in.domain_handle,
3668 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
3669 struct samr_domain_info, &status);
3670 if (!NT_STATUS_IS_OK(status)) {
3674 /* append the alias' RID to it */
3676 if (!sid_compose(&sid, &dinfo->sid, alias_rid))
3677 return NT_STATUS_NO_SUCH_ALIAS;
3679 /*check if access can be granted as requested by client. */
3681 map_max_allowed_access(p->server_info->ptok, &des_access);
3683 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
3684 se_map_generic(&des_access,&ali_generic_mapping);
3686 se_priv_copy( &se_rights, &se_add_users );
3689 status = access_check_samr_object(psd, p->server_info->ptok,
3690 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
3691 &acc_granted, "_samr_OpenAlias");
3693 if ( !NT_STATUS_IS_OK(status) )
3697 /* Check we actually have the requested alias */
3698 enum lsa_SidType type;
3703 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
3706 if (!result || (type != SID_NAME_ALIAS)) {
3707 return NT_STATUS_NO_SUCH_ALIAS;
3710 /* make sure there is a mapping */
3712 if ( !sid_to_gid( &sid, &gid ) ) {
3713 return NT_STATUS_NO_SUCH_ALIAS;
3718 ainfo = policy_handle_create(p, r->out.alias_handle, acc_granted,
3719 struct samr_alias_info, &status);
3720 if (!NT_STATUS_IS_OK(status)) {
3725 return NT_STATUS_OK;
3728 /*******************************************************************
3730 ********************************************************************/
3732 static NTSTATUS set_user_info_2(TALLOC_CTX *mem_ctx,
3733 struct samr_UserInfo2 *id2,
3737 DEBUG(5,("set_user_info_2: NULL id2\n"));
3738 return NT_STATUS_ACCESS_DENIED;
3741 copy_id2_to_sam_passwd(pwd, id2);
3743 return pdb_update_sam_account(pwd);
3746 /*******************************************************************
3748 ********************************************************************/
3750 static NTSTATUS set_user_info_4(TALLOC_CTX *mem_ctx,
3751 struct samr_UserInfo4 *id4,
3755 DEBUG(5,("set_user_info_2: NULL id4\n"));
3756 return NT_STATUS_ACCESS_DENIED;
3759 copy_id4_to_sam_passwd(pwd, id4);
3761 return pdb_update_sam_account(pwd);
3764 /*******************************************************************
3766 ********************************************************************/
3768 static NTSTATUS set_user_info_6(TALLOC_CTX *mem_ctx,
3769 struct samr_UserInfo6 *id6,
3773 DEBUG(5,("set_user_info_6: NULL id6\n"));
3774 return NT_STATUS_ACCESS_DENIED;
3777 copy_id6_to_sam_passwd(pwd, id6);
3779 return pdb_update_sam_account(pwd);
3782 /*******************************************************************
3784 ********************************************************************/
3786 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
3787 struct samr_UserInfo7 *id7,
3793 DEBUG(5, ("set_user_info_7: NULL id7\n"));
3794 return NT_STATUS_ACCESS_DENIED;
3797 if (!id7->account_name.string) {
3798 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
3799 return NT_STATUS_ACCESS_DENIED;
3802 /* check to see if the new username already exists. Note: we can't
3803 reliably lock all backends, so there is potentially the
3804 possibility that a user can be created in between this check and
3805 the rename. The rename should fail, but may not get the
3806 exact same failure status code. I think this is small enough
3807 of a window for this type of operation and the results are
3808 simply that the rename fails with a slightly different status
3809 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3811 rc = can_create(mem_ctx, id7->account_name.string);
3812 if (!NT_STATUS_IS_OK(rc)) {
3816 rc = pdb_rename_sam_account(pwd, id7->account_name.string);
3821 /*******************************************************************
3823 ********************************************************************/
3825 static NTSTATUS set_user_info_8(TALLOC_CTX *mem_ctx,
3826 struct samr_UserInfo8 *id8,
3830 DEBUG(5,("set_user_info_8: NULL id8\n"));
3831 return NT_STATUS_ACCESS_DENIED;
3834 copy_id8_to_sam_passwd(pwd, id8);
3836 return pdb_update_sam_account(pwd);
3839 /*******************************************************************
3841 ********************************************************************/
3843 static NTSTATUS set_user_info_10(TALLOC_CTX *mem_ctx,
3844 struct samr_UserInfo10 *id10,
3848 DEBUG(5,("set_user_info_8: NULL id10\n"));
3849 return NT_STATUS_ACCESS_DENIED;
3852 copy_id10_to_sam_passwd(pwd, id10);
3854 return pdb_update_sam_account(pwd);
3857 /*******************************************************************
3859 ********************************************************************/
3861 static NTSTATUS set_user_info_11(TALLOC_CTX *mem_ctx,
3862 struct samr_UserInfo11 *id11,
3866 DEBUG(5,("set_user_info_11: NULL id11\n"));
3867 return NT_STATUS_ACCESS_DENIED;
3870 copy_id11_to_sam_passwd(pwd, id11);
3872 return pdb_update_sam_account(pwd);
3875 /*******************************************************************
3877 ********************************************************************/
3879 static NTSTATUS set_user_info_12(TALLOC_CTX *mem_ctx,
3880 struct samr_UserInfo12 *id12,
3884 DEBUG(5,("set_user_info_12: NULL id12\n"));
3885 return NT_STATUS_ACCESS_DENIED;
3888 copy_id12_to_sam_passwd(pwd, id12);
3890 return pdb_update_sam_account(pwd);
3893 /*******************************************************************
3895 ********************************************************************/
3897 static NTSTATUS set_user_info_13(TALLOC_CTX *mem_ctx,
3898 struct samr_UserInfo13 *id13,
3902 DEBUG(5,("set_user_info_13: NULL id13\n"));
3903 return NT_STATUS_ACCESS_DENIED;
3906 copy_id13_to_sam_passwd(pwd, id13);
3908 return pdb_update_sam_account(pwd);
3911 /*******************************************************************
3913 ********************************************************************/
3915 static NTSTATUS set_user_info_14(TALLOC_CTX *mem_ctx,
3916 struct samr_UserInfo14 *id14,
3920 DEBUG(5,("set_user_info_14: NULL id14\n"));
3921 return NT_STATUS_ACCESS_DENIED;
3924 copy_id14_to_sam_passwd(pwd, id14);
3926 return pdb_update_sam_account(pwd);
3929 /*******************************************************************
3931 ********************************************************************/
3933 static NTSTATUS set_user_info_16(TALLOC_CTX *mem_ctx,
3934 struct samr_UserInfo16 *id16,
3938 DEBUG(5,("set_user_info_16: NULL id16\n"));
3939 return NT_STATUS_ACCESS_DENIED;
3942 copy_id16_to_sam_passwd(pwd, id16);
3944 return pdb_update_sam_account(pwd);
3947 /*******************************************************************
3949 ********************************************************************/
3951 static NTSTATUS set_user_info_17(TALLOC_CTX *mem_ctx,
3952 struct samr_UserInfo17 *id17,
3956 DEBUG(5,("set_user_info_17: NULL id17\n"));
3957 return NT_STATUS_ACCESS_DENIED;
3960 copy_id17_to_sam_passwd(pwd, id17);
3962 return pdb_update_sam_account(pwd);
3965 /*******************************************************************
3967 ********************************************************************/
3969 static NTSTATUS set_user_info_18(struct samr_UserInfo18 *id18,
3970 TALLOC_CTX *mem_ctx,
3971 DATA_BLOB *session_key,
3975 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3976 return NT_STATUS_INVALID_PARAMETER;
3979 if (id18->nt_pwd_active || id18->lm_pwd_active) {
3980 if (!session_key->length) {
3981 return NT_STATUS_NO_USER_SESSION_KEY;
3985 if (id18->nt_pwd_active) {
3989 in = data_blob_const(id18->nt_pwd.hash, 16);
3990 out = data_blob_talloc_zero(mem_ctx, 16);
3992 sess_crypt_blob(&out, &in, session_key, false);
3994 if (!pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED)) {
3995 return NT_STATUS_ACCESS_DENIED;
3998 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4001 if (id18->lm_pwd_active) {
4005 in = data_blob_const(id18->lm_pwd.hash, 16);
4006 out = data_blob_talloc_zero(mem_ctx, 16);
4008 sess_crypt_blob(&out, &in, session_key, false);
4010 if (!pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED)) {
4011 return NT_STATUS_ACCESS_DENIED;
4014 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4017 copy_id18_to_sam_passwd(pwd, id18);
4019 return pdb_update_sam_account(pwd);
4022 /*******************************************************************
4024 ********************************************************************/
4026 static NTSTATUS set_user_info_20(TALLOC_CTX *mem_ctx,
4027 struct samr_UserInfo20 *id20,
4031 DEBUG(5,("set_user_info_20: NULL id20\n"));
4032 return NT_STATUS_ACCESS_DENIED;
4035 copy_id20_to_sam_passwd(pwd, id20);
4037 return pdb_update_sam_account(pwd);
4040 /*******************************************************************
4042 ********************************************************************/
4044 static NTSTATUS set_user_info_21(struct samr_UserInfo21 *id21,
4045 TALLOC_CTX *mem_ctx,
4046 DATA_BLOB *session_key,
4052 DEBUG(5, ("set_user_info_21: NULL id21\n"));
4053 return NT_STATUS_INVALID_PARAMETER;
4056 if (id21->fields_present == 0) {
4057 return NT_STATUS_INVALID_PARAMETER;
4060 if (id21->fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4061 return NT_STATUS_ACCESS_DENIED;
4064 if (id21->fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) {
4065 if (id21->nt_password_set) {
4068 if ((id21->nt_owf_password.length != 16) ||
4069 (id21->nt_owf_password.size != 16)) {
4070 return NT_STATUS_INVALID_PARAMETER;
4073 if (!session_key->length) {
4074 return NT_STATUS_NO_USER_SESSION_KEY;
4077 in = data_blob_const(id21->nt_owf_password.array, 16);
4078 out = data_blob_talloc_zero(mem_ctx, 16);
4080 sess_crypt_blob(&out, &in, session_key, false);
4082 pdb_set_nt_passwd(pwd, out.data, PDB_CHANGED);
4083 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4087 if (id21->fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT) {
4088 if (id21->lm_password_set) {
4091 if ((id21->lm_owf_password.length != 16) ||
4092 (id21->lm_owf_password.size != 16)) {
4093 return NT_STATUS_INVALID_PARAMETER;
4096 if (!session_key->length) {
4097 return NT_STATUS_NO_USER_SESSION_KEY;
4100 in = data_blob_const(id21->lm_owf_password.array, 16);
4101 out = data_blob_talloc_zero(mem_ctx, 16);
4103 sess_crypt_blob(&out, &in, session_key, false);
4105 pdb_set_lanman_passwd(pwd, out.data, PDB_CHANGED);
4106 pdb_set_pass_last_set_time(pwd, time(NULL), PDB_CHANGED);
4110 /* we need to separately check for an account rename first */
4112 if (id21->account_name.string &&
4113 (!strequal(id21->account_name.string, pdb_get_username(pwd))))
4116 /* check to see if the new username already exists. Note: we can't
4117 reliably lock all backends, so there is potentially the
4118 possibility that a user can be created in between this check and
4119 the rename. The rename should fail, but may not get the
4120 exact same failure status code. I think this is small enough
4121 of a window for this type of operation and the results are
4122 simply that the rename fails with a slightly different status
4123 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
4125 status = can_create(mem_ctx, id21->account_name.string);
4126 if (!NT_STATUS_IS_OK(status)) {
4130 status = pdb_rename_sam_account(pwd, id21->account_name.string);
4132 if (!NT_STATUS_IS_OK(status)) {
4133 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
4134 nt_errstr(status)));
4138 /* set the new username so that later
4139 functions can work on the new account */
4140 pdb_set_username(pwd, id21->account_name.string, PDB_SET);
4143 copy_id21_to_sam_passwd("INFO_21", pwd, id21);
4146 * The funny part about the previous two calls is
4147 * that pwd still has the password hashes from the
4148 * passdb entry. These have not been updated from
4149 * id21. I don't know if they need to be set. --jerry
4152 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4153 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4154 if ( !NT_STATUS_IS_OK(status) ) {
4159 /* Don't worry about writing out the user account since the
4160 primary group SID is generated solely from the user's Unix
4163 /* write the change out */
4164 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4168 return NT_STATUS_OK;
4171 /*******************************************************************
4173 ********************************************************************/
4175 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx,
4176 struct samr_UserInfo23 *id23,
4179 char *plaintext_buf = NULL;
4185 DEBUG(5, ("set_user_info_23: NULL id23\n"));
4186 return NT_STATUS_INVALID_PARAMETER;
4189 if (id23->info.fields_present == 0) {
4190 return NT_STATUS_INVALID_PARAMETER;
4193 if (id23->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4194 return NT_STATUS_ACCESS_DENIED;
4197 if ((id23->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4198 (id23->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4200 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
4201 pdb_get_username(pwd)));
4203 if (!decode_pw_buffer(mem_ctx,
4204 id23->password.data,
4208 return NT_STATUS_WRONG_PASSWORD;
4211 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4212 return NT_STATUS_ACCESS_DENIED;
4216 copy_id23_to_sam_passwd(pwd, id23);
4218 acct_ctrl = pdb_get_acct_ctrl(pwd);
4220 /* if it's a trust account, don't update /etc/passwd */
4221 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4222 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4223 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4224 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
4225 } else if (plaintext_buf) {
4226 /* update the UNIX password */
4227 if (lp_unix_password_sync() ) {
4228 struct passwd *passwd;
4229 if (pdb_get_username(pwd) == NULL) {
4230 DEBUG(1, ("chgpasswd: User without name???\n"));
4231 return NT_STATUS_ACCESS_DENIED;
4234 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4235 if (passwd == NULL) {
4236 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4239 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
4240 return NT_STATUS_ACCESS_DENIED;
4242 TALLOC_FREE(passwd);
4246 if (plaintext_buf) {
4247 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4250 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
4251 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
4256 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4260 return NT_STATUS_OK;
4263 /*******************************************************************
4265 ********************************************************************/
4267 static bool set_user_info_pw(uint8 *pass, struct samu *pwd)
4270 char *plaintext_buf = NULL;
4273 DEBUG(5, ("Attempting administrator password change for user %s\n",
4274 pdb_get_username(pwd)));
4276 acct_ctrl = pdb_get_acct_ctrl(pwd);
4278 if (!decode_pw_buffer(talloc_tos(),
4286 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
4290 /* if it's a trust account, don't update /etc/passwd */
4291 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
4292 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
4293 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
4294 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
4296 /* update the UNIX password */
4297 if (lp_unix_password_sync()) {
4298 struct passwd *passwd;
4300 if (pdb_get_username(pwd) == NULL) {
4301 DEBUG(1, ("chgpasswd: User without name???\n"));
4305 passwd = Get_Pwnam_alloc(pwd, pdb_get_username(pwd));
4306 if (passwd == NULL) {
4307 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
4310 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
4313 TALLOC_FREE(passwd);
4317 memset(plaintext_buf, '\0', strlen(plaintext_buf));
4319 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
4324 /*******************************************************************
4326 ********************************************************************/
4328 static NTSTATUS set_user_info_24(TALLOC_CTX *mem_ctx,
4329 struct samr_UserInfo24 *id24,
4335 DEBUG(5, ("set_user_info_24: NULL id24\n"));
4336 return NT_STATUS_INVALID_PARAMETER;
4339 if (!set_user_info_pw(id24->password.data, pwd)) {
4340 return NT_STATUS_WRONG_PASSWORD;
4343 copy_id24_to_sam_passwd(pwd, id24);
4345 status = pdb_update_sam_account(pwd);
4346 if (!NT_STATUS_IS_OK(status)) {
4350 return NT_STATUS_OK;
4353 /*******************************************************************
4355 ********************************************************************/
4357 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx,
4358 struct samr_UserInfo25 *id25,
4364 DEBUG(5, ("set_user_info_25: NULL id25\n"));
4365 return NT_STATUS_INVALID_PARAMETER;
4368 if (id25->info.fields_present == 0) {
4369 return NT_STATUS_INVALID_PARAMETER;
4372 if (id25->info.fields_present & SAMR_FIELD_LAST_PWD_CHANGE) {
4373 return NT_STATUS_ACCESS_DENIED;
4376 if ((id25->info.fields_present & SAMR_FIELD_NT_PASSWORD_PRESENT) ||
4377 (id25->info.fields_present & SAMR_FIELD_LM_PASSWORD_PRESENT)) {
4379 if (!set_user_info_pw(id25->password.data, pwd)) {
4380 return NT_STATUS_WRONG_PASSWORD;
4384 copy_id25_to_sam_passwd(pwd, id25);
4386 /* write the change out */
4387 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
4392 * We need to "pdb_update_sam_account" before the unix primary group
4393 * is set, because the idealx scripts would also change the
4394 * sambaPrimaryGroupSid using the ldap replace method. pdb_ldap uses
4395 * the delete explicit / add explicit, which would then fail to find
4396 * the previous primaryGroupSid value.
4399 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
4400 status = pdb_set_unix_primary_group(mem_ctx, pwd);
4401 if ( !NT_STATUS_IS_OK(status) ) {
4406 return NT_STATUS_OK;
4409 /*******************************************************************
4411 ********************************************************************/
4413 static NTSTATUS set_user_info_26(TALLOC_CTX *mem_ctx,
4414 struct samr_UserInfo26 *id26,
4420 DEBUG(5, ("set_user_info_26: NULL id26\n"));
4421 return NT_STATUS_INVALID_PARAMETER;
4424 if (!set_user_info_pw(id26->password.data, pwd)) {
4425 return NT_STATUS_WRONG_PASSWORD;
4428 copy_id26_to_sam_passwd(pwd, id26);
4430 status = pdb_update_sam_account(pwd);
4431 if (!NT_STATUS_IS_OK(status)) {
4435 return NT_STATUS_OK;
4439 /*******************************************************************
4441 ********************************************************************/
4443 NTSTATUS _samr_SetUserInfo(pipes_struct *p,
4444 struct samr_SetUserInfo *r)
4446 struct samr_user_info *uinfo;
4448 struct samu *pwd = NULL;
4449 union samr_UserInfo *info = r->in.info;
4450 uint16_t switch_value = r->in.level;
4451 uint32_t acc_required;
4453 bool has_enough_rights = False;
4456 DEBUG(5,("_samr_SetUserInfo: %d\n", __LINE__));
4458 /* This is tricky. A WinXP domain join sets
4459 (SAMR_USER_ACCESS_SET_PASSWORD|SAMR_USER_ACCESS_SET_ATTRIBUTES|SAMR_USER_ACCESS_GET_ATTRIBUTES)
4460 The MMC lusrmgr plugin includes these perms and more in the SamrOpenUser(). But the
4461 standard Win32 API calls just ask for SAMR_USER_ACCESS_SET_PASSWORD in the SamrOpenUser().
4462 This should be enough for levels 18, 24, 25,& 26. Info level 23 can set more so
4463 we'll use the set from the WinXP join as the basis. */
4465 switch (switch_value) {
4470 acc_required = SAMR_USER_ACCESS_SET_PASSWORD;
4473 acc_required = SAMR_USER_ACCESS_SET_PASSWORD |
4474 SAMR_USER_ACCESS_SET_ATTRIBUTES |
4475 SAMR_USER_ACCESS_GET_ATTRIBUTES;
4479 uinfo = policy_handle_find(p, r->in.user_handle, acc_required, NULL,
4480 struct samr_user_info, &status);
4481 if (!NT_STATUS_IS_OK(status)) {
4485 DEBUG(5, ("_samr_SetUserInfo: sid:%s, level:%d\n",
4486 sid_string_dbg(&uinfo->sid), switch_value));
4489 DEBUG(5, ("_samr_SetUserInfo: NULL info level\n"));
4490 return NT_STATUS_INVALID_INFO_CLASS;
4493 if (!(pwd = samu_new(NULL))) {
4494 return NT_STATUS_NO_MEMORY;
4498 ret = pdb_getsampwsid(pwd, &uinfo->sid);
4503 return NT_STATUS_NO_SUCH_USER;
4506 /* deal with machine password changes differently from userinfo changes */
4507 /* check to see if we have the sufficient rights */
4509 acb_info = pdb_get_acct_ctrl(pwd);
4510 if (acb_info & ACB_WSTRUST)
4511 has_enough_rights = user_has_privileges(p->server_info->ptok,
4512 &se_machine_account);
4513 else if (acb_info & ACB_NORMAL)
4514 has_enough_rights = user_has_privileges(p->server_info->ptok,
4516 else if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
4517 if (lp_enable_privileges()) {
4518 has_enough_rights = nt_token_check_domain_rid(p->server_info->ptok,
4519 DOMAIN_GROUP_RID_ADMINS);
4523 DEBUG(5, ("_samr_SetUserInfo: %s does%s possess sufficient rights\n",
4524 uidtoname(p->server_info->utok.uid),
4525 has_enough_rights ? "" : " not"));
4527 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
4529 if (has_enough_rights) {
4533 /* ok! user info levels (lots: see MSDEV help), off we go... */
4535 switch (switch_value) {
4538 status = set_user_info_2(p->mem_ctx,
4543 status = set_user_info_4(p->mem_ctx,
4548 status = set_user_info_6(p->mem_ctx,
4553 status = set_user_info_7(p->mem_ctx,
4558 status = set_user_info_8(p->mem_ctx,
4563 status = set_user_info_10(p->mem_ctx,
4564 &info->info10, pwd);
4568 status = set_user_info_11(p->mem_ctx,
4569 &info->info11, pwd);
4573 status = set_user_info_12(p->mem_ctx,
4574 &info->info12, pwd);
4578 status = set_user_info_13(p->mem_ctx,
4579 &info->info13, pwd);
4583 status = set_user_info_14(p->mem_ctx,
4584 &info->info14, pwd);
4588 status = set_user_info_16(p->mem_ctx,
4589 &info->info16, pwd);
4593 status = set_user_info_17(p->mem_ctx,
4594 &info->info17, pwd);
4598 /* Used by AS/U JRA. */
4599 status = set_user_info_18(&info->info18,
4601 &p->server_info->user_session_key,
4606 status = set_user_info_20(p->mem_ctx,
4607 &info->info20, pwd);
4611 status = set_user_info_21(&info->info21,
4613 &p->server_info->user_session_key,
4618 if (!p->server_info->user_session_key.length) {
4619 status = NT_STATUS_NO_USER_SESSION_KEY;
4621 arcfour_crypt_blob(info->info23.password.data, 516,
4622 &p->server_info->user_session_key);
4624 dump_data(100, info->info23.password.data, 516);
4626 status = set_user_info_23(p->mem_ctx,
4627 &info->info23, pwd);
4631 if (!p->server_info->user_session_key.length) {
4632 status = NT_STATUS_NO_USER_SESSION_KEY;
4634 arcfour_crypt_blob(info->info24.password.data,
4636 &p->server_info->user_session_key);
4638 dump_data(100, info->info24.password.data, 516);
4640 status = set_user_info_24(p->mem_ctx,
4641 &info->info24, pwd);
4645 if (!p->server_info->user_session_key.length) {
4646 status = NT_STATUS_NO_USER_SESSION_KEY;
4648 encode_or_decode_arc4_passwd_buffer(
4649 info->info25.password.data,
4650 &p->server_info->user_session_key);
4652 dump_data(100, info->info25.password.data, 532);
4654 status = set_user_info_25(p->mem_ctx,
4655 &info->info25, pwd);
4659 if (!p->server_info->user_session_key.length) {
4660 status = NT_STATUS_NO_USER_SESSION_KEY;
4662 encode_or_decode_arc4_passwd_buffer(
4663 info->info26.password.data,
4664 &p->server_info->user_session_key);
4666 dump_data(100, info->info26.password.data, 516);
4668 status = set_user_info_26(p->mem_ctx,
4669 &info->info26, pwd);
4673 status = NT_STATUS_INVALID_INFO_CLASS;
4678 if (has_enough_rights) {
4682 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
4684 if (NT_STATUS_IS_OK(status)) {
4685 force_flush_samr_cache(&uinfo->sid);
4691 /*******************************************************************
4693 ********************************************************************/
4695 NTSTATUS _samr_SetUserInfo2(pipes_struct *p,
4696 struct samr_SetUserInfo2 *r)
4698 struct samr_SetUserInfo q;
4700 q.in.user_handle = r->in.user_handle;
4701 q.in.level = r->in.level;
4702 q.in.info = r->in.info;
4704 return _samr_SetUserInfo(p, &q);
4707 /*********************************************************************
4708 _samr_GetAliasMembership
4709 *********************************************************************/
4711 NTSTATUS _samr_GetAliasMembership(pipes_struct *p,
4712 struct samr_GetAliasMembership *r)
4714 size_t num_alias_rids;
4716 struct samr_domain_info *dinfo;
4723 DEBUG(5,("_samr_GetAliasMembership: %d\n", __LINE__));
4725 dinfo = policy_handle_find(p, r->in.domain_handle,
4726 SAMR_DOMAIN_ACCESS_LOOKUP_ALIAS
4727 | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
4728 struct samr_domain_info, &status);
4729 if (!NT_STATUS_IS_OK(status)) {
4733 if (!sid_check_is_domain(&dinfo->sid) &&
4734 !sid_check_is_builtin(&dinfo->sid))
4735 return NT_STATUS_OBJECT_TYPE_MISMATCH;
4737 if (r->in.sids->num_sids) {
4738 members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, r->in.sids->num_sids);
4740 if (members == NULL)
4741 return NT_STATUS_NO_MEMORY;
4746 for (i=0; i<r->in.sids->num_sids; i++)
4747 sid_copy(&members[i], r->in.sids->sids[i].sid);
4753 status = pdb_enum_alias_memberships(p->mem_ctx, &dinfo->sid, members,
4754 r->in.sids->num_sids,
4755 &alias_rids, &num_alias_rids);
4758 if (!NT_STATUS_IS_OK(status)) {
4762 r->out.rids->count = num_alias_rids;
4763 r->out.rids->ids = alias_rids;
4765 return NT_STATUS_OK;
4768 /*********************************************************************
4769 _samr_GetMembersInAlias
4770 *********************************************************************/
4772 NTSTATUS _samr_GetMembersInAlias(pipes_struct *p,
4773 struct samr_GetMembersInAlias *r)
4775 struct samr_alias_info *ainfo;
4778 size_t num_sids = 0;
4779 struct lsa_SidPtr *sids = NULL;
4780 DOM_SID *pdb_sids = NULL;
4782 ainfo = policy_handle_find(p, r->in.alias_handle,
4783 SAMR_ALIAS_ACCESS_GET_MEMBERS, NULL,
4784 struct samr_alias_info, &status);
4785 if (!NT_STATUS_IS_OK(status)) {
4789 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
4792 status = pdb_enum_aliasmem(&ainfo->sid, &pdb_sids, &num_sids);
4795 if (!NT_STATUS_IS_OK(status)) {
4800 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr, num_sids);
4802 TALLOC_FREE(pdb_sids);
4803 return NT_STATUS_NO_MEMORY;
4807 for (i = 0; i < num_sids; i++) {
4808 sids[i].sid = sid_dup_talloc(p->mem_ctx, &pdb_sids[i]);
4810 TALLOC_FREE(pdb_sids);
4811 return NT_STATUS_NO_MEMORY;
4815 r->out.sids->num_sids = num_sids;
4816 r->out.sids->sids = sids;
4818 TALLOC_FREE(pdb_sids);
4820 return NT_STATUS_OK;
4823 /*********************************************************************
4824 _samr_QueryGroupMember
4825 *********************************************************************/
4827 NTSTATUS _samr_QueryGroupMember(pipes_struct *p,
4828 struct samr_QueryGroupMember *r)
4830 struct samr_group_info *ginfo;
4831 size_t i, num_members;
4837 struct samr_RidTypeArray *rids = NULL;
4839 ginfo = policy_handle_find(p, r->in.group_handle,
4840 SAMR_GROUP_ACCESS_GET_MEMBERS, NULL,
4841 struct samr_group_info, &status);
4842 if (!NT_STATUS_IS_OK(status)) {
4846 rids = TALLOC_ZERO_P(p->mem_ctx, struct samr_RidTypeArray);
4848 return NT_STATUS_NO_MEMORY;
4851 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
4853 if (!sid_check_is_in_our_domain(&ginfo->sid)) {
4854 DEBUG(3, ("sid %s is not in our domain\n",
4855 sid_string_dbg(&ginfo->sid)));
4856 return NT_STATUS_NO_SUCH_GROUP;
4859 DEBUG(10, ("lookup on Domain SID\n"));
4862 status = pdb_enum_group_members(p->mem_ctx, &ginfo->sid,
4863 &rid, &num_members);
4866 if (!NT_STATUS_IS_OK(status))
4870 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
4872 return NT_STATUS_NO_MEMORY;
4878 for (i=0; i<num_members; i++)
4879 attr[i] = SID_NAME_USER;
4881 rids->count = num_members;
4885 *r->out.rids = rids;
4887 return NT_STATUS_OK;
4890 /*********************************************************************
4891 _samr_AddAliasMember
4892 *********************************************************************/
4894 NTSTATUS _samr_AddAliasMember(pipes_struct *p,
4895 struct samr_AddAliasMember *r)
4897 struct samr_alias_info *ainfo;
4899 bool can_add_accounts;
4902 ainfo = policy_handle_find(p, r->in.alias_handle,
4903 SAMR_ALIAS_ACCESS_ADD_MEMBER, NULL,
4904 struct samr_alias_info, &status);
4905 if (!NT_STATUS_IS_OK(status)) {
4909 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
4911 se_priv_copy( &se_rights, &se_add_users );
4912 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4914 /******** BEGIN SeAddUsers BLOCK *********/
4916 if ( can_add_accounts )
4919 status = pdb_add_aliasmem(&ainfo->sid, r->in.sid);
4921 if ( can_add_accounts )
4924 /******** END SeAddUsers BLOCK *********/
4926 if (NT_STATUS_IS_OK(status)) {
4927 force_flush_samr_cache(&ainfo->sid);
4933 /*********************************************************************
4934 _samr_DeleteAliasMember
4935 *********************************************************************/
4937 NTSTATUS _samr_DeleteAliasMember(pipes_struct *p,
4938 struct samr_DeleteAliasMember *r)
4940 struct samr_alias_info *ainfo;
4942 bool can_add_accounts;
4945 ainfo = policy_handle_find(p, r->in.alias_handle,
4946 SAMR_ALIAS_ACCESS_REMOVE_MEMBER, NULL,
4947 struct samr_alias_info, &status);
4948 if (!NT_STATUS_IS_OK(status)) {
4952 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
4953 sid_string_dbg(&ainfo->sid)));
4955 se_priv_copy( &se_rights, &se_add_users );
4956 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
4958 /******** BEGIN SeAddUsers BLOCK *********/
4960 if ( can_add_accounts )
4963 status = pdb_del_aliasmem(&ainfo->sid, r->in.sid);
4965 if ( can_add_accounts )
4968 /******** END SeAddUsers BLOCK *********/
4970 if (NT_STATUS_IS_OK(status)) {
4971 force_flush_samr_cache(&ainfo->sid);
4977 /*********************************************************************
4978 _samr_AddGroupMember
4979 *********************************************************************/
4981 NTSTATUS _samr_AddGroupMember(pipes_struct *p,
4982 struct samr_AddGroupMember *r)
4984 struct samr_group_info *ginfo;
4988 bool can_add_accounts;
4990 ginfo = policy_handle_find(p, r->in.group_handle,
4991 SAMR_GROUP_ACCESS_ADD_MEMBER, NULL,
4992 struct samr_group_info, &status);
4993 if (!NT_STATUS_IS_OK(status)) {
4997 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
4999 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5001 return NT_STATUS_INVALID_HANDLE;
5004 se_priv_copy( &se_rights, &se_add_users );
5005 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
5007 /******** BEGIN SeAddUsers BLOCK *********/
5009 if ( can_add_accounts )
5012 status = pdb_add_groupmem(p->mem_ctx, group_rid, r->in.rid);
5014 if ( can_add_accounts )
5017 /******** END SeAddUsers BLOCK *********/
5019 force_flush_samr_cache(&ginfo->sid);
5024 /*********************************************************************
5025 _samr_DeleteGroupMember
5026 *********************************************************************/
5028 NTSTATUS _samr_DeleteGroupMember(pipes_struct *p,
5029 struct samr_DeleteGroupMember *r)
5032 struct samr_group_info *ginfo;
5036 bool can_add_accounts;
5039 * delete the group member named r->in.rid
5040 * who is a member of the sid associated with the handle
5041 * the rid is a user's rid as the group is a domain group.
5044 ginfo = policy_handle_find(p, r->in.group_handle,
5045 SAMR_GROUP_ACCESS_REMOVE_MEMBER, NULL,
5046 struct samr_group_info, &status);
5047 if (!NT_STATUS_IS_OK(status)) {
5051 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5053 return NT_STATUS_INVALID_HANDLE;
5056 se_priv_copy( &se_rights, &se_add_users );
5057 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
5059 /******** BEGIN SeAddUsers BLOCK *********/
5061 if ( can_add_accounts )
5064 status = pdb_del_groupmem(p->mem_ctx, group_rid, r->in.rid);
5066 if ( can_add_accounts )
5069 /******** END SeAddUsers BLOCK *********/
5071 force_flush_samr_cache(&ginfo->sid);
5076 /*********************************************************************
5078 *********************************************************************/
5080 NTSTATUS _samr_DeleteUser(pipes_struct *p,
5081 struct samr_DeleteUser *r)
5083 struct samr_user_info *uinfo;
5085 struct samu *sam_pass=NULL;
5086 bool can_add_accounts;
5090 DEBUG(5, ("_samr_DeleteUser: %d\n", __LINE__));
5092 uinfo = policy_handle_find(p, r->in.user_handle,
5093 STD_RIGHT_DELETE_ACCESS, NULL,
5094 struct samr_user_info, &status);
5095 if (!NT_STATUS_IS_OK(status)) {
5099 if (!sid_check_is_in_our_domain(&uinfo->sid))
5100 return NT_STATUS_CANNOT_DELETE;
5102 /* check if the user exists before trying to delete */
5103 if ( !(sam_pass = samu_new( NULL )) ) {
5104 return NT_STATUS_NO_MEMORY;
5108 ret = pdb_getsampwsid(sam_pass, &uinfo->sid);
5112 DEBUG(5,("_samr_DeleteUser: User %s doesn't exist.\n",
5113 sid_string_dbg(&uinfo->sid)));
5114 TALLOC_FREE(sam_pass);
5115 return NT_STATUS_NO_SUCH_USER;
5118 acb_info = pdb_get_acct_ctrl(sam_pass);
5120 /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
5121 if ( acb_info & ACB_WSTRUST ) {
5122 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_machine_account );
5124 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_add_users );
5127 /******** BEGIN SeAddUsers BLOCK *********/
5129 if ( can_add_accounts )
5132 status = pdb_delete_user(p->mem_ctx, sam_pass);
5134 if ( can_add_accounts )
5137 /******** END SeAddUsers BLOCK *********/
5139 if ( !NT_STATUS_IS_OK(status) ) {
5140 DEBUG(5,("_samr_DeleteUser: Failed to delete entry for "
5141 "user %s: %s.\n", pdb_get_username(sam_pass),
5142 nt_errstr(status)));
5143 TALLOC_FREE(sam_pass);
5148 TALLOC_FREE(sam_pass);
5150 if (!close_policy_hnd(p, r->in.user_handle))
5151 return NT_STATUS_OBJECT_NAME_INVALID;
5153 ZERO_STRUCTP(r->out.user_handle);
5155 force_flush_samr_cache(&uinfo->sid);
5157 return NT_STATUS_OK;
5160 /*********************************************************************
5161 _samr_DeleteDomainGroup
5162 *********************************************************************/
5164 NTSTATUS _samr_DeleteDomainGroup(pipes_struct *p,
5165 struct samr_DeleteDomainGroup *r)
5167 struct samr_group_info *ginfo;
5171 bool can_add_accounts;
5173 DEBUG(5, ("samr_DeleteDomainGroup: %d\n", __LINE__));
5175 ginfo = policy_handle_find(p, r->in.group_handle,
5176 STD_RIGHT_DELETE_ACCESS, NULL,
5177 struct samr_group_info, &status);
5178 if (!NT_STATUS_IS_OK(status)) {
5182 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ginfo->sid)));
5184 if (!sid_peek_check_rid(get_global_sam_sid(), &ginfo->sid,
5186 return NT_STATUS_NO_SUCH_GROUP;
5189 se_priv_copy( &se_rights, &se_add_users );
5190 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
5192 /******** BEGIN SeAddUsers BLOCK *********/
5194 if ( can_add_accounts )
5197 status = pdb_delete_dom_group(p->mem_ctx, group_rid);
5199 if ( can_add_accounts )
5202 /******** END SeAddUsers BLOCK *********/
5204 if ( !NT_STATUS_IS_OK(status) ) {
5205 DEBUG(5,("_samr_DeleteDomainGroup: Failed to delete mapping "
5206 "entry for group %s: %s\n",
5207 sid_string_dbg(&ginfo->sid),
5208 nt_errstr(status)));
5212 if (!close_policy_hnd(p, r->in.group_handle))
5213 return NT_STATUS_OBJECT_NAME_INVALID;
5215 force_flush_samr_cache(&ginfo->sid);
5217 return NT_STATUS_OK;
5220 /*********************************************************************
5221 _samr_DeleteDomAlias
5222 *********************************************************************/
5224 NTSTATUS _samr_DeleteDomAlias(pipes_struct *p,
5225 struct samr_DeleteDomAlias *r)
5227 struct samr_alias_info *ainfo;
5229 bool can_add_accounts;
5232 DEBUG(5, ("_samr_DeleteDomAlias: %d\n", __LINE__));
5234 ainfo = policy_handle_find(p, r->in.alias_handle,
5235 STD_RIGHT_DELETE_ACCESS, NULL,
5236 struct samr_alias_info, &status);
5237 if (!NT_STATUS_IS_OK(status)) {
5241 DEBUG(10, ("sid is %s\n", sid_string_dbg(&ainfo->sid)));
5243 /* Don't let Windows delete builtin groups */
5245 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
5246 return NT_STATUS_SPECIAL_ACCOUNT;
5249 if (!sid_check_is_in_our_domain(&ainfo->sid))
5250 return NT_STATUS_NO_SUCH_ALIAS;
5252 DEBUG(10, ("lookup on Local SID\n"));
5254 se_priv_copy( &se_rights, &se_add_users );
5255 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
5257 /******** BEGIN SeAddUsers BLOCK *********/
5259 if ( can_add_accounts )
5262 /* Have passdb delete the alias */
5263 status = pdb_delete_alias(&ainfo->sid);
5265 if ( can_add_accounts )
5268 /******** END SeAddUsers BLOCK *********/
5270 if ( !NT_STATUS_IS_OK(status))
5273 if (!close_policy_hnd(p, r->in.alias_handle))
5274 return NT_STATUS_OBJECT_NAME_INVALID;
5276 force_flush_samr_cache(&ainfo->sid);
5278 return NT_STATUS_OK;
5281 /*********************************************************************
5282 _samr_CreateDomainGroup
5283 *********************************************************************/
5285 NTSTATUS _samr_CreateDomainGroup(pipes_struct *p,
5286 struct samr_CreateDomainGroup *r)
5291 struct samr_domain_info *dinfo;
5292 struct samr_group_info *ginfo;
5294 bool can_add_accounts;
5296 dinfo = policy_handle_find(p, r->in.domain_handle,
5297 SAMR_DOMAIN_ACCESS_CREATE_GROUP, NULL,
5298 struct samr_domain_info, &status);
5299 if (!NT_STATUS_IS_OK(status)) {
5303 if (!sid_equal(&dinfo->sid, get_global_sam_sid()))
5304 return NT_STATUS_ACCESS_DENIED;
5306 name = r->in.name->string;
5308 return NT_STATUS_NO_MEMORY;
5311 status = can_create(p->mem_ctx, name);
5312 if (!NT_STATUS_IS_OK(status)) {
5316 se_priv_copy( &se_rights, &se_add_users );
5317 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
5319 /******** BEGIN SeAddUsers BLOCK *********/
5321 if ( can_add_accounts )
5324 /* check that we successfully create the UNIX group */
5326 status = pdb_create_dom_group(p->mem_ctx, name, r->out.rid);
5328 if ( can_add_accounts )
5331 /******** END SeAddUsers BLOCK *********/
5333 /* check if we should bail out here */
5335 if ( !NT_STATUS_IS_OK(status) )
5338 ginfo = policy_handle_create(p, r->out.group_handle,
5339 GENERIC_RIGHTS_GROUP_ALL_ACCESS,
5340 struct samr_group_info, &status);
5341 if (!NT_STATUS_IS_OK(status)) {
5344 sid_compose(&ginfo->sid, &dinfo->sid, *r->out.rid);
5346 force_flush_samr_cache(&dinfo->sid);
5348 return NT_STATUS_OK;
5351 /*********************************************************************
5352 _samr_CreateDomAlias
5353 *********************************************************************/
5355 NTSTATUS _samr_CreateDomAlias(pipes_struct *p,
5356 struct samr_CreateDomAlias *r)
5359 const char *name = NULL;
5360 struct samr_domain_info *dinfo;
5361 struct samr_alias_info *ainfo;
5365 bool can_add_accounts;
5367 dinfo = policy_handle_find(p, r->in.domain_handle,
5368 SAMR_DOMAIN_ACCESS_CREATE_ALIAS, NULL,
5369 struct samr_domain_info, &result);
5370 if (!NT_STATUS_IS_OK(result)) {
5374 if (!sid_equal(&dinfo->sid, get_global_sam_sid()))
5375 return NT_STATUS_ACCESS_DENIED;
5377 name = r->in.alias_name->string;
5379 se_priv_copy( &se_rights, &se_add_users );
5380 can_add_accounts = user_has_privileges( p->server_info->ptok, &se_rights );
5382 result = can_create(p->mem_ctx, name);
5383 if (!NT_STATUS_IS_OK(result)) {
5387 /******** BEGIN SeAddUsers BLOCK *********/
5389 if ( can_add_accounts )
5392 /* Have passdb create the alias */
5393 result = pdb_create_alias(name, r->out.rid);
5395 if ( can_add_accounts )
5398 /******** END SeAddUsers BLOCK *********/
5400 if (!NT_STATUS_IS_OK(result)) {
5401 DEBUG(10, ("pdb_create_alias failed: %s\n",
5402 nt_errstr(result)));
5406 sid_compose(&info_sid, &dinfo->sid, *r->out.rid);
5408 if (!sid_to_gid(&info_sid, &gid)) {
5409 DEBUG(10, ("Could not find alias just created\n"));
5410 return NT_STATUS_ACCESS_DENIED;
5413 /* check if the group has been successfully created */
5414 if ( getgrgid(gid) == NULL ) {
5415 DEBUG(10, ("getgrgid(%d) of just created alias failed\n",
5417 return NT_STATUS_ACCESS_DENIED;
5420 ainfo = policy_handle_create(p, r->out.alias_handle,
5421 GENERIC_RIGHTS_ALIAS_ALL_ACCESS,
5422 struct samr_alias_info, &result);
5423 if (!NT_STATUS_IS_OK(result)) {
5426 ainfo->sid = info_sid;
5428 force_flush_samr_cache(&info_sid);
5430 return NT_STATUS_OK;
5433 /*********************************************************************
5434 _samr_QueryGroupInfo
5435 *********************************************************************/
5437 NTSTATUS _samr_QueryGroupInfo(pipes_struct *p,
5438 struct samr_QueryGroupInfo *r)
5440 struct samr_group_info *ginfo;
5443 union samr_GroupInfo *info = NULL;
5445 uint32_t attributes = SE_GROUP_MANDATORY |
5446 SE_GROUP_ENABLED_BY_DEFAULT |
5448 const char *group_name = NULL;
5449 const char *group_description = NULL;
5451 ginfo = policy_handle_find(p, r->in.group_handle,
5452 SAMR_GROUP_ACCESS_LOOKUP_INFO, NULL,
5453 struct samr_group_info, &status);
5454 if (!NT_STATUS_IS_OK(status)) {
5459 ret = get_domain_group_from_sid(ginfo->sid, &map);
5462 return NT_STATUS_INVALID_HANDLE;
5464 /* FIXME: map contains fstrings */
5465 group_name = talloc_strdup(r, map.nt_name);
5466 group_description = talloc_strdup(r, map.comment);
5468 info = TALLOC_ZERO_P(p->mem_ctx, union samr_GroupInfo);
5470 return NT_STATUS_NO_MEMORY;
5473 switch (r->in.level) {
5479 status = pdb_enum_group_members(
5480 p->mem_ctx, &ginfo->sid, &members,
5484 if (!NT_STATUS_IS_OK(status)) {
5488 info->all.name.string = group_name;
5489 info->all.attributes = attributes;
5490 info->all.num_members = num_members;
5491 info->all.description.string = group_description;
5495 info->name.string = group_name;
5498 info->attributes.attributes = attributes;
5501 info->description.string = group_description;
5511 status = pdb_enum_group_members(
5512 p->mem_ctx, &ginfo->sid, &members,
5516 if (!NT_STATUS_IS_OK(status)) {
5520 info->all2.name.string = group_name;
5521 info->all2.attributes = attributes;
5522 info->all2.num_members = 0; /* num_members - in w2k3 this is always 0 */
5523 info->all2.description.string = group_description;
5528 return NT_STATUS_INVALID_INFO_CLASS;
5531 *r->out.info = info;
5533 return NT_STATUS_OK;
5536 /*********************************************************************
5538 *********************************************************************/
5540 NTSTATUS _samr_SetGroupInfo(pipes_struct *p,
5541 struct samr_SetGroupInfo *r)
5543 struct samr_group_info *ginfo;
5547 bool can_mod_accounts;
5549 ginfo = policy_handle_find(p, r->in.group_handle,
5550 SAMR_GROUP_ACCESS_SET_INFO, NULL,
5551 struct samr_group_info, &status);
5552 if (!NT_STATUS_IS_OK(status)) {
5557 ret = get_domain_group_from_sid(ginfo->sid, &map);
5560 return NT_STATUS_NO_SUCH_GROUP;
5562 switch (r->in.level) {
5564 fstrcpy(map.comment, r->in.info->all.description.string);
5567 /* group rename is not supported yet */
5568 return NT_STATUS_NOT_SUPPORTED;
5570 fstrcpy(map.comment, r->in.info->description.string);
5573 return NT_STATUS_INVALID_INFO_CLASS;
5576 can_mod_accounts = user_has_privileges( p->server_info->ptok, &se_add_users );
5578 /******** BEGIN SeAddUsers BLOCK *********/
5580 if ( can_mod_accounts )
5583 status = pdb_update_group_mapping_entry(&map);
5585 if ( can_mod_accounts )
5588 /******** End SeAddUsers BLOCK *********/
5590 if (NT_STATUS_IS_OK(status)) {
5591 force_flush_samr_cache(&ginfo->sid);
5597 /*********************************************************************
5599 *********************************************************************/
5601 NTSTATUS _samr_SetAliasInfo(pipes_struct *p,
5602 struct samr_SetAliasInfo *r)
5604 struct samr_alias_info *ainfo;
5605 struct acct_info info;
5606 bool can_mod_accounts;
5609 ainfo = policy_handle_find(p, r->in.alias_handle,
5610 SAMR_ALIAS_ACCESS_SET_INFO, NULL,
5611 struct samr_alias_info, &status);
5612 if (!NT_STATUS_IS_OK(status)) {
5616 /* get the current group information */
5619 status = pdb_get_aliasinfo( &ainfo->sid, &info );
5622 if ( !NT_STATUS_IS_OK(status))
5625 switch (r->in.level) {
5630 /* We currently do not support renaming groups in the
5631 the BUILTIN domain. Refer to util_builtin.c to understand
5632 why. The eventually needs to be fixed to be like Windows
5633 where you can rename builtin groups, just not delete them */
5635 if ( sid_check_is_in_builtin( &ainfo->sid ) ) {
5636 return NT_STATUS_SPECIAL_ACCOUNT;
5639 /* There has to be a valid name (and it has to be different) */
5641 if ( !r->in.info->name.string )
5642 return NT_STATUS_INVALID_PARAMETER;
5644 /* If the name is the same just reply "ok". Yes this
5645 doesn't allow you to change the case of a group name. */
5647 if ( strequal( r->in.info->name.string, info.acct_name ) )
5648 return NT_STATUS_OK;
5650 fstrcpy( info.acct_name, r->in.info->name.string);
5652 /* make sure the name doesn't already exist as a user
5655 fstr_sprintf( group_name, "%s\\%s", global_myname(), info.acct_name );
5656 status = can_create( p->mem_ctx, group_name );
5657 if ( !NT_STATUS_IS_OK( status ) )
5661 case ALIASINFODESCRIPTION:
5662 if (r->in.info->description.string) {
5663 fstrcpy(info.acct_desc,
5664 r->in.info->description.string);
5666 fstrcpy( info.acct_desc, "" );
5670 return NT_STATUS_INVALID_INFO_CLASS;
5673 can_mod_accounts = user_has_privileges( p->server_info->ptok, &se_add_users );
5675 /******** BEGIN SeAddUsers BLOCK *********/
5677 if ( can_mod_accounts )
5680 status = pdb_set_aliasinfo( &ainfo->sid, &info );
5682 if ( can_mod_accounts )
5685 /******** End SeAddUsers BLOCK *********/
5687 if (NT_STATUS_IS_OK(status))
5688 force_flush_samr_cache(&ainfo->sid);
5693 /****************************************************************
5695 ****************************************************************/
5697 NTSTATUS _samr_GetDomPwInfo(pipes_struct *p,
5698 struct samr_GetDomPwInfo *r)
5700 uint32_t min_password_length = 0;
5701 uint32_t password_properties = 0;
5703 /* Perform access check. Since this rpc does not require a
5704 policy handle it will not be caught by the access checks on
5705 SAMR_CONNECT or SAMR_CONNECT_ANON. */
5707 if (!pipe_access_check(p)) {
5708 DEBUG(3, ("access denied to _samr_GetDomPwInfo\n"));
5709 return NT_STATUS_ACCESS_DENIED;
5713 pdb_get_account_policy(AP_MIN_PASSWORD_LEN,
5714 &min_password_length);
5715 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS,
5716 &password_properties);
5719 if (lp_check_password_script() && *lp_check_password_script()) {
5720 password_properties |= DOMAIN_PASSWORD_COMPLEX;
5723 r->out.info->min_password_length = min_password_length;
5724 r->out.info->password_properties = password_properties;
5726 return NT_STATUS_OK;
5729 /*********************************************************************
5731 *********************************************************************/
5733 NTSTATUS _samr_OpenGroup(pipes_struct *p,
5734 struct samr_OpenGroup *r)
5739 struct samr_domain_info *dinfo;
5740 struct samr_group_info *ginfo;
5741 SEC_DESC *psd = NULL;
5743 uint32 des_access = r->in.access_mask;
5749 dinfo = policy_handle_find(p, r->in.domain_handle,
5750 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT, NULL,
5751 struct samr_domain_info, &status);
5752 if (!NT_STATUS_IS_OK(status)) {
5756 /*check if access can be granted as requested by client. */
5757 map_max_allowed_access(p->server_info->ptok, &des_access);
5759 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
5760 se_map_generic(&des_access,&grp_generic_mapping);
5762 se_priv_copy( &se_rights, &se_add_users );
5764 status = access_check_samr_object(psd, p->server_info->ptok,
5765 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access,
5766 &acc_granted, "_samr_OpenGroup");
5768 if ( !NT_STATUS_IS_OK(status) )
5771 /* this should not be hard-coded like this */
5773 if (!sid_equal(&dinfo->sid, get_global_sam_sid()))
5774 return NT_STATUS_ACCESS_DENIED;
5776 sid_compose(&info_sid, &dinfo->sid, r->in.rid);
5778 DEBUG(10, ("_samr_OpenGroup:Opening SID: %s\n",
5779 sid_string_dbg(&info_sid)));
5781 /* check if that group really exists */
5783 ret = get_domain_group_from_sid(info_sid, &map);
5786 return NT_STATUS_NO_SUCH_GROUP;
5788 ginfo = policy_handle_create(p, r->out.group_handle,
5789 GENERIC_RIGHTS_GROUP_ALL_ACCESS,
5790 struct samr_group_info, &status);
5791 if (!NT_STATUS_IS_OK(status)) {
5794 ginfo->sid = info_sid;
5796 return NT_STATUS_OK;
5799 /*********************************************************************
5800 _samr_RemoveMemberFromForeignDomain
5801 *********************************************************************/
5803 NTSTATUS _samr_RemoveMemberFromForeignDomain(pipes_struct *p,
5804 struct samr_RemoveMemberFromForeignDomain *r)
5806 struct samr_domain_info *dinfo;
5809 DEBUG(5,("_samr_RemoveMemberFromForeignDomain: removing SID [%s]\n",
5810 sid_string_dbg(r->in.sid)));
5812 /* Find the policy handle. Open a policy on it. */
5814 dinfo = policy_handle_find(p, r->in.domain_handle,
5815 STD_RIGHT_DELETE_ACCESS, NULL,
5816 struct samr_domain_info, &result);
5817 if (!NT_STATUS_IS_OK(result)) {
5821 DEBUG(8, ("_samr_RemoveMemberFromForeignDomain: sid is %s\n",
5822 sid_string_dbg(&dinfo->sid)));
5824 /* we can only delete a user from a group since we don't have
5825 nested groups anyways. So in the latter case, just say OK */
5827 /* TODO: The above comment nowadays is bogus. Since we have nested
5828 * groups now, and aliases members are never reported out of the unix
5829 * group membership, the "just say OK" makes this call a no-op. For
5830 * us. This needs fixing however. */
5832 /* I've only ever seen this in the wild when deleting a user from
5833 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
5834 * is the user about to be deleted. I very much suspect this is the
5835 * only application of this call. To verify this, let people report
5838 if (!sid_check_is_builtin(&dinfo->sid)) {
5839 DEBUG(1,("_samr_RemoveMemberFromForeignDomain: domain_sid = %s, "
5840 "global_sam_sid() = %s\n",
5841 sid_string_dbg(&dinfo->sid),
5842 sid_string_dbg(get_global_sam_sid())));
5843 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
5844 return NT_STATUS_OK;
5847 force_flush_samr_cache(&dinfo->sid);
5849 result = NT_STATUS_OK;
5854 /*******************************************************************
5855 _samr_QueryDomainInfo2
5856 ********************************************************************/
5858 NTSTATUS _samr_QueryDomainInfo2(pipes_struct *p,
5859 struct samr_QueryDomainInfo2 *r)
5861 struct samr_QueryDomainInfo q;
5863 q.in.domain_handle = r->in.domain_handle;
5864 q.in.level = r->in.level;
5866 q.out.info = r->out.info;
5868 return _samr_QueryDomainInfo(p, &q);
5871 /*******************************************************************
5873 ********************************************************************/
5875 NTSTATUS _samr_SetDomainInfo(pipes_struct *p,
5876 struct samr_SetDomainInfo *r)
5878 struct samr_domain_info *dinfo;
5879 time_t u_expire, u_min_age;
5881 time_t u_lock_duration, u_reset_time;
5884 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5886 /* We do have different access bits for info
5887 * levels here, but we're really just looking for
5888 * GENERIC_RIGHTS_DOMAIN_WRITE access. Unfortunately
5889 * this maps to different specific bits. So
5890 * assume if we have SAMR_DOMAIN_ACCESS_SET_INFO_1
5893 dinfo = policy_handle_find(p, r->in.domain_handle,
5894 SAMR_DOMAIN_ACCESS_SET_INFO_1, NULL,
5895 struct samr_domain_info, &result);
5896 if (!NT_STATUS_IS_OK(result)) {
5900 DEBUG(5,("_samr_SetDomainInfo: level: %d\n", r->in.level));
5902 switch (r->in.level) {
5904 u_expire=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.max_password_age);
5905 u_min_age=nt_time_to_unix_abs((NTTIME *)&r->in.info->info1.min_password_age);
5906 pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)r->in.info->info1.min_password_length);
5907 pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)r->in.info->info1.password_history_length);
5908 pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)r->in.info->info1.password_properties);
5909 pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
5910 pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
5915 u_logout=nt_time_to_unix_abs((NTTIME *)&r->in.info->info3.force_logoff_time);
5916 pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
5925 u_lock_duration=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_duration);
5926 if (u_lock_duration != -1)
5927 u_lock_duration /= 60;
5929 u_reset_time=nt_time_to_unix_abs((NTTIME *)&r->in.info->info12.lockout_window)/60;
5931 pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
5932 pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
5933 pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)r->in.info->info12.lockout_threshold);
5936 return NT_STATUS_INVALID_INFO_CLASS;
5939 DEBUG(5,("_samr_SetDomainInfo: %d\n", __LINE__));
5941 return NT_STATUS_OK;
5944 /****************************************************************
5945 _samr_GetDisplayEnumerationIndex
5946 ****************************************************************/
5948 NTSTATUS _samr_GetDisplayEnumerationIndex(pipes_struct *p,
5949 struct samr_GetDisplayEnumerationIndex *r)
5951 struct samr_domain_info *dinfo;
5952 uint32_t max_entries = (uint32_t) -1;
5953 uint32_t enum_context = 0;
5955 uint32_t num_account = 0;
5956 struct samr_displayentry *entries = NULL;
5959 DEBUG(5,("_samr_GetDisplayEnumerationIndex: %d\n", __LINE__));
5961 dinfo = policy_handle_find(p, r->in.domain_handle,
5962 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS, NULL,
5963 struct samr_domain_info, &status);
5964 if (!NT_STATUS_IS_OK(status)) {
5968 if ((r->in.level < 1) || (r->in.level > 3)) {
5969 DEBUG(0,("_samr_GetDisplayEnumerationIndex: "
5970 "Unknown info level (%u)\n",
5972 return NT_STATUS_INVALID_INFO_CLASS;
5977 /* The following done as ROOT. Don't return without unbecome_root(). */
5979 switch (r->in.level) {
5981 if (dinfo->disp_info->users == NULL) {
5982 dinfo->disp_info->users = pdb_search_users(
5983 dinfo->disp_info, ACB_NORMAL);
5984 if (dinfo->disp_info->users == NULL) {
5986 return NT_STATUS_ACCESS_DENIED;
5988 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5989 "starting user enumeration at index %u\n",
5990 (unsigned int)enum_context));
5992 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
5993 "using cached user enumeration at index %u\n",
5994 (unsigned int)enum_context));
5996 num_account = pdb_search_entries(dinfo->disp_info->users,
5997 enum_context, max_entries,
6001 if (dinfo->disp_info->machines == NULL) {
6002 dinfo->disp_info->machines = pdb_search_users(
6003 dinfo->disp_info, ACB_WSTRUST|ACB_SVRTRUST);
6004 if (dinfo->disp_info->machines == NULL) {
6006 return NT_STATUS_ACCESS_DENIED;
6008 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6009 "starting machine enumeration at index %u\n",
6010 (unsigned int)enum_context));
6012 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6013 "using cached machine enumeration at index %u\n",
6014 (unsigned int)enum_context));
6016 num_account = pdb_search_entries(dinfo->disp_info->machines,
6017 enum_context, max_entries,
6021 if (dinfo->disp_info->groups == NULL) {
6022 dinfo->disp_info->groups = pdb_search_groups(
6024 if (dinfo->disp_info->groups == NULL) {
6026 return NT_STATUS_ACCESS_DENIED;
6028 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6029 "starting group enumeration at index %u\n",
6030 (unsigned int)enum_context));
6032 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6033 "using cached group enumeration at index %u\n",
6034 (unsigned int)enum_context));
6036 num_account = pdb_search_entries(dinfo->disp_info->groups,
6037 enum_context, max_entries,
6042 smb_panic("info class changed");
6048 /* Ensure we cache this enumeration. */
6049 set_disp_info_cache_timeout(dinfo->disp_info, DISP_INFO_CACHE_TIMEOUT);
6051 DEBUG(10,("_samr_GetDisplayEnumerationIndex: looking for :%s\n",
6052 r->in.name->string));
6054 for (i=0; i<num_account; i++) {
6055 if (strequal(entries[i].account_name, r->in.name->string)) {
6056 DEBUG(10,("_samr_GetDisplayEnumerationIndex: "
6057 "found %s at idx %d\n",
6058 r->in.name->string, i));
6060 return NT_STATUS_OK;
6064 /* assuming account_name lives at the very end */
6065 *r->out.idx = num_account;
6067 return NT_STATUS_NO_MORE_ENTRIES;
6070 /****************************************************************
6071 _samr_GetDisplayEnumerationIndex2
6072 ****************************************************************/
6074 NTSTATUS _samr_GetDisplayEnumerationIndex2(pipes_struct *p,
6075 struct samr_GetDisplayEnumerationIndex2 *r)
6077 struct samr_GetDisplayEnumerationIndex q;
6079 q.in.domain_handle = r->in.domain_handle;
6080 q.in.level = r->in.level;
6081 q.in.name = r->in.name;
6083 q.out.idx = r->out.idx;
6085 return _samr_GetDisplayEnumerationIndex(p, &q);
6088 /****************************************************************
6089 ****************************************************************/
6091 NTSTATUS _samr_Shutdown(pipes_struct *p,
6092 struct samr_Shutdown *r)
6094 p->rng_fault_state = true;
6095 return NT_STATUS_NOT_IMPLEMENTED;
6098 /****************************************************************
6099 ****************************************************************/
6101 NTSTATUS _samr_SetMemberAttributesOfGroup(pipes_struct *p,
6102 struct samr_SetMemberAttributesOfGroup *r)
6104 p->rng_fault_state = true;
6105 return NT_STATUS_NOT_IMPLEMENTED;
6108 /****************************************************************
6109 ****************************************************************/
6111 NTSTATUS _samr_ChangePasswordUser(pipes_struct *p,
6112 struct samr_ChangePasswordUser *r)
6114 p->rng_fault_state = true;
6115 return NT_STATUS_NOT_IMPLEMENTED;
6118 /****************************************************************
6119 ****************************************************************/
6121 NTSTATUS _samr_TestPrivateFunctionsDomain(pipes_struct *p,
6122 struct samr_TestPrivateFunctionsDomain *r)
6124 p->rng_fault_state = true;
6125 return NT_STATUS_NOT_IMPLEMENTED;
6128 /****************************************************************
6129 ****************************************************************/
6131 NTSTATUS _samr_TestPrivateFunctionsUser(pipes_struct *p,
6132 struct samr_TestPrivateFunctionsUser *r)
6134 return NT_STATUS_NOT_IMPLEMENTED;
6137 /****************************************************************
6138 ****************************************************************/
6140 NTSTATUS _samr_AddMultipleMembersToAlias(pipes_struct *p,
6141 struct samr_AddMultipleMembersToAlias *r)
6143 p->rng_fault_state = true;
6144 return NT_STATUS_NOT_IMPLEMENTED;
6147 /****************************************************************
6148 ****************************************************************/
6150 NTSTATUS _samr_RemoveMultipleMembersFromAlias(pipes_struct *p,
6151 struct samr_RemoveMultipleMembersFromAlias *r)
6153 p->rng_fault_state = true;
6154 return NT_STATUS_NOT_IMPLEMENTED;
6157 /****************************************************************
6158 ****************************************************************/
6160 NTSTATUS _samr_SetBootKeyInformation(pipes_struct *p,
6161 struct samr_SetBootKeyInformation *r)
6163 p->rng_fault_state = true;
6164 return NT_STATUS_NOT_IMPLEMENTED;
6167 /****************************************************************
6168 ****************************************************************/
6170 NTSTATUS _samr_GetBootKeyInformation(pipes_struct *p,
6171 struct samr_GetBootKeyInformation *r)
6173 p->rng_fault_state = true;
6174 return NT_STATUS_NOT_IMPLEMENTED;
6177 /****************************************************************
6178 ****************************************************************/
6180 NTSTATUS _samr_RidToSid(pipes_struct *p,
6181 struct samr_RidToSid *r)
6183 p->rng_fault_state = true;
6184 return NT_STATUS_NOT_IMPLEMENTED;
6187 /****************************************************************
6188 ****************************************************************/
6190 NTSTATUS _samr_SetDsrmPassword(pipes_struct *p,
6191 struct samr_SetDsrmPassword *r)
6193 p->rng_fault_state = true;
6194 return NT_STATUS_NOT_IMPLEMENTED;
6197 /****************************************************************
6198 ****************************************************************/
6200 NTSTATUS _samr_ValidatePassword(pipes_struct *p,
6201 struct samr_ValidatePassword *r)
6203 p->rng_fault_state = true;
6204 return NT_STATUS_NOT_IMPLEMENTED;