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-2005,
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.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 * This is the implementation of the SAMR code.
37 #define DBGC_CLASS DBGC_RPC_SRV
39 #define SAMR_USR_RIGHTS_WRITE_PW \
40 ( READ_CONTROL_ACCESS | \
41 SA_RIGHT_USER_CHANGE_PASSWORD | \
42 SA_RIGHT_USER_SET_LOC_COM )
44 #define DISP_INFO_CACHE_TIMEOUT 10
46 typedef struct disp_info {
47 struct disp_info *next, *prev;
49 DOM_SID sid; /* identify which domain this is. */
50 BOOL builtin_domain; /* Quick flag to check if this is the builtin domain. */
51 struct pdb_search *users; /* querydispinfo 1 and 4 */
52 struct pdb_search *machines; /* querydispinfo 2 */
53 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
54 struct pdb_search *aliases; /* enumaliases */
57 struct pdb_search *enum_users; /* enumusers with a mask */
60 smb_event_id_t di_cache_timeout_event; /* cache idle timeout handler. */
63 /* We keep a static list of these by SID as modern clients close down
64 all resources between each request in a complete enumeration. */
66 static DISP_INFO *disp_info_list;
69 /* for use by the \PIPE\samr policy */
71 BOOL builtin_domain; /* Quick flag to check if this is the builtin domain. */
72 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
78 static struct generic_mapping sam_generic_mapping = {
79 GENERIC_RIGHTS_SAM_READ,
80 GENERIC_RIGHTS_SAM_WRITE,
81 GENERIC_RIGHTS_SAM_EXECUTE,
82 GENERIC_RIGHTS_SAM_ALL_ACCESS};
83 static struct generic_mapping dom_generic_mapping = {
84 GENERIC_RIGHTS_DOMAIN_READ,
85 GENERIC_RIGHTS_DOMAIN_WRITE,
86 GENERIC_RIGHTS_DOMAIN_EXECUTE,
87 GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
88 static struct generic_mapping usr_generic_mapping = {
89 GENERIC_RIGHTS_USER_READ,
90 GENERIC_RIGHTS_USER_WRITE,
91 GENERIC_RIGHTS_USER_EXECUTE,
92 GENERIC_RIGHTS_USER_ALL_ACCESS};
93 static struct generic_mapping grp_generic_mapping = {
94 GENERIC_RIGHTS_GROUP_READ,
95 GENERIC_RIGHTS_GROUP_WRITE,
96 GENERIC_RIGHTS_GROUP_EXECUTE,
97 GENERIC_RIGHTS_GROUP_ALL_ACCESS};
98 static struct generic_mapping ali_generic_mapping = {
99 GENERIC_RIGHTS_ALIAS_READ,
100 GENERIC_RIGHTS_ALIAS_WRITE,
101 GENERIC_RIGHTS_ALIAS_EXECUTE,
102 GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
104 /*******************************************************************
105 *******************************************************************/
107 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
108 struct generic_mapping *map,
109 DOM_SID *sid, uint32 sid_access )
111 DOM_SID domadmin_sid;
112 SEC_ACE ace[5]; /* at most 5 entries */
118 /* basic access for Everyone */
120 init_sec_access(&mask, map->generic_execute | map->generic_read );
121 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
123 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
125 init_sec_access(&mask, map->generic_all);
127 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
128 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
130 /* Add Full Access for Domain Admins if we are a DC */
133 sid_copy( &domadmin_sid, get_global_sam_sid() );
134 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
135 init_sec_ace(&ace[i++], &domadmin_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
138 /* if we have a sid, give it some special access */
141 init_sec_access( &mask, sid_access );
142 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
145 /* create the security descriptor */
147 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
148 return NT_STATUS_NO_MEMORY;
150 if ((*psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, sd_size)) == NULL)
151 return NT_STATUS_NO_MEMORY;
156 /*******************************************************************
157 Checks if access to an object should be granted, and returns that
158 level of access for further checks.
159 ********************************************************************/
161 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
162 SE_PRIV *rights, uint32 rights_mask,
163 uint32 des_access, uint32 *acc_granted,
166 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
167 uint32 saved_mask = 0;
169 /* check privileges; certain SAM access bits should be overridden
170 by privileges (mostly having to do with creating/modifying/deleting
173 if ( rights && user_has_any_privilege( token, rights ) ) {
175 saved_mask = (des_access & rights_mask);
176 des_access &= ~saved_mask;
178 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
183 /* check the security descriptor first */
185 if ( se_access_check(psd, token, des_access, acc_granted, &status) )
188 /* give root a free pass */
190 if ( geteuid() == sec_initial_uid() ) {
192 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
193 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
195 *acc_granted = des_access;
197 status = NT_STATUS_OK;
203 /* add in any bits saved during the privilege check (only
204 matters is status is ok) */
206 *acc_granted |= rights_mask;
208 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
209 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
210 des_access, *acc_granted));
215 /*******************************************************************
216 Checks if access to a function can be granted
217 ********************************************************************/
219 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
221 DEBUG(5,("%s: access check ((granted: %#010x; required: %#010x)\n",
222 debug, acc_granted, acc_required));
224 /* check the security descriptor first */
226 if ( (acc_granted&acc_required) == acc_required )
229 /* give root a free pass */
231 if (geteuid() == sec_initial_uid()) {
233 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
234 debug, acc_granted, acc_required));
235 DEBUGADD(4,("but overwritten by euid == 0\n"));
240 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: %#010x)\n",
241 debug, acc_granted, acc_required));
243 return NT_STATUS_ACCESS_DENIED;
246 /*******************************************************************
247 Fetch or create a dispinfo struct.
248 ********************************************************************/
250 static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid, const char *sid_str)
255 /* There are two cases to consider here:
256 1) The SID is a domain SID and we look for an equality match, or
257 2) This is an account SID and so we return the DISP_INFO* for our
260 if ( psid && sid_check_is_in_our_domain( psid ) ) {
261 DEBUG(10,("get_samr_dispinfo_by_sid: Replacing %s with our domain SID\n",
263 psid = get_global_sam_sid();
266 for (dpi = disp_info_list; dpi; dpi = dpi->next) {
267 if (sid_equal(psid, &dpi->sid)) {
272 /* This struct is never free'd - I'm using talloc so we
273 can get a list out of smbd using smbcontrol. There will
274 be one of these per SID we're authorative for. JRA. */
276 mem_ctx = talloc_init("DISP_INFO for domain sid %s", sid_str);
278 if ((dpi = TALLOC_ZERO_P(mem_ctx, DISP_INFO)) == NULL)
281 dpi->mem_ctx = mem_ctx;
284 sid_copy( &dpi->sid, psid);
285 dpi->builtin_domain = sid_check_is_builtin(psid);
287 dpi->builtin_domain = False;
290 DLIST_ADD(disp_info_list, dpi);
295 /*******************************************************************
296 Create a samr_info struct.
297 ********************************************************************/
299 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
301 struct samr_info *info;
306 sid_to_string(sid_str, psid);
308 fstrcpy(sid_str,"(NULL)");
311 mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
313 if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
316 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
318 sid_copy( &info->sid, psid);
319 info->builtin_domain = sid_check_is_builtin(psid);
321 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
322 info->builtin_domain = False;
324 info->mem_ctx = mem_ctx;
326 info->disp_info = get_samr_dispinfo_by_sid(psid, sid_str);
328 if (!info->disp_info) {
329 talloc_destroy(mem_ctx);
336 /*******************************************************************
337 Function to free the per SID data.
338 ********************************************************************/
340 static void free_samr_cache(DISP_INFO *disp_info, const char *sid_str)
342 DEBUG(10,("free_samr_cache: deleting cache for SID %s\n", sid_str));
344 /* We need to become root here because the paged search might have to
345 * tell the LDAP server we're not interested in the rest anymore. */
349 if (disp_info->users) {
350 DEBUG(10,("free_samr_cache: deleting users cache\n"));
351 pdb_search_destroy(disp_info->users);
352 disp_info->users = NULL;
354 if (disp_info->machines) {
355 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
356 pdb_search_destroy(disp_info->machines);
357 disp_info->machines = NULL;
359 if (disp_info->groups) {
360 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
361 pdb_search_destroy(disp_info->groups);
362 disp_info->groups = NULL;
364 if (disp_info->aliases) {
365 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
366 pdb_search_destroy(disp_info->aliases);
367 disp_info->aliases = NULL;
369 if (disp_info->enum_users) {
370 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
371 pdb_search_destroy(disp_info->enum_users);
372 disp_info->enum_users = NULL;
374 disp_info->enum_acb_mask = 0;
379 /*******************************************************************
380 Function to free the per handle data.
381 ********************************************************************/
383 static void free_samr_info(void *ptr)
385 struct samr_info *info=(struct samr_info *) ptr;
387 /* Only free the dispinfo cache if no one bothered to set up
390 if (info->disp_info && info->disp_info->di_cache_timeout_event == (smb_event_id_t)0) {
392 sid_to_string(sid_str, &info->disp_info->sid);
393 free_samr_cache(info->disp_info, sid_str);
396 talloc_destroy(info->mem_ctx);
399 /*******************************************************************
400 Idle event handler. Throw away the disp info cache.
401 ********************************************************************/
403 static void disp_info_cache_idle_timeout_handler(void **private_data,
408 DISP_INFO *disp_info = (DISP_INFO *)(*private_data);
410 sid_to_string(sid_str, &disp_info->sid);
412 free_samr_cache(disp_info, sid_str);
414 /* Remove the event. */
415 smb_unregister_idle_event(disp_info->di_cache_timeout_event);
416 disp_info->di_cache_timeout_event = (smb_event_id_t)0;
418 DEBUG(10,("disp_info_cache_idle_timeout_handler: caching timed out for SID %s at %u\n",
419 sid_str, (unsigned int)ev_now));
422 /*******************************************************************
423 Setup cache removal idle event handler.
424 ********************************************************************/
426 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
430 sid_to_string(sid_str, &disp_info->sid);
432 /* Remove any pending timeout and update. */
434 if (disp_info->di_cache_timeout_event) {
435 smb_unregister_idle_event(disp_info->di_cache_timeout_event);
436 disp_info->di_cache_timeout_event = (smb_event_id_t)0;
439 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for SID %s for %u seconds\n",
440 sid_str, (unsigned int)secs_fromnow ));
442 disp_info->di_cache_timeout_event =
443 smb_register_idle_event(disp_info_cache_idle_timeout_handler,
448 /*******************************************************************
449 Force flush any cache. We do this on any samr_set_xxx call.
450 We must also remove the timeout handler.
451 ********************************************************************/
453 static void force_flush_samr_cache(DISP_INFO *disp_info)
458 sid_to_string(sid_str, &disp_info->sid);
459 if (disp_info->di_cache_timeout_event) {
460 smb_unregister_idle_event(disp_info->di_cache_timeout_event);
461 disp_info->di_cache_timeout_event = (smb_event_id_t)0;
462 DEBUG(10,("force_flush_samr_cache: clearing idle event for SID %s\n",
465 free_samr_cache(disp_info, sid_str);
469 /*******************************************************************
470 Ensure password info is never given out. Paranioa... JRA.
471 ********************************************************************/
473 static void samr_clear_sam_passwd(struct samu *sam_pass)
479 /* These now zero out the old password */
481 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
482 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
485 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
487 struct samr_displayentry *entry;
489 if (info->builtin_domain) {
490 /* No users in builtin. */
494 if (info->users == NULL) {
495 info->users = pdb_search_users(acct_flags);
496 if (info->users == NULL) {
500 /* Fetch the last possible entry, thus trigger an enumeration */
501 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
503 /* Ensure we cache this enumeration. */
504 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
506 return info->users->num_entries;
509 static uint32 count_sam_groups(struct disp_info *info)
511 struct samr_displayentry *entry;
513 if (info->builtin_domain) {
514 /* No groups in builtin. */
518 if (info->groups == NULL) {
519 info->groups = pdb_search_groups();
520 if (info->groups == NULL) {
524 /* Fetch the last possible entry, thus trigger an enumeration */
525 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
527 /* Ensure we cache this enumeration. */
528 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
530 return info->groups->num_entries;
533 static uint32 count_sam_aliases(struct disp_info *info)
535 struct samr_displayentry *entry;
537 if (info->aliases == NULL) {
538 info->aliases = pdb_search_aliases(&info->sid);
539 if (info->aliases == NULL) {
543 /* Fetch the last possible entry, thus trigger an enumeration */
544 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
546 /* Ensure we cache this enumeration. */
547 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
549 return info->aliases->num_entries;
552 /*******************************************************************
554 ********************************************************************/
556 NTSTATUS _samr_close_hnd(pipes_struct *p, SAMR_Q_CLOSE_HND *q_u, SAMR_R_CLOSE_HND *r_u)
558 r_u->status = NT_STATUS_OK;
560 /* close the policy handle */
561 if (!close_policy_hnd(p, &q_u->pol))
562 return NT_STATUS_OBJECT_NAME_INVALID;
564 DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
569 /*******************************************************************
570 samr_reply_open_domain
571 ********************************************************************/
573 NTSTATUS _samr_open_domain(pipes_struct *p, SAMR_Q_OPEN_DOMAIN *q_u, SAMR_R_OPEN_DOMAIN *r_u)
575 struct samr_info *info;
576 SEC_DESC *psd = NULL;
578 uint32 des_access = q_u->flags;
583 r_u->status = NT_STATUS_OK;
585 /* find the connection policy handle. */
587 if ( !find_policy_by_hnd(p, &q_u->pol, (void**)(void *)&info) )
588 return NT_STATUS_INVALID_HANDLE;
590 status = access_check_samr_function( info->acc_granted,
591 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_open_domain" );
593 if ( !NT_STATUS_IS_OK(status) )
596 /*check if access can be granted as requested by client. */
598 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
599 se_map_generic( &des_access, &dom_generic_mapping );
601 se_priv_copy( &se_rights, &se_machine_account );
602 se_priv_add( &se_rights, &se_add_users );
604 status = access_check_samr_object( psd, p->pipe_user.nt_user_token,
605 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
606 &acc_granted, "_samr_open_domain" );
608 if ( !NT_STATUS_IS_OK(status) )
611 if (!sid_check_is_domain(&q_u->dom_sid.sid) &&
612 !sid_check_is_builtin(&q_u->dom_sid.sid)) {
613 return NT_STATUS_NO_SUCH_DOMAIN;
616 /* associate the domain SID with the (unique) handle. */
617 if ((info = get_samr_info_by_sid(&q_u->dom_sid.sid))==NULL)
618 return NT_STATUS_NO_MEMORY;
619 info->acc_granted = acc_granted;
621 /* get a (unique) handle. open a policy on it. */
622 if (!create_policy_hnd(p, &r_u->domain_pol, free_samr_info, (void *)info))
623 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
625 DEBUG(5,("samr_open_domain: %d\n", __LINE__));
630 /*******************************************************************
631 _samr_get_usrdom_pwinfo
632 ********************************************************************/
634 NTSTATUS _samr_get_usrdom_pwinfo(pipes_struct *p, SAMR_Q_GET_USRDOM_PWINFO *q_u, SAMR_R_GET_USRDOM_PWINFO *r_u)
636 struct samr_info *info = NULL;
638 r_u->status = NT_STATUS_OK;
640 /* find the policy handle. open a policy on it. */
641 if (!find_policy_by_hnd(p, &q_u->user_pol, (void **)(void *)&info))
642 return NT_STATUS_INVALID_HANDLE;
644 if (!sid_check_is_in_our_domain(&info->sid))
645 return NT_STATUS_OBJECT_TYPE_MISMATCH;
647 init_samr_r_get_usrdom_pwinfo(r_u, NT_STATUS_OK);
649 DEBUG(5,("_samr_get_usrdom_pwinfo: %d\n", __LINE__));
652 * NT sometimes return NT_STATUS_ACCESS_DENIED
653 * I don't know yet why.
659 /*******************************************************************
661 ********************************************************************/
663 NTSTATUS _samr_set_sec_obj(pipes_struct *p, SAMR_Q_SET_SEC_OBJ *q_u, SAMR_R_SET_SEC_OBJ *r_u)
665 DEBUG(0,("_samr_set_sec_obj: Not yet implemented!\n"));
666 return NT_STATUS_NOT_IMPLEMENTED;
669 /*******************************************************************
670 ********************************************************************/
672 static BOOL get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol,
673 DOM_SID *sid, uint32 *acc_granted,
674 DISP_INFO **ppdisp_info)
676 struct samr_info *info = NULL;
678 /* find the policy handle. open a policy on it. */
679 if (!find_policy_by_hnd(p, pol, (void **)(void *)&info))
686 *acc_granted = info->acc_granted;
688 *ppdisp_info = info->disp_info;
694 /*******************************************************************
696 ********************************************************************/
698 NTSTATUS _samr_query_sec_obj(pipes_struct *p, SAMR_Q_QUERY_SEC_OBJ *q_u, SAMR_R_QUERY_SEC_OBJ *r_u)
702 SEC_DESC * psd = NULL;
706 r_u->status = NT_STATUS_OK;
709 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &pol_sid, &acc_granted, NULL))
710 return NT_STATUS_INVALID_HANDLE;
712 DEBUG(10,("_samr_query_sec_obj: querying security on SID: %s\n", sid_to_string(str_sid, &pol_sid)));
714 /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
716 /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
717 if (pol_sid.sid_rev_num == 0) {
718 DEBUG(5,("_samr_query_sec_obj: querying security on SAM\n"));
719 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
720 } else if (sid_equal(&pol_sid,get_global_sam_sid())) {
721 /* check if it is our domain SID */
722 DEBUG(5,("_samr_query_sec_obj: querying security on Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
723 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
724 } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
725 /* check if it is the Builtin Domain */
726 /* TODO: Builtin probably needs a different SD with restricted write access*/
727 DEBUG(5,("_samr_query_sec_obj: querying security on Builtin Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
728 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
729 } else if (sid_check_is_in_our_domain(&pol_sid) ||
730 sid_check_is_in_builtin(&pol_sid)) {
731 /* TODO: different SDs have to be generated for aliases groups and users.
732 Currently all three get a default user SD */
733 DEBUG(10,("_samr_query_sec_obj: querying security on Object with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
734 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
736 return NT_STATUS_OBJECT_TYPE_MISMATCH;
739 if ((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
740 return NT_STATUS_NO_MEMORY;
742 if (NT_STATUS_IS_OK(r_u->status))
748 /*******************************************************************
749 makes a SAM_ENTRY / UNISTR2* structure from a user list.
750 ********************************************************************/
752 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
753 UNISTR2 **uni_name_pp,
754 uint32 num_entries, uint32 start_idx,
755 struct samr_displayentry *entries)
764 if (num_entries == 0)
767 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_entries);
769 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_entries);
771 if (sam == NULL || uni_name == NULL) {
772 DEBUG(0, ("make_user_sam_entry_list: talloc_zero failed!\n"));
773 return NT_STATUS_NO_MEMORY;
776 for (i = 0; i < num_entries; i++) {
777 UNISTR2 uni_temp_name;
779 * usrmgr expects a non-NULL terminated string with
780 * trust relationships
782 if (entries[i].acct_flags & ACB_DOMTRUST) {
783 init_unistr2(&uni_temp_name, entries[i].account_name,
786 init_unistr2(&uni_temp_name, entries[i].account_name,
790 init_sam_entry(&sam[i], &uni_temp_name, entries[i].rid);
791 copy_unistr2(&uni_name[i], &uni_temp_name);
795 *uni_name_pp = uni_name;
799 /*******************************************************************
800 samr_reply_enum_dom_users
801 ********************************************************************/
803 NTSTATUS _samr_enum_dom_users(pipes_struct *p, SAMR_Q_ENUM_DOM_USERS *q_u,
804 SAMR_R_ENUM_DOM_USERS *r_u)
806 struct samr_info *info = NULL;
808 uint32 enum_context=q_u->start_idx;
809 enum remote_arch_types ra_type = get_remote_arch();
810 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
811 uint32 max_entries = max_sam_entries;
812 struct samr_displayentry *entries = NULL;
814 r_u->status = NT_STATUS_OK;
816 /* find the policy handle. open a policy on it. */
817 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
818 return NT_STATUS_INVALID_HANDLE;
820 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted,
821 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
822 "_samr_enum_dom_users"))) {
826 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
828 if (info->builtin_domain) {
829 /* No users in builtin. */
830 init_samr_r_enum_dom_users(r_u, q_u->start_idx, 0);
831 DEBUG(5,("_samr_enum_dom_users: No users in BUILTIN\n"));
839 if ((info->disp_info->enum_users != NULL) &&
840 (info->disp_info->enum_acb_mask != q_u->acb_mask)) {
841 pdb_search_destroy(info->disp_info->enum_users);
842 info->disp_info->enum_users = NULL;
845 if (info->disp_info->enum_users == NULL) {
846 info->disp_info->enum_users = pdb_search_users(q_u->acb_mask);
847 info->disp_info->enum_acb_mask = q_u->acb_mask;
850 if (info->disp_info->enum_users == NULL) {
851 /* END AS ROOT !!!! */
853 return NT_STATUS_ACCESS_DENIED;
856 num_account = pdb_search_entries(info->disp_info->enum_users,
857 enum_context, max_entries,
860 /* END AS ROOT !!!! */
864 if (num_account == 0) {
865 DEBUG(5, ("_samr_enum_dom_users: enumeration handle over "
870 r_u->status = make_user_sam_entry_list(p->mem_ctx, &r_u->sam,
872 num_account, enum_context,
875 if (!NT_STATUS_IS_OK(r_u->status))
878 if (max_entries <= num_account) {
879 r_u->status = STATUS_MORE_ENTRIES;
881 r_u->status = NT_STATUS_OK;
884 /* Ensure we cache this enumeration. */
885 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
887 DEBUG(5, ("_samr_enum_dom_users: %d\n", __LINE__));
889 init_samr_r_enum_dom_users(r_u, q_u->start_idx + num_account,
892 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
897 /*******************************************************************
898 makes a SAM_ENTRY / UNISTR2* structure from a group list.
899 ********************************************************************/
901 static void make_group_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
902 UNISTR2 **uni_name_pp,
903 uint32 num_sam_entries,
904 struct samr_displayentry *entries)
913 if (num_sam_entries == 0)
916 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
917 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
919 if (sam == NULL || uni_name == NULL) {
920 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
924 for (i = 0; i < num_sam_entries; i++) {
926 * JRA. I think this should include the null. TNG does not.
928 init_unistr2(&uni_name[i], entries[i].account_name,
930 init_sam_entry(&sam[i], &uni_name[i], entries[i].rid);
934 *uni_name_pp = uni_name;
937 /*******************************************************************
938 samr_reply_enum_dom_groups
939 ********************************************************************/
941 NTSTATUS _samr_enum_dom_groups(pipes_struct *p, SAMR_Q_ENUM_DOM_GROUPS *q_u, SAMR_R_ENUM_DOM_GROUPS *r_u)
943 struct samr_info *info = NULL;
944 struct samr_displayentry *groups;
947 r_u->status = NT_STATUS_OK;
949 /* find the policy handle. open a policy on it. */
950 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
951 return NT_STATUS_INVALID_HANDLE;
953 r_u->status = access_check_samr_function(info->acc_granted,
954 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
955 "_samr_enum_dom_groups");
956 if (!NT_STATUS_IS_OK(r_u->status))
959 DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
961 if (info->builtin_domain) {
962 /* No groups in builtin. */
963 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, 0);
964 DEBUG(5,("_samr_enum_dom_users: No groups in BUILTIN\n"));
968 /* the domain group array is being allocated in the function below */
972 if (info->disp_info->groups == NULL) {
973 info->disp_info->groups = pdb_search_groups();
975 if (info->disp_info->groups == NULL) {
977 return NT_STATUS_ACCESS_DENIED;
981 num_groups = pdb_search_entries(info->disp_info->groups, q_u->start_idx,
982 MAX_SAM_ENTRIES, &groups);
985 /* Ensure we cache this enumeration. */
986 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
988 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
991 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, num_groups);
993 DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
998 /*******************************************************************
999 samr_reply_enum_dom_aliases
1000 ********************************************************************/
1002 NTSTATUS _samr_enum_dom_aliases(pipes_struct *p, SAMR_Q_ENUM_DOM_ALIASES *q_u, SAMR_R_ENUM_DOM_ALIASES *r_u)
1004 struct samr_info *info;
1005 struct samr_displayentry *aliases;
1006 uint32 num_aliases = 0;
1008 /* find the policy handle. open a policy on it. */
1009 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1010 return NT_STATUS_INVALID_HANDLE;
1012 r_u->status = access_check_samr_function(info->acc_granted,
1013 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1014 "_samr_enum_dom_aliases");
1015 if (!NT_STATUS_IS_OK(r_u->status))
1018 DEBUG(5,("samr_reply_enum_dom_aliases: sid %s\n",
1019 sid_string_static(&info->sid)));
1023 if (info->disp_info->aliases == NULL) {
1024 info->disp_info->aliases = pdb_search_aliases(&info->sid);
1025 if (info->disp_info->aliases == NULL) {
1027 return NT_STATUS_ACCESS_DENIED;
1031 num_aliases = pdb_search_entries(info->disp_info->aliases, q_u->start_idx,
1032 MAX_SAM_ENTRIES, &aliases);
1035 /* Ensure we cache this enumeration. */
1036 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1038 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
1039 num_aliases, aliases);
1041 init_samr_r_enum_dom_aliases(r_u, q_u->start_idx + num_aliases,
1044 DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
1049 /*******************************************************************
1050 samr_reply_query_dispinfo
1051 ********************************************************************/
1053 NTSTATUS _samr_query_dispinfo(pipes_struct *p, SAMR_Q_QUERY_DISPINFO *q_u,
1054 SAMR_R_QUERY_DISPINFO *r_u)
1056 struct samr_info *info = NULL;
1057 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1059 uint32 max_entries=q_u->max_entries;
1060 uint32 enum_context=q_u->start_idx;
1061 uint32 max_size=q_u->max_size;
1063 SAM_DISPINFO_CTR *ctr;
1064 uint32 temp_size=0, total_data_size=0;
1065 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1066 uint32 num_account = 0;
1067 enum remote_arch_types ra_type = get_remote_arch();
1068 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1069 struct samr_displayentry *entries = NULL;
1071 DEBUG(5, ("samr_reply_query_dispinfo: %d\n", __LINE__));
1072 r_u->status = NT_STATUS_UNSUCCESSFUL;
1074 /* find the policy handle. open a policy on it. */
1075 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info))
1076 return NT_STATUS_INVALID_HANDLE;
1079 * calculate how many entries we will return.
1081 * - the number of entries the client asked
1082 * - our limit on that
1083 * - the starting point (enumeration context)
1084 * - the buffer size the client will accept
1088 * We are a lot more like W2K. Instead of reading the SAM
1089 * each time to find the records we need to send back,
1090 * we read it once and link that copy to the sam handle.
1091 * For large user list (over the MAX_SAM_ENTRIES)
1092 * it's a definitive win.
1093 * second point to notice: between enumerations
1094 * our sam is now the same as it's a snapshoot.
1095 * third point: got rid of the static SAM_USER_21 struct
1096 * no more intermediate.
1097 * con: it uses much more memory, as a full copy is stored
1100 * If you want to change it, think twice and think
1101 * of the second point , that's really important.
1106 if ((q_u->switch_level < 1) || (q_u->switch_level > 5)) {
1107 DEBUG(0,("_samr_query_dispinfo: Unknown info level (%u)\n",
1108 (unsigned int)q_u->switch_level ));
1109 return NT_STATUS_INVALID_INFO_CLASS;
1112 /* first limit the number of entries we will return */
1113 if(max_entries > max_sam_entries) {
1114 DEBUG(5, ("samr_reply_query_dispinfo: client requested %d "
1115 "entries, limiting to %d\n", max_entries,
1117 max_entries = max_sam_entries;
1120 /* calculate the size and limit on the number of entries we will
1123 temp_size=max_entries*struct_size;
1125 if (temp_size>max_size) {
1126 max_entries=MIN((max_size/struct_size),max_entries);;
1127 DEBUG(5, ("samr_reply_query_dispinfo: buffer size limits to "
1128 "only %d entries\n", max_entries));
1131 if (!(ctr = TALLOC_ZERO_P(p->mem_ctx,SAM_DISPINFO_CTR)))
1132 return NT_STATUS_NO_MEMORY;
1138 /* THe following done as ROOT. Don't return without unbecome_root(). */
1140 switch (q_u->switch_level) {
1143 if (info->disp_info->users == NULL) {
1144 info->disp_info->users = pdb_search_users(ACB_NORMAL);
1145 if (info->disp_info->users == NULL) {
1147 return NT_STATUS_ACCESS_DENIED;
1149 DEBUG(10,("samr_reply_query_dispinfo: starting user enumeration at index %u\n",
1150 (unsigned int)enum_context ));
1152 DEBUG(10,("samr_reply_query_dispinfo: using cached user enumeration at index %u\n",
1153 (unsigned int)enum_context ));
1156 num_account = pdb_search_entries(info->disp_info->users,
1157 enum_context, max_entries,
1161 if (info->disp_info->machines == NULL) {
1162 info->disp_info->machines =
1163 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
1164 if (info->disp_info->machines == NULL) {
1166 return NT_STATUS_ACCESS_DENIED;
1168 DEBUG(10,("samr_reply_query_dispinfo: starting machine enumeration at index %u\n",
1169 (unsigned int)enum_context ));
1171 DEBUG(10,("samr_reply_query_dispinfo: using cached machine enumeration at index %u\n",
1172 (unsigned int)enum_context ));
1175 num_account = pdb_search_entries(info->disp_info->machines,
1176 enum_context, max_entries,
1181 if (info->disp_info->groups == NULL) {
1182 info->disp_info->groups = pdb_search_groups();
1183 if (info->disp_info->groups == NULL) {
1185 return NT_STATUS_ACCESS_DENIED;
1187 DEBUG(10,("samr_reply_query_dispinfo: starting group enumeration at index %u\n",
1188 (unsigned int)enum_context ));
1190 DEBUG(10,("samr_reply_query_dispinfo: using cached group enumeration at index %u\n",
1191 (unsigned int)enum_context ));
1194 num_account = pdb_search_entries(info->disp_info->groups,
1195 enum_context, max_entries,
1200 smb_panic("info class changed");
1205 /* Now create reply structure */
1206 switch (q_u->switch_level) {
1208 disp_ret = init_sam_dispinfo_1(p->mem_ctx, &ctr->sam.info1,
1209 num_account, enum_context,
1213 disp_ret = init_sam_dispinfo_2(p->mem_ctx, &ctr->sam.info2,
1214 num_account, enum_context,
1218 disp_ret = init_sam_dispinfo_3(p->mem_ctx, &ctr->sam.info3,
1219 num_account, enum_context,
1223 disp_ret = init_sam_dispinfo_4(p->mem_ctx, &ctr->sam.info4,
1224 num_account, enum_context,
1228 disp_ret = init_sam_dispinfo_5(p->mem_ctx, &ctr->sam.info5,
1229 num_account, enum_context,
1233 smb_panic("info class changed");
1237 if (!NT_STATUS_IS_OK(disp_ret))
1240 /* calculate the total size */
1241 total_data_size=num_account*struct_size;
1244 r_u->status = STATUS_MORE_ENTRIES;
1246 r_u->status = NT_STATUS_OK;
1249 /* Ensure we cache this enumeration. */
1250 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1252 DEBUG(5, ("_samr_query_dispinfo: %d\n", __LINE__));
1254 init_samr_r_query_dispinfo(r_u, num_account, total_data_size,
1255 temp_size, q_u->switch_level, ctr,
1262 /*******************************************************************
1263 samr_reply_query_aliasinfo
1264 ********************************************************************/
1266 NTSTATUS _samr_query_aliasinfo(pipes_struct *p, SAMR_Q_QUERY_ALIASINFO *q_u, SAMR_R_QUERY_ALIASINFO *r_u)
1269 struct acct_info info;
1273 r_u->status = NT_STATUS_OK;
1275 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1277 /* find the policy handle. open a policy on it. */
1278 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
1279 return NT_STATUS_INVALID_HANDLE;
1280 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_LOOKUP_INFO, "_samr_query_aliasinfo"))) {
1285 ret = pdb_get_aliasinfo(&sid, &info);
1289 return NT_STATUS_NO_SUCH_ALIAS;
1291 if ( !(r_u->ctr = TALLOC_ZERO_P( p->mem_ctx, ALIAS_INFO_CTR )) )
1292 return NT_STATUS_NO_MEMORY;
1295 switch (q_u->level ) {
1297 r_u->ctr->level = 1;
1298 init_samr_alias_info1(&r_u->ctr->alias.info1, info.acct_name, 1, info.acct_desc);
1301 r_u->ctr->level = 3;
1302 init_samr_alias_info3(&r_u->ctr->alias.info3, info.acct_desc);
1305 return NT_STATUS_INVALID_INFO_CLASS;
1308 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1314 /*******************************************************************
1315 samr_reply_lookup_ids
1316 ********************************************************************/
1318 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1320 uint32 rid[MAX_SAM_ENTRIES];
1321 int num_rids = q_u->num_sids1;
1323 r_u->status = NT_STATUS_OK;
1325 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1327 if (num_rids > MAX_SAM_ENTRIES) {
1328 num_rids = MAX_SAM_ENTRIES;
1329 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1334 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1336 for (i = 0; i < num_rids && status == 0; i++)
1338 struct sam_passwd *sam_pass;
1342 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1343 q_u->uni_user_name[i].uni_str_len));
1345 /* find the user account */
1347 sam_pass = get_smb21pwd_entry(user_name, 0);
1350 if (sam_pass == NULL)
1352 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1357 rid[i] = sam_pass->user_rid;
1363 rid[0] = BUILTIN_ALIAS_RID_USERS;
1365 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1367 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1373 /*******************************************************************
1375 ********************************************************************/
1377 NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LOOKUP_NAMES *r_u)
1379 uint32 rid[MAX_SAM_ENTRIES];
1380 enum SID_NAME_USE type[MAX_SAM_ENTRIES];
1382 int num_rids = q_u->num_names2;
1387 r_u->status = NT_STATUS_OK;
1389 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1394 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL)) {
1395 init_samr_r_lookup_names(p->mem_ctx, r_u, 0, NULL, NULL, NT_STATUS_OBJECT_TYPE_MISMATCH);
1399 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, 0, "_samr_lookup_names"))) { /* Don't know the acc_bits yet */
1403 if (num_rids > MAX_SAM_ENTRIES) {
1404 num_rids = MAX_SAM_ENTRIES;
1405 DEBUG(5,("_samr_lookup_names: truncating entries to %d\n", num_rids));
1408 DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1410 for (i = 0; i < num_rids; i++) {
1414 r_u->status = NT_STATUS_NONE_MAPPED;
1415 type[i] = SID_NAME_UNKNOWN;
1417 rid [i] = 0xffffffff;
1419 ret = rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1425 if (sid_check_is_builtin(&pol_sid)) {
1426 if (lookup_builtin_name(name, &rid[i])) {
1427 type[i] = SID_NAME_ALIAS;
1430 lookup_global_sam_name(name, 0, &rid[i], &type[i]);
1433 if (type[i] != SID_NAME_UNKNOWN) {
1434 r_u->status = NT_STATUS_OK;
1438 init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, type, r_u->status);
1440 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1445 /*******************************************************************
1446 _samr_chgpasswd_user
1447 ********************************************************************/
1449 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1454 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1456 r_u->status = NT_STATUS_OK;
1458 rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1459 rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1461 DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1464 * Pass the user through the NT -> unix user mapping
1468 (void)map_username(user_name);
1471 * UNIX username case mangling not required, pass_oem_change
1472 * is case insensitive.
1475 r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1476 q_u->nt_newpass.pass, q_u->nt_oldhash.hash, NULL);
1478 init_samr_r_chgpasswd_user(r_u, r_u->status);
1480 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1485 /*******************************************************************
1486 _samr_chgpasswd_user3
1487 ********************************************************************/
1489 NTSTATUS _samr_chgpasswd_user3(pipes_struct *p, SAMR_Q_CHGPASSWD_USER3 *q_u, SAMR_R_CHGPASSWD_USER3 *r_u)
1493 uint32 reject_reason;
1494 SAM_UNK_INFO_1 *info = NULL;
1495 SAMR_CHANGE_REJECT *reject = NULL;
1497 DEBUG(5,("_samr_chgpasswd_user3: %d\n", __LINE__));
1499 rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1500 rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1502 DEBUG(5,("_samr_chgpasswd_user3: user: %s wks: %s\n", user_name, wks));
1505 * Pass the user through the NT -> unix user mapping
1509 (void)map_username(user_name);
1512 * UNIX username case mangling not required, pass_oem_change
1513 * is case insensitive.
1516 r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1517 q_u->nt_newpass.pass, q_u->nt_oldhash.hash, &reject_reason);
1519 if (NT_STATUS_EQUAL(r_u->status, NT_STATUS_PASSWORD_RESTRICTION) ||
1520 NT_STATUS_EQUAL(r_u->status, NT_STATUS_ACCOUNT_RESTRICTION)) {
1522 uint32 min_pass_len,pass_hist,password_properties;
1523 time_t u_expire, u_min_age;
1524 NTTIME nt_expire, nt_min_age;
1525 uint32 account_policy_temp;
1527 if ((info = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_INFO_1)) == NULL) {
1528 return NT_STATUS_NO_MEMORY;
1531 if ((reject = TALLOC_ZERO_P(p->mem_ctx, SAMR_CHANGE_REJECT)) == NULL) {
1532 return NT_STATUS_NO_MEMORY;
1536 ZERO_STRUCTP(reject);
1542 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
1543 min_pass_len = account_policy_temp;
1545 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
1546 pass_hist = account_policy_temp;
1548 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
1549 password_properties = account_policy_temp;
1551 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
1552 u_expire = account_policy_temp;
1554 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
1555 u_min_age = account_policy_temp;
1561 unix_to_nt_time_abs(&nt_expire, u_expire);
1562 unix_to_nt_time_abs(&nt_min_age, u_min_age);
1564 init_unk_info1(info, (uint16)min_pass_len, (uint16)pass_hist,
1565 password_properties, nt_expire, nt_min_age);
1567 reject->reject_reason = reject_reason;
1570 init_samr_r_chgpasswd_user3(r_u, r_u->status, reject, info);
1572 DEBUG(5,("_samr_chgpasswd_user3: %d\n", __LINE__));
1577 /*******************************************************************
1578 makes a SAMR_R_LOOKUP_RIDS structure.
1579 ********************************************************************/
1581 static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1582 const char **names, UNIHDR **pp_hdr_name,
1583 UNISTR2 **pp_uni_name)
1586 UNIHDR *hdr_name=NULL;
1587 UNISTR2 *uni_name=NULL;
1589 *pp_uni_name = NULL;
1590 *pp_hdr_name = NULL;
1592 if (num_names != 0) {
1593 hdr_name = TALLOC_ZERO_ARRAY(ctx, UNIHDR, num_names);
1594 if (hdr_name == NULL)
1597 uni_name = TALLOC_ZERO_ARRAY(ctx,UNISTR2, num_names);
1598 if (uni_name == NULL)
1602 for (i = 0; i < num_names; i++) {
1603 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1604 init_unistr2(&uni_name[i], names[i], UNI_FLAGS_NONE);
1605 init_uni_hdr(&hdr_name[i], &uni_name[i]);
1608 *pp_uni_name = uni_name;
1609 *pp_hdr_name = hdr_name;
1614 /*******************************************************************
1616 ********************************************************************/
1618 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1621 enum SID_NAME_USE *attrs = NULL;
1622 uint32 *wire_attrs = NULL;
1623 UNIHDR *hdr_name = NULL;
1624 UNISTR2 *uni_name = NULL;
1626 int num_rids = q_u->num_rids1;
1630 r_u->status = NT_STATUS_OK;
1632 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1634 /* find the policy handle. open a policy on it. */
1635 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL))
1636 return NT_STATUS_INVALID_HANDLE;
1638 if (num_rids > 1000) {
1639 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
1640 "to samba4 idl this is not possible\n", num_rids));
1641 return NT_STATUS_UNSUCCESSFUL;
1644 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
1645 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, enum SID_NAME_USE, num_rids);
1646 wire_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
1648 if ((num_rids != 0) && ((names == NULL) || (attrs == NULL) || wire_attrs))
1649 return NT_STATUS_NO_MEMORY;
1651 become_root(); /* lookup_sid can require root privs */
1652 r_u->status = pdb_lookup_rids(&pol_sid, num_rids, q_u->rid,
1656 if ( NT_STATUS_EQUAL(r_u->status, NT_STATUS_NONE_MAPPED) && (num_rids == 0) ) {
1657 r_u->status = NT_STATUS_OK;
1660 if(!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
1661 &hdr_name, &uni_name))
1662 return NT_STATUS_NO_MEMORY;
1664 /* Convert from enum SID_NAME_USE to uint32 for wire format. */
1665 for (i = 0; i < num_rids; i++) {
1666 wire_attrs[i] = (uint32)attrs[i];
1669 init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, wire_attrs);
1671 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1676 /*******************************************************************
1677 _samr_open_user. Safe - gives out no passwd info.
1678 ********************************************************************/
1680 NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1682 struct samu *sampass=NULL;
1684 POLICY_HND domain_pol = q_u->domain_pol;
1685 POLICY_HND *user_pol = &r_u->user_pol;
1686 struct samr_info *info = NULL;
1687 SEC_DESC *psd = NULL;
1689 uint32 des_access = q_u->access_mask;
1695 r_u->status = NT_STATUS_OK;
1697 /* find the domain policy handle and get domain SID / access bits in the domain policy. */
1699 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
1700 return NT_STATUS_INVALID_HANDLE;
1702 nt_status = access_check_samr_function( acc_granted,
1703 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_user" );
1705 if ( !NT_STATUS_IS_OK(nt_status) )
1708 if ( !(sampass = samu_new( p->mem_ctx )) ) {
1709 return NT_STATUS_NO_MEMORY;
1712 /* append the user's RID to it */
1714 if (!sid_append_rid(&sid, q_u->user_rid))
1715 return NT_STATUS_NO_SUCH_USER;
1717 /* check if access can be granted as requested by client. */
1719 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
1720 se_map_generic(&des_access, &usr_generic_mapping);
1722 se_priv_copy( &se_rights, &se_machine_account );
1723 se_priv_add( &se_rights, &se_add_users );
1725 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
1726 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
1727 &acc_granted, "_samr_open_user");
1729 if ( !NT_STATUS_IS_OK(nt_status) )
1733 ret=pdb_getsampwsid(sampass, &sid);
1736 /* check that the SID exists in our domain. */
1738 return NT_STATUS_NO_SUCH_USER;
1741 TALLOC_FREE(sampass);
1743 /* associate the user's SID and access bits with the new handle. */
1744 if ((info = get_samr_info_by_sid(&sid)) == NULL)
1745 return NT_STATUS_NO_MEMORY;
1746 info->acc_granted = acc_granted;
1748 /* get a (unique) handle. open a policy on it. */
1749 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
1750 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1755 /*************************************************************************
1756 get_user_info_7. Safe. Only gives out account_name.
1757 *************************************************************************/
1759 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_SID *user_sid)
1761 struct samu *smbpass=NULL;
1764 if ( !(smbpass = samu_new( mem_ctx )) ) {
1765 return NT_STATUS_NO_MEMORY;
1769 ret = pdb_getsampwsid(smbpass, user_sid);
1773 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1774 return NT_STATUS_NO_SUCH_USER;
1777 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1780 init_sam_user_info7(id7, pdb_get_username(smbpass) );
1782 TALLOC_FREE(smbpass);
1784 return NT_STATUS_OK;
1787 /*************************************************************************
1788 get_user_info_9. Only gives out primary group SID.
1789 *************************************************************************/
1790 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx, SAM_USER_INFO_9 * id9, DOM_SID *user_sid)
1792 struct samu *smbpass=NULL;
1795 if ( !(smbpass = samu_new( mem_ctx )) ) {
1796 return NT_STATUS_NO_MEMORY;
1800 ret = pdb_getsampwsid(smbpass, user_sid);
1804 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1805 return NT_STATUS_NO_SUCH_USER;
1808 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1811 init_sam_user_info9(id9, pdb_get_group_rid(smbpass) );
1813 TALLOC_FREE(smbpass);
1815 return NT_STATUS_OK;
1818 /*************************************************************************
1819 get_user_info_16. Safe. Only gives out acb bits.
1820 *************************************************************************/
1822 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DOM_SID *user_sid)
1824 struct samu *smbpass=NULL;
1827 if ( !(smbpass = samu_new( mem_ctx )) ) {
1828 return NT_STATUS_NO_MEMORY;
1832 ret = pdb_getsampwsid(smbpass, user_sid);
1836 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1837 return NT_STATUS_NO_SUCH_USER;
1840 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1843 init_sam_user_info16(id16, pdb_get_acct_ctrl(smbpass) );
1845 TALLOC_FREE(smbpass);
1847 return NT_STATUS_OK;
1850 /*************************************************************************
1851 get_user_info_18. OK - this is the killer as it gives out password info.
1852 Ensure that this is only allowed on an encrypted connection with a root
1854 *************************************************************************/
1856 static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_INFO_18 * id18, DOM_SID *user_sid)
1858 struct samu *smbpass=NULL;
1861 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
1862 return NT_STATUS_ACCESS_DENIED;
1865 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
1866 return NT_STATUS_ACCESS_DENIED;
1870 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1873 if ( !(smbpass = samu_new( mem_ctx )) ) {
1874 return NT_STATUS_NO_MEMORY;
1877 ret = pdb_getsampwsid(smbpass, user_sid);
1880 DEBUG(4, ("User %s not found\n", sid_string_static(user_sid)));
1881 TALLOC_FREE(smbpass);
1882 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
1885 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1887 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1888 TALLOC_FREE(smbpass);
1889 return NT_STATUS_ACCOUNT_DISABLED;
1893 init_sam_user_info18(id18, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1895 TALLOC_FREE(smbpass);
1897 return NT_STATUS_OK;
1900 /*************************************************************************
1902 *************************************************************************/
1904 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DOM_SID *user_sid)
1906 struct samu *sampass=NULL;
1909 if ( !(sampass = samu_new( mem_ctx )) ) {
1910 return NT_STATUS_NO_MEMORY;
1914 ret = pdb_getsampwsid(sampass, user_sid);
1918 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1919 return NT_STATUS_NO_SUCH_USER;
1922 samr_clear_sam_passwd(sampass);
1924 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1927 init_sam_user_info20A(id20, sampass);
1929 TALLOC_FREE(sampass);
1931 return NT_STATUS_OK;
1934 /*************************************************************************
1936 *************************************************************************/
1938 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21,
1939 DOM_SID *user_sid, DOM_SID *domain_sid)
1941 struct samu *sampass=NULL;
1945 if ( !(sampass = samu_new( mem_ctx )) ) {
1946 return NT_STATUS_NO_MEMORY;
1950 ret = pdb_getsampwsid(sampass, user_sid);
1954 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1955 return NT_STATUS_NO_SUCH_USER;
1958 samr_clear_sam_passwd(sampass);
1960 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1963 nt_status = init_sam_user_info21A(id21, sampass, domain_sid);
1965 TALLOC_FREE(sampass);
1970 /*******************************************************************
1971 _samr_query_userinfo
1972 ********************************************************************/
1974 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
1976 SAM_USERINFO_CTR *ctr;
1977 struct samr_info *info = NULL;
1981 r_u->status=NT_STATUS_OK;
1983 /* search for the handle */
1984 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1985 return NT_STATUS_INVALID_HANDLE;
1987 domain_sid = info->sid;
1989 sid_split_rid(&domain_sid, &rid);
1991 if (!sid_check_is_in_our_domain(&info->sid))
1992 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1994 DEBUG(5,("_samr_query_userinfo: sid:%s\n", sid_string_static(&info->sid)));
1996 ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_USERINFO_CTR);
1998 return NT_STATUS_NO_MEMORY;
2002 /* ok! user info levels (lots: see MSDEV help), off we go... */
2003 ctr->switch_value = q_u->switch_value;
2005 DEBUG(5,("_samr_query_userinfo: user info level: %d\n", q_u->switch_value));
2007 switch (q_u->switch_value) {
2009 ctr->info.id7 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_7);
2010 if (ctr->info.id7 == NULL)
2011 return NT_STATUS_NO_MEMORY;
2013 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_7(p->mem_ctx, ctr->info.id7, &info->sid)))
2017 ctr->info.id9 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_9);
2018 if (ctr->info.id9 == NULL)
2019 return NT_STATUS_NO_MEMORY;
2021 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_9(p->mem_ctx, ctr->info.id9, &info->sid)))
2025 ctr->info.id16 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_16);
2026 if (ctr->info.id16 == NULL)
2027 return NT_STATUS_NO_MEMORY;
2029 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_16(p->mem_ctx, ctr->info.id16, &info->sid)))
2034 ctr->info.id18 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_18);
2035 if (ctr->info.id18 == NULL)
2036 return NT_STATUS_NO_MEMORY;
2038 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_18(p, p->mem_ctx, ctr->info.id18, &info->sid)))
2043 ctr->info.id20 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_20);
2044 if (ctr->info.id20 == NULL)
2045 return NT_STATUS_NO_MEMORY;
2046 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_20(p->mem_ctx, ctr->info.id20, &info->sid)))
2051 ctr->info.id21 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_21);
2052 if (ctr->info.id21 == NULL)
2053 return NT_STATUS_NO_MEMORY;
2054 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_21(p->mem_ctx, ctr->info.id21,
2055 &info->sid, &domain_sid)))
2060 return NT_STATUS_INVALID_INFO_CLASS;
2063 init_samr_r_query_userinfo(r_u, ctr, r_u->status);
2065 DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
2070 /*******************************************************************
2071 samr_reply_query_usergroups
2072 ********************************************************************/
2074 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
2076 struct samu *sam_pass=NULL;
2080 DOM_GID *gids = NULL;
2081 uint32 primary_group_rid;
2082 size_t num_groups = 0;
2090 * from the SID in the request:
2091 * we should send back the list of DOMAIN GROUPS
2092 * the user is a member of
2094 * and only the DOMAIN GROUPS
2095 * no ALIASES !!! neither aliases of the domain
2096 * nor aliases of the builtin SID
2101 r_u->status = NT_STATUS_OK;
2103 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2105 /* find the policy handle. open a policy on it. */
2106 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
2107 return NT_STATUS_INVALID_HANDLE;
2109 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_USER_GET_GROUPS, "_samr_query_usergroups"))) {
2113 if (!sid_check_is_in_our_domain(&sid))
2114 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2116 if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2117 return NT_STATUS_NO_MEMORY;
2121 ret = pdb_getsampwsid(sam_pass, &sid);
2125 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2126 sid_string_static(&sid)));
2127 return NT_STATUS_NO_SUCH_USER;
2133 result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2134 &sids, &unix_gids, &num_groups);
2137 if (!NT_STATUS_IS_OK(result)) {
2138 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2139 sid_string_static(&sid)));
2146 dom_gid.attr = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2149 if (!sid_peek_check_rid(get_global_sam_sid(),
2150 pdb_get_group_sid(sam_pass),
2151 &primary_group_rid)) {
2152 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2153 sid_string_static(pdb_get_group_sid(sam_pass)),
2154 pdb_get_username(sam_pass)));
2155 TALLOC_FREE(sam_pass);
2156 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2159 dom_gid.g_rid = primary_group_rid;
2161 ADD_TO_ARRAY(p->mem_ctx, DOM_GID, dom_gid, &gids, &num_gids);
2163 for (i=0; i<num_groups; i++) {
2165 if (!sid_peek_check_rid(get_global_sam_sid(),
2166 &(sids[i]), &dom_gid.g_rid)) {
2167 DEBUG(10, ("Found sid %s not in our domain\n",
2168 sid_string_static(&sids[i])));
2172 if (dom_gid.g_rid == primary_group_rid) {
2173 /* We added the primary group directly from the
2174 * sam_account. The other SIDs are unique from
2175 * enum_group_memberships */
2179 ADD_TO_ARRAY(p->mem_ctx, DOM_GID, dom_gid, &gids, &num_gids);
2182 /* construct the response. lkclXXXX: gids are not copied! */
2183 init_samr_r_query_usergroups(r_u, num_gids, gids, r_u->status);
2185 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2190 /*******************************************************************
2191 _samr_query_domain_info
2192 ********************************************************************/
2194 NTSTATUS _samr_query_domain_info(pipes_struct *p,
2195 SAMR_Q_QUERY_DOMAIN_INFO *q_u,
2196 SAMR_R_QUERY_DOMAIN_INFO *r_u)
2198 struct samr_info *info = NULL;
2200 uint32 min_pass_len,pass_hist,password_properties;
2201 time_t u_expire, u_min_age;
2202 NTTIME nt_expire, nt_min_age;
2204 time_t u_lock_duration, u_reset_time;
2205 NTTIME nt_lock_duration, nt_reset_time;
2210 uint32 account_policy_temp;
2215 uint32 num_users=0, num_groups=0, num_aliases=0;
2217 if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL) {
2218 return NT_STATUS_NO_MEMORY;
2223 r_u->status = NT_STATUS_OK;
2225 DEBUG(5,("_samr_query_domain_info: %d\n", __LINE__));
2227 /* find the policy handle. open a policy on it. */
2228 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info)) {
2229 return NT_STATUS_INVALID_HANDLE;
2232 switch (q_u->switch_value) {
2239 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2240 min_pass_len = account_policy_temp;
2242 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2243 pass_hist = account_policy_temp;
2245 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2246 password_properties = account_policy_temp;
2248 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2249 u_expire = account_policy_temp;
2251 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2252 u_min_age = account_policy_temp;
2258 unix_to_nt_time_abs(&nt_expire, u_expire);
2259 unix_to_nt_time_abs(&nt_min_age, u_min_age);
2261 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
2262 password_properties, nt_expire, nt_min_age);
2270 num_users = count_sam_users(info->disp_info, ACB_NORMAL);
2271 num_groups = count_sam_groups(info->disp_info);
2272 num_aliases = count_sam_aliases(info->disp_info);
2274 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
2275 u_logout = account_policy_temp;
2277 unix_to_nt_time_abs(&nt_logout, u_logout);
2279 if (!pdb_get_seq_num(&seq_num))
2280 seq_num = time(NULL);
2286 server_role = ROLE_DOMAIN_PDC;
2287 if (lp_server_role() == ROLE_DOMAIN_BDC)
2288 server_role = ROLE_DOMAIN_BDC;
2290 init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num,
2291 num_users, num_groups, num_aliases, nt_logout, server_role);
2301 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
2302 u_logout = (time_t)ul;
2309 unix_to_nt_time_abs(&nt_logout, u_logout);
2311 init_unk_info3(&ctr->info.inf3, nt_logout);
2314 init_unk_info4(&ctr->info.inf4, lp_serverstring());
2317 init_unk_info5(&ctr->info.inf5, get_global_sam_name());
2320 /* NT returns its own name when a PDC. win2k and later
2321 * only the name of the PDC if itself is a BDC (samba4
2323 init_unk_info6(&ctr->info.inf6, global_myname());
2326 server_role = ROLE_DOMAIN_PDC;
2327 if (lp_server_role() == ROLE_DOMAIN_BDC)
2328 server_role = ROLE_DOMAIN_BDC;
2330 init_unk_info7(&ctr->info.inf7, server_role);
2338 if (!pdb_get_seq_num(&seq_num)) {
2339 seq_num = time(NULL);
2346 init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
2354 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
2355 u_lock_duration = account_policy_temp;
2356 if (u_lock_duration != -1) {
2357 u_lock_duration *= 60;
2360 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
2361 u_reset_time = account_policy_temp * 60;
2363 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
2364 lockout = account_policy_temp;
2370 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
2371 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
2373 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
2376 return NT_STATUS_INVALID_INFO_CLASS;
2380 init_samr_r_query_domain_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
2382 DEBUG(5,("_samr_query_domain_info: %d\n", __LINE__));
2387 /* W2k3 seems to use the same check for all 3 objects that can be created via
2388 * SAMR, if you try to create for example "Dialup" as an alias it says
2389 * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
2392 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
2394 enum SID_NAME_USE type;
2397 DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
2400 /* Lookup in our local databases (only LOOKUP_NAME_ISOLATED set)
2401 * whether the name already exists */
2402 result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_ISOLATED,
2403 NULL, NULL, NULL, &type);
2407 DEBUG(10, ("%s does not exist, can create it\n", new_name));
2408 return NT_STATUS_OK;
2411 DEBUG(5, ("trying to create %s, exists as %s\n",
2412 new_name, sid_type_lookup(type)));
2414 if (type == SID_NAME_DOM_GRP) {
2415 return NT_STATUS_GROUP_EXISTS;
2417 if (type == SID_NAME_ALIAS) {
2418 return NT_STATUS_ALIAS_EXISTS;
2421 /* Yes, the default is NT_STATUS_USER_EXISTS */
2422 return NT_STATUS_USER_EXISTS;
2425 /*******************************************************************
2427 Create an account, can be either a normal user or a machine.
2428 This funcion will need to be updated for bdc/domain trusts.
2429 ********************************************************************/
2431 NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
2432 SAMR_R_CREATE_USER *r_u)
2436 POLICY_HND dom_pol = q_u->domain_pol;
2437 uint16 acb_info = q_u->acb_info;
2438 POLICY_HND *user_pol = &r_u->user_pol;
2439 struct samr_info *info = NULL;
2444 /* check this, when giving away 'add computer to domain' privs */
2445 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
2446 BOOL can_add_account = False;
2448 DISP_INFO *disp_info = NULL;
2450 /* Get the domain SID stored in the domain policy */
2451 if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted,
2453 return NT_STATUS_INVALID_HANDLE;
2455 nt_status = access_check_samr_function(acc_granted,
2456 SA_RIGHT_DOMAIN_CREATE_USER,
2457 "_samr_create_user");
2458 if (!NT_STATUS_IS_OK(nt_status)) {
2462 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
2463 acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
2464 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
2465 this parameter is not an account type */
2466 return NT_STATUS_INVALID_PARAMETER;
2469 account = rpcstr_pull_unistr2_talloc(p->mem_ctx, &q_u->uni_name);
2470 if (account == NULL) {
2471 return NT_STATUS_NO_MEMORY;
2474 nt_status = can_create(p->mem_ctx, account);
2475 if (!NT_STATUS_IS_OK(nt_status)) {
2479 /* determine which user right we need to check based on the acb_info */
2481 if ( acb_info & ACB_WSTRUST )
2483 se_priv_copy( &se_rights, &se_machine_account );
2484 can_add_account = user_has_privileges(
2485 p->pipe_user.nt_user_token, &se_rights );
2487 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
2488 account for domain trusts and changes the ACB flags later */
2489 else if ( acb_info & ACB_NORMAL &&
2490 (account[strlen(account)-1] != '$') )
2492 se_priv_copy( &se_rights, &se_add_users );
2493 can_add_account = user_has_privileges(
2494 p->pipe_user.nt_user_token, &se_rights );
2496 else /* implicit assumption of a BDC or domain trust account here
2497 * (we already check the flags earlier) */
2499 if ( lp_enable_privileges() ) {
2500 /* only Domain Admins can add a BDC or domain trust */
2501 se_priv_copy( &se_rights, &se_priv_none );
2502 can_add_account = nt_token_check_domain_rid(
2503 p->pipe_user.nt_user_token,
2504 DOMAIN_GROUP_RID_ADMINS );
2508 DEBUG(5, ("_samr_create_user: %s can add this account : %s\n",
2509 uidtoname(p->pipe_user.ut.uid),
2510 can_add_account ? "True":"False" ));
2512 /********** BEGIN Admin BLOCK **********/
2514 if ( can_add_account )
2517 nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
2520 if ( can_add_account )
2523 /********** END Admin BLOCK **********/
2525 /* now check for failure */
2527 if ( !NT_STATUS_IS_OK(nt_status) )
2530 /* Get the user's SID */
2532 sid_compose(&sid, get_global_sam_sid(), r_u->user_rid);
2534 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
2535 &sid, SAMR_USR_RIGHTS_WRITE_PW);
2536 se_map_generic(&des_access, &usr_generic_mapping);
2538 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2539 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2540 &acc_granted, "_samr_create_user");
2542 if ( !NT_STATUS_IS_OK(nt_status) ) {
2546 /* associate the user's SID with the new handle. */
2547 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
2548 return NT_STATUS_NO_MEMORY;
2553 info->acc_granted = acc_granted;
2555 /* get a (unique) handle. open a policy on it. */
2556 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
2557 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2560 /* After a "set" ensure we have no cached display info. */
2561 force_flush_samr_cache(info->disp_info);
2563 r_u->access_granted = acc_granted;
2565 return NT_STATUS_OK;
2568 /*******************************************************************
2569 samr_reply_connect_anon
2570 ********************************************************************/
2572 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2574 struct samr_info *info = NULL;
2575 uint32 des_access = q_u->access_mask;
2579 if (!pipe_access_check(p)) {
2580 DEBUG(3, ("access denied to samr_connect_anon\n"));
2581 r_u->status = NT_STATUS_ACCESS_DENIED;
2585 /* set up the SAMR connect_anon response */
2587 r_u->status = NT_STATUS_OK;
2589 /* associate the user's SID with the new handle. */
2590 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2591 return NT_STATUS_NO_MEMORY;
2593 /* don't give away the farm but this is probably ok. The SA_RIGHT_SAM_ENUM_DOMAINS
2594 was observed from a win98 client trying to enumerate users (when configured
2595 user level access control on shares) --jerry */
2597 if (des_access == MAXIMUM_ALLOWED_ACCESS) {
2598 /* Map to max possible knowing we're filtered below. */
2599 des_access = GENERIC_ALL_ACCESS;
2602 se_map_generic( &des_access, &sam_generic_mapping );
2603 info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
2605 info->status = q_u->unknown_0;
2607 /* get a (unique) handle. open a policy on it. */
2608 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2609 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2614 /*******************************************************************
2616 ********************************************************************/
2618 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2620 struct samr_info *info = NULL;
2621 SEC_DESC *psd = NULL;
2623 uint32 des_access = q_u->access_mask;
2628 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2632 if (!pipe_access_check(p)) {
2633 DEBUG(3, ("access denied to samr_connect\n"));
2634 r_u->status = NT_STATUS_ACCESS_DENIED;
2638 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2639 se_map_generic(&des_access, &sam_generic_mapping);
2641 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2642 NULL, 0, des_access, &acc_granted, "_samr_connect");
2644 if ( !NT_STATUS_IS_OK(nt_status) )
2647 r_u->status = NT_STATUS_OK;
2649 /* associate the user's SID and access granted with the new handle. */
2650 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2651 return NT_STATUS_NO_MEMORY;
2653 info->acc_granted = acc_granted;
2654 info->status = q_u->access_mask;
2656 /* get a (unique) handle. open a policy on it. */
2657 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2658 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2660 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2665 /*******************************************************************
2667 ********************************************************************/
2669 NTSTATUS _samr_connect4(pipes_struct *p, SAMR_Q_CONNECT4 *q_u, SAMR_R_CONNECT4 *r_u)
2671 struct samr_info *info = NULL;
2672 SEC_DESC *psd = NULL;
2674 uint32 des_access = q_u->access_mask;
2679 DEBUG(5,("_samr_connect4: %d\n", __LINE__));
2683 if (!pipe_access_check(p)) {
2684 DEBUG(3, ("access denied to samr_connect4\n"));
2685 r_u->status = NT_STATUS_ACCESS_DENIED;
2689 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2690 se_map_generic(&des_access, &sam_generic_mapping);
2692 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2693 NULL, 0, des_access, &acc_granted, "_samr_connect4");
2695 if ( !NT_STATUS_IS_OK(nt_status) )
2698 r_u->status = NT_STATUS_OK;
2700 /* associate the user's SID and access granted with the new handle. */
2701 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2702 return NT_STATUS_NO_MEMORY;
2704 info->acc_granted = acc_granted;
2705 info->status = q_u->access_mask;
2707 /* get a (unique) handle. open a policy on it. */
2708 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2709 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2711 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2716 /*******************************************************************
2718 ********************************************************************/
2720 NTSTATUS _samr_connect5(pipes_struct *p, SAMR_Q_CONNECT5 *q_u, SAMR_R_CONNECT5 *r_u)
2722 struct samr_info *info = NULL;
2723 SEC_DESC *psd = NULL;
2725 uint32 des_access = q_u->access_mask;
2731 DEBUG(5,("_samr_connect5: %d\n", __LINE__));
2737 if (!pipe_access_check(p)) {
2738 DEBUG(3, ("access denied to samr_connect5\n"));
2739 r_u->status = NT_STATUS_ACCESS_DENIED;
2743 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2744 se_map_generic(&des_access, &sam_generic_mapping);
2746 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2747 NULL, 0, des_access, &acc_granted, "_samr_connect5");
2749 if ( !NT_STATUS_IS_OK(nt_status) )
2752 /* associate the user's SID and access granted with the new handle. */
2753 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2754 return NT_STATUS_NO_MEMORY;
2756 info->acc_granted = acc_granted;
2757 info->status = q_u->access_mask;
2759 /* get a (unique) handle. open a policy on it. */
2760 if (!create_policy_hnd(p, &pol, free_samr_info, (void *)info))
2761 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2763 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2765 init_samr_r_connect5(r_u, &pol, NT_STATUS_OK);
2770 /**********************************************************************
2771 api_samr_lookup_domain
2772 **********************************************************************/
2774 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2776 struct samr_info *info;
2777 fstring domain_name;
2780 r_u->status = NT_STATUS_OK;
2782 if (!find_policy_by_hnd(p, &q_u->connect_pol, (void**)(void *)&info))
2783 return NT_STATUS_INVALID_HANDLE;
2785 /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.
2786 Reverted that change so we will work with RAS servers again */
2788 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted,
2789 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_lookup_domain")))
2794 rpcstr_pull(domain_name, q_u->uni_domain.buffer, sizeof(domain_name), q_u->uni_domain.uni_str_len*2, 0);
2798 if (strequal(domain_name, builtin_domain_name())) {
2799 sid_copy(&sid, &global_sid_Builtin);
2801 if (!secrets_fetch_domain_sid(domain_name, &sid)) {
2802 r_u->status = NT_STATUS_NO_SUCH_DOMAIN;
2806 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name, sid_string_static(&sid)));
2808 init_samr_r_lookup_domain(r_u, &sid, r_u->status);
2813 /******************************************************************
2814 makes a SAMR_R_ENUM_DOMAINS structure.
2815 ********************************************************************/
2817 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2818 UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2824 DEBUG(5, ("make_enum_domains\n"));
2827 *pp_uni_name = NULL;
2829 if (num_sam_entries == 0)
2832 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
2833 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
2835 if (sam == NULL || uni_name == NULL)
2838 for (i = 0; i < num_sam_entries; i++) {
2839 init_unistr2(&uni_name[i], doms[i], UNI_FLAGS_NONE);
2840 init_sam_entry(&sam[i], &uni_name[i], 0);
2844 *pp_uni_name = uni_name;
2849 /**********************************************************************
2850 api_samr_enum_domains
2851 **********************************************************************/
2853 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2855 struct samr_info *info;
2856 uint32 num_entries = 2;
2860 r_u->status = NT_STATUS_OK;
2862 if (!find_policy_by_hnd(p, &q_u->pol, (void**)(void *)&info))
2863 return NT_STATUS_INVALID_HANDLE;
2865 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, SA_RIGHT_SAM_ENUM_DOMAINS, "_samr_enum_domains"))) {
2869 name = get_global_sam_name();
2871 fstrcpy(dom[0],name);
2873 fstrcpy(dom[1],"Builtin");
2875 if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2876 return NT_STATUS_NO_MEMORY;
2878 init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2883 /*******************************************************************
2885 ********************************************************************/
2887 NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2890 POLICY_HND domain_pol = q_u->dom_pol;
2891 uint32 alias_rid = q_u->rid_alias;
2892 POLICY_HND *alias_pol = &r_u->pol;
2893 struct samr_info *info = NULL;
2894 SEC_DESC *psd = NULL;
2896 uint32 des_access = q_u->access_mask;
2901 r_u->status = NT_STATUS_OK;
2903 /* find the domain policy and get the SID / access bits stored in the domain policy */
2905 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
2906 return NT_STATUS_INVALID_HANDLE;
2908 status = access_check_samr_function(acc_granted,
2909 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_alias");
2911 if ( !NT_STATUS_IS_OK(status) )
2914 /* append the alias' RID to it */
2916 if (!sid_append_rid(&sid, alias_rid))
2917 return NT_STATUS_NO_SUCH_ALIAS;
2919 /*check if access can be granted as requested by client. */
2921 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
2922 se_map_generic(&des_access,&ali_generic_mapping);
2924 se_priv_copy( &se_rights, &se_add_users );
2927 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2928 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
2929 &acc_granted, "_samr_open_alias");
2931 if ( !NT_STATUS_IS_OK(status) )
2935 /* Check we actually have the requested alias */
2936 enum SID_NAME_USE type;
2941 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
2944 if (!result || (type != SID_NAME_ALIAS)) {
2945 return NT_STATUS_NO_SUCH_ALIAS;
2948 /* make sure there is a mapping */
2950 if ( !sid_to_gid( &sid, &gid ) ) {
2951 return NT_STATUS_NO_SUCH_ALIAS;
2956 /* associate the alias SID with the new handle. */
2957 if ((info = get_samr_info_by_sid(&sid)) == NULL)
2958 return NT_STATUS_NO_MEMORY;
2960 info->acc_granted = acc_granted;
2962 /* get a (unique) handle. open a policy on it. */
2963 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
2964 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2969 /*******************************************************************
2971 ********************************************************************/
2972 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
2973 const SAM_USER_INFO_7 *id7, struct samu *pwd)
2979 DEBUG(5, ("set_user_info_7: NULL id7\n"));
2981 return NT_STATUS_ACCESS_DENIED;
2984 if(!rpcstr_pull(new_name, id7->uni_name.buffer, sizeof(new_name), id7->uni_name.uni_str_len*2, 0)) {
2985 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
2987 return NT_STATUS_ACCESS_DENIED;
2990 /* check to see if the new username already exists. Note: we can't
2991 reliably lock all backends, so there is potentially the
2992 possibility that a user can be created in between this check and
2993 the rename. The rename should fail, but may not get the
2994 exact same failure status code. I think this is small enough
2995 of a window for this type of operation and the results are
2996 simply that the rename fails with a slightly different status
2997 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
2999 rc = can_create(mem_ctx, new_name);
3000 if (!NT_STATUS_IS_OK(rc)) {
3004 rc = pdb_rename_sam_account(pwd, new_name);
3010 /*******************************************************************
3012 ********************************************************************/
3014 static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, struct samu *pwd)
3017 DEBUG(5, ("set_user_info_16: NULL id16\n"));
3022 /* FIX ME: check if the value is really changed --metze */
3023 if (!pdb_set_acct_ctrl(pwd, id16->acb_info, PDB_CHANGED)) {
3028 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3038 /*******************************************************************
3040 ********************************************************************/
3042 static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, struct samu *pwd)
3046 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3051 if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd, PDB_CHANGED)) {
3055 if (!pdb_set_nt_passwd (pwd, id18->nt_pwd, PDB_CHANGED)) {
3059 if (!pdb_set_pass_changed_now (pwd)) {
3064 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3073 /*******************************************************************
3075 ********************************************************************/
3077 static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, struct samu *pwd)
3080 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3084 copy_id20_to_sam_passwd(pwd, id20);
3086 /* write the change out */
3087 if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3096 /*******************************************************************
3098 ********************************************************************/
3100 static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21,
3107 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3108 return NT_STATUS_INVALID_PARAMETER;
3111 /* we need to separately check for an account rename first */
3113 if (rpcstr_pull(new_name, id21->uni_user_name.buffer,
3114 sizeof(new_name), id21->uni_user_name.uni_str_len*2, 0)
3115 && (!strequal(new_name, pdb_get_username(pwd))))
3118 /* check to see if the new username already exists. Note: we can't
3119 reliably lock all backends, so there is potentially the
3120 possibility that a user can be created in between this check and
3121 the rename. The rename should fail, but may not get the
3122 exact same failure status code. I think this is small enough
3123 of a window for this type of operation and the results are
3124 simply that the rename fails with a slightly different status
3125 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3127 status = can_create(mem_ctx, new_name);
3128 if (!NT_STATUS_IS_OK(status)) {
3132 status = pdb_rename_sam_account(pwd, new_name);
3134 if (!NT_STATUS_IS_OK(status)) {
3135 DEBUG(0,("set_user_info_21: failed to rename account: %s\n",
3136 nt_errstr(status)));
3141 /* set the new username so that later
3142 functions can work on the new account */
3143 pdb_set_username(pwd, new_name, PDB_SET);
3146 copy_id21_to_sam_passwd(pwd, id21);
3149 * The funny part about the previous two calls is
3150 * that pwd still has the password hashes from the
3151 * passdb entry. These have not been updated from
3152 * id21. I don't know if they need to be set. --jerry
3155 if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3156 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3157 if ( !NT_STATUS_IS_OK(status) ) {
3162 /* Don't worry about writing out the user account since the
3163 primary group SID is generated solely from the user's Unix
3166 /* write the change out */
3167 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3174 return NT_STATUS_OK;
3177 /*******************************************************************
3179 ********************************************************************/
3181 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx, SAM_USER_INFO_23 *id23,
3184 pstring plaintext_buf;
3190 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3191 return NT_STATUS_INVALID_PARAMETER;
3194 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3195 pdb_get_username(pwd)));
3197 acct_ctrl = pdb_get_acct_ctrl(pwd);
3199 if (!decode_pw_buffer(id23->pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3201 return NT_STATUS_INVALID_PARAMETER;
3204 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3206 return NT_STATUS_ACCESS_DENIED;
3209 copy_id23_to_sam_passwd(pwd, id23);
3211 /* if it's a trust account, don't update /etc/passwd */
3212 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3213 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3214 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3215 DEBUG(5, ("Changing trust account. Not updating /etc/passwd\n"));
3217 /* update the UNIX password */
3218 if (lp_unix_password_sync() ) {
3219 struct passwd *passwd;
3220 if (pdb_get_username(pwd) == NULL) {
3221 DEBUG(1, ("chgpasswd: User without name???\n"));
3223 return NT_STATUS_ACCESS_DENIED;
3226 if ((passwd = Get_Pwnam(pdb_get_username(pwd))) == NULL) {
3227 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3230 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3232 return NT_STATUS_ACCESS_DENIED;
3237 ZERO_STRUCT(plaintext_buf);
3239 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
3240 (!NT_STATUS_IS_OK(status = pdb_set_unix_primary_group(mem_ctx,
3246 if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3253 return NT_STATUS_OK;
3256 /*******************************************************************
3258 ********************************************************************/
3260 static BOOL set_user_info_pw(uint8 *pass, struct samu *pwd)
3263 pstring plaintext_buf;
3266 DEBUG(5, ("Attempting administrator password change for user %s\n",
3267 pdb_get_username(pwd)));
3269 acct_ctrl = pdb_get_acct_ctrl(pwd);
3271 ZERO_STRUCT(plaintext_buf);
3273 if (!decode_pw_buffer(pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3278 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {