r11236: Implement user rename for smbpasswd and ldap backends. Some cleanup on
[Samba.git] / source3 / rpc_server / srv_samr_nt.c
blob598f9db1dad3eedceacbe94572b58a1a8a9358c8
1 /*
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-2002,
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.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 * This is the implementation of the SAMR code.
33 #include "includes.h"
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_RPC_SRV
38 #define SAMR_USR_RIGHTS_WRITE_PW \
39 ( READ_CONTROL_ACCESS | \
40 SA_RIGHT_USER_CHANGE_PASSWORD | \
41 SA_RIGHT_USER_SET_LOC_COM )
43 extern rid_name domain_group_rids[];
44 extern rid_name domain_alias_rids[];
45 extern rid_name builtin_alias_rids[];
47 typedef struct disp_info {
48 struct pdb_search *users; /* querydispinfo 1 and 4 */
49 struct pdb_search *machines; /* querydispinfo 2 */
50 struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
51 struct pdb_search *aliases; /* enumaliases */
52 struct pdb_search *builtins; /* enumaliases */
54 uint16 enum_acb_mask;
55 struct pdb_search *enum_users; /* enumusers with a mask */
56 } DISP_INFO;
58 struct samr_info {
59 /* for use by the \PIPE\samr policy */
60 DOM_SID sid;
61 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
62 uint32 acc_granted;
63 uint16 acb_mask;
64 BOOL only_machines;
65 DISP_INFO disp_info;
67 TALLOC_CTX *mem_ctx;
70 struct generic_mapping sam_generic_mapping = {GENERIC_RIGHTS_SAM_READ, GENERIC_RIGHTS_SAM_WRITE, GENERIC_RIGHTS_SAM_EXECUTE, GENERIC_RIGHTS_SAM_ALL_ACCESS};
71 struct generic_mapping dom_generic_mapping = {GENERIC_RIGHTS_DOMAIN_READ, GENERIC_RIGHTS_DOMAIN_WRITE, GENERIC_RIGHTS_DOMAIN_EXECUTE, GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
72 struct generic_mapping usr_generic_mapping = {GENERIC_RIGHTS_USER_READ, GENERIC_RIGHTS_USER_WRITE, GENERIC_RIGHTS_USER_EXECUTE, GENERIC_RIGHTS_USER_ALL_ACCESS};
73 struct generic_mapping grp_generic_mapping = {GENERIC_RIGHTS_GROUP_READ, GENERIC_RIGHTS_GROUP_WRITE, GENERIC_RIGHTS_GROUP_EXECUTE, GENERIC_RIGHTS_GROUP_ALL_ACCESS};
74 struct generic_mapping ali_generic_mapping = {GENERIC_RIGHTS_ALIAS_READ, GENERIC_RIGHTS_ALIAS_WRITE, GENERIC_RIGHTS_ALIAS_EXECUTE, GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
76 /*******************************************************************
77 *******************************************************************/
79 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
80 struct generic_mapping *map,
81 DOM_SID *sid, uint32 sid_access )
83 DOM_SID domadmin_sid;
84 SEC_ACE ace[5]; /* at most 5 entries */
85 SEC_ACCESS mask;
86 size_t i = 0;
88 SEC_ACL *psa = NULL;
90 /* basic access for Everyone */
92 init_sec_access(&mask, map->generic_execute | map->generic_read );
93 init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
95 /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
97 init_sec_access(&mask, map->generic_all);
99 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
100 init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
102 /* Add Full Access for Domain Admins if we are a DC */
104 if ( IS_DC ) {
105 sid_copy( &domadmin_sid, get_global_sam_sid() );
106 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
107 init_sec_ace(&ace[i++], &domadmin_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
110 /* if we have a sid, give it some special access */
112 if ( sid ) {
113 init_sec_access( &mask, sid_access );
114 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
117 /* create the security descriptor */
119 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
120 return NT_STATUS_NO_MEMORY;
122 if ((*psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, sd_size)) == NULL)
123 return NT_STATUS_NO_MEMORY;
125 return NT_STATUS_OK;
128 /*******************************************************************
129 Checks if access to an object should be granted, and returns that
130 level of access for further checks.
131 ********************************************************************/
133 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token,
134 SE_PRIV *rights, uint32 rights_mask,
135 uint32 des_access, uint32 *acc_granted,
136 const char *debug )
138 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
139 uint32 saved_mask = 0;
141 /* check privileges; certain SAM access bits should be overridden
142 by privileges (mostly having to do with creating/modifying/deleting
143 users and groups) */
145 if ( rights && user_has_any_privilege( token, rights ) ) {
147 saved_mask = (des_access & rights_mask);
148 des_access &= ~saved_mask;
150 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
151 rights_mask));
155 /* check the security descriptor first */
157 if ( se_access_check(psd, token, des_access, acc_granted, &status) )
158 goto done;
160 /* give root a free pass */
162 if ( geteuid() == sec_initial_uid() ) {
164 DEBUG(4,("%s: ACCESS should be DENIED (requested: %#010x)\n", debug, des_access));
165 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
167 *acc_granted = des_access;
169 status = NT_STATUS_OK;
170 goto done;
174 done:
175 /* add in any bits saved during the privilege check (only
176 matters is status is ok) */
178 *acc_granted |= rights_mask;
180 DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n",
181 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED",
182 des_access, *acc_granted));
184 return status;
187 /*******************************************************************
188 Checks if access to a function can be granted
189 ********************************************************************/
191 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
193 DEBUG(5,("%s: access check ((granted: %#010x; required: %#010x)\n",
194 debug, acc_granted, acc_required));
196 /* check the security descriptor first */
198 if ( (acc_granted&acc_required) == acc_required )
199 return NT_STATUS_OK;
201 /* give root a free pass */
203 if (geteuid() == sec_initial_uid()) {
205 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
206 debug, acc_granted, acc_required));
207 DEBUGADD(4,("but overwritten by euid == 0\n"));
209 return NT_STATUS_OK;
212 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: %#010x)\n",
213 debug, acc_granted, acc_required));
215 return NT_STATUS_ACCESS_DENIED;
219 /*******************************************************************
220 Create a samr_info struct.
221 ********************************************************************/
223 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
225 struct samr_info *info;
226 fstring sid_str;
227 TALLOC_CTX *mem_ctx;
229 if (psid) {
230 sid_to_string(sid_str, psid);
231 } else {
232 fstrcpy(sid_str,"(NULL)");
235 mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
237 if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
238 return NULL;
240 DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
241 if (psid) {
242 sid_copy( &info->sid, psid);
243 } else {
244 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
246 info->mem_ctx = mem_ctx;
247 return info;
250 /*******************************************************************
251 Function to free the per handle data.
252 ********************************************************************/
254 /*******************************************************************
255 Function to free the per handle data.
256 ********************************************************************/
258 static void free_samr_db(struct samr_info *info)
260 pdb_search_destroy(info->disp_info.users);
261 info->disp_info.users = NULL;
262 pdb_search_destroy(info->disp_info.machines);
263 info->disp_info.machines = NULL;
264 pdb_search_destroy(info->disp_info.groups);
265 info->disp_info.groups = NULL;
266 pdb_search_destroy(info->disp_info.aliases);
267 info->disp_info.aliases = NULL;
268 pdb_search_destroy(info->disp_info.builtins);
269 info->disp_info.builtins = NULL;
270 pdb_search_destroy(info->disp_info.enum_users);
271 info->disp_info.enum_users = NULL;
274 static void free_samr_info(void *ptr)
276 struct samr_info *info=(struct samr_info *) ptr;
278 free_samr_db(info);
279 talloc_destroy(info->mem_ctx);
282 /*******************************************************************
283 Ensure password info is never given out. Paranioa... JRA.
284 ********************************************************************/
286 static void samr_clear_sam_passwd(SAM_ACCOUNT *sam_pass)
289 if (!sam_pass)
290 return;
292 /* These now zero out the old password */
294 pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
295 pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
298 static uint32 count_sam_users(struct disp_info *info, uint16 acct_flags)
300 struct samr_displayentry *entry;
301 if (info->users == NULL)
302 info->users = pdb_search_users(acct_flags);
303 if (info->users == NULL)
304 return 0;
305 /* Fetch the last possible entry, thus trigger an enumeration */
306 pdb_search_entries(info->users, 0xffffffff, 1, &entry);
307 return info->users->num_entries;
310 static uint32 count_sam_groups(struct disp_info *info)
312 struct samr_displayentry *entry;
313 if (info->groups == NULL)
314 info->groups = pdb_search_groups();
315 if (info->groups == NULL)
316 return 0;
317 /* Fetch the last possible entry, thus trigger an enumeration */
318 pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
319 return info->groups->num_entries;
322 /*******************************************************************
323 _samr_close_hnd
324 ********************************************************************/
326 NTSTATUS _samr_close_hnd(pipes_struct *p, SAMR_Q_CLOSE_HND *q_u, SAMR_R_CLOSE_HND *r_u)
328 r_u->status = NT_STATUS_OK;
330 /* close the policy handle */
331 if (!close_policy_hnd(p, &q_u->pol))
332 return NT_STATUS_OBJECT_NAME_INVALID;
334 DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
336 return r_u->status;
339 /*******************************************************************
340 samr_reply_open_domain
341 ********************************************************************/
343 NTSTATUS _samr_open_domain(pipes_struct *p, SAMR_Q_OPEN_DOMAIN *q_u, SAMR_R_OPEN_DOMAIN *r_u)
345 struct samr_info *info;
346 SEC_DESC *psd = NULL;
347 uint32 acc_granted;
348 uint32 des_access = q_u->flags;
349 NTSTATUS status;
350 size_t sd_size;
351 SE_PRIV se_rights;
353 r_u->status = NT_STATUS_OK;
355 /* find the connection policy handle. */
357 if ( !find_policy_by_hnd(p, &q_u->pol, (void**)&info) )
358 return NT_STATUS_INVALID_HANDLE;
360 status = access_check_samr_function( info->acc_granted,
361 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_open_domain" );
363 if ( !NT_STATUS_IS_OK(status) )
364 return status;
366 /*check if access can be granted as requested by client. */
368 make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
369 se_map_generic( &des_access, &dom_generic_mapping );
371 se_priv_copy( &se_rights, &se_machine_account );
372 se_priv_add( &se_rights, &se_add_users );
374 status = access_check_samr_object( psd, p->pipe_user.nt_user_token,
375 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access,
376 &acc_granted, "_samr_open_domain" );
378 if ( !NT_STATUS_IS_OK(status) )
379 return status;
381 /* associate the domain SID with the (unique) handle. */
382 if ((info = get_samr_info_by_sid(&q_u->dom_sid.sid))==NULL)
383 return NT_STATUS_NO_MEMORY;
384 info->acc_granted = acc_granted;
386 /* get a (unique) handle. open a policy on it. */
387 if (!create_policy_hnd(p, &r_u->domain_pol, free_samr_info, (void *)info))
388 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
390 DEBUG(5,("samr_open_domain: %d\n", __LINE__));
392 return r_u->status;
395 /*******************************************************************
396 _samr_get_usrdom_pwinfo
397 ********************************************************************/
399 NTSTATUS _samr_get_usrdom_pwinfo(pipes_struct *p, SAMR_Q_GET_USRDOM_PWINFO *q_u, SAMR_R_GET_USRDOM_PWINFO *r_u)
401 struct samr_info *info = NULL;
403 r_u->status = NT_STATUS_OK;
405 /* find the policy handle. open a policy on it. */
406 if (!find_policy_by_hnd(p, &q_u->user_pol, (void **)&info))
407 return NT_STATUS_INVALID_HANDLE;
409 if (!sid_check_is_in_our_domain(&info->sid))
410 return NT_STATUS_OBJECT_TYPE_MISMATCH;
412 init_samr_r_get_usrdom_pwinfo(r_u, NT_STATUS_OK);
414 DEBUG(5,("_samr_get_usrdom_pwinfo: %d\n", __LINE__));
417 * NT sometimes return NT_STATUS_ACCESS_DENIED
418 * I don't know yet why.
421 return r_u->status;
424 /*******************************************************************
425 _samr_set_sec_obj
426 ********************************************************************/
428 NTSTATUS _samr_set_sec_obj(pipes_struct *p, SAMR_Q_SET_SEC_OBJ *q_u, SAMR_R_SET_SEC_OBJ *r_u)
430 DEBUG(0,("_samr_set_sec_obj: Not yet implemented!\n"));
431 return NT_STATUS_NOT_IMPLEMENTED;
435 /*******************************************************************
436 ********************************************************************/
438 static BOOL get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol,
439 DOM_SID *sid, uint32 *acc_granted)
441 struct samr_info *info = NULL;
443 /* find the policy handle. open a policy on it. */
444 if (!find_policy_by_hnd(p, pol, (void **)&info))
445 return False;
447 if (!info)
448 return False;
450 *sid = info->sid;
451 *acc_granted = info->acc_granted;
452 return True;
455 /*******************************************************************
456 _samr_query_sec_obj
457 ********************************************************************/
459 NTSTATUS _samr_query_sec_obj(pipes_struct *p, SAMR_Q_QUERY_SEC_OBJ *q_u, SAMR_R_QUERY_SEC_OBJ *r_u)
461 DOM_SID pol_sid;
462 fstring str_sid;
463 SEC_DESC * psd = NULL;
464 uint32 acc_granted;
465 size_t sd_size;
467 r_u->status = NT_STATUS_OK;
469 /* Get the SID. */
470 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &pol_sid, &acc_granted))
471 return NT_STATUS_INVALID_HANDLE;
475 DEBUG(10,("_samr_query_sec_obj: querying security on SID: %s\n", sid_to_string(str_sid, &pol_sid)));
477 /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
479 /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
480 if (pol_sid.sid_rev_num == 0)
482 DEBUG(5,("_samr_query_sec_obj: querying security on SAM\n"));
483 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
485 else if (sid_equal(&pol_sid,get_global_sam_sid())) /* check if it is our domain SID */
488 DEBUG(5,("_samr_query_sec_obj: querying security on Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
489 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
491 else if (sid_equal(&pol_sid,&global_sid_Builtin)) /* check if it is the Builtin Domain */
493 /* TODO: Builtin probably needs a different SD with restricted write access*/
494 DEBUG(5,("_samr_query_sec_obj: querying security on Builtin Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
495 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
497 else if (sid_check_is_in_our_domain(&pol_sid) ||
498 sid_check_is_in_builtin(&pol_sid))
500 /* TODO: different SDs have to be generated for aliases groups and users.
501 Currently all three get a default user SD */
502 DEBUG(10,("_samr_query_sec_obj: querying security on Object with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
503 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
505 else return NT_STATUS_OBJECT_TYPE_MISMATCH;
507 if ((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
508 return NT_STATUS_NO_MEMORY;
510 if (NT_STATUS_IS_OK(r_u->status))
511 r_u->ptr = 1;
513 return r_u->status;
516 /*******************************************************************
517 makes a SAM_ENTRY / UNISTR2* structure from a user list.
518 ********************************************************************/
520 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
521 UNISTR2 **uni_name_pp,
522 uint32 num_entries, uint32 start_idx,
523 struct samr_displayentry *entries)
525 uint32 i;
526 SAM_ENTRY *sam;
527 UNISTR2 *uni_name;
529 *sam_pp = NULL;
530 *uni_name_pp = NULL;
532 if (num_entries == 0)
533 return NT_STATUS_OK;
535 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_entries);
537 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_entries);
539 if (sam == NULL || uni_name == NULL) {
540 DEBUG(0, ("make_user_sam_entry_list: talloc_zero failed!\n"));
541 return NT_STATUS_NO_MEMORY;
544 for (i = 0; i < num_entries; i++) {
545 UNISTR2 uni_temp_name;
547 * usrmgr expects a non-NULL terminated string with
548 * trust relationships
550 if (entries[i].acct_flags & ACB_DOMTRUST) {
551 init_unistr2(&uni_temp_name, entries[i].account_name,
552 UNI_FLAGS_NONE);
553 } else {
554 init_unistr2(&uni_temp_name, entries[i].account_name,
555 UNI_STR_TERMINATE);
558 init_sam_entry(&sam[i], &uni_temp_name, entries[i].rid);
559 copy_unistr2(&uni_name[i], &uni_temp_name);
562 *sam_pp = sam;
563 *uni_name_pp = uni_name;
564 return NT_STATUS_OK;
567 /*******************************************************************
568 samr_reply_enum_dom_users
569 ********************************************************************/
571 NTSTATUS _samr_enum_dom_users(pipes_struct *p, SAMR_Q_ENUM_DOM_USERS *q_u,
572 SAMR_R_ENUM_DOM_USERS *r_u)
574 struct samr_info *info = NULL;
575 int num_account;
576 uint32 enum_context=q_u->start_idx;
577 enum remote_arch_types ra_type = get_remote_arch();
578 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
579 uint32 max_entries = max_sam_entries;
580 struct samr_displayentry *entries = NULL;
582 r_u->status = NT_STATUS_OK;
584 /* find the policy handle. open a policy on it. */
585 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
586 return NT_STATUS_INVALID_HANDLE;
588 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted,
589 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
590 "_samr_enum_dom_users"))) {
591 return r_u->status;
594 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
596 become_root();
597 if ((info->disp_info.enum_users != NULL) &&
598 (info->disp_info.enum_acb_mask != q_u->acb_mask)) {
599 pdb_search_destroy(info->disp_info.enum_users);
600 info->disp_info.enum_users = NULL;
603 if (info->disp_info.enum_users == NULL) {
604 info->disp_info.enum_users = pdb_search_users(q_u->acb_mask);
605 info->disp_info.enum_acb_mask = q_u->acb_mask;
607 if (info->disp_info.enum_users == NULL)
608 return NT_STATUS_ACCESS_DENIED;
609 num_account = pdb_search_entries(info->disp_info.enum_users,
610 enum_context, max_entries,
611 &entries);
612 unbecome_root();
614 if (num_account == 0) {
615 DEBUG(5, ("_samr_enum_dom_users: enumeration handle over "
616 "total entries\n"));
617 return NT_STATUS_OK;
620 r_u->status = make_user_sam_entry_list(p->mem_ctx, &r_u->sam,
621 &r_u->uni_acct_name,
622 num_account, enum_context,
623 entries);
625 if (!NT_STATUS_IS_OK(r_u->status))
626 return r_u->status;
628 if (max_entries <= num_account)
629 r_u->status = STATUS_MORE_ENTRIES;
631 DEBUG(5, ("_samr_enum_dom_users: %d\n", __LINE__));
633 init_samr_r_enum_dom_users(r_u, q_u->start_idx + num_account,
634 num_account);
636 DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
638 return r_u->status;
641 /*******************************************************************
642 makes a SAM_ENTRY / UNISTR2* structure from a group list.
643 ********************************************************************/
645 static void make_group_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
646 UNISTR2 **uni_name_pp,
647 uint32 num_sam_entries,
648 struct samr_displayentry *entries)
650 uint32 i;
651 SAM_ENTRY *sam;
652 UNISTR2 *uni_name;
654 *sam_pp = NULL;
655 *uni_name_pp = NULL;
657 if (num_sam_entries == 0)
658 return;
660 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
661 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
663 if (sam == NULL || uni_name == NULL) {
664 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
665 return;
668 for (i = 0; i < num_sam_entries; i++) {
670 * JRA. I think this should include the null. TNG does not.
672 init_unistr2(&uni_name[i], entries[i].account_name,
673 UNI_STR_TERMINATE);
674 init_sam_entry(&sam[i], &uni_name[i], entries[i].rid);
677 *sam_pp = sam;
678 *uni_name_pp = uni_name;
681 /*******************************************************************
682 samr_reply_enum_dom_groups
683 ********************************************************************/
685 NTSTATUS _samr_enum_dom_groups(pipes_struct *p, SAMR_Q_ENUM_DOM_GROUPS *q_u, SAMR_R_ENUM_DOM_GROUPS *r_u)
687 struct samr_info *info = NULL;
688 struct samr_displayentry *groups;
689 uint32 num_groups;
691 r_u->status = NT_STATUS_OK;
693 /* find the policy handle. open a policy on it. */
694 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
695 return NT_STATUS_INVALID_HANDLE;
697 r_u->status = access_check_samr_function(info->acc_granted,
698 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
699 "_samr_enum_dom_groups");
700 if (!NT_STATUS_IS_OK(r_u->status))
701 return r_u->status;
703 DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
705 /* the domain group array is being allocated in the function below */
707 become_root();
708 if (info->disp_info.groups == NULL)
709 info->disp_info.groups = pdb_search_groups();
710 unbecome_root();
712 if (info->disp_info.groups == NULL)
713 return NT_STATUS_ACCESS_DENIED;
715 become_root();
716 num_groups = pdb_search_entries(info->disp_info.groups, q_u->start_idx,
717 MAX_SAM_ENTRIES, &groups);
718 unbecome_root();
720 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
721 num_groups, groups);
723 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, num_groups);
725 DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
727 return r_u->status;
730 /*******************************************************************
731 samr_reply_enum_dom_aliases
732 ********************************************************************/
734 NTSTATUS _samr_enum_dom_aliases(pipes_struct *p, SAMR_Q_ENUM_DOM_ALIASES *q_u, SAMR_R_ENUM_DOM_ALIASES *r_u)
736 struct samr_info *info;
737 struct samr_displayentry *aliases;
738 struct pdb_search **search = NULL;
739 uint32 num_aliases = 0;
741 /* find the policy handle. open a policy on it. */
742 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
743 return NT_STATUS_INVALID_HANDLE;
745 r_u->status = access_check_samr_function(info->acc_granted,
746 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
747 "_samr_enum_dom_aliases");
748 if (!NT_STATUS_IS_OK(r_u->status))
749 return r_u->status;
751 DEBUG(5,("samr_reply_enum_dom_aliases: sid %s\n",
752 sid_string_static(&info->sid)));
754 if (sid_check_is_domain(&info->sid))
755 search = &info->disp_info.aliases;
756 if (sid_check_is_builtin(&info->sid))
757 search = &info->disp_info.builtins;
759 if (search == NULL)
760 return NT_STATUS_INVALID_HANDLE;
762 become_root();
763 if (*search == NULL)
764 *search = pdb_search_aliases(&info->sid);
765 unbecome_root();
767 if (*search == NULL)
768 return NT_STATUS_ACCESS_DENIED;
770 become_root();
771 num_aliases = pdb_search_entries(*search, q_u->start_idx,
772 MAX_SAM_ENTRIES, &aliases);
773 unbecome_root();
775 make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
776 num_aliases, aliases);
778 init_samr_r_enum_dom_aliases(r_u, q_u->start_idx + num_aliases,
779 num_aliases);
781 DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
783 return r_u->status;
786 /*******************************************************************
787 samr_reply_query_dispinfo
788 ********************************************************************/
790 NTSTATUS _samr_query_dispinfo(pipes_struct *p, SAMR_Q_QUERY_DISPINFO *q_u,
791 SAMR_R_QUERY_DISPINFO *r_u)
793 struct samr_info *info = NULL;
794 uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
796 uint32 max_entries=q_u->max_entries;
797 uint32 enum_context=q_u->start_idx;
798 uint32 max_size=q_u->max_size;
800 SAM_DISPINFO_CTR *ctr;
801 uint32 temp_size=0, total_data_size=0;
802 NTSTATUS disp_ret;
803 uint32 num_account = 0;
804 enum remote_arch_types ra_type = get_remote_arch();
805 int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
806 struct samr_displayentry *entries = NULL;
808 DEBUG(5, ("samr_reply_query_dispinfo: %d\n", __LINE__));
809 r_u->status = NT_STATUS_UNSUCCESSFUL;
811 /* find the policy handle. open a policy on it. */
812 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
813 return NT_STATUS_INVALID_HANDLE;
816 * calculate how many entries we will return.
817 * based on
818 * - the number of entries the client asked
819 * - our limit on that
820 * - the starting point (enumeration context)
821 * - the buffer size the client will accept
825 * We are a lot more like W2K. Instead of reading the SAM
826 * each time to find the records we need to send back,
827 * we read it once and link that copy to the sam handle.
828 * For large user list (over the MAX_SAM_ENTRIES)
829 * it's a definitive win.
830 * second point to notice: between enumerations
831 * our sam is now the same as it's a snapshoot.
832 * third point: got rid of the static SAM_USER_21 struct
833 * no more intermediate.
834 * con: it uses much more memory, as a full copy is stored
835 * in memory.
837 * If you want to change it, think twice and think
838 * of the second point , that's really important.
840 * JFM, 12/20/2001
843 if ((q_u->switch_level < 1) || (q_u->switch_level > 5)) {
844 DEBUG(0,("_samr_query_dispinfo: Unknown info level (%u)\n",
845 (unsigned int)q_u->switch_level ));
846 return NT_STATUS_INVALID_INFO_CLASS;
849 /* first limit the number of entries we will return */
850 if(max_entries > max_sam_entries) {
851 DEBUG(5, ("samr_reply_query_dispinfo: client requested %d "
852 "entries, limiting to %d\n", max_entries,
853 max_sam_entries));
854 max_entries = max_sam_entries;
857 /* calculate the size and limit on the number of entries we will
858 * return */
860 temp_size=max_entries*struct_size;
862 if (temp_size>max_size) {
863 max_entries=MIN((max_size/struct_size),max_entries);;
864 DEBUG(5, ("samr_reply_query_dispinfo: buffer size limits to "
865 "only %d entries\n", max_entries));
868 if (!(ctr = TALLOC_ZERO_P(p->mem_ctx,SAM_DISPINFO_CTR)))
869 return NT_STATUS_NO_MEMORY;
871 ZERO_STRUCTP(ctr);
873 become_root();
875 switch (q_u->switch_level) {
876 case 0x1:
877 case 0x4:
878 if (info->disp_info.users == NULL)
879 info->disp_info.users = pdb_search_users(ACB_NORMAL);
880 if (info->disp_info.users == NULL)
881 return NT_STATUS_ACCESS_DENIED;
882 num_account = pdb_search_entries(info->disp_info.users,
883 enum_context, max_entries,
884 &entries);
885 break;
886 case 0x2:
887 if (info->disp_info.machines == NULL)
888 info->disp_info.machines =
889 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
890 if (info->disp_info.machines == NULL)
891 return NT_STATUS_ACCESS_DENIED;
892 num_account = pdb_search_entries(info->disp_info.machines,
893 enum_context, max_entries,
894 &entries);
895 break;
896 case 0x3:
897 case 0x5:
898 if (info->disp_info.groups == NULL)
899 info->disp_info.groups = pdb_search_groups();
900 if (info->disp_info.groups == NULL)
901 return NT_STATUS_ACCESS_DENIED;
902 num_account = pdb_search_entries(info->disp_info.groups,
903 enum_context, max_entries,
904 &entries);
905 break;
906 default:
907 smb_panic("info class changed");
908 break;
910 unbecome_root();
912 /* Now create reply structure */
913 switch (q_u->switch_level) {
914 case 0x1:
915 disp_ret = init_sam_dispinfo_1(p->mem_ctx, &ctr->sam.info1,
916 num_account, enum_context,
917 entries);
918 break;
919 case 0x2:
920 disp_ret = init_sam_dispinfo_2(p->mem_ctx, &ctr->sam.info2,
921 num_account, enum_context,
922 entries);
923 break;
924 case 0x3:
925 disp_ret = init_sam_dispinfo_3(p->mem_ctx, &ctr->sam.info3,
926 num_account, enum_context,
927 entries);
928 break;
929 case 0x4:
930 disp_ret = init_sam_dispinfo_4(p->mem_ctx, &ctr->sam.info4,
931 num_account, enum_context,
932 entries);
933 break;
934 case 0x5:
935 disp_ret = init_sam_dispinfo_5(p->mem_ctx, &ctr->sam.info5,
936 num_account, enum_context,
937 entries);
938 break;
939 default:
940 smb_panic("info class changed");
941 break;
944 if (!NT_STATUS_IS_OK(disp_ret))
945 return disp_ret;
947 /* calculate the total size */
948 total_data_size=num_account*struct_size;
950 if (num_account)
951 r_u->status = STATUS_MORE_ENTRIES;
952 else
953 r_u->status = NT_STATUS_OK;
955 DEBUG(5, ("_samr_query_dispinfo: %d\n", __LINE__));
957 init_samr_r_query_dispinfo(r_u, num_account, total_data_size,
958 temp_size, q_u->switch_level, ctr,
959 r_u->status);
961 return r_u->status;
965 /*******************************************************************
966 samr_reply_query_aliasinfo
967 ********************************************************************/
969 NTSTATUS _samr_query_aliasinfo(pipes_struct *p, SAMR_Q_QUERY_ALIASINFO *q_u, SAMR_R_QUERY_ALIASINFO *r_u)
971 DOM_SID sid;
972 struct acct_info info;
973 uint32 acc_granted;
974 BOOL ret;
976 r_u->status = NT_STATUS_OK;
978 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
980 /* find the policy handle. open a policy on it. */
981 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted))
982 return NT_STATUS_INVALID_HANDLE;
983 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_LOOKUP_INFO, "_samr_query_aliasinfo"))) {
984 return r_u->status;
987 become_root();
988 ret = pdb_get_aliasinfo(&sid, &info);
989 unbecome_root();
991 if ( !ret )
992 return NT_STATUS_NO_SUCH_ALIAS;
994 if ( !(r_u->ctr = TALLOC_ZERO_P( p->mem_ctx, ALIAS_INFO_CTR )) )
995 return NT_STATUS_NO_MEMORY;
998 switch (q_u->level ) {
999 case 1:
1000 r_u->ctr->level = 1;
1001 init_samr_alias_info1(&r_u->ctr->alias.info1, info.acct_name, 1, info.acct_desc);
1002 break;
1003 case 3:
1004 r_u->ctr->level = 3;
1005 init_samr_alias_info3(&r_u->ctr->alias.info3, info.acct_desc);
1006 break;
1007 default:
1008 return NT_STATUS_INVALID_INFO_CLASS;
1011 DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1013 return r_u->status;
1016 #if 0
1017 /*******************************************************************
1018 samr_reply_lookup_ids
1019 ********************************************************************/
1021 uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1023 uint32 rid[MAX_SAM_ENTRIES];
1024 int num_rids = q_u->num_sids1;
1026 r_u->status = NT_STATUS_OK;
1028 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1030 if (num_rids > MAX_SAM_ENTRIES) {
1031 num_rids = MAX_SAM_ENTRIES;
1032 DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1035 #if 0
1036 int i;
1037 SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1039 for (i = 0; i < num_rids && status == 0; i++)
1041 struct sam_passwd *sam_pass;
1042 fstring user_name;
1045 fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1046 q_u->uni_user_name[i].uni_str_len));
1048 /* find the user account */
1049 become_root();
1050 sam_pass = get_smb21pwd_entry(user_name, 0);
1051 unbecome_root();
1053 if (sam_pass == NULL)
1055 status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1056 rid[i] = 0;
1058 else
1060 rid[i] = sam_pass->user_rid;
1063 #endif
1065 num_rids = 1;
1066 rid[0] = BUILTIN_ALIAS_RID_USERS;
1068 init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1070 DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1072 return r_u->status;
1074 #endif
1076 /*******************************************************************
1077 _samr_lookup_names
1078 ********************************************************************/
1080 NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LOOKUP_NAMES *r_u)
1082 uint32 rid[MAX_SAM_ENTRIES];
1083 uint32 local_rid;
1084 enum SID_NAME_USE type[MAX_SAM_ENTRIES];
1085 enum SID_NAME_USE local_type;
1086 int i;
1087 int num_rids = q_u->num_names2;
1088 DOM_SID pol_sid;
1089 fstring sid_str;
1090 uint32 acc_granted;
1092 r_u->status = NT_STATUS_OK;
1094 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1096 ZERO_ARRAY(rid);
1097 ZERO_ARRAY(type);
1099 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted)) {
1100 init_samr_r_lookup_names(p->mem_ctx, r_u, 0, NULL, NULL, NT_STATUS_OBJECT_TYPE_MISMATCH);
1101 return r_u->status;
1104 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 */
1105 return r_u->status;
1108 if (num_rids > MAX_SAM_ENTRIES) {
1109 num_rids = MAX_SAM_ENTRIES;
1110 DEBUG(5,("_samr_lookup_names: truncating entries to %d\n", num_rids));
1113 DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1115 for (i = 0; i < num_rids; i++) {
1116 fstring name;
1117 DOM_SID sid;
1118 int ret;
1120 r_u->status = NT_STATUS_NONE_MAPPED;
1122 rid [i] = 0xffffffff;
1123 type[i] = SID_NAME_UNKNOWN;
1125 ret = rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1128 * we are only looking for a name
1129 * the SID we get back can be outside
1130 * the scope of the pol_sid
1132 * in clear: it prevents to reply to domain\group: yes
1133 * when only builtin\group exists.
1135 * a cleaner code is to add the sid of the domain we're looking in
1136 * to the local_lookup_name function.
1139 if ((ret > 0) && local_lookup_name(name, &sid, &local_type)) {
1140 sid_split_rid(&sid, &local_rid);
1142 if (sid_equal(&sid, &pol_sid)) {
1143 rid[i]=local_rid;
1145 /* Windows does not return WKN_GRP here, even
1146 * on lookups in builtin */
1147 type[i] = (local_type == SID_NAME_WKN_GRP) ?
1148 SID_NAME_ALIAS : local_type;
1150 r_u->status = NT_STATUS_OK;
1155 init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, (uint32 *)type, r_u->status);
1157 DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1159 return r_u->status;
1162 /*******************************************************************
1163 _samr_chgpasswd_user
1164 ********************************************************************/
1166 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1168 fstring user_name;
1169 fstring wks;
1171 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1173 r_u->status = NT_STATUS_OK;
1175 rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1176 rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1178 DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1181 * Pass the user through the NT -> unix user mapping
1182 * function.
1185 (void)map_username(user_name);
1188 * UNIX username case mangling not required, pass_oem_change
1189 * is case insensitive.
1192 r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1193 q_u->nt_newpass.pass, q_u->nt_oldhash.hash);
1195 init_samr_r_chgpasswd_user(r_u, r_u->status);
1197 DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1199 return r_u->status;
1202 /*******************************************************************
1203 makes a SAMR_R_LOOKUP_RIDS structure.
1204 ********************************************************************/
1206 static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1207 const char **names, UNIHDR **pp_hdr_name,
1208 UNISTR2 **pp_uni_name)
1210 uint32 i;
1211 UNIHDR *hdr_name=NULL;
1212 UNISTR2 *uni_name=NULL;
1214 *pp_uni_name = NULL;
1215 *pp_hdr_name = NULL;
1217 if (num_names != 0) {
1218 hdr_name = TALLOC_ZERO_ARRAY(ctx, UNIHDR, num_names);
1219 if (hdr_name == NULL)
1220 return False;
1222 uni_name = TALLOC_ZERO_ARRAY(ctx,UNISTR2, num_names);
1223 if (uni_name == NULL)
1224 return False;
1227 for (i = 0; i < num_names; i++) {
1228 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1229 init_unistr2(&uni_name[i], names[i], UNI_FLAGS_NONE);
1230 init_uni_hdr(&hdr_name[i], &uni_name[i]);
1233 *pp_uni_name = uni_name;
1234 *pp_hdr_name = hdr_name;
1236 return True;
1239 /*******************************************************************
1240 _samr_lookup_rids
1241 ********************************************************************/
1243 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1245 const char **names;
1246 uint32 *attrs = NULL;
1247 UNIHDR *hdr_name = NULL;
1248 UNISTR2 *uni_name = NULL;
1249 DOM_SID pol_sid;
1250 int num_rids = q_u->num_rids1;
1251 uint32 acc_granted;
1253 r_u->status = NT_STATUS_OK;
1255 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1257 /* find the policy handle. open a policy on it. */
1258 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted))
1259 return NT_STATUS_INVALID_HANDLE;
1261 if (num_rids > 1000) {
1262 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
1263 "to samba4 idl this is not possible\n", num_rids));
1264 return NT_STATUS_UNSUCCESSFUL;
1267 names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
1268 attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
1270 if ((num_rids != 0) && ((names == NULL) || (attrs == NULL)))
1271 return NT_STATUS_NO_MEMORY;
1273 if (!sid_equal(&pol_sid, get_global_sam_sid())) {
1274 /* TODO: Sooner or later we need to look up BUILTIN rids as
1275 * well. -- vl */
1276 goto done;
1279 become_root(); /* lookup_sid can require root privs */
1280 r_u->status = pdb_lookup_rids(p->mem_ctx, &pol_sid, num_rids, q_u->rid,
1281 &names, &attrs);
1282 unbecome_root();
1284 done:
1286 if(!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
1287 &hdr_name, &uni_name))
1288 return NT_STATUS_NO_MEMORY;
1290 init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, attrs);
1292 DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1294 return r_u->status;
1297 /*******************************************************************
1298 _samr_open_user. Safe - gives out no passwd info.
1299 ********************************************************************/
1301 NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1303 SAM_ACCOUNT *sampass=NULL;
1304 DOM_SID sid;
1305 POLICY_HND domain_pol = q_u->domain_pol;
1306 POLICY_HND *user_pol = &r_u->user_pol;
1307 struct samr_info *info = NULL;
1308 SEC_DESC *psd = NULL;
1309 uint32 acc_granted;
1310 uint32 des_access = q_u->access_mask;
1311 size_t sd_size;
1312 BOOL ret;
1313 NTSTATUS nt_status;
1314 SE_PRIV se_rights;
1316 r_u->status = NT_STATUS_OK;
1318 /* find the domain policy handle and get domain SID / access bits in the domain policy. */
1320 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted) )
1321 return NT_STATUS_INVALID_HANDLE;
1323 nt_status = access_check_samr_function( acc_granted,
1324 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_user" );
1326 if ( !NT_STATUS_IS_OK(nt_status) )
1327 return nt_status;
1329 nt_status = pdb_init_sam_talloc(p->mem_ctx, &sampass);
1331 if (!NT_STATUS_IS_OK(nt_status))
1332 return nt_status;
1334 /* append the user's RID to it */
1336 if (!sid_append_rid(&sid, q_u->user_rid))
1337 return NT_STATUS_NO_SUCH_USER;
1339 /* check if access can be granted as requested by client. */
1341 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
1342 se_map_generic(&des_access, &usr_generic_mapping);
1344 se_priv_copy( &se_rights, &se_machine_account );
1345 se_priv_add( &se_rights, &se_add_users );
1347 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
1348 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
1349 &acc_granted, "_samr_open_user");
1351 if ( !NT_STATUS_IS_OK(nt_status) )
1352 return nt_status;
1354 become_root();
1355 ret=pdb_getsampwsid(sampass, &sid);
1356 unbecome_root();
1358 /* check that the SID exists in our domain. */
1359 if (ret == False) {
1360 return NT_STATUS_NO_SUCH_USER;
1363 pdb_free_sam(&sampass);
1365 /* associate the user's SID and access bits with the new handle. */
1366 if ((info = get_samr_info_by_sid(&sid)) == NULL)
1367 return NT_STATUS_NO_MEMORY;
1368 info->acc_granted = acc_granted;
1370 /* get a (unique) handle. open a policy on it. */
1371 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
1372 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1374 return r_u->status;
1377 /*************************************************************************
1378 get_user_info_7. Safe. Only gives out account_name.
1379 *************************************************************************/
1381 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_SID *user_sid)
1383 SAM_ACCOUNT *smbpass=NULL;
1384 BOOL ret;
1385 NTSTATUS nt_status;
1387 nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1389 if (!NT_STATUS_IS_OK(nt_status)) {
1390 return nt_status;
1393 become_root();
1394 ret = pdb_getsampwsid(smbpass, user_sid);
1395 unbecome_root();
1397 if (ret==False) {
1398 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1399 return NT_STATUS_NO_SUCH_USER;
1402 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1404 ZERO_STRUCTP(id7);
1405 init_sam_user_info7(id7, pdb_get_username(smbpass) );
1407 pdb_free_sam(&smbpass);
1409 return NT_STATUS_OK;
1411 /*************************************************************************
1412 get_user_info_16. Safe. Only gives out acb bits.
1413 *************************************************************************/
1415 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DOM_SID *user_sid)
1417 SAM_ACCOUNT *smbpass=NULL;
1418 BOOL ret;
1419 NTSTATUS nt_status;
1421 nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1423 if (!NT_STATUS_IS_OK(nt_status)) {
1424 return nt_status;
1427 become_root();
1428 ret = pdb_getsampwsid(smbpass, user_sid);
1429 unbecome_root();
1431 if (ret==False) {
1432 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1433 return NT_STATUS_NO_SUCH_USER;
1436 DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1438 ZERO_STRUCTP(id16);
1439 init_sam_user_info16(id16, pdb_get_acct_ctrl(smbpass) );
1441 pdb_free_sam(&smbpass);
1443 return NT_STATUS_OK;
1446 /*************************************************************************
1447 get_user_info_18. OK - this is the killer as it gives out password info.
1448 Ensure that this is only allowed on an encrypted connection with a root
1449 user. JRA.
1450 *************************************************************************/
1452 static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_INFO_18 * id18, DOM_SID *user_sid)
1454 SAM_ACCOUNT *smbpass=NULL;
1455 BOOL ret;
1456 NTSTATUS nt_status;
1458 if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
1459 return NT_STATUS_ACCESS_DENIED;
1462 if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
1463 return NT_STATUS_ACCESS_DENIED;
1467 * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1470 nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1472 if (!NT_STATUS_IS_OK(nt_status)) {
1473 return nt_status;
1476 ret = pdb_getsampwsid(smbpass, user_sid);
1478 if (ret == False) {
1479 DEBUG(4, ("User %s not found\n", sid_string_static(user_sid)));
1480 pdb_free_sam(&smbpass);
1481 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
1484 DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1486 if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1487 pdb_free_sam(&smbpass);
1488 return NT_STATUS_ACCOUNT_DISABLED;
1491 ZERO_STRUCTP(id18);
1492 init_sam_user_info18(id18, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1494 pdb_free_sam(&smbpass);
1496 return NT_STATUS_OK;
1499 /*************************************************************************
1500 get_user_info_20
1501 *************************************************************************/
1503 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DOM_SID *user_sid)
1505 SAM_ACCOUNT *sampass=NULL;
1506 BOOL ret;
1508 pdb_init_sam_talloc(mem_ctx, &sampass);
1510 become_root();
1511 ret = pdb_getsampwsid(sampass, user_sid);
1512 unbecome_root();
1514 if (ret == False) {
1515 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1516 return NT_STATUS_NO_SUCH_USER;
1519 samr_clear_sam_passwd(sampass);
1521 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1523 ZERO_STRUCTP(id20);
1524 init_sam_user_info20A(id20, sampass);
1526 pdb_free_sam(&sampass);
1528 return NT_STATUS_OK;
1531 /*************************************************************************
1532 get_user_info_21
1533 *************************************************************************/
1535 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21,
1536 DOM_SID *user_sid, DOM_SID *domain_sid)
1538 SAM_ACCOUNT *sampass=NULL;
1539 BOOL ret;
1540 NTSTATUS nt_status;
1542 nt_status = pdb_init_sam_talloc(mem_ctx, &sampass);
1543 if (!NT_STATUS_IS_OK(nt_status)) {
1544 return nt_status;
1547 become_root();
1548 ret = pdb_getsampwsid(sampass, user_sid);
1549 unbecome_root();
1551 if (ret == False) {
1552 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1553 return NT_STATUS_NO_SUCH_USER;
1556 samr_clear_sam_passwd(sampass);
1558 DEBUG(3,("User:[%s]\n", pdb_get_username(sampass) ));
1560 ZERO_STRUCTP(id21);
1561 nt_status = init_sam_user_info21A(id21, sampass, domain_sid);
1563 pdb_free_sam(&sampass);
1565 return NT_STATUS_OK;
1568 /*******************************************************************
1569 _samr_query_userinfo
1570 ********************************************************************/
1572 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
1574 SAM_USERINFO_CTR *ctr;
1575 struct samr_info *info = NULL;
1576 DOM_SID domain_sid;
1577 uint32 rid;
1579 r_u->status=NT_STATUS_OK;
1581 /* search for the handle */
1582 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1583 return NT_STATUS_INVALID_HANDLE;
1585 domain_sid = info->sid;
1587 sid_split_rid(&domain_sid, &rid);
1589 if (!sid_check_is_in_our_domain(&info->sid))
1590 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1592 DEBUG(5,("_samr_query_userinfo: sid:%s\n", sid_string_static(&info->sid)));
1594 ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_USERINFO_CTR);
1595 if (!ctr)
1596 return NT_STATUS_NO_MEMORY;
1598 ZERO_STRUCTP(ctr);
1600 /* ok! user info levels (lots: see MSDEV help), off we go... */
1601 ctr->switch_value = q_u->switch_value;
1603 switch (q_u->switch_value) {
1604 case 7:
1605 ctr->info.id7 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_7);
1606 if (ctr->info.id7 == NULL)
1607 return NT_STATUS_NO_MEMORY;
1609 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_7(p->mem_ctx, ctr->info.id7, &info->sid)))
1610 return r_u->status;
1611 break;
1612 case 16:
1613 ctr->info.id16 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_16);
1614 if (ctr->info.id16 == NULL)
1615 return NT_STATUS_NO_MEMORY;
1617 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_16(p->mem_ctx, ctr->info.id16, &info->sid)))
1618 return r_u->status;
1619 break;
1621 #if 0
1622 /* whoops - got this wrong. i think. or don't understand what's happening. */
1623 case 17:
1625 NTTIME expire;
1626 info = (void *)&id11;
1628 expire.low = 0xffffffff;
1629 expire.high = 0x7fffffff;
1631 ctr->info.id = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_17));
1632 ZERO_STRUCTP(ctr->info.id17);
1633 init_sam_user_info17(ctr->info.id17, &expire,
1634 "BROOKFIELDS$", /* name */
1635 0x03ef, /* user rid */
1636 0x201, /* group rid */
1637 0x0080); /* acb info */
1639 break;
1641 #endif
1643 case 18:
1644 ctr->info.id18 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_18);
1645 if (ctr->info.id18 == NULL)
1646 return NT_STATUS_NO_MEMORY;
1648 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_18(p, p->mem_ctx, ctr->info.id18, &info->sid)))
1649 return r_u->status;
1650 break;
1652 case 20:
1653 ctr->info.id20 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_20);
1654 if (ctr->info.id20 == NULL)
1655 return NT_STATUS_NO_MEMORY;
1656 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_20(p->mem_ctx, ctr->info.id20, &info->sid)))
1657 return r_u->status;
1658 break;
1660 case 21:
1661 ctr->info.id21 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_21);
1662 if (ctr->info.id21 == NULL)
1663 return NT_STATUS_NO_MEMORY;
1664 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_21(p->mem_ctx, ctr->info.id21,
1665 &info->sid, &domain_sid)))
1666 return r_u->status;
1667 break;
1669 default:
1670 return NT_STATUS_INVALID_INFO_CLASS;
1673 init_samr_r_query_userinfo(r_u, ctr, r_u->status);
1675 DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
1677 return r_u->status;
1680 /*******************************************************************
1681 samr_reply_query_usergroups
1682 ********************************************************************/
1684 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
1686 SAM_ACCOUNT *sam_pass=NULL;
1687 struct passwd *passwd;
1688 DOM_SID sid;
1689 DOM_SID *sids;
1690 DOM_GID *gids = NULL;
1691 size_t num_groups = 0;
1692 gid_t *unix_gids;
1693 size_t i, num_gids;
1694 uint32 acc_granted;
1695 BOOL ret;
1696 NTSTATUS result;
1699 * from the SID in the request:
1700 * we should send back the list of DOMAIN GROUPS
1701 * the user is a member of
1703 * and only the DOMAIN GROUPS
1704 * no ALIASES !!! neither aliases of the domain
1705 * nor aliases of the builtin SID
1707 * JFM, 12/2/2001
1710 r_u->status = NT_STATUS_OK;
1712 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1714 /* find the policy handle. open a policy on it. */
1715 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted))
1716 return NT_STATUS_INVALID_HANDLE;
1718 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_USER_GET_GROUPS, "_samr_query_usergroups"))) {
1719 return r_u->status;
1722 if (!sid_check_is_in_our_domain(&sid))
1723 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1725 pdb_init_sam(&sam_pass);
1727 become_root();
1728 ret = pdb_getsampwsid(sam_pass, &sid);
1729 unbecome_root();
1731 if (ret == False) {
1732 pdb_free_sam(&sam_pass);
1733 return NT_STATUS_NO_SUCH_USER;
1736 passwd = getpwnam_alloc(pdb_get_username(sam_pass));
1737 if (passwd == NULL) {
1738 pdb_free_sam(&sam_pass);
1739 return NT_STATUS_NO_SUCH_USER;
1742 sids = NULL;
1744 become_root();
1745 result = pdb_enum_group_memberships(pdb_get_username(sam_pass),
1746 passwd->pw_gid,
1747 &sids, &unix_gids, &num_groups);
1748 unbecome_root();
1750 pdb_free_sam(&sam_pass);
1751 passwd_free(&passwd);
1753 if (!NT_STATUS_IS_OK(result))
1754 return result;
1756 SAFE_FREE(unix_gids);
1758 gids = NULL;
1759 num_gids = 0;
1761 for (i=0; i<num_groups; i++) {
1762 uint32 rid;
1764 if (!sid_peek_check_rid(get_global_sam_sid(),
1765 &(sids[i]), &rid))
1766 continue;
1768 gids = TALLOC_REALLOC_ARRAY(p->mem_ctx, gids, DOM_GID, num_gids+1);
1769 gids[num_gids].attr=7;
1770 gids[num_gids].g_rid = rid;
1771 num_gids += 1;
1773 SAFE_FREE(sids);
1775 /* construct the response. lkclXXXX: gids are not copied! */
1776 init_samr_r_query_usergroups(r_u, num_groups, gids, r_u->status);
1778 DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1780 return r_u->status;
1783 /*******************************************************************
1784 _samr_query_dom_info
1785 ********************************************************************/
1787 NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SAMR_R_QUERY_DOMAIN_INFO *r_u)
1789 struct samr_info *info = NULL;
1790 SAM_UNK_CTR *ctr;
1791 uint32 min_pass_len,pass_hist,flag;
1792 time_t u_expire, u_min_age;
1793 NTTIME nt_expire, nt_min_age;
1795 time_t u_lock_duration, u_reset_time;
1796 NTTIME nt_lock_duration, nt_reset_time;
1797 uint32 lockout;
1798 time_t u_logout;
1799 NTTIME nt_logout;
1801 uint32 account_policy_temp;
1803 time_t seq_num;
1804 uint32 server_role;
1806 uint32 num_users=0, num_groups=0, num_aliases=0;
1808 if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL)
1809 return NT_STATUS_NO_MEMORY;
1811 ZERO_STRUCTP(ctr);
1813 r_u->status = NT_STATUS_OK;
1815 DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
1817 /* find the policy handle. open a policy on it. */
1818 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
1819 return NT_STATUS_INVALID_HANDLE;
1821 switch (q_u->switch_value) {
1822 case 0x01:
1824 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
1825 min_pass_len = account_policy_temp;
1827 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
1828 pass_hist = account_policy_temp;
1830 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
1831 flag = account_policy_temp;
1833 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
1834 u_expire = account_policy_temp;
1836 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
1837 u_min_age = account_policy_temp;
1839 unix_to_nt_time_abs(&nt_expire, u_expire);
1840 unix_to_nt_time_abs(&nt_min_age, u_min_age);
1842 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
1843 flag, nt_expire, nt_min_age);
1844 break;
1845 case 0x02:
1846 become_root();
1847 num_users=count_sam_users(&info->disp_info,
1848 ACB_NORMAL);
1849 num_groups=count_sam_groups(&info->disp_info);
1850 unbecome_root();
1852 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
1853 u_logout = account_policy_temp;
1855 unix_to_nt_time_abs(&nt_logout, u_logout);
1857 if (!pdb_get_seq_num(&seq_num))
1858 seq_num = time(NULL);
1860 server_role = ROLE_DOMAIN_PDC;
1861 if (lp_server_role() == ROLE_DOMAIN_BDC)
1862 server_role = ROLE_DOMAIN_BDC;
1864 init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num,
1865 num_users, num_groups, num_aliases, nt_logout, server_role);
1866 break;
1867 case 0x03:
1868 pdb_get_account_policy(AP_TIME_TO_LOGOUT, (unsigned int *)&u_logout);
1869 unix_to_nt_time_abs(&nt_logout, u_logout);
1871 init_unk_info3(&ctr->info.inf3, nt_logout);
1872 break;
1873 case 0x05:
1874 init_unk_info5(&ctr->info.inf5, global_myname());
1875 break;
1876 case 0x06:
1877 init_unk_info6(&ctr->info.inf6);
1878 break;
1879 case 0x07:
1880 server_role = ROLE_DOMAIN_PDC;
1881 if (lp_server_role() == ROLE_DOMAIN_BDC)
1882 server_role = ROLE_DOMAIN_BDC;
1884 init_unk_info7(&ctr->info.inf7, server_role);
1885 break;
1886 case 0x08:
1887 if (!pdb_get_seq_num(&seq_num))
1888 seq_num = time(NULL);
1890 init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
1891 break;
1892 case 0x0c:
1893 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
1894 u_lock_duration = account_policy_temp;
1895 if (u_lock_duration != -1)
1896 u_lock_duration *= 60;
1898 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
1899 u_reset_time = account_policy_temp * 60;
1901 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
1902 lockout = account_policy_temp;
1904 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
1905 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
1907 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
1908 break;
1909 default:
1910 return NT_STATUS_INVALID_INFO_CLASS;
1913 init_samr_r_query_dom_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
1915 DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
1917 return r_u->status;
1920 /*******************************************************************
1921 _samr_create_user
1922 Create an account, can be either a normal user or a machine.
1923 This funcion will need to be updated for bdc/domain trusts.
1924 ********************************************************************/
1926 NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u, SAMR_R_CREATE_USER *r_u)
1928 SAM_ACCOUNT *sam_pass=NULL;
1929 fstring account;
1930 DOM_SID sid;
1931 pstring add_script;
1932 POLICY_HND dom_pol = q_u->domain_pol;
1933 UNISTR2 user_account = q_u->uni_name;
1934 uint16 acb_info = q_u->acb_info;
1935 POLICY_HND *user_pol = &r_u->user_pol;
1936 struct samr_info *info = NULL;
1937 BOOL ret;
1938 NTSTATUS nt_status;
1939 struct passwd *pw;
1940 uint32 acc_granted;
1941 SEC_DESC *psd;
1942 size_t sd_size;
1943 uint32 new_rid = 0;
1944 /* check this, when giving away 'add computer to domain' privs */
1945 uint32 des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
1946 BOOL can_add_account = False;
1947 SE_PRIV se_rights;
1949 /* Get the domain SID stored in the domain policy */
1950 if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted))
1951 return NT_STATUS_INVALID_HANDLE;
1953 if (!NT_STATUS_IS_OK(nt_status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_USER, "_samr_create_user"))) {
1954 return nt_status;
1957 if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST || acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) {
1958 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if
1959 this parameter is not an account type */
1960 return NT_STATUS_INVALID_PARAMETER;
1963 rpcstr_pull(account, user_account.buffer, sizeof(account), user_account.uni_str_len*2, 0);
1964 strlower_m(account);
1966 pdb_init_sam(&sam_pass);
1968 become_root();
1969 ret = pdb_getsampwnam(sam_pass, account);
1970 unbecome_root();
1971 if (ret == True) {
1972 /* this account exists: say so */
1973 pdb_free_sam(&sam_pass);
1974 return NT_STATUS_USER_EXISTS;
1977 pdb_free_sam(&sam_pass);
1979 /*********************************************************************
1980 * HEADS UP! If we have to create a new user account, we have to get
1981 * a new RID from somewhere. This used to be done by the passdb
1982 * backend. It has been moved into idmap now. Since idmap is now
1983 * wrapped up behind winbind, this means you have to run winbindd if you
1984 * want new accounts to get a new RID when "enable rid algorithm = no".
1985 * Tough. We now have a uniform way of allocating RIDs regardless
1986 * of what ever passdb backend people may use.
1987 * --jerry (2003-07-10)
1988 *********************************************************************/
1990 pw = Get_Pwnam(account);
1992 /* determine which user right we need to check based on the acb_info */
1994 if ( acb_info & ACB_WSTRUST )
1996 pstrcpy(add_script, lp_addmachine_script());
1997 se_priv_copy( &se_rights, &se_machine_account );
1998 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2000 /* usrmgr.exe (and net rpc trustdom grant) creates a normal user
2001 account for domain trusts and changes the ACB flags later */
2002 else if ( acb_info & ACB_NORMAL && (account[strlen(account)-1] != '$') )
2004 pstrcpy(add_script, lp_adduser_script());
2005 se_priv_copy( &se_rights, &se_add_users );
2006 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2008 else /* implicit assumption of a BDC or domain trust account here (we already check the flags earlier) */
2010 pstrcpy(add_script, lp_addmachine_script());
2011 if ( lp_enable_privileges() ) {
2012 /* only Domain Admins can add a BDC or domain trust */
2013 se_priv_copy( &se_rights, &se_priv_none );
2014 can_add_account = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
2018 DEBUG(5, ("_samr_create_user: %s can add this account : %s\n",
2019 p->pipe_user_name, can_add_account ? "True":"False" ));
2021 /********** BEGIN Admin BLOCK **********/
2023 if ( can_add_account )
2024 become_root();
2026 if ( !pw ) {
2027 if (*add_script) {
2028 int add_ret;
2030 all_string_sub(add_script, "%u", account, sizeof(add_script));
2031 add_ret = smbrun(add_script,NULL);
2032 DEBUG(add_ret ? 0 : 3,("_samr_create_user: Running the command `%s' gave %d\n", add_script, add_ret));
2036 /* implicit call to getpwnam() next. we have a valid SID coming out of this call */
2038 flush_pwnam_cache();
2039 nt_status = pdb_init_sam_new(&sam_pass, account, new_rid);
2041 /* this code is order such that we have no unnecessary retuns
2042 out of the admin block of code */
2044 if ( NT_STATUS_IS_OK(nt_status) ) {
2045 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
2047 if ( !(ret = pdb_add_sam_account(sam_pass)) ) {
2048 pdb_free_sam(&sam_pass);
2049 DEBUG(0, ("could not add user/computer %s to passdb. Check permissions?\n",
2050 account));
2051 nt_status = NT_STATUS_ACCESS_DENIED;
2055 if ( can_add_account )
2056 unbecome_root();
2058 /********** END Admin BLOCK **********/
2060 /* now check for failure */
2062 if ( !NT_STATUS_IS_OK(nt_status) )
2063 return nt_status;
2065 /* Get the user's SID */
2067 sid_copy(&sid, pdb_get_user_sid(sam_pass));
2069 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2070 se_map_generic(&des_access, &usr_generic_mapping);
2072 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2073 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access,
2074 &acc_granted, "_samr_create_user");
2076 if ( !NT_STATUS_IS_OK(nt_status) ) {
2077 return nt_status;
2080 /* associate the user's SID with the new handle. */
2081 if ((info = get_samr_info_by_sid(&sid)) == NULL) {
2082 pdb_free_sam(&sam_pass);
2083 return NT_STATUS_NO_MEMORY;
2086 ZERO_STRUCTP(info);
2087 info->sid = sid;
2088 info->acc_granted = acc_granted;
2090 /* get a (unique) handle. open a policy on it. */
2091 if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
2092 pdb_free_sam(&sam_pass);
2093 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2096 r_u->user_rid=pdb_get_user_rid(sam_pass);
2098 r_u->access_granted = acc_granted;
2100 pdb_free_sam(&sam_pass);
2102 return NT_STATUS_OK;
2105 /*******************************************************************
2106 samr_reply_connect_anon
2107 ********************************************************************/
2109 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2111 struct samr_info *info = NULL;
2112 uint32 des_access = q_u->access_mask;
2114 /* Access check */
2116 if (!pipe_access_check(p)) {
2117 DEBUG(3, ("access denied to samr_connect_anon\n"));
2118 r_u->status = NT_STATUS_ACCESS_DENIED;
2119 return r_u->status;
2122 /* set up the SAMR connect_anon response */
2124 r_u->status = NT_STATUS_OK;
2126 /* associate the user's SID with the new handle. */
2127 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2128 return NT_STATUS_NO_MEMORY;
2130 /* don't give away the farm but this is probably ok. The SA_RIGHT_SAM_ENUM_DOMAINS
2131 was observed from a win98 client trying to enumerate users (when configured
2132 user level access control on shares) --jerry */
2134 se_map_generic( &des_access, &sam_generic_mapping );
2135 info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
2137 info->status = q_u->unknown_0;
2139 /* get a (unique) handle. open a policy on it. */
2140 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2141 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2143 return r_u->status;
2146 /*******************************************************************
2147 samr_reply_connect
2148 ********************************************************************/
2150 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2152 struct samr_info *info = NULL;
2153 SEC_DESC *psd = NULL;
2154 uint32 acc_granted;
2155 uint32 des_access = q_u->access_mask;
2156 NTSTATUS nt_status;
2157 size_t sd_size;
2160 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2162 /* Access check */
2164 if (!pipe_access_check(p)) {
2165 DEBUG(3, ("access denied to samr_connect\n"));
2166 r_u->status = NT_STATUS_ACCESS_DENIED;
2167 return r_u->status;
2170 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2171 se_map_generic(&des_access, &sam_generic_mapping);
2173 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2174 NULL, 0, des_access, &acc_granted, "_samr_connect");
2176 if ( !NT_STATUS_IS_OK(nt_status) )
2177 return nt_status;
2179 r_u->status = NT_STATUS_OK;
2181 /* associate the user's SID and access granted with the new handle. */
2182 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2183 return NT_STATUS_NO_MEMORY;
2185 info->acc_granted = acc_granted;
2186 info->status = q_u->access_mask;
2188 /* get a (unique) handle. open a policy on it. */
2189 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2190 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2192 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2194 return r_u->status;
2197 /*******************************************************************
2198 samr_connect4
2199 ********************************************************************/
2201 NTSTATUS _samr_connect4(pipes_struct *p, SAMR_Q_CONNECT4 *q_u, SAMR_R_CONNECT4 *r_u)
2203 struct samr_info *info = NULL;
2204 SEC_DESC *psd = NULL;
2205 uint32 acc_granted;
2206 uint32 des_access = q_u->access_mask;
2207 NTSTATUS nt_status;
2208 size_t sd_size;
2211 DEBUG(5,("_samr_connect4: %d\n", __LINE__));
2213 /* Access check */
2215 if (!pipe_access_check(p)) {
2216 DEBUG(3, ("access denied to samr_connect4\n"));
2217 r_u->status = NT_STATUS_ACCESS_DENIED;
2218 return r_u->status;
2221 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2222 se_map_generic(&des_access, &sam_generic_mapping);
2224 nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2225 NULL, 0, des_access, &acc_granted, "_samr_connect4");
2227 if ( !NT_STATUS_IS_OK(nt_status) )
2228 return nt_status;
2230 r_u->status = NT_STATUS_OK;
2232 /* associate the user's SID and access granted with the new handle. */
2233 if ((info = get_samr_info_by_sid(NULL)) == NULL)
2234 return NT_STATUS_NO_MEMORY;
2236 info->acc_granted = acc_granted;
2237 info->status = q_u->access_mask;
2239 /* get a (unique) handle. open a policy on it. */
2240 if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2241 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2243 DEBUG(5,("_samr_connect: %d\n", __LINE__));
2245 return r_u->status;
2248 /**********************************************************************
2249 api_samr_lookup_domain
2250 **********************************************************************/
2252 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2254 struct samr_info *info;
2255 fstring domain_name;
2256 DOM_SID sid;
2258 r_u->status = NT_STATUS_OK;
2260 if (!find_policy_by_hnd(p, &q_u->connect_pol, (void**)&info))
2261 return NT_STATUS_INVALID_HANDLE;
2263 /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.
2264 Reverted that change so we will work with RAS servers again */
2266 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted,
2267 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_lookup_domain")))
2269 return r_u->status;
2272 rpcstr_pull(domain_name, q_u->uni_domain.buffer, sizeof(domain_name), q_u->uni_domain.uni_str_len*2, 0);
2274 ZERO_STRUCT(sid);
2276 if (!secrets_fetch_domain_sid(domain_name, &sid)) {
2277 r_u->status = NT_STATUS_NO_SUCH_DOMAIN;
2280 DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name, sid_string_static(&sid)));
2282 init_samr_r_lookup_domain(r_u, &sid, r_u->status);
2284 return r_u->status;
2287 /******************************************************************
2288 makes a SAMR_R_ENUM_DOMAINS structure.
2289 ********************************************************************/
2291 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2292 UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2294 uint32 i;
2295 SAM_ENTRY *sam;
2296 UNISTR2 *uni_name;
2298 DEBUG(5, ("make_enum_domains\n"));
2300 *pp_sam = NULL;
2301 *pp_uni_name = NULL;
2303 if (num_sam_entries == 0)
2304 return True;
2306 sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
2307 uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
2309 if (sam == NULL || uni_name == NULL)
2310 return False;
2312 for (i = 0; i < num_sam_entries; i++) {
2313 init_unistr2(&uni_name[i], doms[i], UNI_FLAGS_NONE);
2314 init_sam_entry(&sam[i], &uni_name[i], 0);
2317 *pp_sam = sam;
2318 *pp_uni_name = uni_name;
2320 return True;
2323 /**********************************************************************
2324 api_samr_enum_domains
2325 **********************************************************************/
2327 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2329 struct samr_info *info;
2330 uint32 num_entries = 2;
2331 fstring dom[2];
2332 const char *name;
2334 r_u->status = NT_STATUS_OK;
2336 if (!find_policy_by_hnd(p, &q_u->pol, (void**)&info))
2337 return NT_STATUS_INVALID_HANDLE;
2339 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, SA_RIGHT_SAM_ENUM_DOMAINS, "_samr_enum_domains"))) {
2340 return r_u->status;
2343 name = get_global_sam_name();
2345 fstrcpy(dom[0],name);
2346 strupper_m(dom[0]);
2347 fstrcpy(dom[1],"Builtin");
2349 if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2350 return NT_STATUS_NO_MEMORY;
2352 init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2354 return r_u->status;
2357 /*******************************************************************
2358 api_samr_open_alias
2359 ********************************************************************/
2361 NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2363 DOM_SID sid;
2364 POLICY_HND domain_pol = q_u->dom_pol;
2365 uint32 alias_rid = q_u->rid_alias;
2366 POLICY_HND *alias_pol = &r_u->pol;
2367 struct samr_info *info = NULL;
2368 SEC_DESC *psd = NULL;
2369 uint32 acc_granted;
2370 uint32 des_access = q_u->access_mask;
2371 size_t sd_size;
2372 NTSTATUS status;
2373 SE_PRIV se_rights;
2375 r_u->status = NT_STATUS_OK;
2377 /* find the domain policy and get the SID / access bits stored in the domain policy */
2379 if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted) )
2380 return NT_STATUS_INVALID_HANDLE;
2382 status = access_check_samr_function(acc_granted,
2383 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_alias");
2385 if ( !NT_STATUS_IS_OK(status) )
2386 return status;
2388 /* append the alias' RID to it */
2390 if (!sid_append_rid(&sid, alias_rid))
2391 return NT_STATUS_NO_SUCH_USER;
2393 /*check if access can be granted as requested by client. */
2395 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
2396 se_map_generic(&des_access,&ali_generic_mapping);
2398 se_priv_copy( &se_rights, &se_add_users );
2401 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
2402 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access,
2403 &acc_granted, "_samr_open_alias");
2405 if ( !NT_STATUS_IS_OK(status) )
2406 return status;
2409 * we should check if the rid really exist !!!
2410 * JFM.
2413 /* associate the user's SID with the new handle. */
2414 if ((info = get_samr_info_by_sid(&sid)) == NULL)
2415 return NT_STATUS_NO_MEMORY;
2417 info->acc_granted = acc_granted;
2419 /* get a (unique) handle. open a policy on it. */
2420 if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
2421 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2423 return r_u->status;
2426 /*******************************************************************
2427 set_user_info_7
2428 ********************************************************************/
2429 static NTSTATUS set_user_info_7(const SAM_USER_INFO_7 *id7, SAM_ACCOUNT *pwd)
2431 fstring new_name;
2432 SAM_ACCOUNT *check_acct = NULL;
2433 NTSTATUS rc;
2434 BOOL check_rc;
2436 if (id7 == NULL) {
2437 DEBUG(5, ("set_user_info_7: NULL id7\n"));
2438 pdb_free_sam(&pwd);
2439 return NT_STATUS_ACCESS_DENIED;
2442 if(!rpcstr_pull(new_name, id7->uni_name.buffer, sizeof(new_name), id7->uni_name.uni_str_len*2, 0)) {
2443 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
2444 pdb_free_sam(&pwd);
2445 return NT_STATUS_ACCESS_DENIED;
2448 /* check to see if the new username already exists. Note: we can't
2449 reliably lock all backends, so there is potentially the
2450 possibility that a user can be created in between this check and
2451 the rename. The rename should fail, but may not get the
2452 exact same failure status code. I think this is small enough
2453 of a window for this type of operation and the results are
2454 simply that the rename fails with a slightly different status
2455 code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
2457 pdb_init_sam(&check_acct);
2458 check_rc = pdb_getsampwnam(check_acct, new_name);
2459 pdb_free_sam(&check_acct);
2461 if (check_rc == True) {
2462 /* this account exists: say so */
2463 return NT_STATUS_USER_EXISTS;
2466 rc = pdb_rename_sam_account(pwd, new_name);
2468 pdb_free_sam(&pwd);
2469 return rc;
2472 /*******************************************************************
2473 set_user_info_16
2474 ********************************************************************/
2476 static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, SAM_ACCOUNT *pwd)
2478 if (id16 == NULL) {
2479 DEBUG(5, ("set_user_info_16: NULL id16\n"));
2480 pdb_free_sam(&pwd);
2481 return False;
2484 /* FIX ME: check if the value is really changed --metze */
2485 if (!pdb_set_acct_ctrl(pwd, id16->acb_info, PDB_CHANGED)) {
2486 pdb_free_sam(&pwd);
2487 return False;
2490 if(!pdb_update_sam_account(pwd)) {
2491 pdb_free_sam(&pwd);
2492 return False;
2495 pdb_free_sam(&pwd);
2497 return True;
2500 /*******************************************************************
2501 set_user_info_18
2502 ********************************************************************/
2504 static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, SAM_ACCOUNT *pwd)
2507 if (id18 == NULL) {
2508 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
2509 pdb_free_sam(&pwd);
2510 return False;
2513 if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd, PDB_CHANGED)) {
2514 pdb_free_sam(&pwd);
2515 return False;
2517 if (!pdb_set_nt_passwd (pwd, id18->nt_pwd, PDB_CHANGED)) {
2518 pdb_free_sam(&pwd);
2519 return False;
2521 if (!pdb_set_pass_changed_now (pwd)) {
2522 pdb_free_sam(&pwd);
2523 return False;
2526 if(!pdb_update_sam_account(pwd)) {
2527 pdb_free_sam(&pwd);
2528 return False;
2531 pdb_free_sam(&pwd);
2532 return True;
2535 /*******************************************************************
2536 The GROUPSID field in the SAM_ACCOUNT changed. Try to tell unix.
2537 ********************************************************************/
2538 static BOOL set_unix_primary_group(SAM_ACCOUNT *sampass)
2540 struct group *grp;
2541 gid_t gid;
2543 if (!NT_STATUS_IS_OK(sid_to_gid(pdb_get_group_sid(sampass),
2544 &gid))) {
2545 DEBUG(2,("Could not get gid for primary group of "
2546 "user %s\n", pdb_get_username(sampass)));
2547 return False;
2550 grp = getgrgid(gid);
2552 if (grp == NULL) {
2553 DEBUG(2,("Could not find primary group %lu for "
2554 "user %s\n", (unsigned long)gid,
2555 pdb_get_username(sampass)));
2556 return False;
2559 if (smb_set_primary_group(grp->gr_name,
2560 pdb_get_username(sampass)) != 0) {
2561 DEBUG(2,("Could not set primary group for user %s to "
2562 "%s\n",
2563 pdb_get_username(sampass), grp->gr_name));
2564 return False;
2567 return True;
2571 /*******************************************************************
2572 set_user_info_20
2573 ********************************************************************/
2575 static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, SAM_ACCOUNT *pwd)
2577 if (id20 == NULL) {
2578 DEBUG(5, ("set_user_info_20: NULL id20\n"));
2579 return False;
2582 copy_id20_to_sam_passwd(pwd, id20);
2584 /* write the change out */
2585 if(!pdb_update_sam_account(pwd)) {
2586 pdb_free_sam(&pwd);
2587 return False;
2590 pdb_free_sam(&pwd);
2592 return True;
2594 /*******************************************************************
2595 set_user_info_21
2596 ********************************************************************/
2598 static BOOL set_user_info_21(SAM_USER_INFO_21 *id21, SAM_ACCOUNT *pwd)
2601 if (id21 == NULL) {
2602 DEBUG(5, ("set_user_info_21: NULL id21\n"));
2603 return False;
2606 copy_id21_to_sam_passwd(pwd, id21);
2609 * The funny part about the previous two calls is
2610 * that pwd still has the password hashes from the
2611 * passdb entry. These have not been updated from
2612 * id21. I don't know if they need to be set. --jerry
2615 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
2616 set_unix_primary_group(pwd);
2618 /* write the change out */
2619 if(!pdb_update_sam_account(pwd)) {
2620 pdb_free_sam(&pwd);
2621 return False;
2624 pdb_free_sam(&pwd);
2626 return True;
2629 /*******************************************************************
2630 set_user_info_23
2631 ********************************************************************/
2633 static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, SAM_ACCOUNT *pwd)
2635 pstring plaintext_buf;
2636 uint32 len;
2637 uint16 acct_ctrl;
2639 if (id23 == NULL) {
2640 DEBUG(5, ("set_user_info_23: NULL id23\n"));
2641 return False;
2644 DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
2645 pdb_get_username(pwd)));
2647 acct_ctrl = pdb_get_acct_ctrl(pwd);
2649 if (!decode_pw_buffer(id23->pass, plaintext_buf, 256, &len, STR_UNICODE)) {
2650 pdb_free_sam(&pwd);
2651 return False;
2654 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
2655 pdb_free_sam(&pwd);
2656 return False;
2659 copy_id23_to_sam_passwd(pwd, id23);
2661 /* if it's a trust account, don't update /etc/passwd */
2662 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
2663 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
2664 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
2665 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
2666 } else {
2667 /* update the UNIX password */
2668 if (lp_unix_password_sync() ) {
2669 struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
2670 if (!passwd) {
2671 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
2674 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
2675 pdb_free_sam(&pwd);
2676 return False;
2681 ZERO_STRUCT(plaintext_buf);
2683 if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
2684 set_unix_primary_group(pwd);
2686 if(!pdb_update_sam_account(pwd)) {
2687 pdb_free_sam(&pwd);
2688 return False;
2691 pdb_free_sam(&pwd);
2693 return True;
2696 /*******************************************************************
2697 set_user_info_pw
2698 ********************************************************************/
2700 static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
2702 uint32 len;
2703 pstring plaintext_buf;
2704 uint16 acct_ctrl;
2706 DEBUG(5, ("Attempting administrator password change for user %s\n",
2707 pdb_get_username(pwd)));
2709 acct_ctrl = pdb_get_acct_ctrl(pwd);
2711 ZERO_STRUCT(plaintext_buf);
2713 if (!decode_pw_buffer(pass, plaintext_buf, 256, &len, STR_UNICODE)) {
2714 pdb_free_sam(&pwd);
2715 return False;
2718 if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
2719 pdb_free_sam(&pwd);
2720 return False;
2723 /* if it's a trust account, don't update /etc/passwd */
2724 if ( ( (acct_ctrl & ACB_DOMTRUST) == ACB_DOMTRUST ) ||
2725 ( (acct_ctrl & ACB_WSTRUST) == ACB_WSTRUST) ||
2726 ( (acct_ctrl & ACB_SVRTRUST) == ACB_SVRTRUST) ) {
2727 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
2728 } else {
2729 /* update the UNIX password */
2730 if (lp_unix_password_sync()) {
2731 struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
2732 if (!passwd) {
2733 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
2736 if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
2737 pdb_free_sam(&pwd);
2738 return False;
2743 ZERO_STRUCT(plaintext_buf);
2745 DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
2747 /* update the SAMBA password */
2748 if(!pdb_update_sam_account(pwd)) {
2749 pdb_free_sam(&pwd);
2750 return False;
2753 pdb_free_sam(&pwd);
2755 return True;
2758 /*******************************************************************
2759 samr_reply_set_userinfo
2760 ********************************************************************/
2762 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
2764 SAM_ACCOUNT *pwd = NULL;
2765 DOM_SID sid;
2766 POLICY_HND *pol = &q_u->pol;
2767 uint16 switch_value = q_u->switch_value;
2768 SAM_USERINFO_CTR *ctr = q_u->ctr;
2769 uint32 acc_granted;
2770 uint32 acc_required;
2771 BOOL ret;
2772 BOOL has_enough_rights = False;
2773 uint32 acb_info;
2775 DEBUG(5, ("_samr_set_userinfo: %d\n", __LINE__));
2777 r_u->status = NT_STATUS_OK;
2779 /* find the policy handle. open a policy on it. */
2780 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted))
2781 return NT_STATUS_INVALID_HANDLE;
2783 /* observed when joining an XP client to a Samba domain */
2785 acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
2787 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo"))) {
2788 return r_u->status;
2791 DEBUG(5, ("_samr_set_userinfo: sid:%s, level:%d\n", sid_string_static(&sid), switch_value));
2793 if (ctr == NULL) {
2794 DEBUG(5, ("_samr_set_userinfo: NULL info level\n"));
2795 return NT_STATUS_INVALID_INFO_CLASS;
2798 pdb_init_sam(&pwd);
2800 become_root();
2801 ret = pdb_getsampwsid(pwd, &sid);
2802 unbecome_root();
2804 if ( !ret ) {
2805 pdb_free_sam(&pwd);
2806 return NT_STATUS_NO_SUCH_USER;
2809 /* deal with machine password changes differently from userinfo changes */
2810 /* check to see if we have the sufficient rights */
2812 acb_info = pdb_get_acct_ctrl(pwd);
2813 if ( acb_info & ACB_WSTRUST )
2814 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
2815 else if ( acb_info & ACB_NORMAL )
2816 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
2817 else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
2818 if ( lp_enable_privileges() )
2819 has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
2822 DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
2823 p->pipe_user_name, has_enough_rights ? "" : " not"));
2825 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
2827 if ( has_enough_rights )
2828 become_root();
2830 /* ok! user info levels (lots: see MSDEV help), off we go... */
2832 switch (switch_value) {
2833 case 18:
2834 if (!set_user_info_18(ctr->info.id18, pwd))
2835 r_u->status = NT_STATUS_ACCESS_DENIED;
2836 break;
2838 case 24:
2839 if (!p->session_key.length) {
2840 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
2842 SamOEMhashBlob(ctr->info.id24->pass, 516, &p->session_key);
2844 dump_data(100, (char *)ctr->info.id24->pass, 516);
2846 if (!set_user_info_pw(ctr->info.id24->pass, pwd))
2847 r_u->status = NT_STATUS_ACCESS_DENIED;
2848 break;
2850 case 25:
2851 #if 0
2853 * Currently we don't really know how to unmarshall
2854 * the level 25 struct, and the password encryption
2855 * is different. This is a placeholder for when we
2856 * do understand it. In the meantime just return INVALID
2857 * info level and W2K SP2 drops down to level 23... JRA.
2860 if (!p->session_key.length) {
2861 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
2863 SamOEMhashBlob(ctr->info.id25->pass, 532, &p->session_key);
2865 dump_data(100, (char *)ctr->info.id25->pass, 532);
2867 if (!set_user_info_pw(ctr->info.id25->pass, &sid))
2868 r_u->status = NT_STATUS_ACCESS_DENIED;
2869 break;
2870 #endif
2871 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
2872 break;
2874 case 23:
2875 if (!p->session_key.length) {
2876 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
2878 SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
2880 dump_data(100, (char *)ctr->info.id23->pass, 516);
2882 if (!set_user_info_23(ctr->info.id23, pwd))
2883 r_u->status = NT_STATUS_ACCESS_DENIED;
2884 break;
2886 default:
2887 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
2891 if ( has_enough_rights )
2892 unbecome_root();
2894 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
2896 return r_u->status;
2899 /*******************************************************************
2900 samr_reply_set_userinfo2
2901 ********************************************************************/
2903 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
2905 SAM_ACCOUNT *pwd = NULL;
2906 DOM_SID sid;
2907 SAM_USERINFO_CTR *ctr = q_u->ctr;
2908 POLICY_HND *pol = &q_u->pol;
2909 uint16 switch_value = q_u->switch_value;
2910 uint32 acc_granted;
2911 uint32 acc_required;
2912 BOOL ret;
2913 BOOL has_enough_rights = False;
2914 uint32 acb_info;
2916 DEBUG(5, ("samr_reply_set_userinfo2: %d\n", __LINE__));
2918 r_u->status = NT_STATUS_OK;
2920 /* find the policy handle. open a policy on it. */
2921 if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted))
2922 return NT_STATUS_INVALID_HANDLE;
2924 /* observed when joining XP client to Samba domain */
2926 acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
2928 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo2"))) {
2929 return r_u->status;
2932 DEBUG(5, ("samr_reply_set_userinfo2: sid:%s\n", sid_string_static(&sid)));
2934 if (ctr == NULL) {
2935 DEBUG(5, ("samr_reply_set_userinfo2: NULL info level\n"));
2936 return NT_STATUS_INVALID_INFO_CLASS;
2939 switch_value=ctr->switch_value;
2941 pdb_init_sam(&pwd);
2943 become_root();
2944 ret = pdb_getsampwsid(pwd, &sid);
2945 unbecome_root();
2947 if ( !ret ) {
2948 pdb_free_sam(&pwd);
2949 return NT_STATUS_NO_SUCH_USER;
2952 acb_info = pdb_get_acct_ctrl(pwd);
2953 if ( acb_info & ACB_WSTRUST )
2954 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
2955 else if ( acb_info & ACB_NORMAL )
2956 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
2957 else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
2958 if ( lp_enable_privileges() )
2959 has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
2962 DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
2963 p->pipe_user_name, has_enough_rights ? "" : " not"));
2965 /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
2967 if ( has_enough_rights )
2968 become_root();
2970 /* ok! user info levels (lots: see MSDEV help), off we go... */
2972 switch (switch_value) {
2973 case 7:
2974 r_u->status = set_user_info_7(ctr->info.id7, pwd);
2975 break;
2976 case 16:
2977 if (!set_user_info_16(ctr->info.id16, pwd))
2978 r_u->status = NT_STATUS_ACCESS_DENIED;
2979 break;
2980 case 18:
2981 /* Used by AS/U JRA. */
2982 if (!set_user_info_18(ctr->info.id18, pwd))
2983 r_u->status = NT_STATUS_ACCESS_DENIED;
2984 break;
2985 case 20:
2986 if (!set_user_info_20(ctr->info.id20, pwd))
2987 r_u->status = NT_STATUS_ACCESS_DENIED;
2988 break;
2989 case 21:
2990 if (!set_user_info_21(ctr->info.id21, pwd))
2991 return NT_STATUS_ACCESS_DENIED;
2992 break;
2993 default:
2994 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
2997 if ( has_enough_rights )
2998 unbecome_root();
3000 /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3002 return r_u->status;
3005 /*********************************************************************
3006 _samr_query_aliasmem
3007 *********************************************************************/
3009 NTSTATUS _samr_query_useraliases(pipes_struct *p, SAMR_Q_QUERY_USERALIASES *q_u, SAMR_R_QUERY_USERALIASES *r_u)
3011 size_t num_alias_rids;
3012 uint32 *alias_rids;
3013 struct samr_info *info = NULL;
3014 size_t i;
3016 NTSTATUS ntstatus1;
3017 NTSTATUS ntstatus2;
3019 DOM_SID *members;
3020 BOOL res;
3022 r_u->status = NT_STATUS_OK;
3024 DEBUG(5,("_samr_query_useraliases: %d\n", __LINE__));
3026 /* find the policy handle. open a policy on it. */
3027 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
3028 return NT_STATUS_INVALID_HANDLE;
3030 ntstatus1 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM, "_samr_query_useraliases");
3031 ntstatus2 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_query_useraliases");
3033 if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
3034 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
3035 !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
3036 return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
3040 if (!sid_check_is_domain(&info->sid) &&
3041 !sid_check_is_builtin(&info->sid))
3042 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3044 members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, q_u->num_sids1);
3046 if (members == NULL)
3047 return NT_STATUS_NO_MEMORY;
3049 for (i=0; i<q_u->num_sids1; i++)
3050 sid_copy(&members[i], &q_u->sid[i].sid);
3052 alias_rids = NULL;
3053 num_alias_rids = 0;
3055 become_root();
3056 res = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
3057 q_u->num_sids1,
3058 &alias_rids, &num_alias_rids);
3059 unbecome_root();
3061 if (!res)
3062 return NT_STATUS_UNSUCCESSFUL;
3064 init_samr_r_query_useraliases(r_u, num_alias_rids, alias_rids,
3065 NT_STATUS_OK);
3066 return NT_STATUS_OK;
3069 /*********************************************************************
3070 _samr_query_aliasmem
3071 *********************************************************************/
3073 NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_R_QUERY_ALIASMEM *r_u)
3075 size_t i;
3076 size_t num_sids = 0;
3077 DOM_SID2 *sid;
3078 DOM_SID *sids=NULL;
3080 DOM_SID alias_sid;
3082 uint32 acc_granted;
3084 /* find the policy handle. open a policy on it. */
3085 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted))
3086 return NT_STATUS_INVALID_HANDLE;
3088 if (!NT_STATUS_IS_OK(r_u->status =
3089 access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_GET_MEMBERS, "_samr_query_aliasmem"))) {
3090 return r_u->status;
3093 DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3095 if (!pdb_enum_aliasmem(&alias_sid, &sids, &num_sids))
3096 return NT_STATUS_NO_SUCH_ALIAS;
3098 sid = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_SID2, num_sids);
3099 if (num_sids!=0 && sid == NULL) {
3100 SAFE_FREE(sids);
3101 return NT_STATUS_NO_MEMORY;
3104 for (i = 0; i < num_sids; i++) {
3105 init_dom_sid2(&sid[i], &sids[i]);
3108 init_samr_r_query_aliasmem(r_u, num_sids, sid, NT_STATUS_OK);
3110 SAFE_FREE(sids);
3112 return NT_STATUS_OK;
3115 static void add_uid_to_array_unique(uid_t uid, uid_t **uids, int *num)
3117 int i;
3119 for (i=0; i<*num; i++) {
3120 if ((*uids)[i] == uid)
3121 return;
3124 *uids = SMB_REALLOC_ARRAY(*uids, uid_t, *num+1);
3126 if (*uids == NULL)
3127 return;
3129 (*uids)[*num] = uid;
3130 *num += 1;
3134 static BOOL get_memberuids(gid_t gid, uid_t **uids, int *num)
3136 struct group *grp;
3137 char **gr;
3138 struct sys_pwent *userlist, *user;
3140 *uids = NULL;
3141 *num = 0;
3143 /* We only look at our own sam, so don't care about imported stuff */
3145 winbind_off();
3147 if ((grp = getgrgid(gid)) == NULL) {
3148 winbind_on();
3149 return False;
3152 /* Primary group members */
3154 userlist = getpwent_list();
3156 for (user = userlist; user != NULL; user = user->next) {
3157 if (user->pw_gid != gid)
3158 continue;
3159 add_uid_to_array_unique(user->pw_uid, uids, num);
3162 pwent_free(userlist);
3164 /* Secondary group members */
3166 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
3167 struct passwd *pw = getpwnam(*gr);
3169 if (pw == NULL)
3170 continue;
3171 add_uid_to_array_unique(pw->pw_uid, uids, num);
3174 winbind_on();
3176 return True;
3179 /*********************************************************************
3180 _samr_query_groupmem
3181 *********************************************************************/
3183 NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_R_QUERY_GROUPMEM *r_u)
3185 DOM_SID group_sid;
3186 fstring group_sid_str;
3187 size_t i, num_members;
3189 uint32 *rid=NULL;
3190 uint32 *attr=NULL;
3192 uint32 acc_granted;
3194 NTSTATUS result;
3196 /* find the policy handle. open a policy on it. */
3197 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted))
3198 return NT_STATUS_INVALID_HANDLE;
3200 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_GET_MEMBERS, "_samr_query_groupmem"))) {
3201 return r_u->status;
3204 sid_to_string(group_sid_str, &group_sid);
3205 DEBUG(10, ("sid is %s\n", group_sid_str));
3207 if (!sid_check_is_in_our_domain(&group_sid)) {
3208 DEBUG(3, ("sid %s is not in our domain\n", group_sid_str));
3209 return NT_STATUS_NO_SUCH_GROUP;
3212 DEBUG(10, ("lookup on Domain SID\n"));
3214 become_root();
3215 result = pdb_enum_group_members(p->mem_ctx, &group_sid,
3216 &rid, &num_members);
3217 unbecome_root();
3219 if (!NT_STATUS_IS_OK(result))
3220 return result;
3222 attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
3224 if ((num_members!=0) && (rid==NULL))
3225 return NT_STATUS_NO_MEMORY;
3227 for (i=0; i<num_members; i++)
3228 attr[i] = SID_NAME_USER;
3230 init_samr_r_query_groupmem(r_u, num_members, rid, attr, NT_STATUS_OK);
3232 return NT_STATUS_OK;
3235 /*********************************************************************
3236 _samr_add_aliasmem
3237 *********************************************************************/
3239 NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_ADD_ALIASMEM *r_u)
3241 DOM_SID alias_sid;
3242 uint32 acc_granted;
3243 SE_PRIV se_rights;
3244 BOOL can_add_accounts;
3245 BOOL ret;
3248 /* Find the policy handle. Open a policy on it. */
3249 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted))
3250 return NT_STATUS_INVALID_HANDLE;
3252 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_ADD_MEMBER, "_samr_add_aliasmem"))) {
3253 return r_u->status;
3256 DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3258 se_priv_copy( &se_rights, &se_add_users );
3259 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3261 /******** BEGIN SeAddUsers BLOCK *********/
3263 if ( can_add_accounts )
3264 become_root();
3266 ret = pdb_add_aliasmem(&alias_sid, &q_u->sid.sid);
3268 if ( can_add_accounts )
3269 unbecome_root();
3271 /******** END SeAddUsers BLOCK *********/
3273 return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3276 /*********************************************************************
3277 _samr_del_aliasmem
3278 *********************************************************************/
3280 NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DEL_ALIASMEM *r_u)
3282 DOM_SID alias_sid;
3283 uint32 acc_granted;
3284 SE_PRIV se_rights;
3285 BOOL can_add_accounts;
3286 BOOL ret;
3288 /* Find the policy handle. Open a policy on it. */
3289 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted))
3290 return NT_STATUS_INVALID_HANDLE;
3292 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_REMOVE_MEMBER, "_samr_del_aliasmem"))) {
3293 return r_u->status;
3296 DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
3297 sid_string_static(&alias_sid)));
3299 se_priv_copy( &se_rights, &se_add_users );
3300 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3302 /******** BEGIN SeAddUsers BLOCK *********/
3304 if ( can_add_accounts )
3305 become_root();
3307 ret = pdb_del_aliasmem(&alias_sid, &q_u->sid.sid);
3309 if ( can_add_accounts )
3310 unbecome_root();
3312 /******** END SeAddUsers BLOCK *********/
3314 return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3317 /*********************************************************************
3318 _samr_add_groupmem
3319 *********************************************************************/
3321 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
3323 DOM_SID group_sid;
3324 DOM_SID user_sid;
3325 fstring group_sid_str;
3326 uid_t uid;
3327 struct passwd *pwd;
3328 struct group *grp;
3329 fstring grp_name;
3330 GROUP_MAP map;
3331 NTSTATUS ret;
3332 SAM_ACCOUNT *sam_user=NULL;
3333 BOOL check;
3334 uint32 acc_granted;
3335 SE_PRIV se_rights;
3336 BOOL can_add_accounts;
3338 /* Find the policy handle. Open a policy on it. */
3339 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted))
3340 return NT_STATUS_INVALID_HANDLE;
3342 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_ADD_MEMBER, "_samr_add_groupmem"))) {
3343 return r_u->status;
3346 sid_to_string(group_sid_str, &group_sid);
3347 DEBUG(10, ("sid is %s\n", group_sid_str));
3349 if (sid_compare(&group_sid, get_global_sam_sid())<=0)
3350 return NT_STATUS_NO_SUCH_GROUP;
3352 DEBUG(10, ("lookup on Domain SID\n"));
3354 if(!get_domain_group_from_sid(group_sid, &map))
3355 return NT_STATUS_NO_SUCH_GROUP;
3357 sid_copy(&user_sid, get_global_sam_sid());
3358 sid_append_rid(&user_sid, q_u->rid);
3360 ret = pdb_init_sam(&sam_user);
3361 if (!NT_STATUS_IS_OK(ret))
3362 return ret;
3364 check = pdb_getsampwsid(sam_user, &user_sid);
3366 if (check != True) {
3367 pdb_free_sam(&sam_user);
3368 return NT_STATUS_NO_SUCH_USER;
3371 /* check a real user exist before we run the script to add a user to a group */
3372 if (!NT_STATUS_IS_OK(sid_to_uid(pdb_get_user_sid(sam_user), &uid))) {
3373 pdb_free_sam(&sam_user);
3374 return NT_STATUS_NO_SUCH_USER;
3377 pdb_free_sam(&sam_user);
3379 if ((pwd=getpwuid_alloc(uid)) == NULL) {
3380 return NT_STATUS_NO_SUCH_USER;
3383 if ((grp=getgrgid(map.gid)) == NULL) {
3384 passwd_free(&pwd);
3385 return NT_STATUS_NO_SUCH_GROUP;
3388 /* we need to copy the name otherwise it's overloaded in user_in_unix_group_list */
3389 fstrcpy(grp_name, grp->gr_name);
3391 /* if the user is already in the group */
3392 if(user_in_unix_group_list(pwd->pw_name, grp_name)) {
3393 passwd_free(&pwd);
3394 return NT_STATUS_MEMBER_IN_GROUP;
3397 se_priv_copy( &se_rights, &se_add_users );
3398 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3400 /******** BEGIN SeAddUsers BLOCK *********/
3402 if ( can_add_accounts )
3403 become_root();
3406 * ok, the group exist, the user exist, the user is not in the group,
3408 * we can (finally) add it to the group !
3411 smb_add_user_group(grp_name, pwd->pw_name);
3413 if ( can_add_accounts )
3414 unbecome_root();
3416 /******** END SeAddUsers BLOCK *********/
3418 /* check if the user has been added then ... */
3419 if(!user_in_unix_group_list(pwd->pw_name, grp_name)) {
3420 passwd_free(&pwd);
3421 return NT_STATUS_MEMBER_NOT_IN_GROUP; /* don't know what to reply else */
3424 passwd_free(&pwd);
3425 return NT_STATUS_OK;
3428 /*********************************************************************
3429 _samr_del_groupmem
3430 *********************************************************************/
3432 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
3434 DOM_SID group_sid;
3435 DOM_SID user_sid;
3436 SAM_ACCOUNT *sam_pass=NULL;
3437 GROUP_MAP map;
3438 fstring grp_name;
3439 struct group *grp;
3440 uint32 acc_granted;
3441 SE_PRIV se_rights;
3442 BOOL can_add_accounts;
3445 * delete the group member named q_u->rid
3446 * who is a member of the sid associated with the handle
3447 * the rid is a user's rid as the group is a domain group.
3450 /* Find the policy handle. Open a policy on it. */
3451 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted))
3452 return NT_STATUS_INVALID_HANDLE;
3454 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_REMOVE_MEMBER, "_samr_del_groupmem"))) {
3455 return r_u->status;
3458 if (!sid_check_is_in_our_domain(&group_sid))
3459 return NT_STATUS_NO_SUCH_GROUP;
3461 sid_copy(&user_sid, get_global_sam_sid());
3462 sid_append_rid(&user_sid, q_u->rid);
3464 if (!get_domain_group_from_sid(group_sid, &map))
3465 return NT_STATUS_NO_SUCH_GROUP;
3467 if ((grp=getgrgid(map.gid)) == NULL)
3468 return NT_STATUS_NO_SUCH_GROUP;
3470 /* we need to copy the name otherwise it's overloaded in user_in_group_list */
3471 fstrcpy(grp_name, grp->gr_name);
3473 /* check if the user exists before trying to remove it from the group */
3474 pdb_init_sam(&sam_pass);
3475 if (!pdb_getsampwsid(sam_pass, &user_sid)) {
3476 DEBUG(5,("User %s doesn't exist.\n", pdb_get_username(sam_pass)));
3477 pdb_free_sam(&sam_pass);
3478 return NT_STATUS_NO_SUCH_USER;
3481 /* if the user is not in the group */
3482 if (!user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3483 pdb_free_sam(&sam_pass);
3484 return NT_STATUS_MEMBER_NOT_IN_GROUP;
3488 se_priv_copy( &se_rights, &se_add_users );
3489 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3491 /******** BEGIN SeAddUsers BLOCK *********/
3493 if ( can_add_accounts )
3494 become_root();
3496 smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
3498 if ( can_add_accounts )
3499 unbecome_root();
3501 /******** END SeAddUsers BLOCK *********/
3503 /* check if the user has been removed then ... */
3504 if (user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3505 pdb_free_sam(&sam_pass);
3506 return NT_STATUS_ACCESS_DENIED; /* don't know what to reply else */
3509 pdb_free_sam(&sam_pass);
3510 return NT_STATUS_OK;
3514 /****************************************************************************
3515 Delete a UNIX user on demand.
3516 ****************************************************************************/
3518 static int smb_delete_user(const char *unix_user)
3520 pstring del_script;
3521 int ret;
3523 pstrcpy(del_script, lp_deluser_script());
3524 if (! *del_script)
3525 return -1;
3526 all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
3527 ret = smbrun(del_script,NULL);
3528 flush_pwnam_cache();
3529 DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
3531 return ret;
3534 /*********************************************************************
3535 _samr_delete_dom_user
3536 *********************************************************************/
3538 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
3540 DOM_SID user_sid;
3541 SAM_ACCOUNT *sam_pass=NULL;
3542 uint32 acc_granted;
3543 BOOL can_add_accounts;
3544 BOOL ret;
3546 DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
3548 /* Find the policy handle. Open a policy on it. */
3549 if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &user_sid, &acc_granted))
3550 return NT_STATUS_INVALID_HANDLE;
3552 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_user"))) {
3553 return r_u->status;
3556 if (!sid_check_is_in_our_domain(&user_sid))
3557 return NT_STATUS_CANNOT_DELETE;
3559 /* check if the user exists before trying to delete */
3560 pdb_init_sam(&sam_pass);
3561 if(!pdb_getsampwsid(sam_pass, &user_sid)) {
3562 DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n",
3563 sid_string_static(&user_sid)));
3564 pdb_free_sam(&sam_pass);
3565 return NT_STATUS_NO_SUCH_USER;
3568 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3570 /******** BEGIN SeAddUsers BLOCK *********/
3572 if ( can_add_accounts )
3573 become_root();
3575 /* First delete the samba side....
3576 code is order to prevent unnecessary returns out of the admin
3577 block of code */
3579 if ( (ret = pdb_delete_sam_account(sam_pass)) == True ) {
3581 * Now delete the unix side ....
3582 * note: we don't check if the delete really happened
3583 * as the script is not necessary present
3584 * and maybe the sysadmin doesn't want to delete the unix side
3586 smb_delete_user( pdb_get_username(sam_pass) );
3589 if ( can_add_accounts )
3590 unbecome_root();
3592 /******** END SeAddUsers BLOCK *********/
3594 if ( !ret ) {
3595 DEBUG(5,("_samr_delete_dom_user:Failed to delete entry for user %s.\n", pdb_get_username(sam_pass)));
3596 pdb_free_sam(&sam_pass);
3597 return NT_STATUS_CANNOT_DELETE;
3601 pdb_free_sam(&sam_pass);
3603 if (!close_policy_hnd(p, &q_u->user_pol))
3604 return NT_STATUS_OBJECT_NAME_INVALID;
3606 return NT_STATUS_OK;
3609 /*********************************************************************
3610 _samr_delete_dom_group
3611 *********************************************************************/
3613 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
3615 DOM_SID group_sid;
3616 DOM_SID dom_sid;
3617 uint32 group_rid;
3618 fstring group_sid_str;
3619 gid_t gid;
3620 struct group *grp;
3621 GROUP_MAP map;
3622 uint32 acc_granted;
3623 SE_PRIV se_rights;
3624 BOOL can_add_accounts;
3625 BOOL ret;
3627 DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
3629 /* Find the policy handle. Open a policy on it. */
3630 if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted))
3631 return NT_STATUS_INVALID_HANDLE;
3633 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_group"))) {
3634 return r_u->status;
3637 sid_copy(&dom_sid, &group_sid);
3638 sid_to_string(group_sid_str, &dom_sid);
3639 sid_split_rid(&dom_sid, &group_rid);
3641 DEBUG(10, ("sid is %s\n", group_sid_str));
3643 /* we check if it's our SID before deleting */
3644 if (!sid_equal(&dom_sid, get_global_sam_sid()))
3645 return NT_STATUS_NO_SUCH_GROUP;
3647 DEBUG(10, ("lookup on Domain SID\n"));
3649 if(!get_domain_group_from_sid(group_sid, &map))
3650 return NT_STATUS_NO_SUCH_GROUP;
3652 gid=map.gid;
3654 /* check if group really exists */
3655 if ( (grp=getgrgid(gid)) == NULL)
3656 return NT_STATUS_NO_SUCH_GROUP;
3658 se_priv_copy( &se_rights, &se_add_users );
3659 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3661 /******** BEGIN SeAddUsers BLOCK *********/
3663 if ( can_add_accounts )
3664 become_root();
3666 /* delete mapping first */
3668 if ( (ret = pdb_delete_group_mapping_entry(group_sid)) == True ) {
3669 smb_delete_group( grp->gr_name );
3672 if ( can_add_accounts )
3673 unbecome_root();
3675 /******** END SeAddUsers BLOCK *********/
3677 if ( !ret ) {
3678 DEBUG(5,("_samr_delete_dom_group: Failed to delete mapping entry for group %s.\n",
3679 group_sid_str));
3680 return NT_STATUS_ACCESS_DENIED;
3683 /* don't check that the unix group has been deleted. Work like
3684 _samr_delet_dom_user() */
3686 if (!close_policy_hnd(p, &q_u->group_pol))
3687 return NT_STATUS_OBJECT_NAME_INVALID;
3689 return NT_STATUS_OK;
3692 /*********************************************************************
3693 _samr_delete_dom_alias
3694 *********************************************************************/
3696 NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, SAMR_R_DELETE_DOM_ALIAS *r_u)
3698 DOM_SID alias_sid;
3699 uint32 acc_granted;
3700 SE_PRIV se_rights;
3701 BOOL can_add_accounts;
3702 BOOL ret;
3704 DEBUG(5, ("_samr_delete_dom_alias: %d\n", __LINE__));
3706 /* Find the policy handle. Open a policy on it. */
3707 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted))
3708 return NT_STATUS_INVALID_HANDLE;
3710 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_alias"))) {
3711 return r_u->status;
3714 DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3716 if (!sid_check_is_in_our_domain(&alias_sid))
3717 return NT_STATUS_NO_SUCH_ALIAS;
3719 DEBUG(10, ("lookup on Local SID\n"));
3721 se_priv_copy( &se_rights, &se_add_users );
3722 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3724 /******** BEGIN SeAddUsers BLOCK *********/
3726 if ( can_add_accounts )
3727 become_root();
3729 /* Have passdb delete the alias */
3730 ret = pdb_delete_alias(&alias_sid);
3732 if ( can_add_accounts )
3733 unbecome_root();
3735 /******** END SeAddUsers BLOCK *********/
3737 if ( !ret )
3738 return NT_STATUS_ACCESS_DENIED;
3740 if (!close_policy_hnd(p, &q_u->alias_pol))
3741 return NT_STATUS_OBJECT_NAME_INVALID;
3743 return NT_STATUS_OK;
3746 /*********************************************************************
3747 _samr_create_dom_group
3748 *********************************************************************/
3750 NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, SAMR_R_CREATE_DOM_GROUP *r_u)
3752 DOM_SID dom_sid;
3753 DOM_SID info_sid;
3754 fstring name;
3755 fstring sid_string;
3756 struct group *grp;
3757 struct samr_info *info;
3758 uint32 acc_granted;
3759 gid_t gid;
3760 SE_PRIV se_rights;
3761 BOOL can_add_accounts;
3762 NTSTATUS result;
3764 /* Find the policy handle. Open a policy on it. */
3765 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &dom_sid, &acc_granted))
3766 return NT_STATUS_INVALID_HANDLE;
3768 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_GROUP, "_samr_create_dom_group"))) {
3769 return r_u->status;
3772 if (!sid_equal(&dom_sid, get_global_sam_sid()))
3773 return NT_STATUS_ACCESS_DENIED;
3775 unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
3777 /* check if group already exist */
3778 if ((grp=getgrnam(name)) != NULL)
3779 return NT_STATUS_GROUP_EXISTS;
3781 se_priv_copy( &se_rights, &se_add_users );
3782 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3784 /******** BEGIN SeAddUsers BLOCK *********/
3786 if ( can_add_accounts )
3787 become_root();
3789 /* check that we successfully create the UNIX group */
3791 result = NT_STATUS_ACCESS_DENIED;
3792 if ( (smb_create_group(name, &gid) == 0) && ((grp=getgrgid(gid)) != NULL) ) {
3794 /* so far, so good */
3796 result = NT_STATUS_OK;
3798 r_u->rid = pdb_gid_to_group_rid( grp->gr_gid );
3800 /* add the group to the mapping table */
3802 sid_copy( &info_sid, get_global_sam_sid() );
3803 sid_append_rid( &info_sid, r_u->rid );
3804 sid_to_string( sid_string, &info_sid );
3806 /* reset the error code if we fail to add the mapping entry */
3808 if ( !add_initial_entry(grp->gr_gid, sid_string, SID_NAME_DOM_GRP, name, NULL) )
3809 result = NT_STATUS_ACCESS_DENIED;
3812 if ( can_add_accounts )
3813 unbecome_root();
3815 /******** END SeAddUsers BLOCK *********/
3817 /* check if we should bail out here */
3819 if ( !NT_STATUS_IS_OK(result) )
3820 return result;
3822 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3823 return NT_STATUS_NO_MEMORY;
3826 /* they created it; let the user do what he wants with it */
3828 info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
3830 /* get a (unique) handle. open a policy on it. */
3831 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
3832 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3834 return NT_STATUS_OK;
3837 /*********************************************************************
3838 _samr_create_dom_alias
3839 *********************************************************************/
3841 NTSTATUS _samr_create_dom_alias(pipes_struct *p, SAMR_Q_CREATE_DOM_ALIAS *q_u, SAMR_R_CREATE_DOM_ALIAS *r_u)
3843 DOM_SID dom_sid;
3844 DOM_SID info_sid;
3845 fstring name;
3846 struct samr_info *info;
3847 uint32 acc_granted;
3848 gid_t gid;
3849 NTSTATUS result;
3850 SE_PRIV se_rights;
3851 BOOL can_add_accounts;
3853 /* Find the policy handle. Open a policy on it. */
3854 if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &dom_sid, &acc_granted))
3855 return NT_STATUS_INVALID_HANDLE;
3857 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_ALIAS, "_samr_create_alias"))) {
3858 return r_u->status;
3861 if (!sid_equal(&dom_sid, get_global_sam_sid()))
3862 return NT_STATUS_ACCESS_DENIED;
3864 unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
3866 se_priv_copy( &se_rights, &se_add_users );
3867 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3869 /******** BEGIN SeAddUsers BLOCK *********/
3871 if ( can_add_accounts )
3872 become_root();
3874 /* Have passdb create the alias */
3875 result = pdb_create_alias(name, &r_u->rid);
3877 if ( can_add_accounts )
3878 unbecome_root();
3880 /******** END SeAddUsers BLOCK *********/
3882 if (!NT_STATUS_IS_OK(result))
3883 return result;
3885 sid_copy(&info_sid, get_global_sam_sid());
3886 sid_append_rid(&info_sid, r_u->rid);
3888 if (!NT_STATUS_IS_OK(sid_to_gid(&info_sid, &gid)))
3889 return NT_STATUS_ACCESS_DENIED;
3891 /* check if the group has been successfully created */
3892 if ( getgrgid(gid) == NULL )
3893 return NT_STATUS_ACCESS_DENIED;
3895 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3896 return NT_STATUS_NO_MEMORY;
3898 /* they created it; let the user do what he wants with it */
3900 info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
3902 /* get a (unique) handle. open a policy on it. */
3903 if (!create_policy_hnd(p, &r_u->alias_pol, free_samr_info, (void *)info))
3904 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3906 return NT_STATUS_OK;
3909 /*********************************************************************
3910 _samr_query_groupinfo
3912 sends the name/comment pair of a domain group
3913 level 1 send also the number of users of that group
3914 *********************************************************************/
3916 NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAMR_R_QUERY_GROUPINFO *r_u)
3918 DOM_SID group_sid;
3919 GROUP_MAP map;
3920 DOM_SID *sids=NULL;
3921 uid_t *uids;
3922 int num=0;
3923 GROUP_INFO_CTR *ctr;
3924 uint32 acc_granted;
3925 BOOL ret;
3927 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted))
3928 return NT_STATUS_INVALID_HANDLE;
3930 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_LOOKUP_INFO, "_samr_query_groupinfo"))) {
3931 return r_u->status;
3934 become_root();
3935 ret = get_domain_group_from_sid(group_sid, &map);
3936 unbecome_root();
3937 if (!ret)
3938 return NT_STATUS_INVALID_HANDLE;
3940 ctr=TALLOC_ZERO_P(p->mem_ctx, GROUP_INFO_CTR);
3941 if (ctr==NULL)
3942 return NT_STATUS_NO_MEMORY;
3944 switch (q_u->switch_level) {
3945 case 1:
3946 ctr->switch_value1 = 1;
3947 if(!get_memberuids(map.gid, &uids, &num))
3948 return NT_STATUS_NO_SUCH_GROUP;
3949 SAFE_FREE(uids);
3950 init_samr_group_info1(&ctr->group.info1, map.nt_name, map.comment, num);
3951 SAFE_FREE(sids);
3952 break;
3953 case 3:
3954 ctr->switch_value1 = 3;
3955 init_samr_group_info3(&ctr->group.info3);
3956 break;
3957 case 4:
3958 ctr->switch_value1 = 4;
3959 init_samr_group_info4(&ctr->group.info4, map.comment);
3960 break;
3961 default:
3962 return NT_STATUS_INVALID_INFO_CLASS;
3965 init_samr_r_query_groupinfo(r_u, ctr, NT_STATUS_OK);
3967 return NT_STATUS_OK;
3970 /*********************************************************************
3971 _samr_set_groupinfo
3973 update a domain group's comment.
3974 *********************************************************************/
3976 NTSTATUS _samr_set_groupinfo(pipes_struct *p, SAMR_Q_SET_GROUPINFO *q_u, SAMR_R_SET_GROUPINFO *r_u)
3978 DOM_SID group_sid;
3979 GROUP_MAP map;
3980 GROUP_INFO_CTR *ctr;
3981 uint32 acc_granted;
3982 BOOL ret;
3983 BOOL can_mod_accounts;
3985 if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted))
3986 return NT_STATUS_INVALID_HANDLE;
3988 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_SET_INFO, "_samr_set_groupinfo"))) {
3989 return r_u->status;
3992 if (!get_domain_group_from_sid(group_sid, &map))
3993 return NT_STATUS_NO_SUCH_GROUP;
3995 ctr=q_u->ctr;
3997 switch (ctr->switch_value1) {
3998 case 1:
3999 unistr2_to_ascii(map.comment, &(ctr->group.info1.uni_acct_desc), sizeof(map.comment)-1);
4000 break;
4001 case 4:
4002 unistr2_to_ascii(map.comment, &(ctr->group.info4.uni_acct_desc), sizeof(map.comment)-1);
4003 break;
4004 default:
4005 return NT_STATUS_INVALID_INFO_CLASS;
4008 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4010 /******** BEGIN SeAddUsers BLOCK *********/
4012 if ( can_mod_accounts )
4013 become_root();
4015 ret = pdb_update_group_mapping_entry(&map);
4017 if ( can_mod_accounts )
4018 unbecome_root();
4020 /******** End SeAddUsers BLOCK *********/
4022 return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4025 /*********************************************************************
4026 _samr_set_aliasinfo
4028 update an alias's comment.
4029 *********************************************************************/
4031 NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_SET_ALIASINFO *r_u)
4033 DOM_SID group_sid;
4034 struct acct_info info;
4035 ALIAS_INFO_CTR *ctr;
4036 uint32 acc_granted;
4037 BOOL ret;
4038 BOOL can_mod_accounts;
4040 if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &group_sid, &acc_granted))
4041 return NT_STATUS_INVALID_HANDLE;
4043 if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_SET_INFO, "_samr_set_aliasinfo"))) {
4044 return r_u->status;
4047 ctr=&q_u->ctr;
4049 switch (ctr->level) {
4050 case 3:
4051 if ( ctr->alias.info3.description.string ) {
4052 unistr2_to_ascii( info.acct_desc,
4053 ctr->alias.info3.description.string,
4054 sizeof(info.acct_desc)-1 );
4056 break;
4057 default:
4058 return NT_STATUS_INVALID_INFO_CLASS;
4061 can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4063 /******** BEGIN SeAddUsers BLOCK *********/
4065 if ( can_mod_accounts )
4066 become_root();
4068 ret = pdb_set_aliasinfo( &group_sid, &info );
4070 if ( can_mod_accounts )
4071 unbecome_root();
4073 /******** End SeAddUsers BLOCK *********/
4075 return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4078 /*********************************************************************
4079 _samr_get_dom_pwinfo
4080 *********************************************************************/
4082 NTSTATUS _samr_get_dom_pwinfo(pipes_struct *p, SAMR_Q_GET_DOM_PWINFO *q_u, SAMR_R_GET_DOM_PWINFO *r_u)
4084 /* Perform access check. Since this rpc does not require a
4085 policy handle it will not be caught by the access checks on
4086 SAMR_CONNECT or SAMR_CONNECT_ANON. */
4088 if (!pipe_access_check(p)) {
4089 DEBUG(3, ("access denied to samr_get_dom_pwinfo\n"));
4090 r_u->status = NT_STATUS_ACCESS_DENIED;
4091 return r_u->status;
4094 /* Actually, returning zeros here works quite well :-). */
4096 return NT_STATUS_OK;
4099 /*********************************************************************
4100 _samr_open_group
4101 *********************************************************************/
4103 NTSTATUS _samr_open_group(pipes_struct *p, SAMR_Q_OPEN_GROUP *q_u, SAMR_R_OPEN_GROUP *r_u)
4105 DOM_SID sid;
4106 DOM_SID info_sid;
4107 GROUP_MAP map;
4108 struct samr_info *info;
4109 SEC_DESC *psd = NULL;
4110 uint32 acc_granted;
4111 uint32 des_access = q_u->access_mask;
4112 size_t sd_size;
4113 NTSTATUS status;
4114 fstring sid_string;
4115 BOOL ret;
4116 SE_PRIV se_rights;
4118 if (!get_lsa_policy_samr_sid(p, &q_u->domain_pol, &sid, &acc_granted))
4119 return NT_STATUS_INVALID_HANDLE;
4121 status = access_check_samr_function(acc_granted,
4122 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_group");
4124 if ( !NT_STATUS_IS_OK(status) )
4125 return status;
4127 /*check if access can be granted as requested by client. */
4128 make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
4129 se_map_generic(&des_access,&grp_generic_mapping);
4131 se_priv_copy( &se_rights, &se_add_users );
4133 status = access_check_samr_object(psd, p->pipe_user.nt_user_token,
4134 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access,
4135 &acc_granted, "_samr_open_group");
4137 if ( !NT_STATUS_IS_OK(status) )
4138 return status;
4140 /* this should not be hard-coded like this */
4142 if (!sid_equal(&sid, get_global_sam_sid()))
4143 return NT_STATUS_ACCESS_DENIED;
4145 sid_copy(&info_sid, get_global_sam_sid());
4146 sid_append_rid(&info_sid, q_u->rid_group);
4147 sid_to_string(sid_string, &info_sid);
4149 if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4150 return NT_STATUS_NO_MEMORY;
4152 info->acc_granted = acc_granted;
4154 DEBUG(10, ("_samr_open_group:Opening SID: %s\n", sid_string));
4156 /* check if that group really exists */
4157 become_root();
4158 ret = get_domain_group_from_sid(info->sid, &map);
4159 unbecome_root();
4160 if (!ret)
4161 return NT_STATUS_NO_SUCH_GROUP;
4163 /* get a (unique) handle. open a policy on it. */
4164 if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4165 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4167 return NT_STATUS_OK;
4170 /*********************************************************************
4171 _samr_remove_sid_foreign_domain
4172 *********************************************************************/
4174 NTSTATUS _samr_remove_sid_foreign_domain(pipes_struct *p,
4175 SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN *q_u,
4176 SAMR_R_REMOVE_SID_FOREIGN_DOMAIN *r_u)
4178 DOM_SID delete_sid, domain_sid;
4179 uint32 acc_granted;
4180 NTSTATUS result;
4182 sid_copy( &delete_sid, &q_u->sid.sid );
4184 DEBUG(5,("_samr_remove_sid_foreign_domain: removing SID [%s]\n",
4185 sid_string_static(&delete_sid)));
4187 /* Find the policy handle. Open a policy on it. */
4189 if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &domain_sid,
4190 &acc_granted))
4191 return NT_STATUS_INVALID_HANDLE;
4193 result = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS,
4194 "_samr_remove_sid_foreign_domain");
4196 if (!NT_STATUS_IS_OK(result))
4197 return result;
4199 DEBUG(8, ("_samr_remove_sid_foreign_domain:sid is %s\n",
4200 sid_string_static(&domain_sid)));
4202 /* we can only delete a user from a group since we don't have
4203 nested groups anyways. So in the latter case, just say OK */
4205 /* TODO: The above comment nowadays is bogus. Since we have nested
4206 * groups now, and aliases members are never reported out of the unix
4207 * group membership, the "just say OK" makes this call a no-op. For
4208 * us. This needs fixing however. */
4210 /* I've only ever seen this in the wild when deleting a user from
4211 * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
4212 * is the user about to be deleted. I very much suspect this is the
4213 * only application of this call. To verify this, let people report
4214 * other cases. */
4216 if (!sid_check_is_builtin(&domain_sid)) {
4217 DEBUG(1,("_samr_remove_sid_foreign_domain: domain_sid = %s, "
4218 "global_sam_sid() = %s\n",
4219 sid_string_static(&domain_sid),
4220 sid_string_static(get_global_sam_sid())));
4221 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
4222 return NT_STATUS_OK;
4226 result = NT_STATUS_OK;
4228 return result;
4231 /*******************************************************************
4232 _samr_unknown_2e
4233 ********************************************************************/
4235 NTSTATUS _samr_unknown_2e(pipes_struct *p, SAMR_Q_UNKNOWN_2E *q_u, SAMR_R_UNKNOWN_2E *r_u)
4237 struct samr_info *info = NULL;
4238 SAM_UNK_CTR *ctr;
4239 uint32 min_pass_len,pass_hist,flag;
4240 time_t u_expire, u_min_age;
4241 NTTIME nt_expire, nt_min_age;
4243 time_t u_lock_duration, u_reset_time;
4244 NTTIME nt_lock_duration, nt_reset_time;
4245 uint32 lockout;
4247 time_t u_logout;
4248 NTTIME nt_logout;
4250 uint32 num_users=0, num_groups=0, num_aliases=0;
4252 uint32 account_policy_temp;
4254 time_t seq_num;
4255 uint32 server_role;
4257 if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL)
4258 return NT_STATUS_NO_MEMORY;
4260 ZERO_STRUCTP(ctr);
4262 r_u->status = NT_STATUS_OK;
4264 DEBUG(5,("_samr_unknown_2e: %d\n", __LINE__));
4266 /* find the policy handle. open a policy on it. */
4267 if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
4268 return NT_STATUS_INVALID_HANDLE;
4270 switch (q_u->switch_value) {
4271 case 0x01:
4272 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
4273 min_pass_len = account_policy_temp;
4275 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
4276 pass_hist = account_policy_temp;
4278 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
4279 flag = account_policy_temp;
4281 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
4282 u_expire = account_policy_temp;
4284 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
4285 u_min_age = account_policy_temp;
4287 unix_to_nt_time_abs(&nt_expire, u_expire);
4288 unix_to_nt_time_abs(&nt_min_age, u_min_age);
4290 init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist,
4291 flag, nt_expire, nt_min_age);
4292 break;
4293 case 0x02:
4294 become_root();
4295 num_users = count_sam_users(&info->disp_info,
4296 ACB_NORMAL);
4297 num_groups = count_sam_groups(&info->disp_info);
4298 unbecome_root();
4300 free_samr_db(info);
4302 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4303 u_logout = account_policy_temp;
4305 unix_to_nt_time_abs(&nt_logout, u_logout);
4307 if (!pdb_get_seq_num(&seq_num))
4308 seq_num = time(NULL);
4310 server_role = ROLE_DOMAIN_PDC;
4311 if (lp_server_role() == ROLE_DOMAIN_BDC)
4312 server_role = ROLE_DOMAIN_BDC;
4314 init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num,
4315 num_users, num_groups, num_aliases, nt_logout, server_role);
4316 break;
4317 case 0x03:
4318 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4319 u_logout = account_policy_temp;
4321 unix_to_nt_time_abs(&nt_logout, u_logout);
4323 init_unk_info3(&ctr->info.inf3, nt_logout);
4324 break;
4325 case 0x05:
4326 init_unk_info5(&ctr->info.inf5, global_myname());
4327 break;
4328 case 0x06:
4329 init_unk_info6(&ctr->info.inf6);
4330 break;
4331 case 0x07:
4332 server_role = ROLE_DOMAIN_PDC;
4333 if (lp_server_role() == ROLE_DOMAIN_BDC)
4334 server_role = ROLE_DOMAIN_BDC;
4336 init_unk_info7(&ctr->info.inf7, server_role);
4337 break;
4338 case 0x08:
4339 if (!pdb_get_seq_num(&seq_num))
4340 seq_num = time(NULL);
4342 init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
4343 break;
4344 case 0x0c:
4345 pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
4346 u_lock_duration = account_policy_temp;
4347 if (u_lock_duration != -1)
4348 u_lock_duration *= 60;
4350 pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
4351 u_reset_time = account_policy_temp * 60;
4353 pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
4354 lockout = account_policy_temp;
4356 unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
4357 unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
4359 init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
4360 break;
4361 default:
4362 return NT_STATUS_INVALID_INFO_CLASS;
4365 init_samr_r_samr_unknown_2e(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
4367 DEBUG(5,("_samr_unknown_2e: %d\n", __LINE__));
4369 return r_u->status;
4372 /*******************************************************************
4373 _samr_
4374 ********************************************************************/
4376 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)
4378 time_t u_expire, u_min_age;
4379 time_t u_logout;
4380 time_t u_lock_duration, u_reset_time;
4382 r_u->status = NT_STATUS_OK;
4384 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4386 /* find the policy handle. open a policy on it. */
4387 if (!find_policy_by_hnd(p, &q_u->domain_pol, NULL))
4388 return NT_STATUS_INVALID_HANDLE;
4390 DEBUG(5,("_samr_set_dom_info: switch_value: %d\n", q_u->switch_value));
4392 switch (q_u->switch_value) {
4393 case 0x01:
4394 u_expire=nt_time_to_unix_abs(&q_u->ctr->info.inf1.expire);
4395 u_min_age=nt_time_to_unix_abs(&q_u->ctr->info.inf1.min_passwordage);
4397 pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)q_u->ctr->info.inf1.min_length_password);
4398 pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)q_u->ctr->info.inf1.password_history);
4399 pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)q_u->ctr->info.inf1.flag);
4400 pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
4401 pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
4402 break;
4403 case 0x02:
4404 break;
4405 case 0x03:
4406 u_logout=nt_time_to_unix_abs(&q_u->ctr->info.inf3.logout);
4407 pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
4408 break;
4409 case 0x05:
4410 break;
4411 case 0x06:
4412 break;
4413 case 0x07:
4414 break;
4415 case 0x0c:
4416 u_lock_duration=nt_time_to_unix_abs(&q_u->ctr->info.inf12.duration);
4417 if (u_lock_duration != -1)
4418 u_lock_duration /= 60;
4420 u_reset_time=nt_time_to_unix_abs(&q_u->ctr->info.inf12.reset_count)/60;
4422 pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
4423 pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
4424 pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)q_u->ctr->info.inf12.bad_attempt_lockout);
4425 break;
4426 default:
4427 return NT_STATUS_INVALID_INFO_CLASS;
4430 init_samr_r_set_domain_info(r_u, NT_STATUS_OK);
4432 DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4434 return r_u->status;