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