r11769: Looking at a performance problem enumerating accounts, wondered
[samba.git] / source3 / rpc_server / srv_samr_nt.c
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.
13  *
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.
18  *
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.
23  *
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.
27  */
28
29 /*
30  * This is the implementation of the SAMR code.
31  */
32
33 #include "includes.h"
34
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_RPC_SRV
37
38 #define SAMR_USR_RIGHTS_WRITE_PW \
39                 ( READ_CONTROL_ACCESS           | \
40                   SA_RIGHT_USER_CHANGE_PASSWORD | \
41                   SA_RIGHT_USER_SET_LOC_COM )
42
43 extern rid_name domain_group_rids[];
44 extern rid_name domain_alias_rids[];
45 extern rid_name builtin_alias_rids[];
46
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 */
53
54         uint16 enum_acb_mask;
55         struct pdb_search *enum_users; /* enumusers with a mask */
56 } DISP_INFO;
57
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;
66
67         TALLOC_CTX *mem_ctx;
68 };
69
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};
75
76 /*******************************************************************
77 *******************************************************************/
78
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 )
82 {
83         DOM_SID domadmin_sid;
84         SEC_ACE ace[5];         /* at most 5 entries */
85         SEC_ACCESS mask;
86         size_t i = 0;
87
88         SEC_ACL *psa = NULL;
89
90         /* basic access for Everyone */
91
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);
94
95         /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
96
97         init_sec_access(&mask, map->generic_all);
98         
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);
101
102         /* Add Full Access for Domain Admins if we are a DC */
103         
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);
108         }
109
110         /* if we have a sid, give it some special access */
111
112         if ( sid ) {
113                 init_sec_access( &mask, sid_access );
114                 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
115 }
116
117         /* create the security descriptor */
118
119         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
120                 return NT_STATUS_NO_MEMORY;
121
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;
124
125         return NT_STATUS_OK;
126 }
127
128 /*******************************************************************
129  Checks if access to an object should be granted, and returns that
130  level of access for further checks.
131 ********************************************************************/
132
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 )
137 {
138         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
139         uint32 saved_mask = 0;
140
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) */
144         
145         if ( rights && user_has_any_privilege( token, rights ) ) {
146         
147                 saved_mask = (des_access & rights_mask);
148                 des_access &= ~saved_mask;
149                 
150                 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
151                         rights_mask));
152         }
153                 
154         
155         /* check the security descriptor first */
156         
157         if ( se_access_check(psd, token, des_access, acc_granted, &status) )
158                 goto done;
159         
160         /* give root a free pass */
161         
162         if ( geteuid() == sec_initial_uid() ) {
163         
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"));
166                 
167                 *acc_granted = des_access;
168                 
169                 status = NT_STATUS_OK;
170                 goto done;
171         }
172         
173         
174 done:
175         /* add in any bits saved during the privilege check (only 
176            matters is status is ok) */
177         
178         *acc_granted |= rights_mask;
179
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));
183         
184         return status;
185 }
186
187 /*******************************************************************
188  Checks if access to a function can be granted
189 ********************************************************************/
190
191 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
192 {
193         DEBUG(5,("%s: access check ((granted: %#010x;  required: %#010x)\n",  
194                 debug, acc_granted, acc_required));
195
196         /* check the security descriptor first */
197         
198         if ( (acc_granted&acc_required) == acc_required )
199                 return NT_STATUS_OK;
200                 
201         /* give root a free pass */
202
203         if (geteuid() == sec_initial_uid()) {
204         
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"));
208                 
209                 return NT_STATUS_OK;
210         }
211         
212         DEBUG(2,("%s: ACCESS DENIED (granted: %#010x;  required: %#010x)\n", 
213                 debug, acc_granted, acc_required));
214                 
215         return NT_STATUS_ACCESS_DENIED;
216 }
217
218
219 /*******************************************************************
220  Create a samr_info struct.
221 ********************************************************************/
222
223 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
224 {
225         struct samr_info *info;
226         fstring sid_str;
227         TALLOC_CTX *mem_ctx;
228         
229         if (psid) {
230                 sid_to_string(sid_str, psid);
231         } else {
232                 fstrcpy(sid_str,"(NULL)");
233         }
234
235         mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
236
237         if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
238                 return NULL;
239
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"));
245         }
246         info->mem_ctx = mem_ctx;
247         return info;
248 }
249
250 /*******************************************************************
251  Function to free the per handle data.
252  ********************************************************************/
253
254 /*******************************************************************
255  Function to free the per handle data.
256  ********************************************************************/
257
258 static void free_samr_db(struct samr_info *info)
259 {
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;
272 }
273
274 static void free_samr_info(void *ptr)
275 {
276         struct samr_info *info=(struct samr_info *) ptr;
277
278         free_samr_db(info);
279         talloc_destroy(info->mem_ctx);
280 }
281
282 /*******************************************************************
283  Ensure password info is never given out. Paranioa... JRA.
284  ********************************************************************/
285
286 static void samr_clear_sam_passwd(SAM_ACCOUNT *sam_pass)
287 {
288         
289         if (!sam_pass)
290                 return;
291
292         /* These now zero out the old password */
293
294         pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
295         pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
296 }
297
298 static uint32 count_sam_users(struct disp_info *info, uint16 acct_flags)
299 {
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;
308 }
309
310 static uint32 count_sam_groups(struct disp_info *info)
311 {
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;
320 }
321
322 /*******************************************************************
323  _samr_close_hnd
324  ********************************************************************/
325
326 NTSTATUS _samr_close_hnd(pipes_struct *p, SAMR_Q_CLOSE_HND *q_u, SAMR_R_CLOSE_HND *r_u)
327 {
328         r_u->status = NT_STATUS_OK;
329
330         /* close the policy handle */
331         if (!close_policy_hnd(p, &q_u->pol))
332                 return NT_STATUS_OBJECT_NAME_INVALID;
333
334         DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
335
336         return r_u->status;
337 }
338
339 /*******************************************************************
340  samr_reply_open_domain
341  ********************************************************************/
342
343 NTSTATUS _samr_open_domain(pipes_struct *p, SAMR_Q_OPEN_DOMAIN *q_u, SAMR_R_OPEN_DOMAIN *r_u)
344 {
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;
352
353         r_u->status = NT_STATUS_OK;
354
355         /* find the connection policy handle. */
356         
357         if ( !find_policy_by_hnd(p, &q_u->pol, (void**)&info) )
358                 return NT_STATUS_INVALID_HANDLE;
359
360         status = access_check_samr_function( info->acc_granted, 
361                 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_open_domain" );
362                 
363         if ( !NT_STATUS_IS_OK(status) )
364                 return status;
365
366         /*check if access can be granted as requested by client. */
367         
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 );
370         
371         se_priv_copy( &se_rights, &se_machine_account );
372         se_priv_add( &se_rights, &se_add_users );
373
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" );
377                 
378         if ( !NT_STATUS_IS_OK(status) )
379                 return status;
380
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;
385
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;
389
390         DEBUG(5,("samr_open_domain: %d\n", __LINE__));
391
392         return r_u->status;
393 }
394
395 /*******************************************************************
396  _samr_get_usrdom_pwinfo
397  ********************************************************************/
398
399 NTSTATUS _samr_get_usrdom_pwinfo(pipes_struct *p, SAMR_Q_GET_USRDOM_PWINFO *q_u, SAMR_R_GET_USRDOM_PWINFO *r_u)
400 {
401         struct samr_info *info = NULL;
402
403         r_u->status = NT_STATUS_OK;
404
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;
408
409         if (!sid_check_is_in_our_domain(&info->sid))
410                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
411
412         init_samr_r_get_usrdom_pwinfo(r_u, NT_STATUS_OK);
413
414         DEBUG(5,("_samr_get_usrdom_pwinfo: %d\n", __LINE__));
415
416         /* 
417          * NT sometimes return NT_STATUS_ACCESS_DENIED
418          * I don't know yet why.
419          */
420
421         return r_u->status;
422 }
423
424 /*******************************************************************
425  _samr_set_sec_obj
426  ********************************************************************/
427
428 NTSTATUS _samr_set_sec_obj(pipes_struct *p, SAMR_Q_SET_SEC_OBJ *q_u, SAMR_R_SET_SEC_OBJ *r_u)
429 {
430         DEBUG(0,("_samr_set_sec_obj: Not yet implemented!\n"));
431         return NT_STATUS_NOT_IMPLEMENTED;
432 }
433
434
435 /*******************************************************************
436 ********************************************************************/
437
438 static BOOL get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol, 
439                                      DOM_SID *sid, uint32 *acc_granted)
440 {
441         struct samr_info *info = NULL;
442
443         /* find the policy handle.  open a policy on it. */
444         if (!find_policy_by_hnd(p, pol, (void **)&info))
445                 return False;
446
447         if (!info)
448                 return False;
449
450         *sid = info->sid;
451         *acc_granted = info->acc_granted;
452         return True;
453 }
454
455 /*******************************************************************
456  _samr_query_sec_obj
457  ********************************************************************/
458
459 NTSTATUS _samr_query_sec_obj(pipes_struct *p, SAMR_Q_QUERY_SEC_OBJ *q_u, SAMR_R_QUERY_SEC_OBJ *r_u)
460 {
461         DOM_SID pol_sid;
462         fstring str_sid;
463         SEC_DESC * psd = NULL;
464         uint32 acc_granted;
465         size_t sd_size;
466
467         r_u->status = NT_STATUS_OK;
468
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;
472
473
474
475         DEBUG(10,("_samr_query_sec_obj: querying security on SID: %s\n", sid_to_string(str_sid, &pol_sid)));
476
477         /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
478
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)
481         {
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);
484         }
485         else if (sid_equal(&pol_sid,get_global_sam_sid()))  /* check if it is our domain SID */
486
487         {
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);
490         }
491         else if (sid_equal(&pol_sid,&global_sid_Builtin)) /* check if it is the Builtin  Domain */
492         {
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);
496         }
497         else if (sid_check_is_in_our_domain(&pol_sid) ||
498                  sid_check_is_in_builtin(&pol_sid))
499         {
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);
504         }
505         else return NT_STATUS_OBJECT_TYPE_MISMATCH;
506
507         if ((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
508                 return NT_STATUS_NO_MEMORY;
509
510         if (NT_STATUS_IS_OK(r_u->status))
511                 r_u->ptr = 1;
512
513         return r_u->status;
514 }
515
516 /*******************************************************************
517 makes a SAM_ENTRY / UNISTR2* structure from a user list.
518 ********************************************************************/
519
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)
524 {
525         uint32 i;
526         SAM_ENTRY *sam;
527         UNISTR2 *uni_name;
528         
529         *sam_pp = NULL;
530         *uni_name_pp = NULL;
531
532         if (num_entries == 0)
533                 return NT_STATUS_OK;
534
535         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_entries);
536
537         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_entries);
538
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;
542         }
543
544         for (i = 0; i < num_entries; i++) {
545                 UNISTR2 uni_temp_name;
546                 /*
547                  * usrmgr expects a non-NULL terminated string with
548                  * trust relationships
549                  */
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);
556                 }
557
558                 init_sam_entry(&sam[i], &uni_temp_name, entries[i].rid);
559                 copy_unistr2(&uni_name[i], &uni_temp_name);
560         }
561
562         *sam_pp = sam;
563         *uni_name_pp = uni_name;
564         return NT_STATUS_OK;
565 }
566
567 /*******************************************************************
568  samr_reply_enum_dom_users
569  ********************************************************************/
570
571 NTSTATUS _samr_enum_dom_users(pipes_struct *p, SAMR_Q_ENUM_DOM_USERS *q_u, 
572                               SAMR_R_ENUM_DOM_USERS *r_u)
573 {
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;
581         
582         r_u->status = NT_STATUS_OK;
583
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;
587
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;
592         }
593         
594         DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
595
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;
601         }
602
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;
606         }
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();
613
614         if (num_account == 0) {
615                 DEBUG(5, ("_samr_enum_dom_users: enumeration handle over "
616                           "total entries\n"));
617                 return NT_STATUS_OK;
618         }
619
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);
624
625         if (!NT_STATUS_IS_OK(r_u->status))
626                 return r_u->status;
627
628         if (max_entries <= num_account)
629                 r_u->status = STATUS_MORE_ENTRIES;
630
631         DEBUG(5, ("_samr_enum_dom_users: %d\n", __LINE__));
632
633         init_samr_r_enum_dom_users(r_u, q_u->start_idx + num_account,
634                                    num_account);
635
636         DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
637
638         return r_u->status;
639 }
640
641 /*******************************************************************
642 makes a SAM_ENTRY / UNISTR2* structure from a group list.
643 ********************************************************************/
644
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)
649 {
650         uint32 i;
651         SAM_ENTRY *sam;
652         UNISTR2 *uni_name;
653
654         *sam_pp = NULL;
655         *uni_name_pp = NULL;
656
657         if (num_sam_entries == 0)
658                 return;
659
660         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
661         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
662
663         if (sam == NULL || uni_name == NULL) {
664                 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
665                 return;
666         }
667
668         for (i = 0; i < num_sam_entries; i++) {
669                 /*
670                  * JRA. I think this should include the null. TNG does not.
671                  */
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);
675         }
676
677         *sam_pp = sam;
678         *uni_name_pp = uni_name;
679 }
680
681 /*******************************************************************
682  samr_reply_enum_dom_groups
683  ********************************************************************/
684
685 NTSTATUS _samr_enum_dom_groups(pipes_struct *p, SAMR_Q_ENUM_DOM_GROUPS *q_u, SAMR_R_ENUM_DOM_GROUPS *r_u)
686 {
687         struct samr_info *info = NULL;
688         struct samr_displayentry *groups;
689         uint32 num_groups;
690
691         r_u->status = NT_STATUS_OK;
692
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;
696
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;
702
703         DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
704
705         /* the domain group array is being allocated in the function below */
706
707         become_root();
708         if (info->disp_info.groups == NULL)
709                 info->disp_info.groups = pdb_search_groups();
710         unbecome_root();
711
712         if (info->disp_info.groups == NULL)
713                 return NT_STATUS_ACCESS_DENIED;
714
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();
719         
720         make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
721                                   num_groups, groups);
722
723         init_samr_r_enum_dom_groups(r_u, q_u->start_idx, num_groups);
724
725         DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
726
727         return r_u->status;
728 }
729
730 /*******************************************************************
731  samr_reply_enum_dom_aliases
732  ********************************************************************/
733
734 NTSTATUS _samr_enum_dom_aliases(pipes_struct *p, SAMR_Q_ENUM_DOM_ALIASES *q_u, SAMR_R_ENUM_DOM_ALIASES *r_u)
735 {
736         struct samr_info *info;
737         struct samr_displayentry *aliases;
738         struct pdb_search **search = NULL;
739         uint32 num_aliases = 0;
740
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;
744
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;
750
751         DEBUG(5,("samr_reply_enum_dom_aliases: sid %s\n",
752                  sid_string_static(&info->sid)));
753
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;
758
759         if (search == NULL) 
760                 return NT_STATUS_INVALID_HANDLE;
761
762         become_root();
763         if (*search == NULL)
764                 *search = pdb_search_aliases(&info->sid);
765         unbecome_root();
766
767         if (*search == NULL) 
768                 return NT_STATUS_ACCESS_DENIED;
769
770         become_root();
771         num_aliases = pdb_search_entries(*search, q_u->start_idx,
772                                          MAX_SAM_ENTRIES, &aliases);
773         unbecome_root();
774         
775         make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
776                                   num_aliases, aliases);
777
778         init_samr_r_enum_dom_aliases(r_u, q_u->start_idx + num_aliases,
779                                      num_aliases);
780
781         DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
782
783         return r_u->status;
784 }
785
786 /*******************************************************************
787  samr_reply_query_dispinfo
788  ********************************************************************/
789
790 NTSTATUS _samr_query_dispinfo(pipes_struct *p, SAMR_Q_QUERY_DISPINFO *q_u, 
791                               SAMR_R_QUERY_DISPINFO *r_u)
792 {
793         struct samr_info *info = NULL;
794         uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
795         
796         uint32 max_entries=q_u->max_entries;
797         uint32 enum_context=q_u->start_idx;
798         uint32 max_size=q_u->max_size;
799
800         SAM_DISPINFO_CTR *ctr;
801         uint32 temp_size=0, total_data_size=0;
802         NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
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;
807
808         DEBUG(5, ("samr_reply_query_dispinfo: %d\n", __LINE__));
809         r_u->status = NT_STATUS_UNSUCCESSFUL;
810
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;
814
815         /*
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
822          */
823
824         /*
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.
836          *
837          * If you want to change it, think twice and think
838          * of the second point , that's really important.
839          *
840          * JFM, 12/20/2001
841          */
842
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;
847         }
848
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;
855         }
856
857         /* calculate the size and limit on the number of entries we will
858          * return */
859
860         temp_size=max_entries*struct_size;
861         
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));
866         }
867
868         if (!(ctr = TALLOC_ZERO_P(p->mem_ctx,SAM_DISPINFO_CTR)))
869                 return NT_STATUS_NO_MEMORY;
870
871         ZERO_STRUCTP(ctr);
872
873         become_root();
874
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;
909         }
910         unbecome_root();
911
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;
942         }
943
944         if (!NT_STATUS_IS_OK(disp_ret))
945                 return disp_ret;
946
947         /* calculate the total size */
948         total_data_size=num_account*struct_size;
949
950         if (num_account)
951                 r_u->status = STATUS_MORE_ENTRIES;
952         else
953                 r_u->status = NT_STATUS_OK;
954
955         DEBUG(5, ("_samr_query_dispinfo: %d\n", __LINE__));
956
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);
960
961         return r_u->status;
962
963 }
964
965 /*******************************************************************
966  samr_reply_query_aliasinfo
967  ********************************************************************/
968
969 NTSTATUS _samr_query_aliasinfo(pipes_struct *p, SAMR_Q_QUERY_ALIASINFO *q_u, SAMR_R_QUERY_ALIASINFO *r_u)
970 {
971         DOM_SID   sid;
972         struct acct_info info;
973         uint32    acc_granted;
974         BOOL ret;
975
976         r_u->status = NT_STATUS_OK;
977
978         DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
979
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;
985         }
986
987         become_root();
988         ret = pdb_get_aliasinfo(&sid, &info);
989         unbecome_root();
990         
991         if ( !ret )
992                 return NT_STATUS_NO_SUCH_ALIAS;
993
994         if ( !(r_u->ctr = TALLOC_ZERO_P( p->mem_ctx, ALIAS_INFO_CTR )) ) 
995                 return NT_STATUS_NO_MEMORY;
996
997
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;
1009         }
1010
1011         DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1012
1013         return r_u->status;
1014 }
1015
1016 #if 0
1017 /*******************************************************************
1018  samr_reply_lookup_ids
1019  ********************************************************************/
1020
1021  uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1022 {
1023     uint32 rid[MAX_SAM_ENTRIES];
1024     int num_rids = q_u->num_sids1;
1025
1026     r_u->status = NT_STATUS_OK;
1027
1028     DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1029
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));
1033     }
1034
1035 #if 0
1036     int i;
1037     SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1038
1039     for (i = 0; i < num_rids && status == 0; i++)
1040     {
1041         struct sam_passwd *sam_pass;
1042         fstring user_name;
1043
1044
1045         fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1046                                     q_u->uni_user_name[i].uni_str_len));
1047
1048         /* find the user account */
1049         become_root();
1050         sam_pass = get_smb21pwd_entry(user_name, 0);
1051         unbecome_root();
1052
1053         if (sam_pass == NULL)
1054         {
1055             status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1056             rid[i] = 0;
1057         }
1058         else
1059         {
1060             rid[i] = sam_pass->user_rid;
1061         }
1062     }
1063 #endif
1064
1065     num_rids = 1;
1066     rid[0] = BUILTIN_ALIAS_RID_USERS;
1067
1068     init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1069
1070     DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1071
1072     return r_u->status;
1073 }
1074 #endif
1075
1076 /*******************************************************************
1077  _samr_lookup_names
1078  ********************************************************************/
1079
1080 NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LOOKUP_NAMES *r_u)
1081 {
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;
1091
1092         r_u->status = NT_STATUS_OK;
1093
1094         DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1095
1096         ZERO_ARRAY(rid);
1097         ZERO_ARRAY(type);
1098
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;
1102         }
1103         
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;
1106         }
1107
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));
1111         }
1112
1113         DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1114         
1115         for (i = 0; i < num_rids; i++) {
1116                 fstring name;
1117                 DOM_SID sid;
1118                 int ret;
1119
1120                 r_u->status = NT_STATUS_NONE_MAPPED;
1121
1122                 rid [i] = 0xffffffff;
1123                 type[i] = SID_NAME_UNKNOWN;
1124
1125                 ret = rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1126
1127                 /*
1128                  * we are only looking for a name
1129                  * the SID we get back can be outside
1130                  * the scope of the pol_sid
1131                  * 
1132                  * in clear: it prevents to reply to domain\group: yes
1133                  * when only builtin\group exists.
1134                  *
1135                  * a cleaner code is to add the sid of the domain we're looking in
1136                  * to the local_lookup_name function.
1137                  */
1138                  
1139                 if ((ret > 0) && local_lookup_name(name, &sid, &local_type)) {
1140                         sid_split_rid(&sid, &local_rid);
1141                                 
1142                         if (sid_equal(&sid, &pol_sid)) {
1143                                 rid[i]=local_rid;
1144
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;
1149
1150                                 r_u->status = NT_STATUS_OK;
1151                         }
1152                 }
1153         }
1154
1155         init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, (uint32 *)type, r_u->status);
1156
1157         DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1158
1159         return r_u->status;
1160 }
1161
1162 /*******************************************************************
1163  _samr_chgpasswd_user
1164  ********************************************************************/
1165
1166 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1167 {
1168         fstring user_name;
1169         fstring wks;
1170
1171         DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1172
1173         r_u->status = NT_STATUS_OK;
1174
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);
1177
1178         DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1179
1180         /*
1181          * Pass the user through the NT -> unix user mapping
1182          * function.
1183          */
1184  
1185         (void)map_username(user_name);
1186  
1187         /*
1188          * UNIX username case mangling not required, pass_oem_change 
1189          * is case insensitive.
1190          */
1191
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);
1194
1195         init_samr_r_chgpasswd_user(r_u, r_u->status);
1196
1197         DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1198
1199         return r_u->status;
1200 }
1201
1202 /*******************************************************************
1203 makes a SAMR_R_LOOKUP_RIDS structure.
1204 ********************************************************************/
1205
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)
1209 {
1210         uint32 i;
1211         UNIHDR *hdr_name=NULL;
1212         UNISTR2 *uni_name=NULL;
1213
1214         *pp_uni_name = NULL;
1215         *pp_hdr_name = NULL;
1216
1217         if (num_names != 0) {
1218                 hdr_name = TALLOC_ZERO_ARRAY(ctx, UNIHDR, num_names);
1219                 if (hdr_name == NULL)
1220                         return False;
1221
1222                 uni_name = TALLOC_ZERO_ARRAY(ctx,UNISTR2, num_names);
1223                 if (uni_name == NULL)
1224                         return False;
1225         }
1226
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]);
1231         }
1232
1233         *pp_uni_name = uni_name;
1234         *pp_hdr_name = hdr_name;
1235
1236         return True;
1237 }
1238
1239 /*******************************************************************
1240  _samr_lookup_rids
1241  ********************************************************************/
1242
1243 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1244 {
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;
1252         
1253         r_u->status = NT_STATUS_OK;
1254
1255         DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1256
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;
1260
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;
1265         }
1266
1267         names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
1268         attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
1269
1270         if ((num_rids != 0) && ((names == NULL) || (attrs == NULL)))
1271                 return NT_STATUS_NO_MEMORY;
1272
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;
1277         }
1278
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();
1283
1284  done:
1285
1286         if(!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
1287                                   &hdr_name, &uni_name))
1288                 return NT_STATUS_NO_MEMORY;
1289
1290         init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, attrs);
1291
1292         DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1293
1294         return r_u->status;
1295 }
1296
1297 /*******************************************************************
1298  _samr_open_user. Safe - gives out no passwd info.
1299  ********************************************************************/
1300
1301 NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1302 {
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;
1315
1316         r_u->status = NT_STATUS_OK;
1317
1318         /* find the domain policy handle and get domain SID / access bits in the domain policy. */
1319         
1320         if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted) )
1321                 return NT_STATUS_INVALID_HANDLE;
1322         
1323         nt_status = access_check_samr_function( acc_granted, 
1324                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_user" );
1325                 
1326         if ( !NT_STATUS_IS_OK(nt_status) )
1327                 return nt_status;
1328
1329         nt_status = pdb_init_sam_talloc(p->mem_ctx, &sampass);
1330         
1331         if (!NT_STATUS_IS_OK(nt_status))
1332                 return nt_status;
1333
1334         /* append the user's RID to it */
1335         
1336         if (!sid_append_rid(&sid, q_u->user_rid))
1337                 return NT_STATUS_NO_SUCH_USER;
1338         
1339         /* check if access can be granted as requested by client. */
1340         
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);
1343         
1344         se_priv_copy( &se_rights, &se_machine_account );
1345         se_priv_add( &se_rights, &se_add_users );
1346         
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");
1350                 
1351         if ( !NT_STATUS_IS_OK(nt_status) )
1352                 return nt_status;
1353
1354         become_root();
1355         ret=pdb_getsampwsid(sampass, &sid);
1356         unbecome_root();
1357
1358         /* check that the SID exists in our domain. */
1359         if (ret == False) {
1360                 return NT_STATUS_NO_SUCH_USER;
1361         }
1362
1363         pdb_free_sam(&sampass);
1364
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;
1369
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;
1373
1374         return r_u->status;
1375 }
1376
1377 /*************************************************************************
1378  get_user_info_7. Safe. Only gives out account_name.
1379  *************************************************************************/
1380
1381 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_SID *user_sid)
1382 {
1383         SAM_ACCOUNT *smbpass=NULL;
1384         BOOL ret;
1385         NTSTATUS nt_status;
1386
1387         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1388         
1389         if (!NT_STATUS_IS_OK(nt_status)) {
1390                 return nt_status;
1391         }
1392
1393         become_root();
1394         ret = pdb_getsampwsid(smbpass, user_sid);
1395         unbecome_root();
1396
1397         if (ret==False) {
1398                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1399                 return NT_STATUS_NO_SUCH_USER;
1400         }
1401
1402         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1403
1404         ZERO_STRUCTP(id7);
1405         init_sam_user_info7(id7, pdb_get_username(smbpass) );
1406
1407         pdb_free_sam(&smbpass);
1408
1409         return NT_STATUS_OK;
1410 }
1411 /*************************************************************************
1412  get_user_info_16. Safe. Only gives out acb bits.
1413  *************************************************************************/
1414
1415 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DOM_SID *user_sid)
1416 {
1417         SAM_ACCOUNT *smbpass=NULL;
1418         BOOL ret;
1419         NTSTATUS nt_status;
1420
1421         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1422         
1423         if (!NT_STATUS_IS_OK(nt_status)) {
1424                 return nt_status;
1425         }
1426
1427         become_root();
1428         ret = pdb_getsampwsid(smbpass, user_sid);
1429         unbecome_root();
1430
1431         if (ret==False) {
1432                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1433                 return NT_STATUS_NO_SUCH_USER;
1434         }
1435
1436         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1437
1438         ZERO_STRUCTP(id16);
1439         init_sam_user_info16(id16, pdb_get_acct_ctrl(smbpass) );
1440
1441         pdb_free_sam(&smbpass);
1442
1443         return NT_STATUS_OK;
1444 }
1445
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  *************************************************************************/
1451
1452 static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_INFO_18 * id18, DOM_SID *user_sid)
1453 {
1454         SAM_ACCOUNT *smbpass=NULL;
1455         BOOL ret;
1456         NTSTATUS nt_status;
1457
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;
1460         }
1461
1462         if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
1463                 return NT_STATUS_ACCESS_DENIED;
1464         }
1465
1466         /*
1467          * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1468          */
1469
1470         nt_status = pdb_init_sam_talloc(mem_ctx, &smbpass);
1471         
1472         if (!NT_STATUS_IS_OK(nt_status)) {
1473                 return nt_status;
1474         }
1475
1476         ret = pdb_getsampwsid(smbpass, user_sid);
1477
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;
1482         }
1483
1484         DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1485
1486         if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1487                 pdb_free_sam(&smbpass);
1488                 return NT_STATUS_ACCOUNT_DISABLED;
1489         }
1490
1491         ZERO_STRUCTP(id18);
1492         init_sam_user_info18(id18, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1493         
1494         pdb_free_sam(&smbpass);
1495
1496         return NT_STATUS_OK;
1497 }
1498
1499 /*************************************************************************
1500  get_user_info_20
1501  *************************************************************************/
1502
1503 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DOM_SID *user_sid)
1504 {
1505         SAM_ACCOUNT *sampass=NULL;
1506         BOOL ret;
1507
1508         pdb_init_sam_talloc(mem_ctx, &sampass);
1509
1510         become_root();
1511         ret = pdb_getsampwsid(sampass, user_sid);
1512         unbecome_root();
1513
1514         if (ret == False) {
1515                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1516                 return NT_STATUS_NO_SUCH_USER;
1517         }
1518
1519         samr_clear_sam_passwd(sampass);
1520
1521         DEBUG(3,("User:[%s]\n",  pdb_get_username(sampass) ));
1522
1523         ZERO_STRUCTP(id20);
1524         init_sam_user_info20A(id20, sampass);
1525         
1526         pdb_free_sam(&sampass);
1527
1528         return NT_STATUS_OK;
1529 }
1530
1531 /*************************************************************************
1532  get_user_info_21
1533  *************************************************************************/
1534
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)
1537 {
1538         SAM_ACCOUNT *sampass=NULL;
1539         BOOL ret;
1540         NTSTATUS nt_status;
1541
1542         nt_status = pdb_init_sam_talloc(mem_ctx, &sampass);
1543         if (!NT_STATUS_IS_OK(nt_status)) {
1544                 return nt_status;
1545         }
1546
1547         become_root();
1548         ret = pdb_getsampwsid(sampass, user_sid);
1549         unbecome_root();
1550
1551         if (ret == False) {
1552                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1553                 return NT_STATUS_NO_SUCH_USER;
1554         }
1555
1556         samr_clear_sam_passwd(sampass);
1557
1558         DEBUG(3,("User:[%s]\n",  pdb_get_username(sampass) ));
1559
1560         ZERO_STRUCTP(id21);
1561         nt_status = init_sam_user_info21A(id21, sampass, domain_sid);
1562         
1563         pdb_free_sam(&sampass);
1564
1565         return NT_STATUS_OK;
1566 }
1567
1568 /*******************************************************************
1569  _samr_query_userinfo
1570  ********************************************************************/
1571
1572 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
1573 {
1574         SAM_USERINFO_CTR *ctr;
1575         struct samr_info *info = NULL;
1576         DOM_SID domain_sid;
1577         uint32 rid;
1578         
1579         r_u->status=NT_STATUS_OK;
1580
1581         /* search for the handle */
1582         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1583                 return NT_STATUS_INVALID_HANDLE;
1584
1585         domain_sid = info->sid;
1586
1587         sid_split_rid(&domain_sid, &rid);
1588
1589         if (!sid_check_is_in_our_domain(&info->sid))
1590                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1591
1592         DEBUG(5,("_samr_query_userinfo: sid:%s\n", sid_string_static(&info->sid)));
1593
1594         ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_USERINFO_CTR);
1595         if (!ctr)
1596                 return NT_STATUS_NO_MEMORY;
1597
1598         ZERO_STRUCTP(ctr);
1599
1600         /* ok!  user info levels (lots: see MSDEV help), off we go... */
1601         ctr->switch_value = q_u->switch_value;
1602
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;
1608
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;
1616
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;
1620
1621 #if 0
1622 /* whoops - got this wrong.  i think.  or don't understand what's happening. */
1623         case 17:
1624         {
1625             NTTIME expire;
1626             info = (void *)&id11;
1627
1628             expire.low = 0xffffffff;
1629             expire.high = 0x7fffffff;
1630
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 */
1638
1639             break;
1640         }
1641 #endif
1642
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;
1647
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;
1651                 
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;
1659
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;
1668
1669         default:
1670                 return NT_STATUS_INVALID_INFO_CLASS;
1671         }
1672
1673         init_samr_r_query_userinfo(r_u, ctr, r_u->status);
1674
1675         DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
1676         
1677         return r_u->status;
1678 }
1679
1680 /*******************************************************************
1681  samr_reply_query_usergroups
1682  ********************************************************************/
1683
1684 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
1685 {
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;
1697
1698         /*
1699          * from the SID in the request:
1700          * we should send back the list of DOMAIN GROUPS
1701          * the user is a member of
1702          *
1703          * and only the DOMAIN GROUPS
1704          * no ALIASES !!! neither aliases of the domain
1705          * nor aliases of the builtin SID
1706          *
1707          * JFM, 12/2/2001
1708          */
1709
1710         r_u->status = NT_STATUS_OK;
1711
1712         DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1713
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;
1717         
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;
1720         }
1721
1722         if (!sid_check_is_in_our_domain(&sid))
1723                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1724
1725         pdb_init_sam(&sam_pass);
1726         
1727         become_root();
1728         ret = pdb_getsampwsid(sam_pass, &sid);
1729         unbecome_root();
1730
1731         if (ret == False) {
1732                 pdb_free_sam(&sam_pass);
1733                 return NT_STATUS_NO_SUCH_USER;
1734         }
1735
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;
1740         }
1741
1742         sids = NULL;
1743
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();
1749
1750         pdb_free_sam(&sam_pass);
1751         passwd_free(&passwd);
1752
1753         if (!NT_STATUS_IS_OK(result))
1754                 return result;
1755
1756         SAFE_FREE(unix_gids);
1757
1758         gids = NULL;
1759         num_gids = 0;
1760
1761         for (i=0; i<num_groups; i++) {
1762                 uint32 rid;
1763
1764                 if (!sid_peek_check_rid(get_global_sam_sid(),
1765                                         &(sids[i]), &rid))
1766                         continue;
1767
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;
1772         }
1773         SAFE_FREE(sids);
1774         
1775         /* construct the response.  lkclXXXX: gids are not copied! */
1776         init_samr_r_query_usergroups(r_u, num_groups, gids, r_u->status);
1777         
1778         DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
1779         
1780         return r_u->status;
1781 }
1782
1783 /*******************************************************************
1784  _samr_query_dom_info
1785  ********************************************************************/
1786
1787 NTSTATUS _samr_query_dom_info(pipes_struct *p, SAMR_Q_QUERY_DOMAIN_INFO *q_u, SAMR_R_QUERY_DOMAIN_INFO *r_u)
1788 {
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;
1794
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;
1800
1801         uint32 account_policy_temp;
1802
1803         time_t seq_num;
1804         uint32 server_role;
1805
1806         uint32 num_users=0, num_groups=0, num_aliases=0;
1807
1808         if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL) {
1809                 return NT_STATUS_NO_MEMORY;
1810         }
1811
1812         ZERO_STRUCTP(ctr);
1813
1814         r_u->status = NT_STATUS_OK;
1815         
1816         DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
1817         
1818         /* find the policy handle.  open a policy on it. */
1819         if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info)) {
1820                 return NT_STATUS_INVALID_HANDLE;
1821         }
1822         
1823         switch (q_u->switch_value) {
1824                 case 0x01:
1825                         
1826                         become_root();
1827
1828                         /* AS ROOT !!! */
1829
1830                         pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
1831                         min_pass_len = account_policy_temp;
1832
1833                         pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
1834                         pass_hist = account_policy_temp;
1835
1836                         pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
1837                         flag = account_policy_temp;
1838
1839                         pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
1840                         u_expire = account_policy_temp;
1841
1842                         pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
1843                         u_min_age = account_policy_temp;
1844
1845                         /* !AS ROOT */
1846                         
1847                         unbecome_root();
1848
1849                         unix_to_nt_time_abs(&nt_expire, u_expire);
1850                         unix_to_nt_time_abs(&nt_min_age, u_min_age);
1851
1852                         init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist, 
1853                                        flag, nt_expire, nt_min_age);
1854                         break;
1855                 case 0x02:
1856
1857                         become_root();
1858
1859                         /* AS ROOT !!! */
1860
1861                         num_users=count_sam_users(&info->disp_info,
1862                                                   ACB_NORMAL);
1863                         num_groups=count_sam_groups(&info->disp_info);
1864
1865                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
1866                         u_logout = account_policy_temp;
1867
1868                         unix_to_nt_time_abs(&nt_logout, u_logout);
1869
1870                         if (!pdb_get_seq_num(&seq_num))
1871                                 seq_num = time(NULL);
1872
1873                         /* !AS ROOT */
1874                         
1875                         unbecome_root();
1876
1877                         server_role = ROLE_DOMAIN_PDC;
1878                         if (lp_server_role() == ROLE_DOMAIN_BDC)
1879                                 server_role = ROLE_DOMAIN_BDC;
1880
1881                         init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num, 
1882                                        num_users, num_groups, num_aliases, nt_logout, server_role);
1883                         break;
1884                 case 0x03:
1885
1886                         become_root();
1887
1888                         /* AS ROOT !!! */
1889
1890                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, (unsigned int *)&u_logout);
1891
1892                         /* !AS ROOT */
1893                         
1894                         unbecome_root();
1895
1896                         unix_to_nt_time_abs(&nt_logout, u_logout);
1897                         
1898                         init_unk_info3(&ctr->info.inf3, nt_logout);
1899                         break;
1900                 case 0x05:
1901                         init_unk_info5(&ctr->info.inf5, global_myname());
1902                         break;
1903                 case 0x06:
1904                         init_unk_info6(&ctr->info.inf6);
1905                         break;
1906                 case 0x07:
1907                         server_role = ROLE_DOMAIN_PDC;
1908                         if (lp_server_role() == ROLE_DOMAIN_BDC)
1909                                 server_role = ROLE_DOMAIN_BDC;
1910
1911                         init_unk_info7(&ctr->info.inf7, server_role);
1912                         break;
1913                 case 0x08:
1914
1915                         become_root();
1916
1917                         /* AS ROOT !!! */
1918
1919                         if (!pdb_get_seq_num(&seq_num)) {
1920                                 seq_num = time(NULL);
1921                         }
1922
1923                         /* !AS ROOT */
1924                         
1925                         unbecome_root();
1926
1927                         init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
1928                         break;
1929                 case 0x0c:
1930
1931                         become_root();
1932
1933                         /* AS ROOT !!! */
1934
1935                         pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
1936                         u_lock_duration = account_policy_temp;
1937                         if (u_lock_duration != -1) {
1938                                 u_lock_duration *= 60;
1939                         }
1940
1941                         pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
1942                         u_reset_time = account_policy_temp * 60;
1943
1944                         pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
1945                         lockout = account_policy_temp;
1946
1947                         /* !AS ROOT */
1948                         
1949                         unbecome_root();
1950
1951                         unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
1952                         unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
1953         
1954                         init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
1955                         break;
1956                 default:
1957                         return NT_STATUS_INVALID_INFO_CLASS;
1958                 }
1959         
1960
1961         init_samr_r_query_dom_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
1962         
1963         DEBUG(5,("_samr_query_dom_info: %d\n", __LINE__));
1964         
1965         return r_u->status;
1966 }
1967
1968 /*******************************************************************
1969  _samr_create_user
1970  Create an account, can be either a normal user or a machine.
1971  This funcion will need to be updated for bdc/domain trusts.
1972  ********************************************************************/
1973
1974 NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u, SAMR_R_CREATE_USER *r_u)
1975 {
1976         SAM_ACCOUNT *sam_pass=NULL;
1977         fstring account;
1978         DOM_SID sid;
1979         pstring add_script;
1980         POLICY_HND dom_pol = q_u->domain_pol;
1981         UNISTR2 user_account = q_u->uni_name;
1982         uint16 acb_info = q_u->acb_info;
1983         POLICY_HND *user_pol = &r_u->user_pol;
1984         struct samr_info *info = NULL;
1985         BOOL ret;
1986         NTSTATUS nt_status;
1987         struct passwd *pw;
1988         uint32 acc_granted;
1989         SEC_DESC *psd;
1990         size_t    sd_size;
1991         uint32 new_rid = 0;
1992         /* check this, when giving away 'add computer to domain' privs */
1993         uint32    des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
1994         BOOL can_add_account = False;
1995         SE_PRIV se_rights;
1996
1997         /* Get the domain SID stored in the domain policy */
1998         if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted))
1999                 return NT_STATUS_INVALID_HANDLE;
2000
2001         if (!NT_STATUS_IS_OK(nt_status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_USER, "_samr_create_user"))) {
2002                 return nt_status;
2003         }
2004
2005         if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST || acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) { 
2006                 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if 
2007                    this parameter is not an account type */
2008                 return NT_STATUS_INVALID_PARAMETER;
2009         }
2010
2011         rpcstr_pull(account, user_account.buffer, sizeof(account), user_account.uni_str_len*2, 0);
2012         strlower_m(account);
2013
2014         pdb_init_sam(&sam_pass);
2015
2016         become_root();
2017         ret = pdb_getsampwnam(sam_pass, account);
2018         unbecome_root();
2019         if (ret == True) {
2020                 /* this account exists: say so */
2021                 pdb_free_sam(&sam_pass);
2022                 return NT_STATUS_USER_EXISTS;
2023         }
2024
2025         pdb_free_sam(&sam_pass);
2026
2027         /*********************************************************************
2028          * HEADS UP!  If we have to create a new user account, we have to get 
2029          * a new RID from somewhere.  This used to be done by the passdb 
2030          * backend. It has been moved into idmap now.  Since idmap is now 
2031          * wrapped up behind winbind, this means you have to run winbindd if you
2032          * want new accounts to get a new RID when "enable rid algorithm = no".
2033          * Tough.  We now have a uniform way of allocating RIDs regardless
2034          * of what ever passdb backend people may use.
2035          *                                             --jerry (2003-07-10)
2036          *********************************************************************/
2037
2038         pw = Get_Pwnam(account);
2039
2040         /* determine which user right we need to check based on the acb_info */
2041         
2042         if ( acb_info & ACB_WSTRUST )
2043         {
2044                 pstrcpy(add_script, lp_addmachine_script());
2045                 se_priv_copy( &se_rights, &se_machine_account );
2046                 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2047         } 
2048         /* usrmgr.exe (and net rpc trustdom grant) creates a normal user 
2049            account for domain trusts and changes the ACB flags later */
2050         else if ( acb_info & ACB_NORMAL && (account[strlen(account)-1] != '$') )
2051         {
2052                 pstrcpy(add_script, lp_adduser_script());
2053                 se_priv_copy( &se_rights, &se_add_users );
2054                 can_add_account = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
2055         } 
2056         else    /* implicit assumption of a BDC or domain trust account here (we already check the flags earlier) */
2057         {
2058                 pstrcpy(add_script, lp_addmachine_script());
2059                 if ( lp_enable_privileges() ) {
2060                         /* only Domain Admins can add a BDC or domain trust */
2061                         se_priv_copy( &se_rights, &se_priv_none );
2062                         can_add_account = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
2063         }
2064         }
2065                 
2066         DEBUG(5, ("_samr_create_user: %s can add this account : %s\n",
2067                 p->pipe_user_name, can_add_account ? "True":"False" ));
2068                 
2069         /********** BEGIN Admin BLOCK **********/
2070
2071         if ( can_add_account )
2072                 become_root();
2073
2074         if ( !pw ) {
2075                 if (*add_script) {
2076                         int add_ret;
2077
2078                         all_string_sub(add_script, "%u", account, sizeof(add_script));
2079                         add_ret = smbrun(add_script,NULL);
2080                         DEBUG(add_ret ? 0 : 3,("_samr_create_user: Running the command `%s' gave %d\n", add_script, add_ret));
2081                 }
2082         }
2083
2084         /* implicit call to getpwnam() next.  we have a valid SID coming out of this call */
2085
2086         flush_pwnam_cache();
2087         nt_status = pdb_init_sam_new(&sam_pass, account, new_rid);
2088
2089         /* this code is order such that we have no unnecessary retuns 
2090            out of the admin block of code */    
2091            
2092         if ( NT_STATUS_IS_OK(nt_status) ) {
2093                 pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
2094         
2095                 if ( !(ret = pdb_add_sam_account(sam_pass)) ) {
2096                         pdb_free_sam(&sam_pass);
2097                         DEBUG(0, ("could not add user/computer %s to passdb.  Check permissions?\n", 
2098                                 account));
2099                         nt_status = NT_STATUS_ACCESS_DENIED;
2100                 }
2101         }
2102                 
2103         if ( can_add_account )
2104                 unbecome_root();
2105
2106         /********** END Admin BLOCK **********/
2107         
2108         /* now check for failure */
2109         
2110         if ( !NT_STATUS_IS_OK(nt_status) )
2111                 return nt_status;
2112                         
2113         /* Get the user's SID */
2114         
2115         sid_copy(&sid, pdb_get_user_sid(sam_pass));
2116         
2117         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
2118         se_map_generic(&des_access, &usr_generic_mapping);
2119         
2120         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2121                 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access, 
2122                 &acc_granted, "_samr_create_user");
2123                 
2124         if ( !NT_STATUS_IS_OK(nt_status) ) {
2125                 return nt_status;
2126         }
2127
2128         /* associate the user's SID with the new handle. */
2129         if ((info = get_samr_info_by_sid(&sid)) == NULL) {
2130                 pdb_free_sam(&sam_pass);
2131                 return NT_STATUS_NO_MEMORY;
2132         }
2133
2134         ZERO_STRUCTP(info);
2135         info->sid = sid;
2136         info->acc_granted = acc_granted;
2137
2138         /* get a (unique) handle.  open a policy on it. */
2139         if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
2140                 pdb_free_sam(&sam_pass);
2141                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2142         }
2143
2144         r_u->user_rid=pdb_get_user_rid(sam_pass);
2145
2146         r_u->access_granted = acc_granted;
2147
2148         pdb_free_sam(&sam_pass);
2149
2150         return NT_STATUS_OK;
2151 }
2152
2153 /*******************************************************************
2154  samr_reply_connect_anon
2155  ********************************************************************/
2156
2157 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2158 {
2159         struct samr_info *info = NULL;
2160         uint32    des_access = q_u->access_mask;
2161
2162         /* Access check */
2163
2164         if (!pipe_access_check(p)) {
2165                 DEBUG(3, ("access denied to samr_connect_anon\n"));
2166                 r_u->status = NT_STATUS_ACCESS_DENIED;
2167                 return r_u->status;
2168         }
2169
2170         /* set up the SAMR connect_anon response */
2171
2172         r_u->status = NT_STATUS_OK;
2173
2174         /* associate the user's SID with the new handle. */
2175         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2176                 return NT_STATUS_NO_MEMORY;
2177
2178         /* don't give away the farm but this is probably ok.  The SA_RIGHT_SAM_ENUM_DOMAINS
2179            was observed from a win98 client trying to enumerate users (when configured  
2180            user level access control on shares)   --jerry */
2181            
2182         se_map_generic( &des_access, &sam_generic_mapping );
2183         info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
2184         
2185         info->status = q_u->unknown_0;
2186
2187         /* get a (unique) handle.  open a policy on it. */
2188         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2189                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2190
2191         return r_u->status;
2192 }
2193
2194 /*******************************************************************
2195  samr_reply_connect
2196  ********************************************************************/
2197
2198 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2199 {
2200         struct samr_info *info = NULL;
2201         SEC_DESC *psd = NULL;
2202         uint32    acc_granted;
2203         uint32    des_access = q_u->access_mask;
2204         NTSTATUS  nt_status;
2205         size_t    sd_size;
2206
2207
2208         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2209
2210         /* Access check */
2211
2212         if (!pipe_access_check(p)) {
2213                 DEBUG(3, ("access denied to samr_connect\n"));
2214                 r_u->status = NT_STATUS_ACCESS_DENIED;
2215                 return r_u->status;
2216         }
2217
2218         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2219         se_map_generic(&des_access, &sam_generic_mapping);
2220         
2221         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2222                 NULL, 0, des_access, &acc_granted, "_samr_connect");
2223         
2224         if ( !NT_STATUS_IS_OK(nt_status) ) 
2225                 return nt_status;
2226
2227         r_u->status = NT_STATUS_OK;
2228
2229         /* associate the user's SID and access granted with the new handle. */
2230         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2231                 return NT_STATUS_NO_MEMORY;
2232
2233         info->acc_granted = acc_granted;
2234         info->status = q_u->access_mask;
2235
2236         /* get a (unique) handle.  open a policy on it. */
2237         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2238                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2239
2240         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2241
2242         return r_u->status;
2243 }
2244
2245 /*******************************************************************
2246  samr_connect4
2247  ********************************************************************/
2248
2249 NTSTATUS _samr_connect4(pipes_struct *p, SAMR_Q_CONNECT4 *q_u, SAMR_R_CONNECT4 *r_u)
2250 {
2251         struct samr_info *info = NULL;
2252         SEC_DESC *psd = NULL;
2253         uint32    acc_granted;
2254         uint32    des_access = q_u->access_mask;
2255         NTSTATUS  nt_status;
2256         size_t    sd_size;
2257
2258
2259         DEBUG(5,("_samr_connect4: %d\n", __LINE__));
2260
2261         /* Access check */
2262
2263         if (!pipe_access_check(p)) {
2264                 DEBUG(3, ("access denied to samr_connect4\n"));
2265                 r_u->status = NT_STATUS_ACCESS_DENIED;
2266                 return r_u->status;
2267         }
2268
2269         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2270         se_map_generic(&des_access, &sam_generic_mapping);
2271         
2272         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2273                 NULL, 0, des_access, &acc_granted, "_samr_connect4");
2274         
2275         if ( !NT_STATUS_IS_OK(nt_status) ) 
2276                 return nt_status;
2277
2278         r_u->status = NT_STATUS_OK;
2279
2280         /* associate the user's SID and access granted with the new handle. */
2281         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2282                 return NT_STATUS_NO_MEMORY;
2283
2284         info->acc_granted = acc_granted;
2285         info->status = q_u->access_mask;
2286
2287         /* get a (unique) handle.  open a policy on it. */
2288         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2289                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2290
2291         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2292
2293         return r_u->status;
2294 }
2295
2296 /*******************************************************************
2297  samr_connect5
2298  ********************************************************************/
2299
2300 NTSTATUS _samr_connect5(pipes_struct *p, SAMR_Q_CONNECT5 *q_u, SAMR_R_CONNECT5 *r_u)
2301 {
2302         struct samr_info *info = NULL;
2303         SEC_DESC *psd = NULL;
2304         uint32    acc_granted;
2305         uint32    des_access = q_u->access_mask;
2306         NTSTATUS  nt_status;
2307         POLICY_HND pol;
2308         size_t    sd_size;
2309
2310
2311         DEBUG(5,("_samr_connect5: %d\n", __LINE__));
2312
2313         ZERO_STRUCTP(r_u);
2314
2315         /* Access check */
2316
2317         if (!pipe_access_check(p)) {
2318                 DEBUG(3, ("access denied to samr_connect5\n"));
2319                 r_u->status = NT_STATUS_ACCESS_DENIED;
2320                 return r_u->status;
2321         }
2322
2323         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2324         se_map_generic(&des_access, &sam_generic_mapping);
2325         
2326         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2327                 NULL, 0, des_access, &acc_granted, "_samr_connect5");
2328         
2329         if ( !NT_STATUS_IS_OK(nt_status) ) 
2330                 return nt_status;
2331
2332         /* associate the user's SID and access granted with the new handle. */
2333         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2334                 return NT_STATUS_NO_MEMORY;
2335
2336         info->acc_granted = acc_granted;
2337         info->status = q_u->access_mask;
2338
2339         /* get a (unique) handle.  open a policy on it. */
2340         if (!create_policy_hnd(p, &pol, free_samr_info, (void *)info))
2341                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2342
2343         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2344
2345         init_samr_r_connect5(r_u, &pol, NT_STATUS_OK);
2346
2347         return r_u->status;
2348 }
2349
2350 /**********************************************************************
2351  api_samr_lookup_domain
2352  **********************************************************************/
2353
2354 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2355 {
2356         struct samr_info *info;
2357         fstring domain_name;
2358         DOM_SID sid;
2359
2360         r_u->status = NT_STATUS_OK;
2361
2362         if (!find_policy_by_hnd(p, &q_u->connect_pol, (void**)&info))
2363                 return NT_STATUS_INVALID_HANDLE;
2364
2365         /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.  
2366            Reverted that change so we will work with RAS servers again */
2367
2368         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, 
2369                 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_lookup_domain"))) 
2370         {
2371                 return r_u->status;
2372         }
2373
2374         rpcstr_pull(domain_name, q_u->uni_domain.buffer, sizeof(domain_name), q_u->uni_domain.uni_str_len*2, 0);
2375
2376         ZERO_STRUCT(sid);
2377
2378         if (!secrets_fetch_domain_sid(domain_name, &sid)) {
2379                 r_u->status = NT_STATUS_NO_SUCH_DOMAIN;
2380         }
2381
2382         DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name, sid_string_static(&sid)));
2383
2384         init_samr_r_lookup_domain(r_u, &sid, r_u->status);
2385
2386         return r_u->status;
2387 }
2388
2389 /******************************************************************
2390 makes a SAMR_R_ENUM_DOMAINS structure.
2391 ********************************************************************/
2392
2393 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2394                         UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2395 {
2396         uint32 i;
2397         SAM_ENTRY *sam;
2398         UNISTR2 *uni_name;
2399
2400         DEBUG(5, ("make_enum_domains\n"));
2401
2402         *pp_sam = NULL;
2403         *pp_uni_name = NULL;
2404
2405         if (num_sam_entries == 0)
2406                 return True;
2407
2408         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
2409         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
2410
2411         if (sam == NULL || uni_name == NULL)
2412                 return False;
2413
2414         for (i = 0; i < num_sam_entries; i++) {
2415                 init_unistr2(&uni_name[i], doms[i], UNI_FLAGS_NONE);
2416                 init_sam_entry(&sam[i], &uni_name[i], 0);
2417         }
2418
2419         *pp_sam = sam;
2420         *pp_uni_name = uni_name;
2421
2422         return True;
2423 }
2424
2425 /**********************************************************************
2426  api_samr_enum_domains
2427  **********************************************************************/
2428
2429 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2430 {
2431         struct samr_info *info;
2432         uint32 num_entries = 2;
2433         fstring dom[2];
2434         const char *name;
2435
2436         r_u->status = NT_STATUS_OK;
2437         
2438         if (!find_policy_by_hnd(p, &q_u->pol, (void**)&info))
2439                 return NT_STATUS_INVALID_HANDLE;
2440         
2441         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, SA_RIGHT_SAM_ENUM_DOMAINS, "_samr_enum_domains"))) {
2442                 return r_u->status;
2443         }
2444
2445         name = get_global_sam_name();
2446
2447         fstrcpy(dom[0],name);
2448         strupper_m(dom[0]);
2449         fstrcpy(dom[1],"Builtin");
2450
2451         if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2452                 return NT_STATUS_NO_MEMORY;
2453
2454         init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2455
2456         return r_u->status;
2457 }
2458
2459 /*******************************************************************
2460  api_samr_open_alias
2461  ********************************************************************/
2462
2463 NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2464 {
2465         DOM_SID sid;
2466         POLICY_HND domain_pol = q_u->dom_pol;
2467         uint32 alias_rid = q_u->rid_alias;
2468         POLICY_HND *alias_pol = &r_u->pol;
2469         struct    samr_info *info = NULL;
2470         SEC_DESC *psd = NULL;
2471         uint32    acc_granted;
2472         uint32    des_access = q_u->access_mask;
2473         size_t    sd_size;
2474         NTSTATUS  status;
2475         SE_PRIV se_rights;
2476
2477         r_u->status = NT_STATUS_OK;
2478
2479         /* find the domain policy and get the SID / access bits stored in the domain policy */
2480         
2481         if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted) )
2482                 return NT_STATUS_INVALID_HANDLE;
2483         
2484         status = access_check_samr_function(acc_granted, 
2485                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_alias");
2486                 
2487         if ( !NT_STATUS_IS_OK(status) ) 
2488                 return status;
2489
2490         /* append the alias' RID to it */
2491         
2492         if (!sid_append_rid(&sid, alias_rid))
2493                 return NT_STATUS_NO_SUCH_USER;
2494                 
2495         /*check if access can be granted as requested by client. */
2496         
2497         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
2498         se_map_generic(&des_access,&ali_generic_mapping);
2499         
2500         se_priv_copy( &se_rights, &se_add_users );
2501         
2502         
2503         status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2504                 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access, 
2505                 &acc_granted, "_samr_open_alias");
2506                 
2507         if ( !NT_STATUS_IS_OK(status) )
2508                 return status;
2509
2510         /*
2511          * we should check if the rid really exist !!!
2512          * JFM.
2513          */
2514
2515         /* associate the user's SID with the new handle. */
2516         if ((info = get_samr_info_by_sid(&sid)) == NULL)
2517                 return NT_STATUS_NO_MEMORY;
2518                 
2519         info->acc_granted = acc_granted;
2520
2521         /* get a (unique) handle.  open a policy on it. */
2522         if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
2523                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2524
2525         return r_u->status;
2526 }
2527
2528 /*******************************************************************
2529  set_user_info_7
2530  ********************************************************************/
2531 static NTSTATUS set_user_info_7(const SAM_USER_INFO_7 *id7, SAM_ACCOUNT *pwd)
2532 {
2533         fstring new_name;
2534         SAM_ACCOUNT *check_acct = NULL;
2535         NTSTATUS rc;
2536         BOOL check_rc;
2537
2538         if (id7 == NULL) {
2539                 DEBUG(5, ("set_user_info_7: NULL id7\n"));
2540                 pdb_free_sam(&pwd);
2541                 return NT_STATUS_ACCESS_DENIED;
2542         }
2543
2544         if(!rpcstr_pull(new_name, id7->uni_name.buffer, sizeof(new_name), id7->uni_name.uni_str_len*2, 0)) {
2545                 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
2546                 pdb_free_sam(&pwd);
2547                 return NT_STATUS_ACCESS_DENIED;
2548         }
2549
2550         /* check to see if the new username already exists.  Note: we can't
2551            reliably lock all backends, so there is potentially the 
2552            possibility that a user can be created in between this check and
2553            the rename.  The rename should fail, but may not get the
2554            exact same failure status code.  I think this is small enough
2555            of a window for this type of operation and the results are
2556            simply that the rename fails with a slightly different status
2557            code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
2558
2559         pdb_init_sam(&check_acct);
2560         check_rc = pdb_getsampwnam(check_acct, new_name);
2561         pdb_free_sam(&check_acct);
2562
2563         if (check_rc == True) {
2564                 /* this account exists: say so */
2565                 return NT_STATUS_USER_EXISTS;
2566         }
2567
2568         rc = pdb_rename_sam_account(pwd, new_name);
2569
2570         pdb_free_sam(&pwd);
2571         return rc;
2572 }
2573
2574 /*******************************************************************
2575  set_user_info_16
2576  ********************************************************************/
2577
2578 static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, SAM_ACCOUNT *pwd)
2579 {
2580         if (id16 == NULL) {
2581                 DEBUG(5, ("set_user_info_16: NULL id16\n"));
2582                 pdb_free_sam(&pwd);
2583                 return False;
2584         }
2585         
2586         /* FIX ME: check if the value is really changed --metze */
2587         if (!pdb_set_acct_ctrl(pwd, id16->acb_info, PDB_CHANGED)) {
2588                 pdb_free_sam(&pwd);
2589                 return False;
2590         }
2591
2592         if(!pdb_update_sam_account(pwd)) {
2593                 pdb_free_sam(&pwd);
2594                 return False;
2595         }
2596
2597         pdb_free_sam(&pwd);
2598
2599         return True;
2600 }
2601
2602 /*******************************************************************
2603  set_user_info_18
2604  ********************************************************************/
2605
2606 static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, SAM_ACCOUNT *pwd)
2607 {
2608
2609         if (id18 == NULL) {
2610                 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
2611                 pdb_free_sam(&pwd);
2612                 return False;
2613         }
2614  
2615         if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd, PDB_CHANGED)) {
2616                 pdb_free_sam(&pwd);
2617                 return False;
2618         }
2619         if (!pdb_set_nt_passwd     (pwd, id18->nt_pwd, PDB_CHANGED)) {
2620                 pdb_free_sam(&pwd);
2621                 return False;
2622         }
2623         if (!pdb_set_pass_changed_now (pwd)) {
2624                 pdb_free_sam(&pwd);
2625                 return False; 
2626         }
2627  
2628         if(!pdb_update_sam_account(pwd)) {
2629                 pdb_free_sam(&pwd);
2630                 return False;
2631         }
2632
2633         pdb_free_sam(&pwd);
2634         return True;
2635 }
2636
2637 /*******************************************************************
2638  The GROUPSID field in the SAM_ACCOUNT changed. Try to tell unix.
2639  ********************************************************************/
2640 static BOOL set_unix_primary_group(SAM_ACCOUNT *sampass)
2641 {
2642         struct group *grp;
2643         gid_t gid;
2644
2645         if (!NT_STATUS_IS_OK(sid_to_gid(pdb_get_group_sid(sampass),
2646                                         &gid))) {
2647                 DEBUG(2,("Could not get gid for primary group of "
2648                          "user %s\n", pdb_get_username(sampass)));
2649                 return False;
2650         }
2651
2652         grp = getgrgid(gid);
2653
2654         if (grp == NULL) {
2655                 DEBUG(2,("Could not find primary group %lu for "
2656                          "user %s\n", (unsigned long)gid, 
2657                          pdb_get_username(sampass)));
2658                 return False;
2659         }
2660
2661         if (smb_set_primary_group(grp->gr_name,
2662                                   pdb_get_username(sampass)) != 0) {
2663                 DEBUG(2,("Could not set primary group for user %s to "
2664                          "%s\n",
2665                          pdb_get_username(sampass), grp->gr_name));
2666                 return False;
2667         }
2668
2669         return True;
2670 }
2671         
2672
2673 /*******************************************************************
2674  set_user_info_20
2675  ********************************************************************/
2676
2677 static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, SAM_ACCOUNT *pwd)
2678 {
2679         if (id20 == NULL) {
2680                 DEBUG(5, ("set_user_info_20: NULL id20\n"));
2681                 return False;
2682         }
2683  
2684         copy_id20_to_sam_passwd(pwd, id20);
2685
2686         /* write the change out */
2687         if(!pdb_update_sam_account(pwd)) {
2688                 pdb_free_sam(&pwd);
2689                 return False;
2690         }
2691
2692         pdb_free_sam(&pwd);
2693
2694         return True;
2695 }
2696 /*******************************************************************
2697  set_user_info_21
2698  ********************************************************************/
2699
2700 static BOOL set_user_info_21(SAM_USER_INFO_21 *id21, SAM_ACCOUNT *pwd)
2701 {
2702  
2703         if (id21 == NULL) {
2704                 DEBUG(5, ("set_user_info_21: NULL id21\n"));
2705                 return False;
2706         }
2707  
2708         copy_id21_to_sam_passwd(pwd, id21);
2709  
2710         /*
2711          * The funny part about the previous two calls is
2712          * that pwd still has the password hashes from the
2713          * passdb entry.  These have not been updated from
2714          * id21.  I don't know if they need to be set.    --jerry
2715          */
2716  
2717         if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
2718                 set_unix_primary_group(pwd);
2719
2720         /* write the change out */
2721         if(!pdb_update_sam_account(pwd)) {
2722                 pdb_free_sam(&pwd);
2723                 return False;
2724         }
2725
2726         pdb_free_sam(&pwd);
2727
2728         return True;
2729 }
2730
2731 /*******************************************************************
2732  set_user_info_23
2733  ********************************************************************/
2734
2735 static BOOL set_user_info_23(SAM_USER_INFO_23 *id23, SAM_ACCOUNT *pwd)
2736 {
2737         pstring plaintext_buf;
2738         uint32 len;
2739         uint16 acct_ctrl;
2740  
2741         if (id23 == NULL) {
2742                 DEBUG(5, ("set_user_info_23: NULL id23\n"));
2743                 return False;
2744         }
2745  
2746         DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
2747                   pdb_get_username(pwd)));
2748
2749         acct_ctrl = pdb_get_acct_ctrl(pwd);
2750
2751         if (!decode_pw_buffer(id23->pass, plaintext_buf, 256, &len, STR_UNICODE)) {
2752                 pdb_free_sam(&pwd);
2753                 return False;
2754         }
2755   
2756         if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
2757                 pdb_free_sam(&pwd);
2758                 return False;
2759         }
2760  
2761         copy_id23_to_sam_passwd(pwd, id23);
2762  
2763         /* if it's a trust account, don't update /etc/passwd */
2764         if (    ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
2765                 ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
2766                 ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
2767                 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
2768         } else  {
2769                 /* update the UNIX password */
2770                 if (lp_unix_password_sync() ) {
2771                         struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
2772                         if (!passwd) {
2773                                 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
2774                         }
2775                         
2776                         if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
2777                                 pdb_free_sam(&pwd);
2778                                 return False;
2779                         }
2780                 }
2781         }
2782  
2783         ZERO_STRUCT(plaintext_buf);
2784  
2785         if (IS_SAM_CHANGED(pwd, PDB_GROUPSID))
2786                 set_unix_primary_group(pwd);
2787
2788         if(!pdb_update_sam_account(pwd)) {
2789                 pdb_free_sam(&pwd);
2790                 return False;
2791         }
2792  
2793         pdb_free_sam(&pwd);
2794
2795         return True;
2796 }
2797
2798 /*******************************************************************
2799  set_user_info_pw
2800  ********************************************************************/
2801
2802 static BOOL set_user_info_pw(uint8 *pass, SAM_ACCOUNT *pwd)
2803 {
2804         uint32 len;
2805         pstring plaintext_buf;
2806         uint16 acct_ctrl;
2807  
2808         DEBUG(5, ("Attempting administrator password change for user %s\n",
2809                   pdb_get_username(pwd)));
2810
2811         acct_ctrl = pdb_get_acct_ctrl(pwd);
2812
2813         ZERO_STRUCT(plaintext_buf);
2814  
2815         if (!decode_pw_buffer(pass, plaintext_buf, 256, &len, STR_UNICODE)) {
2816                 pdb_free_sam(&pwd);
2817                 return False;
2818         }
2819
2820         if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
2821                 pdb_free_sam(&pwd);
2822                 return False;
2823         }
2824  
2825         /* if it's a trust account, don't update /etc/passwd */
2826         if ( ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
2827                 ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
2828                 ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
2829                 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
2830         } else {
2831                 /* update the UNIX password */
2832                 if (lp_unix_password_sync()) {
2833                         struct passwd *passwd = Get_Pwnam(pdb_get_username(pwd));
2834                         if (!passwd) {
2835                                 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
2836                         }
2837                         
2838                         if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
2839                                 pdb_free_sam(&pwd);
2840                                 return False;
2841                         }
2842                 }
2843         }
2844  
2845         ZERO_STRUCT(plaintext_buf);
2846  
2847         DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
2848  
2849         /* update the SAMBA password */
2850         if(!pdb_update_sam_account(pwd)) {
2851                 pdb_free_sam(&pwd);
2852                 return False;
2853         }
2854
2855         pdb_free_sam(&pwd);
2856
2857         return True;
2858 }
2859
2860 /*******************************************************************
2861  samr_reply_set_userinfo
2862  ********************************************************************/
2863
2864 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
2865 {
2866         SAM_ACCOUNT *pwd = NULL;
2867         DOM_SID sid;
2868         POLICY_HND *pol = &q_u->pol;
2869         uint16 switch_value = q_u->switch_value;
2870         SAM_USERINFO_CTR *ctr = q_u->ctr;
2871         uint32 acc_granted;
2872         uint32 acc_required;
2873         BOOL ret;
2874         BOOL has_enough_rights = False;
2875         uint32 acb_info;
2876
2877         DEBUG(5, ("_samr_set_userinfo: %d\n", __LINE__));
2878
2879         r_u->status = NT_STATUS_OK;
2880
2881         /* find the policy handle.  open a policy on it. */
2882         if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted))
2883                 return NT_STATUS_INVALID_HANDLE;
2884
2885         /* observed when joining an XP client to a Samba domain */
2886         
2887         acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;     
2888
2889         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo"))) {
2890                 return r_u->status;
2891         }
2892
2893         DEBUG(5, ("_samr_set_userinfo: sid:%s, level:%d\n", sid_string_static(&sid), switch_value));
2894
2895         if (ctr == NULL) {
2896                 DEBUG(5, ("_samr_set_userinfo: NULL info level\n"));
2897                 return NT_STATUS_INVALID_INFO_CLASS;
2898         }
2899         
2900         pdb_init_sam(&pwd);     
2901         
2902         become_root();
2903         ret = pdb_getsampwsid(pwd, &sid);
2904         unbecome_root();
2905         
2906         if ( !ret ) {
2907                 pdb_free_sam(&pwd);
2908                 return NT_STATUS_NO_SUCH_USER;
2909         }
2910         
2911         /* deal with machine password changes differently from userinfo changes */
2912         /* check to see if we have the sufficient rights */
2913         
2914         acb_info = pdb_get_acct_ctrl(pwd);
2915         if ( acb_info & ACB_WSTRUST ) 
2916                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
2917         else if ( acb_info & ACB_NORMAL )
2918                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
2919         else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
2920                 if ( lp_enable_privileges() )
2921                         has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
2922         }
2923         
2924         DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
2925                 p->pipe_user_name, has_enough_rights ? "" : " not"));
2926
2927         /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
2928         
2929         if ( has_enough_rights )                                
2930                 become_root(); 
2931         
2932         /* ok!  user info levels (lots: see MSDEV help), off we go... */
2933
2934         switch (switch_value) {
2935                 case 18:
2936                         if (!set_user_info_18(ctr->info.id18, pwd))
2937                                 r_u->status = NT_STATUS_ACCESS_DENIED;
2938                         break;
2939
2940                 case 24:
2941                         if (!p->session_key.length) {
2942                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
2943                         }
2944                         SamOEMhashBlob(ctr->info.id24->pass, 516, &p->session_key);
2945
2946                         dump_data(100, (char *)ctr->info.id24->pass, 516);
2947
2948                         if (!set_user_info_pw(ctr->info.id24->pass, pwd))
2949                                 r_u->status = NT_STATUS_ACCESS_DENIED;
2950                         break;
2951
2952                 case 25:
2953 #if 0
2954                         /*
2955                          * Currently we don't really know how to unmarshall
2956                          * the level 25 struct, and the password encryption
2957                          * is different. This is a placeholder for when we
2958                          * do understand it. In the meantime just return INVALID
2959                          * info level and W2K SP2 drops down to level 23... JRA.
2960                          */
2961
2962                         if (!p->session_key.length) {
2963                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
2964                         }
2965                         SamOEMhashBlob(ctr->info.id25->pass, 532, &p->session_key);
2966
2967                         dump_data(100, (char *)ctr->info.id25->pass, 532);
2968
2969                         if (!set_user_info_pw(ctr->info.id25->pass, &sid))
2970                                 r_u->status = NT_STATUS_ACCESS_DENIED;
2971                         break;
2972 #endif
2973                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
2974                         break;
2975
2976                 case 23:
2977                         if (!p->session_key.length) {
2978                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
2979                         }
2980                         SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
2981
2982                         dump_data(100, (char *)ctr->info.id23->pass, 516);
2983
2984                         if (!set_user_info_23(ctr->info.id23, pwd))
2985                                 r_u->status = NT_STATUS_ACCESS_DENIED;
2986                         break;
2987
2988                 default:
2989                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
2990         }
2991
2992         
2993         if ( has_enough_rights )                                
2994                 unbecome_root();
2995                 
2996         /* ================ END SeMachineAccountPrivilege BLOCK ================ */
2997
2998         return r_u->status;
2999 }
3000
3001 /*******************************************************************
3002  samr_reply_set_userinfo2
3003  ********************************************************************/
3004
3005 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
3006 {
3007         SAM_ACCOUNT *pwd = NULL;
3008         DOM_SID sid;
3009         SAM_USERINFO_CTR *ctr = q_u->ctr;
3010         POLICY_HND *pol = &q_u->pol;
3011         uint16 switch_value = q_u->switch_value;
3012         uint32 acc_granted;
3013         uint32 acc_required;
3014         BOOL ret;
3015         BOOL has_enough_rights = False;
3016         uint32 acb_info;
3017
3018         DEBUG(5, ("samr_reply_set_userinfo2: %d\n", __LINE__));
3019
3020         r_u->status = NT_STATUS_OK;
3021
3022         /* find the policy handle.  open a policy on it. */
3023         if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted))
3024                 return NT_STATUS_INVALID_HANDLE;
3025
3026         /* observed when joining XP client to Samba domain */
3027                 
3028         acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
3029         
3030         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo2"))) {
3031                 return r_u->status;
3032         }
3033
3034         DEBUG(5, ("samr_reply_set_userinfo2: sid:%s\n", sid_string_static(&sid)));
3035
3036         if (ctr == NULL) {
3037                 DEBUG(5, ("samr_reply_set_userinfo2: NULL info level\n"));
3038                 return NT_STATUS_INVALID_INFO_CLASS;
3039         }
3040
3041         switch_value=ctr->switch_value;
3042
3043         pdb_init_sam(&pwd);     
3044         
3045         become_root();
3046         ret = pdb_getsampwsid(pwd, &sid);
3047         unbecome_root();
3048         
3049         if ( !ret ) {
3050                 pdb_free_sam(&pwd);
3051                 return NT_STATUS_NO_SUCH_USER;
3052         }
3053         
3054         acb_info = pdb_get_acct_ctrl(pwd);
3055         if ( acb_info & ACB_WSTRUST ) 
3056                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3057         else if ( acb_info & ACB_NORMAL )
3058                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3059         else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3060                 if ( lp_enable_privileges() )
3061                         has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3062         }
3063         
3064         DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
3065                 p->pipe_user_name, has_enough_rights ? "" : " not"));
3066
3067         /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3068         
3069         if ( has_enough_rights )                                
3070                 become_root(); 
3071         
3072         /* ok!  user info levels (lots: see MSDEV help), off we go... */
3073         
3074         switch (switch_value) {
3075                 case 7:
3076                         r_u->status = set_user_info_7(ctr->info.id7, pwd);
3077                         break;
3078                 case 16:
3079                         if (!set_user_info_16(ctr->info.id16, pwd))
3080                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3081                         break;
3082                 case 18:
3083                         /* Used by AS/U JRA. */
3084                         if (!set_user_info_18(ctr->info.id18, pwd))
3085                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3086                         break;
3087                 case 20:
3088                         if (!set_user_info_20(ctr->info.id20, pwd))
3089                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3090                         break;
3091                 case 21:
3092                         if (!set_user_info_21(ctr->info.id21, pwd))
3093                                 return NT_STATUS_ACCESS_DENIED;
3094                         break;
3095                 default:
3096                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3097         }
3098
3099         if ( has_enough_rights )                                
3100                 unbecome_root();
3101                 
3102         /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3103
3104         return r_u->status;
3105 }
3106
3107 /*********************************************************************
3108  _samr_query_aliasmem
3109 *********************************************************************/
3110
3111 NTSTATUS _samr_query_useraliases(pipes_struct *p, SAMR_Q_QUERY_USERALIASES *q_u, SAMR_R_QUERY_USERALIASES *r_u)
3112 {
3113         size_t num_alias_rids;
3114         uint32 *alias_rids;
3115         struct samr_info *info = NULL;
3116         size_t i;
3117                 
3118         NTSTATUS ntstatus1;
3119         NTSTATUS ntstatus2;
3120
3121         DOM_SID *members;
3122         BOOL res;
3123
3124         r_u->status = NT_STATUS_OK;
3125
3126         DEBUG(5,("_samr_query_useraliases: %d\n", __LINE__));
3127
3128         /* find the policy handle.  open a policy on it. */
3129         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
3130                 return NT_STATUS_INVALID_HANDLE;
3131                 
3132         ntstatus1 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM, "_samr_query_useraliases");
3133         ntstatus2 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_query_useraliases");
3134         
3135         if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
3136                 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
3137                     !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
3138                         return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
3139                 }
3140         }               
3141
3142         if (!sid_check_is_domain(&info->sid) &&
3143             !sid_check_is_builtin(&info->sid))
3144                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3145
3146         members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, q_u->num_sids1);
3147
3148         if (members == NULL)
3149                 return NT_STATUS_NO_MEMORY;
3150
3151         for (i=0; i<q_u->num_sids1; i++)
3152                 sid_copy(&members[i], &q_u->sid[i].sid);
3153
3154         alias_rids = NULL;
3155         num_alias_rids = 0;
3156
3157         become_root();
3158         res = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
3159                                          q_u->num_sids1,
3160                                          &alias_rids, &num_alias_rids);
3161         unbecome_root();
3162
3163         if (!res)
3164                 return NT_STATUS_UNSUCCESSFUL;
3165
3166         init_samr_r_query_useraliases(r_u, num_alias_rids, alias_rids,
3167                                       NT_STATUS_OK);
3168         return NT_STATUS_OK;
3169 }
3170
3171 /*********************************************************************
3172  _samr_query_aliasmem
3173 *********************************************************************/
3174
3175 NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_R_QUERY_ALIASMEM *r_u)
3176 {
3177         size_t i;
3178         size_t num_sids = 0;
3179         DOM_SID2 *sid;
3180         DOM_SID *sids=NULL;
3181
3182         DOM_SID alias_sid;
3183
3184         uint32 acc_granted;
3185
3186         /* find the policy handle.  open a policy on it. */
3187         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted)) 
3188                 return NT_STATUS_INVALID_HANDLE;
3189         
3190         if (!NT_STATUS_IS_OK(r_u->status = 
3191                 access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_GET_MEMBERS, "_samr_query_aliasmem"))) {
3192                 return r_u->status;
3193         }
3194
3195         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3196
3197         if (!pdb_enum_aliasmem(&alias_sid, &sids, &num_sids))
3198                 return NT_STATUS_NO_SUCH_ALIAS;
3199
3200         sid = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_SID2, num_sids);        
3201         if (num_sids!=0 && sid == NULL) {
3202                 SAFE_FREE(sids);
3203                 return NT_STATUS_NO_MEMORY;
3204         }
3205
3206         for (i = 0; i < num_sids; i++) {
3207                 init_dom_sid2(&sid[i], &sids[i]);
3208         }
3209
3210         init_samr_r_query_aliasmem(r_u, num_sids, sid, NT_STATUS_OK);
3211
3212         SAFE_FREE(sids);
3213
3214         return NT_STATUS_OK;
3215 }
3216
3217 static void add_uid_to_array_unique(uid_t uid, uid_t **uids, int *num)
3218 {
3219         int i;
3220
3221         for (i=0; i<*num; i++) {
3222                 if ((*uids)[i] == uid)
3223                         return;
3224         }
3225         
3226         *uids = SMB_REALLOC_ARRAY(*uids, uid_t, *num+1);
3227
3228         if (*uids == NULL)
3229                 return;
3230
3231         (*uids)[*num] = uid;
3232         *num += 1;
3233 }
3234
3235
3236 static BOOL get_memberuids(gid_t gid, uid_t **uids, int *num)
3237 {
3238         struct group *grp;
3239         char **gr;
3240         struct sys_pwent *userlist, *user;
3241  
3242         *uids = NULL;
3243         *num = 0;
3244
3245         /* We only look at our own sam, so don't care about imported stuff */
3246
3247         winbind_off();
3248
3249         if ((grp = getgrgid(gid)) == NULL) {
3250                 winbind_on();
3251                 return False;
3252         }
3253
3254         /* Primary group members */
3255
3256         userlist = getpwent_list();
3257
3258         for (user = userlist; user != NULL; user = user->next) {
3259                 if (user->pw_gid != gid)
3260                         continue;
3261                 add_uid_to_array_unique(user->pw_uid, uids, num);
3262         }
3263
3264         pwent_free(userlist);
3265
3266         /* Secondary group members */
3267
3268         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
3269                 struct passwd *pw = getpwnam(*gr);
3270
3271                 if (pw == NULL)
3272                         continue;
3273                 add_uid_to_array_unique(pw->pw_uid, uids, num);
3274         }
3275
3276         winbind_on();
3277
3278         return True;
3279 }       
3280
3281 /*********************************************************************
3282  _samr_query_groupmem
3283 *********************************************************************/
3284
3285 NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_R_QUERY_GROUPMEM *r_u)
3286 {
3287         DOM_SID group_sid;
3288         fstring group_sid_str;
3289         size_t i, num_members;
3290
3291         uint32 *rid=NULL;
3292         uint32 *attr=NULL;
3293
3294         uint32 acc_granted;
3295
3296         NTSTATUS result;
3297
3298         /* find the policy handle.  open a policy on it. */
3299         if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted)) 
3300                 return NT_STATUS_INVALID_HANDLE;
3301                 
3302         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_GET_MEMBERS, "_samr_query_groupmem"))) {
3303                 return r_u->status;
3304         }
3305                 
3306         sid_to_string(group_sid_str, &group_sid);
3307         DEBUG(10, ("sid is %s\n", group_sid_str));
3308
3309         if (!sid_check_is_in_our_domain(&group_sid)) {
3310                 DEBUG(3, ("sid %s is not in our domain\n", group_sid_str));
3311                 return NT_STATUS_NO_SUCH_GROUP;
3312         }
3313
3314         DEBUG(10, ("lookup on Domain SID\n"));
3315
3316         become_root();
3317         result = pdb_enum_group_members(p->mem_ctx, &group_sid,
3318                                         &rid, &num_members);
3319         unbecome_root();
3320
3321         if (!NT_STATUS_IS_OK(result))
3322                 return result;
3323
3324         attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
3325         
3326         if ((num_members!=0) && (rid==NULL))
3327                 return NT_STATUS_NO_MEMORY;
3328         
3329         for (i=0; i<num_members; i++)
3330                 attr[i] = SID_NAME_USER;
3331
3332         init_samr_r_query_groupmem(r_u, num_members, rid, attr, NT_STATUS_OK);
3333
3334         return NT_STATUS_OK;
3335 }
3336
3337 /*********************************************************************
3338  _samr_add_aliasmem
3339 *********************************************************************/
3340
3341 NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_ADD_ALIASMEM *r_u)
3342 {
3343         DOM_SID alias_sid;
3344         uint32 acc_granted;
3345         SE_PRIV se_rights;
3346         BOOL can_add_accounts;
3347         BOOL ret;
3348
3349
3350         /* Find the policy handle. Open a policy on it. */
3351         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted)) 
3352                 return NT_STATUS_INVALID_HANDLE;
3353         
3354         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_ADD_MEMBER, "_samr_add_aliasmem"))) {
3355                 return r_u->status;
3356         }
3357                 
3358         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3359         
3360         se_priv_copy( &se_rights, &se_add_users );
3361         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3362
3363         /******** BEGIN SeAddUsers BLOCK *********/
3364         
3365         if ( can_add_accounts )
3366                 become_root();
3367         
3368         ret = pdb_add_aliasmem(&alias_sid, &q_u->sid.sid);
3369         
3370         if ( can_add_accounts )
3371                 unbecome_root();
3372                 
3373         /******** END SeAddUsers BLOCK *********/
3374         
3375         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3376 }
3377
3378 /*********************************************************************
3379  _samr_del_aliasmem
3380 *********************************************************************/
3381
3382 NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DEL_ALIASMEM *r_u)
3383 {
3384         DOM_SID alias_sid;
3385         uint32 acc_granted;
3386         SE_PRIV se_rights;
3387         BOOL can_add_accounts;
3388         BOOL ret;
3389
3390         /* Find the policy handle. Open a policy on it. */
3391         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted)) 
3392                 return NT_STATUS_INVALID_HANDLE;
3393         
3394         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_REMOVE_MEMBER, "_samr_del_aliasmem"))) {
3395                 return r_u->status;
3396         }
3397         
3398         DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
3399                    sid_string_static(&alias_sid)));
3400
3401         se_priv_copy( &se_rights, &se_add_users );
3402         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3403
3404         /******** BEGIN SeAddUsers BLOCK *********/
3405         
3406         if ( can_add_accounts )
3407                 become_root();
3408
3409         ret = pdb_del_aliasmem(&alias_sid, &q_u->sid.sid);
3410         
3411         if ( can_add_accounts )
3412                 unbecome_root();
3413                 
3414         /******** END SeAddUsers BLOCK *********/
3415         
3416         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
3417 }
3418
3419 /*********************************************************************
3420  _samr_add_groupmem
3421 *********************************************************************/
3422
3423 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
3424 {
3425         DOM_SID group_sid;
3426         DOM_SID user_sid;
3427         fstring group_sid_str;
3428         uid_t uid;
3429         struct passwd *pwd;
3430         struct group *grp;
3431         fstring grp_name;
3432         GROUP_MAP map;
3433         NTSTATUS ret;
3434         SAM_ACCOUNT *sam_user=NULL;
3435         BOOL check;
3436         uint32 acc_granted;
3437         SE_PRIV se_rights;
3438         BOOL can_add_accounts;
3439
3440         /* Find the policy handle. Open a policy on it. */
3441         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted)) 
3442                 return NT_STATUS_INVALID_HANDLE;
3443         
3444         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_ADD_MEMBER, "_samr_add_groupmem"))) {
3445                 return r_u->status;
3446         }
3447
3448         sid_to_string(group_sid_str, &group_sid);
3449         DEBUG(10, ("sid is %s\n", group_sid_str));
3450
3451         if (sid_compare(&group_sid, get_global_sam_sid())<=0)
3452                 return NT_STATUS_NO_SUCH_GROUP;
3453
3454         DEBUG(10, ("lookup on Domain SID\n"));
3455
3456         if(!get_domain_group_from_sid(group_sid, &map))
3457                 return NT_STATUS_NO_SUCH_GROUP;
3458
3459         sid_copy(&user_sid, get_global_sam_sid());
3460         sid_append_rid(&user_sid, q_u->rid);
3461
3462         ret = pdb_init_sam(&sam_user);
3463         if (!NT_STATUS_IS_OK(ret))
3464                 return ret;
3465         
3466         check = pdb_getsampwsid(sam_user, &user_sid);
3467         
3468         if (check != True) {
3469                 pdb_free_sam(&sam_user);
3470                 return NT_STATUS_NO_SUCH_USER;
3471         }
3472
3473         /* check a real user exist before we run the script to add a user to a group */
3474         if (!NT_STATUS_IS_OK(sid_to_uid(pdb_get_user_sid(sam_user), &uid))) {
3475                 pdb_free_sam(&sam_user);
3476                 return NT_STATUS_NO_SUCH_USER;
3477         }
3478
3479         pdb_free_sam(&sam_user);
3480
3481         if ((pwd=getpwuid_alloc(uid)) == NULL) {
3482                 return NT_STATUS_NO_SUCH_USER;
3483         }
3484
3485         if ((grp=getgrgid(map.gid)) == NULL) {
3486                 passwd_free(&pwd);
3487                 return NT_STATUS_NO_SUCH_GROUP;
3488         }
3489
3490         /* we need to copy the name otherwise it's overloaded in user_in_unix_group_list */
3491         fstrcpy(grp_name, grp->gr_name);
3492
3493         /* if the user is already in the group */
3494         if(user_in_unix_group_list(pwd->pw_name, grp_name)) {
3495                 passwd_free(&pwd);
3496                 return NT_STATUS_MEMBER_IN_GROUP;
3497         }
3498
3499         se_priv_copy( &se_rights, &se_add_users );
3500         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3501
3502         /******** BEGIN SeAddUsers BLOCK *********/
3503         
3504         if ( can_add_accounts )
3505                 become_root();
3506                 
3507         /* 
3508          * ok, the group exist, the user exist, the user is not in the group,
3509          *
3510          * we can (finally) add it to the group !
3511          */
3512
3513         smb_add_user_group(grp_name, pwd->pw_name);
3514
3515         if ( can_add_accounts )
3516                 unbecome_root();
3517                 
3518         /******** END SeAddUsers BLOCK *********/
3519         
3520         /* check if the user has been added then ... */
3521         if(!user_in_unix_group_list(pwd->pw_name, grp_name)) {
3522                 passwd_free(&pwd);
3523                 return NT_STATUS_MEMBER_NOT_IN_GROUP;           /* don't know what to reply else */
3524         }
3525
3526         passwd_free(&pwd);
3527         return NT_STATUS_OK;
3528 }
3529
3530 /*********************************************************************
3531  _samr_del_groupmem
3532 *********************************************************************/
3533
3534 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
3535 {
3536         DOM_SID group_sid;
3537         DOM_SID user_sid;
3538         SAM_ACCOUNT *sam_pass=NULL;
3539         GROUP_MAP map;
3540         fstring grp_name;
3541         struct group *grp;
3542         uint32 acc_granted;
3543         SE_PRIV se_rights;
3544         BOOL can_add_accounts;
3545
3546         /*
3547          * delete the group member named q_u->rid
3548          * who is a member of the sid associated with the handle
3549          * the rid is a user's rid as the group is a domain group.
3550          */
3551
3552         /* Find the policy handle. Open a policy on it. */
3553         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted)) 
3554                 return NT_STATUS_INVALID_HANDLE;
3555         
3556         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_REMOVE_MEMBER, "_samr_del_groupmem"))) {
3557                 return r_u->status;
3558         }
3559                 
3560         if (!sid_check_is_in_our_domain(&group_sid))
3561                 return NT_STATUS_NO_SUCH_GROUP;
3562
3563         sid_copy(&user_sid, get_global_sam_sid());
3564         sid_append_rid(&user_sid, q_u->rid);
3565
3566         if (!get_domain_group_from_sid(group_sid, &map))
3567                 return NT_STATUS_NO_SUCH_GROUP;
3568
3569         if ((grp=getgrgid(map.gid)) == NULL)
3570                 return NT_STATUS_NO_SUCH_GROUP;
3571
3572         /* we need to copy the name otherwise it's overloaded in user_in_group_list */
3573         fstrcpy(grp_name, grp->gr_name);
3574
3575         /* check if the user exists before trying to remove it from the group */
3576         pdb_init_sam(&sam_pass);
3577         if (!pdb_getsampwsid(sam_pass, &user_sid)) {
3578                 DEBUG(5,("User %s doesn't exist.\n", pdb_get_username(sam_pass)));
3579                 pdb_free_sam(&sam_pass);
3580                 return NT_STATUS_NO_SUCH_USER;
3581         }
3582
3583         /* if the user is not in the group */
3584         if (!user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3585                 pdb_free_sam(&sam_pass);
3586                 return NT_STATUS_MEMBER_NOT_IN_GROUP;
3587         }
3588         
3589
3590         se_priv_copy( &se_rights, &se_add_users );
3591         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3592
3593         /******** BEGIN SeAddUsers BLOCK *********/
3594         
3595         if ( can_add_accounts )
3596                 become_root();
3597                 
3598         smb_delete_user_group(grp_name, pdb_get_username(sam_pass));
3599
3600         if ( can_add_accounts )
3601                 unbecome_root();
3602                 
3603         /******** END SeAddUsers BLOCK *********/
3604         
3605         /* check if the user has been removed then ... */
3606         if (user_in_unix_group_list(pdb_get_username(sam_pass), grp_name)) {
3607                 pdb_free_sam(&sam_pass);
3608                 return NT_STATUS_ACCESS_DENIED;         /* don't know what to reply else */
3609         }
3610         
3611         pdb_free_sam(&sam_pass);
3612         return NT_STATUS_OK;
3613
3614 }
3615
3616 /****************************************************************************
3617  Delete a UNIX user on demand.
3618 ****************************************************************************/
3619
3620 static int smb_delete_user(const char *unix_user)
3621 {
3622         pstring del_script;
3623         int ret;
3624
3625         pstrcpy(del_script, lp_deluser_script());
3626         if (! *del_script)
3627                 return -1;
3628         all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
3629         ret = smbrun(del_script,NULL);
3630         flush_pwnam_cache();
3631         DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
3632
3633         return ret;
3634 }
3635
3636 /*********************************************************************
3637  _samr_delete_dom_user
3638 *********************************************************************/
3639
3640 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
3641 {
3642         DOM_SID user_sid;
3643         SAM_ACCOUNT *sam_pass=NULL;
3644         uint32 acc_granted;
3645         BOOL can_add_accounts;
3646         BOOL ret;
3647
3648         DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
3649
3650         /* Find the policy handle. Open a policy on it. */
3651         if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &user_sid, &acc_granted)) 
3652                 return NT_STATUS_INVALID_HANDLE;
3653                 
3654         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_user"))) {
3655                 return r_u->status;
3656         }
3657                 
3658         if (!sid_check_is_in_our_domain(&user_sid))
3659                 return NT_STATUS_CANNOT_DELETE;
3660
3661         /* check if the user exists before trying to delete */
3662         pdb_init_sam(&sam_pass);
3663         if(!pdb_getsampwsid(sam_pass, &user_sid)) {
3664                 DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n", 
3665                         sid_string_static(&user_sid)));
3666                 pdb_free_sam(&sam_pass);
3667                 return NT_STATUS_NO_SUCH_USER;
3668         }
3669         
3670         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3671
3672         /******** BEGIN SeAddUsers BLOCK *********/
3673         
3674         if ( can_add_accounts )
3675                 become_root();
3676
3677         /* First delete the samba side....
3678            code is order to prevent unnecessary returns out of the admin 
3679            block of code */
3680            
3681         if ( (ret = pdb_delete_sam_account(sam_pass)) == True ) {
3682                 /*
3683                  * Now delete the unix side ....
3684                  * note: we don't check if the delete really happened
3685                  * as the script is not necessary present
3686                  * and maybe the sysadmin doesn't want to delete the unix side
3687                  */
3688                 smb_delete_user( pdb_get_username(sam_pass) );
3689         }
3690         
3691         if ( can_add_accounts )
3692                 unbecome_root();
3693                 
3694         /******** END SeAddUsers BLOCK *********/
3695                 
3696         if ( !ret ) {
3697                 DEBUG(5,("_samr_delete_dom_user:Failed to delete entry for user %s.\n", pdb_get_username(sam_pass)));
3698                 pdb_free_sam(&sam_pass);
3699                 return NT_STATUS_CANNOT_DELETE;
3700         }
3701
3702
3703         pdb_free_sam(&sam_pass);
3704
3705         if (!close_policy_hnd(p, &q_u->user_pol))
3706                 return NT_STATUS_OBJECT_NAME_INVALID;
3707
3708         return NT_STATUS_OK;
3709 }
3710
3711 /*********************************************************************
3712  _samr_delete_dom_group
3713 *********************************************************************/
3714
3715 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
3716 {
3717         DOM_SID group_sid;
3718         DOM_SID dom_sid;
3719         uint32 group_rid;
3720         fstring group_sid_str;
3721         gid_t gid;
3722         struct group *grp;
3723         GROUP_MAP map;
3724         uint32 acc_granted;
3725         SE_PRIV se_rights;
3726         BOOL can_add_accounts;
3727         BOOL ret;
3728
3729         DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
3730
3731         /* Find the policy handle. Open a policy on it. */
3732         if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted)) 
3733                 return NT_STATUS_INVALID_HANDLE;
3734                 
3735         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_group"))) {
3736                 return r_u->status;
3737         }
3738                 
3739         sid_copy(&dom_sid, &group_sid);
3740         sid_to_string(group_sid_str, &dom_sid);
3741         sid_split_rid(&dom_sid, &group_rid);
3742
3743         DEBUG(10, ("sid is %s\n", group_sid_str));
3744
3745         /* we check if it's our SID before deleting */
3746         if (!sid_equal(&dom_sid, get_global_sam_sid()))
3747                 return NT_STATUS_NO_SUCH_GROUP;
3748
3749         DEBUG(10, ("lookup on Domain SID\n"));
3750
3751         if(!get_domain_group_from_sid(group_sid, &map))
3752                 return NT_STATUS_NO_SUCH_GROUP;
3753
3754         gid=map.gid;
3755
3756         /* check if group really exists */
3757         if ( (grp=getgrgid(gid)) == NULL) 
3758                 return NT_STATUS_NO_SUCH_GROUP;
3759
3760         se_priv_copy( &se_rights, &se_add_users );
3761         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3762
3763         /******** BEGIN SeAddUsers BLOCK *********/
3764         
3765         if ( can_add_accounts )
3766                 become_root();
3767
3768         /* delete mapping first */
3769         
3770         if ( (ret = pdb_delete_group_mapping_entry(group_sid)) == True ) {
3771                 smb_delete_group( grp->gr_name );
3772         }
3773
3774         if ( can_add_accounts )
3775                 unbecome_root();
3776                 
3777         /******** END SeAddUsers BLOCK *********/
3778         
3779         if ( !ret ) {
3780                 DEBUG(5,("_samr_delete_dom_group: Failed to delete mapping entry for group %s.\n", 
3781                         group_sid_str));
3782                 return NT_STATUS_ACCESS_DENIED;
3783         }
3784         
3785         /* don't check that the unix group has been deleted.  Work like 
3786            _samr_delet_dom_user() */
3787
3788         if (!close_policy_hnd(p, &q_u->group_pol))
3789                 return NT_STATUS_OBJECT_NAME_INVALID;
3790
3791         return NT_STATUS_OK;
3792 }
3793
3794 /*********************************************************************
3795  _samr_delete_dom_alias
3796 *********************************************************************/
3797
3798 NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, SAMR_R_DELETE_DOM_ALIAS *r_u)
3799 {
3800         DOM_SID alias_sid;
3801         uint32 acc_granted;
3802         SE_PRIV se_rights;
3803         BOOL can_add_accounts;
3804         BOOL ret;
3805
3806         DEBUG(5, ("_samr_delete_dom_alias: %d\n", __LINE__));
3807
3808         /* Find the policy handle. Open a policy on it. */
3809         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted)) 
3810                 return NT_STATUS_INVALID_HANDLE;
3811         
3812         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_alias"))) {
3813                 return r_u->status;
3814         }
3815
3816         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3817
3818         if (!sid_check_is_in_our_domain(&alias_sid))
3819                 return NT_STATUS_NO_SUCH_ALIAS;
3820                 
3821         DEBUG(10, ("lookup on Local SID\n"));
3822
3823         se_priv_copy( &se_rights, &se_add_users );
3824         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3825
3826         /******** BEGIN SeAddUsers BLOCK *********/
3827         
3828         if ( can_add_accounts )
3829                 become_root();
3830
3831         /* Have passdb delete the alias */
3832         ret = pdb_delete_alias(&alias_sid);
3833         
3834         if ( can_add_accounts )
3835                 unbecome_root();
3836                 
3837         /******** END SeAddUsers BLOCK *********/
3838
3839         if ( !ret )
3840                 return NT_STATUS_ACCESS_DENIED;
3841
3842         if (!close_policy_hnd(p, &q_u->alias_pol))
3843                 return NT_STATUS_OBJECT_NAME_INVALID;
3844
3845         return NT_STATUS_OK;
3846 }
3847
3848 /*********************************************************************
3849  _samr_create_dom_group
3850 *********************************************************************/
3851
3852 NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, SAMR_R_CREATE_DOM_GROUP *r_u)
3853 {
3854         DOM_SID dom_sid;
3855         DOM_SID info_sid;
3856         fstring name;
3857         fstring sid_string;
3858         struct group *grp;
3859         struct samr_info *info;
3860         uint32 acc_granted;
3861         gid_t gid;
3862         SE_PRIV se_rights;
3863         BOOL can_add_accounts;
3864         NTSTATUS result;
3865
3866         /* Find the policy handle. Open a policy on it. */
3867         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &dom_sid, &acc_granted)) 
3868                 return NT_STATUS_INVALID_HANDLE;
3869         
3870         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_GROUP, "_samr_create_dom_group"))) {
3871                 return r_u->status;
3872         }
3873                 
3874         if (!sid_equal(&dom_sid, get_global_sam_sid()))
3875                 return NT_STATUS_ACCESS_DENIED;
3876
3877         unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
3878
3879         /* check if group already exist */
3880         if ((grp=getgrnam(name)) != NULL)
3881                 return NT_STATUS_GROUP_EXISTS;
3882
3883         se_priv_copy( &se_rights, &se_add_users );
3884         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3885
3886         /******** BEGIN SeAddUsers BLOCK *********/
3887         
3888         if ( can_add_accounts )
3889                 become_root();
3890         
3891         /* check that we successfully create the UNIX group */
3892         
3893         result = NT_STATUS_ACCESS_DENIED;
3894         if ( (smb_create_group(name, &gid) == 0) && ((grp=getgrgid(gid)) != NULL) ) {
3895         
3896                 /* so far, so good */
3897                 
3898                 result = NT_STATUS_OK;
3899                 
3900                 r_u->rid = pdb_gid_to_group_rid( grp->gr_gid );
3901
3902                 /* add the group to the mapping table */
3903                 
3904                 sid_copy( &info_sid, get_global_sam_sid() );
3905                 sid_append_rid( &info_sid, r_u->rid );
3906                 sid_to_string( sid_string, &info_sid );
3907                 
3908                 /* reset the error code if we fail to add the mapping entry */
3909                 
3910                 if ( !add_initial_entry(grp->gr_gid, sid_string, SID_NAME_DOM_GRP, name, NULL) )
3911                         result = NT_STATUS_ACCESS_DENIED;
3912         }
3913
3914         if ( can_add_accounts )
3915                 unbecome_root();
3916                 
3917         /******** END SeAddUsers BLOCK *********/
3918         
3919         /* check if we should bail out here */
3920         
3921         if ( !NT_STATUS_IS_OK(result) )
3922                 return result;
3923         
3924         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3925                 return NT_STATUS_NO_MEMORY;
3926
3927
3928         /* they created it; let the user do what he wants with it */
3929
3930         info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
3931
3932         /* get a (unique) handle.  open a policy on it. */
3933         if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
3934                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3935
3936         return NT_STATUS_OK;
3937 }
3938
3939 /*********************************************************************
3940  _samr_create_dom_alias
3941 *********************************************************************/
3942
3943 NTSTATUS _samr_create_dom_alias(pipes_struct *p, SAMR_Q_CREATE_DOM_ALIAS *q_u, SAMR_R_CREATE_DOM_ALIAS *r_u)
3944 {
3945         DOM_SID dom_sid;
3946         DOM_SID info_sid;
3947         fstring name;
3948         struct samr_info *info;
3949         uint32 acc_granted;
3950         gid_t gid;
3951         NTSTATUS result;
3952         SE_PRIV se_rights;
3953         BOOL can_add_accounts;
3954
3955         /* Find the policy handle. Open a policy on it. */
3956         if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &dom_sid, &acc_granted)) 
3957                 return NT_STATUS_INVALID_HANDLE;
3958                 
3959         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_ALIAS, "_samr_create_alias"))) {
3960                 return r_u->status;
3961         }
3962                 
3963         if (!sid_equal(&dom_sid, get_global_sam_sid()))
3964                 return NT_STATUS_ACCESS_DENIED;
3965
3966         unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
3967
3968         se_priv_copy( &se_rights, &se_add_users );
3969         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3970
3971         /******** BEGIN SeAddUsers BLOCK *********/
3972         
3973         if ( can_add_accounts )
3974                 become_root();
3975
3976         /* Have passdb create the alias */
3977         result = pdb_create_alias(name, &r_u->rid);
3978
3979         if ( can_add_accounts )
3980                 unbecome_root();
3981                 
3982         /******** END SeAddUsers BLOCK *********/
3983
3984         if (!NT_STATUS_IS_OK(result))
3985                 return result;
3986
3987         sid_copy(&info_sid, get_global_sam_sid());
3988         sid_append_rid(&info_sid, r_u->rid);
3989
3990         if (!NT_STATUS_IS_OK(sid_to_gid(&info_sid, &gid)))
3991                 return NT_STATUS_ACCESS_DENIED;
3992
3993         /* check if the group has been successfully created */
3994         if ( getgrgid(gid) == NULL )
3995                 return NT_STATUS_ACCESS_DENIED;
3996
3997         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
3998                 return NT_STATUS_NO_MEMORY;
3999
4000         /* they created it; let the user do what he wants with it */
4001
4002         info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
4003
4004         /* get a (unique) handle.  open a policy on it. */
4005         if (!create_policy_hnd(p, &r_u->alias_pol, free_samr_info, (void *)info))
4006                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4007
4008         return NT_STATUS_OK;
4009 }
4010
4011 /*********************************************************************
4012  _samr_query_groupinfo
4013
4014 sends the name/comment pair of a domain group
4015 level 1 send also the number of users of that group
4016 *********************************************************************/
4017
4018 NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAMR_R_QUERY_GROUPINFO *r_u)
4019 {
4020         DOM_SID group_sid;
4021         GROUP_MAP map;
4022         DOM_SID *sids=NULL;
4023         uid_t *uids;
4024         int num=0;
4025         GROUP_INFO_CTR *ctr;
4026         uint32 acc_granted;
4027         BOOL ret;
4028
4029         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted)) 
4030                 return NT_STATUS_INVALID_HANDLE;
4031         
4032         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_LOOKUP_INFO, "_samr_query_groupinfo"))) {
4033                 return r_u->status;
4034         }
4035                 
4036         become_root();
4037         ret = get_domain_group_from_sid(group_sid, &map);
4038         unbecome_root();
4039         if (!ret)
4040                 return NT_STATUS_INVALID_HANDLE;
4041
4042         ctr=TALLOC_ZERO_P(p->mem_ctx, GROUP_INFO_CTR);
4043         if (ctr==NULL)
4044                 return NT_STATUS_NO_MEMORY;
4045
4046         switch (q_u->switch_level) {
4047                 case 1:
4048                         ctr->switch_value1 = 1;
4049                         if(!get_memberuids(map.gid, &uids, &num))
4050                                 return NT_STATUS_NO_SUCH_GROUP;
4051                         SAFE_FREE(uids);
4052                         init_samr_group_info1(&ctr->group.info1, map.nt_name, map.comment, num);
4053                         SAFE_FREE(sids);
4054                         break;
4055                 case 3:
4056                         ctr->switch_value1 = 3;
4057                         init_samr_group_info3(&ctr->group.info3);
4058                         break;
4059                 case 4:
4060                         ctr->switch_value1 = 4;
4061                         init_samr_group_info4(&ctr->group.info4, map.comment);
4062                         break;
4063                 default:
4064                         return NT_STATUS_INVALID_INFO_CLASS;
4065         }
4066
4067         init_samr_r_query_groupinfo(r_u, ctr, NT_STATUS_OK);
4068
4069         return NT_STATUS_OK;
4070 }
4071
4072 /*********************************************************************
4073  _samr_set_groupinfo
4074  
4075  update a domain group's comment.
4076 *********************************************************************/
4077
4078 NTSTATUS _samr_set_groupinfo(pipes_struct *p, SAMR_Q_SET_GROUPINFO *q_u, SAMR_R_SET_GROUPINFO *r_u)
4079 {
4080         DOM_SID group_sid;
4081         GROUP_MAP map;
4082         GROUP_INFO_CTR *ctr;
4083         uint32 acc_granted;
4084         BOOL ret;
4085         BOOL can_mod_accounts;
4086
4087         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted))
4088                 return NT_STATUS_INVALID_HANDLE;
4089         
4090         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_SET_INFO, "_samr_set_groupinfo"))) {
4091                 return r_u->status;
4092         }
4093                 
4094         if (!get_domain_group_from_sid(group_sid, &map))
4095                 return NT_STATUS_NO_SUCH_GROUP;
4096         
4097         ctr=q_u->ctr;
4098
4099         switch (ctr->switch_value1) {
4100                 case 1:
4101                         unistr2_to_ascii(map.comment, &(ctr->group.info1.uni_acct_desc), sizeof(map.comment)-1);
4102                         break;
4103                 case 4:
4104                         unistr2_to_ascii(map.comment, &(ctr->group.info4.uni_acct_desc), sizeof(map.comment)-1);
4105                         break;
4106                 default:
4107                         return NT_STATUS_INVALID_INFO_CLASS;
4108         }
4109
4110         can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4111
4112         /******** BEGIN SeAddUsers BLOCK *********/
4113
4114         if ( can_mod_accounts )
4115                 become_root();
4116           
4117         ret = pdb_update_group_mapping_entry(&map);
4118
4119         if ( can_mod_accounts )
4120                 unbecome_root();
4121
4122         /******** End SeAddUsers BLOCK *********/
4123
4124         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4125 }
4126
4127 /*********************************************************************
4128  _samr_set_aliasinfo
4129  
4130  update an alias's comment.
4131 *********************************************************************/
4132
4133 NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_SET_ALIASINFO *r_u)
4134 {
4135         DOM_SID group_sid;
4136         struct acct_info info;
4137         ALIAS_INFO_CTR *ctr;
4138         uint32 acc_granted;
4139         BOOL ret;
4140         BOOL can_mod_accounts;
4141
4142         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &group_sid, &acc_granted))
4143                 return NT_STATUS_INVALID_HANDLE;
4144         
4145         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_SET_INFO, "_samr_set_aliasinfo"))) {
4146                 return r_u->status;
4147         }
4148                 
4149         ctr=&q_u->ctr;
4150
4151         switch (ctr->level) {
4152                 case 3:
4153                         if ( ctr->alias.info3.description.string ) {
4154                                 unistr2_to_ascii( info.acct_desc, 
4155                                         ctr->alias.info3.description.string, 
4156                                         sizeof(info.acct_desc)-1 );
4157                         }
4158                         break;
4159                 default:
4160                         return NT_STATUS_INVALID_INFO_CLASS;
4161         }
4162
4163         can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4164
4165         /******** BEGIN SeAddUsers BLOCK *********/
4166
4167         if ( can_mod_accounts )
4168                 become_root();
4169
4170         ret = pdb_set_aliasinfo( &group_sid, &info );
4171
4172         if ( can_mod_accounts )
4173                 unbecome_root();
4174
4175         /******** End SeAddUsers BLOCK *********/
4176
4177         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4178 }
4179
4180 /*********************************************************************
4181  _samr_get_dom_pwinfo
4182 *********************************************************************/
4183
4184 NTSTATUS _samr_get_dom_pwinfo(pipes_struct *p, SAMR_Q_GET_DOM_PWINFO *q_u, SAMR_R_GET_DOM_PWINFO *r_u)
4185 {
4186         /* Perform access check.  Since this rpc does not require a
4187            policy handle it will not be caught by the access checks on
4188            SAMR_CONNECT or SAMR_CONNECT_ANON. */
4189
4190         if (!pipe_access_check(p)) {
4191                 DEBUG(3, ("access denied to samr_get_dom_pwinfo\n"));
4192                 r_u->status = NT_STATUS_ACCESS_DENIED;
4193                 return r_u->status;
4194         }
4195
4196         /* Actually, returning zeros here works quite well :-). */
4197
4198         return NT_STATUS_OK;
4199 }
4200
4201 /*********************************************************************
4202  _samr_open_group
4203 *********************************************************************/
4204
4205 NTSTATUS _samr_open_group(pipes_struct *p, SAMR_Q_OPEN_GROUP *q_u, SAMR_R_OPEN_GROUP *r_u)
4206 {
4207         DOM_SID sid;
4208         DOM_SID info_sid;
4209         GROUP_MAP map;
4210         struct samr_info *info;
4211         SEC_DESC         *psd = NULL;
4212         uint32            acc_granted;
4213         uint32            des_access = q_u->access_mask;
4214         size_t            sd_size;
4215         NTSTATUS          status;
4216         fstring sid_string;
4217         BOOL ret;
4218         SE_PRIV se_rights;
4219
4220         if (!get_lsa_policy_samr_sid(p, &q_u->domain_pol, &sid, &acc_granted)) 
4221                 return NT_STATUS_INVALID_HANDLE;
4222         
4223         status = access_check_samr_function(acc_granted, 
4224                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_group");
4225                 
4226         if ( !NT_STATUS_IS_OK(status) )
4227                 return status;
4228                 
4229         /*check if access can be granted as requested by client. */
4230         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
4231         se_map_generic(&des_access,&grp_generic_mapping);
4232
4233         se_priv_copy( &se_rights, &se_add_users );
4234
4235         status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
4236                 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access, 
4237                 &acc_granted, "_samr_open_group");
4238                 
4239         if ( !NT_STATUS_IS_OK(status) ) 
4240                 return status;
4241
4242         /* this should not be hard-coded like this */
4243         
4244         if (!sid_equal(&sid, get_global_sam_sid()))
4245                 return NT_STATUS_ACCESS_DENIED;
4246
4247         sid_copy(&info_sid, get_global_sam_sid());
4248         sid_append_rid(&info_sid, q_u->rid_group);
4249         sid_to_string(sid_string, &info_sid);
4250
4251         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4252                 return NT_STATUS_NO_MEMORY;
4253                 
4254         info->acc_granted = acc_granted;
4255
4256         DEBUG(10, ("_samr_open_group:Opening SID: %s\n", sid_string));
4257
4258         /* check if that group really exists */
4259         become_root();
4260         ret = get_domain_group_from_sid(info->sid, &map);
4261         unbecome_root();
4262         if (!ret)
4263                 return NT_STATUS_NO_SUCH_GROUP;
4264
4265         /* get a (unique) handle.  open a policy on it. */
4266         if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4267                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4268
4269         return NT_STATUS_OK;
4270 }
4271
4272 /*********************************************************************
4273  _samr_remove_sid_foreign_domain
4274 *********************************************************************/
4275
4276 NTSTATUS _samr_remove_sid_foreign_domain(pipes_struct *p, 
4277                                           SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN *q_u, 
4278                                           SAMR_R_REMOVE_SID_FOREIGN_DOMAIN *r_u)
4279 {
4280         DOM_SID                 delete_sid, domain_sid;
4281         uint32                  acc_granted;
4282         NTSTATUS                result;
4283         
4284         sid_copy( &delete_sid, &q_u->sid.sid );
4285         
4286         DEBUG(5,("_samr_remove_sid_foreign_domain: removing SID [%s]\n",
4287                 sid_string_static(&delete_sid)));
4288                 
4289         /* Find the policy handle. Open a policy on it. */
4290         
4291         if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &domain_sid,
4292                                      &acc_granted)) 
4293                 return NT_STATUS_INVALID_HANDLE;
4294         
4295         result = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, 
4296                 "_samr_remove_sid_foreign_domain");
4297                 
4298         if (!NT_STATUS_IS_OK(result)) 
4299                 return result;
4300                         
4301         DEBUG(8, ("_samr_remove_sid_foreign_domain:sid is %s\n", 
4302                 sid_string_static(&domain_sid)));
4303
4304         /* we can only delete a user from a group since we don't have 
4305            nested groups anyways.  So in the latter case, just say OK */
4306
4307         /* TODO: The above comment nowadays is bogus. Since we have nested
4308          * groups now, and aliases members are never reported out of the unix
4309          * group membership, the "just say OK" makes this call a no-op. For
4310          * us. This needs fixing however. */
4311
4312         /* I've only ever seen this in the wild when deleting a user from
4313          * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
4314          * is the user about to be deleted. I very much suspect this is the
4315          * only application of this call. To verify this, let people report
4316          * other cases. */
4317
4318         if (!sid_check_is_builtin(&domain_sid)) {
4319                 DEBUG(1,("_samr_remove_sid_foreign_domain: domain_sid = %s, "
4320                          "global_sam_sid() = %s\n",
4321                          sid_string_static(&domain_sid),
4322                          sid_string_static(get_global_sam_sid())));
4323                 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
4324                 return NT_STATUS_OK;
4325         }
4326
4327
4328         result = NT_STATUS_OK;
4329
4330         return result;
4331 }
4332
4333 /*******************************************************************
4334  _samr_unknown_2e
4335  ********************************************************************/
4336
4337 NTSTATUS _samr_unknown_2e(pipes_struct *p, SAMR_Q_UNKNOWN_2E *q_u, SAMR_R_UNKNOWN_2E *r_u)
4338 {
4339         struct samr_info *info = NULL;
4340         SAM_UNK_CTR *ctr;
4341         uint32 min_pass_len,pass_hist,flag;
4342         time_t u_expire, u_min_age;
4343         NTTIME nt_expire, nt_min_age;
4344
4345         time_t u_lock_duration, u_reset_time;
4346         NTTIME nt_lock_duration, nt_reset_time;
4347         uint32 lockout;
4348         
4349         time_t u_logout;
4350         NTTIME nt_logout;
4351
4352         uint32 num_users=0, num_groups=0, num_aliases=0;
4353
4354         uint32 account_policy_temp;
4355
4356         time_t seq_num;
4357         uint32 server_role;
4358
4359         if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL)
4360                 return NT_STATUS_NO_MEMORY;
4361
4362         ZERO_STRUCTP(ctr);
4363
4364         r_u->status = NT_STATUS_OK;
4365
4366         DEBUG(5,("_samr_unknown_2e: %d\n", __LINE__));
4367
4368         /* find the policy handle.  open a policy on it. */
4369         if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)&info))
4370                 return NT_STATUS_INVALID_HANDLE;
4371
4372         switch (q_u->switch_value) {
4373                 case 0x01:
4374                         pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
4375                         min_pass_len = account_policy_temp;
4376
4377                         pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
4378                         pass_hist = account_policy_temp;
4379
4380                         pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
4381                         flag = account_policy_temp;
4382
4383                         pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
4384                         u_expire = account_policy_temp;
4385
4386                         pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
4387                         u_min_age = account_policy_temp;
4388
4389                         unix_to_nt_time_abs(&nt_expire, u_expire);
4390                         unix_to_nt_time_abs(&nt_min_age, u_min_age);
4391
4392                         init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist, 
4393                                        flag, nt_expire, nt_min_age);
4394                         break;
4395                 case 0x02:
4396                         become_root();          
4397                         num_users = count_sam_users(&info->disp_info,
4398                                                     ACB_NORMAL);
4399                         num_groups = count_sam_groups(&info->disp_info);
4400                         unbecome_root();
4401
4402                         free_samr_db(info);
4403
4404                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4405                         u_logout = account_policy_temp;
4406
4407                         unix_to_nt_time_abs(&nt_logout, u_logout);
4408
4409                         if (!pdb_get_seq_num(&seq_num))
4410                                 seq_num = time(NULL);
4411
4412                         server_role = ROLE_DOMAIN_PDC;
4413                         if (lp_server_role() == ROLE_DOMAIN_BDC)
4414                                 server_role = ROLE_DOMAIN_BDC;
4415
4416                         init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num, 
4417                                        num_users, num_groups, num_aliases, nt_logout, server_role);
4418                         break;
4419                 case 0x03:
4420                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
4421                         u_logout = account_policy_temp;
4422
4423                         unix_to_nt_time_abs(&nt_logout, u_logout);
4424                         
4425                         init_unk_info3(&ctr->info.inf3, nt_logout);
4426                         break;
4427                 case 0x05:
4428                         init_unk_info5(&ctr->info.inf5, global_myname());
4429                         break;
4430                 case 0x06:
4431                         init_unk_info6(&ctr->info.inf6);
4432                         break;
4433                 case 0x07:
4434                         server_role = ROLE_DOMAIN_PDC;
4435                         if (lp_server_role() == ROLE_DOMAIN_BDC)
4436                                 server_role = ROLE_DOMAIN_BDC;
4437
4438                         init_unk_info7(&ctr->info.inf7, server_role);
4439                         break;
4440                 case 0x08:
4441                         if (!pdb_get_seq_num(&seq_num))
4442                                 seq_num = time(NULL);
4443
4444                         init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
4445                         break;
4446                 case 0x0c:
4447                         pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
4448                         u_lock_duration = account_policy_temp;
4449                         if (u_lock_duration != -1)
4450                                 u_lock_duration *= 60;
4451
4452                         pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
4453                         u_reset_time = account_policy_temp * 60;
4454
4455                         pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
4456                         lockout = account_policy_temp;
4457         
4458                         unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
4459                         unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
4460         
4461                         init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
4462                         break;
4463                 default:
4464                         return NT_STATUS_INVALID_INFO_CLASS;
4465         }
4466
4467         init_samr_r_samr_unknown_2e(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
4468
4469         DEBUG(5,("_samr_unknown_2e: %d\n", __LINE__));
4470
4471         return r_u->status;
4472 }
4473
4474 /*******************************************************************
4475  _samr_
4476  ********************************************************************/
4477
4478 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)
4479 {
4480         time_t u_expire, u_min_age;
4481         time_t u_logout;
4482         time_t u_lock_duration, u_reset_time;
4483
4484         r_u->status = NT_STATUS_OK;
4485
4486         DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4487
4488         /* find the policy handle.  open a policy on it. */
4489         if (!find_policy_by_hnd(p, &q_u->domain_pol, NULL))
4490                 return NT_STATUS_INVALID_HANDLE;
4491
4492         DEBUG(5,("_samr_set_dom_info: switch_value: %d\n", q_u->switch_value));
4493
4494         switch (q_u->switch_value) {
4495                 case 0x01:
4496                         u_expire=nt_time_to_unix_abs(&q_u->ctr->info.inf1.expire);
4497                         u_min_age=nt_time_to_unix_abs(&q_u->ctr->info.inf1.min_passwordage);
4498                         
4499                         pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)q_u->ctr->info.inf1.min_length_password);
4500                         pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)q_u->ctr->info.inf1.password_history);
4501                         pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)q_u->ctr->info.inf1.flag);
4502                         pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
4503                         pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
4504                         break;
4505                 case 0x02:
4506                         break;
4507                 case 0x03:
4508                         u_logout=nt_time_to_unix_abs(&q_u->ctr->info.inf3.logout);
4509                         pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
4510                         break;
4511                 case 0x05:
4512                         break;
4513                 case 0x06:
4514                         break;
4515                 case 0x07:
4516                         break;
4517                 case 0x0c:
4518                         u_lock_duration=nt_time_to_unix_abs(&q_u->ctr->info.inf12.duration);
4519                         if (u_lock_duration != -1)
4520                                 u_lock_duration /= 60;
4521
4522                         u_reset_time=nt_time_to_unix_abs(&q_u->ctr->info.inf12.reset_count)/60;
4523                         
4524                         pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
4525                         pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
4526                         pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)q_u->ctr->info.inf12.bad_attempt_lockout);
4527                         break;
4528                 default:
4529                         return NT_STATUS_INVALID_INFO_CLASS;
4530         }
4531
4532         init_samr_r_set_domain_info(r_u, NT_STATUS_OK);
4533
4534         DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4535
4536         return r_u->status;
4537 }