r16544: Fix bug #3864 reported by jason@ncac.gwu.edu.
[ira/wip.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-2005,
9  *  Copyright (C) Jean François Micouleau           1998-2001,
10  *  Copyright (C) Jim McDonough <jmcd@us.ibm.com>   2002,
11  *  Copyright (C) Gerald (Jerry) Carter             2003-2004,
12  *  Copyright (C) Simo Sorce                        2003.
13  *  Copyright (C) Volker Lendecke                   2005.
14  *
15  *  This program is free software; you can redistribute it and/or modify
16  *  it under the terms of the GNU General Public License as published by
17  *  the Free Software Foundation; either version 2 of the License, or
18  *  (at your option) any later version.
19  *
20  *  This program is distributed in the hope that it will be useful,
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *  GNU General Public License for more details.
24  *
25  *  You should have received a copy of the GNU General Public License
26  *  along with this program; if not, write to the Free Software
27  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 /*
31  * This is the implementation of the SAMR code.
32  */
33
34 #include "includes.h"
35
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_RPC_SRV
38
39 #define SAMR_USR_RIGHTS_WRITE_PW \
40                 ( READ_CONTROL_ACCESS           | \
41                   SA_RIGHT_USER_CHANGE_PASSWORD | \
42                   SA_RIGHT_USER_SET_LOC_COM )
43
44 #define DISP_INFO_CACHE_TIMEOUT 10
45
46 typedef struct disp_info {
47         struct disp_info *next, *prev;
48         TALLOC_CTX *mem_ctx;
49         DOM_SID sid; /* identify which domain this is. */
50         BOOL builtin_domain; /* Quick flag to check if this is the builtin domain. */
51         struct pdb_search *users; /* querydispinfo 1 and 4 */
52         struct pdb_search *machines; /* querydispinfo 2 */
53         struct pdb_search *groups; /* querydispinfo 3 and 5, enumgroups */
54         struct pdb_search *aliases; /* enumaliases */
55
56         uint16 enum_acb_mask;
57         struct pdb_search *enum_users; /* enumusers with a mask */
58
59
60         smb_event_id_t di_cache_timeout_event; /* cache idle timeout handler. */
61 } DISP_INFO;
62
63 /* We keep a static list of these by SID as modern clients close down
64    all resources between each request in a complete enumeration. */
65
66 static DISP_INFO *disp_info_list;
67
68 struct samr_info {
69         /* for use by the \PIPE\samr policy */
70         DOM_SID sid;
71         BOOL builtin_domain; /* Quick flag to check if this is the builtin domain. */
72         uint32 status; /* some sort of flag.  best to record it.  comes from opnum 0x39 */
73         uint32 acc_granted;
74         DISP_INFO *disp_info;
75         TALLOC_CTX *mem_ctx;
76 };
77
78 static struct generic_mapping sam_generic_mapping = {
79         GENERIC_RIGHTS_SAM_READ,
80         GENERIC_RIGHTS_SAM_WRITE,
81         GENERIC_RIGHTS_SAM_EXECUTE,
82         GENERIC_RIGHTS_SAM_ALL_ACCESS};
83 static struct generic_mapping dom_generic_mapping = {
84         GENERIC_RIGHTS_DOMAIN_READ,
85         GENERIC_RIGHTS_DOMAIN_WRITE,
86         GENERIC_RIGHTS_DOMAIN_EXECUTE,
87         GENERIC_RIGHTS_DOMAIN_ALL_ACCESS};
88 static struct generic_mapping usr_generic_mapping = {
89         GENERIC_RIGHTS_USER_READ,
90         GENERIC_RIGHTS_USER_WRITE,
91         GENERIC_RIGHTS_USER_EXECUTE,
92         GENERIC_RIGHTS_USER_ALL_ACCESS};
93 static struct generic_mapping grp_generic_mapping = {
94         GENERIC_RIGHTS_GROUP_READ,
95         GENERIC_RIGHTS_GROUP_WRITE,
96         GENERIC_RIGHTS_GROUP_EXECUTE,
97         GENERIC_RIGHTS_GROUP_ALL_ACCESS};
98 static struct generic_mapping ali_generic_mapping = {
99         GENERIC_RIGHTS_ALIAS_READ,
100         GENERIC_RIGHTS_ALIAS_WRITE,
101         GENERIC_RIGHTS_ALIAS_EXECUTE,
102         GENERIC_RIGHTS_ALIAS_ALL_ACCESS};
103
104 /*******************************************************************
105 *******************************************************************/
106
107 static NTSTATUS make_samr_object_sd( TALLOC_CTX *ctx, SEC_DESC **psd, size_t *sd_size,
108                                      struct generic_mapping *map,
109                                      DOM_SID *sid, uint32 sid_access )
110 {
111         DOM_SID domadmin_sid;
112         SEC_ACE ace[5];         /* at most 5 entries */
113         SEC_ACCESS mask;
114         size_t i = 0;
115
116         SEC_ACL *psa = NULL;
117
118         /* basic access for Everyone */
119
120         init_sec_access(&mask, map->generic_execute | map->generic_read );
121         init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
122
123         /* add Full Access 'BUILTIN\Administrators' and 'BUILTIN\Account Operators */
124
125         init_sec_access(&mask, map->generic_all);
126         
127         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
128         init_sec_ace(&ace[i++], &global_sid_Builtin_Account_Operators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
129
130         /* Add Full Access for Domain Admins if we are a DC */
131         
132         if ( IS_DC ) {
133                 sid_copy( &domadmin_sid, get_global_sam_sid() );
134                 sid_append_rid( &domadmin_sid, DOMAIN_GROUP_RID_ADMINS );
135                 init_sec_ace(&ace[i++], &domadmin_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
136         }
137
138         /* if we have a sid, give it some special access */
139
140         if ( sid ) {
141                 init_sec_access( &mask, sid_access );
142                 init_sec_ace(&ace[i++], sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
143         }
144
145         /* create the security descriptor */
146
147         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) == NULL)
148                 return NT_STATUS_NO_MEMORY;
149
150         if ((*psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, sd_size)) == NULL)
151                 return NT_STATUS_NO_MEMORY;
152
153         return NT_STATUS_OK;
154 }
155
156 /*******************************************************************
157  Checks if access to an object should be granted, and returns that
158  level of access for further checks.
159 ********************************************************************/
160
161 static NTSTATUS access_check_samr_object( SEC_DESC *psd, NT_USER_TOKEN *token, 
162                                           SE_PRIV *rights, uint32 rights_mask,
163                                           uint32 des_access, uint32 *acc_granted, 
164                                           const char *debug )
165 {
166         NTSTATUS status = NT_STATUS_ACCESS_DENIED;
167         uint32 saved_mask = 0;
168
169         /* check privileges; certain SAM access bits should be overridden 
170            by privileges (mostly having to do with creating/modifying/deleting 
171            users and groups) */
172         
173         if ( rights && user_has_any_privilege( token, rights ) ) {
174         
175                 saved_mask = (des_access & rights_mask);
176                 des_access &= ~saved_mask;
177                 
178                 DEBUG(4,("access_check_samr_object: user rights access mask [0x%x]\n",
179                         rights_mask));
180         }
181                 
182         
183         /* check the security descriptor first */
184         
185         if ( se_access_check(psd, token, des_access, acc_granted, &status) )
186                 goto done;
187         
188         /* give root a free pass */
189         
190         if ( geteuid() == sec_initial_uid() ) {
191         
192                 DEBUG(4,("%s: ACCESS should be DENIED  (requested: %#010x)\n", debug, des_access));
193                 DEBUGADD(4,("but overritten by euid == sec_initial_uid()\n"));
194                 
195                 *acc_granted = des_access;
196                 
197                 status = NT_STATUS_OK;
198                 goto done;
199         }
200         
201         
202 done:
203         /* add in any bits saved during the privilege check (only 
204            matters is status is ok) */
205         
206         *acc_granted |= rights_mask;
207
208         DEBUG(4,("%s: access %s (requested: 0x%08x, granted: 0x%08x)\n", 
209                 debug, NT_STATUS_IS_OK(status) ? "GRANTED" : "DENIED", 
210                 des_access, *acc_granted));
211         
212         return status;
213 }
214
215 /*******************************************************************
216  Checks if access to a function can be granted
217 ********************************************************************/
218
219 static NTSTATUS access_check_samr_function(uint32 acc_granted, uint32 acc_required, const char *debug)
220 {
221         DEBUG(5,("%s: access check ((granted: %#010x;  required: %#010x)\n",  
222                 debug, acc_granted, acc_required));
223
224         /* check the security descriptor first */
225         
226         if ( (acc_granted&acc_required) == acc_required )
227                 return NT_STATUS_OK;
228                 
229         /* give root a free pass */
230
231         if (geteuid() == sec_initial_uid()) {
232         
233                 DEBUG(4,("%s: ACCESS should be DENIED (granted: %#010x;  required: %#010x)\n",
234                         debug, acc_granted, acc_required));
235                 DEBUGADD(4,("but overwritten by euid == 0\n"));
236                 
237                 return NT_STATUS_OK;
238         }
239         
240         DEBUG(2,("%s: ACCESS DENIED (granted: %#010x;  required: %#010x)\n", 
241                 debug, acc_granted, acc_required));
242                 
243         return NT_STATUS_ACCESS_DENIED;
244 }
245
246 /*******************************************************************
247  Fetch or create a dispinfo struct.
248 ********************************************************************/
249
250 static DISP_INFO *get_samr_dispinfo_by_sid(DOM_SID *psid, const char *sid_str)
251 {
252         TALLOC_CTX *mem_ctx;
253         DISP_INFO *dpi;
254
255         /* There are two cases to consider here:
256            1) The SID is a domain SID and we look for an equality match, or
257            2) This is an account SID and so we return the DISP_INFO* for our 
258               domain */
259
260         if ( psid && sid_check_is_in_our_domain( psid ) ) {
261                 DEBUG(10,("get_samr_dispinfo_by_sid: Replacing %s with our domain SID\n",
262                         sid_str));
263                 psid = get_global_sam_sid();
264         }
265
266         for (dpi = disp_info_list; dpi; dpi = dpi->next) {
267                 if (sid_equal(psid, &dpi->sid)) {
268                         return dpi;
269                 }
270         }
271
272         /* This struct is never free'd - I'm using talloc so we
273            can get a list out of smbd using smbcontrol. There will
274            be one of these per SID we're authorative for. JRA. */
275
276         mem_ctx = talloc_init("DISP_INFO for domain sid %s", sid_str);
277
278         if ((dpi = TALLOC_ZERO_P(mem_ctx, DISP_INFO)) == NULL)
279                 return NULL;
280
281         dpi->mem_ctx = mem_ctx;
282
283         if (psid) {
284                 sid_copy( &dpi->sid, psid);
285                 dpi->builtin_domain = sid_check_is_builtin(psid);
286         } else {
287                 dpi->builtin_domain = False;
288         }
289
290         DLIST_ADD(disp_info_list, dpi);
291
292         return dpi;
293 }
294
295 /*******************************************************************
296  Create a samr_info struct.
297 ********************************************************************/
298
299 static struct samr_info *get_samr_info_by_sid(DOM_SID *psid)
300 {
301         struct samr_info *info;
302         fstring sid_str;
303         TALLOC_CTX *mem_ctx;
304         
305         if (psid) {
306                 sid_to_string(sid_str, psid);
307         } else {
308                 fstrcpy(sid_str,"(NULL)");
309         }
310
311         mem_ctx = talloc_init("samr_info for domain sid %s", sid_str);
312
313         if ((info = TALLOC_ZERO_P(mem_ctx, struct samr_info)) == NULL)
314                 return NULL;
315
316         DEBUG(10,("get_samr_info_by_sid: created new info for sid %s\n", sid_str));
317         if (psid) {
318                 sid_copy( &info->sid, psid);
319                 info->builtin_domain = sid_check_is_builtin(psid);
320         } else {
321                 DEBUG(10,("get_samr_info_by_sid: created new info for NULL sid.\n"));
322                 info->builtin_domain = False;
323         }
324         info->mem_ctx = mem_ctx;
325
326         info->disp_info = get_samr_dispinfo_by_sid(psid, sid_str);
327
328         if (!info->disp_info) {
329                 talloc_destroy(mem_ctx);
330                 return NULL;
331         }
332
333         return info;
334 }
335
336 /*******************************************************************
337  Function to free the per SID data.
338  ********************************************************************/
339
340 static void free_samr_cache(DISP_INFO *disp_info, const char *sid_str)
341 {
342         DEBUG(10,("free_samr_cache: deleting cache for SID %s\n", sid_str));
343
344         /* We need to become root here because the paged search might have to
345          * tell the LDAP server we're not interested in the rest anymore. */
346
347         become_root();
348
349         if (disp_info->users) {
350                 DEBUG(10,("free_samr_cache: deleting users cache\n"));
351                 pdb_search_destroy(disp_info->users);
352                 disp_info->users = NULL;
353         }
354         if (disp_info->machines) {
355                 DEBUG(10,("free_samr_cache: deleting machines cache\n"));
356                 pdb_search_destroy(disp_info->machines);
357                 disp_info->machines = NULL;
358         }
359         if (disp_info->groups) {
360                 DEBUG(10,("free_samr_cache: deleting groups cache\n"));
361                 pdb_search_destroy(disp_info->groups);
362                 disp_info->groups = NULL;
363         }
364         if (disp_info->aliases) {
365                 DEBUG(10,("free_samr_cache: deleting aliases cache\n"));
366                 pdb_search_destroy(disp_info->aliases);
367                 disp_info->aliases = NULL;
368         }
369         if (disp_info->enum_users) {
370                 DEBUG(10,("free_samr_cache: deleting enum_users cache\n"));
371                 pdb_search_destroy(disp_info->enum_users);
372                 disp_info->enum_users = NULL;
373         }
374         disp_info->enum_acb_mask = 0;
375
376         unbecome_root();
377 }
378
379 /*******************************************************************
380  Function to free the per handle data.
381  ********************************************************************/
382
383 static void free_samr_info(void *ptr)
384 {
385         struct samr_info *info=(struct samr_info *) ptr;
386
387         /* Only free the dispinfo cache if no one bothered to set up
388            a timeout. */
389
390         if (info->disp_info && info->disp_info->di_cache_timeout_event == (smb_event_id_t)0) {
391                 fstring sid_str;
392                 sid_to_string(sid_str, &info->disp_info->sid);
393                 free_samr_cache(info->disp_info, sid_str);
394         }
395
396         talloc_destroy(info->mem_ctx);
397 }
398
399 /*******************************************************************
400  Idle event handler. Throw away the disp info cache.
401  ********************************************************************/
402
403 static void disp_info_cache_idle_timeout_handler(void **private_data,
404                                         time_t *ev_interval,
405                                         time_t ev_now)
406 {
407         fstring sid_str;
408         DISP_INFO *disp_info = (DISP_INFO *)(*private_data);
409
410         sid_to_string(sid_str, &disp_info->sid);
411
412         free_samr_cache(disp_info, sid_str);
413
414         /* Remove the event. */
415         smb_unregister_idle_event(disp_info->di_cache_timeout_event);
416         disp_info->di_cache_timeout_event = (smb_event_id_t)0;
417
418         DEBUG(10,("disp_info_cache_idle_timeout_handler: caching timed out for SID %s at %u\n",
419                 sid_str, (unsigned int)ev_now));
420 }
421
422 /*******************************************************************
423  Setup cache removal idle event handler.
424  ********************************************************************/
425
426 static void set_disp_info_cache_timeout(DISP_INFO *disp_info, time_t secs_fromnow)
427 {
428         fstring sid_str;
429
430         sid_to_string(sid_str, &disp_info->sid);
431
432         /* Remove any pending timeout and update. */
433
434         if (disp_info->di_cache_timeout_event) {
435                 smb_unregister_idle_event(disp_info->di_cache_timeout_event);
436                 disp_info->di_cache_timeout_event = (smb_event_id_t)0;
437         }
438
439         DEBUG(10,("set_disp_info_cache_timeout: caching enumeration for SID %s for %u seconds\n",
440                 sid_str, (unsigned int)secs_fromnow ));
441
442         disp_info->di_cache_timeout_event =
443                 smb_register_idle_event(disp_info_cache_idle_timeout_handler,
444                                         disp_info,
445                                         secs_fromnow);
446 }
447
448 /*******************************************************************
449  Force flush any cache. We do this on any samr_set_xxx call.
450  We must also remove the timeout handler.
451  ********************************************************************/
452
453 static void force_flush_samr_cache(DISP_INFO *disp_info)
454 {
455         if (disp_info) {
456                 fstring sid_str;
457
458                 sid_to_string(sid_str, &disp_info->sid);
459                 if (disp_info->di_cache_timeout_event) {
460                         smb_unregister_idle_event(disp_info->di_cache_timeout_event);
461                         disp_info->di_cache_timeout_event = (smb_event_id_t)0;
462                         DEBUG(10,("force_flush_samr_cache: clearing idle event for SID %s\n",
463                                 sid_str));
464                 }
465                 free_samr_cache(disp_info, sid_str);
466         }
467 }
468
469 /*******************************************************************
470  Ensure password info is never given out. Paranioa... JRA.
471  ********************************************************************/
472
473 static void samr_clear_sam_passwd(struct samu *sam_pass)
474 {
475         
476         if (!sam_pass)
477                 return;
478
479         /* These now zero out the old password */
480
481         pdb_set_lanman_passwd(sam_pass, NULL, PDB_DEFAULT);
482         pdb_set_nt_passwd(sam_pass, NULL, PDB_DEFAULT);
483 }
484
485 static uint32 count_sam_users(struct disp_info *info, uint32 acct_flags)
486 {
487         struct samr_displayentry *entry;
488
489         if (info->builtin_domain) {
490                 /* No users in builtin. */
491                 return 0;
492         }
493
494         if (info->users == NULL) {
495                 info->users = pdb_search_users(acct_flags);
496                 if (info->users == NULL) {
497                         return 0;
498                 }
499         }
500         /* Fetch the last possible entry, thus trigger an enumeration */
501         pdb_search_entries(info->users, 0xffffffff, 1, &entry);
502
503         /* Ensure we cache this enumeration. */
504         set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
505
506         return info->users->num_entries;
507 }
508
509 static uint32 count_sam_groups(struct disp_info *info)
510 {
511         struct samr_displayentry *entry;
512
513         if (info->builtin_domain) {
514                 /* No groups in builtin. */
515                 return 0;
516         }
517
518         if (info->groups == NULL) {
519                 info->groups = pdb_search_groups();
520                 if (info->groups == NULL) {
521                         return 0;
522                 }
523         }
524         /* Fetch the last possible entry, thus trigger an enumeration */
525         pdb_search_entries(info->groups, 0xffffffff, 1, &entry);
526
527         /* Ensure we cache this enumeration. */
528         set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
529
530         return info->groups->num_entries;
531 }
532
533 static uint32 count_sam_aliases(struct disp_info *info)
534 {
535         struct samr_displayentry *entry;
536
537         if (info->aliases == NULL) {
538                 info->aliases = pdb_search_aliases(&info->sid);
539                 if (info->aliases == NULL) {
540                         return 0;
541                 }
542         }
543         /* Fetch the last possible entry, thus trigger an enumeration */
544         pdb_search_entries(info->aliases, 0xffffffff, 1, &entry);
545
546         /* Ensure we cache this enumeration. */
547         set_disp_info_cache_timeout(info, DISP_INFO_CACHE_TIMEOUT);
548
549         return info->aliases->num_entries;
550 }
551
552 /*******************************************************************
553  _samr_close_hnd
554  ********************************************************************/
555
556 NTSTATUS _samr_close_hnd(pipes_struct *p, SAMR_Q_CLOSE_HND *q_u, SAMR_R_CLOSE_HND *r_u)
557 {
558         r_u->status = NT_STATUS_OK;
559
560         /* close the policy handle */
561         if (!close_policy_hnd(p, &q_u->pol))
562                 return NT_STATUS_OBJECT_NAME_INVALID;
563
564         DEBUG(5,("samr_reply_close_hnd: %d\n", __LINE__));
565
566         return r_u->status;
567 }
568
569 /*******************************************************************
570  samr_reply_open_domain
571  ********************************************************************/
572
573 NTSTATUS _samr_open_domain(pipes_struct *p, SAMR_Q_OPEN_DOMAIN *q_u, SAMR_R_OPEN_DOMAIN *r_u)
574 {
575         struct    samr_info *info;
576         SEC_DESC *psd = NULL;
577         uint32    acc_granted;
578         uint32    des_access = q_u->flags;
579         NTSTATUS  status;
580         size_t    sd_size;
581         SE_PRIV se_rights;
582
583         r_u->status = NT_STATUS_OK;
584
585         /* find the connection policy handle. */
586         
587         if ( !find_policy_by_hnd(p, &q_u->pol, (void**)(void *)&info) )
588                 return NT_STATUS_INVALID_HANDLE;
589
590         status = access_check_samr_function( info->acc_granted, 
591                 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_open_domain" );
592                 
593         if ( !NT_STATUS_IS_OK(status) )
594                 return status;
595
596         /*check if access can be granted as requested by client. */
597         
598         make_samr_object_sd( p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0 );
599         se_map_generic( &des_access, &dom_generic_mapping );
600         
601         se_priv_copy( &se_rights, &se_machine_account );
602         se_priv_add( &se_rights, &se_add_users );
603
604         status = access_check_samr_object( psd, p->pipe_user.nt_user_token, 
605                 &se_rights, GENERIC_RIGHTS_DOMAIN_WRITE, des_access, 
606                 &acc_granted, "_samr_open_domain" );
607                 
608         if ( !NT_STATUS_IS_OK(status) )
609                 return status;
610
611         if (!sid_check_is_domain(&q_u->dom_sid.sid) &&
612             !sid_check_is_builtin(&q_u->dom_sid.sid)) {
613                 return NT_STATUS_NO_SUCH_DOMAIN;
614         }
615
616         /* associate the domain SID with the (unique) handle. */
617         if ((info = get_samr_info_by_sid(&q_u->dom_sid.sid))==NULL)
618                 return NT_STATUS_NO_MEMORY;
619         info->acc_granted = acc_granted;
620
621         /* get a (unique) handle.  open a policy on it. */
622         if (!create_policy_hnd(p, &r_u->domain_pol, free_samr_info, (void *)info))
623                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
624
625         DEBUG(5,("samr_open_domain: %d\n", __LINE__));
626
627         return r_u->status;
628 }
629
630 /*******************************************************************
631  _samr_get_usrdom_pwinfo
632  ********************************************************************/
633
634 NTSTATUS _samr_get_usrdom_pwinfo(pipes_struct *p, SAMR_Q_GET_USRDOM_PWINFO *q_u, SAMR_R_GET_USRDOM_PWINFO *r_u)
635 {
636         struct samr_info *info = NULL;
637
638         r_u->status = NT_STATUS_OK;
639
640         /* find the policy handle.  open a policy on it. */
641         if (!find_policy_by_hnd(p, &q_u->user_pol, (void **)(void *)&info))
642                 return NT_STATUS_INVALID_HANDLE;
643
644         if (!sid_check_is_in_our_domain(&info->sid))
645                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
646
647         init_samr_r_get_usrdom_pwinfo(r_u, NT_STATUS_OK);
648
649         DEBUG(5,("_samr_get_usrdom_pwinfo: %d\n", __LINE__));
650
651         /* 
652          * NT sometimes return NT_STATUS_ACCESS_DENIED
653          * I don't know yet why.
654          */
655
656         return r_u->status;
657 }
658
659 /*******************************************************************
660  _samr_set_sec_obj
661  ********************************************************************/
662
663 NTSTATUS _samr_set_sec_obj(pipes_struct *p, SAMR_Q_SET_SEC_OBJ *q_u, SAMR_R_SET_SEC_OBJ *r_u)
664 {
665         DEBUG(0,("_samr_set_sec_obj: Not yet implemented!\n"));
666         return NT_STATUS_NOT_IMPLEMENTED;
667 }
668
669 /*******************************************************************
670 ********************************************************************/
671
672 static BOOL get_lsa_policy_samr_sid( pipes_struct *p, POLICY_HND *pol, 
673                                         DOM_SID *sid, uint32 *acc_granted,
674                                         DISP_INFO **ppdisp_info)
675 {
676         struct samr_info *info = NULL;
677
678         /* find the policy handle.  open a policy on it. */
679         if (!find_policy_by_hnd(p, pol, (void **)(void *)&info))
680                 return False;
681
682         if (!info)
683                 return False;
684
685         *sid = info->sid;
686         *acc_granted = info->acc_granted;
687         if (ppdisp_info) {
688                 *ppdisp_info = info->disp_info;
689         }
690
691         return True;
692 }
693
694 /*******************************************************************
695  _samr_query_sec_obj
696  ********************************************************************/
697
698 NTSTATUS _samr_query_sec_obj(pipes_struct *p, SAMR_Q_QUERY_SEC_OBJ *q_u, SAMR_R_QUERY_SEC_OBJ *r_u)
699 {
700         DOM_SID pol_sid;
701         fstring str_sid;
702         SEC_DESC * psd = NULL;
703         uint32 acc_granted;
704         size_t sd_size;
705
706         r_u->status = NT_STATUS_OK;
707
708         /* Get the SID. */
709         if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &pol_sid, &acc_granted, NULL))
710                 return NT_STATUS_INVALID_HANDLE;
711
712         DEBUG(10,("_samr_query_sec_obj: querying security on SID: %s\n", sid_to_string(str_sid, &pol_sid)));
713
714         /* Check what typ of SID is beeing queried (e.g Domain SID, User SID, Group SID) */
715
716         /* To query the security of the SAM it self an invalid SID with S-0-0 is passed to this function */
717         if (pol_sid.sid_rev_num == 0) {
718                 DEBUG(5,("_samr_query_sec_obj: querying security on SAM\n"));
719                 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
720         } else if (sid_equal(&pol_sid,get_global_sam_sid())) { 
721                 /* check if it is our domain SID */
722                 DEBUG(5,("_samr_query_sec_obj: querying security on Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
723                 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
724         } else if (sid_equal(&pol_sid,&global_sid_Builtin)) {
725                 /* check if it is the Builtin  Domain */
726                 /* TODO: Builtin probably needs a different SD with restricted write access*/
727                 DEBUG(5,("_samr_query_sec_obj: querying security on Builtin Domain with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
728                 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &dom_generic_mapping, NULL, 0);
729         } else if (sid_check_is_in_our_domain(&pol_sid) ||
730                  sid_check_is_in_builtin(&pol_sid)) {
731                 /* TODO: different SDs have to be generated for aliases groups and users.
732                          Currently all three get a default user SD  */
733                 DEBUG(10,("_samr_query_sec_obj: querying security on Object with SID: %s\n", sid_to_string(str_sid, &pol_sid)));
734                 r_u->status = make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &pol_sid, SAMR_USR_RIGHTS_WRITE_PW);
735         } else {
736                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
737         }
738
739         if ((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
740                 return NT_STATUS_NO_MEMORY;
741
742         if (NT_STATUS_IS_OK(r_u->status))
743                 r_u->ptr = 1;
744
745         return r_u->status;
746 }
747
748 /*******************************************************************
749 makes a SAM_ENTRY / UNISTR2* structure from a user list.
750 ********************************************************************/
751
752 static NTSTATUS make_user_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
753                                          UNISTR2 **uni_name_pp,
754                                          uint32 num_entries, uint32 start_idx,
755                                          struct samr_displayentry *entries)
756 {
757         uint32 i;
758         SAM_ENTRY *sam;
759         UNISTR2 *uni_name;
760         
761         *sam_pp = NULL;
762         *uni_name_pp = NULL;
763
764         if (num_entries == 0)
765                 return NT_STATUS_OK;
766
767         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_entries);
768
769         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_entries);
770
771         if (sam == NULL || uni_name == NULL) {
772                 DEBUG(0, ("make_user_sam_entry_list: talloc_zero failed!\n"));
773                 return NT_STATUS_NO_MEMORY;
774         }
775
776         for (i = 0; i < num_entries; i++) {
777                 UNISTR2 uni_temp_name;
778                 /*
779                  * usrmgr expects a non-NULL terminated string with
780                  * trust relationships
781                  */
782                 if (entries[i].acct_flags & ACB_DOMTRUST) {
783                         init_unistr2(&uni_temp_name, entries[i].account_name,
784                                      UNI_FLAGS_NONE);
785                 } else {
786                         init_unistr2(&uni_temp_name, entries[i].account_name,
787                                      UNI_STR_TERMINATE);
788                 }
789
790                 init_sam_entry(&sam[i], &uni_temp_name, entries[i].rid);
791                 copy_unistr2(&uni_name[i], &uni_temp_name);
792         }
793
794         *sam_pp = sam;
795         *uni_name_pp = uni_name;
796         return NT_STATUS_OK;
797 }
798
799 /*******************************************************************
800  samr_reply_enum_dom_users
801  ********************************************************************/
802
803 NTSTATUS _samr_enum_dom_users(pipes_struct *p, SAMR_Q_ENUM_DOM_USERS *q_u, 
804                               SAMR_R_ENUM_DOM_USERS *r_u)
805 {
806         struct samr_info *info = NULL;
807         int num_account;
808         uint32 enum_context=q_u->start_idx;
809         enum remote_arch_types ra_type = get_remote_arch();
810         int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
811         uint32 max_entries = max_sam_entries;
812         struct samr_displayentry *entries = NULL;
813         
814         r_u->status = NT_STATUS_OK;
815
816         /* find the policy handle.  open a policy on it. */
817         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
818                 return NT_STATUS_INVALID_HANDLE;
819
820         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, 
821                                         SA_RIGHT_DOMAIN_ENUM_ACCOUNTS, 
822                                         "_samr_enum_dom_users"))) {
823                 return r_u->status;
824         }
825         
826         DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
827
828         if (info->builtin_domain) {
829                 /* No users in builtin. */
830                 init_samr_r_enum_dom_users(r_u, q_u->start_idx, 0);
831                 DEBUG(5,("_samr_enum_dom_users: No users in BUILTIN\n"));
832                 return r_u->status;
833         }
834
835         become_root();
836
837         /* AS ROOT !!!! */
838
839         if ((info->disp_info->enum_users != NULL) &&
840             (info->disp_info->enum_acb_mask != q_u->acb_mask)) {
841                 pdb_search_destroy(info->disp_info->enum_users);
842                 info->disp_info->enum_users = NULL;
843         }
844
845         if (info->disp_info->enum_users == NULL) {
846                 info->disp_info->enum_users = pdb_search_users(q_u->acb_mask);
847                 info->disp_info->enum_acb_mask = q_u->acb_mask;
848         }
849
850         if (info->disp_info->enum_users == NULL) {
851                 /* END AS ROOT !!!! */
852                 unbecome_root();
853                 return NT_STATUS_ACCESS_DENIED;
854         }
855
856         num_account = pdb_search_entries(info->disp_info->enum_users,
857                                          enum_context, max_entries,
858                                          &entries);
859
860         /* END AS ROOT !!!! */
861
862         unbecome_root();
863
864         if (num_account == 0) {
865                 DEBUG(5, ("_samr_enum_dom_users: enumeration handle over "
866                           "total entries\n"));
867                 return NT_STATUS_OK;
868         }
869
870         r_u->status = make_user_sam_entry_list(p->mem_ctx, &r_u->sam,
871                                                &r_u->uni_acct_name, 
872                                                num_account, enum_context,
873                                                entries);
874
875         if (!NT_STATUS_IS_OK(r_u->status))
876                 return r_u->status;
877
878         if (max_entries <= num_account) {
879                 r_u->status = STATUS_MORE_ENTRIES;
880         } else {
881                 r_u->status = NT_STATUS_OK;
882         }
883
884         /* Ensure we cache this enumeration. */
885         set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
886
887         DEBUG(5, ("_samr_enum_dom_users: %d\n", __LINE__));
888
889         init_samr_r_enum_dom_users(r_u, q_u->start_idx + num_account,
890                                    num_account);
891
892         DEBUG(5,("_samr_enum_dom_users: %d\n", __LINE__));
893
894         return r_u->status;
895 }
896
897 /*******************************************************************
898 makes a SAM_ENTRY / UNISTR2* structure from a group list.
899 ********************************************************************/
900
901 static void make_group_sam_entry_list(TALLOC_CTX *ctx, SAM_ENTRY **sam_pp,
902                                       UNISTR2 **uni_name_pp,
903                                       uint32 num_sam_entries,
904                                       struct samr_displayentry *entries)
905 {
906         uint32 i;
907         SAM_ENTRY *sam;
908         UNISTR2 *uni_name;
909
910         *sam_pp = NULL;
911         *uni_name_pp = NULL;
912
913         if (num_sam_entries == 0)
914                 return;
915
916         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
917         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
918
919         if (sam == NULL || uni_name == NULL) {
920                 DEBUG(0, ("NULL pointers in SAMR_R_QUERY_DISPINFO\n"));
921                 return;
922         }
923
924         for (i = 0; i < num_sam_entries; i++) {
925                 /*
926                  * JRA. I think this should include the null. TNG does not.
927                  */
928                 init_unistr2(&uni_name[i], entries[i].account_name,
929                              UNI_STR_TERMINATE);
930                 init_sam_entry(&sam[i], &uni_name[i], entries[i].rid);
931         }
932
933         *sam_pp = sam;
934         *uni_name_pp = uni_name;
935 }
936
937 /*******************************************************************
938  samr_reply_enum_dom_groups
939  ********************************************************************/
940
941 NTSTATUS _samr_enum_dom_groups(pipes_struct *p, SAMR_Q_ENUM_DOM_GROUPS *q_u, SAMR_R_ENUM_DOM_GROUPS *r_u)
942 {
943         struct samr_info *info = NULL;
944         struct samr_displayentry *groups;
945         uint32 num_groups;
946
947         r_u->status = NT_STATUS_OK;
948
949         /* find the policy handle.  open a policy on it. */
950         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
951                 return NT_STATUS_INVALID_HANDLE;
952
953         r_u->status = access_check_samr_function(info->acc_granted,
954                                                  SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
955                                                  "_samr_enum_dom_groups");
956         if (!NT_STATUS_IS_OK(r_u->status))
957                 return r_u->status;
958
959         DEBUG(5,("samr_reply_enum_dom_groups: %d\n", __LINE__));
960
961         if (info->builtin_domain) {
962                 /* No groups in builtin. */
963                 init_samr_r_enum_dom_groups(r_u, q_u->start_idx, 0);
964                 DEBUG(5,("_samr_enum_dom_users: No groups in BUILTIN\n"));
965                 return r_u->status;
966         }
967
968         /* the domain group array is being allocated in the function below */
969
970         become_root();
971
972         if (info->disp_info->groups == NULL) {
973                 info->disp_info->groups = pdb_search_groups();
974
975                 if (info->disp_info->groups == NULL) {
976                         unbecome_root();
977                         return NT_STATUS_ACCESS_DENIED;
978                 }
979         }
980
981         num_groups = pdb_search_entries(info->disp_info->groups, q_u->start_idx,
982                                         MAX_SAM_ENTRIES, &groups);
983         unbecome_root();
984         
985         /* Ensure we cache this enumeration. */
986         set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
987
988         make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
989                                   num_groups, groups);
990
991         init_samr_r_enum_dom_groups(r_u, q_u->start_idx, num_groups);
992
993         DEBUG(5,("samr_enum_dom_groups: %d\n", __LINE__));
994
995         return r_u->status;
996 }
997
998 /*******************************************************************
999  samr_reply_enum_dom_aliases
1000  ********************************************************************/
1001
1002 NTSTATUS _samr_enum_dom_aliases(pipes_struct *p, SAMR_Q_ENUM_DOM_ALIASES *q_u, SAMR_R_ENUM_DOM_ALIASES *r_u)
1003 {
1004         struct samr_info *info;
1005         struct samr_displayentry *aliases;
1006         uint32 num_aliases = 0;
1007
1008         /* find the policy handle.  open a policy on it. */
1009         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1010                 return NT_STATUS_INVALID_HANDLE;
1011
1012         r_u->status = access_check_samr_function(info->acc_granted,
1013                                                  SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
1014                                                  "_samr_enum_dom_aliases");
1015         if (!NT_STATUS_IS_OK(r_u->status))
1016                 return r_u->status;
1017
1018         DEBUG(5,("samr_reply_enum_dom_aliases: sid %s\n",
1019                  sid_string_static(&info->sid)));
1020
1021         become_root();
1022
1023         if (info->disp_info->aliases == NULL) {
1024                 info->disp_info->aliases = pdb_search_aliases(&info->sid);
1025                 if (info->disp_info->aliases == NULL) {
1026                         unbecome_root();
1027                         return NT_STATUS_ACCESS_DENIED;
1028                 }
1029         }
1030
1031         num_aliases = pdb_search_entries(info->disp_info->aliases, q_u->start_idx,
1032                                          MAX_SAM_ENTRIES, &aliases);
1033         unbecome_root();
1034         
1035         /* Ensure we cache this enumeration. */
1036         set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1037
1038         make_group_sam_entry_list(p->mem_ctx, &r_u->sam, &r_u->uni_grp_name,
1039                                   num_aliases, aliases);
1040
1041         init_samr_r_enum_dom_aliases(r_u, q_u->start_idx + num_aliases,
1042                                      num_aliases);
1043
1044         DEBUG(5,("samr_enum_dom_aliases: %d\n", __LINE__));
1045
1046         return r_u->status;
1047 }
1048
1049 /*******************************************************************
1050  samr_reply_query_dispinfo
1051  ********************************************************************/
1052
1053 NTSTATUS _samr_query_dispinfo(pipes_struct *p, SAMR_Q_QUERY_DISPINFO *q_u, 
1054                               SAMR_R_QUERY_DISPINFO *r_u)
1055 {
1056         struct samr_info *info = NULL;
1057         uint32 struct_size=0x20; /* W2K always reply that, client doesn't care */
1058         
1059         uint32 max_entries=q_u->max_entries;
1060         uint32 enum_context=q_u->start_idx;
1061         uint32 max_size=q_u->max_size;
1062
1063         SAM_DISPINFO_CTR *ctr;
1064         uint32 temp_size=0, total_data_size=0;
1065         NTSTATUS disp_ret = NT_STATUS_UNSUCCESSFUL;
1066         uint32 num_account = 0;
1067         enum remote_arch_types ra_type = get_remote_arch();
1068         int max_sam_entries = (ra_type == RA_WIN95) ? MAX_SAM_ENTRIES_W95 : MAX_SAM_ENTRIES_W2K;
1069         struct samr_displayentry *entries = NULL;
1070
1071         DEBUG(5, ("samr_reply_query_dispinfo: %d\n", __LINE__));
1072         r_u->status = NT_STATUS_UNSUCCESSFUL;
1073
1074         /* find the policy handle.  open a policy on it. */
1075         if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info))
1076                 return NT_STATUS_INVALID_HANDLE;
1077
1078         /*
1079          * calculate how many entries we will return.
1080          * based on 
1081          * - the number of entries the client asked
1082          * - our limit on that
1083          * - the starting point (enumeration context)
1084          * - the buffer size the client will accept
1085          */
1086
1087         /*
1088          * We are a lot more like W2K. Instead of reading the SAM
1089          * each time to find the records we need to send back,
1090          * we read it once and link that copy to the sam handle.
1091          * For large user list (over the MAX_SAM_ENTRIES)
1092          * it's a definitive win.
1093          * second point to notice: between enumerations
1094          * our sam is now the same as it's a snapshoot.
1095          * third point: got rid of the static SAM_USER_21 struct
1096          * no more intermediate.
1097          * con: it uses much more memory, as a full copy is stored
1098          * in memory.
1099          *
1100          * If you want to change it, think twice and think
1101          * of the second point , that's really important.
1102          *
1103          * JFM, 12/20/2001
1104          */
1105
1106         if ((q_u->switch_level < 1) || (q_u->switch_level > 5)) {
1107                 DEBUG(0,("_samr_query_dispinfo: Unknown info level (%u)\n",
1108                          (unsigned int)q_u->switch_level ));
1109                 return NT_STATUS_INVALID_INFO_CLASS;
1110         }
1111
1112         /* first limit the number of entries we will return */
1113         if(max_entries > max_sam_entries) {
1114                 DEBUG(5, ("samr_reply_query_dispinfo: client requested %d "
1115                           "entries, limiting to %d\n", max_entries,
1116                           max_sam_entries));
1117                 max_entries = max_sam_entries;
1118         }
1119
1120         /* calculate the size and limit on the number of entries we will
1121          * return */
1122
1123         temp_size=max_entries*struct_size;
1124         
1125         if (temp_size>max_size) {
1126                 max_entries=MIN((max_size/struct_size),max_entries);;
1127                 DEBUG(5, ("samr_reply_query_dispinfo: buffer size limits to "
1128                           "only %d entries\n", max_entries));
1129         }
1130
1131         if (!(ctr = TALLOC_ZERO_P(p->mem_ctx,SAM_DISPINFO_CTR)))
1132                 return NT_STATUS_NO_MEMORY;
1133
1134         ZERO_STRUCTP(ctr);
1135
1136         become_root();
1137
1138         /* THe following done as ROOT. Don't return without unbecome_root(). */
1139
1140         switch (q_u->switch_level) {
1141         case 0x1:
1142         case 0x4:
1143                 if (info->disp_info->users == NULL) {
1144                         info->disp_info->users = pdb_search_users(ACB_NORMAL);
1145                         if (info->disp_info->users == NULL) {
1146                                 unbecome_root();
1147                                 return NT_STATUS_ACCESS_DENIED;
1148                         }
1149                         DEBUG(10,("samr_reply_query_dispinfo: starting user enumeration at index %u\n",
1150                                 (unsigned  int)enum_context ));
1151                 } else {
1152                         DEBUG(10,("samr_reply_query_dispinfo: using cached user enumeration at index %u\n",
1153                                 (unsigned  int)enum_context ));
1154                 }
1155
1156                 num_account = pdb_search_entries(info->disp_info->users,
1157                                                  enum_context, max_entries,
1158                                                  &entries);
1159                 break;
1160         case 0x2:
1161                 if (info->disp_info->machines == NULL) {
1162                         info->disp_info->machines =
1163                                 pdb_search_users(ACB_WSTRUST|ACB_SVRTRUST);
1164                         if (info->disp_info->machines == NULL) {
1165                                 unbecome_root();
1166                                 return NT_STATUS_ACCESS_DENIED;
1167                         }
1168                         DEBUG(10,("samr_reply_query_dispinfo: starting machine enumeration at index %u\n",
1169                                 (unsigned  int)enum_context ));
1170                 } else {
1171                         DEBUG(10,("samr_reply_query_dispinfo: using cached machine enumeration at index %u\n",
1172                                 (unsigned  int)enum_context ));
1173                 }
1174
1175                 num_account = pdb_search_entries(info->disp_info->machines,
1176                                                  enum_context, max_entries,
1177                                                  &entries);
1178                 break;
1179         case 0x3:
1180         case 0x5:
1181                 if (info->disp_info->groups == NULL) {
1182                         info->disp_info->groups = pdb_search_groups();
1183                         if (info->disp_info->groups == NULL) {
1184                                 unbecome_root();
1185                                 return NT_STATUS_ACCESS_DENIED;
1186                         }
1187                         DEBUG(10,("samr_reply_query_dispinfo: starting group enumeration at index %u\n",
1188                                 (unsigned  int)enum_context ));
1189                 } else {
1190                         DEBUG(10,("samr_reply_query_dispinfo: using cached group enumeration at index %u\n",
1191                                 (unsigned  int)enum_context ));
1192                 }
1193
1194                 num_account = pdb_search_entries(info->disp_info->groups,
1195                                                  enum_context, max_entries,
1196                                                  &entries);
1197                 break;
1198         default:
1199                 unbecome_root();
1200                 smb_panic("info class changed");
1201                 break;
1202         }
1203         unbecome_root();
1204
1205         /* Now create reply structure */
1206         switch (q_u->switch_level) {
1207         case 0x1:
1208                 disp_ret = init_sam_dispinfo_1(p->mem_ctx, &ctr->sam.info1,
1209                                                num_account, enum_context,
1210                                                entries);
1211                 break;
1212         case 0x2:
1213                 disp_ret = init_sam_dispinfo_2(p->mem_ctx, &ctr->sam.info2,
1214                                                num_account, enum_context,
1215                                                entries);
1216                 break;
1217         case 0x3:
1218                 disp_ret = init_sam_dispinfo_3(p->mem_ctx, &ctr->sam.info3,
1219                                                num_account, enum_context,
1220                                                entries);
1221                 break;
1222         case 0x4:
1223                 disp_ret = init_sam_dispinfo_4(p->mem_ctx, &ctr->sam.info4,
1224                                                num_account, enum_context,
1225                                                entries);
1226                 break;
1227         case 0x5:
1228                 disp_ret = init_sam_dispinfo_5(p->mem_ctx, &ctr->sam.info5,
1229                                                num_account, enum_context,
1230                                                entries);
1231                 break;
1232         default:
1233                 smb_panic("info class changed");
1234                 break;
1235         }
1236
1237         if (!NT_STATUS_IS_OK(disp_ret))
1238                 return disp_ret;
1239
1240         /* calculate the total size */
1241         total_data_size=num_account*struct_size;
1242
1243         if (num_account) {
1244                 r_u->status = STATUS_MORE_ENTRIES;
1245         } else {
1246                 r_u->status = NT_STATUS_OK;
1247         }
1248
1249         /* Ensure we cache this enumeration. */
1250         set_disp_info_cache_timeout(info->disp_info, DISP_INFO_CACHE_TIMEOUT);
1251
1252         DEBUG(5, ("_samr_query_dispinfo: %d\n", __LINE__));
1253
1254         init_samr_r_query_dispinfo(r_u, num_account, total_data_size,
1255                                    temp_size, q_u->switch_level, ctr,
1256                                    r_u->status);
1257
1258         return r_u->status;
1259
1260 }
1261
1262 /*******************************************************************
1263  samr_reply_query_aliasinfo
1264  ********************************************************************/
1265
1266 NTSTATUS _samr_query_aliasinfo(pipes_struct *p, SAMR_Q_QUERY_ALIASINFO *q_u, SAMR_R_QUERY_ALIASINFO *r_u)
1267 {
1268         DOM_SID   sid;
1269         struct acct_info info;
1270         uint32    acc_granted;
1271         BOOL ret;
1272
1273         r_u->status = NT_STATUS_OK;
1274
1275         DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1276
1277         /* find the policy handle.  open a policy on it. */
1278         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
1279                 return NT_STATUS_INVALID_HANDLE;
1280         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_LOOKUP_INFO, "_samr_query_aliasinfo"))) {
1281                 return r_u->status;
1282         }
1283
1284         become_root();
1285         ret = pdb_get_aliasinfo(&sid, &info);
1286         unbecome_root();
1287         
1288         if ( !ret )
1289                 return NT_STATUS_NO_SUCH_ALIAS;
1290
1291         if ( !(r_u->ctr = TALLOC_ZERO_P( p->mem_ctx, ALIAS_INFO_CTR )) ) 
1292                 return NT_STATUS_NO_MEMORY;
1293
1294
1295         switch (q_u->level ) {
1296         case 1:
1297                 r_u->ctr->level = 1;
1298                 init_samr_alias_info1(&r_u->ctr->alias.info1, info.acct_name, 1, info.acct_desc);
1299                 break;
1300         case 3:
1301                 r_u->ctr->level = 3;
1302                 init_samr_alias_info3(&r_u->ctr->alias.info3, info.acct_desc);
1303                 break;
1304         default:
1305                 return NT_STATUS_INVALID_INFO_CLASS;
1306         }
1307
1308         DEBUG(5,("_samr_query_aliasinfo: %d\n", __LINE__));
1309
1310         return r_u->status;
1311 }
1312
1313 #if 0
1314 /*******************************************************************
1315  samr_reply_lookup_ids
1316  ********************************************************************/
1317
1318  uint32 _samr_lookup_ids(pipes_struct *p, SAMR_Q_LOOKUP_IDS *q_u, SAMR_R_LOOKUP_IDS *r_u)
1319 {
1320     uint32 rid[MAX_SAM_ENTRIES];
1321     int num_rids = q_u->num_sids1;
1322
1323     r_u->status = NT_STATUS_OK;
1324
1325     DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1326
1327     if (num_rids > MAX_SAM_ENTRIES) {
1328         num_rids = MAX_SAM_ENTRIES;
1329         DEBUG(5,("_samr_lookup_ids: truncating entries to %d\n", num_rids));
1330     }
1331
1332 #if 0
1333     int i;
1334     SMB_ASSERT_ARRAY(q_u->uni_user_name, num_rids);
1335
1336     for (i = 0; i < num_rids && status == 0; i++)
1337     {
1338         struct sam_passwd *sam_pass;
1339         fstring user_name;
1340
1341
1342         fstrcpy(user_name, unistrn2(q_u->uni_user_name[i].buffer,
1343                                     q_u->uni_user_name[i].uni_str_len));
1344
1345         /* find the user account */
1346         become_root();
1347         sam_pass = get_smb21pwd_entry(user_name, 0);
1348         unbecome_root();
1349
1350         if (sam_pass == NULL)
1351         {
1352             status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
1353             rid[i] = 0;
1354         }
1355         else
1356         {
1357             rid[i] = sam_pass->user_rid;
1358         }
1359     }
1360 #endif
1361
1362     num_rids = 1;
1363     rid[0] = BUILTIN_ALIAS_RID_USERS;
1364
1365     init_samr_r_lookup_ids(&r_u, num_rids, rid, NT_STATUS_OK);
1366
1367     DEBUG(5,("_samr_lookup_ids: %d\n", __LINE__));
1368
1369     return r_u->status;
1370 }
1371 #endif
1372
1373 /*******************************************************************
1374  _samr_lookup_names
1375  ********************************************************************/
1376
1377 NTSTATUS _samr_lookup_names(pipes_struct *p, SAMR_Q_LOOKUP_NAMES *q_u, SAMR_R_LOOKUP_NAMES *r_u)
1378 {
1379         uint32 rid[MAX_SAM_ENTRIES];
1380         enum SID_NAME_USE type[MAX_SAM_ENTRIES];
1381         int i;
1382         int num_rids = q_u->num_names2;
1383         DOM_SID pol_sid;
1384         fstring sid_str;
1385         uint32  acc_granted;
1386
1387         r_u->status = NT_STATUS_OK;
1388
1389         DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1390
1391         ZERO_ARRAY(rid);
1392         ZERO_ARRAY(type);
1393
1394         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL)) {
1395                 init_samr_r_lookup_names(p->mem_ctx, r_u, 0, NULL, NULL, NT_STATUS_OBJECT_TYPE_MISMATCH);
1396                 return r_u->status;
1397         }
1398         
1399         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, 0, "_samr_lookup_names"))) { /* Don't know the acc_bits yet */
1400                 return r_u->status;
1401         }
1402
1403         if (num_rids > MAX_SAM_ENTRIES) {
1404                 num_rids = MAX_SAM_ENTRIES;
1405                 DEBUG(5,("_samr_lookup_names: truncating entries to %d\n", num_rids));
1406         }
1407
1408         DEBUG(5,("_samr_lookup_names: looking name on SID %s\n", sid_to_string(sid_str, &pol_sid)));
1409         
1410         for (i = 0; i < num_rids; i++) {
1411                 fstring name;
1412                 int ret;
1413
1414                 r_u->status = NT_STATUS_NONE_MAPPED;
1415                 type[i] = SID_NAME_UNKNOWN;
1416
1417                 rid [i] = 0xffffffff;
1418
1419                 ret = rpcstr_pull(name, q_u->uni_name[i].buffer, sizeof(name), q_u->uni_name[i].uni_str_len*2, 0);
1420
1421                 if (ret <= 0) {
1422                         continue;
1423                 }
1424
1425                 if (sid_check_is_builtin(&pol_sid)) {
1426                         if (lookup_builtin_name(name, &rid[i])) {
1427                                 type[i] = SID_NAME_ALIAS;
1428                         }
1429                 } else {
1430                         lookup_global_sam_name(name, 0, &rid[i], &type[i]);
1431                 }
1432
1433                 if (type[i] != SID_NAME_UNKNOWN) {
1434                         r_u->status = NT_STATUS_OK;
1435                 }
1436         }
1437
1438         init_samr_r_lookup_names(p->mem_ctx, r_u, num_rids, rid, (uint32 *)type, r_u->status);
1439
1440         DEBUG(5,("_samr_lookup_names: %d\n", __LINE__));
1441
1442         return r_u->status;
1443 }
1444
1445 /*******************************************************************
1446  _samr_chgpasswd_user
1447  ********************************************************************/
1448
1449 NTSTATUS _samr_chgpasswd_user(pipes_struct *p, SAMR_Q_CHGPASSWD_USER *q_u, SAMR_R_CHGPASSWD_USER *r_u)
1450 {
1451         fstring user_name;
1452         fstring wks;
1453
1454         DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1455
1456         r_u->status = NT_STATUS_OK;
1457
1458         rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1459         rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1460
1461         DEBUG(5,("samr_chgpasswd_user: user: %s wks: %s\n", user_name, wks));
1462
1463         /*
1464          * Pass the user through the NT -> unix user mapping
1465          * function.
1466          */
1467  
1468         (void)map_username(user_name);
1469  
1470         /*
1471          * UNIX username case mangling not required, pass_oem_change 
1472          * is case insensitive.
1473          */
1474
1475         r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1476                                 q_u->nt_newpass.pass, q_u->nt_oldhash.hash, NULL);
1477
1478         init_samr_r_chgpasswd_user(r_u, r_u->status);
1479
1480         DEBUG(5,("_samr_chgpasswd_user: %d\n", __LINE__));
1481
1482         return r_u->status;
1483 }
1484
1485 /*******************************************************************
1486  _samr_chgpasswd_user3
1487  ********************************************************************/
1488
1489 NTSTATUS _samr_chgpasswd_user3(pipes_struct *p, SAMR_Q_CHGPASSWD_USER3 *q_u, SAMR_R_CHGPASSWD_USER3 *r_u)
1490 {
1491         fstring user_name;
1492         fstring wks;
1493         uint32 reject_reason;
1494         SAM_UNK_INFO_1 *info = NULL;
1495         SAMR_CHANGE_REJECT *reject = NULL;
1496
1497         DEBUG(5,("_samr_chgpasswd_user3: %d\n", __LINE__));
1498
1499         rpcstr_pull(user_name, q_u->uni_user_name.buffer, sizeof(user_name), q_u->uni_user_name.uni_str_len*2, 0);
1500         rpcstr_pull(wks, q_u->uni_dest_host.buffer, sizeof(wks), q_u->uni_dest_host.uni_str_len*2,0);
1501
1502         DEBUG(5,("_samr_chgpasswd_user3: user: %s wks: %s\n", user_name, wks));
1503
1504         /*
1505          * Pass the user through the NT -> unix user mapping
1506          * function.
1507          */
1508  
1509         (void)map_username(user_name);
1510  
1511         /*
1512          * UNIX username case mangling not required, pass_oem_change 
1513          * is case insensitive.
1514          */
1515
1516         r_u->status = pass_oem_change(user_name, q_u->lm_newpass.pass, q_u->lm_oldhash.hash,
1517                                       q_u->nt_newpass.pass, q_u->nt_oldhash.hash, &reject_reason);
1518
1519         if (NT_STATUS_EQUAL(r_u->status, NT_STATUS_PASSWORD_RESTRICTION) || 
1520             NT_STATUS_EQUAL(r_u->status, NT_STATUS_ACCOUNT_RESTRICTION)) {
1521
1522                 uint32 min_pass_len,pass_hist,password_properties;
1523                 time_t u_expire, u_min_age;
1524                 NTTIME nt_expire, nt_min_age;
1525                 uint32 account_policy_temp;
1526
1527                 if ((info = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_INFO_1)) == NULL) {
1528                         return NT_STATUS_NO_MEMORY;
1529                 }
1530
1531                 if ((reject = TALLOC_ZERO_P(p->mem_ctx, SAMR_CHANGE_REJECT)) == NULL) {
1532                         return NT_STATUS_NO_MEMORY;
1533                 }
1534
1535                 ZERO_STRUCTP(info);
1536                 ZERO_STRUCTP(reject);
1537
1538                 become_root();
1539
1540                 /* AS ROOT !!! */
1541
1542                 pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
1543                 min_pass_len = account_policy_temp;
1544
1545                 pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
1546                 pass_hist = account_policy_temp;
1547
1548                 pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
1549                 password_properties = account_policy_temp;
1550
1551                 pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
1552                 u_expire = account_policy_temp;
1553
1554                 pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
1555                 u_min_age = account_policy_temp;
1556
1557                 /* !AS ROOT */
1558                 
1559                 unbecome_root();
1560
1561                 unix_to_nt_time_abs(&nt_expire, u_expire);
1562                 unix_to_nt_time_abs(&nt_min_age, u_min_age);
1563
1564                 init_unk_info1(info, (uint16)min_pass_len, (uint16)pass_hist, 
1565                                password_properties, nt_expire, nt_min_age);
1566
1567                 reject->reject_reason = reject_reason;
1568         }
1569         
1570         init_samr_r_chgpasswd_user3(r_u, r_u->status, reject, info);
1571
1572         DEBUG(5,("_samr_chgpasswd_user3: %d\n", __LINE__));
1573
1574         return r_u->status;
1575 }
1576
1577 /*******************************************************************
1578 makes a SAMR_R_LOOKUP_RIDS structure.
1579 ********************************************************************/
1580
1581 static BOOL make_samr_lookup_rids(TALLOC_CTX *ctx, uint32 num_names,
1582                                   const char **names, UNIHDR **pp_hdr_name,
1583                                   UNISTR2 **pp_uni_name)
1584 {
1585         uint32 i;
1586         UNIHDR *hdr_name=NULL;
1587         UNISTR2 *uni_name=NULL;
1588
1589         *pp_uni_name = NULL;
1590         *pp_hdr_name = NULL;
1591
1592         if (num_names != 0) {
1593                 hdr_name = TALLOC_ZERO_ARRAY(ctx, UNIHDR, num_names);
1594                 if (hdr_name == NULL)
1595                         return False;
1596
1597                 uni_name = TALLOC_ZERO_ARRAY(ctx,UNISTR2, num_names);
1598                 if (uni_name == NULL)
1599                         return False;
1600         }
1601
1602         for (i = 0; i < num_names; i++) {
1603                 DEBUG(10, ("names[%d]:%s\n", i, names[i] && *names[i] ? names[i] : ""));
1604                 init_unistr2(&uni_name[i], names[i], UNI_FLAGS_NONE);
1605                 init_uni_hdr(&hdr_name[i], &uni_name[i]);
1606         }
1607
1608         *pp_uni_name = uni_name;
1609         *pp_hdr_name = hdr_name;
1610
1611         return True;
1612 }
1613
1614 /*******************************************************************
1615  _samr_lookup_rids
1616  ********************************************************************/
1617
1618 NTSTATUS _samr_lookup_rids(pipes_struct *p, SAMR_Q_LOOKUP_RIDS *q_u, SAMR_R_LOOKUP_RIDS *r_u)
1619 {
1620         const char **names;
1621         uint32 *attrs = NULL;
1622         UNIHDR *hdr_name = NULL;
1623         UNISTR2 *uni_name = NULL;
1624         DOM_SID pol_sid;
1625         int num_rids = q_u->num_rids1;
1626         uint32 acc_granted;
1627         
1628         r_u->status = NT_STATUS_OK;
1629
1630         DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1631
1632         /* find the policy handle.  open a policy on it. */
1633         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &pol_sid, &acc_granted, NULL))
1634                 return NT_STATUS_INVALID_HANDLE;
1635
1636         if (num_rids > 1000) {
1637                 DEBUG(0, ("Got asked for %d rids (more than 1000) -- according "
1638                           "to samba4 idl this is not possible\n", num_rids));
1639                 return NT_STATUS_UNSUCCESSFUL;
1640         }
1641
1642         names = TALLOC_ZERO_ARRAY(p->mem_ctx, const char *, num_rids);
1643         attrs = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_rids);
1644
1645         if ((num_rids != 0) && ((names == NULL) || (attrs == NULL)))
1646                 return NT_STATUS_NO_MEMORY;
1647
1648         become_root();  /* lookup_sid can require root privs */
1649         r_u->status = pdb_lookup_rids(&pol_sid, num_rids, q_u->rid,
1650                                       names, attrs);
1651         unbecome_root();
1652
1653         if ( NT_STATUS_EQUAL(r_u->status, NT_STATUS_NONE_MAPPED) && (num_rids == 0) ) {
1654                 r_u->status = NT_STATUS_OK;
1655         }
1656
1657         if(!make_samr_lookup_rids(p->mem_ctx, num_rids, names,
1658                                   &hdr_name, &uni_name))
1659                 return NT_STATUS_NO_MEMORY;
1660
1661         init_samr_r_lookup_rids(r_u, num_rids, hdr_name, uni_name, attrs);
1662
1663         DEBUG(5,("_samr_lookup_rids: %d\n", __LINE__));
1664
1665         return r_u->status;
1666 }
1667
1668 /*******************************************************************
1669  _samr_open_user. Safe - gives out no passwd info.
1670  ********************************************************************/
1671
1672 NTSTATUS _samr_open_user(pipes_struct *p, SAMR_Q_OPEN_USER *q_u, SAMR_R_OPEN_USER *r_u)
1673 {
1674         struct samu *sampass=NULL;
1675         DOM_SID sid;
1676         POLICY_HND domain_pol = q_u->domain_pol;
1677         POLICY_HND *user_pol = &r_u->user_pol;
1678         struct samr_info *info = NULL;
1679         SEC_DESC *psd = NULL;
1680         uint32    acc_granted;
1681         uint32    des_access = q_u->access_mask;
1682         size_t    sd_size;
1683         BOOL ret;
1684         NTSTATUS nt_status;
1685         SE_PRIV se_rights;
1686
1687         r_u->status = NT_STATUS_OK;
1688
1689         /* find the domain policy handle and get domain SID / access bits in the domain policy. */
1690         
1691         if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
1692                 return NT_STATUS_INVALID_HANDLE;
1693         
1694         nt_status = access_check_samr_function( acc_granted, 
1695                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_user" );
1696                 
1697         if ( !NT_STATUS_IS_OK(nt_status) )
1698                 return nt_status;
1699
1700         if ( !(sampass = samu_new( p->mem_ctx )) ) {
1701                 return NT_STATUS_NO_MEMORY;
1702         }
1703
1704         /* append the user's RID to it */
1705         
1706         if (!sid_append_rid(&sid, q_u->user_rid))
1707                 return NT_STATUS_NO_SUCH_USER;
1708         
1709         /* check if access can be granted as requested by client. */
1710         
1711         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping, &sid, SAMR_USR_RIGHTS_WRITE_PW);
1712         se_map_generic(&des_access, &usr_generic_mapping);
1713         
1714         se_priv_copy( &se_rights, &se_machine_account );
1715         se_priv_add( &se_rights, &se_add_users );
1716         
1717         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
1718                 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access, 
1719                 &acc_granted, "_samr_open_user");
1720                 
1721         if ( !NT_STATUS_IS_OK(nt_status) )
1722                 return nt_status;
1723
1724         become_root();
1725         ret=pdb_getsampwsid(sampass, &sid);
1726         unbecome_root();
1727
1728         /* check that the SID exists in our domain. */
1729         if (ret == False) {
1730                 return NT_STATUS_NO_SUCH_USER;
1731         }
1732
1733         TALLOC_FREE(sampass);
1734
1735         /* associate the user's SID and access bits with the new handle. */
1736         if ((info = get_samr_info_by_sid(&sid)) == NULL)
1737                 return NT_STATUS_NO_MEMORY;
1738         info->acc_granted = acc_granted;
1739
1740         /* get a (unique) handle.  open a policy on it. */
1741         if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info))
1742                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1743
1744         return r_u->status;
1745 }
1746
1747 /*************************************************************************
1748  get_user_info_7. Safe. Only gives out account_name.
1749  *************************************************************************/
1750
1751 static NTSTATUS get_user_info_7(TALLOC_CTX *mem_ctx, SAM_USER_INFO_7 *id7, DOM_SID *user_sid)
1752 {
1753         struct samu *smbpass=NULL;
1754         BOOL ret;
1755
1756         if ( !(smbpass = samu_new( mem_ctx )) ) {
1757                 return NT_STATUS_NO_MEMORY;
1758         }
1759         
1760         become_root();
1761         ret = pdb_getsampwsid(smbpass, user_sid);
1762         unbecome_root();
1763
1764         if ( !ret ) {
1765                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1766                 return NT_STATUS_NO_SUCH_USER;
1767         }
1768
1769         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1770
1771         ZERO_STRUCTP(id7);
1772         init_sam_user_info7(id7, pdb_get_username(smbpass) );
1773
1774         TALLOC_FREE(smbpass);
1775
1776         return NT_STATUS_OK;
1777 }
1778
1779 /*************************************************************************
1780  get_user_info_9. Only gives out primary group SID.
1781  *************************************************************************/
1782 static NTSTATUS get_user_info_9(TALLOC_CTX *mem_ctx, SAM_USER_INFO_9 * id9, DOM_SID *user_sid)
1783 {
1784         struct samu *smbpass=NULL;
1785         BOOL ret;
1786
1787         if ( !(smbpass = samu_new( mem_ctx )) ) {
1788                 return NT_STATUS_NO_MEMORY;
1789         }
1790
1791         become_root();
1792         ret = pdb_getsampwsid(smbpass, user_sid);
1793         unbecome_root();
1794
1795         if (ret==False) {
1796                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1797                 return NT_STATUS_NO_SUCH_USER;
1798         }
1799
1800         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1801
1802         ZERO_STRUCTP(id9);
1803         init_sam_user_info9(id9, pdb_get_group_rid(smbpass) );
1804
1805         TALLOC_FREE(smbpass);
1806
1807         return NT_STATUS_OK;
1808 }
1809
1810 /*************************************************************************
1811  get_user_info_16. Safe. Only gives out acb bits.
1812  *************************************************************************/
1813
1814 static NTSTATUS get_user_info_16(TALLOC_CTX *mem_ctx, SAM_USER_INFO_16 *id16, DOM_SID *user_sid)
1815 {
1816         struct samu *smbpass=NULL;
1817         BOOL ret;
1818
1819         if ( !(smbpass = samu_new( mem_ctx )) ) {
1820                 return NT_STATUS_NO_MEMORY;
1821         }
1822
1823         become_root();
1824         ret = pdb_getsampwsid(smbpass, user_sid);
1825         unbecome_root();
1826
1827         if (ret==False) {
1828                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1829                 return NT_STATUS_NO_SUCH_USER;
1830         }
1831
1832         DEBUG(3,("User:[%s]\n", pdb_get_username(smbpass) ));
1833
1834         ZERO_STRUCTP(id16);
1835         init_sam_user_info16(id16, pdb_get_acct_ctrl(smbpass) );
1836
1837         TALLOC_FREE(smbpass);
1838
1839         return NT_STATUS_OK;
1840 }
1841
1842 /*************************************************************************
1843  get_user_info_18. OK - this is the killer as it gives out password info.
1844  Ensure that this is only allowed on an encrypted connection with a root
1845  user. JRA. 
1846  *************************************************************************/
1847
1848 static NTSTATUS get_user_info_18(pipes_struct *p, TALLOC_CTX *mem_ctx, SAM_USER_INFO_18 * id18, DOM_SID *user_sid)
1849 {
1850         struct samu *smbpass=NULL;
1851         BOOL ret;
1852
1853         if (p->auth.auth_type != PIPE_AUTH_TYPE_NTLMSSP || p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
1854                 return NT_STATUS_ACCESS_DENIED;
1855         }
1856
1857         if (p->auth.auth_level != PIPE_AUTH_LEVEL_PRIVACY) {
1858                 return NT_STATUS_ACCESS_DENIED;
1859         }
1860
1861         /*
1862          * Do *NOT* do become_root()/unbecome_root() here ! JRA.
1863          */
1864
1865         if ( !(smbpass = samu_new( mem_ctx )) ) {
1866                 return NT_STATUS_NO_MEMORY;
1867         }
1868
1869         ret = pdb_getsampwsid(smbpass, user_sid);
1870
1871         if (ret == False) {
1872                 DEBUG(4, ("User %s not found\n", sid_string_static(user_sid)));
1873                 TALLOC_FREE(smbpass);
1874                 return (geteuid() == (uid_t)0) ? NT_STATUS_NO_SUCH_USER : NT_STATUS_ACCESS_DENIED;
1875         }
1876
1877         DEBUG(3,("User:[%s] 0x%x\n", pdb_get_username(smbpass), pdb_get_acct_ctrl(smbpass) ));
1878
1879         if ( pdb_get_acct_ctrl(smbpass) & ACB_DISABLED) {
1880                 TALLOC_FREE(smbpass);
1881                 return NT_STATUS_ACCOUNT_DISABLED;
1882         }
1883
1884         ZERO_STRUCTP(id18);
1885         init_sam_user_info18(id18, pdb_get_lanman_passwd(smbpass), pdb_get_nt_passwd(smbpass));
1886         
1887         TALLOC_FREE(smbpass);
1888
1889         return NT_STATUS_OK;
1890 }
1891
1892 /*************************************************************************
1893  get_user_info_20
1894  *************************************************************************/
1895
1896 static NTSTATUS get_user_info_20(TALLOC_CTX *mem_ctx, SAM_USER_INFO_20 *id20, DOM_SID *user_sid)
1897 {
1898         struct samu *sampass=NULL;
1899         BOOL ret;
1900
1901         if ( !(sampass = samu_new( mem_ctx )) ) {
1902                 return NT_STATUS_NO_MEMORY;
1903         }
1904
1905         become_root();
1906         ret = pdb_getsampwsid(sampass, user_sid);
1907         unbecome_root();
1908
1909         if (ret == False) {
1910                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1911                 return NT_STATUS_NO_SUCH_USER;
1912         }
1913
1914         samr_clear_sam_passwd(sampass);
1915
1916         DEBUG(3,("User:[%s]\n",  pdb_get_username(sampass) ));
1917
1918         ZERO_STRUCTP(id20);
1919         init_sam_user_info20A(id20, sampass);
1920         
1921         TALLOC_FREE(sampass);
1922
1923         return NT_STATUS_OK;
1924 }
1925
1926 /*************************************************************************
1927  get_user_info_21
1928  *************************************************************************/
1929
1930 static NTSTATUS get_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21, 
1931                                  DOM_SID *user_sid, DOM_SID *domain_sid)
1932 {
1933         struct samu *sampass=NULL;
1934         BOOL ret;
1935         NTSTATUS nt_status;
1936
1937         if ( !(sampass = samu_new( mem_ctx )) ) {
1938                 return NT_STATUS_NO_MEMORY;
1939         }
1940
1941         become_root();
1942         ret = pdb_getsampwsid(sampass, user_sid);
1943         unbecome_root();
1944
1945         if (ret == False) {
1946                 DEBUG(4,("User %s not found\n", sid_string_static(user_sid)));
1947                 return NT_STATUS_NO_SUCH_USER;
1948         }
1949
1950         samr_clear_sam_passwd(sampass);
1951
1952         DEBUG(3,("User:[%s]\n",  pdb_get_username(sampass) ));
1953
1954         ZERO_STRUCTP(id21);
1955         nt_status = init_sam_user_info21A(id21, sampass, domain_sid);
1956         
1957         TALLOC_FREE(sampass);
1958
1959         return nt_status;
1960 }
1961
1962 /*******************************************************************
1963  _samr_query_userinfo
1964  ********************************************************************/
1965
1966 NTSTATUS _samr_query_userinfo(pipes_struct *p, SAMR_Q_QUERY_USERINFO *q_u, SAMR_R_QUERY_USERINFO *r_u)
1967 {
1968         SAM_USERINFO_CTR *ctr;
1969         struct samr_info *info = NULL;
1970         DOM_SID domain_sid;
1971         uint32 rid;
1972         
1973         r_u->status=NT_STATUS_OK;
1974
1975         /* search for the handle */
1976         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1977                 return NT_STATUS_INVALID_HANDLE;
1978
1979         domain_sid = info->sid;
1980
1981         sid_split_rid(&domain_sid, &rid);
1982
1983         if (!sid_check_is_in_our_domain(&info->sid))
1984                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1985
1986         DEBUG(5,("_samr_query_userinfo: sid:%s\n", sid_string_static(&info->sid)));
1987
1988         ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_USERINFO_CTR);
1989         if (!ctr)
1990                 return NT_STATUS_NO_MEMORY;
1991
1992         ZERO_STRUCTP(ctr);
1993
1994         /* ok!  user info levels (lots: see MSDEV help), off we go... */
1995         ctr->switch_value = q_u->switch_value;
1996
1997         DEBUG(5,("_samr_query_userinfo: user info level: %d\n", q_u->switch_value));
1998
1999         switch (q_u->switch_value) {
2000         case 7:
2001                 ctr->info.id7 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_7);
2002                 if (ctr->info.id7 == NULL)
2003                         return NT_STATUS_NO_MEMORY;
2004
2005                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_7(p->mem_ctx, ctr->info.id7, &info->sid)))
2006                         return r_u->status;
2007                 break;
2008         case 9:
2009                 ctr->info.id9 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_9);
2010                 if (ctr->info.id9 == NULL)
2011                         return NT_STATUS_NO_MEMORY;
2012
2013                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_9(p->mem_ctx, ctr->info.id9, &info->sid)))
2014                         return r_u->status;
2015                 break;
2016         case 16:
2017                 ctr->info.id16 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_16);
2018                 if (ctr->info.id16 == NULL)
2019                         return NT_STATUS_NO_MEMORY;
2020
2021                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_16(p->mem_ctx, ctr->info.id16, &info->sid)))
2022                         return r_u->status;
2023                 break;
2024
2025         case 18:
2026                 ctr->info.id18 = TALLOC_ZERO_P(p->mem_ctx, SAM_USER_INFO_18);
2027                 if (ctr->info.id18 == NULL)
2028                         return NT_STATUS_NO_MEMORY;
2029
2030                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_18(p, p->mem_ctx, ctr->info.id18, &info->sid)))
2031                         return r_u->status;
2032                 break;
2033                 
2034         case 20:
2035                 ctr->info.id20 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_20);
2036                 if (ctr->info.id20 == NULL)
2037                         return NT_STATUS_NO_MEMORY;
2038                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_20(p->mem_ctx, ctr->info.id20, &info->sid)))
2039                         return r_u->status;
2040                 break;
2041
2042         case 21:
2043                 ctr->info.id21 = TALLOC_ZERO_P(p->mem_ctx,SAM_USER_INFO_21);
2044                 if (ctr->info.id21 == NULL)
2045                         return NT_STATUS_NO_MEMORY;
2046                 if (!NT_STATUS_IS_OK(r_u->status = get_user_info_21(p->mem_ctx, ctr->info.id21, 
2047                                                                     &info->sid, &domain_sid)))
2048                         return r_u->status;
2049                 break;
2050
2051         default:
2052                 return NT_STATUS_INVALID_INFO_CLASS;
2053         }
2054
2055         init_samr_r_query_userinfo(r_u, ctr, r_u->status);
2056
2057         DEBUG(5,("_samr_query_userinfo: %d\n", __LINE__));
2058         
2059         return r_u->status;
2060 }
2061
2062 /*******************************************************************
2063  samr_reply_query_usergroups
2064  ********************************************************************/
2065
2066 NTSTATUS _samr_query_usergroups(pipes_struct *p, SAMR_Q_QUERY_USERGROUPS *q_u, SAMR_R_QUERY_USERGROUPS *r_u)
2067 {
2068         struct samu *sam_pass=NULL;
2069         DOM_SID  sid;
2070         DOM_SID *sids;
2071         DOM_GID dom_gid;
2072         DOM_GID *gids = NULL;
2073         uint32 primary_group_rid;
2074         size_t num_groups = 0;
2075         gid_t *unix_gids;
2076         size_t i, num_gids;
2077         uint32 acc_granted;
2078         BOOL ret;
2079         NTSTATUS result;
2080
2081         /*
2082          * from the SID in the request:
2083          * we should send back the list of DOMAIN GROUPS
2084          * the user is a member of
2085          *
2086          * and only the DOMAIN GROUPS
2087          * no ALIASES !!! neither aliases of the domain
2088          * nor aliases of the builtin SID
2089          *
2090          * JFM, 12/2/2001
2091          */
2092
2093         r_u->status = NT_STATUS_OK;
2094
2095         DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2096
2097         /* find the policy handle.  open a policy on it. */
2098         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &sid, &acc_granted, NULL))
2099                 return NT_STATUS_INVALID_HANDLE;
2100         
2101         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_USER_GET_GROUPS, "_samr_query_usergroups"))) {
2102                 return r_u->status;
2103         }
2104
2105         if (!sid_check_is_in_our_domain(&sid))
2106                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
2107
2108         if ( !(sam_pass = samu_new( p->mem_ctx )) ) {
2109                 return NT_STATUS_NO_MEMORY;
2110         }
2111
2112         become_root();
2113         ret = pdb_getsampwsid(sam_pass, &sid);
2114         unbecome_root();
2115
2116         if (!ret) {
2117                 DEBUG(10, ("pdb_getsampwsid failed for %s\n",
2118                            sid_string_static(&sid)));
2119                 return NT_STATUS_NO_SUCH_USER;
2120         }
2121
2122         sids = NULL;
2123
2124         become_root();
2125         result = pdb_enum_group_memberships(p->mem_ctx, sam_pass,
2126                                             &sids, &unix_gids, &num_groups);
2127         unbecome_root();
2128
2129         if (!NT_STATUS_IS_OK(result)) {
2130                 DEBUG(10, ("pdb_enum_group_memberships failed for %s\n",
2131                            sid_string_static(&sid)));
2132                 return result;
2133         }
2134
2135         gids = NULL;
2136         num_gids = 0;
2137
2138         dom_gid.attr = (SE_GROUP_MANDATORY|SE_GROUP_ENABLED_BY_DEFAULT|
2139                         SE_GROUP_ENABLED);
2140
2141         if (!sid_peek_check_rid(get_global_sam_sid(),
2142                                 pdb_get_group_sid(sam_pass),
2143                                 &primary_group_rid)) {
2144                 DEBUG(5, ("Group sid %s for user %s not in our domain\n",
2145                           sid_string_static(pdb_get_group_sid(sam_pass)),
2146                           pdb_get_username(sam_pass)));
2147                 TALLOC_FREE(sam_pass);
2148                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
2149         }
2150
2151         dom_gid.g_rid = primary_group_rid;
2152
2153         ADD_TO_ARRAY(p->mem_ctx, DOM_GID, dom_gid, &gids, &num_gids);
2154
2155         for (i=0; i<num_groups; i++) {
2156
2157                 if (!sid_peek_check_rid(get_global_sam_sid(),
2158                                         &(sids[i]), &dom_gid.g_rid)) {
2159                         DEBUG(10, ("Found sid %s not in our domain\n",
2160                                    sid_string_static(&sids[i])));
2161                         continue;
2162                 }
2163
2164                 if (dom_gid.g_rid == primary_group_rid) {
2165                         /* We added the primary group directly from the
2166                          * sam_account. The other SIDs are unique from
2167                          * enum_group_memberships */
2168                         continue;
2169                 }
2170
2171                 ADD_TO_ARRAY(p->mem_ctx, DOM_GID, dom_gid, &gids, &num_gids);
2172         }
2173         
2174         /* construct the response.  lkclXXXX: gids are not copied! */
2175         init_samr_r_query_usergroups(r_u, num_gids, gids, r_u->status);
2176         
2177         DEBUG(5,("_samr_query_usergroups: %d\n", __LINE__));
2178         
2179         return r_u->status;
2180 }
2181
2182 /*******************************************************************
2183  _samr_query_domain_info
2184  ********************************************************************/
2185
2186 NTSTATUS _samr_query_domain_info(pipes_struct *p, 
2187                                  SAMR_Q_QUERY_DOMAIN_INFO *q_u, 
2188                                  SAMR_R_QUERY_DOMAIN_INFO *r_u)
2189 {
2190         struct samr_info *info = NULL;
2191         SAM_UNK_CTR *ctr;
2192         uint32 min_pass_len,pass_hist,password_properties;
2193         time_t u_expire, u_min_age;
2194         NTTIME nt_expire, nt_min_age;
2195
2196         time_t u_lock_duration, u_reset_time;
2197         NTTIME nt_lock_duration, nt_reset_time;
2198         uint32 lockout;
2199         time_t u_logout;
2200         NTTIME nt_logout;
2201
2202         uint32 account_policy_temp;
2203
2204         time_t seq_num;
2205         uint32 server_role;
2206
2207         uint32 num_users=0, num_groups=0, num_aliases=0;
2208
2209         if ((ctr = TALLOC_ZERO_P(p->mem_ctx, SAM_UNK_CTR)) == NULL) {
2210                 return NT_STATUS_NO_MEMORY;
2211         }
2212
2213         ZERO_STRUCTP(ctr);
2214
2215         r_u->status = NT_STATUS_OK;
2216         
2217         DEBUG(5,("_samr_query_domain_info: %d\n", __LINE__));
2218         
2219         /* find the policy handle.  open a policy on it. */
2220         if (!find_policy_by_hnd(p, &q_u->domain_pol, (void **)(void *)&info)) {
2221                 return NT_STATUS_INVALID_HANDLE;
2222         }
2223         
2224         switch (q_u->switch_value) {
2225                 case 0x01:
2226                         
2227                         become_root();
2228
2229                         /* AS ROOT !!! */
2230
2231                         pdb_get_account_policy(AP_MIN_PASSWORD_LEN, &account_policy_temp);
2232                         min_pass_len = account_policy_temp;
2233
2234                         pdb_get_account_policy(AP_PASSWORD_HISTORY, &account_policy_temp);
2235                         pass_hist = account_policy_temp;
2236
2237                         pdb_get_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, &account_policy_temp);
2238                         password_properties = account_policy_temp;
2239
2240                         pdb_get_account_policy(AP_MAX_PASSWORD_AGE, &account_policy_temp);
2241                         u_expire = account_policy_temp;
2242
2243                         pdb_get_account_policy(AP_MIN_PASSWORD_AGE, &account_policy_temp);
2244                         u_min_age = account_policy_temp;
2245
2246                         /* !AS ROOT */
2247                         
2248                         unbecome_root();
2249
2250                         unix_to_nt_time_abs(&nt_expire, u_expire);
2251                         unix_to_nt_time_abs(&nt_min_age, u_min_age);
2252
2253                         init_unk_info1(&ctr->info.inf1, (uint16)min_pass_len, (uint16)pass_hist, 
2254                                        password_properties, nt_expire, nt_min_age);
2255                         break;
2256                 case 0x02:
2257
2258                         become_root();
2259
2260                         /* AS ROOT !!! */
2261
2262                         num_users = count_sam_users(info->disp_info, ACB_NORMAL);
2263                         num_groups = count_sam_groups(info->disp_info);
2264                         num_aliases = count_sam_aliases(info->disp_info);
2265
2266                         pdb_get_account_policy(AP_TIME_TO_LOGOUT, &account_policy_temp);
2267                         u_logout = account_policy_temp;
2268
2269                         unix_to_nt_time_abs(&nt_logout, u_logout);
2270
2271                         if (!pdb_get_seq_num(&seq_num))
2272                                 seq_num = time(NULL);
2273
2274                         /* !AS ROOT */
2275                         
2276                         unbecome_root();
2277
2278                         server_role = ROLE_DOMAIN_PDC;
2279                         if (lp_server_role() == ROLE_DOMAIN_BDC)
2280                                 server_role = ROLE_DOMAIN_BDC;
2281
2282                         init_unk_info2(&ctr->info.inf2, lp_serverstring(), lp_workgroup(), global_myname(), seq_num, 
2283                                        num_users, num_groups, num_aliases, nt_logout, server_role);
2284                         break;
2285                 case 0x03:
2286
2287                         become_root();
2288
2289                         /* AS ROOT !!! */
2290
2291                         {
2292                                 uint32 ul;
2293                                 pdb_get_account_policy(AP_TIME_TO_LOGOUT, &ul);
2294                                 u_logout = (time_t)ul;
2295                         }
2296
2297                         /* !AS ROOT */
2298                         
2299                         unbecome_root();
2300
2301                         unix_to_nt_time_abs(&nt_logout, u_logout);
2302                         
2303                         init_unk_info3(&ctr->info.inf3, nt_logout);
2304                         break;
2305                 case 0x04:
2306                         init_unk_info4(&ctr->info.inf4, lp_serverstring());
2307                         break;
2308                 case 0x05:
2309                         init_unk_info5(&ctr->info.inf5, get_global_sam_name());
2310                         break;
2311                 case 0x06:
2312                         /* NT returns its own name when a PDC. win2k and later
2313                          * only the name of the PDC if itself is a BDC (samba4
2314                          * idl) */
2315                         init_unk_info6(&ctr->info.inf6, global_myname());
2316                         break;
2317                 case 0x07:
2318                         server_role = ROLE_DOMAIN_PDC;
2319                         if (lp_server_role() == ROLE_DOMAIN_BDC)
2320                                 server_role = ROLE_DOMAIN_BDC;
2321
2322                         init_unk_info7(&ctr->info.inf7, server_role);
2323                         break;
2324                 case 0x08:
2325
2326                         become_root();
2327
2328                         /* AS ROOT !!! */
2329
2330                         if (!pdb_get_seq_num(&seq_num)) {
2331                                 seq_num = time(NULL);
2332                         }
2333
2334                         /* !AS ROOT */
2335                         
2336                         unbecome_root();
2337
2338                         init_unk_info8(&ctr->info.inf8, (uint32) seq_num);
2339                         break;
2340                 case 0x0c:
2341
2342                         become_root();
2343
2344                         /* AS ROOT !!! */
2345
2346                         pdb_get_account_policy(AP_LOCK_ACCOUNT_DURATION, &account_policy_temp);
2347                         u_lock_duration = account_policy_temp;
2348                         if (u_lock_duration != -1) {
2349                                 u_lock_duration *= 60;
2350                         }
2351
2352                         pdb_get_account_policy(AP_RESET_COUNT_TIME, &account_policy_temp);
2353                         u_reset_time = account_policy_temp * 60;
2354
2355                         pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &account_policy_temp);
2356                         lockout = account_policy_temp;
2357
2358                         /* !AS ROOT */
2359                         
2360                         unbecome_root();
2361
2362                         unix_to_nt_time_abs(&nt_lock_duration, u_lock_duration);
2363                         unix_to_nt_time_abs(&nt_reset_time, u_reset_time);
2364         
2365                         init_unk_info12(&ctr->info.inf12, nt_lock_duration, nt_reset_time, (uint16)lockout);
2366                         break;
2367                 default:
2368                         return NT_STATUS_INVALID_INFO_CLASS;
2369                 }
2370         
2371
2372         init_samr_r_query_domain_info(r_u, q_u->switch_value, ctr, NT_STATUS_OK);
2373         
2374         DEBUG(5,("_samr_query_domain_info: %d\n", __LINE__));
2375         
2376         return r_u->status;
2377 }
2378
2379 /* W2k3 seems to use the same check for all 3 objects that can be created via
2380  * SAMR, if you try to create for example "Dialup" as an alias it says
2381  * "NT_STATUS_USER_EXISTS". This is racy, but we can't really lock the user
2382  * database. */
2383
2384 static NTSTATUS can_create(TALLOC_CTX *mem_ctx, const char *new_name)
2385 {
2386         enum SID_NAME_USE type;
2387         BOOL result;
2388
2389         DEBUG(10, ("Checking whether [%s] can be created\n", new_name));
2390
2391         become_root();
2392         /* Lookup in our local databases (only LOOKUP_NAME_ISOLATED set)
2393          * whether the name already exists */
2394         result = lookup_name(mem_ctx, new_name, LOOKUP_NAME_ISOLATED,
2395                              NULL, NULL, NULL, &type);
2396         unbecome_root();
2397
2398         if (!result) {
2399                 DEBUG(10, ("%s does not exist, can create it\n", new_name));
2400                 return NT_STATUS_OK;
2401         }
2402
2403         DEBUG(5, ("trying to create %s, exists as %s\n",
2404                   new_name, sid_type_lookup(type)));
2405
2406         if (type == SID_NAME_DOM_GRP) {
2407                 return NT_STATUS_GROUP_EXISTS;
2408         }
2409         if (type == SID_NAME_ALIAS) {
2410                 return NT_STATUS_ALIAS_EXISTS;
2411         }
2412
2413         /* Yes, the default is NT_STATUS_USER_EXISTS */
2414         return NT_STATUS_USER_EXISTS;
2415 }
2416
2417 /*******************************************************************
2418  _samr_create_user
2419  Create an account, can be either a normal user or a machine.
2420  This funcion will need to be updated for bdc/domain trusts.
2421  ********************************************************************/
2422
2423 NTSTATUS _samr_create_user(pipes_struct *p, SAMR_Q_CREATE_USER *q_u,
2424                            SAMR_R_CREATE_USER *r_u)
2425 {
2426         char *account;
2427         DOM_SID sid;
2428         POLICY_HND dom_pol = q_u->domain_pol;
2429         uint16 acb_info = q_u->acb_info;
2430         POLICY_HND *user_pol = &r_u->user_pol;
2431         struct samr_info *info = NULL;
2432         NTSTATUS nt_status;
2433         uint32 acc_granted;
2434         SEC_DESC *psd;
2435         size_t    sd_size;
2436         /* check this, when giving away 'add computer to domain' privs */
2437         uint32    des_access = GENERIC_RIGHTS_USER_ALL_ACCESS;
2438         BOOL can_add_account = False;
2439         SE_PRIV se_rights;
2440         DISP_INFO *disp_info = NULL;
2441
2442         /* Get the domain SID stored in the domain policy */
2443         if (!get_lsa_policy_samr_sid(p, &dom_pol, &sid, &acc_granted,
2444                                      &disp_info))
2445                 return NT_STATUS_INVALID_HANDLE;
2446
2447         nt_status = access_check_samr_function(acc_granted,
2448                                                SA_RIGHT_DOMAIN_CREATE_USER,
2449                                                "_samr_create_user");
2450         if (!NT_STATUS_IS_OK(nt_status)) {
2451                 return nt_status;
2452         }
2453
2454         if (!(acb_info == ACB_NORMAL || acb_info == ACB_DOMTRUST ||
2455               acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)) { 
2456                 /* Match Win2k, and return NT_STATUS_INVALID_PARAMETER if 
2457                    this parameter is not an account type */
2458                 return NT_STATUS_INVALID_PARAMETER;
2459         }
2460
2461         account = rpcstr_pull_unistr2_talloc(p->mem_ctx, &q_u->uni_name);
2462         if (account == NULL) {
2463                 return NT_STATUS_NO_MEMORY;
2464         }
2465
2466         strlower_m(account);
2467
2468         nt_status = can_create(p->mem_ctx, account);
2469         if (!NT_STATUS_IS_OK(nt_status)) {
2470                 return nt_status;
2471         }
2472
2473         /* determine which user right we need to check based on the acb_info */
2474         
2475         if ( acb_info & ACB_WSTRUST )
2476         {
2477                 se_priv_copy( &se_rights, &se_machine_account );
2478                 can_add_account = user_has_privileges(
2479                         p->pipe_user.nt_user_token, &se_rights );
2480         } 
2481         /* usrmgr.exe (and net rpc trustdom grant) creates a normal user 
2482            account for domain trusts and changes the ACB flags later */
2483         else if ( acb_info & ACB_NORMAL &&
2484                   (account[strlen(account)-1] != '$') )
2485         {
2486                 se_priv_copy( &se_rights, &se_add_users );
2487                 can_add_account = user_has_privileges(
2488                         p->pipe_user.nt_user_token, &se_rights );
2489         } 
2490         else    /* implicit assumption of a BDC or domain trust account here
2491                  * (we already check the flags earlier) */
2492         {
2493                 if ( lp_enable_privileges() ) {
2494                         /* only Domain Admins can add a BDC or domain trust */
2495                         se_priv_copy( &se_rights, &se_priv_none );
2496                         can_add_account = nt_token_check_domain_rid(
2497                                 p->pipe_user.nt_user_token,
2498                                 DOMAIN_GROUP_RID_ADMINS );
2499                 }
2500         }
2501                 
2502         DEBUG(5, ("_samr_create_user: %s can add this account : %s\n",
2503                 p->pipe_user_name, can_add_account ? "True":"False" ));
2504                 
2505         /********** BEGIN Admin BLOCK **********/
2506
2507         if ( can_add_account )
2508                 become_root();
2509
2510         nt_status = pdb_create_user(p->mem_ctx, account, acb_info,
2511                                     &r_u->user_rid);
2512
2513         if ( can_add_account )
2514                 unbecome_root();
2515
2516         /********** END Admin BLOCK **********/
2517         
2518         /* now check for failure */
2519         
2520         if ( !NT_STATUS_IS_OK(nt_status) )
2521                 return nt_status;
2522                         
2523         /* Get the user's SID */
2524
2525         sid_compose(&sid, get_global_sam_sid(), r_u->user_rid);
2526         
2527         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &usr_generic_mapping,
2528                             &sid, SAMR_USR_RIGHTS_WRITE_PW);
2529         se_map_generic(&des_access, &usr_generic_mapping);
2530         
2531         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2532                 &se_rights, GENERIC_RIGHTS_USER_WRITE, des_access, 
2533                 &acc_granted, "_samr_create_user");
2534                 
2535         if ( !NT_STATUS_IS_OK(nt_status) ) {
2536                 return nt_status;
2537         }
2538
2539         /* associate the user's SID with the new handle. */
2540         if ((info = get_samr_info_by_sid(&sid)) == NULL) {
2541                 return NT_STATUS_NO_MEMORY;
2542         }
2543
2544         ZERO_STRUCTP(info);
2545         info->sid = sid;
2546         info->acc_granted = acc_granted;
2547
2548         /* get a (unique) handle.  open a policy on it. */
2549         if (!create_policy_hnd(p, user_pol, free_samr_info, (void *)info)) {
2550                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2551         }
2552
2553         /* After a "set" ensure we have no cached display info. */
2554         force_flush_samr_cache(info->disp_info);
2555
2556         r_u->access_granted = acc_granted;
2557
2558         return NT_STATUS_OK;
2559 }
2560
2561 /*******************************************************************
2562  samr_reply_connect_anon
2563  ********************************************************************/
2564
2565 NTSTATUS _samr_connect_anon(pipes_struct *p, SAMR_Q_CONNECT_ANON *q_u, SAMR_R_CONNECT_ANON *r_u)
2566 {
2567         struct samr_info *info = NULL;
2568         uint32    des_access = q_u->access_mask;
2569
2570         /* Access check */
2571
2572         if (!pipe_access_check(p)) {
2573                 DEBUG(3, ("access denied to samr_connect_anon\n"));
2574                 r_u->status = NT_STATUS_ACCESS_DENIED;
2575                 return r_u->status;
2576         }
2577
2578         /* set up the SAMR connect_anon response */
2579
2580         r_u->status = NT_STATUS_OK;
2581
2582         /* associate the user's SID with the new handle. */
2583         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2584                 return NT_STATUS_NO_MEMORY;
2585
2586         /* don't give away the farm but this is probably ok.  The SA_RIGHT_SAM_ENUM_DOMAINS
2587            was observed from a win98 client trying to enumerate users (when configured  
2588            user level access control on shares)   --jerry */
2589            
2590         if (des_access == MAXIMUM_ALLOWED_ACCESS) {
2591                 /* Map to max possible knowing we're filtered below. */
2592                 des_access = GENERIC_ALL_ACCESS;
2593         }
2594
2595         se_map_generic( &des_access, &sam_generic_mapping );
2596         info->acc_granted = des_access & (SA_RIGHT_SAM_ENUM_DOMAINS|SA_RIGHT_SAM_OPEN_DOMAIN);
2597         
2598         info->status = q_u->unknown_0;
2599
2600         /* get a (unique) handle.  open a policy on it. */
2601         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2602                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2603
2604         return r_u->status;
2605 }
2606
2607 /*******************************************************************
2608  samr_reply_connect
2609  ********************************************************************/
2610
2611 NTSTATUS _samr_connect(pipes_struct *p, SAMR_Q_CONNECT *q_u, SAMR_R_CONNECT *r_u)
2612 {
2613         struct samr_info *info = NULL;
2614         SEC_DESC *psd = NULL;
2615         uint32    acc_granted;
2616         uint32    des_access = q_u->access_mask;
2617         NTSTATUS  nt_status;
2618         size_t    sd_size;
2619
2620
2621         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2622
2623         /* Access check */
2624
2625         if (!pipe_access_check(p)) {
2626                 DEBUG(3, ("access denied to samr_connect\n"));
2627                 r_u->status = NT_STATUS_ACCESS_DENIED;
2628                 return r_u->status;
2629         }
2630
2631         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2632         se_map_generic(&des_access, &sam_generic_mapping);
2633         
2634         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2635                 NULL, 0, des_access, &acc_granted, "_samr_connect");
2636         
2637         if ( !NT_STATUS_IS_OK(nt_status) ) 
2638                 return nt_status;
2639
2640         r_u->status = NT_STATUS_OK;
2641
2642         /* associate the user's SID and access granted with the new handle. */
2643         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2644                 return NT_STATUS_NO_MEMORY;
2645
2646         info->acc_granted = acc_granted;
2647         info->status = q_u->access_mask;
2648
2649         /* get a (unique) handle.  open a policy on it. */
2650         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2651                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2652
2653         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2654
2655         return r_u->status;
2656 }
2657
2658 /*******************************************************************
2659  samr_connect4
2660  ********************************************************************/
2661
2662 NTSTATUS _samr_connect4(pipes_struct *p, SAMR_Q_CONNECT4 *q_u, SAMR_R_CONNECT4 *r_u)
2663 {
2664         struct samr_info *info = NULL;
2665         SEC_DESC *psd = NULL;
2666         uint32    acc_granted;
2667         uint32    des_access = q_u->access_mask;
2668         NTSTATUS  nt_status;
2669         size_t    sd_size;
2670
2671
2672         DEBUG(5,("_samr_connect4: %d\n", __LINE__));
2673
2674         /* Access check */
2675
2676         if (!pipe_access_check(p)) {
2677                 DEBUG(3, ("access denied to samr_connect4\n"));
2678                 r_u->status = NT_STATUS_ACCESS_DENIED;
2679                 return r_u->status;
2680         }
2681
2682         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2683         se_map_generic(&des_access, &sam_generic_mapping);
2684         
2685         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2686                 NULL, 0, des_access, &acc_granted, "_samr_connect4");
2687         
2688         if ( !NT_STATUS_IS_OK(nt_status) ) 
2689                 return nt_status;
2690
2691         r_u->status = NT_STATUS_OK;
2692
2693         /* associate the user's SID and access granted with the new handle. */
2694         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2695                 return NT_STATUS_NO_MEMORY;
2696
2697         info->acc_granted = acc_granted;
2698         info->status = q_u->access_mask;
2699
2700         /* get a (unique) handle.  open a policy on it. */
2701         if (!create_policy_hnd(p, &r_u->connect_pol, free_samr_info, (void *)info))
2702                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2703
2704         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2705
2706         return r_u->status;
2707 }
2708
2709 /*******************************************************************
2710  samr_connect5
2711  ********************************************************************/
2712
2713 NTSTATUS _samr_connect5(pipes_struct *p, SAMR_Q_CONNECT5 *q_u, SAMR_R_CONNECT5 *r_u)
2714 {
2715         struct samr_info *info = NULL;
2716         SEC_DESC *psd = NULL;
2717         uint32    acc_granted;
2718         uint32    des_access = q_u->access_mask;
2719         NTSTATUS  nt_status;
2720         POLICY_HND pol;
2721         size_t    sd_size;
2722
2723
2724         DEBUG(5,("_samr_connect5: %d\n", __LINE__));
2725
2726         ZERO_STRUCTP(r_u);
2727
2728         /* Access check */
2729
2730         if (!pipe_access_check(p)) {
2731                 DEBUG(3, ("access denied to samr_connect5\n"));
2732                 r_u->status = NT_STATUS_ACCESS_DENIED;
2733                 return r_u->status;
2734         }
2735
2736         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &sam_generic_mapping, NULL, 0);
2737         se_map_generic(&des_access, &sam_generic_mapping);
2738         
2739         nt_status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2740                 NULL, 0, des_access, &acc_granted, "_samr_connect5");
2741         
2742         if ( !NT_STATUS_IS_OK(nt_status) ) 
2743                 return nt_status;
2744
2745         /* associate the user's SID and access granted with the new handle. */
2746         if ((info = get_samr_info_by_sid(NULL)) == NULL)
2747                 return NT_STATUS_NO_MEMORY;
2748
2749         info->acc_granted = acc_granted;
2750         info->status = q_u->access_mask;
2751
2752         /* get a (unique) handle.  open a policy on it. */
2753         if (!create_policy_hnd(p, &pol, free_samr_info, (void *)info))
2754                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2755
2756         DEBUG(5,("_samr_connect: %d\n", __LINE__));
2757
2758         init_samr_r_connect5(r_u, &pol, NT_STATUS_OK);
2759
2760         return r_u->status;
2761 }
2762
2763 /**********************************************************************
2764  api_samr_lookup_domain
2765  **********************************************************************/
2766
2767 NTSTATUS _samr_lookup_domain(pipes_struct *p, SAMR_Q_LOOKUP_DOMAIN *q_u, SAMR_R_LOOKUP_DOMAIN *r_u)
2768 {
2769         struct samr_info *info;
2770         fstring domain_name;
2771         DOM_SID sid;
2772
2773         r_u->status = NT_STATUS_OK;
2774
2775         if (!find_policy_by_hnd(p, &q_u->connect_pol, (void**)(void *)&info))
2776                 return NT_STATUS_INVALID_HANDLE;
2777
2778         /* win9x user manager likes to use SA_RIGHT_SAM_ENUM_DOMAINS here.  
2779            Reverted that change so we will work with RAS servers again */
2780
2781         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, 
2782                 SA_RIGHT_SAM_OPEN_DOMAIN, "_samr_lookup_domain"))) 
2783         {
2784                 return r_u->status;
2785         }
2786
2787         rpcstr_pull(domain_name, q_u->uni_domain.buffer, sizeof(domain_name), q_u->uni_domain.uni_str_len*2, 0);
2788
2789         ZERO_STRUCT(sid);
2790
2791         if (strequal(domain_name, builtin_domain_name())) {
2792                 sid_copy(&sid, &global_sid_Builtin);
2793         } else {
2794                 if (!secrets_fetch_domain_sid(domain_name, &sid)) {
2795                         r_u->status = NT_STATUS_NO_SUCH_DOMAIN;
2796                 }
2797         }
2798
2799         DEBUG(2,("Returning domain sid for domain %s -> %s\n", domain_name, sid_string_static(&sid)));
2800
2801         init_samr_r_lookup_domain(r_u, &sid, r_u->status);
2802
2803         return r_u->status;
2804 }
2805
2806 /******************************************************************
2807 makes a SAMR_R_ENUM_DOMAINS structure.
2808 ********************************************************************/
2809
2810 static BOOL make_enum_domains(TALLOC_CTX *ctx, SAM_ENTRY **pp_sam,
2811                         UNISTR2 **pp_uni_name, uint32 num_sam_entries, fstring doms[])
2812 {
2813         uint32 i;
2814         SAM_ENTRY *sam;
2815         UNISTR2 *uni_name;
2816
2817         DEBUG(5, ("make_enum_domains\n"));
2818
2819         *pp_sam = NULL;
2820         *pp_uni_name = NULL;
2821
2822         if (num_sam_entries == 0)
2823                 return True;
2824
2825         sam = TALLOC_ZERO_ARRAY(ctx, SAM_ENTRY, num_sam_entries);
2826         uni_name = TALLOC_ZERO_ARRAY(ctx, UNISTR2, num_sam_entries);
2827
2828         if (sam == NULL || uni_name == NULL)
2829                 return False;
2830
2831         for (i = 0; i < num_sam_entries; i++) {
2832                 init_unistr2(&uni_name[i], doms[i], UNI_FLAGS_NONE);
2833                 init_sam_entry(&sam[i], &uni_name[i], 0);
2834         }
2835
2836         *pp_sam = sam;
2837         *pp_uni_name = uni_name;
2838
2839         return True;
2840 }
2841
2842 /**********************************************************************
2843  api_samr_enum_domains
2844  **********************************************************************/
2845
2846 NTSTATUS _samr_enum_domains(pipes_struct *p, SAMR_Q_ENUM_DOMAINS *q_u, SAMR_R_ENUM_DOMAINS *r_u)
2847 {
2848         struct samr_info *info;
2849         uint32 num_entries = 2;
2850         fstring dom[2];
2851         const char *name;
2852
2853         r_u->status = NT_STATUS_OK;
2854         
2855         if (!find_policy_by_hnd(p, &q_u->pol, (void**)(void *)&info))
2856                 return NT_STATUS_INVALID_HANDLE;
2857         
2858         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(info->acc_granted, SA_RIGHT_SAM_ENUM_DOMAINS, "_samr_enum_domains"))) {
2859                 return r_u->status;
2860         }
2861
2862         name = get_global_sam_name();
2863
2864         fstrcpy(dom[0],name);
2865         strupper_m(dom[0]);
2866         fstrcpy(dom[1],"Builtin");
2867
2868         if (!make_enum_domains(p->mem_ctx, &r_u->sam, &r_u->uni_dom_name, num_entries, dom))
2869                 return NT_STATUS_NO_MEMORY;
2870
2871         init_samr_r_enum_domains(r_u, q_u->start_idx + num_entries, num_entries);
2872
2873         return r_u->status;
2874 }
2875
2876 /*******************************************************************
2877  api_samr_open_alias
2878  ********************************************************************/
2879
2880 NTSTATUS _samr_open_alias(pipes_struct *p, SAMR_Q_OPEN_ALIAS *q_u, SAMR_R_OPEN_ALIAS *r_u)
2881 {
2882         DOM_SID sid;
2883         POLICY_HND domain_pol = q_u->dom_pol;
2884         uint32 alias_rid = q_u->rid_alias;
2885         POLICY_HND *alias_pol = &r_u->pol;
2886         struct    samr_info *info = NULL;
2887         SEC_DESC *psd = NULL;
2888         uint32    acc_granted;
2889         uint32    des_access = q_u->access_mask;
2890         size_t    sd_size;
2891         NTSTATUS  status;
2892         SE_PRIV se_rights;
2893
2894         r_u->status = NT_STATUS_OK;
2895
2896         /* find the domain policy and get the SID / access bits stored in the domain policy */
2897         
2898         if ( !get_lsa_policy_samr_sid(p, &domain_pol, &sid, &acc_granted, NULL) )
2899                 return NT_STATUS_INVALID_HANDLE;
2900         
2901         status = access_check_samr_function(acc_granted, 
2902                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_alias");
2903                 
2904         if ( !NT_STATUS_IS_OK(status) ) 
2905                 return status;
2906
2907         /* append the alias' RID to it */
2908         
2909         if (!sid_append_rid(&sid, alias_rid))
2910                 return NT_STATUS_NO_SUCH_ALIAS;
2911                 
2912         /*check if access can be granted as requested by client. */
2913         
2914         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &ali_generic_mapping, NULL, 0);
2915         se_map_generic(&des_access,&ali_generic_mapping);
2916         
2917         se_priv_copy( &se_rights, &se_add_users );
2918         
2919         
2920         status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
2921                 &se_rights, GENERIC_RIGHTS_ALIAS_WRITE, des_access, 
2922                 &acc_granted, "_samr_open_alias");
2923                 
2924         if ( !NT_STATUS_IS_OK(status) )
2925                 return status;
2926
2927         {
2928                 /* Check we actually have the requested alias */
2929                 enum SID_NAME_USE type;
2930                 BOOL result;
2931                 gid_t gid;
2932
2933                 become_root();
2934                 result = lookup_sid(NULL, &sid, NULL, NULL, &type);
2935                 unbecome_root();
2936
2937                 if (!result || (type != SID_NAME_ALIAS)) {
2938                         return NT_STATUS_NO_SUCH_ALIAS;
2939                 }
2940
2941                 /* make sure there is a mapping */
2942                 
2943                 if ( !sid_to_gid( &sid, &gid ) ) {
2944                         return NT_STATUS_NO_SUCH_ALIAS;
2945                 }
2946
2947         }
2948
2949         /* associate the alias SID with the new handle. */
2950         if ((info = get_samr_info_by_sid(&sid)) == NULL)
2951                 return NT_STATUS_NO_MEMORY;
2952                 
2953         info->acc_granted = acc_granted;
2954
2955         /* get a (unique) handle.  open a policy on it. */
2956         if (!create_policy_hnd(p, alias_pol, free_samr_info, (void *)info))
2957                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2958
2959         return r_u->status;
2960 }
2961
2962 /*******************************************************************
2963  set_user_info_7
2964  ********************************************************************/
2965 static NTSTATUS set_user_info_7(TALLOC_CTX *mem_ctx,
2966                                 const SAM_USER_INFO_7 *id7, struct samu *pwd)
2967 {
2968         fstring new_name;
2969         NTSTATUS rc;
2970
2971         if (id7 == NULL) {
2972                 DEBUG(5, ("set_user_info_7: NULL id7\n"));
2973                 TALLOC_FREE(pwd);
2974                 return NT_STATUS_ACCESS_DENIED;
2975         }
2976
2977         if(!rpcstr_pull(new_name, id7->uni_name.buffer, sizeof(new_name), id7->uni_name.uni_str_len*2, 0)) {
2978                 DEBUG(5, ("set_user_info_7: failed to get new username\n"));
2979                 TALLOC_FREE(pwd);
2980                 return NT_STATUS_ACCESS_DENIED;
2981         }
2982
2983         /* check to see if the new username already exists.  Note: we can't
2984            reliably lock all backends, so there is potentially the 
2985            possibility that a user can be created in between this check and
2986            the rename.  The rename should fail, but may not get the
2987            exact same failure status code.  I think this is small enough
2988            of a window for this type of operation and the results are
2989            simply that the rename fails with a slightly different status
2990            code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
2991
2992         rc = can_create(mem_ctx, new_name);
2993         if (!NT_STATUS_IS_OK(rc)) {
2994                 return rc;
2995         }
2996
2997         rc = pdb_rename_sam_account(pwd, new_name);
2998
2999         TALLOC_FREE(pwd);
3000         return rc;
3001 }
3002
3003 /*******************************************************************
3004  set_user_info_16
3005  ********************************************************************/
3006
3007 static BOOL set_user_info_16(const SAM_USER_INFO_16 *id16, struct samu *pwd)
3008 {
3009         if (id16 == NULL) {
3010                 DEBUG(5, ("set_user_info_16: NULL id16\n"));
3011                 TALLOC_FREE(pwd);
3012                 return False;
3013         }
3014         
3015         /* FIX ME: check if the value is really changed --metze */
3016         if (!pdb_set_acct_ctrl(pwd, id16->acb_info, PDB_CHANGED)) {
3017                 TALLOC_FREE(pwd);
3018                 return False;
3019         }
3020
3021         if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3022                 TALLOC_FREE(pwd);
3023                 return False;
3024         }
3025
3026         TALLOC_FREE(pwd);
3027
3028         return True;
3029 }
3030
3031 /*******************************************************************
3032  set_user_info_18
3033  ********************************************************************/
3034
3035 static BOOL set_user_info_18(SAM_USER_INFO_18 *id18, struct samu *pwd)
3036 {
3037
3038         if (id18 == NULL) {
3039                 DEBUG(2, ("set_user_info_18: id18 is NULL\n"));
3040                 TALLOC_FREE(pwd);
3041                 return False;
3042         }
3043  
3044         if (!pdb_set_lanman_passwd (pwd, id18->lm_pwd, PDB_CHANGED)) {
3045                 TALLOC_FREE(pwd);
3046                 return False;
3047         }
3048         if (!pdb_set_nt_passwd     (pwd, id18->nt_pwd, PDB_CHANGED)) {
3049                 TALLOC_FREE(pwd);
3050                 return False;
3051         }
3052         if (!pdb_set_pass_changed_now (pwd)) {
3053                 TALLOC_FREE(pwd);
3054                 return False; 
3055         }
3056  
3057         if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3058                 TALLOC_FREE(pwd);
3059                 return False;
3060         }
3061
3062         TALLOC_FREE(pwd);
3063         return True;
3064 }
3065
3066 /*******************************************************************
3067  set_user_info_20
3068  ********************************************************************/
3069
3070 static BOOL set_user_info_20(SAM_USER_INFO_20 *id20, struct samu *pwd)
3071 {
3072         if (id20 == NULL) {
3073                 DEBUG(5, ("set_user_info_20: NULL id20\n"));
3074                 return False;
3075         }
3076  
3077         copy_id20_to_sam_passwd(pwd, id20);
3078
3079         /* write the change out */
3080         if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3081                 TALLOC_FREE(pwd);
3082                 return False;
3083         }
3084
3085         TALLOC_FREE(pwd);
3086
3087         return True;
3088 }
3089 /*******************************************************************
3090  set_user_info_21
3091  ********************************************************************/
3092
3093 static NTSTATUS set_user_info_21(TALLOC_CTX *mem_ctx, SAM_USER_INFO_21 *id21,
3094                                  struct samu *pwd)
3095 {
3096         fstring new_name;
3097         NTSTATUS status;
3098         
3099         if (id21 == NULL) {
3100                 DEBUG(5, ("set_user_info_21: NULL id21\n"));
3101                 return NT_STATUS_INVALID_PARAMETER;
3102         }
3103
3104         /* we need to separately check for an account rename first */
3105         if (rpcstr_pull(new_name, id21->uni_user_name.buffer, 
3106                         sizeof(new_name), id21->uni_user_name.uni_str_len*2, 0) && 
3107            (!strequal(new_name, pdb_get_username(pwd)))) {
3108
3109                 /* check to see if the new username already exists.  Note: we can't
3110                    reliably lock all backends, so there is potentially the 
3111                    possibility that a user can be created in between this check and
3112                    the rename.  The rename should fail, but may not get the
3113                    exact same failure status code.  I think this is small enough
3114                    of a window for this type of operation and the results are
3115                    simply that the rename fails with a slightly different status
3116                    code (like UNSUCCESSFUL instead of ALREADY_EXISTS). */
3117
3118                 status = can_create(mem_ctx, new_name);
3119                 if (!NT_STATUS_IS_OK(status)) {
3120                         return status;
3121                 }
3122
3123                 status = pdb_rename_sam_account(pwd, new_name);
3124
3125                 if (!NT_STATUS_IS_OK(status)) {
3126                         DEBUG(0,("set_user_info_21: failed to rename account: %s\n", 
3127                                 nt_errstr(status)));
3128                         TALLOC_FREE(pwd);
3129                         return status;
3130                 }
3131
3132                 /* set the new username so that later 
3133                    functions can work on the new account */
3134                 pdb_set_username(pwd, new_name, PDB_SET);
3135         }
3136
3137         copy_id21_to_sam_passwd(pwd, id21);
3138  
3139         /*
3140          * The funny part about the previous two calls is
3141          * that pwd still has the password hashes from the
3142          * passdb entry.  These have not been updated from
3143          * id21.  I don't know if they need to be set.    --jerry
3144          */
3145  
3146         if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3147                 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3148                 if ( !NT_STATUS_IS_OK(status) ) {
3149                         return status;
3150                 }
3151         }
3152         
3153         /* Don't worry about writing out the user account since the
3154            primary group SID is generated solely from the user's Unix 
3155            primary group. */
3156
3157         /* write the change out */
3158         if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3159                 TALLOC_FREE(pwd);
3160                 return status;
3161         }
3162
3163         TALLOC_FREE(pwd);
3164
3165         return NT_STATUS_OK;
3166 }
3167
3168 /*******************************************************************
3169  set_user_info_23
3170  ********************************************************************/
3171
3172 static NTSTATUS set_user_info_23(TALLOC_CTX *mem_ctx, SAM_USER_INFO_23 *id23,
3173                                  struct samu *pwd)
3174 {
3175         pstring plaintext_buf;
3176         uint32 len;
3177         uint16 acct_ctrl;
3178         NTSTATUS status;
3179  
3180         if (id23 == NULL) {
3181                 DEBUG(5, ("set_user_info_23: NULL id23\n"));
3182                 return NT_STATUS_INVALID_PARAMETER;
3183         }
3184  
3185         DEBUG(5, ("Attempting administrator password change (level 23) for user %s\n",
3186                   pdb_get_username(pwd)));
3187
3188         acct_ctrl = pdb_get_acct_ctrl(pwd);
3189
3190         if (!decode_pw_buffer(id23->pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3191                 TALLOC_FREE(pwd);
3192                 return NT_STATUS_INVALID_PARAMETER;
3193         }
3194   
3195         if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3196                 TALLOC_FREE(pwd);
3197                 return NT_STATUS_ACCESS_DENIED;
3198         }
3199  
3200         copy_id23_to_sam_passwd(pwd, id23);
3201  
3202         /* if it's a trust account, don't update /etc/passwd */
3203         if (    ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3204                 ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
3205                 ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
3206                 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3207         } else  {
3208                 /* update the UNIX password */
3209                 if (lp_unix_password_sync() ) {
3210                         struct passwd *passwd;
3211                         if (pdb_get_username(pwd) == NULL) {
3212                                 DEBUG(1, ("chgpasswd: User without name???\n"));
3213                                 TALLOC_FREE(pwd);
3214                                 return NT_STATUS_ACCESS_DENIED;
3215                         }
3216
3217                         if ((passwd = Get_Pwnam(pdb_get_username(pwd))) == NULL) {
3218                                 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3219                         }
3220                         
3221                         if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3222                                 TALLOC_FREE(pwd);
3223                                 return NT_STATUS_ACCESS_DENIED;
3224                         }
3225                 }
3226         }
3227  
3228         ZERO_STRUCT(plaintext_buf);
3229  
3230         if (IS_SAM_CHANGED(pwd, PDB_GROUPSID) &&
3231             (!NT_STATUS_IS_OK(status =  pdb_set_unix_primary_group(mem_ctx,
3232                                                                    pwd)))) {
3233                 TALLOC_FREE(pwd);
3234                 return status;
3235         }
3236
3237         if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3238                 TALLOC_FREE(pwd);
3239                 return status;
3240         }
3241  
3242         TALLOC_FREE(pwd);
3243
3244         return NT_STATUS_OK;
3245 }
3246
3247 /*******************************************************************
3248  set_user_info_pw
3249  ********************************************************************/
3250
3251 static BOOL set_user_info_pw(uint8 *pass, struct samu *pwd)
3252 {
3253         uint32 len;
3254         pstring plaintext_buf;
3255         uint32 acct_ctrl;
3256  
3257         DEBUG(5, ("Attempting administrator password change for user %s\n",
3258                   pdb_get_username(pwd)));
3259
3260         acct_ctrl = pdb_get_acct_ctrl(pwd);
3261
3262         ZERO_STRUCT(plaintext_buf);
3263  
3264         if (!decode_pw_buffer(pass, plaintext_buf, 256, &len, STR_UNICODE)) {
3265                 TALLOC_FREE(pwd);
3266                 return False;
3267         }
3268
3269         if (!pdb_set_plaintext_passwd (pwd, plaintext_buf)) {
3270                 TALLOC_FREE(pwd);
3271                 return False;
3272         }
3273  
3274         /* if it's a trust account, don't update /etc/passwd */
3275         if ( ( (acct_ctrl &  ACB_DOMTRUST) == ACB_DOMTRUST ) ||
3276                 ( (acct_ctrl &  ACB_WSTRUST) ==  ACB_WSTRUST) ||
3277                 ( (acct_ctrl &  ACB_SVRTRUST) ==  ACB_SVRTRUST) ) {
3278                 DEBUG(5, ("Changing trust account or non-unix-user password, not updating /etc/passwd\n"));
3279         } else {
3280                 /* update the UNIX password */
3281                 if (lp_unix_password_sync()) {
3282                         struct passwd *passwd;
3283
3284                         if (pdb_get_username(pwd) == NULL) {
3285                                 DEBUG(1, ("chgpasswd: User without name???\n"));
3286                                 TALLOC_FREE(pwd);
3287                                 return False;
3288                         }
3289
3290                         if ((passwd = Get_Pwnam(pdb_get_username(pwd))) == NULL) {
3291                                 DEBUG(1, ("chgpasswd: Username does not exist in system !?!\n"));
3292                         }
3293                         
3294                         if(!chgpasswd(pdb_get_username(pwd), passwd, "", plaintext_buf, True)) {
3295                                 TALLOC_FREE(pwd);
3296                                 return False;
3297                         }
3298                 }
3299         }
3300  
3301         ZERO_STRUCT(plaintext_buf);
3302  
3303         DEBUG(5,("set_user_info_pw: pdb_update_pwd()\n"));
3304  
3305         /* update the SAMBA password */
3306         if(!NT_STATUS_IS_OK(pdb_update_sam_account(pwd))) {
3307                 TALLOC_FREE(pwd);
3308                 return False;
3309         }
3310
3311         TALLOC_FREE(pwd);
3312
3313         return True;
3314 }
3315
3316 /*******************************************************************
3317  set_user_info_25
3318  ********************************************************************/
3319
3320 static NTSTATUS set_user_info_25(TALLOC_CTX *mem_ctx, SAM_USER_INFO_25 *id25,
3321                                  struct samu *pwd)
3322 {
3323         NTSTATUS status;
3324         
3325         if (id25 == NULL) {
3326                 DEBUG(5, ("set_user_info_25: NULL id25\n"));
3327                 return NT_STATUS_INVALID_PARAMETER;
3328         }
3329
3330         copy_id25_to_sam_passwd(pwd, id25);
3331  
3332         /*
3333          * The funny part about the previous two calls is
3334          * that pwd still has the password hashes from the
3335          * passdb entry.  These have not been updated from
3336          * id21.  I don't know if they need to be set.    --jerry
3337          */
3338  
3339         if ( IS_SAM_CHANGED(pwd, PDB_GROUPSID) ) {
3340                 status = pdb_set_unix_primary_group(mem_ctx, pwd);
3341                 if ( !NT_STATUS_IS_OK(status) ) {
3342                         return status;
3343                 }
3344         }
3345         
3346         /* Don't worry about writing out the user account since the
3347            primary group SID is generated solely from the user's Unix 
3348            primary group. */
3349
3350         /* write the change out */
3351         if(!NT_STATUS_IS_OK(status = pdb_update_sam_account(pwd))) {
3352                 TALLOC_FREE(pwd);
3353                 return status;
3354         }
3355
3356         /* WARNING: No TALLOC_FREE(pwd), we are about to set the password
3357          * hereafter! */
3358
3359         return NT_STATUS_OK;
3360 }
3361
3362 /*******************************************************************
3363  samr_reply_set_userinfo
3364  ********************************************************************/
3365
3366 NTSTATUS _samr_set_userinfo(pipes_struct *p, SAMR_Q_SET_USERINFO *q_u, SAMR_R_SET_USERINFO *r_u)
3367 {
3368         struct samu *pwd = NULL;
3369         DOM_SID sid;
3370         POLICY_HND *pol = &q_u->pol;
3371         uint16 switch_value = q_u->switch_value;
3372         SAM_USERINFO_CTR *ctr = q_u->ctr;
3373         uint32 acc_granted;
3374         uint32 acc_required;
3375         BOOL ret;
3376         BOOL has_enough_rights = False;
3377         uint32 acb_info;
3378         DISP_INFO *disp_info = NULL;
3379
3380         DEBUG(5, ("_samr_set_userinfo: %d\n", __LINE__));
3381
3382         r_u->status = NT_STATUS_OK;
3383
3384         /* find the policy handle.  open a policy on it. */
3385         if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3386                 return NT_STATUS_INVALID_HANDLE;
3387
3388         /* observed when joining an XP client to a Samba domain */
3389         
3390         acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;     
3391
3392         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo"))) {
3393                 return r_u->status;
3394         }
3395
3396         DEBUG(5, ("_samr_set_userinfo: sid:%s, level:%d\n", sid_string_static(&sid), switch_value));
3397
3398         if (ctr == NULL) {
3399                 DEBUG(5, ("_samr_set_userinfo: NULL info level\n"));
3400                 return NT_STATUS_INVALID_INFO_CLASS;
3401         }
3402         
3403         if ( !(pwd = samu_new( NULL )) ) {
3404                 return NT_STATUS_NO_MEMORY;
3405         }
3406         
3407         become_root();
3408         ret = pdb_getsampwsid(pwd, &sid);
3409         unbecome_root();
3410         
3411         if ( !ret ) {
3412                 TALLOC_FREE(pwd);
3413                 return NT_STATUS_NO_SUCH_USER;
3414         }
3415         
3416         /* deal with machine password changes differently from userinfo changes */
3417         /* check to see if we have the sufficient rights */
3418         
3419         acb_info = pdb_get_acct_ctrl(pwd);
3420         if ( acb_info & ACB_WSTRUST ) 
3421                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3422         else if ( acb_info & ACB_NORMAL )
3423                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3424         else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3425                 if ( lp_enable_privileges() )
3426                         has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3427         }
3428         
3429         DEBUG(5, ("_samr_set_userinfo: %s does%s possess sufficient rights\n",
3430                 p->pipe_user_name, has_enough_rights ? "" : " not"));
3431
3432         /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3433         
3434         if ( has_enough_rights )                                
3435                 become_root(); 
3436         
3437         /* ok!  user info levels (lots: see MSDEV help), off we go... */
3438
3439         switch (switch_value) {
3440                 case 18:
3441                         if (!set_user_info_18(ctr->info.id18, pwd))
3442                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3443                         break;
3444
3445                 case 24:
3446                         if (!p->session_key.length) {
3447                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3448                         }
3449                         SamOEMhashBlob(ctr->info.id24->pass, 516, &p->session_key);
3450
3451                         dump_data(100, (char *)ctr->info.id24->pass, 516);
3452
3453                         if (!set_user_info_pw(ctr->info.id24->pass, pwd))
3454                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3455                         break;
3456
3457                 case 25:
3458                         if (!p->session_key.length) {
3459                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3460                         }
3461                         encode_or_decode_arc4_passwd_buffer(ctr->info.id25->pass, &p->session_key);
3462
3463                         dump_data(100, (char *)ctr->info.id25->pass, 532);
3464
3465                         r_u->status = set_user_info_25(p->mem_ctx,
3466                                                        ctr->info.id25, pwd);
3467                         if (!NT_STATUS_IS_OK(r_u->status)) {
3468                                 goto done;
3469                         }
3470                         if (!set_user_info_pw(ctr->info.id25->pass, pwd))
3471                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3472                         break;
3473
3474                 case 26:
3475                         if (!p->session_key.length) {
3476                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3477                         }
3478                         encode_or_decode_arc4_passwd_buffer(ctr->info.id26->pass, &p->session_key);
3479
3480                         dump_data(100, (char *)ctr->info.id26->pass, 516);
3481
3482                         if (!set_user_info_pw(ctr->info.id26->pass, pwd))
3483                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3484                         break;
3485
3486                 case 23:
3487                         if (!p->session_key.length) {
3488                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3489                         }
3490                         SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
3491
3492                         dump_data(100, (char *)ctr->info.id23->pass, 516);
3493
3494                         r_u->status = set_user_info_23(p->mem_ctx,
3495                                                        ctr->info.id23, pwd);
3496                         break;
3497
3498                 default:
3499                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3500         }
3501
3502  done:
3503         
3504         if ( has_enough_rights )                                
3505                 unbecome_root();
3506                 
3507         /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3508
3509         if (NT_STATUS_IS_OK(r_u->status)) {
3510                 force_flush_samr_cache(disp_info);
3511         }
3512
3513         return r_u->status;
3514 }
3515
3516 /*******************************************************************
3517  samr_reply_set_userinfo2
3518  ********************************************************************/
3519
3520 NTSTATUS _samr_set_userinfo2(pipes_struct *p, SAMR_Q_SET_USERINFO2 *q_u, SAMR_R_SET_USERINFO2 *r_u)
3521 {
3522         struct samu *pwd = NULL;
3523         DOM_SID sid;
3524         SAM_USERINFO_CTR *ctr = q_u->ctr;
3525         POLICY_HND *pol = &q_u->pol;
3526         uint16 switch_value = q_u->switch_value;
3527         uint32 acc_granted;
3528         uint32 acc_required;
3529         BOOL ret;
3530         BOOL has_enough_rights = False;
3531         uint32 acb_info;
3532         DISP_INFO *disp_info = NULL;
3533
3534         DEBUG(5, ("samr_reply_set_userinfo2: %d\n", __LINE__));
3535
3536         r_u->status = NT_STATUS_OK;
3537
3538         /* find the policy handle.  open a policy on it. */
3539         if (!get_lsa_policy_samr_sid(p, pol, &sid, &acc_granted, &disp_info))
3540                 return NT_STATUS_INVALID_HANDLE;
3541
3542                 
3543 #if 0   /* this really should be applied on a per info level basis   --jerry */
3544
3545         /* observed when joining XP client to Samba domain */
3546         acc_required = SA_RIGHT_USER_SET_PASSWORD | SA_RIGHT_USER_SET_ATTRIBUTES | SA_RIGHT_USER_ACCT_FLAGS_EXPIRY;
3547 #else
3548         acc_required = SA_RIGHT_USER_SET_ATTRIBUTES;
3549 #endif
3550         
3551         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, acc_required, "_samr_set_userinfo2"))) {
3552                 return r_u->status;
3553         }
3554
3555         DEBUG(5, ("samr_reply_set_userinfo2: sid:%s\n", sid_string_static(&sid)));
3556
3557         if (ctr == NULL) {
3558                 DEBUG(5, ("samr_reply_set_userinfo2: NULL info level\n"));
3559                 return NT_STATUS_INVALID_INFO_CLASS;
3560         }
3561
3562         switch_value=ctr->switch_value;
3563
3564         if ( !(pwd = samu_new( NULL )) ) {
3565                 return NT_STATUS_NO_MEMORY;
3566         }
3567
3568         become_root();
3569         ret = pdb_getsampwsid(pwd, &sid);
3570         unbecome_root();
3571         
3572         if ( !ret ) {
3573                 TALLOC_FREE(pwd);
3574                 return NT_STATUS_NO_SUCH_USER;
3575         }
3576         
3577         acb_info = pdb_get_acct_ctrl(pwd);
3578         if ( acb_info & ACB_WSTRUST ) 
3579                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account);
3580         else if ( acb_info & ACB_NORMAL )
3581                 has_enough_rights = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
3582         else if ( acb_info & (ACB_SVRTRUST|ACB_DOMTRUST) ) {
3583                 if ( lp_enable_privileges() )
3584                         has_enough_rights = nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS );
3585         }
3586         
3587         DEBUG(5, ("_samr_set_userinfo2: %s does%s possess sufficient rights\n",
3588                 p->pipe_user_name, has_enough_rights ? "" : " not"));
3589
3590         /* ================ BEGIN SeMachineAccountPrivilege BLOCK ================ */
3591         
3592         if ( has_enough_rights )                                
3593                 become_root(); 
3594         
3595         /* ok!  user info levels (lots: see MSDEV help), off we go... */
3596         
3597         switch (switch_value) {
3598                 case 7:
3599                         r_u->status = set_user_info_7(p->mem_ctx,
3600                                                       ctr->info.id7, pwd);
3601                         break;
3602                 case 16:
3603                         if (!set_user_info_16(ctr->info.id16, pwd))
3604                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3605                         break;
3606                 case 18:
3607                         /* Used by AS/U JRA. */
3608                         if (!set_user_info_18(ctr->info.id18, pwd))
3609                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3610                         break;
3611                 case 20:
3612                         if (!set_user_info_20(ctr->info.id20, pwd))
3613                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3614                         break;
3615                 case 21:
3616                         r_u->status = set_user_info_21(p->mem_ctx,
3617                                                        ctr->info.id21, pwd);
3618                         break;
3619                 case 23:
3620                         if (!p->session_key.length) {
3621                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3622                         }
3623                         SamOEMhashBlob(ctr->info.id23->pass, 516, &p->session_key);
3624
3625                         dump_data(100, (char *)ctr->info.id23->pass, 516);
3626
3627                         r_u->status = set_user_info_23(p->mem_ctx,
3628                                                        ctr->info.id23, pwd);
3629                         break;
3630                 case 26:
3631                         if (!p->session_key.length) {
3632                                 r_u->status = NT_STATUS_NO_USER_SESSION_KEY;
3633                         }
3634                         encode_or_decode_arc4_passwd_buffer(ctr->info.id26->pass, &p->session_key);
3635
3636                         dump_data(100, (char *)ctr->info.id26->pass, 516);
3637
3638                         if (!set_user_info_pw(ctr->info.id26->pass, pwd))
3639                                 r_u->status = NT_STATUS_ACCESS_DENIED;
3640                         break;
3641                 default:
3642                         r_u->status = NT_STATUS_INVALID_INFO_CLASS;
3643         }
3644
3645         if ( has_enough_rights )                                
3646                 unbecome_root();
3647                 
3648         /* ================ END SeMachineAccountPrivilege BLOCK ================ */
3649
3650         if (NT_STATUS_IS_OK(r_u->status)) {
3651                 force_flush_samr_cache(disp_info);
3652         }
3653
3654         return r_u->status;
3655 }
3656
3657 /*********************************************************************
3658  _samr_query_aliasmem
3659 *********************************************************************/
3660
3661 NTSTATUS _samr_query_useraliases(pipes_struct *p, SAMR_Q_QUERY_USERALIASES *q_u, SAMR_R_QUERY_USERALIASES *r_u)
3662 {
3663         size_t num_alias_rids;
3664         uint32 *alias_rids;
3665         struct samr_info *info = NULL;
3666         size_t i;
3667                 
3668         NTSTATUS ntstatus1;
3669         NTSTATUS ntstatus2;
3670
3671         DOM_SID *members;
3672
3673         r_u->status = NT_STATUS_OK;
3674
3675         DEBUG(5,("_samr_query_useraliases: %d\n", __LINE__));
3676
3677         /* find the policy handle.  open a policy on it. */
3678         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
3679                 return NT_STATUS_INVALID_HANDLE;
3680                 
3681         ntstatus1 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_LOOKUP_ALIAS_BY_MEM, "_samr_query_useraliases");
3682         ntstatus2 = access_check_samr_function(info->acc_granted, SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_query_useraliases");
3683         
3684         if (!NT_STATUS_IS_OK(ntstatus1) || !NT_STATUS_IS_OK(ntstatus2)) {
3685                 if (!(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus2)) &&
3686                     !(NT_STATUS_EQUAL(ntstatus1,NT_STATUS_ACCESS_DENIED) && NT_STATUS_IS_OK(ntstatus1))) {
3687                         return (NT_STATUS_IS_OK(ntstatus1)) ? ntstatus2 : ntstatus1;
3688                 }
3689         }               
3690
3691         if (!sid_check_is_domain(&info->sid) &&
3692             !sid_check_is_builtin(&info->sid))
3693                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
3694
3695         members = TALLOC_ARRAY(p->mem_ctx, DOM_SID, q_u->num_sids1);
3696
3697         if (members == NULL)
3698                 return NT_STATUS_NO_MEMORY;
3699
3700         for (i=0; i<q_u->num_sids1; i++)
3701                 sid_copy(&members[i], &q_u->sid[i].sid);
3702
3703         alias_rids = NULL;
3704         num_alias_rids = 0;
3705
3706         become_root();
3707         ntstatus1 = pdb_enum_alias_memberships(p->mem_ctx, &info->sid, members,
3708                                                q_u->num_sids1,
3709                                                &alias_rids, &num_alias_rids);
3710         unbecome_root();
3711
3712         if (!NT_STATUS_IS_OK(ntstatus1)) {
3713                 return ntstatus1;
3714         }
3715
3716         init_samr_r_query_useraliases(r_u, num_alias_rids, alias_rids,
3717                                       NT_STATUS_OK);
3718         return NT_STATUS_OK;
3719 }
3720
3721 /*********************************************************************
3722  _samr_query_aliasmem
3723 *********************************************************************/
3724
3725 NTSTATUS _samr_query_aliasmem(pipes_struct *p, SAMR_Q_QUERY_ALIASMEM *q_u, SAMR_R_QUERY_ALIASMEM *r_u)
3726 {
3727         NTSTATUS status;
3728         size_t i;
3729         size_t num_sids = 0;
3730         DOM_SID2 *sid;
3731         DOM_SID *sids=NULL;
3732
3733         DOM_SID alias_sid;
3734
3735         uint32 acc_granted;
3736
3737         /* find the policy handle.  open a policy on it. */
3738         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, NULL)) 
3739                 return NT_STATUS_INVALID_HANDLE;
3740         
3741         if (!NT_STATUS_IS_OK(r_u->status = 
3742                 access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_GET_MEMBERS, "_samr_query_aliasmem"))) {
3743                 return r_u->status;
3744         }
3745
3746         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3747
3748         status = pdb_enum_aliasmem(&alias_sid, &sids, &num_sids);
3749
3750         if (!NT_STATUS_IS_OK(status)) {
3751                 return status;
3752         }
3753
3754         sid = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_SID2, num_sids);        
3755         if (num_sids!=0 && sid == NULL) {
3756                 SAFE_FREE(sids);
3757                 return NT_STATUS_NO_MEMORY;
3758         }
3759
3760         for (i = 0; i < num_sids; i++) {
3761                 init_dom_sid2(&sid[i], &sids[i]);
3762         }
3763
3764         init_samr_r_query_aliasmem(r_u, num_sids, sid, NT_STATUS_OK);
3765
3766         SAFE_FREE(sids);
3767
3768         return NT_STATUS_OK;
3769 }
3770
3771 /*********************************************************************
3772  _samr_query_groupmem
3773 *********************************************************************/
3774
3775 NTSTATUS _samr_query_groupmem(pipes_struct *p, SAMR_Q_QUERY_GROUPMEM *q_u, SAMR_R_QUERY_GROUPMEM *r_u)
3776 {
3777         DOM_SID group_sid;
3778         fstring group_sid_str;
3779         size_t i, num_members;
3780
3781         uint32 *rid=NULL;
3782         uint32 *attr=NULL;
3783
3784         uint32 acc_granted;
3785
3786         NTSTATUS result;
3787
3788         /* find the policy handle.  open a policy on it. */
3789         if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, NULL)) 
3790                 return NT_STATUS_INVALID_HANDLE;
3791                 
3792         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_GET_MEMBERS, "_samr_query_groupmem"))) {
3793                 return r_u->status;
3794         }
3795                 
3796         sid_to_string(group_sid_str, &group_sid);
3797         DEBUG(10, ("sid is %s\n", group_sid_str));
3798
3799         if (!sid_check_is_in_our_domain(&group_sid)) {
3800                 DEBUG(3, ("sid %s is not in our domain\n", group_sid_str));
3801                 return NT_STATUS_NO_SUCH_GROUP;
3802         }
3803
3804         DEBUG(10, ("lookup on Domain SID\n"));
3805
3806         become_root();
3807         result = pdb_enum_group_members(p->mem_ctx, &group_sid,
3808                                         &rid, &num_members);
3809         unbecome_root();
3810
3811         if (!NT_STATUS_IS_OK(result))
3812                 return result;
3813
3814         attr=TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_members);
3815         
3816         if ((num_members!=0) && (attr==NULL))
3817                 return NT_STATUS_NO_MEMORY;
3818         
3819         for (i=0; i<num_members; i++)
3820                 attr[i] = SID_NAME_USER;
3821
3822         init_samr_r_query_groupmem(r_u, num_members, rid, attr, NT_STATUS_OK);
3823
3824         return NT_STATUS_OK;
3825 }
3826
3827 /*********************************************************************
3828  _samr_add_aliasmem
3829 *********************************************************************/
3830
3831 NTSTATUS _samr_add_aliasmem(pipes_struct *p, SAMR_Q_ADD_ALIASMEM *q_u, SAMR_R_ADD_ALIASMEM *r_u)
3832 {
3833         DOM_SID alias_sid;
3834         uint32 acc_granted;
3835         SE_PRIV se_rights;
3836         BOOL can_add_accounts;
3837         NTSTATUS ret;
3838         DISP_INFO *disp_info = NULL;
3839
3840         /* Find the policy handle. Open a policy on it. */
3841         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
3842                 return NT_STATUS_INVALID_HANDLE;
3843         
3844         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_ADD_MEMBER, "_samr_add_aliasmem"))) {
3845                 return r_u->status;
3846         }
3847                 
3848         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
3849         
3850         se_priv_copy( &se_rights, &se_add_users );
3851         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3852
3853         /******** BEGIN SeAddUsers BLOCK *********/
3854         
3855         if ( can_add_accounts )
3856                 become_root();
3857         
3858         ret = pdb_add_aliasmem(&alias_sid, &q_u->sid.sid);
3859         
3860         if ( can_add_accounts )
3861                 unbecome_root();
3862                 
3863         /******** END SeAddUsers BLOCK *********/
3864         
3865         if (NT_STATUS_IS_OK(ret)) {
3866                 force_flush_samr_cache(disp_info);
3867         }
3868
3869         return ret;
3870 }
3871
3872 /*********************************************************************
3873  _samr_del_aliasmem
3874 *********************************************************************/
3875
3876 NTSTATUS _samr_del_aliasmem(pipes_struct *p, SAMR_Q_DEL_ALIASMEM *q_u, SAMR_R_DEL_ALIASMEM *r_u)
3877 {
3878         DOM_SID alias_sid;
3879         uint32 acc_granted;
3880         SE_PRIV se_rights;
3881         BOOL can_add_accounts;
3882         NTSTATUS ret;
3883         DISP_INFO *disp_info = NULL;
3884
3885         /* Find the policy handle. Open a policy on it. */
3886         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
3887                 return NT_STATUS_INVALID_HANDLE;
3888         
3889         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_REMOVE_MEMBER, "_samr_del_aliasmem"))) {
3890                 return r_u->status;
3891         }
3892         
3893         DEBUG(10, ("_samr_del_aliasmem:sid is %s\n",
3894                    sid_string_static(&alias_sid)));
3895
3896         se_priv_copy( &se_rights, &se_add_users );
3897         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3898
3899         /******** BEGIN SeAddUsers BLOCK *********/
3900         
3901         if ( can_add_accounts )
3902                 become_root();
3903
3904         ret = pdb_del_aliasmem(&alias_sid, &q_u->sid.sid);
3905         
3906         if ( can_add_accounts )
3907                 unbecome_root();
3908                 
3909         /******** END SeAddUsers BLOCK *********/
3910         
3911         if (NT_STATUS_IS_OK(ret)) {
3912                 force_flush_samr_cache(disp_info);
3913         }
3914
3915         return ret;
3916 }
3917
3918 /*********************************************************************
3919  _samr_add_groupmem
3920 *********************************************************************/
3921
3922 NTSTATUS _samr_add_groupmem(pipes_struct *p, SAMR_Q_ADD_GROUPMEM *q_u, SAMR_R_ADD_GROUPMEM *r_u)
3923 {
3924         DOM_SID group_sid;
3925         uint32 group_rid;
3926         uint32 acc_granted;
3927         SE_PRIV se_rights;
3928         BOOL can_add_accounts;
3929         DISP_INFO *disp_info = NULL;
3930
3931         /* Find the policy handle. Open a policy on it. */
3932         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info)) 
3933                 return NT_STATUS_INVALID_HANDLE;
3934         
3935         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_ADD_MEMBER, "_samr_add_groupmem"))) {
3936                 return r_u->status;
3937         }
3938
3939         DEBUG(10, ("sid is %s\n", sid_string_static(&group_sid)));
3940
3941         if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
3942                                 &group_rid)) {
3943                 return NT_STATUS_INVALID_HANDLE;
3944         }
3945
3946         se_priv_copy( &se_rights, &se_add_users );
3947         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
3948
3949         /******** BEGIN SeAddUsers BLOCK *********/
3950         
3951         if ( can_add_accounts )
3952                 become_root();
3953
3954         r_u->status = pdb_add_groupmem(p->mem_ctx, group_rid, q_u->rid);
3955                 
3956         if ( can_add_accounts )
3957                 unbecome_root();
3958                 
3959         /******** END SeAddUsers BLOCK *********/
3960         
3961         force_flush_samr_cache(disp_info);
3962
3963         return r_u->status;
3964 }
3965
3966 /*********************************************************************
3967  _samr_del_groupmem
3968 *********************************************************************/
3969
3970 NTSTATUS _samr_del_groupmem(pipes_struct *p, SAMR_Q_DEL_GROUPMEM *q_u, SAMR_R_DEL_GROUPMEM *r_u)
3971 {
3972         DOM_SID group_sid;
3973         uint32 group_rid;
3974         uint32 acc_granted;
3975         SE_PRIV se_rights;
3976         BOOL can_add_accounts;
3977         DISP_INFO *disp_info = NULL;
3978
3979         /*
3980          * delete the group member named q_u->rid
3981          * who is a member of the sid associated with the handle
3982          * the rid is a user's rid as the group is a domain group.
3983          */
3984
3985         /* Find the policy handle. Open a policy on it. */
3986         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info)) 
3987                 return NT_STATUS_INVALID_HANDLE;
3988         
3989         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_REMOVE_MEMBER, "_samr_del_groupmem"))) {
3990                 return r_u->status;
3991         }
3992
3993         if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
3994                                 &group_rid)) {
3995                 return NT_STATUS_INVALID_HANDLE;
3996         }
3997
3998         se_priv_copy( &se_rights, &se_add_users );
3999         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4000
4001         /******** BEGIN SeAddUsers BLOCK *********/
4002         
4003         if ( can_add_accounts )
4004                 become_root();
4005                 
4006         r_u->status = pdb_del_groupmem(p->mem_ctx, group_rid, q_u->rid);
4007
4008         if ( can_add_accounts )
4009                 unbecome_root();
4010                 
4011         /******** END SeAddUsers BLOCK *********/
4012         
4013         force_flush_samr_cache(disp_info);
4014
4015         return r_u->status;
4016 }
4017
4018 /*********************************************************************
4019  _samr_delete_dom_user
4020 *********************************************************************/
4021
4022 NTSTATUS _samr_delete_dom_user(pipes_struct *p, SAMR_Q_DELETE_DOM_USER *q_u, SAMR_R_DELETE_DOM_USER *r_u )
4023 {
4024         DOM_SID user_sid;
4025         struct samu *sam_pass=NULL;
4026         uint32 acc_granted;
4027         BOOL can_add_accounts;
4028         uint32 acb_info;
4029         DISP_INFO *disp_info = NULL;
4030
4031         DEBUG(5, ("_samr_delete_dom_user: %d\n", __LINE__));
4032
4033         /* Find the policy handle. Open a policy on it. */
4034         if (!get_lsa_policy_samr_sid(p, &q_u->user_pol, &user_sid, &acc_granted, &disp_info)) 
4035                 return NT_STATUS_INVALID_HANDLE;
4036                 
4037         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_user"))) {
4038                 return r_u->status;
4039         }
4040                 
4041         if (!sid_check_is_in_our_domain(&user_sid))
4042                 return NT_STATUS_CANNOT_DELETE;
4043
4044         /* check if the user exists before trying to delete */
4045         if ( !(sam_pass = samu_new( NULL )) ) {
4046                 return NT_STATUS_NO_MEMORY;
4047         }
4048
4049         if(!pdb_getsampwsid(sam_pass, &user_sid)) {
4050                 DEBUG(5,("_samr_delete_dom_user:User %s doesn't exist.\n", 
4051                         sid_string_static(&user_sid)));
4052                 TALLOC_FREE(sam_pass);
4053                 return NT_STATUS_NO_SUCH_USER;
4054         }
4055         
4056         acb_info = pdb_get_acct_ctrl(sam_pass);
4057
4058         /* For machine accounts it's the SeMachineAccountPrivilege that counts. */
4059         if ( acb_info & ACB_WSTRUST ) {
4060                 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_machine_account );
4061         } else {
4062                 can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4063         } 
4064
4065         /******** BEGIN SeAddUsers BLOCK *********/
4066         
4067         if ( can_add_accounts )
4068                 become_root();
4069
4070         r_u->status = pdb_delete_user(p->mem_ctx, sam_pass);
4071
4072         if ( can_add_accounts )
4073                 unbecome_root();
4074                 
4075         /******** END SeAddUsers BLOCK *********/
4076                 
4077         if ( !NT_STATUS_IS_OK(r_u->status) ) {
4078                 DEBUG(5,("_samr_delete_dom_user: Failed to delete entry for "
4079                          "user %s: %s.\n", pdb_get_username(sam_pass),
4080                          nt_errstr(r_u->status)));
4081                 TALLOC_FREE(sam_pass);
4082                 return r_u->status;
4083         }
4084
4085
4086         TALLOC_FREE(sam_pass);
4087
4088         if (!close_policy_hnd(p, &q_u->user_pol))
4089                 return NT_STATUS_OBJECT_NAME_INVALID;
4090
4091         force_flush_samr_cache(disp_info);
4092
4093         return NT_STATUS_OK;
4094 }
4095
4096 /*********************************************************************
4097  _samr_delete_dom_group
4098 *********************************************************************/
4099
4100 NTSTATUS _samr_delete_dom_group(pipes_struct *p, SAMR_Q_DELETE_DOM_GROUP *q_u, SAMR_R_DELETE_DOM_GROUP *r_u)
4101 {
4102         DOM_SID group_sid;
4103         uint32 group_rid;
4104         uint32 acc_granted;
4105         SE_PRIV se_rights;
4106         BOOL can_add_accounts;
4107         DISP_INFO *disp_info = NULL;
4108
4109         DEBUG(5, ("samr_delete_dom_group: %d\n", __LINE__));
4110
4111         /* Find the policy handle. Open a policy on it. */
4112         if (!get_lsa_policy_samr_sid(p, &q_u->group_pol, &group_sid, &acc_granted, &disp_info)) 
4113                 return NT_STATUS_INVALID_HANDLE;
4114                 
4115         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_group"))) {
4116                 return r_u->status;
4117         }
4118
4119         DEBUG(10, ("sid is %s\n", sid_string_static(&group_sid)));
4120
4121         if (!sid_peek_check_rid(get_global_sam_sid(), &group_sid,
4122                                 &group_rid)) {
4123                 return NT_STATUS_NO_SUCH_GROUP;
4124         }
4125
4126         se_priv_copy( &se_rights, &se_add_users );
4127         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4128
4129         /******** BEGIN SeAddUsers BLOCK *********/
4130         
4131         if ( can_add_accounts )
4132                 become_root();
4133
4134         r_u->status = pdb_delete_dom_group(p->mem_ctx, group_rid);
4135
4136         if ( can_add_accounts )
4137                 unbecome_root();
4138                 
4139         /******** END SeAddUsers BLOCK *********/
4140         
4141         if ( !NT_STATUS_IS_OK(r_u->status) ) {
4142                 DEBUG(5,("_samr_delete_dom_group: Failed to delete mapping "
4143                          "entry for group %s: %s\n",
4144                          sid_string_static(&group_sid),
4145                          nt_errstr(r_u->status)));
4146                 return r_u->status;
4147         }
4148         
4149         if (!close_policy_hnd(p, &q_u->group_pol))
4150                 return NT_STATUS_OBJECT_NAME_INVALID;
4151
4152         force_flush_samr_cache(disp_info);
4153
4154         return NT_STATUS_OK;
4155 }
4156
4157 /*********************************************************************
4158  _samr_delete_dom_alias
4159 *********************************************************************/
4160
4161 NTSTATUS _samr_delete_dom_alias(pipes_struct *p, SAMR_Q_DELETE_DOM_ALIAS *q_u, SAMR_R_DELETE_DOM_ALIAS *r_u)
4162 {
4163         DOM_SID alias_sid;
4164         uint32 acc_granted;
4165         SE_PRIV se_rights;
4166         BOOL can_add_accounts;
4167         BOOL ret;
4168         DISP_INFO *disp_info = NULL;
4169
4170         DEBUG(5, ("_samr_delete_dom_alias: %d\n", __LINE__));
4171
4172         /* Find the policy handle. Open a policy on it. */
4173         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &alias_sid, &acc_granted, &disp_info)) 
4174                 return NT_STATUS_INVALID_HANDLE;
4175         
4176         /* copy the handle to the outgoing reply */
4177
4178         memcpy( &r_u->pol, &q_u->alias_pol, sizeof(r_u->pol) );
4179
4180         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, "_samr_delete_dom_alias"))) {
4181                 return r_u->status;
4182         }
4183
4184         DEBUG(10, ("sid is %s\n", sid_string_static(&alias_sid)));
4185
4186         /* Don't let Windows delete builtin groups */
4187
4188         if ( sid_check_is_in_builtin( &alias_sid ) ) {
4189                 return NT_STATUS_SPECIAL_ACCOUNT;
4190         }
4191
4192         if (!sid_check_is_in_our_domain(&alias_sid))
4193                 return NT_STATUS_NO_SUCH_ALIAS;
4194                 
4195         DEBUG(10, ("lookup on Local SID\n"));
4196
4197         se_priv_copy( &se_rights, &se_add_users );
4198         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4199
4200         /******** BEGIN SeAddUsers BLOCK *********/
4201         
4202         if ( can_add_accounts )
4203                 become_root();
4204
4205         /* Have passdb delete the alias */
4206         ret = pdb_delete_alias(&alias_sid);
4207         
4208         if ( can_add_accounts )
4209                 unbecome_root();
4210                 
4211         /******** END SeAddUsers BLOCK *********/
4212
4213         if ( !ret )
4214                 return NT_STATUS_ACCESS_DENIED;
4215
4216         if (!close_policy_hnd(p, &q_u->alias_pol))
4217                 return NT_STATUS_OBJECT_NAME_INVALID;
4218
4219         force_flush_samr_cache(disp_info);
4220
4221         return NT_STATUS_OK;
4222 }
4223
4224 /*********************************************************************
4225  _samr_create_dom_group
4226 *********************************************************************/
4227
4228 NTSTATUS _samr_create_dom_group(pipes_struct *p, SAMR_Q_CREATE_DOM_GROUP *q_u, SAMR_R_CREATE_DOM_GROUP *r_u)
4229 {
4230         DOM_SID dom_sid;
4231         DOM_SID info_sid;
4232         const char *name;
4233         struct samr_info *info;
4234         uint32 acc_granted;
4235         SE_PRIV se_rights;
4236         BOOL can_add_accounts;
4237         DISP_INFO *disp_info = NULL;
4238
4239         /* Find the policy handle. Open a policy on it. */
4240         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &dom_sid, &acc_granted, &disp_info)) 
4241                 return NT_STATUS_INVALID_HANDLE;
4242         
4243         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_GROUP, "_samr_create_dom_group"))) {
4244                 return r_u->status;
4245         }
4246                 
4247         if (!sid_equal(&dom_sid, get_global_sam_sid()))
4248                 return NT_STATUS_ACCESS_DENIED;
4249
4250         name = rpcstr_pull_unistr2_talloc(p->mem_ctx, &q_u->uni_acct_desc);
4251         if (name == NULL) {
4252                 return NT_STATUS_NO_MEMORY;
4253         }
4254
4255         r_u->status = can_create(p->mem_ctx, name);
4256         if (!NT_STATUS_IS_OK(r_u->status)) {
4257                 return r_u->status;
4258         }
4259
4260         se_priv_copy( &se_rights, &se_add_users );
4261         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4262
4263         /******** BEGIN SeAddUsers BLOCK *********/
4264         
4265         if ( can_add_accounts )
4266                 become_root();
4267         
4268         /* check that we successfully create the UNIX group */
4269         
4270         r_u->status = pdb_create_dom_group(p->mem_ctx, name, &r_u->rid);
4271
4272         if ( can_add_accounts )
4273                 unbecome_root();
4274                 
4275         /******** END SeAddUsers BLOCK *********/
4276         
4277         /* check if we should bail out here */
4278         
4279         if ( !NT_STATUS_IS_OK(r_u->status) )
4280                 return r_u->status;
4281
4282         sid_compose(&info_sid, get_global_sam_sid(), r_u->rid);
4283         
4284         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4285                 return NT_STATUS_NO_MEMORY;
4286
4287         /* they created it; let the user do what he wants with it */
4288
4289         info->acc_granted = GENERIC_RIGHTS_GROUP_ALL_ACCESS;
4290
4291         /* get a (unique) handle.  open a policy on it. */
4292         if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4293                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4294
4295         force_flush_samr_cache(disp_info);
4296
4297         return NT_STATUS_OK;
4298 }
4299
4300 /*********************************************************************
4301  _samr_create_dom_alias
4302 *********************************************************************/
4303
4304 NTSTATUS _samr_create_dom_alias(pipes_struct *p, SAMR_Q_CREATE_DOM_ALIAS *q_u, SAMR_R_CREATE_DOM_ALIAS *r_u)
4305 {
4306         DOM_SID dom_sid;
4307         DOM_SID info_sid;
4308         fstring name;
4309         struct samr_info *info;
4310         uint32 acc_granted;
4311         gid_t gid;
4312         NTSTATUS result;
4313         SE_PRIV se_rights;
4314         BOOL can_add_accounts;
4315         DISP_INFO *disp_info = NULL;
4316
4317         /* Find the policy handle. Open a policy on it. */
4318         if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &dom_sid, &acc_granted, &disp_info)) 
4319                 return NT_STATUS_INVALID_HANDLE;
4320                 
4321         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_DOMAIN_CREATE_ALIAS, "_samr_create_alias"))) {
4322                 return r_u->status;
4323         }
4324                 
4325         if (!sid_equal(&dom_sid, get_global_sam_sid()))
4326                 return NT_STATUS_ACCESS_DENIED;
4327
4328         unistr2_to_ascii(name, &q_u->uni_acct_desc, sizeof(name)-1);
4329
4330         se_priv_copy( &se_rights, &se_add_users );
4331         can_add_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_rights );
4332
4333         result = can_create(p->mem_ctx, name);
4334         if (!NT_STATUS_IS_OK(result)) {
4335                 return result;
4336         }
4337
4338         /******** BEGIN SeAddUsers BLOCK *********/
4339         
4340         if ( can_add_accounts )
4341                 become_root();
4342
4343         /* Have passdb create the alias */
4344         result = pdb_create_alias(name, &r_u->rid);
4345
4346         if ( can_add_accounts )
4347                 unbecome_root();
4348                 
4349         /******** END SeAddUsers BLOCK *********/
4350
4351         if (!NT_STATUS_IS_OK(result)) {
4352                 DEBUG(10, ("pdb_create_alias failed: %s\n",
4353                            nt_errstr(result)));
4354                 return result;
4355         }
4356
4357         sid_copy(&info_sid, get_global_sam_sid());
4358         sid_append_rid(&info_sid, r_u->rid);
4359
4360         if (!sid_to_gid(&info_sid, &gid)) {
4361                 DEBUG(10, ("Could not find alias just created\n"));
4362                 return NT_STATUS_ACCESS_DENIED;
4363         }
4364
4365         /* check if the group has been successfully created */
4366         if ( getgrgid(gid) == NULL ) {
4367                 DEBUG(10, ("getgrgid(%d) of just created alias failed\n",
4368                            gid));
4369                 return NT_STATUS_ACCESS_DENIED;
4370         }
4371
4372         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4373                 return NT_STATUS_NO_MEMORY;
4374
4375         /* they created it; let the user do what he wants with it */
4376
4377         info->acc_granted = GENERIC_RIGHTS_ALIAS_ALL_ACCESS;
4378
4379         /* get a (unique) handle.  open a policy on it. */
4380         if (!create_policy_hnd(p, &r_u->alias_pol, free_samr_info, (void *)info))
4381                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4382
4383         force_flush_samr_cache(disp_info);
4384
4385         return NT_STATUS_OK;
4386 }
4387
4388 /*********************************************************************
4389  _samr_query_groupinfo
4390
4391 sends the name/comment pair of a domain group
4392 level 1 send also the number of users of that group
4393 *********************************************************************/
4394
4395 NTSTATUS _samr_query_groupinfo(pipes_struct *p, SAMR_Q_QUERY_GROUPINFO *q_u, SAMR_R_QUERY_GROUPINFO *r_u)
4396 {
4397         DOM_SID group_sid;
4398         GROUP_MAP map;
4399         GROUP_INFO_CTR *ctr;
4400         uint32 acc_granted;
4401         BOOL ret;
4402
4403         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, NULL)) 
4404                 return NT_STATUS_INVALID_HANDLE;
4405         
4406         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_LOOKUP_INFO, "_samr_query_groupinfo"))) {
4407                 return r_u->status;
4408         }
4409                 
4410         become_root();
4411         ret = get_domain_group_from_sid(group_sid, &map);
4412         unbecome_root();
4413         if (!ret)
4414                 return NT_STATUS_INVALID_HANDLE;
4415
4416         ctr=TALLOC_ZERO_P(p->mem_ctx, GROUP_INFO_CTR);
4417         if (ctr==NULL)
4418                 return NT_STATUS_NO_MEMORY;
4419
4420         switch (q_u->switch_level) {
4421                 case 1: {
4422                         uint32 *members;
4423                         size_t num_members;
4424
4425                         ctr->switch_value1 = 1;
4426
4427                         become_root();
4428                         r_u->status = pdb_enum_group_members(
4429                                 p->mem_ctx, &group_sid, &members, &num_members);
4430                         unbecome_root();
4431         
4432                         if (!NT_STATUS_IS_OK(r_u->status)) {
4433                                 return r_u->status;
4434                         }
4435
4436                         init_samr_group_info1(&ctr->group.info1, map.nt_name,
4437                                       map.comment, num_members);
4438                         break;
4439                 }
4440                 case 2:
4441                         ctr->switch_value1 = 2;
4442                         init_samr_group_info2(&ctr->group.info2, map.nt_name);
4443                         break;
4444                 case 3:
4445                         ctr->switch_value1 = 3;
4446                         init_samr_group_info3(&ctr->group.info3);
4447                         break;
4448                 case 4:
4449                         ctr->switch_value1 = 4;
4450                         init_samr_group_info4(&ctr->group.info4, map.comment);
4451                         break;
4452                 case 5: {
4453                         /*
4454                         uint32 *members;
4455                         size_t num_members;
4456                         */
4457
4458                         ctr->switch_value1 = 5;
4459
4460                         /*
4461                         become_root();
4462                         r_u->status = pdb_enum_group_members(
4463                                 p->mem_ctx, &group_sid, &members, &num_members);
4464                         unbecome_root();
4465         
4466                         if (!NT_STATUS_IS_OK(r_u->status)) {
4467                                 return r_u->status;
4468                         }
4469                         */
4470                         init_samr_group_info5(&ctr->group.info5, map.nt_name,
4471                                       map.comment, 0 /* num_members */); /* in w2k3 this is always 0 */
4472                         break;
4473                 }
4474                 default:
4475                         return NT_STATUS_INVALID_INFO_CLASS;
4476         }
4477
4478         init_samr_r_query_groupinfo(r_u, ctr, NT_STATUS_OK);
4479
4480         return NT_STATUS_OK;
4481 }
4482
4483 /*********************************************************************
4484  _samr_set_groupinfo
4485  
4486  update a domain group's comment.
4487 *********************************************************************/
4488
4489 NTSTATUS _samr_set_groupinfo(pipes_struct *p, SAMR_Q_SET_GROUPINFO *q_u, SAMR_R_SET_GROUPINFO *r_u)
4490 {
4491         DOM_SID group_sid;
4492         GROUP_MAP map;
4493         GROUP_INFO_CTR *ctr;
4494         uint32 acc_granted;
4495         NTSTATUS ret;
4496         BOOL result;
4497         BOOL can_mod_accounts;
4498         DISP_INFO *disp_info = NULL;
4499
4500         if (!get_lsa_policy_samr_sid(p, &q_u->pol, &group_sid, &acc_granted, &disp_info))
4501                 return NT_STATUS_INVALID_HANDLE;
4502         
4503         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_GROUP_SET_INFO, "_samr_set_groupinfo"))) {
4504                 return r_u->status;
4505         }
4506
4507         become_root();
4508         result = get_domain_group_from_sid(group_sid, &map);
4509         unbecome_root();
4510         if (!result)
4511                 return NT_STATUS_NO_SUCH_GROUP;
4512         
4513         ctr=q_u->ctr;
4514
4515         switch (ctr->switch_value1) {
4516                 case 1:
4517                         unistr2_to_ascii(map.comment, &(ctr->group.info1.uni_acct_desc), sizeof(map.comment)-1);
4518                         break;
4519                 case 4:
4520                         unistr2_to_ascii(map.comment, &(ctr->group.info4.uni_acct_desc), sizeof(map.comment)-1);
4521                         break;
4522                 default:
4523                         return NT_STATUS_INVALID_INFO_CLASS;
4524         }
4525
4526         can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4527
4528         /******** BEGIN SeAddUsers BLOCK *********/
4529
4530         if ( can_mod_accounts )
4531                 become_root();
4532           
4533         ret = pdb_update_group_mapping_entry(&map);
4534
4535         if ( can_mod_accounts )
4536                 unbecome_root();
4537
4538         /******** End SeAddUsers BLOCK *********/
4539
4540         if (NT_STATUS_IS_OK(ret)) {
4541                 force_flush_samr_cache(disp_info);
4542         }
4543
4544         return ret;
4545 }
4546
4547 /*********************************************************************
4548  _samr_set_aliasinfo
4549  
4550  update an alias's comment.
4551 *********************************************************************/
4552
4553 NTSTATUS _samr_set_aliasinfo(pipes_struct *p, SAMR_Q_SET_ALIASINFO *q_u, SAMR_R_SET_ALIASINFO *r_u)
4554 {
4555         DOM_SID group_sid;
4556         struct acct_info info;
4557         ALIAS_INFO_CTR *ctr;
4558         uint32 acc_granted;
4559         BOOL ret;
4560         BOOL can_mod_accounts;
4561         DISP_INFO *disp_info = NULL;
4562
4563         if (!get_lsa_policy_samr_sid(p, &q_u->alias_pol, &group_sid, &acc_granted, &disp_info))
4564                 return NT_STATUS_INVALID_HANDLE;
4565         
4566         if (!NT_STATUS_IS_OK(r_u->status = access_check_samr_function(acc_granted, SA_RIGHT_ALIAS_SET_INFO, "_samr_set_aliasinfo"))) {
4567                 return r_u->status;
4568         }
4569                 
4570         ctr=&q_u->ctr;
4571
4572         /* get the current group information */
4573
4574         if ( !pdb_get_aliasinfo( &group_sid, &info ) ) {
4575                 return NT_STATUS_NO_SUCH_ALIAS;
4576         }
4577
4578         switch (ctr->level) {
4579                 case 2:
4580                         /* We currently do not support renaming groups in the
4581                            the BUILTIN domain.  Refer to util_builtin.c to understand 
4582                            why.  The eventually needs to be fixed to be like Windows
4583                            where you can rename builtin groups, just not delete them */
4584
4585                         if ( sid_check_is_in_builtin( &group_sid ) ) {
4586                                 return NT_STATUS_SPECIAL_ACCOUNT;
4587                         }
4588
4589                         if ( ctr->alias.info2.name.string ) {
4590                                 unistr2_to_ascii( info.acct_name, ctr->alias.info2.name.string, 
4591                                         sizeof(info.acct_name)-1 );
4592                         }
4593                         else
4594                                 fstrcpy( info.acct_name, "" );
4595                         break;
4596                 case 3:
4597                         if ( ctr->alias.info3.description.string ) {
4598                                 unistr2_to_ascii( info.acct_desc, 
4599                                         ctr->alias.info3.description.string, 
4600                                         sizeof(info.acct_desc)-1 );
4601                         }
4602                         else
4603                                 fstrcpy( info.acct_desc, "" );
4604                         break;
4605                 default:
4606                         return NT_STATUS_INVALID_INFO_CLASS;
4607         }
4608
4609         can_mod_accounts = user_has_privileges( p->pipe_user.nt_user_token, &se_add_users );
4610
4611         /******** BEGIN SeAddUsers BLOCK *********/
4612
4613         if ( can_mod_accounts )
4614                 become_root();
4615
4616         ret = pdb_set_aliasinfo( &group_sid, &info );
4617
4618         if ( can_mod_accounts )
4619                 unbecome_root();
4620
4621         /******** End SeAddUsers BLOCK *********/
4622
4623         if (ret) {
4624                 force_flush_samr_cache(disp_info);
4625         }
4626
4627         return ret ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
4628 }
4629
4630 /*********************************************************************
4631  _samr_get_dom_pwinfo
4632 *********************************************************************/
4633
4634 NTSTATUS _samr_get_dom_pwinfo(pipes_struct *p, SAMR_Q_GET_DOM_PWINFO *q_u, SAMR_R_GET_DOM_PWINFO *r_u)
4635 {
4636         /* Perform access check.  Since this rpc does not require a
4637            policy handle it will not be caught by the access checks on
4638            SAMR_CONNECT or SAMR_CONNECT_ANON. */
4639
4640         if (!pipe_access_check(p)) {
4641                 DEBUG(3, ("access denied to samr_get_dom_pwinfo\n"));
4642                 r_u->status = NT_STATUS_ACCESS_DENIED;
4643                 return r_u->status;
4644         }
4645
4646         /* Actually, returning zeros here works quite well :-). */
4647
4648         return NT_STATUS_OK;
4649 }
4650
4651 /*********************************************************************
4652  _samr_open_group
4653 *********************************************************************/
4654
4655 NTSTATUS _samr_open_group(pipes_struct *p, SAMR_Q_OPEN_GROUP *q_u, SAMR_R_OPEN_GROUP *r_u)
4656 {
4657         DOM_SID sid;
4658         DOM_SID info_sid;
4659         GROUP_MAP map;
4660         struct samr_info *info;
4661         SEC_DESC         *psd = NULL;
4662         uint32            acc_granted;
4663         uint32            des_access = q_u->access_mask;
4664         size_t            sd_size;
4665         NTSTATUS          status;
4666         fstring sid_string;
4667         BOOL ret;
4668         SE_PRIV se_rights;
4669
4670         if (!get_lsa_policy_samr_sid(p, &q_u->domain_pol, &sid, &acc_granted, NULL)) 
4671                 return NT_STATUS_INVALID_HANDLE;
4672         
4673         status = access_check_samr_function(acc_granted, 
4674                 SA_RIGHT_DOMAIN_OPEN_ACCOUNT, "_samr_open_group");
4675                 
4676         if ( !NT_STATUS_IS_OK(status) )
4677                 return status;
4678                 
4679         /*check if access can be granted as requested by client. */
4680         make_samr_object_sd(p->mem_ctx, &psd, &sd_size, &grp_generic_mapping, NULL, 0);
4681         se_map_generic(&des_access,&grp_generic_mapping);
4682
4683         se_priv_copy( &se_rights, &se_add_users );
4684
4685         status = access_check_samr_object(psd, p->pipe_user.nt_user_token, 
4686                 &se_rights, GENERIC_RIGHTS_GROUP_WRITE, des_access, 
4687                 &acc_granted, "_samr_open_group");
4688                 
4689         if ( !NT_STATUS_IS_OK(status) ) 
4690                 return status;
4691
4692         /* this should not be hard-coded like this */
4693         
4694         if (!sid_equal(&sid, get_global_sam_sid()))
4695                 return NT_STATUS_ACCESS_DENIED;
4696
4697         sid_copy(&info_sid, get_global_sam_sid());
4698         sid_append_rid(&info_sid, q_u->rid_group);
4699         sid_to_string(sid_string, &info_sid);
4700
4701         if ((info = get_samr_info_by_sid(&info_sid)) == NULL)
4702                 return NT_STATUS_NO_MEMORY;
4703                 
4704         info->acc_granted = acc_granted;
4705
4706         DEBUG(10, ("_samr_open_group:Opening SID: %s\n", sid_string));
4707
4708         /* check if that group really exists */
4709         become_root();
4710         ret = get_domain_group_from_sid(info->sid, &map);
4711         unbecome_root();
4712         if (!ret)
4713                 return NT_STATUS_NO_SUCH_GROUP;
4714
4715         /* get a (unique) handle.  open a policy on it. */
4716         if (!create_policy_hnd(p, &r_u->pol, free_samr_info, (void *)info))
4717                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4718
4719         return NT_STATUS_OK;
4720 }
4721
4722 /*********************************************************************
4723  _samr_remove_sid_foreign_domain
4724 *********************************************************************/
4725
4726 NTSTATUS _samr_remove_sid_foreign_domain(pipes_struct *p, 
4727                                           SAMR_Q_REMOVE_SID_FOREIGN_DOMAIN *q_u, 
4728                                           SAMR_R_REMOVE_SID_FOREIGN_DOMAIN *r_u)
4729 {
4730         DOM_SID                 delete_sid, domain_sid;
4731         uint32                  acc_granted;
4732         NTSTATUS                result;
4733         DISP_INFO *disp_info = NULL;
4734
4735         sid_copy( &delete_sid, &q_u->sid.sid );
4736         
4737         DEBUG(5,("_samr_remove_sid_foreign_domain: removing SID [%s]\n",
4738                 sid_string_static(&delete_sid)));
4739                 
4740         /* Find the policy handle. Open a policy on it. */
4741         
4742         if (!get_lsa_policy_samr_sid(p, &q_u->dom_pol, &domain_sid,
4743                                      &acc_granted, &disp_info)) 
4744                 return NT_STATUS_INVALID_HANDLE;
4745         
4746         result = access_check_samr_function(acc_granted, STD_RIGHT_DELETE_ACCESS, 
4747                 "_samr_remove_sid_foreign_domain");
4748                 
4749         if (!NT_STATUS_IS_OK(result)) 
4750                 return result;
4751                         
4752         DEBUG(8, ("_samr_remove_sid_foreign_domain:sid is %s\n", 
4753                 sid_string_static(&domain_sid)));
4754
4755         /* we can only delete a user from a group since we don't have 
4756            nested groups anyways.  So in the latter case, just say OK */
4757
4758         /* TODO: The above comment nowadays is bogus. Since we have nested
4759          * groups now, and aliases members are never reported out of the unix
4760          * group membership, the "just say OK" makes this call a no-op. For
4761          * us. This needs fixing however. */
4762
4763         /* I've only ever seen this in the wild when deleting a user from
4764          * usrmgr.exe. domain_sid is the builtin domain, and the sid to delete
4765          * is the user about to be deleted. I very much suspect this is the
4766          * only application of this call. To verify this, let people report
4767          * other cases. */
4768
4769         if (!sid_check_is_builtin(&domain_sid)) {
4770                 DEBUG(1,("_samr_remove_sid_foreign_domain: domain_sid = %s, "
4771                          "global_sam_sid() = %s\n",
4772                          sid_string_static(&domain_sid),
4773                          sid_string_static(get_global_sam_sid())));
4774                 DEBUGADD(1,("please report to samba-technical@samba.org!\n"));
4775                 return NT_STATUS_OK;
4776         }
4777
4778         force_flush_samr_cache(disp_info);
4779
4780         result = NT_STATUS_OK;
4781
4782         return result;
4783 }
4784
4785 /*******************************************************************
4786  _samr_query_domain_info2
4787  ********************************************************************/
4788
4789 NTSTATUS _samr_query_domain_info2(pipes_struct *p,
4790                                   SAMR_Q_QUERY_DOMAIN_INFO2 *q_u,
4791                                   SAMR_R_QUERY_DOMAIN_INFO2 *r_u)
4792 {
4793         SAMR_Q_QUERY_DOMAIN_INFO q;
4794         SAMR_R_QUERY_DOMAIN_INFO r;
4795
4796         ZERO_STRUCT(q);
4797         ZERO_STRUCT(r);
4798
4799         DEBUG(5,("_samr_query_domain_info2: %d\n", __LINE__));
4800
4801         q.domain_pol = q_u->domain_pol;
4802         q.switch_value = q_u->switch_value;
4803
4804         r_u->status = _samr_query_domain_info(p, &q, &r);
4805
4806         r_u->ptr_0              = r.ptr_0;
4807         r_u->switch_value       = r.switch_value;
4808         r_u->ctr                = r.ctr;
4809
4810         return r_u->status;
4811 }
4812
4813 /*******************************************************************
4814  _samr_set_dom_info
4815  ********************************************************************/
4816
4817 NTSTATUS _samr_set_dom_info(pipes_struct *p, SAMR_Q_SET_DOMAIN_INFO *q_u, SAMR_R_SET_DOMAIN_INFO *r_u)
4818 {
4819         time_t u_expire, u_min_age;
4820         time_t u_logout;
4821         time_t u_lock_duration, u_reset_time;
4822
4823         r_u->status = NT_STATUS_OK;
4824
4825         DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4826
4827         /* find the policy handle.  open a policy on it. */
4828         if (!find_policy_by_hnd(p, &q_u->domain_pol, NULL))
4829                 return NT_STATUS_INVALID_HANDLE;
4830
4831         DEBUG(5,("_samr_set_dom_info: switch_value: %d\n", q_u->switch_value));
4832
4833         switch (q_u->switch_value) {
4834                 case 0x01:
4835                         u_expire=nt_time_to_unix_abs(&q_u->ctr->info.inf1.expire);
4836                         u_min_age=nt_time_to_unix_abs(&q_u->ctr->info.inf1.min_passwordage);
4837                         
4838                         pdb_set_account_policy(AP_MIN_PASSWORD_LEN, (uint32)q_u->ctr->info.inf1.min_length_password);
4839                         pdb_set_account_policy(AP_PASSWORD_HISTORY, (uint32)q_u->ctr->info.inf1.password_history);
4840                         pdb_set_account_policy(AP_USER_MUST_LOGON_TO_CHG_PASS, (uint32)q_u->ctr->info.inf1.password_properties);
4841                         pdb_set_account_policy(AP_MAX_PASSWORD_AGE, (int)u_expire);
4842                         pdb_set_account_policy(AP_MIN_PASSWORD_AGE, (int)u_min_age);
4843                         break;
4844                 case 0x02:
4845                         break;
4846                 case 0x03:
4847                         u_logout=nt_time_to_unix_abs(&q_u->ctr->info.inf3.logout);
4848                         pdb_set_account_policy(AP_TIME_TO_LOGOUT, (int)u_logout);
4849                         break;
4850                 case 0x05:
4851                         break;
4852                 case 0x06:
4853                         break;
4854                 case 0x07:
4855                         break;
4856                 case 0x0c:
4857                         u_lock_duration=nt_time_to_unix_abs(&q_u->ctr->info.inf12.duration);
4858                         if (u_lock_duration != -1)
4859                                 u_lock_duration /= 60;
4860
4861                         u_reset_time=nt_time_to_unix_abs(&q_u->ctr->info.inf12.reset_count)/60;
4862                         
4863                         pdb_set_account_policy(AP_LOCK_ACCOUNT_DURATION, (int)u_lock_duration);
4864                         pdb_set_account_policy(AP_RESET_COUNT_TIME, (int)u_reset_time);
4865                         pdb_set_account_policy(AP_BAD_ATTEMPT_LOCKOUT, (uint32)q_u->ctr->info.inf12.bad_attempt_lockout);
4866                         break;
4867                 default:
4868                         return NT_STATUS_INVALID_INFO_CLASS;
4869         }
4870
4871         init_samr_r_set_domain_info(r_u, NT_STATUS_OK);
4872
4873         DEBUG(5,("_samr_set_dom_info: %d\n", __LINE__));
4874
4875         return r_u->status;
4876 }