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