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