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 for (dpi = disp_info_list; dpi; dpi = dpi->next) {
256 if (sid_equal(psid, &dpi->sid)) {
261 /* This struct is never free'd - I'm using talloc so we
262 can get a list out of smbd using smbcontrol. There will
263 be one of these per SID we're authorative for. JRA. */
265 mem_ctx = talloc_init("DISP_INFO for domain sid %s", sid_str);
267 if ((dpi = TALLOC_ZERO_P(mem_ctx, DISP_INFO)) == NULL)
270 dpi->mem_ctx = mem_ctx;
273 sid_copy( &dpi->sid, psid);
274 dpi->builtin_domain = sid_check_is_builtin(psid);
276 dpi->builtin_domain = False;
279 DLIST_ADD(disp_info_list, dpi);
284 /*******************************************************************
285 Create a samr_info struct.
286 ********************************************************************/
288 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
290 struct samr_info *info;
295 sid_to_string(sid_str, psid);
297 fstrcpy(sid_str,"(NULL)");
300 mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
302 if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
305 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
307 sid_copy( &info->sid, psid);
308 info->builtin_domain = sid_check_is_builtin(psid);
310 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
311 info->builtin_domain = False;
313 info->mem_ctx = mem_ctx;
315 info->disp_info = get_samr_dispinfo_by_sid(psid, sid_str);
317 if (!info->disp_info) {
318 talloc_destroy(mem_ctx);
325 /*******************************************************************
326 Function to free the per SID data.
327 ********************************************************************/
329 static void free_samr_cache(DISP_INFO *disp_info, const char *sid_str)
331 DEBUG(10,("free_samr_cache: deleting cache for SID %s\n", sid_str));
333 /* We need to become root here because the paged search might have to
334 * tell the LDAP server we're not interested in the rest anymore. */
338 if (disp_info->users) {
339 DEBUG(10,("free_samr_cache: deleting users cache\n"));
340 pdb_search_destroy(disp_info->users);
341 disp_info->users = NULL;
343 if (disp_info->machines) {
344 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
345 pdb_search_destroy(disp_info->machines);
346 disp_info->machines = NULL;
348 if (disp_info->groups) {
349 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
350 pdb_search_destroy(disp_info->groups);
351 disp_info->groups = NULL;
353 if (disp_info->aliases) {
354 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
355 pdb_search_destroy(disp_info->aliases);
356 disp_info->aliases = NULL;
358 if (disp_info->enum_users) {
359 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
360 pdb_search_destroy(disp_info->enum_users);
361 disp_info->enum_users = NULL;
363 disp_info->enum_acb_mask = 0;
368 /*******************************************************************
369 Function to free the per handle data.
370 ********************************************************************/
372 static void free_samr_info(void *ptr)
374 struct samr_info *info=(struct samr_info *) ptr;
376 /* Only free the dispinfo cache if no one bothered to set up
379 if (info->disp_info && info->disp_info->di_cache_timeout_event == (smb_event_id_t)0) {
381 sid_to_string(sid_str, &info->disp_info->sid);
382 free_samr_cache(info->disp_info, sid_str);
385 talloc_destroy(info->mem_ctx);
388 /*******************************************************************
389 Idle event handler. Throw away the disp info cache.
390 ********************************************************************/
392 static void disp_info_cache_idle_timeout_handler(void **private_data,
397 DISP_INFO *disp_info = (DISP_INFO *)(*private_data);
399 sid_to_string(sid_str, &disp_info->sid);
401 free_samr_cache(disp_info, sid_str);
403 /* Remove the event. */
404 smb_unregister_idle_event(disp_info->di_cache_timeout_event);
405 disp_info->di_cache_timeout_event = (smb_event_id_t)0;
407 DEBUG(10,("disp_info_cache_idle_timeout_handler: caching timed out for SID %s at %u\n",
408 sid_str, (unsigned int)ev_now));
411 /*******************************************************************
412 Setup cache removal idle event handler.
413 ********************************************************************/
415 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
419 sid_to_string(sid_str, &disp_info->sid);
421 /* Remove any pending timeout and update. */
423 if (disp_info->di_cache_timeout_event) {
424 smb_unregister_idle_event(disp_info->di_cache_timeout_event);
425 disp_info->di_cache_timeout_event = (smb_event_id_t)0;
428 DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for SID %s for %u seconds\n",
429 sid_str, (unsigned int)secs_fromnow ));
431 disp_info->di_cache_timeout_event =
432 smb_register_idle_event(disp_info_cache_idle_timeout_handler,
437 /*******************************************************************
438 Force flush any cache. We do this on any samr_set_xxx call.
439 We must also remove the timeout handler.
440 ********************************************************************/
442 static void force_flush_samr_cache(DISP_INFO *disp_info)
447 sid_to_string(sid_str, &disp_info->sid);
448 if (disp_info->di_cache_timeout_event) {
449 smb_unregister_idle_event(disp_info->di_cache_timeout_event);
450 disp_info->di_cache_timeout_event = (smb_event_id_t)0;
451 DEBUG(10,("force_flush_samr_cache: clearing idle event for SID %s\n",
454 free_samr_cache(disp_info, sid_str);
458 /*******************************************************************
459 Ensure password info is never given out. Paranioa... JRA.
460 ********************************************************************/
462 static void samr_clear_sam_passwd(SAM_ACCOUNT *sam_pass)
468 /* These now zero out the old password */
470 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
471 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
474 static uint32 count_sam_users(struct disp_info *info, uint16 acct_flags)
476 struct samr_displayentry *entry;
478 if (info->builtin_domain) {
479 /* No users in builtin. */
483 if (info->users == NULL) {
484 info->users = pdb_search_users(acct_flags);
485 if (info->users == NULL) {
489 /* Fetch the last possible entry, thus trigger an enumeration */
490 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
492 /* Ensure we cache this enumeration. */
493 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
495 return info->users->num_entries;
498 static uint32 count_sam_groups(struct disp_info *info)
500 struct samr_displayentry *entry;
502 if (info->builtin_domain) {
503 /* No groups in builtin. */
507 if (info->groups == NULL) {
508 info->groups = pdb_search_groups();
509 if (info->groups == NULL) {
513 /* Fetch the last possible entry, thus trigger an enumeration */
514 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
516 /* Ensure we cache this enumeration. */
517 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
519 return info->groups->num_entries;
522 static uint32 count_sam_aliases(struct disp_info *info)
524 struct samr_displayentry *entry;
526 if (info->aliases == NULL) {
527 info->aliases = pdb_search_aliases(&info->sid);
528 if (info->aliases == NULL) {
532 /* Fetch the last possible entry, thus trigger an enumeration */
533 pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
535 /* Ensure we cache this enumeration. */
536 set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
538 return info->aliases->num_entries;
541 /*******************************************************************
543 ********************************************************************/
545 NTSTATUS _samr_close_hnd(pipes_struct *p, SAMR_Q_CLOSE_HND *q_u, SAMR_R_CLOSE_HND *r_u)
547 r_u->status = NT_STATUS_OK;
549 /* close the policy handle */
550 if (!close_policy_hnd(p, &q_u->pol))
551 return NT_STATUS_OBJECT_NAME_INVALID;
553 DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
558 /*******************************************************************
559 samr_reply_open_domain
560 ********************************************************************/
562 NTSTATUS _samr_open_domain(pipes_struct *p, SAMR_Q_OPEN_DOMAIN *q_u, SAMR_R_OPEN_DOMAIN *r_u)
564 struct samr_info *info;
565 SEC_DESC *psd = NULL;
567 uint32 des_access = q_u->flags;
572 r_u->status = NT_STATUS_OK;
574 /* find the connection policy handle. */
576 if ( !find_policy_by_hnd(p, &q_u->pol, (void**)(void *)&info) )
577 return NT_STATUS_INVALID_HANDLE;
579 status = access_check_samr_function( info->acc_granted,
580 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_open_domain" );
582 if ( !NT_STATUS_IS_OK(status) )
585 /*check if access can be granted as requested by client. */
587 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
588 se_map_generic( &des_access, &dom_generic_mapping );
590 se_priv_copy( &se_rights, &se_machine_account );
591 se_priv_add( &se_rights, &se_add_users );
593 status = access_check_samr_object( psd, p->pipe_user.nt_user_token,
594 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
595 &acc_granted, "_samr_open_domain" );
597 if ( !NT_STATUS_IS_OK(status) )
600 if (!sid_check_is_domain(&q_u->dom_sid.sid) &&
601 !sid_check_is_builtin(&q_u->dom_sid.sid)) {
602 return NT_STATUS_NO_SUCH_DOMAIN;
605 /* associate the domain SID with the (unique) handle. */
606 if ((info = get_samr_info_by_sid(&q_u->dom_sid.sid))==NULL)
607 return NT_STATUS_NO_MEMORY;
608 info->acc_granted = acc_granted;
610 /* get a (unique) handle. open a policy on it. */
611 if (!create_policy_hnd(p, &r_u->domain_pol, free_samr_info, (void *)info))
612 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
614 DEBUG(5,("samr_open_domain: %d\n", __LINE__));
619 /*******************************************************************
620 _samr_get_usrdom_pwinfo
621 ********************************************************************/
623 NTSTATUS _samr_get_usrdom_pwinfo(pipes_struct *p, SAMR_Q_GET_USRDOM_PWINFO *q_u, SAMR_R_GET_USRDOM_PWINFO *r_u)
625 struct samr_info *info = NULL;
627 r_u->status = NT_STATUS_OK;
629 /* find the policy handle. open a policy on it. */
630 if (!find_policy_by_hnd(p, &q_u->user_pol, (void **)(void *)&info))
631 return NT_STATUS_INVALID_HANDLE;
633 if (!sid_check_is_in_our_domain(&info->sid))
634 return NT_STATUS_OBJECT_TYPE_MISMATCH;
636 init_samr_r_get_usrdom_pwinfo(r_u, NT_STATUS_OK);
638 DEBUG(5,("_samr_get_usrdom_pwinfo: %d\n", __LINE__));
641 * NT sometimes return NT_STATUS_ACCESS_DENIED
642 * I don't know yet why.
648 /*******************************************************************
650 ********************************************************************/
652 NTSTATUS _samr_set_sec_obj(pipes_struct *p, SAMR_Q_SET_SEC_OBJ *q_u, SAMR_R_SET_SEC_OBJ *r_u)
654 DEBUG(0,("_samr_set_sec_obj: Not yet implemented!\n"));
655 return NT_STATUS_NOT_IMPLEMENTED;
658 /*******************************************************************
659 ********************************************************************/
661 static BOOL get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol,
662 DOM_SID *sid, uint32 *acc_granted,
663 DISP_INFO **ppdisp_info)
665 struct samr_info *info = NULL;
667 /* find the policy handle. open a policy on it. */
668 if (!find_policy_by_hnd(p, pol, (void **)(void *)&info))
675 *acc_granted = info->acc_granted;
677 *ppdisp_info = info->disp_info;
683 /*******************************************************************
685 ********************************************************************/
687 NTSTATUS _samr_query_sec_obj(pipes_struct *p, SAMR_Q_QUERY_SEC_OBJ *q_u, SAMR_R_QUERY_SEC_OBJ *r_u)
691 SEC_DESC * psd = NULL;
695 r_u->status = NT_STATUS_OK;
698 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &pol_sid, &acc_granted, NULL))
699 return NT_STATUS_INVALID_HANDLE;
701 DEBUG(10,("_samr_query_sec_obj: querying security on SID: %s\n", sid_to_string(str_sid, &pol_sid)));
703 /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
705 /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
706 if (pol_sid.sid_rev_num == 0) {
707 DEBUG(5,("_samr_query_sec_obj: querying security on SAM\n"));
708 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
709 } else if (sid_equal(&pol_sid,get_global_sam_sid())) {
710 /* check if it is our domain SID */
711 DEBUG(5,("_samr_query_sec_obj: querying security on Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
712 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
713 } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
714 /* check if it is the Builtin Domain */
715 /* TODO: Builtin probably needs a different SD with restricted write access*/
716 DEBUG(5,("_samr_query_sec_obj: querying security on Builtin Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
717 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
718 } else if (sid_check_is_in_our_domain(&pol_sid) ||
719 sid_check_is_in_builtin(&pol_sid)) {
720 /* TODO: different SDs have to be generated for aliases groups and users.
721 Currently all three get a default user SD */
722 DEBUG(10,("_samr_query_sec_obj: querying security on Object 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, &usr_generic_mapping, &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
725 return NT_STATUS_OBJECT_TYPE_MISMATCH;
728 if ((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
729 return NT_STATUS_NO_MEMORY;
731 if (NT_STATUS_IS_OK(r_u->status))
737 /*******************************************************************
738 makes a SAM_ENTRY / UNISTR2* structure from a user list.
739 ********************************************************************/
741 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
742 UNISTR2 **uni_name_pp,
743 uint32 num_entries, uint32 start_idx,
744 struct samr_displayentry *entries)
753 if (num_entries == 0)
756 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_entries);
758 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_entries);
760 if (sam == NULL || uni_name == NULL) {
761 DEBUG(0, ("make_user_sam_entry_list: talloc_zero failed!\n"));
762 return NT_STATUS_NO_MEMORY;
765 for (i = 0; i < num_entries; i++) {
766 UNISTR2 uni_temp_name;
768 * usrmgr expects a non-NULL terminated string with
769 * trust relationships
771 if (entries[i].acct_flags & ACB_DOMTRUST) {
772 init_unistr2(&uni_temp_name, entries[i].account_name,
775 init_unistr2(&uni_temp_name, entries[i].account_name,
779 init_sam_entry(&sam[i], &uni_temp_name, entries[i].rid);
780 copy_unistr2(&uni_name[i], &uni_temp_name);
784 *uni_name_pp = uni_name;
788 /*******************************************************************
789 samr_reply_enum_dom_users
790 ********************************************************************/
792 NTSTATUS _samr_enum_dom_users(pipes_struct *p, SAMR_Q_ENUM_DOM_USERS *q_u,
793 SAMR_R_ENUM_DOM_USERS *r_u)
795 struct samr_info *info = NULL;
797 uint32 enum_context=q_u->start_idx;
798 enum remote_arch_types ra_type = get_remote_arch();
799 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
800 uint32 max_entries = max_sam_entries;
801 struct samr_displayentry *entries = NULL;
803 r_u->status = NT_STATUS_OK;
805 /* find the policy handle. open a policy on it. */
806 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
807 return NT_STATUS_INVALID_HANDLE;
809 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted,
810 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
811 "_samr_enum_dom_users"))) {
815 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
817 if (info->builtin_domain) {
818 /* No users in builtin. */
819 init_samr_r_enum_dom_users(r_u, q_u->start_idx, 0);
820 DEBUG(5,("_samr_enum_dom_users: No users in BUILTIN\n"));
828 if ((info->disp_info->enum_users != NULL) &&
829 (info->disp_info->enum_acb_mask != q_u->acb_mask)) {
830 pdb_search_destroy(info->disp_info->enum_users);
831 info->disp_info->enum_users = NULL;
834 if (info->disp_info->enum_users == NULL) {
835 info->disp_info->enum_users = pdb_search_users(q_u->acb_mask);
836 info->disp_info->enum_acb_mask = q_u->acb_mask;
839 if (info->disp_info->enum_users == NULL) {
840 /* END AS ROOT !!!! */
842 return NT_STATUS_ACCESS_DENIED;
845 num_account = pdb_search_entries(info->disp_info->enum_users,
846 enum_context, max_entries,
849 /* END AS ROOT !!!! */
853 if (num_account == 0) {
854 DEBUG(5, ("_samr_enum_dom_users: enumeration handle over "
859 r_u->status = make_user_sam_entry_list(p->mem_ctx, &r_u->sam,
861 num_account, enum_context,
864 if (!NT_STATUS_IS_OK(r_u->status))
867 if (max_entries <= num_account) {
868 r_u->status = STATUS_MORE_ENTRIES;
870 r_u->status = NT_STATUS_OK;
873 /* Ensure we cache this enumeration. */
874 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
876 DEBUG(5, ("_samr_enum_dom_users: %d\n", __LINE__));
878 init_samr_r_enum_dom_users(r_u, q_u->start_idx + num_account,
881 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
886 /*******************************************************************
887 makes a SAM_ENTRY / UNISTR2* structure from a group list.
888 ********************************************************************/
890 static void make_group_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
891 UNISTR2 **uni_name_pp,
892 uint32 num_sam_entries,
893 struct samr_displayentry *entries)
902 if (num_sam_entries == 0)
905 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
906 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
908 if (sam == NULL || uni_name == NULL) {
909 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
913 for (i = 0; i < num_sam_entries; i++) {
915 * JRA. I think this should include the null. TNG does not.
917 init_unistr2(&uni_name[i], entries[i].account_name,
919 init_sam_entry(&sam[i], &uni_name[i], entries[i].rid);
923 *uni_name_pp = uni_name;
926 /*******************************************************************
927 samr_reply_enum_dom_groups
928 ********************************************************************/
930 NTSTATUS _samr_enum_dom_groups(pipes_struct *p, SAMR_Q_ENUM_DOM_GROUPS *q_u, SAMR_R_ENUM_DOM_GROUPS *r_u)
932 struct samr_info *info = NULL;
933 struct samr_displayentry *groups;
936 r_u->status = NT_STATUS_OK;
938 /* find the policy handle. open a policy on it. */
939 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
940 return NT_STATUS_INVALID_HANDLE;
942 r_u->status = access_check_samr_function(info->acc_granted,
943 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
944 "_samr_enum_dom_groups");
945 if (!NT_STATUS_IS_OK(r_u->status))
948 DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
950 if (info->builtin_domain) {
951 /* No groups in builtin. */
952 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, 0);
953 DEBUG(5,("_samr_enum_dom_users: No groups in BUILTIN\n"));
957 /* the domain group array is being allocated in the function below */
961 if (info->disp_info->groups == NULL) {
962 info->disp_info->groups = pdb_search_groups();
964 if (info->disp_info->groups == NULL) {
966 return NT_STATUS_ACCESS_DENIED;
970 num_groups = pdb_search_entries(info->disp_info->groups, q_u->start_idx,
971 MAX_SAM_ENTRIES, &groups);
974 /* Ensure we cache this enumeration. */
975 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
977 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
980 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, num_groups);
982 DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
987 /*******************************************************************
988 samr_reply_enum_dom_aliases
989 ********************************************************************/
991 NTSTATUS _samr_enum_dom_aliases(pipes_struct *p, SAMR_Q_ENUM_DOM_ALIASES *q_u, SAMR_R_ENUM_DOM_ALIASES *r_u)
993 struct samr_info *info;
994 struct samr_displayentry *aliases;
995 uint32 num_aliases = 0;
997 /* find the policy handle. open a policy on it. */
998 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
999 return NT_STATUS_INVALID_HANDLE;
1001 r_u->status = access_check_samr_function(info->acc_granted,
1002 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1003 "_samr_enum_dom_aliases");
1004 if (!NT_STATUS_IS_OK(r_u->status))
1007 DEBUG(5,("samr_reply_enum_dom_aliases: sid %s\n",
1008 sid_string_static(&info->sid)));
1012 if (info->disp_info->aliases == NULL) {
1013 info->disp_info->aliases = pdb_search_aliases(&info->sid);
1014 if (info->disp_info->aliases == NULL) {
1016 return NT_STATUS_ACCESS_DENIED;
1020 num_aliases = pdb_search_entries(info->disp_info->aliases, q_u->start_idx,
1021 MAX_SAM_ENTRIES, &aliases);
1024 /* Ensure we cache this enumeration. */
1025 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1027 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
1028 num_aliases, aliases);
1030 init_samr_r_enum_dom_aliases(r_u, q_u->start_idx + num_aliases,
1033 DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
1038 /*******************************************************************
1039 samr_reply_query_dispinfo
1040 ********************************************************************/
1042 NTSTATUS _samr_query_dispinfo(pipes_struct *p, SAMR_Q_QUERY_DISPINFO *q_u,
1043 SAMR_R_QUERY_DISPINFO *r_u)
1045 struct samr_info *info = NULL;
1046 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1048 uint32 max_entries=q_u->max_entries;
1049 uint32 enum_context=q_u->start_idx;
1050 uint32 max_size=q_u->max_size;
1052 SAM_DISPINFO_CTR *ctr;
1053 uint32 temp_size=0, total_data_size=0;
1054 NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1055 uint32 num_account = 0;
1056 enum remote_arch_types ra_type = get_remote_arch();
1057 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1058 struct samr_displayentry *entries = NULL;
1060 DEBUG(5, ("samr_reply_query_dispinfo: %d\n", __LINE__));
1061 r_u->status = NT_STATUS_UNSUCCESSFUL;
1063 /* find the policy handle. open a policy on it. */
1064 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info))
1065 return NT_STATUS_INVALID_HANDLE;
1068 * calculate how many entries we will return.
1070 * - the number of entries the client asked
1071 * - our limit on that
1072 * - the starting point (enumeration context)
1073 * - the buffer size the client will accept
1077 * We are a lot more like W2K. Instead of reading the SAM
1078 * each time to find the records we need to send back,
1079 * we read it once and link that copy to the sam handle.
1080 * For large user list (over the MAX_SAM_ENTRIES)
1081 * it's a definitive win.
1082 * second point to notice: between enumerations
1083 * our sam is now the same as it's a snapshoot.
1084 * third point: got rid of the static SAM_USER_21 struct
1085 * no more intermediate.
1086 * con: it uses much more memory, as a full copy is stored
1089 * If you want to change it, think twice and think
1090 * of the second point , that's really important.
1095 if ((q_u->switch_level < 1) || (q_u->switch_level > 5)) {
1096 DEBUG(0,("_samr_query_dispinfo: Unknown info level (%u)\n",
1097 (unsigned int)q_u->switch_level ));
1098 return NT_STATUS_INVALID_INFO_CLASS;
1101 /* first limit the number of entries we will return */
1102 if(max_entries > max_sam_entries) {
1103 DEBUG(5, ("samr_reply_query_dispinfo: client requested %d "
1104 "entries, limiting to %d\n", max_entries,
1106 max_entries = max_sam_entries;
1109 /* calculate the size and limit on the number of entries we will
1112 temp_size=max_entries*struct_size;
1114 if (temp_size>max_size) {
1115 max_entries=MIN((max_size/struct_size),max_entries);;
1116 DEBUG(5, ("samr_reply_query_dispinfo: buffer size limits to "
1117 "only %d entries\n", max_entries));
1120 if (!(ctr = TALLOC_ZERO_P(p->mem_ctx,SAM_DISPINFO_CTR)))
1121 return NT_STATUS_NO_MEMORY;
1127 /* THe following done as ROOT. Don't return without unbecome_root(). */
1129 switch (q_u->switch_level) {
1132 if (info->disp_info->users == NULL) {
1133 info->disp_info->users = pdb_search_users(ACB_NORMAL);
1134 if (info->disp_info->users == NULL) {
1136 return NT_STATUS_ACCESS_DENIED;
1138 DEBUG(10,("samr_reply_query_dispinfo: starting user enumeration at index %u\n",
1139 (unsigned int)enum_context ));
1141 DEBUG(10,("samr_reply_query_dispinfo: using cached user enumeration at index %u\n",
1142 (unsigned int)enum_context ));
1145 num_account = pdb_search_entries(info->disp_info->users,
1146 enum_context, max_entries,
1150 if (info->disp_info->machines == NULL) {
1151 info->disp_info->machines =
1152 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
1153 if (info->disp_info->machines == NULL) {
1155 return NT_STATUS_ACCESS_DENIED;
1157 DEBUG(10,("samr_reply_query_dispinfo: starting machine enumeration at index %u\n",
1158 (unsigned int)enum_context ));
1160 DEBUG(10,("samr_reply_query_dispinfo: using cached machine enumeration at index %u\n",
1161 (unsigned int)enum_context ));
1164 num_account = pdb_search_entries(info->disp_info->machines,
1165 enum_context, max_entries,
1170 if (info->disp_info->groups == NULL) {
1171 info->disp_info->groups = pdb_search_groups();
1172 if (info->disp_info->groups == NULL) {
1174 return NT_STATUS_ACCESS_DENIED;
1176 DEBUG(10,("samr_reply_query_dispinfo: starting group enumeration at index %u\n",
1177 (unsigned int)enum_context ));
1179 DEBUG(10,("samr_reply_query_dispinfo: using cached group enumeration at index %u\n",
1180 (unsigned int)enum_context ));
1183 num_account = pdb_search_entries(info->disp_info->groups,
1184 enum_context, max_entries,
1189 smb_panic("info class changed");
1194 /* Now create reply structure */
1195 switch (q_u->switch_level) {
1197 disp_ret = init_sam_dispinfo_1(p->mem_ctx, &ctr->sam.info1,
1198 num_account, enum_context,
1202 disp_ret = init_sam_dispinfo_2(p->mem_ctx, &ctr->sam.info2,
1203 num_account, enum_context,
1207 disp_ret = init_sam_dispinfo_3(p->mem_ctx, &ctr->sam.info3,
1208 num_account, enum_context,
1212 disp_ret = init_sam_dispinfo_4(p->mem_ctx, &ctr->sam.info4,
1213 num_account, enum_context,
1217 disp_ret = init_sam_dispinfo_5(p->mem_ctx, &ctr->sam.info5,
1218 num_account, enum_context,
1222 smb_panic("info class changed");
1226 if (!NT_STATUS_IS_OK(disp_ret))
1229 /* calculate the total size */
1230 total_data_size=num_account*struct_size;
1233 r_u->status = STATUS_MORE_ENTRIES;
1235 r_u->status = NT_STATUS_OK;
1238 /* Ensure we cache this enumeration. */
1239 set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1241 DEBUG(5, ("_samr_query_dispinfo: %d\n", __LINE__));
1243 init_samr_r_query_dispinfo(r_u, num_account, total_data_size,
1244 temp_size, q_u->switch_level, ctr,
1251 /*******************************************************************
1252 samr_reply_query_aliasinfo
1253 ********************************************************************/
1255 NTSTATUS _samr_query_aliasinfo(pipes_struct *p, SAMR_Q_QUERY_ALIASINFO *q_u, SAMR_R_QUERY_ALIASINFO *r_u)
1258 struct acct_info info;
1262 r_u->status = NT_STATUS_OK;
1264 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1266 /* find the policy handle. open a policy on it. */
1267 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
1268 return NT_STATUS_INVALID_HANDLE;
1269 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_LOOKUP_INFO, "_samr_query_aliasinfo"))) {
1274 ret = pdb_get_aliasinfo(&sid, &info);
1278 return NT_STATUS_NO_SUCH_ALIAS;
1280 if ( !(r_u->ctr = TALLOC_ZERO_P( p->mem_ctx, ALIAS_INFO_CTR )) )
1281 return NT_STATUS_NO_MEMORY;
1284 switch (q_u->level ) {
1286 r_u->ctr->level = 1;
1287 init_samr_alias_info1(&r_u->ctr->alias.info1, info.acct_name, 1, info.acct_desc);
1290 r_u->ctr->level = 3;
1291 init_samr_alias_info3(&r_u->ctr->alias.info3, info.acct_desc);
1294 return NT_STATUS_INVALID_INFO_CLASS;
1297 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1303 /*******************************************************************
1304 samr_reply_lookup_ids
1305 ********************************************************************/
1307 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1309 uint32 rid[MAX_SAM_ENTRIES];
1310 int num_rids = q_u->num_sids1;
1312 r_u->status = NT_STATUS_OK;
1314 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1316 if (num_rids > MAX_SAM_ENTRIES) {
1317 num_rids = MAX_SAM_ENTRIES;
1318 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1323 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1325 for (i = 0; i < num_rids && status == 0; i++)
1327 struct sam_passwd *sam_pass;
1331 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1332 q_u->uni_user_name[i].uni_str_len));
1334 /* find the user account */
1336 sam_pass = get_smb21pwd_entry(user_name, 0);
1339 if (sam_pass == NULL)
1341 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1346 rid[i] = sam_pass->user_rid;
1352 rid[0] = BUILTIN_ALIAS_RID_USERS;
1354 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1356 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1362 /*******************************************************************
1364 ********************************************************************/
1366 NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LOOKUP_NAMES *r_u)
1368 uint32 rid[MAX_SAM_ENTRIES];
1370 enum SID_NAME_USE type[MAX_SAM_ENTRIES];
1371 enum SID_NAME_USE local_type;
1373 int num_rids = q_u->num_names2;
1378 r_u->status = NT_STATUS_OK;
1380 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1385 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL)) {
1386 init_samr_r_lookup_names(p->mem_ctx, r_u, 0, NULL, NULL, NT_STATUS_OBJECT_TYPE_MISMATCH);
1390 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 */
1394 if (num_rids > MAX_SAM_ENTRIES) {
1395 num_rids = MAX_SAM_ENTRIES;
1396 DEBUG(5,("_samr_lookup_names: truncating entries to %d\n", num_rids));
1399 DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1401 for (i = 0; i < num_rids; i++) {
1406 r_u->status = NT_STATUS_NONE_MAPPED;
1408 rid [i] = 0xffffffff;
1409 type[i] = SID_NAME_UNKNOWN;
1411 ret = rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1414 * we are only looking for a name
1415 * the SID we get back can be outside
1416 * the scope of the pol_sid
1418 * in clear: it prevents to reply to domain\group: yes
1419 * when only builtin\group exists.
1421 * a cleaner code is to add the sid of the domain we're looking in
1422 * to the local_lookup_name function.
1425 if ((ret > 0) && local_lookup_name(name, &sid, &local_type)) {
1426 sid_split_rid(&sid, &local_rid);
1428 if (sid_equal(&sid, &pol_sid)) {
1431 /* Windows does not return WKN_GRP here, even
1432 * on lookups in builtin */
1433 type[i] = (local_type == SID_NAME_WKN_GRP) ?
1434 SID_NAME_ALIAS : local_type;
1436 r_u->status = NT_STATUS_OK;
1441 init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, (uint32 *)type, r_u->status);
1443 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1448 /*******************************************************************
1449 _samr_chgpasswd_user
1450 ********************************************************************/
1452 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1457 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1459 r_u->status = NT_STATUS_OK;
1461 rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1462 rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1464 DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1467 * Pass the user through the NT -> unix user mapping
1471 (void)map_username(user_name);
1474 * UNIX username case mangling not required, pass_oem_change
1475 * is case insensitive.
1478 r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1479 q_u->nt_newpass.pass, q_u->nt_oldhash.hash);
1481 init_samr_r_chgpasswd_user(r_u, r_u->status);
1483 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1488 /*******************************************************************
1489 makes a SAMR_R_LOOKUP_RIDS structure.
1490 ********************************************************************/
1492 static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1493 const char **names, UNIHDR **pp_hdr_name,
1494 UNISTR2 **pp_uni_name)
1497 UNIHDR *hdr_name=NULL;
1498 UNISTR2 *uni_name=NULL;
1500 *pp_uni_name = NULL;
1501 *pp_hdr_name = NULL;
1503 if (num_names != 0) {
1504 hdr_name = TALLOC_ZERO_ARRAY(ctx, UNIHDR, num_names);
1505 if (hdr_name == NULL)
1508 uni_name = TALLOC_ZERO_ARRAY(ctx,UNISTR2, num_names);
1509 if (uni_name == NULL)
1513 for (i = 0; i < num_names; i++) {
1514 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1515 init_unistr2(&uni_name[i], names[i], UNI_FLAGS_NONE);
1516 init_uni_hdr(&hdr_name[i], &uni_name[i]);
1519 *pp_uni_name = uni_name;
1520 *pp_hdr_name = hdr_name;
1525 /*******************************************************************
1527 ********************************************************************/
1529 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1532 uint32 *attrs = NULL;
1533 UNIHDR *hdr_name = NULL;
1534 UNISTR2 *uni_name = NULL;
1536 int num_rids = q_u->num_rids1;
1539 r_u->status = NT_STATUS_OK;
1541 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1543 /* find the policy handle. open a policy on it. */
1544 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL))
1545 return NT_STATUS_INVALID_HANDLE;
1547 if (num_rids > 1000) {
1548 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
1549 "to samba4 idl this is not possible\n", num_rids));
1550 return NT_STATUS_UNSUCCESSFUL;
1553 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
1554 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
1556 if ((num_rids != 0) && ((names == NULL) || (attrs == NULL)))
1557 return NT_STATUS_NO_MEMORY;
1559 become_root(); /* lookup_sid can require root privs */
1560 r_u->status = pdb_lookup_rids(&pol_sid, num_rids, q_u->rid,
1564 if(!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
1565 &hdr_name, &uni_name))
1566 return NT_STATUS_NO_MEMORY;
1568 init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, attrs);
1570 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1575 /*******************************************************************
1576 _samr_open_user. Safe - gives out no passwd info.
1577 ********************************************************************/
1579 NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1581 SAM_ACCOUNT *sampass=NULL;
1583 POLICY_HND domain_pol = q_u->domain_pol;
1584 POLICY_HND *user_pol = &r_u->user_pol;
1585 struct samr_info *info = NULL;
1586 SEC_DESC *psd = NULL;
1588 uint32 des_access = q_u->access_mask;
1594 r_u->status = NT_STATUS_OK;
1596 /* find the domain policy handle and get domain SID / access bits in the domain policy. */
1598 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
1599 return NT_STATUS_INVALID_HANDLE;
1601 nt_status = access_check_samr_function( acc_granted,
1602 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_user" );
1604 if ( !NT_STATUS_IS_OK(nt_status) )
1607 nt_status = pdb_init_sam_talloc(p->mem_ctx, &sampass);
1609 if (!NT_STATUS_IS_OK(nt_status))
1612 /* append the user's RID to it */
1614 if (!sid_append_rid(&sid, q_u->user_rid))
1615 return NT_STATUS_NO_SUCH_USER;
1617 /* check if access can be granted as requested by client. */
1619 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
1620 se_map_generic(&des_access, &usr_generic_mapping);
1622 se_priv_copy( &se_rights, &se_machine_account );
1623 se_priv_add( &se_rights, &se_add_users );
1625 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
1626 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
1627 &acc_granted, "_samr_open_user");
1629 if ( !NT_STATUS_IS_OK(nt_status) )
1633 ret=pdb_getsampwsid(sampass, &sid);
1636 /* check that the SID exists in our domain. */
1638 return NT_STATUS_NO_SUCH_USER;
1641 pdb_free_sam(&sampass);
1643 /* associate the user's SID and access bits with the new handle. */
1644 if ((info = get_samr_info_by_sid(&sid)) == NULL)
1645 return NT_STATUS_NO_MEMORY;
1646 info->acc_granted = acc_granted;
1648 /* get a (unique) handle. open a policy on it. */
1649 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
1650 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1655 /*************************************************************************
1656 get_user_info_7. Safe. Only gives out account_name.
1657 *************************************************************************/
1659 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_SID *user_sid)
1661 SAM_ACCOUNT *smbpass=NULL;
1665 nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1667 if (!NT_STATUS_IS_OK(nt_status)) {
1672 ret = pdb_getsampwsid(smbpass, user_sid);
1676 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1677 return NT_STATUS_NO_SUCH_USER;
1680 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1683 init_sam_user_info7(id7, pdb_get_username(smbpass) );
1685 pdb_free_sam(&smbpass);
1687 return NT_STATUS_OK;
1689 /*************************************************************************
1690 get_user_info_16. Safe. Only gives out acb bits.
1691 *************************************************************************/
1693 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DOM_SID *user_sid)
1695 SAM_ACCOUNT *smbpass=NULL;
1699 nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1701 if (!NT_STATUS_IS_OK(nt_status)) {
1706 ret = pdb_getsampwsid(smbpass, user_sid);
1710 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1711 return NT_STATUS_NO_SUCH_USER;
1714 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1717 init_sam_user_info16(id16, pdb_get_acct_ctrl(smbpass) );
1719 pdb_free_sam(&smbpass);
1721 return NT_STATUS_OK;
1724 /*************************************************************************
1725 get_user_info_18. OK - this is the killer as it gives out password info.
1726 Ensure that this is only allowed on an encrypted connection with a root
1728 *************************************************************************/
1730 static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_INFO_18 * id18, DOM_SID *user_sid)
1732 SAM_ACCOUNT *smbpass=NULL;
1736 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
1737 return NT_STATUS_ACCESS_DENIED;
1740 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
1741 return NT_STATUS_ACCESS_DENIED;
1745 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1748 nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1750 if (!NT_STATUS_IS_OK(nt_status)) {
1754 ret = pdb_getsampwsid(smbpass, user_sid);
1757 DEBUG(4, ("User %s not found\n", sid_string_static(user_sid)));
1758 pdb_free_sam(&smbpass);
1759 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
1762 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1764 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1765 pdb_free_sam(&smbpass);
1766 return NT_STATUS_ACCOUNT_DISABLED;
1770 init_sam_user_info18(id18, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1772 pdb_free_sam(&smbpass);
1774 return NT_STATUS_OK;
1777 /*************************************************************************
1779 *************************************************************************/
1781 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DOM_SID *user_sid)
1783 SAM_ACCOUNT *sampass=NULL;
1786 pdb_init_sam_talloc(mem_ctx, &sampass);
1789 ret = pdb_getsampwsid(sampass, user_sid);
1793 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1794 return NT_STATUS_NO_SUCH_USER;
1797 samr_clear_sam_passwd(sampass);
1799 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1802 init_sam_user_info20A(id20, sampass);
1804 pdb_free_sam(&sampass);
1806 return NT_STATUS_OK;
1809 /*************************************************************************
1811 *************************************************************************/
1813 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21,
1814 DOM_SID *user_sid, DOM_SID *domain_sid)
1816 SAM_ACCOUNT *sampass=NULL;
1820 nt_status = pdb_init_sam_talloc(mem_ctx, &sampass);
1821 if (!NT_STATUS_IS_OK(nt_status)) {
1826 ret = pdb_getsampwsid(sampass, user_sid);
1830 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1831 return NT_STATUS_NO_SUCH_USER;
1834 samr_clear_sam_passwd(sampass);
1836 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1839 nt_status = init_sam_user_info21A(id21, sampass, domain_sid);
1841 pdb_free_sam(&sampass);
1843 return NT_STATUS_OK;
1846 /*******************************************************************
1847 _samr_query_userinfo
1848 ********************************************************************/
1850 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
1852 SAM_USERINFO_CTR *ctr;
1853 struct samr_info *info = NULL;
1857 r_u->status=NT_STATUS_OK;
1859 /* search for the handle */
1860 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1861 return NT_STATUS_INVALID_HANDLE;
1863 domain_sid = info->sid;
1865 sid_split_rid(&domain_sid, &rid);
1867 if (!sid_check_is_in_our_domain(&info->sid))
1868 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1870 DEBUG(5,("_samr_query_userinfo: sid:%s\n", sid_string_static(&info->sid)));
1872 ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_USERINFO_CTR);
1874 return NT_STATUS_NO_MEMORY;
1878 /* ok! user info levels (lots: see MSDEV help), off we go... */
1879 ctr->switch_value = q_u->switch_value;
1881 switch (q_u->switch_value) {
1883 ctr->info.id7 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_7);
1884 if (ctr->info.id7 == NULL)
1885 return NT_STATUS_NO_MEMORY;
1887 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_7(p->mem_ctx, ctr->info.id7, &info->sid)))
1891 ctr->info.id16 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_16);
1892 if (ctr->info.id16 == NULL)
1893 return NT_STATUS_NO_MEMORY;
1895 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_16(p->mem_ctx, ctr->info.id16, &info->sid)))
1900 /* whoops - got this wrong. i think. or don't understand what's happening. */
1904 info = (void *)&id11;
1906 expire.low = 0xffffffff;
1907 expire.high = 0x7fffffff;
1909 ctr->info.id = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_17));
1910 ZERO_STRUCTP(ctr->info.id17);
1911 init_sam_user_info17(ctr->info.id17, &expire,
1912 "BROOKFIELDS$", /* name */
1913 0x03ef, /* user rid */
1914 0x201, /* group rid */
1915 0x0080); /* acb info */
1922 ctr->info.id18 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_18);
1923 if (ctr->info.id18 == NULL)
1924 return NT_STATUS_NO_MEMORY;
1926 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_18(p, p->mem_ctx, ctr->info.id18, &info->sid)))
1931 ctr->info.id20 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_20);
1932 if (ctr->info.id20 == NULL)
1933 return NT_STATUS_NO_MEMORY;
1934 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_20(p->mem_ctx, ctr->info.id20, &info->sid)))
1939 ctr->info.id21 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_21);
1940 if (ctr->info.id21 == NULL)
1941 return NT_STATUS_NO_MEMORY;
1942 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_21(p->mem_ctx, ctr->info.id21,
1943 &info->sid, &domain_sid)))
1948 return NT_STATUS_INVALID_INFO_CLASS;
1951 init_samr_r_query_userinfo(r_u, ctr, r_u->status);
1953 DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
1958 /*******************************************************************
1959 samr_reply_query_usergroups
1960 ********************************************************************/
1962 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
1964 SAM_ACCOUNT *sam_pass=NULL;
1965 struct passwd *passwd;
1968 DOM_GID *gids = NULL;
1969 size_t num_groups = 0;
1977 * from the SID in the request:
1978 * we should send back the list of DOMAIN GROUPS
1979 * the user is a member of
1981 * and only the DOMAIN GROUPS
1982 * no ALIASES !!! neither aliases of the domain
1983 * nor aliases of the builtin SID
1988 r_u->status = NT_STATUS_OK;
1990 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1992 /* find the policy handle. open a policy on it. */
1993 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
1994 return NT_STATUS_INVALID_HANDLE;
1996 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_USER_GET_GROUPS, "_samr_query_usergroups"))) {
2000 if (!sid_check_is_in_our_domain(&sid))
2001 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2003 pdb_init_sam(&sam_pass);
2006 ret = pdb_getsampwsid(sam_pass, &sid);
2010 pdb_free_sam(&sam_pass);
2011 return NT_STATUS_NO_SUCH_USER;
2014 passwd = getpwnam_alloc(pdb_get_username(sam_pass));
2015 if (passwd == NULL) {
2016 pdb_free_sam(&sam_pass);
2017 return NT_STATUS_NO_SUCH_USER;
2023 result = pdb_enum_group_memberships(pdb_get_username(sam_pass),
2025 &sids, &unix_gids, &num_groups);
2028 pdb_free_sam(&sam_pass);
2029 passwd_free(&passwd);
2031 if (!NT_STATUS_IS_OK(result))
2034 SAFE_FREE(unix_gids);
2039 for (i=0; i<num_groups; i++) {
2042 if (!sid_peek_check_rid(get_global_sam_sid(),
2046 gids = TALLOC_REALLOC_ARRAY(p->mem_ctx, gids, DOM_GID, num_gids+1);
2047 gids[num_gids].attr= (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|SE_GROUP_ENABLED);
2048 gids[num_gids].g_rid = rid;
2053 /* construct the response. lkclXXXX: gids are not copied! */
2054 init_samr_r_query_usergroups(r_u, num_groups, gids, r_u->status);
2056 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2061 /*******************************************************************
2062 _samr_query_dom_info
2063 ********************************************************************/
2065 NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SAMR_R_QUERY_DOMAIN_INFO *r_u)
2067 struct samr_info *info = NULL;
2069 uint32 min_pass_len,pass_hist,flag;
2070 time_t u_expire, u_min_age;
2071 NTTIME nt_expire, nt_min_age;
2073 time_t u_lock_duration, u_reset_time;
2074 NTTIME nt_lock_duration, nt_reset_time;
2079 uint32 account_policy_temp;
2084 uint32 num_users=0, num_groups=0, num_aliases=0;
2086 if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL) {
2087 return NT_STATUS_NO_MEMORY;
2092 r_u->status = NT_STATUS_OK;
2094 DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
2096 /* find the policy handle. open a policy on it. */
2097 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info)) {
2098 return NT_STATUS_INVALID_HANDLE;
2101 switch (q_u->switch_value) {
2108 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2109 min_pass_len = account_policy_temp;
2111 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2112 pass_hist = account_policy_temp;
2114 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2115 flag = account_policy_temp;
2117 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2118 u_expire = account_policy_temp;
2120 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2121 u_min_age = account_policy_temp;
2127 unix_to_nt_time_abs(&nt_expire, u_expire);
2128 unix_to_nt_time_abs(&nt_min_age, u_min_age);
2130 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
2131 flag, nt_expire, nt_min_age);
2139 num_users = count_sam_users(info->disp_info, ACB_NORMAL);
2140 num_groups = count_sam_groups(info->disp_info);
2141 num_aliases = count_sam_aliases(info->disp_info);
2143 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
2144 u_logout = account_policy_temp;
2146 unix_to_nt_time_abs(&nt_logout, u_logout);
2148 if (!pdb_get_seq_num(&seq_num))
2149 seq_num = time(NULL);
2155 server_role = ROLE_DOMAIN_PDC;
2156 if (lp_server_role() == ROLE_DOMAIN_BDC)
2157 server_role = ROLE_DOMAIN_BDC;
2159 init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num,
2160 num_users, num_groups, num_aliases, nt_logout, server_role);
2170 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
2171 u_logout = (time_t)ul;
2178 unix_to_nt_time_abs(&nt_logout, u_logout);
2180 init_unk_info3(&ctr->info.inf3, nt_logout);
2183 init_unk_info5(&ctr->info.inf5, global_myname());
2186 init_unk_info6(&ctr->info.inf6);
2189 server_role = ROLE_DOMAIN_PDC;
2190 if (lp_server_role() == ROLE_DOMAIN_BDC)
2191 server_role = ROLE_DOMAIN_BDC;
2193 init_unk_info7(&ctr->info.inf7, server_role);
2201 if (!pdb_get_seq_num(&seq_num)) {
2202 seq_num = time(NULL);
2209 init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
2217 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
2218 u_lock_duration = account_policy_temp;
2219 if (u_lock_duration != -1) {
2220 u_lock_duration *= 60;
2223 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
2224 u_reset_time = account_policy_temp * 60;
2226 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
2227 lockout = account_policy_temp;
2233 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
2234 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
2236 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
2239 return NT_STATUS_INVALID_INFO_CLASS;
2243 init_samr_r_query_dom_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
2245 DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
2250 /*******************************************************************
2252 Create an account, can be either a normal user or a machine.
2253 This funcion will need to be updated for bdc/domain trusts.
2254 ********************************************************************/
2256 NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u, SAMR_R_CREATE_USER *r_u)
2258 SAM_ACCOUNT *sam_pass=NULL;
2262 POLICY_HND dom_pol = q_u->domain_pol;
2263 UNISTR2 user_account = q_u->uni_name;
2264 uint16 acb_info = q_u->acb_info;
2265 POLICY_HND *user_pol = &r_u->user_pol;
2266 struct samr_info *info = NULL;
2274 /* check this, when giving away 'add computer to domain' privs */
2275 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
2276 BOOL can_add_account = False;
2278 DISP_INFO *disp_info = NULL;
2280 /* Get the domain SID stored in the domain policy */
2281 if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted, &disp_info))
2282 return NT_STATUS_INVALID_HANDLE;
2284 if (!NT_STATUS_IS_OK(nt_status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_USER, "_samr_create_user"))) {
2288 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST || acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
2289 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
2290 this parameter is not an account type */
2291 return NT_STATUS_INVALID_PARAMETER;
2294 rpcstr_pull(account, user_account.buffer, sizeof(account), user_account.uni_str_len*2, 0);
2295 strlower_m(account);
2297 pdb_init_sam(&sam_pass);
2300 ret = pdb_getsampwnam(sam_pass, account);
2303 /* this account exists: say so */
2304 pdb_free_sam(&sam_pass);
2305 return NT_STATUS_USER_EXISTS;
2308 pdb_free_sam(&sam_pass);
2310 /*********************************************************************
2311 * HEADS UP! If we have to create a new user account, we have to get
2312 * a new RID from somewhere. This used to be done by the passdb
2313 * backend. It has been moved into idmap now. Since idmap is now
2314 * wrapped up behind winbind, this means you have to run winbindd if you
2315 * want new accounts to get a new RID when "enable rid algorithm = no".
2316 * Tough. We now have a uniform way of allocating RIDs regardless
2317 * of what ever passdb backend people may use.
2318 * --jerry (2003-07-10)
2319 *********************************************************************/
2321 pw = Get_Pwnam(account);
2323 /* determine which user right we need to check based on the acb_info */
2325 if ( acb_info & ACB_WSTRUST )
2327 pstrcpy(add_script, lp_addmachine_script());
2328 se_priv_copy( &se_rights, &se_machine_account );
2329 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2331 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
2332 account for domain trusts and changes the ACB flags later */
2333 else if ( acb_info & ACB_NORMAL && (account[strlen(account)-1] != '$') )
2335 pstrcpy(add_script, lp_adduser_script());
2336 se_priv_copy( &se_rights, &se_add_users );
2337 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2339 else /* implicit assumption of a BDC or domain trust account here (we already check the flags earlier) */
2341 pstrcpy(add_script, lp_addmachine_script());
2342 if ( lp_enable_privileges() ) {
2343 /* only Domain Admins can add a BDC or domain trust */
2344 se_priv_copy( &se_rights, &se_priv_none );
2345 can_add_account = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
2349 DEBUG(5, ("_samr_create_user: %s can add this account : %s\n",
2350 p->pipe_user_name, can_add_account ? "True":"False" ));
2352 /********** BEGIN Admin BLOCK **********/
2354 if ( can_add_account )
2361 all_string_sub(add_script, "%u", account, sizeof(add_script));
2362 add_ret = smbrun(add_script,NULL);
2363 DEBUG(add_ret ? 0 : 3,("_samr_create_user: Running the command `%s' gave %d\n", add_script, add_ret));
2367 /* implicit call to getpwnam() next. we have a valid SID coming out of this call */
2369 flush_pwnam_cache();
2370 nt_status = pdb_init_sam_new(&sam_pass, account, new_rid);
2372 /* this code is order such that we have no unnecessary retuns
2373 out of the admin block of code */
2375 if ( NT_STATUS_IS_OK(nt_status) ) {
2376 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
2378 if ( !(ret = pdb_add_sam_account(sam_pass)) ) {
2379 pdb_free_sam(&sam_pass);
2380 DEBUG(0, ("could not add user/computer %s to passdb. Check permissions?\n",
2382 nt_status = NT_STATUS_ACCESS_DENIED;
2386 if ( can_add_account )
2389 /********** END Admin BLOCK **********/
2391 /* now check for failure */
2393 if ( !NT_STATUS_IS_OK(nt_status) )
2396 /* Get the user's SID */
2398 sid_copy(&sid, pdb_get_user_sid(sam_pass));
2400 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2401 se_map_generic(&des_access, &usr_generic_mapping);
2403 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2404 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2405 &acc_granted, "_samr_create_user");
2407 if ( !NT_STATUS_IS_OK(nt_status) ) {
2411 /* associate the user's SID with the new handle. */
2412 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
2413 pdb_free_sam(&sam_pass);
2414 return NT_STATUS_NO_MEMORY;
2419 info->acc_granted = acc_granted;
2421 /* get a (unique) handle. open a policy on it. */
2422 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
2423 pdb_free_sam(&sam_pass);
2424 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2427 /* After a "set" ensure we have no cached display info. */
2428 force_flush_samr_cache(info->disp_info);
2430 r_u->user_rid=pdb_get_user_rid(sam_pass);
2432 r_u->access_granted = acc_granted;
2434 pdb_free_sam(&sam_pass);
2436 return NT_STATUS_OK;
2439 /*******************************************************************
2440 samr_reply_connect_anon
2441 ********************************************************************/
2443 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2445 struct samr_info *info = NULL;
2446 uint32 des_access = q_u->access_mask;
2450 if (!pipe_access_check(p)) {
2451 DEBUG(3, ("access denied to samr_connect_anon\n"));
2452 r_u->status = NT_STATUS_ACCESS_DENIED;
2456 /* set up the SAMR connect_anon response */
2458 r_u->status = NT_STATUS_OK;
2460 /* associate the user's SID with the new handle. */
2461 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2462 return NT_STATUS_NO_MEMORY;
2464 /* don't give away the farm but this is probably ok. The SA_RIGHT_SAM_ENUM_DOMAINS
2465 was observed from a win98 client trying to enumerate users (when configured
2466 user level access control on shares) --jerry */
2468 se_map_generic( &des_access, &sam_generic_mapping );
2469 info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
2471 info->status = q_u->unknown_0;
2473 /* get a (unique) handle. open a policy on it. */
2474 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2475 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2480 /*******************************************************************
2482 ********************************************************************/
2484 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2486 struct samr_info *info = NULL;
2487 SEC_DESC *psd = NULL;
2489 uint32 des_access = q_u->access_mask;
2494 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2498 if (!pipe_access_check(p)) {
2499 DEBUG(3, ("access denied to samr_connect\n"));
2500 r_u->status = NT_STATUS_ACCESS_DENIED;
2504 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2505 se_map_generic(&des_access, &sam_generic_mapping);
2507 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2508 NULL, 0, des_access, &acc_granted, "_samr_connect");
2510 if ( !NT_STATUS_IS_OK(nt_status) )
2513 r_u->status = NT_STATUS_OK;
2515 /* associate the user's SID and access granted with the new handle. */
2516 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2517 return NT_STATUS_NO_MEMORY;
2519 info->acc_granted = acc_granted;
2520 info->status = q_u->access_mask;
2522 /* get a (unique) handle. open a policy on it. */
2523 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2524 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2526 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2531 /*******************************************************************
2533 ********************************************************************/
2535 NTSTATUS _samr_connect4(pipes_struct *p, SAMR_Q_CONNECT4 *q_u, SAMR_R_CONNECT4 *r_u)
2537 struct samr_info *info = NULL;
2538 SEC_DESC *psd = NULL;
2540 uint32 des_access = q_u->access_mask;
2545 DEBUG(5,("_samr_connect4: %d\n", __LINE__));
2549 if (!pipe_access_check(p)) {
2550 DEBUG(3, ("access denied to samr_connect4\n"));
2551 r_u->status = NT_STATUS_ACCESS_DENIED;
2555 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2556 se_map_generic(&des_access, &sam_generic_mapping);
2558 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2559 NULL, 0, des_access, &acc_granted, "_samr_connect4");
2561 if ( !NT_STATUS_IS_OK(nt_status) )
2564 r_u->status = NT_STATUS_OK;
2566 /* associate the user's SID and access granted with the new handle. */
2567 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2568 return NT_STATUS_NO_MEMORY;
2570 info->acc_granted = acc_granted;
2571 info->status = q_u->access_mask;
2573 /* get a (unique) handle. open a policy on it. */
2574 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2575 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2577 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2582 /*******************************************************************
2584 ********************************************************************/
2586 NTSTATUS _samr_connect5(pipes_struct *p, SAMR_Q_CONNECT5 *q_u, SAMR_R_CONNECT5 *r_u)
2588 struct samr_info *info = NULL;
2589 SEC_DESC *psd = NULL;
2591 uint32 des_access = q_u->access_mask;
2597 DEBUG(5,("_samr_connect5: %d\n", __LINE__));
2603 if (!pipe_access_check(p)) {
2604 DEBUG(3, ("access denied to samr_connect5\n"));
2605 r_u->status = NT_STATUS_ACCESS_DENIED;
2609 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2610 se_map_generic(&des_access, &sam_generic_mapping);
2612 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2613 NULL, 0, des_access, &acc_granted, "_samr_connect5");
2615 if ( !NT_STATUS_IS_OK(nt_status) )
2618 /* associate the user's SID and access granted with the new handle. */
2619 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2620 return NT_STATUS_NO_MEMORY;
2622 info->acc_granted = acc_granted;
2623 info->status = q_u->access_mask;
2625 /* get a (unique) handle. open a policy on it. */
2626 if (!create_policy_hnd(p, &pol, free_samr_info, (void *)info))
2627 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2629 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2631 init_samr_r_connect5(r_u, &pol, NT_STATUS_OK);
2636 /**********************************************************************
2637 api_samr_lookup_domain
2638 **********************************************************************/
2640 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2642 struct samr_info *info;
2643 fstring domain_name;
2646 r_u->status = NT_STATUS_OK;
2648 if (!find_policy_by_hnd(p, &q_u->connect_pol, (void**)(void *)&info))
2649 return NT_STATUS_INVALID_HANDLE;
2651 /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.
2652 Reverted that change so we will work with RAS servers again */
2654 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted,
2655 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_lookup_domain")))
2660 rpcstr_pull(domain_name, q_u->uni_domain.buffer, sizeof(domain_name), q_u->uni_domain.uni_str_len*2, 0);
2664 if (!secrets_fetch_domain_sid(domain_name, &sid)) {
2665 r_u->status = NT_STATUS_NO_SUCH_DOMAIN;
2668 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name, sid_string_static(&sid)));
2670 init_samr_r_lookup_domain(r_u, &sid, r_u->status);
2675 /******************************************************************
2676 makes a SAMR_R_ENUM_DOMAINS structure.
2677 ********************************************************************/
2679 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2680 UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2686 DEBUG(5, ("make_enum_domains\n"));
2689 *pp_uni_name = NULL;
2691 if (num_sam_entries == 0)
2694 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
2695 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
2697 if (sam == NULL || uni_name == NULL)
2700 for (i = 0; i < num_sam_entries; i++) {
2701 init_unistr2(&uni_name[i], doms[i], UNI_FLAGS_NONE);
2702 init_sam_entry(&sam[i], &uni_name[i], 0);
2706 *pp_uni_name = uni_name;
2711 /**********************************************************************
2712 api_samr_enum_domains
2713 **********************************************************************/
2715 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2717 struct samr_info *info;
2718 uint32 num_entries = 2;
2722 r_u->status = NT_STATUS_OK;
2724 if (!find_policy_by_hnd(p, &q_u->pol, (void**)(void *)&info))
2725 return NT_STATUS_INVALID_HANDLE;
2727 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, SA_RIGHT_SAM_ENUM_DOMAINS, "_samr_enum_domains"))) {
2731 name = get_global_sam_name();
2733 fstrcpy(dom[0],name);
2735 fstrcpy(dom[1],"Builtin");
2737 if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2738 return NT_STATUS_NO_MEMORY;
2740 init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2745 /*******************************************************************
2747 ********************************************************************/
2749 NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2752 POLICY_HND domain_pol = q_u->dom_pol;
2753 uint32 alias_rid = q_u->rid_alias;
2754 POLICY_HND *alias_pol = &r_u->pol;
2755 struct samr_info *info = NULL;
2756 SEC_DESC *psd = NULL;
2758 uint32 des_access = q_u->access_mask;
2763 r_u->status = NT_STATUS_OK;
2765 /* find the domain policy and get the SID / access bits stored in the domain policy */
2767 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
2768 return NT_STATUS_INVALID_HANDLE;
2770 status = access_check_samr_function(acc_granted,
2771 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_alias");
2773 if ( !NT_STATUS_IS_OK(status) )
2776 /* append the alias' RID to it */
2778 if (!sid_append_rid(&sid, alias_rid))
2779 return NT_STATUS_NO_SUCH_USER;
2781 /*check if access can be granted as requested by client. */
2783 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
2784 se_map_generic(&des_access,&ali_generic_mapping);
2786 se_priv_copy( &se_rights, &se_add_users );
2789 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2790 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
2791 &acc_granted, "_samr_open_alias");
2793 if ( !NT_STATUS_IS_OK(status) )
2797 * we should check if the rid really exist !!!
2801 /* associate the user's SID with the new handle. */
2802 if ((info = get_samr_info_by_sid(&sid)) == NULL)
2803 return NT_STATUS_NO_MEMORY;
2805 info->acc_granted = acc_granted;
2807 /* get a (unique) handle. open a policy on it. */
2808 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
2809 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2814 /*******************************************************************
2816 ********************************************************************/
2817 static NTSTATUS set_user_info_7(const SAM_USER_INFO_7 *id7, SAM_ACCOUNT *pwd)
2820 SAM_ACCOUNT *check_acct = NULL;
2825 DEBUG(5, ("set_user_info_7: NULL id7\n"));
2827 return NT_STATUS_ACCESS_DENIED;
2830 if(!rpcstr_pull(new_name, id7->uni_name.buffer, sizeof(new_name), id7->uni_name.uni_str_len*2, 0)) {
2831 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
2833 return NT_STATUS_ACCESS_DENIED;
2836 /* check to see if the new username already exists. Note: we can't
2837 reliably lock all backends, so there is potentially the
2838 possibility that a user can be created in between this check and
2839 the rename. The rename should fail, but may not get the
2840 exact same failure status code. I think this is small enough
2841 of a window for this type of operation and the results are
2842 simply that the rename fails with a slightly different status
2843 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
2845 pdb_init_sam(&check_acct);
2846 check_rc = pdb_getsampwnam(check_acct, new_name);
2847 pdb_free_sam(&check_acct);
2849 if (check_rc == True) {
2850 /* this account exists: say so */
2851 return NT_STATUS_USER_EXISTS;
2854 rc = pdb_rename_sam_account(pwd, new_name);
2860 /*******************************************************************
2862 ********************************************************************/
2864 static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, SAM_ACCOUNT *pwd)
2867 DEBUG(5, ("set_user_info_16: NULL id16\n"));
2872 /* FIX ME: check if the value is really changed --metze */
2873 if (!pdb_set_acct_ctrl(pwd, id16->acb_info, PDB_CHANGED)) {
2878 if(!pdb_update_sam_account(pwd)) {
2888 /*******************************************************************
2890 ********************************************************************/
2892 static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, SAM_ACCOUNT *pwd)
2896 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
2901 if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd, PDB_CHANGED)) {
2905 if (!pdb_set_nt_passwd (pwd, id18->nt_pwd, PDB_CHANGED)) {
2909 if (!pdb_set_pass_changed_now (pwd)) {
2914 if(!pdb_update_sam_account(pwd)) {
2923 /*******************************************************************
2924 The GROUPSID field in the SAM_ACCOUNT changed. Try to tell unix.
2925 ********************************************************************/
2926 static BOOL set_unix_primary_group(SAM_ACCOUNT *sampass)
2931 if (!NT_STATUS_IS_OK(sid_to_gid(pdb_get_group_sid(sampass),
2933 DEBUG(2,("Could not get gid for primary group of "
2934 "user %s\n", pdb_get_username(sampass)));
2938 grp = getgrgid(gid);
2941 DEBUG(2,("Could not find primary group %lu for "
2942 "user %s\n", (unsigned long)gid,
2943 pdb_get_username(sampass)));
2947 if (smb_set_primary_group(grp->gr_name,
2948 pdb_get_username(sampass)) != 0) {
2949 DEBUG(2,("Could not set primary group for user %s to "
2951 pdb_get_username(sampass), grp->gr_name));
2959 /*******************************************************************
2961 ********************************************************************/
2963 static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, SAM_ACCOUNT *pwd)
2966 DEBUG(5, ("set_user_info_20: NULL id20\n"));
2970 copy_id20_to_sam_passwd(pwd, id20);
2972 /* write the change out */
2973 if(!pdb_update_sam_account(pwd)) {
2982 /*******************************************************************
2984 ********************************************************************/
2986 static BOOL set_user_info_21(SAM_USER_INFO_21 *id21, SAM_ACCOUNT *pwd)
2990 DEBUG(5, ("set_user_info_21: NULL id21\n"));
2994 copy_id21_to_sam_passwd(pwd, id21);
2997 * The funny part about the previous two calls is
2998 * that pwd still has the password hashes from the
2999 * passdb entry. These have not been updated from
3000 * id21. I don't know if they need to be set. --jerry
3003 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
3004 set_unix_primary_group(pwd);
3006 /* write the change out */
3007 if(!pdb_update_sam_account(pwd)) {
3017 /*******************************************************************
3019 ********************************************************************/
3021 static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, SAM_ACCOUNT *pwd)
3023 pstring plaintext_buf;
3028 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3032 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3033 pdb_get_username(pwd)));
3035 acct_ctrl = pdb_get_acct_ctrl(pwd);
3037 if (!decode_pw_buffer(id23->pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3042 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3047 copy_id23_to_sam_passwd(pwd, id23);
3049 /* if it's a trust account, don't update /etc/passwd */
3050 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3051 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3052 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3053 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3055 /* update the UNIX password */
3056 if (lp_unix_password_sync() ) {
3057 struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
3059 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3062 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3069 ZERO_STRUCT(plaintext_buf);
3071 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
3072 set_unix_primary_group(pwd);
3074 if(!pdb_update_sam_account(pwd)) {
3084 /*******************************************************************
3086 ********************************************************************/
3088 static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
3091 pstring plaintext_buf;
3094 DEBUG(5, ("Attempting administrator password change for user %s\n",
3095 pdb_get_username(pwd)));
3097 acct_ctrl = pdb_get_acct_ctrl(pwd);
3099 ZERO_STRUCT(plaintext_buf);
3101 if (!decode_pw_buffer(pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3106 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3111 /* if it's a trust account, don't update /etc/passwd */
3112 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3113 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
3114 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
3115 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3117 /* update the UNIX password */
3118 if (lp_unix_password_sync()) {
3119 struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
3121 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3124 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3131 ZERO_STRUCT(plaintext_buf);
3133 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
3135 /* update the SAMBA password */
3136 if(!pdb_update_sam_account(pwd)) {
3146 /*******************************************************************
3147 samr_reply_set_userinfo
3148 ********************************************************************/
3150 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
3152 SAM_ACCOUNT *pwd = NULL;
3154 POLICY_HND *pol = &q_u->pol;
3155 uint16 switch_value = q_u->switch_value;
3156 SAM_USERINFO_CTR *ctr = q_u->ctr;
3158 uint32 acc_required;
3160 BOOL has_enough_rights = False;
3162 DISP_INFO *disp_info = NULL;
3164 DEBUG(5, ("_samr_set_userinfo: %d\n", __LINE__));
3166 r_u->status = NT_STATUS_OK;
3168 /* find the policy handle. open a policy on it. */
3169 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3170 return NT_STATUS_INVALID_HANDLE;
3172 /* observed when joining an XP client to a Samba domain */
3174 acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
3176 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo"))) {
3180 DEBUG(5, ("_samr_set_userinfo: sid:%s, level:%d\n", sid_string_static(&sid), switch_value));
3183 DEBUG(5, ("_samr_set_userinfo: NULL info level\n"));
3184 return NT_STATUS_INVALID_INFO_CLASS;
3190 ret = pdb_getsampwsid(pwd, &sid);
3195 return NT_STATUS_NO_SUCH_USER;
3198 /* deal with machine password changes differently from userinfo changes */
3199 /* check to see if we have the sufficient rights */
3201 acb_info = pdb_get_acct_ctrl(pwd);
3202 if ( acb_info & ACB_WSTRUST )
3203 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3204 else if ( acb_info & ACB_NORMAL )
3205 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3206 else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3207 if ( lp_enable_privileges() )
3208 has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3211 DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
3212 p->pipe_user_name, has_enough_rights ? "" : " not"));
3214 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3216 if ( has_enough_rights )
3219 /* ok! user info levels (lots: see MSDEV help), off we go... */
3221 switch (switch_value) {
3223 if (!set_user_info_18(ctr->info.id18, pwd))
3224 r_u->status = NT_STATUS_ACCESS_DENIED;
3228 if (!p->session_key.length) {
3229 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3231 SamOEMhashBlob(ctr->info.id24->pass, 516, &p->session_key);
3233 dump_data(100, (char *)ctr->info.id24->pass, 516);
3235 if (!set_user_info_pw(ctr->info.id24->pass, pwd))
3236 r_u->status = NT_STATUS_ACCESS_DENIED;
3242 * Currently we don't really know how to unmarshall
3243 * the level 25 struct, and the password encryption
3244 * is different. This is a placeholder for when we
3245 * do understand it. In the meantime just return INVALID
3246 * info level and W2K SP2 drops down to level 23... JRA.
3249 if (!p->session_key.length) {
3250 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3252 SamOEMhashBlob(ctr->info.id25->pass, 532, &p->session_key);
3254 dump_data(100, (char *)ctr->info.id25->pass, 532);
3256 if (!set_user_info_pw(ctr->info.id25->pass, &sid))
3257 r_u->status = NT_STATUS_ACCESS_DENIED;
3260 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3264 if (!p->session_key.length) {
3265 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3267 SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
3269 dump_data(100, (char *)ctr->info.id23->pass, 516);
3271 if (!set_user_info_23(ctr->info.id23, pwd))
3272 r_u->status = NT_STATUS_ACCESS_DENIED;
3276 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3280 if ( has_enough_rights )
3283 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3285 if (NT_STATUS_IS_OK(r_u->status)) {
3286 force_flush_samr_cache(disp_info);
3292 /*******************************************************************
3293 samr_reply_set_userinfo2
3294 ********************************************************************/
3296 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
3298 SAM_ACCOUNT *pwd = NULL;
3300 SAM_USERINFO_CTR *ctr = q_u->ctr;
3301 POLICY_HND *pol = &q_u->pol;
3302 uint16 switch_value = q_u->switch_value;
3304 uint32 acc_required;
3306 BOOL has_enough_rights = False;
3308 DISP_INFO *disp_info = NULL;
3310 DEBUG(5, ("samr_reply_set_userinfo2: %d\n", __LINE__));
3312 r_u->status = NT_STATUS_OK;
3314 /* find the policy handle. open a policy on it. */
3315 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3316 return NT_STATUS_INVALID_HANDLE;
3318 /* observed when joining XP client to Samba domain */
3320 acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
3322 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo2"))) {
3326 DEBUG(5, ("samr_reply_set_userinfo2: sid:%s\n", sid_string_static(&sid)));
3329 DEBUG(5, ("samr_reply_set_userinfo2: NULL info level\n"));
3330 return NT_STATUS_INVALID_INFO_CLASS;
3333 switch_value=ctr->switch_value;
3338 ret = pdb_getsampwsid(pwd, &sid);
3343 return NT_STATUS_NO_SUCH_USER;
3346 acb_info = pdb_get_acct_ctrl(pwd);
3347 if ( acb_info & ACB_WSTRUST )
3348 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3349 else if ( acb_info & ACB_NORMAL )
3350 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3351 else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3352 if ( lp_enable_privileges() )
3353 has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3356 DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
3357 p->pipe_user_name, has_enough_rights ? "" : " not"));
3359 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3361 if ( has_enough_rights )
3364 /* ok! user info levels (lots: see MSDEV help), off we go... */
3366 switch (switch_value) {
3368 r_u->status = set_user_info_7(ctr->info.id7, pwd);
3371 if (!set_user_info_16(ctr->info.id16, pwd))
3372 r_u->status = NT_STATUS_ACCESS_DENIED;
3375 /* Used by AS/U JRA. */
3376 if (!set_user_info_18(ctr->info.id18, pwd))
3377 r_u->status = NT_STATUS_ACCESS_DENIED;
3380 if (!set_user_info_20(ctr->info.id20, pwd))
3381 r_u->status = NT_STATUS_ACCESS_DENIED;
3384 if (!set_user_info_21(ctr->info.id21, pwd))
3385 return NT_STATUS_ACCESS_DENIED;
3388 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3391 if ( has_enough_rights )
3394 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3396 if (NT_STATUS_IS_OK(r_u->status)) {
3397 force_flush_samr_cache(disp_info);
3403 /*********************************************************************
3404 _samr_query_aliasmem
3405 *********************************************************************/
3407 NTSTATUS _samr_query_useraliases(pipes_struct *p, SAMR_Q_QUERY_USERALIASES *q_u, SAMR_R_QUERY_USERALIASES *r_u)
3409 size_t num_alias_rids;
3411 struct samr_info *info = NULL;
3420 r_u->status = NT_STATUS_OK;
3422 DEBUG(5,("_samr_query_useraliases: %d\n", __LINE__));
3424 /* find the policy handle. open a policy on it. */
3425 if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
3426 return NT_STATUS_INVALID_HANDLE;
3428 ntstatus1 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM, "_samr_query_useraliases");
3429 ntstatus2 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_query_useraliases");
3431 if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
3432 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
3433 !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
3434 return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
3438 if (!sid_check_is_domain(&info->sid) &&
3439 !sid_check_is_builtin(&info->sid))
3440 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3442 members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, q_u->num_sids1);
3444 if (members == NULL)
3445 return NT_STATUS_NO_MEMORY;
3447 for (i=0; i<q_u->num_sids1; i++)
3448 sid_copy(&members[i], &q_u->sid[i].sid);
3454 res = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
3456 &alias_rids, &num_alias_rids);
3460 return NT_STATUS_UNSUCCESSFUL;
3462 init_samr_r_query_useraliases(r_u, num_alias_rids, alias_rids,
3464 return NT_STATUS_OK;
3467 /*********************************************************************
3468 _samr_query_aliasmem
3469 *********************************************************************/
3471 NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_R_QUERY_ALIASMEM *r_u)
3474 size_t num_sids = 0;
3482 /* find the policy handle. open a policy on it. */
3483 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, NULL))
3484 return NT_STATUS_INVALID_HANDLE;
3486 if (!NT_STATUS_IS_OK(r_u->status =
3487 access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_GET_MEMBERS, "_samr_query_aliasmem"))) {
3491 DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3493 if (!pdb_enum_aliasmem(&alias_sid, &sids, &num_sids))
3494 return NT_STATUS_NO_SUCH_ALIAS;
3496 sid = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_SID2, num_sids);
3497 if (num_sids!=0 && sid == NULL) {
3499 return NT_STATUS_NO_MEMORY;
3502 for (i = 0; i < num_sids; i++) {
3503 init_dom_sid2(&sid[i], &sids[i]);
3506 init_samr_r_query_aliasmem(r_u, num_sids, sid, NT_STATUS_OK);
3510 return NT_STATUS_OK;
3513 static void add_uid_to_array_unique(uid_t uid, uid_t **uids, int *num)
3517 for (i=0; i<*num; i++) {
3518 if ((*uids)[i] == uid)
3522 *uids = SMB_REALLOC_ARRAY(*uids, uid_t, *num+1);
3527 (*uids)[*num] = uid;
3532 static BOOL get_memberuids(gid_t gid, uid_t **uids, int *num)
3536 struct sys_pwent *userlist, *user;
3541 /* We only look at our own sam, so don't care about imported stuff */
3545 if ((grp = getgrgid(gid)) == NULL) {
3550 /* Primary group members */
3552 userlist = getpwent_list();
3554 for (user = userlist; user != NULL; user = user->next) {
3555 if (user->pw_gid != gid)
3557 add_uid_to_array_unique(user->pw_uid, uids, num);
3560 pwent_free(userlist);
3562 /* Secondary group members */
3564 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
3565 struct passwd *pw = getpwnam(*gr);
3569 add_uid_to_array_unique(pw->pw_uid, uids, num);
3577 /*********************************************************************
3578 _samr_query_groupmem
3579 *********************************************************************/
3581 NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_R_QUERY_GROUPMEM *r_u)
3584 fstring group_sid_str;
3585 size_t i, num_members;
3594 /* find the policy handle. open a policy on it. */
3595 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, NULL))
3596 return NT_STATUS_INVALID_HANDLE;
3598 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_GET_MEMBERS, "_samr_query_groupmem"))) {
3602 sid_to_string(group_sid_str, &group_sid);
3603 DEBUG(10, ("sid is %s\n", group_sid_str));
3605 if (!sid_check_is_in_our_domain(&group_sid)) {
3606 DEBUG(3, ("sid %s is not in our domain\n", group_sid_str));
3607 return NT_STATUS_NO_SUCH_GROUP;
3610 DEBUG(10, ("lookup on Domain SID\n"));
3613 result = pdb_enum_group_members(p->mem_ctx, &group_sid,
3614 &rid, &num_members);
3617 if (!NT_STATUS_IS_OK(result))
3620 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
3622 if ((num_members!=0) && (rid==NULL))
3623 return NT_STATUS_NO_MEMORY;
3625 for (i=0; i<num_members; i++)
3626 attr[i] = SID_NAME_USER;
3628 init_samr_r_query_groupmem(r_u, num_members, rid, attr, NT_STATUS_OK);
3630 return NT_STATUS_OK;
3633 /*********************************************************************
3635 *********************************************************************/
3637 NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_ADD_ALIASMEM *r_u)
3642 BOOL can_add_accounts;
3644 DISP_INFO *disp_info = NULL;
3646 /* Find the policy handle. Open a policy on it. */
3647 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info))
3648 return NT_STATUS_INVALID_HANDLE;
3650 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_ADD_MEMBER, "_samr_add_aliasmem"))) {
3654 DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3656 se_priv_copy( &se_rights, &se_add_users );
3657 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3659 /******** BEGIN SeAddUsers BLOCK *********/
3661 if ( can_add_accounts )
3664 ret = pdb_add_aliasmem(&alias_sid, &q_u->sid.sid);
3666 if ( can_add_accounts )
3669 /******** END SeAddUsers BLOCK *********/
3672 force_flush_samr_cache(disp_info);
3675 return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3678 /*********************************************************************
3680 *********************************************************************/
3682 NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DEL_ALIASMEM *r_u)
3687 BOOL can_add_accounts;
3689 DISP_INFO *disp_info = NULL;
3691 /* Find the policy handle. Open a policy on it. */
3692 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info))
3693 return NT_STATUS_INVALID_HANDLE;
3695 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_REMOVE_MEMBER, "_samr_del_aliasmem"))) {
3699 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
3700 sid_string_static(&alias_sid)));
3702 se_priv_copy( &se_rights, &se_add_users );
3703 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3705 /******** BEGIN SeAddUsers BLOCK *********/
3707 if ( can_add_accounts )
3710 ret = pdb_del_aliasmem(&alias_sid, &q_u->sid.sid);
3712 if ( can_add_accounts )
3715 /******** END SeAddUsers BLOCK *********/
3718 force_flush_samr_cache(disp_info);
3721 return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3724 /*********************************************************************
3726 *********************************************************************/
3728 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
3732 fstring group_sid_str;
3739 SAM_ACCOUNT *sam_user=NULL;
3743 BOOL can_add_accounts;
3744 DISP_INFO *disp_info = NULL;
3746 /* Find the policy handle. Open a policy on it. */
3747 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info))
3748 return NT_STATUS_INVALID_HANDLE;
3750 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_ADD_MEMBER, "_samr_add_groupmem"))) {
3754 sid_to_string(group_sid_str, &group_sid);
3755 DEBUG(10, ("sid is %s\n", group_sid_str));
3757 if (sid_compare(&group_sid, get_global_sam_sid())<=0)
3758 return NT_STATUS_NO_SUCH_GROUP;
3760 DEBUG(10, ("lookup on Domain SID\n"));
3762 if(!get_domain_group_from_sid(group_sid, &map))
3763 return NT_STATUS_NO_SUCH_GROUP;
3765 sid_copy(&user_sid, get_global_sam_sid());
3766 sid_append_rid(&user_sid, q_u->rid);
3768 ret = pdb_init_sam(&sam_user);
3769 if (!NT_STATUS_IS_OK(ret))
3772 check = pdb_getsampwsid(sam_user, &user_sid);
3774 if (check != True) {
3775 pdb_free_sam(&sam_user);
3776 return NT_STATUS_NO_SUCH_USER;
3779 /* check a real user exist before we run the script to add a user to a group */
3780 if (!NT_STATUS_IS_OK(sid_to_uid(pdb_get_user_sid(sam_user), &uid))) {
3781 pdb_free_sam(&sam_user);
3782 return NT_STATUS_NO_SUCH_USER;
3785 pdb_free_sam(&sam_user);
3787 if ((pwd=getpwuid_alloc(uid)) == NULL) {
3788 return NT_STATUS_NO_SUCH_USER;
3791 if ((grp=getgrgid(map.gid)) == NULL) {
3793 return NT_STATUS_NO_SUCH_GROUP;
3796 /* we need to copy the name otherwise it's overloaded in user_in_unix_group_list */
3797 fstrcpy(grp_name, grp->gr_name);
3799 /* if the user is already in the group */
3800 if(user_in_unix_group_list(pwd->pw_name, grp_name)) {
3802 return NT_STATUS_MEMBER_IN_GROUP;
3805 se_priv_copy( &se_rights, &se_add_users );
3806 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3808 /******** BEGIN SeAddUsers BLOCK *********/
3810 if ( can_add_accounts )
3814 * ok, the group exist, the user exist, the user is not in the group,
3816 * we can (finally) add it to the group !
3819 smb_add_user_group(grp_name, pwd->pw_name);
3821 if ( can_add_accounts )
3824 /******** END SeAddUsers BLOCK *********/
3826 /* check if the user has been added then ... */
3827 if(!user_in_unix_group_list(pwd->pw_name, grp_name)) {
3829 return NT_STATUS_MEMBER_NOT_IN_GROUP; /* don't know what to reply else */
3834 force_flush_samr_cache(disp_info);
3836 return NT_STATUS_OK;
3839 /*********************************************************************
3841 *********************************************************************/
3843 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
3847 SAM_ACCOUNT *sam_pass=NULL;
3853 BOOL can_add_accounts;
3854 DISP_INFO *disp_info = NULL;
3857 * delete the group member named q_u->rid
3858 * who is a member of the sid associated with the handle
3859 * the rid is a user's rid as the group is a domain group.
3862 /* Find the policy handle. Open a policy on it. */
3863 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info))
3864 return NT_STATUS_INVALID_HANDLE;
3866 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_REMOVE_MEMBER, "_samr_del_groupmem"))) {
3870 if (!sid_check_is_in_our_domain(&group_sid))
3871 return NT_STATUS_NO_SUCH_GROUP;
3873 sid_copy(&user_sid, get_global_sam_sid());
3874 sid_append_rid(&user_sid, q_u->rid);
3876 if (!get_domain_group_from_sid(group_sid, &map))
3877 return NT_STATUS_NO_SUCH_GROUP;
3879 if ((grp=getgrgid(map.gid)) == NULL)
3880 return NT_STATUS_NO_SUCH_GROUP;
3882 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
3883 fstrcpy(grp_name, grp->gr_name);
3885 /* check if the user exists before trying to remove it from the group */
3886 pdb_init_sam(&sam_pass);
3887 if (!pdb_getsampwsid(sam_pass, &user_sid)) {
3888 DEBUG(5,("User %s doesn't exist.\n", pdb_get_username(sam_pass)));
3889 pdb_free_sam(&sam_pass);
3890 return NT_STATUS_NO_SUCH_USER;
3893 /* if the user is not in the group */
3894 if (!user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3895 pdb_free_sam(&sam_pass);
3896 return NT_STATUS_MEMBER_NOT_IN_GROUP;
3900 se_priv_copy( &se_rights, &se_add_users );
3901 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3903 /******** BEGIN SeAddUsers BLOCK *********/
3905 if ( can_add_accounts )
3908 smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
3910 if ( can_add_accounts )
3913 /******** END SeAddUsers BLOCK *********/
3915 /* check if the user has been removed then ... */
3916 if (user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3917 pdb_free_sam(&sam_pass);
3918 return NT_STATUS_ACCESS_DENIED; /* don't know what to reply else */
3921 pdb_free_sam(&sam_pass);
3923 force_flush_samr_cache(disp_info);
3925 return NT_STATUS_OK;
3929 /****************************************************************************
3930 Delete a UNIX user on demand.
3931 ****************************************************************************/
3933 static int smb_delete_user(const char *unix_user)
3938 pstrcpy(del_script, lp_deluser_script());
3941 all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
3942 ret = smbrun(del_script,NULL);
3943 flush_pwnam_cache();
3944 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
3949 /*********************************************************************
3950 _samr_delete_dom_user
3951 *********************************************************************/
3953 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
3956 SAM_ACCOUNT *sam_pass=NULL;
3958 BOOL can_add_accounts;
3960 DISP_INFO *disp_info = NULL;
3962 DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
3964 /* Find the policy handle. Open a policy on it. */
3965 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &user_sid, &acc_granted, &disp_info))
3966 return NT_STATUS_INVALID_HANDLE;
3968 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_user"))) {
3972 if (!sid_check_is_in_our_domain(&user_sid))
3973 return NT_STATUS_CANNOT_DELETE;
3975 /* check if the user exists before trying to delete */
3976 pdb_init_sam(&sam_pass);
3977 if(!pdb_getsampwsid(sam_pass, &user_sid)) {
3978 DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n",
3979 sid_string_static(&user_sid)));
3980 pdb_free_sam(&sam_pass);
3981 return NT_STATUS_NO_SUCH_USER;
3984 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3986 /******** BEGIN SeAddUsers BLOCK *********/
3988 if ( can_add_accounts )
3991 /* First delete the samba side....
3992 code is order to prevent unnecessary returns out of the admin
3995 if ( (ret = pdb_delete_sam_account(sam_pass)) == True ) {
3997 * Now delete the unix side ....
3998 * note: we don't check if the delete really happened
3999 * as the script is not necessary present
4000 * and maybe the sysadmin doesn't want to delete the unix side
4002 smb_delete_user( pdb_get_username(sam_pass) );
4005 if ( can_add_accounts )
4008 /******** END SeAddUsers BLOCK *********/
4011 DEBUG(5,("_samr_delete_dom_user:Failed to delete entry for user %s.\n", pdb_get_username(sam_pass)));
4012 pdb_free_sam(&sam_pass);
4013 return NT_STATUS_CANNOT_DELETE;
4017 pdb_free_sam(&sam_pass);
4019 if (!close_policy_hnd(p, &q_u->user_pol))
4020 return NT_STATUS_OBJECT_NAME_INVALID;
4022 force_flush_samr_cache(disp_info);
4024 return NT_STATUS_OK;
4027 /*********************************************************************
4028 _samr_delete_dom_group
4029 *********************************************************************/
4031 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
4036 fstring group_sid_str;
4042 BOOL can_add_accounts;
4044 DISP_INFO *disp_info = NULL;
4046 DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
4048 /* Find the policy handle. Open a policy on it. */
4049 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, &disp_info))
4050 return NT_STATUS_INVALID_HANDLE;
4052 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_group"))) {
4056 sid_copy(&dom_sid, &group_sid);
4057 sid_to_string(group_sid_str, &dom_sid);
4058 sid_split_rid(&dom_sid, &group_rid);
4060 DEBUG(10, ("sid is %s\n", group_sid_str));
4062 /* we check if it's our SID before deleting */
4063 if (!sid_equal(&dom_sid, get_global_sam_sid()))
4064 return NT_STATUS_NO_SUCH_GROUP;
4066 DEBUG(10, ("lookup on Domain SID\n"));
4068 if(!get_domain_group_from_sid(group_sid, &map))
4069 return NT_STATUS_NO_SUCH_GROUP;
4073 /* check if group really exists */
4074 if ( (grp=getgrgid(gid)) == NULL)
4075 return NT_STATUS_NO_SUCH_GROUP;
4077 se_priv_copy( &se_rights, &se_add_users );
4078 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4080 /******** BEGIN SeAddUsers BLOCK *********/
4082 if ( can_add_accounts )
4085 /* delete mapping first */
4087 if ( (ret = pdb_delete_group_mapping_entry(group_sid)) == True ) {
4088 smb_delete_group( grp->gr_name );
4091 if ( can_add_accounts )
4094 /******** END SeAddUsers BLOCK *********/
4097 DEBUG(5,("_samr_delete_dom_group: Failed to delete mapping entry for group %s.\n",
4099 return NT_STATUS_ACCESS_DENIED;
4102 /* don't check that the unix group has been deleted. Work like
4103 _samr_delet_dom_user() */
4105 if (!close_policy_hnd(p, &q_u->group_pol))
4106 return NT_STATUS_OBJECT_NAME_INVALID;
4108 force_flush_samr_cache(disp_info);
4110 return NT_STATUS_OK;
4113 /*********************************************************************
4114 _samr_delete_dom_alias
4115 *********************************************************************/
4117 NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, SAMR_R_DELETE_DOM_ALIAS *r_u)
4122 BOOL can_add_accounts;
4124 DISP_INFO *disp_info = NULL;
4126 DEBUG(5, ("_samr_delete_dom_alias: %d\n", __LINE__));
4128 /* Find the policy handle. Open a policy on it. */
4129 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info))
4130 return NT_STATUS_INVALID_HANDLE;
4132 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_alias"))) {
4136 DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
4138 if (!sid_check_is_in_our_domain(&alias_sid))
4139 return NT_STATUS_NO_SUCH_ALIAS;
4141 DEBUG(10, ("lookup on Local SID\n"));
4143 se_priv_copy( &se_rights, &se_add_users );
4144 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4146 /******** BEGIN SeAddUsers BLOCK *********/
4148 if ( can_add_accounts )
4151 /* Have passdb delete the alias */
4152 ret = pdb_delete_alias(&alias_sid);
4154 if ( can_add_accounts )
4157 /******** END SeAddUsers BLOCK *********/
4160 return NT_STATUS_ACCESS_DENIED;
4162 if (!close_policy_hnd(p, &q_u->alias_pol))
4163 return NT_STATUS_OBJECT_NAME_INVALID;
4165 force_flush_samr_cache(disp_info);
4167 return NT_STATUS_OK;
4170 /*********************************************************************
4171 _samr_create_dom_group
4172 *********************************************************************/
4174 NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, SAMR_R_CREATE_DOM_GROUP *r_u)
4181 struct samr_info *info;
4185 BOOL can_add_accounts;
4187 DISP_INFO *disp_info = NULL;
4189 /* Find the policy handle. Open a policy on it. */
4190 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &dom_sid, &acc_granted, &disp_info))
4191 return NT_STATUS_INVALID_HANDLE;
4193 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_GROUP, "_samr_create_dom_group"))) {
4197 if (!sid_equal(&dom_sid, get_global_sam_sid()))
4198 return NT_STATUS_ACCESS_DENIED;
4200 unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
4202 /* check if group already exist */
4203 if ((grp=getgrnam(name)) != NULL)
4204 return NT_STATUS_GROUP_EXISTS;
4206 se_priv_copy( &se_rights, &se_add_users );
4207 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4209 /******** BEGIN SeAddUsers BLOCK *********/
4211 if ( can_add_accounts )
4214 /* check that we successfully create the UNIX group */
4216 result = NT_STATUS_ACCESS_DENIED;
4217 if ( (smb_create_group(name, &gid) == 0) && ((grp=getgrgid(gid)) != NULL) ) {
4219 /* so far, so good */
4221 result = NT_STATUS_OK;
4223 r_u->rid = pdb_gid_to_group_rid( grp->gr_gid );
4225 /* add the group to the mapping table */
4227 sid_copy( &info_sid, get_global_sam_sid() );
4228 sid_append_rid( &info_sid, r_u->rid );
4229 sid_to_string( sid_string, &info_sid );
4231 /* reset the error code if we fail to add the mapping entry */
4233 if ( !add_initial_entry(grp->gr_gid, sid_string, SID_NAME_DOM_GRP, name, NULL) )
4234 result = NT_STATUS_ACCESS_DENIED;
4237 if ( can_add_accounts )
4240 /******** END SeAddUsers BLOCK *********/
4242 /* check if we should bail out here */
4244 if ( !NT_STATUS_IS_OK(result) )
4247 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4248 return NT_STATUS_NO_MEMORY;
4251 /* they created it; let the user do what he wants with it */
4253 info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
4255 /* get a (unique) handle. open a policy on it. */
4256 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4257 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4259 force_flush_samr_cache(disp_info);
4261 return NT_STATUS_OK;
4264 /*********************************************************************
4265 _samr_create_dom_alias
4266 *********************************************************************/
4268 NTSTATUS _samr_create_dom_alias(pipes_struct *p, SAMR_Q_CREATE_DOM_ALIAS *q_u, SAMR_R_CREATE_DOM_ALIAS *r_u)
4273 struct samr_info *info;
4278 BOOL can_add_accounts;
4279 DISP_INFO *disp_info = NULL;
4281 /* Find the policy handle. Open a policy on it. */
4282 if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &dom_sid, &acc_granted, &disp_info))
4283 return NT_STATUS_INVALID_HANDLE;
4285 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_ALIAS, "_samr_create_alias"))) {
4289 if (!sid_equal(&dom_sid, get_global_sam_sid()))
4290 return NT_STATUS_ACCESS_DENIED;
4292 unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
4294 se_priv_copy( &se_rights, &se_add_users );
4295 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4297 /******** BEGIN SeAddUsers BLOCK *********/
4299 if ( can_add_accounts )
4302 /* Have passdb create the alias */
4303 result = pdb_create_alias(name, &r_u->rid);
4305 if ( can_add_accounts )
4308 /******** END SeAddUsers BLOCK *********/
4310 if (!NT_STATUS_IS_OK(result))
4313 sid_copy(&info_sid, get_global_sam_sid());
4314 sid_append_rid(&info_sid, r_u->rid);
4316 if (!NT_STATUS_IS_OK(sid_to_gid(&info_sid, &gid)))
4317 return NT_STATUS_ACCESS_DENIED;
4319 /* check if the group has been successfully created */
4320 if ( getgrgid(gid) == NULL )
4321 return NT_STATUS_ACCESS_DENIED;
4323 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4324 return NT_STATUS_NO_MEMORY;
4326 /* they created it; let the user do what he wants with it */
4328 info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
4330 /* get a (unique) handle. open a policy on it. */
4331 if (!create_policy_hnd(p, &r_u->alias_pol, free_samr_info, (void *)info))
4332 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4334 force_flush_samr_cache(disp_info);
4336 return NT_STATUS_OK;
4339 /*********************************************************************
4340 _samr_query_groupinfo
4342 sends the name/comment pair of a domain group
4343 level 1 send also the number of users of that group
4344 *********************************************************************/
4346 NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAMR_R_QUERY_GROUPINFO *r_u)
4353 GROUP_INFO_CTR *ctr;
4357 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, NULL))
4358 return NT_STATUS_INVALID_HANDLE;
4360 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_LOOKUP_INFO, "_samr_query_groupinfo"))) {
4365 ret = get_domain_group_from_sid(group_sid, &map);
4368 return NT_STATUS_INVALID_HANDLE;
4370 ctr=TALLOC_ZERO_P(p->mem_ctx, GROUP_INFO_CTR);
4372 return NT_STATUS_NO_MEMORY;
4374 switch (q_u->switch_level) {
4376 ctr->switch_value1 = 1;
4377 if(!get_memberuids(map.gid, &uids, &num))
4378 return NT_STATUS_NO_SUCH_GROUP;
4380 init_samr_group_info1(&ctr->group.info1, map.nt_name, map.comment, num);
4384 ctr->switch_value1 = 3;
4385 init_samr_group_info3(&ctr->group.info3);
4388 ctr->switch_value1 = 4;
4389 init_samr_group_info4(&ctr->group.info4, map.comment);
4392 return NT_STATUS_INVALID_INFO_CLASS;
4395 init_samr_r_query_groupinfo(r_u, ctr, NT_STATUS_OK);
4397 return NT_STATUS_OK;
4400 /*********************************************************************
4403 update a domain group's comment.
4404 *********************************************************************/
4406 NTSTATUS _samr_set_groupinfo(pipes_struct *p, SAMR_Q_SET_GROUPINFO *q_u, SAMR_R_SET_GROUPINFO *r_u)
4410 GROUP_INFO_CTR *ctr;
4413 BOOL can_mod_accounts;
4414 DISP_INFO *disp_info = NULL;
4416 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info))
4417 return NT_STATUS_INVALID_HANDLE;
4419 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_SET_INFO, "_samr_set_groupinfo"))) {
4423 if (!get_domain_group_from_sid(group_sid, &map))
4424 return NT_STATUS_NO_SUCH_GROUP;
4428 switch (ctr->switch_value1) {
4430 unistr2_to_ascii(map.comment, &(ctr->group.info1.uni_acct_desc), sizeof(map.comment)-1);
4433 unistr2_to_ascii(map.comment, &(ctr->group.info4.uni_acct_desc), sizeof(map.comment)-1);
4436 return NT_STATUS_INVALID_INFO_CLASS;
4439 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4441 /******** BEGIN SeAddUsers BLOCK *********/
4443 if ( can_mod_accounts )
4446 ret = pdb_update_group_mapping_entry(&map);
4448 if ( can_mod_accounts )
4451 /******** End SeAddUsers BLOCK *********/
4454 force_flush_samr_cache(disp_info);
4457 return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4460 /*********************************************************************
4463 update an alias's comment.
4464 *********************************************************************/
4466 NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_SET_ALIASINFO *r_u)
4469 struct acct_info info;
4470 ALIAS_INFO_CTR *ctr;
4473 BOOL can_mod_accounts;
4474 DISP_INFO *disp_info = NULL;
4476 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &group_sid, &acc_granted, &disp_info))
4477 return NT_STATUS_INVALID_HANDLE;
4479 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_SET_INFO, "_samr_set_aliasinfo"))) {
4485 switch (ctr->level) {
4487 if ( ctr->alias.info3.description.string ) {
4488 unistr2_to_ascii( info.acct_desc,
4489 ctr->alias.info3.description.string,
4490 sizeof(info.acct_desc)-1 );
4494 return NT_STATUS_INVALID_INFO_CLASS;
4497 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4499 /******** BEGIN SeAddUsers BLOCK *********/
4501 if ( can_mod_accounts )
4504 ret = pdb_set_aliasinfo( &group_sid, &info );
4506 if ( can_mod_accounts )
4509 /******** End SeAddUsers BLOCK *********/
4512 force_flush_samr_cache(disp_info);
4515 return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4518 /*********************************************************************
4519 _samr_get_dom_pwinfo
4520 *********************************************************************/
4522 NTSTATUS _samr_get_dom_pwinfo(pipes_struct *p, SAMR_Q_GET_DOM_PWINFO *q_u, SAMR_R_GET_DOM_PWINFO *r_u)
4524 /* Perform access check. Since this rpc does not require a
4525 policy handle it will not be caught by the access checks on
4526 SAMR_CONNECT or SAMR_CONNECT_ANON. */
4528 if (!pipe_access_check(p)) {
4529 DEBUG(3, ("access denied to samr_get_dom_pwinfo\n"));
4530 r_u->status = NT_STATUS_ACCESS_DENIED;
4534 /* Actually, returning zeros here works quite well :-). */
4536 return NT_STATUS_OK;
4539 /*********************************************************************
4541 *********************************************************************/
4543 NTSTATUS _samr_open_group(pipes_struct *p, SAMR_Q_OPEN_GROUP *q_u, SAMR_R_OPEN_GROUP *r_u)
4548 struct samr_info *info;
4549 SEC_DESC *psd = NULL;
4551 uint32 des_access = q_u->access_mask;
4558 if (!get_lsa_policy_samr_sid(p, &q_u->domain_pol, &sid, &acc_granted, NULL))
4559 return NT_STATUS_INVALID_HANDLE;
4561 status = access_check_samr_function(acc_granted,
4562 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_group");
4564 if ( !NT_STATUS_IS_OK(status) )
4567 /*check if access can be granted as requested by client. */
4568 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
4569 se_map_generic(&des_access,&grp_generic_mapping);
4571 se_priv_copy( &se_rights, &se_add_users );
4573 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
4574 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access,
4575 &acc_granted, "_samr_open_group");
4577 if ( !NT_STATUS_IS_OK(status) )
4580 /* this should not be hard-coded like this */
4582 if (!sid_equal(&sid, get_global_sam_sid()))
4583 return NT_STATUS_ACCESS_DENIED;
4585 sid_copy(&info_sid, get_global_sam_sid());
4586 sid_append_rid(&info_sid, q_u->rid_group);
4587 sid_to_string(sid_string, &info_sid);
4589 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4590 return NT_STATUS_NO_MEMORY;
4592 info->acc_granted = acc_granted;
4594 DEBUG(10, ("_samr_open_group:Opening SID: %s\n", sid_string));
4596 /* check if that group really exists */
4598 ret = get_domain_group_from_sid(info->sid, &map);
4601 return NT_STATUS_NO_SUCH_GROUP;
4603 /* get a (unique) handle. open a policy on it. */
4604 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4605 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4607 return NT_STATUS_OK;
4610 /*********************************************************************
4611 _samr_remove_sid_foreign_domain
4612 *********************************************************************/
4614 NTSTATUS _samr_remove_sid_foreign_domain(pipes_struct *p,
4615 SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN *q_u,
4616 SAMR_R_REMOVE_SID_FOREIGN_DOMAIN *r_u)
4618 DOM_SID delete_sid, domain_sid;
4621 DISP_INFO *disp_info = NULL;
4623 sid_copy( &delete_sid, &q_u->sid.sid );
4625 DEBUG(5,("_samr_remove_sid_foreign_domain: removing SID [%s]\n",
4626 sid_string_static(&delete_sid)));
4628 /* Find the policy handle. Open a policy on it. */
4630 if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &domain_sid,
4631 &acc_granted, &disp_info))
4632 return NT_STATUS_INVALID_HANDLE;
4634 result = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS,
4635 "_samr_remove_sid_foreign_domain");
4637 if (!NT_STATUS_IS_OK(result))
4640 DEBUG(8, ("_samr_remove_sid_foreign_domain:sid is %s\n",
4641 sid_string_static(&domain_sid)));
4643 /* we can only delete a user from a group since we don't have
4644 nested groups anyways. So in the latter case, just say OK */
4646 /* TODO: The above comment nowadays is bogus. Since we have nested
4647 * groups now, and aliases members are never reported out of the unix
4648 * group membership, the "just say OK" makes this call a no-op. For
4649 * us. This needs fixing however. */
4651 /* I've only ever seen this in the wild when deleting a user from
4652 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
4653 * is the user about to be deleted. I very much suspect this is the
4654 * only application of this call. To verify this, let people report
4657 if (!sid_check_is_builtin(&domain_sid)) {
4658 DEBUG(1,("_samr_remove_sid_foreign_domain: domain_sid = %s, "
4659 "global_sam_sid() = %s\n",
4660 sid_string_static(&domain_sid),
4661 sid_string_static(get_global_sam_sid())));
4662 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
4663 return NT_STATUS_OK;
4666 force_flush_samr_cache(disp_info);
4668 result = NT_STATUS_OK;
4673 /*******************************************************************
4674 _samr_query_domain_info2
4675 ********************************************************************/
4677 NTSTATUS _samr_query_domain_info2(pipes_struct *p,
4678 SAMR_Q_QUERY_DOMAIN_INFO2 *q_u,
4679 SAMR_R_QUERY_DOMAIN_INFO2 *r_u)
4681 struct samr_info *info = NULL;
4683 uint32 min_pass_len,pass_hist,flag;
4684 time_t u_expire, u_min_age;
4685 NTTIME nt_expire, nt_min_age;
4687 time_t u_lock_duration, u_reset_time;
4688 NTTIME nt_lock_duration, nt_reset_time;
4694 uint32 num_users=0, num_groups=0, num_aliases=0;
4696 uint32 account_policy_temp;
4701 if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL)
4702 return NT_STATUS_NO_MEMORY;
4706 r_u->status = NT_STATUS_OK;
4708 DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
4710 /* find the policy handle. open a policy on it. */
4711 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info))
4712 return NT_STATUS_INVALID_HANDLE;
4714 switch (q_u->switch_value) {
4716 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
4717 min_pass_len = account_policy_temp;
4719 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
4720 pass_hist = account_policy_temp;
4722 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
4723 flag = account_policy_temp;
4725 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
4726 u_expire = account_policy_temp;
4728 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
4729 u_min_age = account_policy_temp;
4731 unix_to_nt_time_abs(&nt_expire, u_expire);
4732 unix_to_nt_time_abs(&nt_min_age, u_min_age);
4734 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
4735 flag, nt_expire, nt_min_age);
4739 num_users = count_sam_users(info->disp_info, ACB_NORMAL);
4740 num_groups = count_sam_groups(info->disp_info);
4741 num_aliases = count_sam_aliases(info->disp_info);
4744 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4745 u_logout = account_policy_temp;
4747 unix_to_nt_time_abs(&nt_logout, u_logout);
4749 if (!pdb_get_seq_num(&seq_num))
4750 seq_num = time(NULL);
4752 server_role = ROLE_DOMAIN_PDC;
4753 if (lp_server_role() == ROLE_DOMAIN_BDC)
4754 server_role = ROLE_DOMAIN_BDC;
4756 init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num,
4757 num_users, num_groups, num_aliases, nt_logout, server_role);
4760 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4761 u_logout = account_policy_temp;
4763 unix_to_nt_time_abs(&nt_logout, u_logout);
4765 init_unk_info3(&ctr->info.inf3, nt_logout);
4768 init_unk_info5(&ctr->info.inf5, global_myname());
4771 init_unk_info6(&ctr->info.inf6);
4774 server_role = ROLE_DOMAIN_PDC;
4775 if (lp_server_role() == ROLE_DOMAIN_BDC)
4776 server_role = ROLE_DOMAIN_BDC;
4778 init_unk_info7(&ctr->info.inf7, server_role);
4781 if (!pdb_get_seq_num(&seq_num))
4782 seq_num = time(NULL);
4784 init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
4787 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
4788 u_lock_duration = account_policy_temp;
4789 if (u_lock_duration != -1)
4790 u_lock_duration *= 60;
4792 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
4793 u_reset_time = account_policy_temp * 60;
4795 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
4796 lockout = account_policy_temp;
4798 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
4799 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
4801 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
4804 return NT_STATUS_INVALID_INFO_CLASS;
4807 init_samr_r_samr_query_domain_info2(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
4809 DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
4814 /*******************************************************************
4816 ********************************************************************/
4818 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)
4820 time_t u_expire, u_min_age;
4822 time_t u_lock_duration, u_reset_time;
4824 r_u->status = NT_STATUS_OK;
4826 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4828 /* find the policy handle. open a policy on it. */
4829 if (!find_policy_by_hnd(p, &q_u->domain_pol, NULL))
4830 return NT_STATUS_INVALID_HANDLE;
4832 DEBUG(5,("_samr_set_dom_info: switch_value: %d\n", q_u->switch_value));
4834 switch (q_u->switch_value) {
4836 u_expire=nt_time_to_unix_abs(&q_u->ctr->info.inf1.expire);
4837 u_min_age=nt_time_to_unix_abs(&q_u->ctr->info.inf1.min_passwordage);
4839 pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)q_u->ctr->info.inf1.min_length_password);
4840 pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)q_u->ctr->info.inf1.password_history);
4841 pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)q_u->ctr->info.inf1.password_properties);
4842 pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
4843 pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
4848 u_logout=nt_time_to_unix_abs(&q_u->ctr->info.inf3.logout);
4849 pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
4858 u_lock_duration=nt_time_to_unix_abs(&q_u->ctr->info.inf12.duration);
4859 if (u_lock_duration != -1)
4860 u_lock_duration /= 60;
4862 u_reset_time=nt_time_to_unix_abs(&q_u->ctr->info.inf12.reset_count)/60;
4864 pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
4865 pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
4866 pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)q_u->ctr->info.inf12.bad_attempt_lockout);
4869 return NT_STATUS_INVALID_INFO_CLASS;
4872 init_samr_r_set_domain_info(r_u, NT_STATUS_OK);
4874 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));