02f7f090a4b1ace1dccbeb723e5c7696f89af366
[ira/wip.git] / source3 / rpc_server / srv_lsa_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) Jeremy Allison                    2001, 2006.
8  *  Copyright (C) Rafal Szczesniak                  2002,
9  *  Copyright (C) Jim McDonough <jmcd@us.ibm.com>   2002,
10  *  Copyright (C) Simo Sorce                        2003.
11  *  Copyright (C) Gerald (Jerry) Carter             2005.
12  *  Copyright (C) Volker Lendecke                   2005.
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 3 of the License, or
17  *  (at your option) any later version.
18  *  
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *  
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
26  */
27
28 /* This is the implementation of the lsa server code. */
29
30 #include "includes.h"
31
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_RPC_SRV
34
35 extern PRIVS privs[];
36
37 struct lsa_info {
38         DOM_SID sid;
39         uint32 access;
40 };
41
42 const struct generic_mapping lsa_generic_mapping = {
43         POLICY_READ,
44         POLICY_WRITE,
45         POLICY_EXECUTE,
46         POLICY_ALL_ACCESS
47 };
48
49 /*******************************************************************
50  Function to free the per handle data.
51  ********************************************************************/
52
53 static void free_lsa_info(void *ptr)
54 {
55         struct lsa_info *lsa = (struct lsa_info *)ptr;
56
57         SAFE_FREE(lsa);
58 }
59
60 /***************************************************************************
61 Init dom_query
62  ***************************************************************************/
63
64 static void init_dom_query_3(DOM_QUERY_3 *d_q, const char *dom_name, DOM_SID *dom_sid)
65 {
66         d_q->buffer_dom_name = (dom_name != NULL) ? 1 : 0; /* domain buffer pointer */
67         d_q->buffer_dom_sid = (dom_sid != NULL) ? 1 : 0;  /* domain sid pointer */
68
69         /* this string is supposed to be non-null terminated. */
70         /* But the maxlen in this UNISTR2 must include the terminating null. */
71         init_unistr2(&d_q->uni_domain_name, dom_name, UNI_BROKEN_NON_NULL);
72
73         /*
74          * I'm not sure why this really odd combination of length
75          * values works, but it does appear to. I need to look at
76          * this *much* more closely - but at the moment leave alone
77          * until it's understood. This allows a W2k client to join
78          * a domain with both odd and even length names... JRA.
79          */
80
81         /*
82          * IMPORTANT NOTE !!!!
83          * The two fields below probably are reversed in meaning, ie.
84          * the first field is probably the str_len, the second the max
85          * len. Both are measured in bytes anyway.
86          */
87
88         d_q->uni_dom_str_len = d_q->uni_domain_name.uni_max_len * 2;
89         d_q->uni_dom_max_len = d_q->uni_domain_name.uni_str_len * 2;
90
91         if (dom_sid != NULL)
92                 init_dom_sid2(&d_q->dom_sid, dom_sid);
93 }
94
95 /***************************************************************************
96 Init dom_query
97  ***************************************************************************/
98
99 static void init_dom_query_5(DOM_QUERY_5 *d_q, const char *dom_name, DOM_SID *dom_sid)
100 {
101         init_dom_query_3(d_q, dom_name, dom_sid);
102 }
103
104 /***************************************************************************
105  init_dom_ref - adds a domain if it's not already in, returns the index.
106 ***************************************************************************/
107
108 static int init_dom_ref(DOM_R_REF *ref, const char *dom_name, DOM_SID *dom_sid)
109 {
110         int num = 0;
111
112         if (dom_name != NULL) {
113                 for (num = 0; num < ref->num_ref_doms_1; num++) {
114                         if (sid_equal(dom_sid, &ref->ref_dom[num].ref_dom.sid))
115                                 return num;
116                 }
117         } else {
118                 num = ref->num_ref_doms_1;
119         }
120
121         if (num >= MAX_REF_DOMAINS) {
122                 /* index not found, already at maximum domain limit */
123                 return -1;
124         }
125
126         ref->num_ref_doms_1 = num+1;
127         ref->ptr_ref_dom  = 1;
128         ref->max_entries = MAX_REF_DOMAINS;
129         ref->num_ref_doms_2 = num+1;
130
131         ref->hdr_ref_dom[num].ptr_dom_sid = 1; /* dom sid cannot be NULL. */
132
133         init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, UNI_FLAGS_NONE);
134         init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, &ref->ref_dom[num].uni_dom_name);
135
136         init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
137
138         return num;
139 }
140
141 /***************************************************************************
142  lookup_lsa_rids. Must be called as root for lookup_name to work.
143  ***************************************************************************/
144
145 static NTSTATUS lookup_lsa_rids(TALLOC_CTX *mem_ctx,
146                         DOM_R_REF *ref,
147                         DOM_RID *prid,
148                         uint32 num_entries,
149                         const UNISTR2 *name,
150                         int flags,
151                         uint32 *pmapped_count)
152 {
153         uint32 mapped_count, i;
154
155         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
156
157         mapped_count = 0;
158         *pmapped_count = 0;
159
160         for (i = 0; i < num_entries; i++) {
161                 DOM_SID sid;
162                 uint32 rid;
163                 int dom_idx;
164                 char *full_name;
165                 const char *domain;
166                 enum lsa_SidType type = SID_NAME_UNKNOWN;
167
168                 /* Split name into domain and user component */
169
170                 full_name = rpcstr_pull_unistr2_talloc(mem_ctx, &name[i]);
171                 if (full_name == NULL) {
172                         DEBUG(0, ("pull_ucs2_talloc failed\n"));
173                         return NT_STATUS_NO_MEMORY;
174                 }
175
176                 DEBUG(5, ("lookup_lsa_rids: looking up name %s\n", full_name));
177
178                 /* We can ignore the result of lookup_name, it will not touch
179                    "type" if it's not successful */
180
181                 lookup_name(mem_ctx, full_name, flags, &domain, NULL,
182                             &sid, &type);
183
184                 switch (type) {
185                 case SID_NAME_USER:
186                 case SID_NAME_DOM_GRP:
187                 case SID_NAME_DOMAIN:
188                 case SID_NAME_ALIAS:
189                 case SID_NAME_WKN_GRP:
190                         DEBUG(5, ("init_lsa_rids: %s found\n", full_name));
191                         /* Leave these unchanged */
192                         break;
193                 default:
194                         /* Don't hand out anything but the list above */
195                         DEBUG(5, ("init_lsa_rids: %s not found\n", full_name));
196                         type = SID_NAME_UNKNOWN;
197                         break;
198                 }
199
200                 rid = 0;
201                 dom_idx = -1;
202
203                 if (type != SID_NAME_UNKNOWN) {
204                         sid_split_rid(&sid, &rid);
205                         dom_idx = init_dom_ref(ref, domain, &sid);
206                         mapped_count++;
207                 }
208
209                 init_dom_rid(&prid[i], rid, type, dom_idx);
210         }
211
212         *pmapped_count = mapped_count;
213         return NT_STATUS_OK;
214 }
215
216 /***************************************************************************
217  lookup_lsa_sids. Must be called as root for lookup_name to work.
218  ***************************************************************************/
219
220 static NTSTATUS lookup_lsa_sids(TALLOC_CTX *mem_ctx,
221                         DOM_R_REF *ref,
222                         LSA_TRANSLATED_SID3 *trans_sids,
223                         uint32 num_entries,
224                         const UNISTR2 *name,
225                         int flags,
226                         uint32 *pmapped_count)
227 {
228         uint32 mapped_count, i;
229
230         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
231
232         mapped_count = 0;
233         *pmapped_count = 0;
234
235         for (i = 0; i < num_entries; i++) {
236                 DOM_SID sid;
237                 uint32 rid;
238                 int dom_idx;
239                 char *full_name;
240                 const char *domain;
241                 enum lsa_SidType type = SID_NAME_UNKNOWN;
242
243                 /* Split name into domain and user component */
244
245                 full_name = rpcstr_pull_unistr2_talloc(mem_ctx, &name[i]);
246                 if (full_name == NULL) {
247                         DEBUG(0, ("pull_ucs2_talloc failed\n"));
248                         return NT_STATUS_NO_MEMORY;
249                 }
250
251                 DEBUG(5, ("init_lsa_sids: looking up name %s\n", full_name));
252
253                 /* We can ignore the result of lookup_name, it will not touch
254                    "type" if it's not successful */
255
256                 lookup_name(mem_ctx, full_name, flags, &domain, NULL,
257                             &sid, &type);
258
259                 switch (type) {
260                 case SID_NAME_USER:
261                 case SID_NAME_DOM_GRP:
262                 case SID_NAME_DOMAIN:
263                 case SID_NAME_ALIAS:
264                 case SID_NAME_WKN_GRP:
265                         DEBUG(5, ("init_lsa_sids: %s found\n", full_name));
266                         /* Leave these unchanged */
267                         break;
268                 default:
269                         /* Don't hand out anything but the list above */
270                         DEBUG(5, ("init_lsa_sids: %s not found\n", full_name));
271                         type = SID_NAME_UNKNOWN;
272                         break;
273                 }
274
275                 rid = 0;
276                 dom_idx = -1;
277
278                 if (type != SID_NAME_UNKNOWN) {
279                         DOM_SID domain_sid;
280                         sid_copy(&domain_sid, &sid);
281                         sid_split_rid(&domain_sid, &rid);
282                         dom_idx = init_dom_ref(ref, domain, &domain_sid);
283                         mapped_count++;
284                 }
285
286                 /* Initialize the LSA_TRANSLATED_SID3 return. */
287                 trans_sids[i].sid_type = type;
288                 trans_sids[i].sid2 = TALLOC_P(mem_ctx, DOM_SID2);
289                 if (trans_sids[i].sid2 == NULL) {
290                         return NT_STATUS_NO_MEMORY;
291                 }
292                 init_dom_sid2(trans_sids[i].sid2, &sid);
293                 trans_sids[i].sid_idx = dom_idx;
294         }
295
296         *pmapped_count = mapped_count;
297         return NT_STATUS_OK;
298 }
299
300 /***************************************************************************
301  init_reply_lookup_names
302  ***************************************************************************/
303
304 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
305                 DOM_R_REF *ref, uint32 num_entries,
306                 DOM_RID *rid, uint32 mapped_count)
307 {
308         r_l->ptr_dom_ref  = 1;
309         r_l->dom_ref      = ref;
310
311         r_l->num_entries  = num_entries;
312         r_l->ptr_entries  = 1;
313         r_l->num_entries2 = num_entries;
314         r_l->dom_rid      = rid;
315
316         r_l->mapped_count = mapped_count;
317 }
318
319 /***************************************************************************
320  init_reply_lookup_names2
321  ***************************************************************************/
322
323 static void init_reply_lookup_names2(LSA_R_LOOKUP_NAMES2 *r_l,
324                 DOM_R_REF *ref, uint32 num_entries,
325                 DOM_RID2 *rid, uint32 mapped_count)
326 {
327         r_l->ptr_dom_ref  = 1;
328         r_l->dom_ref      = ref;
329
330         r_l->num_entries  = num_entries;
331         r_l->ptr_entries  = 1;
332         r_l->num_entries2 = num_entries;
333         r_l->dom_rid      = rid;
334
335         r_l->mapped_count = mapped_count;
336 }
337
338 /***************************************************************************
339  init_reply_lookup_names3
340  ***************************************************************************/
341
342 static void init_reply_lookup_names3(LSA_R_LOOKUP_NAMES3 *r_l,
343                 DOM_R_REF *ref, uint32 num_entries,
344                 LSA_TRANSLATED_SID3 *trans_sids, uint32 mapped_count)
345 {
346         r_l->ptr_dom_ref  = 1;
347         r_l->dom_ref      = ref;
348
349         r_l->num_entries  = num_entries;
350         r_l->ptr_entries  = 1;
351         r_l->num_entries2 = num_entries;
352         r_l->trans_sids   = trans_sids;
353
354         r_l->mapped_count = mapped_count;
355 }
356
357 /***************************************************************************
358  init_reply_lookup_names4
359  ***************************************************************************/
360
361 static void init_reply_lookup_names4(LSA_R_LOOKUP_NAMES4 *r_l,
362                 DOM_R_REF *ref, uint32 num_entries,
363                 LSA_TRANSLATED_SID3 *trans_sids, uint32 mapped_count)
364 {
365         r_l->ptr_dom_ref  = 1;
366         r_l->dom_ref      = ref;
367
368         r_l->num_entries  = num_entries;
369         r_l->ptr_entries  = 1;
370         r_l->num_entries2 = num_entries;
371         r_l->trans_sids   = trans_sids;
372
373         r_l->mapped_count = mapped_count;
374 }
375
376 /***************************************************************************
377  Init_reply_lookup_sids.
378  ***************************************************************************/
379
380 static void init_reply_lookup_sids2(LSA_R_LOOKUP_SIDS2 *r_l,
381                                 DOM_R_REF *ref,
382                                 uint32 mapped_count)
383 {
384         r_l->ptr_dom_ref  = ref ? 1 : 0;
385         r_l->dom_ref      = ref;
386         r_l->mapped_count = mapped_count;
387 }
388
389 /***************************************************************************
390  Init_reply_lookup_sids.
391  ***************************************************************************/
392
393 static void init_reply_lookup_sids3(LSA_R_LOOKUP_SIDS3 *r_l,
394                                 DOM_R_REF *ref,
395                                 uint32 mapped_count)
396 {
397         r_l->ptr_dom_ref  = ref ? 1 : 0;
398         r_l->dom_ref      = ref;
399         r_l->mapped_count = mapped_count;
400 }
401
402 /***************************************************************************
403  Init_reply_lookup_sids.
404  ***************************************************************************/
405
406 static NTSTATUS init_reply_lookup_sids(TALLOC_CTX *mem_ctx,
407                                 LSA_R_LOOKUP_SIDS *r_l,
408                                 DOM_R_REF *ref,
409                                 LSA_TRANS_NAME_ENUM2 *names,
410                                 uint32 mapped_count)
411 {
412         LSA_TRANS_NAME_ENUM *oldnames = &r_l->names;
413
414         oldnames->num_entries = names->num_entries;
415         oldnames->ptr_trans_names = names->ptr_trans_names;
416         oldnames->num_entries2 = names->num_entries2;
417         oldnames->uni_name = names->uni_name;
418
419         if (names->num_entries) {
420                 int i;
421
422                 oldnames->name = TALLOC_ARRAY(mem_ctx, LSA_TRANS_NAME, names->num_entries);
423
424                 if (!oldnames->name) {
425                         return NT_STATUS_NO_MEMORY;
426                 }
427                 for (i = 0; i < names->num_entries; i++) {
428                         oldnames->name[i].sid_name_use = names->name[i].sid_name_use;
429                         oldnames->name[i].hdr_name = names->name[i].hdr_name;
430                         oldnames->name[i].domain_idx = names->name[i].domain_idx;
431                 }
432         }
433
434         r_l->ptr_dom_ref  = ref ? 1 : 0;
435         r_l->dom_ref      = ref;
436         r_l->mapped_count = mapped_count;
437         return NT_STATUS_OK;
438 }
439
440 static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size)
441 {
442         DOM_SID local_adm_sid;
443         DOM_SID adm_sid;
444
445         SEC_ACE ace[3];
446         SEC_ACCESS mask;
447
448         SEC_ACL *psa = NULL;
449
450         init_sec_access(&mask, POLICY_EXECUTE);
451         init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
452
453         sid_copy(&adm_sid, get_global_sam_sid());
454         sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
455         init_sec_access(&mask, POLICY_ALL_ACCESS);
456         init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
457
458         sid_copy(&local_adm_sid, &global_sid_Builtin);
459         sid_append_rid(&local_adm_sid, BUILTIN_ALIAS_RID_ADMINS);
460         init_sec_access(&mask, POLICY_ALL_ACCESS);
461         init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
462
463         if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
464                 return NT_STATUS_NO_MEMORY;
465
466         if((*sd = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, &adm_sid, NULL, NULL, psa, sd_size)) == NULL)
467                 return NT_STATUS_NO_MEMORY;
468
469         return NT_STATUS_OK;
470 }
471
472 #if 0   /* AD DC work in ongoing in Samba 4 */
473
474 /***************************************************************************
475  Init_dns_dom_info.
476 ***************************************************************************/
477
478 static void init_dns_dom_info(LSA_DNS_DOM_INFO *r_l, const char *nb_name,
479                               const char *dns_name, const char *forest_name,
480                               struct GUID *dom_guid, DOM_SID *dom_sid)
481 {
482         if (nb_name && *nb_name) {
483                 init_unistr2(&r_l->uni_nb_dom_name, nb_name, UNI_FLAGS_NONE);
484                 init_uni_hdr(&r_l->hdr_nb_dom_name, &r_l->uni_nb_dom_name);
485                 r_l->hdr_nb_dom_name.uni_max_len += 2;
486                 r_l->uni_nb_dom_name.uni_max_len += 1;
487         }
488         
489         if (dns_name && *dns_name) {
490                 init_unistr2(&r_l->uni_dns_dom_name, dns_name, UNI_FLAGS_NONE);
491                 init_uni_hdr(&r_l->hdr_dns_dom_name, &r_l->uni_dns_dom_name);
492                 r_l->hdr_dns_dom_name.uni_max_len += 2;
493                 r_l->uni_dns_dom_name.uni_max_len += 1;
494         }
495
496         if (forest_name && *forest_name) {
497                 init_unistr2(&r_l->uni_forest_name, forest_name, UNI_FLAGS_NONE);
498                 init_uni_hdr(&r_l->hdr_forest_name, &r_l->uni_forest_name);
499                 r_l->hdr_forest_name.uni_max_len += 2;
500                 r_l->uni_forest_name.uni_max_len += 1;
501         }
502
503         /* how do we init the guid ? probably should write an init fn */
504         if (dom_guid) {
505                 memcpy(&r_l->dom_guid, dom_guid, sizeof(struct GUID));
506         }
507         
508         if (dom_sid) {
509                 r_l->ptr_dom_sid = 1;
510                 init_dom_sid2(&r_l->dom_sid, dom_sid);
511         }
512 }
513 #endif  /* AD DC work in ongoing in Samba 4 */
514
515
516 /***************************************************************************
517  _lsa_open_policy2.
518  ***************************************************************************/
519
520 NTSTATUS _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
521 {
522         struct lsa_info *info;
523         SEC_DESC *psd = NULL;
524         size_t sd_size;
525         uint32 des_access=q_u->des_access;
526         uint32 acc_granted;
527         NTSTATUS status;
528
529
530         /* map the generic bits to the lsa policy ones */
531         se_map_generic(&des_access, &lsa_generic_mapping);
532
533         /* get the generic lsa policy SD until we store it */
534         lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
535
536         if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
537                 if (p->pipe_user.ut.uid != sec_initial_uid()) {
538                         return status;
539                 }
540                 DEBUG(4,("ACCESS should be DENIED (granted: %#010x;  required: %#010x)\n",
541                          acc_granted, des_access));
542                 DEBUGADD(4,("but overwritten by euid == 0\n"));
543         }
544
545         /* This is needed for lsa_open_account and rpcclient .... :-) */
546
547         if (p->pipe_user.ut.uid == sec_initial_uid())
548                 acc_granted = POLICY_ALL_ACCESS;
549
550         /* associate the domain SID with the (unique) handle. */
551         if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
552                 return NT_STATUS_NO_MEMORY;
553
554         ZERO_STRUCTP(info);
555         sid_copy(&info->sid,get_global_sam_sid());
556         info->access = acc_granted;
557
558         /* set up the LSA QUERY INFO response */
559         if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
560                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
561
562         return NT_STATUS_OK;
563 }
564
565 /***************************************************************************
566  _lsa_open_policy
567  ***************************************************************************/
568
569 NTSTATUS _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
570 {
571         struct lsa_info *info;
572         SEC_DESC *psd = NULL;
573         size_t sd_size;
574         uint32 des_access=q_u->des_access;
575         uint32 acc_granted;
576         NTSTATUS status;
577
578
579         /* map the generic bits to the lsa policy ones */
580         se_map_generic(&des_access, &lsa_generic_mapping);
581
582         /* get the generic lsa policy SD until we store it */
583         lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
584
585         if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
586                 if (geteuid() != 0) {
587                         return status;
588                 }
589                 DEBUG(4,("ACCESS should be DENIED (granted: %#010x;  required: %#010x)\n",
590                          acc_granted, des_access));
591                 DEBUGADD(4,("but overwritten by euid == 0\n"));
592                 acc_granted = des_access;
593         }
594
595         /* associate the domain SID with the (unique) handle. */
596         if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
597                 return NT_STATUS_NO_MEMORY;
598
599         ZERO_STRUCTP(info);
600         sid_copy(&info->sid,get_global_sam_sid());
601         info->access = acc_granted;
602
603         /* set up the LSA QUERY INFO response */
604         if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
605                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
606
607         return NT_STATUS_OK;
608 }
609
610 /***************************************************************************
611  _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
612  ufff, done :)  mimir
613  ***************************************************************************/
614
615 NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u,
616                              LSA_R_ENUM_TRUST_DOM *r_u)
617 {
618         struct lsa_info *info;
619         uint32 next_idx;
620         struct trustdom_info **domains;
621
622         /*
623          * preferred length is set to 5 as a "our" preferred length
624          * nt sets this parameter to 2
625          * update (20.08.2002): it's not preferred length, but preferred size!
626          * it needs further investigation how to optimally choose this value
627          */
628         uint32 max_num_domains =
629                 q_u->preferred_len < 5 ? q_u->preferred_len : 10;
630         uint32 num_domains;
631         NTSTATUS nt_status;
632         uint32 num_thistime;
633
634         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
635                 return NT_STATUS_INVALID_HANDLE;
636
637         /* check if the user have enough rights */
638         if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
639                 return NT_STATUS_ACCESS_DENIED;
640
641         nt_status = pdb_enum_trusteddoms(p->mem_ctx, &num_domains, &domains);
642
643         if (!NT_STATUS_IS_OK(nt_status)) {
644                 return nt_status;
645         }
646
647         if (q_u->enum_context < num_domains) {
648                 num_thistime = MIN(num_domains, max_num_domains);
649
650                 r_u->status = STATUS_MORE_ENTRIES;
651
652                 if (q_u->enum_context + num_thistime > num_domains) {
653                         num_thistime = num_domains - q_u->enum_context;
654                         r_u->status = NT_STATUS_OK;
655                 }
656
657                 next_idx = q_u->enum_context + num_thistime;
658         } else {
659                 num_thistime = 0;
660                 next_idx = 0xffffffff;
661                 r_u->status = NT_STATUS_NO_MORE_ENTRIES;
662         }
663                 
664         /* set up the lsa_enum_trust_dom response */
665
666         init_r_enum_trust_dom(p->mem_ctx, r_u, next_idx,
667                               num_thistime, domains+q_u->enum_context);
668
669         return r_u->status;
670 }
671
672 /***************************************************************************
673  _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
674  ***************************************************************************/
675
676 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
677 {
678         struct lsa_info *handle;
679         LSA_INFO_CTR *ctr = &r_u->ctr;
680         DOM_SID domain_sid;
681         const char *name;
682         DOM_SID *sid = NULL;
683
684         r_u->status = NT_STATUS_OK;
685
686         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
687                 return NT_STATUS_INVALID_HANDLE;
688
689         switch (q_u->info_class) {
690         case 0x02:
691                 {
692
693                 uint32 policy_def = LSA_AUDIT_POLICY_ALL;
694                 
695                 /* check if the user have enough rights */
696                 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION)) {
697                         DEBUG(10,("_lsa_query_info: insufficient access rights\n"));
698                         return NT_STATUS_ACCESS_DENIED;
699                 }
700
701                 /* fake info: We audit everything. ;) */
702                 ctr->info.id2.ptr = 1;
703                 ctr->info.id2.auditing_enabled = True;
704                 ctr->info.id2.count1 = ctr->info.id2.count2 = LSA_AUDIT_NUM_CATEGORIES;
705
706                 if ((ctr->info.id2.auditsettings = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, LSA_AUDIT_NUM_CATEGORIES)) == NULL)
707                         return NT_STATUS_NO_MEMORY;
708
709                 ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_ACCOUNT_MANAGEMENT] = policy_def;
710                 ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_FILE_AND_OBJECT_ACCESS] = policy_def; 
711                 ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_LOGON] = policy_def; 
712                 ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_PROCCESS_TRACKING] = policy_def; 
713                 ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_SECURITY_POLICY_CHANGES] = policy_def;
714                 ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_SYSTEM] = policy_def;
715                 ctr->info.id2.auditsettings[LSA_AUDIT_CATEGORY_USE_OF_USER_RIGHTS] = policy_def; 
716
717                 break;
718                 }
719         case 0x03:
720                 /* check if the user have enough rights */
721                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
722                         return NT_STATUS_ACCESS_DENIED;
723
724                 /* Request PolicyPrimaryDomainInformation. */
725                 switch (lp_server_role()) {
726                         case ROLE_DOMAIN_PDC:
727                         case ROLE_DOMAIN_BDC:
728                                 name = get_global_sam_name();
729                                 sid = get_global_sam_sid();
730                                 break;
731                         case ROLE_DOMAIN_MEMBER:
732                                 name = lp_workgroup();
733                                 /* We need to return the Domain SID here. */
734                                 if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid))
735                                         sid = &domain_sid;
736                                 else
737                                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
738                                 break;
739                         case ROLE_STANDALONE:
740                                 name = lp_workgroup();
741                                 sid = NULL;
742                                 break;
743                         default:
744                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
745                 }
746                 init_dom_query_3(&r_u->ctr.info.id3, name, sid);
747                 break;
748         case 0x05:
749                 /* check if the user have enough rights */
750                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
751                         return NT_STATUS_ACCESS_DENIED;
752
753                 /* Request PolicyAccountDomainInformation. */
754                 name = get_global_sam_name();
755                 sid = get_global_sam_sid();
756                 init_dom_query_5(&r_u->ctr.info.id5, name, sid);
757                 break;
758         case 0x06:
759                 /* check if the user have enough rights */
760                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
761                         return NT_STATUS_ACCESS_DENIED;
762
763                 switch (lp_server_role()) {
764                         case ROLE_DOMAIN_BDC:
765                                 /*
766                                  * only a BDC is a backup controller
767                                  * of the domain, it controls.
768                                  */
769                                 ctr->info.id6.server_role = 2;
770                                 break;
771                         default:
772                                 /*
773                                  * any other role is a primary
774                                  * of the domain, it controls.
775                                  */
776                                 ctr->info.id6.server_role = 3;
777                                 break; 
778                 }
779                 break;
780         default:
781                 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
782                 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
783                 break;
784         }
785
786         if (NT_STATUS_IS_OK(r_u->status)) {
787                 r_u->dom_ptr = 0x22000000; /* bizarre */
788                 ctr->info_class = q_u->info_class;
789         }
790
791         return r_u->status;
792 }
793
794 /***************************************************************************
795  _lsa_lookup_sids_internal
796  ***************************************************************************/
797
798 static NTSTATUS _lsa_lookup_sids_internal(pipes_struct *p,
799                                 uint16 level,                           /* input */
800                                 int num_sids,                           /* input */
801                                 const DOM_SID2 *sid,                    /* input */
802                                 DOM_R_REF **pp_ref,                     /* output */
803                                 LSA_TRANS_NAME_ENUM2 *names,            /* input/output */
804                                 uint32 *pp_mapped_count)
805 {
806         NTSTATUS status;
807         int i;
808         const DOM_SID **sids = NULL;
809         DOM_R_REF *ref = NULL;
810         uint32 mapped_count = 0;
811         struct lsa_dom_info *dom_infos = NULL;
812         struct lsa_name_info *name_infos = NULL;
813
814         *pp_mapped_count = 0;
815         *pp_ref = NULL;
816         ZERO_STRUCTP(names);
817
818         if (num_sids == 0) {
819                 return NT_STATUS_OK;
820         }
821
822         sids = TALLOC_ARRAY(p->mem_ctx, const DOM_SID *, num_sids);
823         ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
824
825         if (sids == NULL || ref == NULL) {
826                 return NT_STATUS_NO_MEMORY;
827         }
828
829         for (i=0; i<num_sids; i++) {
830                 sids[i] = &sid[i].sid;
831         }
832
833         status = lookup_sids(p->mem_ctx, num_sids, sids, level,
834                                   &dom_infos, &name_infos);
835
836         if (!NT_STATUS_IS_OK(status)) {
837                 return status;
838         }
839
840         names->name = TALLOC_ARRAY(p->mem_ctx, LSA_TRANS_NAME2, num_sids);
841         names->uni_name = TALLOC_ARRAY(p->mem_ctx, UNISTR2, num_sids);
842         if ((names->name == NULL) || (names->uni_name == NULL)) {
843                 return NT_STATUS_NO_MEMORY;
844         }
845
846         for (i=0; i<MAX_REF_DOMAINS; i++) {
847
848                 if (!dom_infos[i].valid) {
849                         break;
850                 }
851
852                 if (init_dom_ref(ref, dom_infos[i].name,
853                                  &dom_infos[i].sid) != i) {
854                         DEBUG(0, ("Domain %s mentioned twice??\n",
855                                   dom_infos[i].name));
856                         return NT_STATUS_INTERNAL_ERROR;
857                 }
858         }
859
860         for (i=0; i<num_sids; i++) {
861                 struct lsa_name_info *name = &name_infos[i];
862
863                 if (name->type == SID_NAME_UNKNOWN) {
864                         fstring tmp;
865                         name->dom_idx = -1;
866                         /* Unknown sids should return the string
867                          * representation of the SID. Windows 2003 behaves
868                          * rather erratic here, in many cases it returns the
869                          * RID as 8 bytes hex, in others it returns the full
870                          * SID. We (Jerry/VL) could not figure out which the
871                          * hard cases are, so leave it with the SID.  */
872                         name->name = talloc_asprintf(p->mem_ctx, "%s", 
873                                                      sid_to_string(tmp,
874                                                                    sids[i]));
875                         if (name->name == NULL) {
876                                 return NT_STATUS_NO_MEMORY;
877                         }
878                 } else {
879                         mapped_count += 1;
880                 }
881                 init_lsa_trans_name2(&names->name[i], &names->uni_name[i],
882                                     name->type, name->name, name->dom_idx);
883         }
884
885         names->num_entries = num_sids;
886         names->ptr_trans_names = 1;
887         names->num_entries2 = num_sids;
888
889         status = NT_STATUS_NONE_MAPPED;
890         if (mapped_count > 0) {
891                 status = (mapped_count < num_sids) ?
892                         STATUS_SOME_UNMAPPED : NT_STATUS_OK;
893         }
894
895         DEBUG(10, ("num_sids %d, mapped_count %d, status %s\n",
896                    num_sids, mapped_count, nt_errstr(status)));
897
898         *pp_mapped_count = mapped_count;
899         *pp_ref = ref;
900
901         return status;
902 }
903
904 /***************************************************************************
905  _lsa_lookup_sids
906  ***************************************************************************/
907
908 NTSTATUS _lsa_lookup_sids(pipes_struct *p,
909                           LSA_Q_LOOKUP_SIDS *q_u,
910                           LSA_R_LOOKUP_SIDS *r_u)
911 {
912         struct lsa_info *handle;
913         int num_sids = q_u->sids.num_entries;
914         uint32 mapped_count = 0;
915         DOM_R_REF *ref = NULL;
916         LSA_TRANS_NAME_ENUM2 names;
917         NTSTATUS status;
918
919         if ((q_u->level < 1) || (q_u->level > 6)) {
920                 return NT_STATUS_INVALID_PARAMETER;
921         }
922
923         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle)) {
924                 return NT_STATUS_INVALID_HANDLE;
925         }
926
927         /* check if the user has enough rights */
928         if (!(handle->access & POLICY_LOOKUP_NAMES)) {
929                 return NT_STATUS_ACCESS_DENIED;
930         }
931
932         if (num_sids >  MAX_LOOKUP_SIDS) {
933                 DEBUG(5,("_lsa_lookup_sids: limit of %d exceeded, requested %d\n",
934                          MAX_LOOKUP_SIDS, num_sids));
935                 return NT_STATUS_NONE_MAPPED;
936         }
937
938         r_u->status = _lsa_lookup_sids_internal(p,
939                                                 q_u->level,
940                                                 num_sids, 
941                                                 q_u->sids.sid,
942                                                 &ref,
943                                                 &names,
944                                                 &mapped_count);
945
946         /* Convert from LSA_TRANS_NAME_ENUM2 to LSA_TRANS_NAME_ENUM */
947
948         status = init_reply_lookup_sids(p->mem_ctx, r_u, ref, &names, mapped_count);
949         if (!NT_STATUS_IS_OK(status)) {
950                 return status;
951         }
952         return r_u->status;
953 }
954
955 /***************************************************************************
956  _lsa_lookup_sids2
957  ***************************************************************************/
958
959 NTSTATUS _lsa_lookup_sids2(pipes_struct *p,
960                           LSA_Q_LOOKUP_SIDS2 *q_u,
961                           LSA_R_LOOKUP_SIDS2 *r_u)
962 {
963         struct lsa_info *handle;
964         int num_sids = q_u->sids.num_entries;
965         uint32 mapped_count = 0;
966         DOM_R_REF *ref = NULL;
967
968         if ((q_u->level < 1) || (q_u->level > 6)) {
969                 return NT_STATUS_INVALID_PARAMETER;
970         }
971
972         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle)) {
973                 return NT_STATUS_INVALID_HANDLE;
974         }
975
976         /* check if the user have enough rights */
977         if (!(handle->access & POLICY_LOOKUP_NAMES)) {
978                 return NT_STATUS_ACCESS_DENIED;
979         }
980
981         if (num_sids >  MAX_LOOKUP_SIDS) {
982                 DEBUG(5,("_lsa_lookup_sids2: limit of %d exceeded, requested %d\n",
983                          MAX_LOOKUP_SIDS, num_sids));
984                 return NT_STATUS_NONE_MAPPED;
985         }
986
987         r_u->status = _lsa_lookup_sids_internal(p,
988                                                 q_u->level,
989                                                 num_sids, 
990                                                 q_u->sids.sid,
991                                                 &ref,
992                                                 &r_u->names,
993                                                 &mapped_count);
994
995         init_reply_lookup_sids2(r_u, ref, mapped_count);
996         return r_u->status;
997 }
998
999 /***************************************************************************
1000  _lsa_lookup_sida3
1001  ***************************************************************************/
1002
1003 NTSTATUS _lsa_lookup_sids3(pipes_struct *p,
1004                           LSA_Q_LOOKUP_SIDS3 *q_u,
1005                           LSA_R_LOOKUP_SIDS3 *r_u)
1006 {
1007         int num_sids = q_u->sids.num_entries;
1008         uint32 mapped_count = 0;
1009         DOM_R_REF *ref = NULL;
1010
1011         if ((q_u->level < 1) || (q_u->level > 6)) {
1012                 return NT_STATUS_INVALID_PARAMETER;
1013         }
1014
1015         /* No policy handle on this call. Restrict to crypto connections. */
1016         if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1017                 DEBUG(0,("_lsa_lookup_sids3: client %s not using schannel for netlogon\n",
1018                         get_remote_machine_name() ));
1019                 return NT_STATUS_INVALID_PARAMETER;
1020         }
1021
1022         if (num_sids >  MAX_LOOKUP_SIDS) {
1023                 DEBUG(5,("_lsa_lookup_sids3: limit of %d exceeded, requested %d\n",
1024                          MAX_LOOKUP_SIDS, num_sids));
1025                 return NT_STATUS_NONE_MAPPED;
1026         }
1027
1028         r_u->status = _lsa_lookup_sids_internal(p,
1029                                                 q_u->level,
1030                                                 num_sids, 
1031                                                 q_u->sids.sid,
1032                                                 &ref,
1033                                                 &r_u->names,
1034                                                 &mapped_count);
1035
1036         init_reply_lookup_sids3(r_u, ref, mapped_count);
1037         return r_u->status;
1038 }
1039
1040 /***************************************************************************
1041 lsa_reply_lookup_names
1042  ***************************************************************************/
1043
1044 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
1045 {
1046         struct lsa_info *handle;
1047         UNISTR2 *names = q_u->uni_name;
1048         uint32 num_entries = q_u->num_entries;
1049         DOM_R_REF *ref;
1050         DOM_RID *rids;
1051         uint32 mapped_count = 0;
1052         int flags = 0;
1053
1054         if (num_entries >  MAX_LOOKUP_SIDS) {
1055                 num_entries = MAX_LOOKUP_SIDS;
1056                 DEBUG(5,("_lsa_lookup_names: truncating name lookup list to %d\n", num_entries));
1057         }
1058                 
1059         /* Probably the lookup_level is some sort of bitmask. */
1060         if (q_u->lookup_level == 1) {
1061                 flags = LOOKUP_NAME_ALL;
1062         }
1063
1064         ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
1065         if (!ref) {
1066                 return NT_STATUS_NO_MEMORY;
1067         }
1068
1069         if (num_entries) {
1070                 rids = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_RID, num_entries);
1071                 if (!rids) {
1072                         return NT_STATUS_NO_MEMORY;
1073                 }
1074         } else {
1075                 rids = NULL;
1076         }
1077
1078         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle)) {
1079                 r_u->status = NT_STATUS_INVALID_HANDLE;
1080                 goto done;
1081         }
1082
1083         /* check if the user have enough rights */
1084         if (!(handle->access & POLICY_LOOKUP_NAMES)) {
1085                 r_u->status = NT_STATUS_ACCESS_DENIED;
1086                 goto done;
1087         }
1088
1089         /* set up the LSA Lookup RIDs response */
1090         become_root(); /* lookup_name can require root privs */
1091         r_u->status = lookup_lsa_rids(p->mem_ctx, ref, rids, num_entries,
1092                                       names, flags, &mapped_count);
1093         unbecome_root();
1094
1095 done:
1096
1097         if (NT_STATUS_IS_OK(r_u->status) && (num_entries != 0) ) {
1098                 if (mapped_count == 0)
1099                         r_u->status = NT_STATUS_NONE_MAPPED;
1100                 else if (mapped_count != num_entries)
1101                         r_u->status = STATUS_SOME_UNMAPPED;
1102         }
1103
1104         init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
1105         return r_u->status;
1106 }
1107
1108 /***************************************************************************
1109 lsa_reply_lookup_names2
1110  ***************************************************************************/
1111
1112 NTSTATUS _lsa_lookup_names2(pipes_struct *p, LSA_Q_LOOKUP_NAMES2 *q_u, LSA_R_LOOKUP_NAMES2 *r_u)
1113 {
1114         struct lsa_info *handle;
1115         UNISTR2 *names = q_u->uni_name;
1116         uint32 num_entries = q_u->num_entries;
1117         DOM_R_REF *ref;
1118         DOM_RID *rids;
1119         DOM_RID2 *rids2;
1120         int i;
1121         uint32 mapped_count = 0;
1122         int flags = 0;
1123
1124         if (num_entries >  MAX_LOOKUP_SIDS) {
1125                 num_entries = MAX_LOOKUP_SIDS;
1126                 DEBUG(5,("_lsa_lookup_names2: truncating name lookup list to %d\n", num_entries));
1127         }
1128                 
1129         /* Probably the lookup_level is some sort of bitmask. */
1130         if (q_u->lookup_level == 1) {
1131                 flags = LOOKUP_NAME_ALL;
1132         }
1133
1134         ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
1135         if (ref == NULL) {
1136                 r_u->status = NT_STATUS_NO_MEMORY;
1137                 return NT_STATUS_NO_MEMORY;
1138         }
1139
1140         if (num_entries) {
1141                 rids = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_RID, num_entries);
1142                 rids2 = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_RID2, num_entries);
1143                 if ((rids == NULL) || (rids2 == NULL)) {
1144                         r_u->status = NT_STATUS_NO_MEMORY;
1145                         return NT_STATUS_NO_MEMORY;
1146                 }
1147         } else {
1148                 rids = NULL;
1149                 rids2 = NULL;
1150         }
1151
1152         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle)) {
1153                 r_u->status = NT_STATUS_INVALID_HANDLE;
1154                 goto done;
1155         }
1156
1157         /* check if the user have enough rights */
1158         if (!(handle->access & POLICY_LOOKUP_NAMES)) {
1159                 r_u->status = NT_STATUS_ACCESS_DENIED;
1160                 goto done;
1161         }
1162
1163         /* set up the LSA Lookup RIDs response */
1164         become_root(); /* lookup_name can require root privs */
1165         r_u->status = lookup_lsa_rids(p->mem_ctx, ref, rids, num_entries,
1166                                       names, flags, &mapped_count);
1167         unbecome_root();
1168
1169 done:
1170
1171         if (NT_STATUS_IS_OK(r_u->status)) {
1172                 if (mapped_count == 0) {
1173                         r_u->status = NT_STATUS_NONE_MAPPED;
1174                 } else if (mapped_count != num_entries) {
1175                         r_u->status = STATUS_SOME_UNMAPPED;
1176                 }
1177         }
1178
1179         /* Convert the rids array to rids2. */
1180         for (i = 0; i < num_entries; i++) {
1181                 rids2[i].type = rids[i].type;
1182                 rids2[i].rid = rids[i].rid;
1183                 rids2[i].rid_idx = rids[i].rid_idx;
1184                 rids2[i].unknown = 0;
1185         }
1186
1187         init_reply_lookup_names2(r_u, ref, num_entries, rids2, mapped_count);
1188         return r_u->status;
1189 }
1190
1191 /***************************************************************************
1192 lsa_reply_lookup_names3.
1193  ***************************************************************************/
1194
1195 NTSTATUS _lsa_lookup_names3(pipes_struct *p, LSA_Q_LOOKUP_NAMES3 *q_u, LSA_R_LOOKUP_NAMES3 *r_u)
1196 {
1197         struct lsa_info *handle;
1198         UNISTR2 *names = q_u->uni_name;
1199         uint32 num_entries = q_u->num_entries;
1200         DOM_R_REF *ref = NULL;
1201         LSA_TRANSLATED_SID3 *trans_sids = NULL;
1202         uint32 mapped_count = 0;
1203         int flags = 0;
1204
1205         if (num_entries >  MAX_LOOKUP_SIDS) {
1206                 num_entries = MAX_LOOKUP_SIDS;
1207                 DEBUG(5,("_lsa_lookup_names3: truncating name lookup list to %d\n", num_entries));
1208         }
1209                 
1210         /* Probably the lookup_level is some sort of bitmask. */
1211         if (q_u->lookup_level == 1) {
1212                 flags = LOOKUP_NAME_ALL;
1213         }
1214
1215         ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
1216         if (ref == NULL) {
1217                 return NT_STATUS_NO_MEMORY;
1218         }
1219         if (num_entries) {
1220                 trans_sids = TALLOC_ZERO_ARRAY(p->mem_ctx, LSA_TRANSLATED_SID3, num_entries);
1221                 if (!trans_sids) {
1222                         return NT_STATUS_NO_MEMORY;
1223                 }
1224         } else {
1225                 trans_sids = NULL;
1226         }
1227
1228         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle)) {
1229                 r_u->status = NT_STATUS_INVALID_HANDLE;
1230                 goto done;
1231         }
1232
1233         /* check if the user have enough rights */
1234         if (!(handle->access & POLICY_LOOKUP_NAMES)) {
1235                 r_u->status = NT_STATUS_ACCESS_DENIED;
1236                 goto done;
1237         }
1238
1239         /* set up the LSA Lookup SIDs response */
1240         become_root(); /* lookup_name can require root privs */
1241         r_u->status = lookup_lsa_sids(p->mem_ctx, ref, trans_sids, num_entries,
1242                                       names, flags, &mapped_count);
1243         unbecome_root();
1244
1245 done:
1246
1247         if (NT_STATUS_IS_OK(r_u->status)) {
1248                 if (mapped_count == 0) {
1249                         r_u->status = NT_STATUS_NONE_MAPPED;
1250                 } else if (mapped_count != num_entries) {
1251                         r_u->status = STATUS_SOME_UNMAPPED;
1252                 }
1253         }
1254
1255         init_reply_lookup_names3(r_u, ref, num_entries, trans_sids, mapped_count);
1256         return r_u->status;
1257 }
1258
1259 /***************************************************************************
1260 lsa_reply_lookup_names4.
1261  ***************************************************************************/
1262
1263 NTSTATUS _lsa_lookup_names4(pipes_struct *p, LSA_Q_LOOKUP_NAMES4 *q_u, LSA_R_LOOKUP_NAMES4 *r_u)
1264 {
1265         UNISTR2 *names = q_u->uni_name;
1266         uint32 num_entries = q_u->num_entries;
1267         DOM_R_REF *ref = NULL;
1268         LSA_TRANSLATED_SID3 *trans_sids = NULL;
1269         uint32 mapped_count = 0;
1270         int flags = 0;
1271
1272         if (num_entries >  MAX_LOOKUP_SIDS) {
1273                 num_entries = MAX_LOOKUP_SIDS;
1274                 DEBUG(5,("_lsa_lookup_names4: truncating name lookup list to %d\n", num_entries));
1275         }
1276                 
1277         /* Probably the lookup_level is some sort of bitmask. */
1278         if (q_u->lookup_level == 1) {
1279                 flags = LOOKUP_NAME_ALL;
1280         }
1281
1282         /* No policy handle on this call. Restrict to crypto connections. */
1283         if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1284                 DEBUG(0,("_lsa_lookup_names4: client %s not using schannel for netlogon\n",
1285                         get_remote_machine_name() ));
1286                 return NT_STATUS_INVALID_PARAMETER;
1287         }
1288
1289         ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
1290         if (!ref) {
1291                 return NT_STATUS_NO_MEMORY;
1292         }
1293
1294         if (num_entries) {
1295                 trans_sids = TALLOC_ZERO_ARRAY(p->mem_ctx, LSA_TRANSLATED_SID3, num_entries);
1296                 if (!trans_sids) {
1297                         return NT_STATUS_NO_MEMORY;
1298                 }
1299         } else {
1300                 trans_sids = NULL;
1301         }
1302
1303         /* set up the LSA Lookup SIDs response */
1304         become_root(); /* lookup_name can require root privs */
1305         r_u->status = lookup_lsa_sids(p->mem_ctx, ref, trans_sids, num_entries,
1306                                       names, flags, &mapped_count);
1307         unbecome_root();
1308
1309         if (NT_STATUS_IS_OK(r_u->status)) {
1310                 if (mapped_count == 0) {
1311                         r_u->status = NT_STATUS_NONE_MAPPED;
1312                 } else if (mapped_count != num_entries) {
1313                         r_u->status = STATUS_SOME_UNMAPPED;
1314                 }
1315         }
1316
1317         init_reply_lookup_names4(r_u, ref, num_entries, trans_sids, mapped_count);
1318         return r_u->status;
1319 }
1320
1321 /***************************************************************************
1322  _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
1323  ***************************************************************************/
1324
1325 NTSTATUS _lsa_Close(pipes_struct *p, struct lsa_Close *r)
1326 {
1327         if (!find_policy_by_hnd(p, r->in.handle, NULL)) {
1328                 return NT_STATUS_INVALID_HANDLE;
1329         }
1330
1331         close_policy_hnd(p, r->in.handle);
1332         return NT_STATUS_OK;
1333 }
1334
1335 /***************************************************************************
1336  ***************************************************************************/
1337
1338 NTSTATUS _lsa_OpenSecret(pipes_struct *p, struct lsa_OpenSecret *r)
1339 {
1340         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1341 }
1342
1343 /***************************************************************************
1344  ***************************************************************************/
1345
1346 NTSTATUS _lsa_OpenTrustedDomain(pipes_struct *p, struct lsa_OpenTrustedDomain *r)
1347 {
1348         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1349 }
1350
1351 /***************************************************************************
1352  ***************************************************************************/
1353
1354 NTSTATUS _lsa_CreateTrustedDomain(pipes_struct *p, struct lsa_CreateTrustedDomain *r)
1355 {
1356         return NT_STATUS_ACCESS_DENIED;
1357 }
1358
1359 /***************************************************************************
1360  ***************************************************************************/
1361
1362 NTSTATUS _lsa_CreateSecret(pipes_struct *p, struct lsa_CreateSecret *r)
1363 {
1364         return NT_STATUS_ACCESS_DENIED;
1365 }
1366
1367 /***************************************************************************
1368  ***************************************************************************/
1369
1370 NTSTATUS _lsa_SetSecret(pipes_struct *p, struct lsa_SetSecret *r)
1371 {
1372         return NT_STATUS_ACCESS_DENIED;
1373 }
1374
1375 /***************************************************************************
1376  ***************************************************************************/
1377
1378 NTSTATUS _lsa_delete_object(pipes_struct *p, LSA_Q_DELETE_OBJECT *q_u, LSA_R_DELETE_OBJECT *r_u)
1379 {
1380         return NT_STATUS_ACCESS_DENIED;
1381 }
1382
1383 /***************************************************************************
1384 _lsa_enum_privs.
1385  ***************************************************************************/
1386
1387 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
1388 {
1389         struct lsa_info *handle;
1390         uint32 i;
1391         uint32 enum_context = q_u->enum_context;
1392         int num_privs = count_all_privileges();
1393         LSA_PRIV_ENTRY *entries = NULL;
1394         LUID_ATTR luid;
1395
1396         /* remember that the enum_context starts at 0 and not 1 */
1397
1398         if ( enum_context >= num_privs )
1399                 return NT_STATUS_NO_MORE_ENTRIES;
1400                 
1401         DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", 
1402                 enum_context, num_privs));
1403         
1404         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
1405                 return NT_STATUS_INVALID_HANDLE;
1406
1407         /* check if the user have enough rights
1408            I don't know if it's the right one. not documented.  */
1409
1410         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1411                 return NT_STATUS_ACCESS_DENIED;
1412
1413         if (num_privs) {
1414                 if ( !(entries = TALLOC_ZERO_ARRAY(p->mem_ctx, LSA_PRIV_ENTRY, num_privs )) )
1415                         return NT_STATUS_NO_MEMORY;
1416         } else {
1417                 entries = NULL;
1418         }
1419
1420         for (i = 0; i < num_privs; i++) {
1421                 if( i < enum_context) {
1422                         init_unistr2(&entries[i].name, NULL, UNI_FLAGS_NONE);
1423                         init_uni_hdr(&entries[i].hdr_name, &entries[i].name);
1424                         
1425                         entries[i].luid_low = 0;
1426                         entries[i].luid_high = 0;
1427                 } else {
1428                         init_unistr2(&entries[i].name, privs[i].name, UNI_FLAGS_NONE);
1429                         init_uni_hdr(&entries[i].hdr_name, &entries[i].name);
1430                         
1431                         luid = get_privilege_luid( &privs[i].se_priv );
1432                         
1433                         entries[i].luid_low = luid.luid.low;
1434                         entries[i].luid_high = luid.luid.high;
1435                 }
1436         }
1437
1438         enum_context = num_privs;
1439         
1440         init_lsa_r_enum_privs(r_u, enum_context, num_privs, entries);
1441
1442         return NT_STATUS_OK;
1443 }
1444
1445 /***************************************************************************
1446 _lsa_priv_get_dispname.
1447  ***************************************************************************/
1448
1449 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
1450 {
1451         struct lsa_info *handle;
1452         fstring name_asc;
1453         const char *description;
1454
1455         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
1456                 return NT_STATUS_INVALID_HANDLE;
1457
1458         /* check if the user have enough rights */
1459
1460         /*
1461          * I don't know if it's the right one. not documented.
1462          */
1463         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1464                 return NT_STATUS_ACCESS_DENIED;
1465
1466         unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
1467
1468         DEBUG(10,("_lsa_priv_get_dispname: name = %s\n", name_asc));
1469
1470         description = get_privilege_dispname( name_asc );
1471         
1472         if ( description ) {
1473                 DEBUG(10,("_lsa_priv_get_dispname: display name = %s\n", description));
1474                 
1475                 init_unistr2(&r_u->desc, description, UNI_FLAGS_NONE);
1476                 init_uni_hdr(&r_u->hdr_desc, &r_u->desc);
1477
1478                 r_u->ptr_info = 0xdeadbeef;
1479                 r_u->lang_id = q_u->lang_id;
1480                 
1481                 return NT_STATUS_OK;
1482         } else {
1483                 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
1484                 
1485                 r_u->ptr_info = 0;
1486                 
1487                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1488         }
1489 }
1490
1491 /***************************************************************************
1492 _lsa_enum_accounts.
1493  ***************************************************************************/
1494
1495 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
1496 {
1497         struct lsa_info *handle;
1498         DOM_SID *sid_list;
1499         int i, j, num_entries;
1500         LSA_SID_ENUM *sids=&r_u->sids;
1501         NTSTATUS ret;
1502
1503         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
1504                 return NT_STATUS_INVALID_HANDLE;
1505
1506         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1507                 return NT_STATUS_ACCESS_DENIED;
1508
1509         sid_list = NULL;
1510         num_entries = 0;
1511
1512         /* The only way we can currently find out all the SIDs that have been
1513            privileged is to scan all privileges */
1514
1515         if (!NT_STATUS_IS_OK(ret = privilege_enumerate_accounts(&sid_list, &num_entries))) {
1516                 return ret;
1517         }
1518
1519         if (q_u->enum_context >= num_entries)
1520                 return NT_STATUS_NO_MORE_ENTRIES;
1521
1522         if (num_entries-q_u->enum_context) {
1523                 sids->ptr_sid = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_entries-q_u->enum_context);
1524                 sids->sid = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_SID2, num_entries-q_u->enum_context);
1525
1526                 if (sids->ptr_sid==NULL || sids->sid==NULL) {
1527                         SAFE_FREE(sid_list);
1528                         return NT_STATUS_NO_MEMORY;
1529                 }
1530
1531                 for (i = q_u->enum_context, j = 0; i < num_entries; i++, j++) {
1532                         init_dom_sid2(&(*sids).sid[j], &sid_list[i]);
1533                         (*sids).ptr_sid[j] = 1;
1534                 }
1535         } else {
1536                 sids->ptr_sid = NULL;
1537                 sids->sid = NULL;
1538         }
1539
1540         talloc_free(sid_list);
1541
1542         init_lsa_r_enum_accounts(r_u, num_entries);
1543
1544         return NT_STATUS_OK;
1545 }
1546
1547
1548 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
1549 {
1550         const char *username, *domname;
1551         user_struct *vuser = get_valid_user_struct(p->vuid);
1552   
1553         if (vuser == NULL)
1554                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1555
1556         if (vuser->guest) {
1557                 /*
1558                  * I'm 99% sure this is not the right place to do this,
1559                  * global_sid_Anonymous should probably be put into the token
1560                  * instead of the guest id -- vl
1561                  */
1562                 if (!lookup_sid(p->mem_ctx, &global_sid_Anonymous,
1563                                 &domname, &username, NULL)) {
1564                         return NT_STATUS_NO_MEMORY;
1565                 }
1566         } else {
1567                 username = vuser->user.smb_name;
1568                 domname = vuser->user.domain;
1569         }
1570   
1571         r_u->ptr_user_name = 1;
1572         init_unistr2(&r_u->uni2_user_name, username, UNI_STR_TERMINATE);
1573         init_uni_hdr(&r_u->hdr_user_name, &r_u->uni2_user_name);
1574
1575         r_u->unk1 = 1;
1576   
1577         r_u->ptr_dom_name = 1;
1578         init_unistr2(&r_u->uni2_dom_name, domname,  UNI_STR_TERMINATE);
1579         init_uni_hdr(&r_u->hdr_dom_name, &r_u->uni2_dom_name);
1580
1581         r_u->status = NT_STATUS_OK;
1582   
1583         return r_u->status;
1584 }
1585
1586 /***************************************************************************
1587  Lsa Create Account 
1588  ***************************************************************************/
1589
1590 NTSTATUS _lsa_create_account(pipes_struct *p, LSA_Q_CREATEACCOUNT *q_u, LSA_R_CREATEACCOUNT *r_u)
1591 {
1592         struct lsa_info *handle;
1593         struct lsa_info *info;
1594
1595         /* find the connection policy handle. */
1596         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
1597                 return NT_STATUS_INVALID_HANDLE;
1598
1599         /* check if the user have enough rights */
1600
1601         /*
1602          * I don't know if it's the right one. not documented.
1603          * but guessed with rpcclient.
1604          */
1605         if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
1606                 return NT_STATUS_ACCESS_DENIED;
1607
1608         /* check to see if the pipe_user is a Domain Admin since 
1609            account_pol.tdb was already opened as root, this is all we have */
1610            
1611         if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1612                 return NT_STATUS_ACCESS_DENIED;
1613                 
1614         if ( is_privileged_sid( &q_u->sid.sid ) )
1615                 return NT_STATUS_OBJECT_NAME_COLLISION;
1616
1617         /* associate the user/group SID with the (unique) handle. */
1618         
1619         if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
1620                 return NT_STATUS_NO_MEMORY;
1621
1622         ZERO_STRUCTP(info);
1623         info->sid = q_u->sid.sid;
1624         info->access = q_u->access;
1625
1626         /* get a (unique) handle.  open a policy on it. */
1627         if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
1628                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1629
1630         return privilege_create_account( &info->sid );
1631 }
1632
1633
1634 /***************************************************************************
1635  Lsa Open Account
1636  ***************************************************************************/
1637
1638 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
1639 {
1640         struct lsa_info *handle;
1641         struct lsa_info *info;
1642
1643         /* find the connection policy handle. */
1644         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
1645                 return NT_STATUS_INVALID_HANDLE;
1646
1647         /* check if the user have enough rights */
1648
1649         /*
1650          * I don't know if it's the right one. not documented.
1651          * but guessed with rpcclient.
1652          */
1653         if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
1654                 return NT_STATUS_ACCESS_DENIED;
1655
1656         /* TODO: Fis the parsing routine before reenabling this check! */
1657         #if 0
1658         if (!lookup_sid(&handle->sid, dom_name, name, &type))
1659                 return NT_STATUS_ACCESS_DENIED;
1660         #endif
1661         /* associate the user/group SID with the (unique) handle. */
1662         if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
1663                 return NT_STATUS_NO_MEMORY;
1664
1665         ZERO_STRUCTP(info);
1666         info->sid = q_u->sid.sid;
1667         info->access = q_u->access;
1668
1669         /* get a (unique) handle.  open a policy on it. */
1670         if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
1671                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1672
1673         return NT_STATUS_OK;
1674 }
1675
1676 /***************************************************************************
1677  For a given SID, enumerate all the privilege this account has.
1678  ***************************************************************************/
1679
1680 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, prs_struct *ps, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
1681 {
1682         struct lsa_info *info=NULL;
1683         SE_PRIV mask;
1684         PRIVILEGE_SET privileges;
1685
1686         /* find the connection policy handle. */
1687         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1688                 return NT_STATUS_INVALID_HANDLE;
1689
1690         if ( !get_privileges_for_sids( &mask, &info->sid, 1 ) ) 
1691                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1692
1693         privilege_set_init( &privileges );
1694
1695         if ( se_priv_to_privilege_set( &privileges, &mask ) ) {
1696
1697                 DEBUG(10,("_lsa_enum_privsaccount: %s has %d privileges\n",
1698                           sid_string_dbg(&info->sid),
1699                           privileges.count));
1700
1701                 r_u->status = init_lsa_r_enum_privsaccount(ps->mem_ctx, r_u, privileges.set, privileges.count, 0);
1702         }
1703         else
1704                 r_u->status = NT_STATUS_NO_SUCH_PRIVILEGE;
1705
1706         privilege_set_free( &privileges );
1707
1708         return r_u->status;
1709 }
1710
1711 /***************************************************************************
1712  
1713  ***************************************************************************/
1714
1715 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
1716 {
1717         struct lsa_info *info=NULL;
1718
1719         /* find the connection policy handle. */
1720
1721         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1722                 return NT_STATUS_INVALID_HANDLE;
1723
1724         if (!lookup_sid(p->mem_ctx, &info->sid, NULL, NULL, NULL))
1725                 return NT_STATUS_ACCESS_DENIED;
1726
1727         /*
1728           0x01 -> Log on locally
1729           0x02 -> Access this computer from network
1730           0x04 -> Log on as a batch job
1731           0x10 -> Log on as a service
1732           
1733           they can be ORed together
1734         */
1735
1736         r_u->access = PR_LOG_ON_LOCALLY | PR_ACCESS_FROM_NETWORK;
1737
1738         return NT_STATUS_OK;
1739 }
1740
1741 /***************************************************************************
1742   update the systemaccount information
1743  ***************************************************************************/
1744
1745 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
1746 {
1747         struct lsa_info *info=NULL;
1748         GROUP_MAP map;
1749         r_u->status = NT_STATUS_OK;
1750
1751         /* find the connection policy handle. */
1752         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1753                 return NT_STATUS_INVALID_HANDLE;
1754
1755         /* check to see if the pipe_user is a Domain Admin since 
1756            account_pol.tdb was already opened as root, this is all we have */
1757            
1758         if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1759                 return NT_STATUS_ACCESS_DENIED;
1760
1761         if (!pdb_getgrsid(&map, info->sid))
1762                 return NT_STATUS_NO_SUCH_GROUP;
1763
1764         return pdb_update_group_mapping_entry(&map);
1765 }
1766
1767 /***************************************************************************
1768  For a given SID, add some privileges.
1769  ***************************************************************************/
1770
1771 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
1772 {
1773         struct lsa_info *info = NULL;
1774         SE_PRIV mask;
1775         PRIVILEGE_SET *set = NULL;
1776
1777         /* find the connection policy handle. */
1778         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1779                 return NT_STATUS_INVALID_HANDLE;
1780                 
1781         /* check to see if the pipe_user is root or a Domain Admin since 
1782            account_pol.tdb was already opened as root, this is all we have */
1783            
1784         if ( p->pipe_user.ut.uid != sec_initial_uid() 
1785                 && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1786         {
1787                 return NT_STATUS_ACCESS_DENIED;
1788         }
1789
1790         set = &q_u->set;
1791
1792         if ( !privilege_set_to_se_priv( &mask, set ) )
1793                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1794
1795         if ( !grant_privilege( &info->sid, &mask ) ) {
1796                 DEBUG(3,("_lsa_addprivs: grant_privilege(%s) failed!\n",
1797                          sid_string_dbg(&info->sid) ));
1798                 DEBUG(3,("Privilege mask:\n"));
1799                 dump_se_priv( DBGC_ALL, 3, &mask );
1800                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1801         }
1802
1803         return NT_STATUS_OK;
1804 }
1805
1806 /***************************************************************************
1807  For a given SID, remove some privileges.
1808  ***************************************************************************/
1809
1810 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1811 {
1812         struct lsa_info *info = NULL;
1813         SE_PRIV mask;
1814         PRIVILEGE_SET *set = NULL;
1815
1816         /* find the connection policy handle. */
1817         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1818                 return NT_STATUS_INVALID_HANDLE;
1819
1820         /* check to see if the pipe_user is root or a Domain Admin since 
1821            account_pol.tdb was already opened as root, this is all we have */
1822            
1823         if ( p->pipe_user.ut.uid != sec_initial_uid()
1824                 && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) ) 
1825         {
1826                 return NT_STATUS_ACCESS_DENIED;
1827         }
1828
1829         set = &q_u->set;
1830
1831         if ( !privilege_set_to_se_priv( &mask, set ) )
1832                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1833
1834         if ( !revoke_privilege( &info->sid, &mask ) ) {
1835                 DEBUG(3,("_lsa_removeprivs: revoke_privilege(%s) failed!\n",
1836                          sid_string_dbg(&info->sid) ));
1837                 DEBUG(3,("Privilege mask:\n"));
1838                 dump_se_priv( DBGC_ALL, 3, &mask );
1839                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1840         }
1841
1842         return NT_STATUS_OK;
1843 }
1844
1845 /***************************************************************************
1846  For a given SID, remove some privileges.
1847  ***************************************************************************/
1848
1849 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1850 {
1851         struct lsa_info *handle=NULL;
1852         SEC_DESC *psd = NULL;
1853         size_t sd_size;
1854         NTSTATUS status;
1855
1856         r_u->status = NT_STATUS_OK;
1857
1858         /* find the connection policy handle. */
1859         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
1860                 return NT_STATUS_INVALID_HANDLE;
1861
1862         /* check if the user have enough rights */
1863         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1864                 return NT_STATUS_ACCESS_DENIED;
1865
1866
1867         switch (q_u->sec_info) {
1868         case 1:
1869                 /* SD contains only the owner */
1870
1871                 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1872                 if(!NT_STATUS_IS_OK(status))
1873                         return NT_STATUS_NO_MEMORY;
1874
1875
1876                 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1877                         return NT_STATUS_NO_MEMORY;
1878                 break;
1879         case 4:
1880                 /* SD contains only the ACL */
1881
1882                 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1883                 if(!NT_STATUS_IS_OK(status))
1884                         return NT_STATUS_NO_MEMORY;
1885
1886                 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1887                         return NT_STATUS_NO_MEMORY;
1888                 break;
1889         default:
1890                 return NT_STATUS_INVALID_LEVEL;
1891         }
1892
1893         r_u->ptr=1;
1894
1895         return r_u->status;
1896 }
1897
1898 #if 0   /* AD DC work in ongoing in Samba 4 */
1899
1900 /***************************************************************************
1901  ***************************************************************************/
1902
1903 NTSTATUS _lsa_query_info2(pipes_struct *p, LSA_Q_QUERY_INFO2 *q_u, LSA_R_QUERY_INFO2 *r_u)
1904 {
1905         struct lsa_info *handle;
1906         const char *nb_name;
1907         char *dns_name = NULL;
1908         char *forest_name = NULL;
1909         DOM_SID *sid = NULL;
1910         struct GUID guid;
1911         fstring dnsdomname;
1912
1913         ZERO_STRUCT(guid);
1914         r_u->status = NT_STATUS_OK;
1915
1916         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
1917                 return NT_STATUS_INVALID_HANDLE;
1918
1919         switch (q_u->info_class) {
1920         case 0x0c:
1921                 /* check if the user have enough rights */
1922                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1923                         return NT_STATUS_ACCESS_DENIED;
1924
1925                 /* Request PolicyPrimaryDomainInformation. */
1926                 switch (lp_server_role()) {
1927                         case ROLE_DOMAIN_PDC:
1928                         case ROLE_DOMAIN_BDC:
1929                                 nb_name = get_global_sam_name();
1930                                 /* ugly temp hack for these next two */
1931
1932                                 /* This should be a 'netbios domain -> DNS domain' mapping */
1933                                 dnsdomname = get_mydnsdomname(p->mem_ctx);
1934                                 if (!dnsdomname || !*dnsdomname) {
1935                                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1936                                 }
1937                                 strlower_m(dnsdomname);
1938
1939                                 dns_name = dnsdomname;
1940                                 forest_name = dnsdomname;
1941
1942                                 sid = get_global_sam_sid();
1943                                 secrets_fetch_domain_guid(lp_workgroup(), &guid);
1944                                 break;
1945                         default:
1946                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1947                 }
1948                 init_dns_dom_info(&r_u->info.dns_dom_info, nb_name, dns_name, 
1949                                   forest_name,&guid,sid);
1950                 break;
1951         default:
1952                 DEBUG(0,("_lsa_query_info2: unknown info level in Lsa Query: %d\n", q_u->info_class));
1953                 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
1954                 break;
1955         }
1956
1957         if (NT_STATUS_IS_OK(r_u->status)) {
1958                 r_u->ptr = 0x1;
1959                 r_u->info_class = q_u->info_class;
1960         }
1961
1962         return r_u->status;
1963 }
1964 #endif  /* AD DC work in ongoing in Samba 4 */
1965
1966 /***************************************************************************
1967  ***************************************************************************/
1968
1969 NTSTATUS _lsa_add_acct_rights(pipes_struct *p, LSA_Q_ADD_ACCT_RIGHTS *q_u, LSA_R_ADD_ACCT_RIGHTS *r_u)
1970 {
1971         struct lsa_info *info = NULL;
1972         int i = 0;
1973         DOM_SID sid;
1974         fstring privname;
1975         UNISTR4_ARRAY *uni_privnames = q_u->rights;
1976         
1977
1978         /* find the connection policy handle. */
1979         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
1980                 return NT_STATUS_INVALID_HANDLE;
1981                 
1982         /* check to see if the pipe_user is a Domain Admin since 
1983            account_pol.tdb was already opened as root, this is all we have */
1984            
1985         if ( p->pipe_user.ut.uid != sec_initial_uid()
1986                 && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) ) 
1987         {
1988                 return NT_STATUS_ACCESS_DENIED;
1989         }
1990
1991         /* according to an NT4 PDC, you can add privileges to SIDs even without
1992            call_lsa_create_account() first.  And you can use any arbitrary SID. */
1993            
1994         sid_copy( &sid, &q_u->sid.sid );
1995         
1996         /* just a little sanity check */
1997         
1998         if ( q_u->count != uni_privnames->count ) {
1999                 DEBUG(0,("_lsa_add_acct_rights: count != number of UNISTR2 elements!\n"));
2000                 return NT_STATUS_INVALID_HANDLE;        
2001         }
2002                 
2003         for ( i=0; i<q_u->count; i++ ) {
2004                 UNISTR4 *uni4_str = &uni_privnames->strings[i];
2005
2006                 /* only try to add non-null strings */
2007
2008                 if ( !uni4_str->string )
2009                         continue;
2010
2011                 rpcstr_pull( privname, uni4_str->string->buffer, sizeof(privname), -1, STR_TERMINATE );
2012                 
2013                 if ( !grant_privilege_by_name( &sid, privname ) ) {
2014                         DEBUG(2,("_lsa_add_acct_rights: Failed to add privilege [%s]\n", privname ));
2015                         return NT_STATUS_NO_SUCH_PRIVILEGE;
2016                 }
2017         }
2018
2019         return NT_STATUS_OK;
2020 }
2021
2022 /***************************************************************************
2023  ***************************************************************************/
2024
2025 NTSTATUS _lsa_remove_acct_rights(pipes_struct *p, LSA_Q_REMOVE_ACCT_RIGHTS *q_u, LSA_R_REMOVE_ACCT_RIGHTS *r_u)
2026 {
2027         struct lsa_info *info = NULL;
2028         int i = 0;
2029         DOM_SID sid;
2030         fstring privname;
2031         UNISTR4_ARRAY *uni_privnames = q_u->rights;
2032         
2033
2034         /* find the connection policy handle. */
2035         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
2036                 return NT_STATUS_INVALID_HANDLE;
2037                 
2038         /* check to see if the pipe_user is a Domain Admin since 
2039            account_pol.tdb was already opened as root, this is all we have */
2040            
2041         if ( p->pipe_user.ut.uid != sec_initial_uid()
2042                 && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
2043         {
2044                 return NT_STATUS_ACCESS_DENIED;
2045         }
2046
2047         sid_copy( &sid, &q_u->sid.sid );
2048
2049         if ( q_u->removeall ) {
2050                 if ( !revoke_all_privileges( &sid ) ) 
2051                         return NT_STATUS_ACCESS_DENIED;
2052         
2053                 return NT_STATUS_OK;
2054         }
2055         
2056         /* just a little sanity check */
2057         
2058         if ( q_u->count != uni_privnames->count ) {
2059                 DEBUG(0,("_lsa_add_acct_rights: count != number of UNISTR2 elements!\n"));
2060                 return NT_STATUS_INVALID_HANDLE;        
2061         }
2062                 
2063         for ( i=0; i<q_u->count; i++ ) {
2064                 UNISTR4 *uni4_str = &uni_privnames->strings[i];
2065
2066                 /* only try to add non-null strings */
2067
2068                 if ( !uni4_str->string )
2069                         continue;
2070
2071                 rpcstr_pull( privname, uni4_str->string->buffer, sizeof(privname), -1, STR_TERMINATE );
2072                 
2073                 if ( !revoke_privilege_by_name( &sid, privname ) ) {
2074                         DEBUG(2,("_lsa_remove_acct_rights: Failed to revoke privilege [%s]\n", privname ));
2075                         return NT_STATUS_NO_SUCH_PRIVILEGE;
2076                 }
2077         }
2078
2079         return NT_STATUS_OK;
2080 }
2081
2082
2083 /***************************************************************************
2084  ***************************************************************************/
2085
2086 NTSTATUS _lsa_enum_acct_rights(pipes_struct *p, LSA_Q_ENUM_ACCT_RIGHTS *q_u, LSA_R_ENUM_ACCT_RIGHTS *r_u)
2087 {
2088         struct lsa_info *info = NULL;
2089         DOM_SID sid;
2090         PRIVILEGE_SET privileges;
2091         SE_PRIV mask;
2092         
2093
2094         /* find the connection policy handle. */
2095         
2096         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
2097                 return NT_STATUS_INVALID_HANDLE;
2098                 
2099         /* according to an NT4 PDC, you can add privileges to SIDs even without
2100            call_lsa_create_account() first.  And you can use any arbitrary SID. */
2101            
2102         sid_copy( &sid, &q_u->sid.sid );
2103         
2104         if ( !get_privileges_for_sids( &mask, &sid, 1 ) )
2105                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2106
2107         privilege_set_init( &privileges );
2108
2109         if ( se_priv_to_privilege_set( &privileges, &mask ) ) {
2110
2111                 DEBUG(10,("_lsa_enum_acct_rights: %s has %d privileges\n",
2112                           sid_string_dbg(&sid), privileges.count));
2113
2114                 r_u->status = init_r_enum_acct_rights( r_u, &privileges );
2115         }
2116         else 
2117                 r_u->status = NT_STATUS_NO_SUCH_PRIVILEGE;
2118
2119         privilege_set_free( &privileges );
2120
2121         return r_u->status;
2122 }
2123
2124
2125 /***************************************************************************
2126  ***************************************************************************/
2127
2128 NTSTATUS _lsa_lookup_priv_value(pipes_struct *p, LSA_Q_LOOKUP_PRIV_VALUE *q_u, LSA_R_LOOKUP_PRIV_VALUE *r_u)
2129 {
2130         struct lsa_info *info = NULL;
2131         fstring name;
2132         LUID_ATTR priv_luid;
2133         SE_PRIV mask;
2134         
2135         /* find the connection policy handle. */
2136         
2137         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&info))
2138                 return NT_STATUS_INVALID_HANDLE;
2139                 
2140         unistr2_to_ascii(name, &q_u->privname.unistring, sizeof(name));
2141         
2142         DEBUG(10,("_lsa_lookup_priv_value: name = %s\n", name));
2143
2144         if ( !se_priv_from_name( name, &mask ) )
2145                 return NT_STATUS_NO_SUCH_PRIVILEGE;
2146
2147         priv_luid = get_privilege_luid( &mask );
2148
2149         r_u->luid.low  = priv_luid.luid.low;
2150         r_u->luid.high = priv_luid.luid.high;
2151                 
2152
2153         return NT_STATUS_OK;
2154 }
2155
2156
2157 /*
2158  * From here on the server routines are just dummy ones to make smbd link with
2159  * librpc/gen_ndr/srv_lsa.c. These routines are actually never called, we are
2160  * pulling the server stubs across one by one.
2161  */ 
2162
2163 NTSTATUS _lsa_Delete(pipes_struct *p, struct lsa_Delete *r)
2164 {
2165         p->rng_fault_state = True;
2166         return NT_STATUS_NOT_IMPLEMENTED;
2167 }
2168
2169 NTSTATUS _lsa_EnumPrivs(pipes_struct *p, struct lsa_EnumPrivs *r)
2170 {
2171         p->rng_fault_state = True;
2172         return NT_STATUS_NOT_IMPLEMENTED;
2173 }
2174
2175 NTSTATUS _lsa_QuerySecurity(pipes_struct *p, struct lsa_QuerySecurity *r)
2176 {
2177         p->rng_fault_state = True;
2178         return NT_STATUS_NOT_IMPLEMENTED;
2179 }
2180
2181 NTSTATUS _lsa_SetSecObj(pipes_struct *p, struct lsa_SetSecObj *r)
2182 {
2183         p->rng_fault_state = True;
2184         return NT_STATUS_NOT_IMPLEMENTED;
2185 }
2186
2187 NTSTATUS _lsa_ChangePassword(pipes_struct *p, struct lsa_ChangePassword *r)
2188 {
2189         p->rng_fault_state = True;
2190         return NT_STATUS_NOT_IMPLEMENTED;
2191 }
2192
2193 NTSTATUS _lsa_OpenPolicy(pipes_struct *p, struct lsa_OpenPolicy *r)
2194 {
2195         p->rng_fault_state = True;
2196         return NT_STATUS_NOT_IMPLEMENTED;
2197 }
2198
2199 NTSTATUS _lsa_QueryInfoPolicy(pipes_struct *p, struct lsa_QueryInfoPolicy *r)
2200 {
2201         p->rng_fault_state = True;
2202         return NT_STATUS_NOT_IMPLEMENTED;
2203 }
2204
2205 NTSTATUS _lsa_SetInfoPolicy(pipes_struct *p, struct lsa_SetInfoPolicy *r)
2206 {
2207         p->rng_fault_state = True;
2208         return NT_STATUS_NOT_IMPLEMENTED;
2209 }
2210
2211 NTSTATUS _lsa_ClearAuditLog(pipes_struct *p, struct lsa_ClearAuditLog *r)
2212 {
2213         p->rng_fault_state = True;
2214         return NT_STATUS_NOT_IMPLEMENTED;
2215 }
2216
2217 NTSTATUS _lsa_CreateAccount(pipes_struct *p, struct lsa_CreateAccount *r)
2218 {
2219         p->rng_fault_state = True;
2220         return NT_STATUS_NOT_IMPLEMENTED;
2221 }
2222
2223 NTSTATUS _lsa_EnumAccounts(pipes_struct *p, struct lsa_EnumAccounts *r)
2224 {
2225         p->rng_fault_state = True;
2226         return NT_STATUS_NOT_IMPLEMENTED;
2227 }
2228
2229 NTSTATUS _lsa_EnumTrustDom(pipes_struct *p, struct lsa_EnumTrustDom *r)
2230 {
2231         p->rng_fault_state = True;
2232         return NT_STATUS_NOT_IMPLEMENTED;
2233 }
2234
2235 NTSTATUS _lsa_LookupNames(pipes_struct *p, struct lsa_LookupNames *r)
2236 {
2237         p->rng_fault_state = True;
2238         return NT_STATUS_NOT_IMPLEMENTED;
2239 }
2240
2241 NTSTATUS _lsa_LookupSids(pipes_struct *p, struct lsa_LookupSids *r)
2242 {
2243         p->rng_fault_state = True;
2244         return NT_STATUS_NOT_IMPLEMENTED;
2245 }
2246
2247 NTSTATUS _lsa_OpenAccount(pipes_struct *p, struct lsa_OpenAccount *r)
2248 {
2249         p->rng_fault_state = True;
2250         return NT_STATUS_NOT_IMPLEMENTED;
2251 }
2252
2253 NTSTATUS _lsa_EnumPrivsAccount(pipes_struct *p, struct lsa_EnumPrivsAccount *r)
2254 {
2255         p->rng_fault_state = True;
2256         return NT_STATUS_NOT_IMPLEMENTED;
2257 }
2258
2259 NTSTATUS _lsa_AddPrivilegesToAccount(pipes_struct *p, struct lsa_AddPrivilegesToAccount *r)
2260 {
2261         p->rng_fault_state = True;
2262         return NT_STATUS_NOT_IMPLEMENTED;
2263 }
2264
2265 NTSTATUS _lsa_RemovePrivilegesFromAccount(pipes_struct *p, struct lsa_RemovePrivilegesFromAccount *r)
2266 {
2267         p->rng_fault_state = True;
2268         return NT_STATUS_NOT_IMPLEMENTED;
2269 }
2270
2271 NTSTATUS _lsa_GetQuotasForAccount(pipes_struct *p, struct lsa_GetQuotasForAccount *r)
2272 {
2273         p->rng_fault_state = True;
2274         return NT_STATUS_NOT_IMPLEMENTED;
2275 }
2276
2277 NTSTATUS _lsa_SetQuotasForAccount(pipes_struct *p, struct lsa_SetQuotasForAccount *r)
2278 {
2279         p->rng_fault_state = True;
2280         return NT_STATUS_NOT_IMPLEMENTED;
2281 }
2282
2283 NTSTATUS _lsa_GetSystemAccessAccount(pipes_struct *p, struct lsa_GetSystemAccessAccount *r)
2284 {
2285         p->rng_fault_state = True;
2286         return NT_STATUS_NOT_IMPLEMENTED;
2287 }
2288
2289 NTSTATUS _lsa_SetSystemAccessAccount(pipes_struct *p, struct lsa_SetSystemAccessAccount *r)
2290 {
2291         p->rng_fault_state = True;
2292         return NT_STATUS_NOT_IMPLEMENTED;
2293 }
2294
2295 NTSTATUS _lsa_QueryTrustedDomainInfo(pipes_struct *p, struct lsa_QueryTrustedDomainInfo *r)
2296 {
2297         p->rng_fault_state = True;
2298         return NT_STATUS_NOT_IMPLEMENTED;
2299 }
2300
2301 NTSTATUS _lsa_SetInformationTrustedDomain(pipes_struct *p, struct lsa_SetInformationTrustedDomain *r)
2302 {
2303         p->rng_fault_state = True;
2304         return NT_STATUS_NOT_IMPLEMENTED;
2305 }
2306
2307 NTSTATUS _lsa_QuerySecret(pipes_struct *p, struct lsa_QuerySecret *r)
2308 {
2309         p->rng_fault_state = True;
2310         return NT_STATUS_NOT_IMPLEMENTED;
2311 }
2312
2313 NTSTATUS _lsa_LookupPrivValue(pipes_struct *p, struct lsa_LookupPrivValue *r)
2314 {
2315         p->rng_fault_state = True;
2316         return NT_STATUS_NOT_IMPLEMENTED;
2317 }
2318
2319 NTSTATUS _lsa_LookupPrivName(pipes_struct *p, struct lsa_LookupPrivName *r)
2320 {
2321         p->rng_fault_state = True;
2322         return NT_STATUS_NOT_IMPLEMENTED;
2323 }
2324
2325 NTSTATUS _lsa_LookupPrivDisplayName(pipes_struct *p, struct lsa_LookupPrivDisplayName *r)
2326 {
2327         p->rng_fault_state = True;
2328         return NT_STATUS_NOT_IMPLEMENTED;
2329 }
2330
2331 NTSTATUS _lsa_DeleteObject(pipes_struct *p, struct lsa_DeleteObject *r)
2332 {
2333         p->rng_fault_state = True;
2334         return NT_STATUS_NOT_IMPLEMENTED;
2335 }
2336
2337 NTSTATUS _lsa_EnumAccountsWithUserRight(pipes_struct *p, struct lsa_EnumAccountsWithUserRight *r)
2338 {
2339         p->rng_fault_state = True;
2340         return NT_STATUS_NOT_IMPLEMENTED;
2341 }
2342
2343 NTSTATUS _lsa_EnumAccountRights(pipes_struct *p, struct lsa_EnumAccountRights *r)
2344 {
2345         p->rng_fault_state = True;
2346         return NT_STATUS_NOT_IMPLEMENTED;
2347 }
2348
2349 NTSTATUS _lsa_AddAccountRights(pipes_struct *p, struct lsa_AddAccountRights *r)
2350 {
2351         p->rng_fault_state = True;
2352         return NT_STATUS_NOT_IMPLEMENTED;
2353 }
2354
2355 NTSTATUS _lsa_RemoveAccountRights(pipes_struct *p, struct lsa_RemoveAccountRights *r)
2356 {
2357         p->rng_fault_state = True;
2358         return NT_STATUS_NOT_IMPLEMENTED;
2359 }
2360
2361 NTSTATUS _lsa_QueryTrustedDomainInfoBySid(pipes_struct *p, struct lsa_QueryTrustedDomainInfoBySid *r)
2362 {
2363         p->rng_fault_state = True;
2364         return NT_STATUS_NOT_IMPLEMENTED;
2365 }
2366
2367 NTSTATUS _lsa_SetTrustedDomainInfo(pipes_struct *p, struct lsa_SetTrustedDomainInfo *r)
2368 {
2369         p->rng_fault_state = True;
2370         return NT_STATUS_NOT_IMPLEMENTED;
2371 }
2372
2373 NTSTATUS _lsa_DeleteTrustedDomain(pipes_struct *p, struct lsa_DeleteTrustedDomain *r)
2374 {
2375         p->rng_fault_state = True;
2376         return NT_STATUS_NOT_IMPLEMENTED;
2377 }
2378
2379 NTSTATUS _lsa_StorePrivateData(pipes_struct *p, struct lsa_StorePrivateData *r)
2380 {
2381         p->rng_fault_state = True;
2382         return NT_STATUS_NOT_IMPLEMENTED;
2383 }
2384
2385 NTSTATUS _lsa_RetrievePrivateData(pipes_struct *p, struct lsa_RetrievePrivateData *r)
2386 {
2387         p->rng_fault_state = True;
2388         return NT_STATUS_NOT_IMPLEMENTED;
2389 }
2390
2391 NTSTATUS _lsa_OpenPolicy2(pipes_struct *p, struct lsa_OpenPolicy2 *r)
2392 {
2393         p->rng_fault_state = True;
2394         return NT_STATUS_NOT_IMPLEMENTED;
2395 }
2396
2397 NTSTATUS _lsa_GetUserName(pipes_struct *p, struct lsa_GetUserName *r)
2398 {
2399         p->rng_fault_state = True;
2400         return NT_STATUS_NOT_IMPLEMENTED;
2401 }
2402
2403 NTSTATUS _lsa_QueryInfoPolicy2(pipes_struct *p, struct lsa_QueryInfoPolicy2 *r)
2404 {
2405         p->rng_fault_state = True;
2406         return NT_STATUS_NOT_IMPLEMENTED;
2407 }
2408
2409 NTSTATUS _lsa_SetInfoPolicy2(pipes_struct *p, struct lsa_SetInfoPolicy2 *r)
2410 {
2411         p->rng_fault_state = True;
2412         return NT_STATUS_NOT_IMPLEMENTED;
2413 }
2414
2415 NTSTATUS _lsa_QueryTrustedDomainInfoByName(pipes_struct *p, struct lsa_QueryTrustedDomainInfoByName *r)
2416 {
2417         p->rng_fault_state = True;
2418         return NT_STATUS_NOT_IMPLEMENTED;
2419 }
2420
2421 NTSTATUS _lsa_SetTrustedDomainInfoByName(pipes_struct *p, struct lsa_SetTrustedDomainInfoByName *r)
2422 {
2423         p->rng_fault_state = True;
2424         return NT_STATUS_NOT_IMPLEMENTED;
2425 }
2426
2427 NTSTATUS _lsa_EnumTrustedDomainsEx(pipes_struct *p, struct lsa_EnumTrustedDomainsEx *r)
2428 {
2429         p->rng_fault_state = True;
2430         return NT_STATUS_NOT_IMPLEMENTED;
2431 }
2432
2433 NTSTATUS _lsa_CreateTrustedDomainEx(pipes_struct *p, struct lsa_CreateTrustedDomainEx *r)
2434 {
2435         p->rng_fault_state = True;
2436         return NT_STATUS_NOT_IMPLEMENTED;
2437 }
2438
2439 NTSTATUS _lsa_CloseTrustedDomainEx(pipes_struct *p, struct lsa_CloseTrustedDomainEx *r)
2440 {
2441         p->rng_fault_state = True;
2442         return NT_STATUS_NOT_IMPLEMENTED;
2443 }
2444
2445 NTSTATUS _lsa_QueryDomainInformationPolicy(pipes_struct *p, struct lsa_QueryDomainInformationPolicy *r)
2446 {
2447         p->rng_fault_state = True;
2448         return NT_STATUS_NOT_IMPLEMENTED;
2449 }
2450
2451 NTSTATUS _lsa_SetDomainInformationPolicy(pipes_struct *p, struct lsa_SetDomainInformationPolicy *r)
2452 {
2453         p->rng_fault_state = True;
2454         return NT_STATUS_NOT_IMPLEMENTED;
2455 }
2456
2457 NTSTATUS _lsa_OpenTrustedDomainByName(pipes_struct *p, struct lsa_OpenTrustedDomainByName *r)
2458 {
2459         p->rng_fault_state = True;
2460         return NT_STATUS_NOT_IMPLEMENTED;
2461 }
2462
2463 NTSTATUS _lsa_TestCall(pipes_struct *p, struct lsa_TestCall *r)
2464 {
2465         p->rng_fault_state = True;
2466         return NT_STATUS_NOT_IMPLEMENTED;
2467 }
2468
2469 NTSTATUS _lsa_LookupSids2(pipes_struct *p, struct lsa_LookupSids2 *r)
2470 {
2471         p->rng_fault_state = True;
2472         return NT_STATUS_NOT_IMPLEMENTED;
2473 }
2474
2475 NTSTATUS _lsa_LookupNames2(pipes_struct *p, struct lsa_LookupNames2 *r)
2476 {
2477         p->rng_fault_state = True;
2478         return NT_STATUS_NOT_IMPLEMENTED;
2479 }
2480
2481 NTSTATUS _lsa_CreateTrustedDomainEx2(pipes_struct *p, struct lsa_CreateTrustedDomainEx2 *r)
2482 {
2483         p->rng_fault_state = True;
2484         return NT_STATUS_NOT_IMPLEMENTED;
2485 }
2486
2487 NTSTATUS _lsa_CREDRWRITE(pipes_struct *p, struct lsa_CREDRWRITE *r)
2488 {
2489         p->rng_fault_state = True;
2490         return NT_STATUS_NOT_IMPLEMENTED;
2491 }
2492
2493 NTSTATUS _lsa_CREDRREAD(pipes_struct *p, struct lsa_CREDRREAD *r)
2494 {
2495         p->rng_fault_state = True;
2496         return NT_STATUS_NOT_IMPLEMENTED;
2497 }
2498
2499 NTSTATUS _lsa_CREDRENUMERATE(pipes_struct *p, struct lsa_CREDRENUMERATE *r)
2500 {
2501         p->rng_fault_state = True;
2502         return NT_STATUS_NOT_IMPLEMENTED;
2503 }
2504
2505 NTSTATUS _lsa_CREDRWRITEDOMAINCREDENTIALS(pipes_struct *p, struct lsa_CREDRWRITEDOMAINCREDENTIALS *r)
2506 {
2507         p->rng_fault_state = True;
2508         return NT_STATUS_NOT_IMPLEMENTED;
2509 }
2510
2511 NTSTATUS _lsa_CREDRREADDOMAINCREDENTIALS(pipes_struct *p, struct lsa_CREDRREADDOMAINCREDENTIALS *r)
2512 {
2513         p->rng_fault_state = True;
2514         return NT_STATUS_NOT_IMPLEMENTED;
2515 }
2516
2517 NTSTATUS _lsa_CREDRDELETE(pipes_struct *p, struct lsa_CREDRDELETE *r)
2518 {
2519         p->rng_fault_state = True;
2520         return NT_STATUS_NOT_IMPLEMENTED;
2521 }
2522
2523 NTSTATUS _lsa_CREDRGETTARGETINFO(pipes_struct *p, struct lsa_CREDRGETTARGETINFO *r)
2524 {
2525         p->rng_fault_state = True;
2526         return NT_STATUS_NOT_IMPLEMENTED;
2527 }
2528
2529 NTSTATUS _lsa_CREDRPROFILELOADED(pipes_struct *p, struct lsa_CREDRPROFILELOADED *r)
2530 {
2531         p->rng_fault_state = True;
2532         return NT_STATUS_NOT_IMPLEMENTED;
2533 }
2534
2535 NTSTATUS _lsa_LookupNames3(pipes_struct *p, struct lsa_LookupNames3 *r)
2536 {
2537         p->rng_fault_state = True;
2538         return NT_STATUS_NOT_IMPLEMENTED;
2539 }
2540
2541 NTSTATUS _lsa_CREDRGETSESSIONTYPES(pipes_struct *p, struct lsa_CREDRGETSESSIONTYPES *r)
2542 {
2543         p->rng_fault_state = True;
2544         return NT_STATUS_NOT_IMPLEMENTED;
2545 }
2546
2547 NTSTATUS _lsa_LSARREGISTERAUDITEVENT(pipes_struct *p, struct lsa_LSARREGISTERAUDITEVENT *r)
2548 {
2549         p->rng_fault_state = True;
2550         return NT_STATUS_NOT_IMPLEMENTED;
2551 }
2552
2553 NTSTATUS _lsa_LSARGENAUDITEVENT(pipes_struct *p, struct lsa_LSARGENAUDITEVENT *r)
2554 {
2555         p->rng_fault_state = True;
2556         return NT_STATUS_NOT_IMPLEMENTED;
2557 }
2558
2559 NTSTATUS _lsa_LSARUNREGISTERAUDITEVENT(pipes_struct *p, struct lsa_LSARUNREGISTERAUDITEVENT *r)
2560 {
2561         p->rng_fault_state = True;
2562         return NT_STATUS_NOT_IMPLEMENTED;
2563 }
2564
2565 NTSTATUS _lsa_LSARQUERYFORESTTRUSTINFORMATION(pipes_struct *p, struct lsa_LSARQUERYFORESTTRUSTINFORMATION *r)
2566 {
2567         p->rng_fault_state = True;
2568         return NT_STATUS_NOT_IMPLEMENTED;
2569 }
2570
2571 NTSTATUS _lsa_LSARSETFORESTTRUSTINFORMATION(pipes_struct *p, struct lsa_LSARSETFORESTTRUSTINFORMATION *r)
2572 {
2573         p->rng_fault_state = True;
2574         return NT_STATUS_NOT_IMPLEMENTED;
2575 }
2576
2577 NTSTATUS _lsa_CREDRRENAME(pipes_struct *p, struct lsa_CREDRRENAME *r)
2578 {
2579         p->rng_fault_state = True;
2580         return NT_STATUS_NOT_IMPLEMENTED;
2581 }
2582
2583 NTSTATUS _lsa_LookupSids3(pipes_struct *p, struct lsa_LookupSids3 *r)
2584 {
2585         p->rng_fault_state = True;
2586         return NT_STATUS_NOT_IMPLEMENTED;
2587 }
2588
2589 NTSTATUS _lsa_LookupNames4(pipes_struct *p, struct lsa_LookupNames4 *r)
2590 {
2591         p->rng_fault_state = True;
2592         return NT_STATUS_NOT_IMPLEMENTED;
2593 }
2594
2595 NTSTATUS _lsa_LSAROPENPOLICYSCE(pipes_struct *p, struct lsa_LSAROPENPOLICYSCE *r)
2596 {
2597         p->rng_fault_state = True;
2598         return NT_STATUS_NOT_IMPLEMENTED;
2599 }
2600
2601 NTSTATUS _lsa_LSARADTREGISTERSECURITYEVENTSOURCE(pipes_struct *p, struct lsa_LSARADTREGISTERSECURITYEVENTSOURCE *r)
2602 {
2603         p->rng_fault_state = True;
2604         return NT_STATUS_NOT_IMPLEMENTED;
2605 }
2606
2607 NTSTATUS _lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE(pipes_struct *p, struct lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE *r)
2608 {
2609         p->rng_fault_state = True;
2610         return NT_STATUS_NOT_IMPLEMENTED;
2611 }
2612
2613 NTSTATUS _lsa_LSARADTREPORTSECURITYEVENT(pipes_struct *p, struct lsa_LSARADTREPORTSECURITYEVENT *r)
2614 {
2615         p->rng_fault_state = True;
2616         return NT_STATUS_NOT_IMPLEMENTED;
2617 }