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