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