Use pidl for _lsa_LookupSids, _lsa_LookupSids2 and _lsa_LookupSids3.
[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                                           TALLOC_CTX *mem_ctx,
768                                           uint16_t level,                       /* input */
769                                           int num_sids,                         /* input */
770                                           struct lsa_SidPtr *sid,               /* input */
771                                           struct lsa_RefDomainList **pp_ref,    /* input/output */
772                                           struct lsa_TranslatedName2 **pp_names,/* input/output */
773                                           uint32_t *pp_mapped_count)            /* input/output */
774 {
775         NTSTATUS status;
776         int i;
777         const DOM_SID **sids = NULL;
778         struct lsa_RefDomainList *ref = NULL;
779         uint32 mapped_count = 0;
780         struct lsa_dom_info *dom_infos = NULL;
781         struct lsa_name_info *name_infos = NULL;
782         struct lsa_TranslatedName2 *names = NULL;
783
784         *pp_mapped_count = 0;
785         *pp_names = NULL;
786         *pp_ref = NULL;
787
788         if (num_sids == 0) {
789                 return NT_STATUS_OK;
790         }
791
792         sids = TALLOC_ARRAY(p->mem_ctx, const DOM_SID *, num_sids);
793         ref = TALLOC_ZERO_P(p->mem_ctx, struct lsa_RefDomainList);
794
795         if (sids == NULL || ref == NULL) {
796                 return NT_STATUS_NO_MEMORY;
797         }
798
799         for (i=0; i<num_sids; i++) {
800                 sids[i] = sid[i].sid;
801         }
802
803         status = lookup_sids(p->mem_ctx, num_sids, sids, level,
804                                   &dom_infos, &name_infos);
805
806         if (!NT_STATUS_IS_OK(status)) {
807                 return status;
808         }
809
810         names = TALLOC_ARRAY(p->mem_ctx, struct lsa_TranslatedName2, num_sids);
811         if (names == NULL) {
812                 return NT_STATUS_NO_MEMORY;
813         }
814
815         for (i=0; i<MAX_REF_DOMAINS; i++) {
816
817                 if (!dom_infos[i].valid) {
818                         break;
819                 }
820
821                 if (init_lsa_ref_domain_list(mem_ctx, ref,
822                                              dom_infos[i].name,
823                                              &dom_infos[i].sid) != i) {
824                         DEBUG(0, ("Domain %s mentioned twice??\n",
825                                   dom_infos[i].name));
826                         return NT_STATUS_INTERNAL_ERROR;
827                 }
828         }
829
830         for (i=0; i<num_sids; i++) {
831                 struct lsa_name_info *name = &name_infos[i];
832
833                 if (name->type == SID_NAME_UNKNOWN) {
834                         fstring tmp;
835                         name->dom_idx = -1;
836                         /* Unknown sids should return the string
837                          * representation of the SID. Windows 2003 behaves
838                          * rather erratic here, in many cases it returns the
839                          * RID as 8 bytes hex, in others it returns the full
840                          * SID. We (Jerry/VL) could not figure out which the
841                          * hard cases are, so leave it with the SID.  */
842                         name->name = talloc_asprintf(p->mem_ctx, "%s",
843                                                      sid_to_fstring(tmp,
844                                                                     sids[i]));
845                         if (name->name == NULL) {
846                                 return NT_STATUS_NO_MEMORY;
847                         }
848                 } else {
849                         mapped_count += 1;
850                 }
851
852                 init_lsa_translated_name2(&names[i], name->type,
853                                           name->name, name->dom_idx, 0);
854         }
855
856         status = NT_STATUS_NONE_MAPPED;
857         if (mapped_count > 0) {
858                 status = (mapped_count < num_sids) ?
859                         STATUS_SOME_UNMAPPED : NT_STATUS_OK;
860         }
861
862         DEBUG(10, ("num_sids %d, mapped_count %d, status %s\n",
863                    num_sids, mapped_count, nt_errstr(status)));
864
865         *pp_mapped_count = mapped_count;
866         *pp_names = names;
867         *pp_ref = ref;
868
869         return status;
870 }
871
872 /***************************************************************************
873  _lsa_LookupSids
874  ***************************************************************************/
875
876 NTSTATUS _lsa_LookupSids(pipes_struct *p,
877                          struct lsa_LookupSids *r)
878 {
879         NTSTATUS status;
880         struct lsa_info *handle;
881         int num_sids = r->in.sids->num_sids;
882         uint32 mapped_count = 0;
883         struct lsa_RefDomainList *domains = NULL;
884         struct lsa_TranslatedName *names_out = NULL;
885         struct lsa_TranslatedName2 *names = NULL;
886         int i;
887
888         if ((r->in.level < 1) || (r->in.level > 6)) {
889                 return NT_STATUS_INVALID_PARAMETER;
890         }
891
892         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle)) {
893                 return NT_STATUS_INVALID_HANDLE;
894         }
895
896         /* check if the user has enough rights */
897         if (!(handle->access & POLICY_LOOKUP_NAMES)) {
898                 return NT_STATUS_ACCESS_DENIED;
899         }
900
901         if (num_sids >  MAX_LOOKUP_SIDS) {
902                 DEBUG(5,("_lsa_LookupSids: limit of %d exceeded, requested %d\n",
903                          MAX_LOOKUP_SIDS, num_sids));
904                 return NT_STATUS_NONE_MAPPED;
905         }
906
907         status = _lsa_lookup_sids_internal(p,
908                                            p->mem_ctx,
909                                            r->in.level,
910                                            num_sids,
911                                            r->in.sids->sids,
912                                            &domains,
913                                            &names,
914                                            &mapped_count);
915
916         /* Convert from lsa_TranslatedName2 to lsa_TranslatedName */
917         names_out = TALLOC_ARRAY(p->mem_ctx, struct lsa_TranslatedName,
918                                  num_sids);
919         if (!names_out) {
920                 return NT_STATUS_NO_MEMORY;
921         }
922
923         for (i=0; i<num_sids; i++) {
924                 names_out[i].sid_type = names[i].sid_type;
925                 names_out[i].name = names[i].name;
926                 names_out[i].sid_index = names[i].sid_index;
927         }
928
929         *r->out.domains = domains;
930         r->out.names->count = num_sids;
931         r->out.names->names = names_out;
932         *r->out.count = mapped_count;
933
934         return status;
935 }
936
937 /***************************************************************************
938  _lsa_LookupSids2
939  ***************************************************************************/
940
941 NTSTATUS _lsa_LookupSids2(pipes_struct *p,
942                           struct lsa_LookupSids2 *r)
943 {
944         NTSTATUS status;
945         struct lsa_info *handle;
946         int num_sids = r->in.sids->num_sids;
947         uint32 mapped_count = 0;
948         struct lsa_RefDomainList *domains = NULL;
949         struct lsa_TranslatedName2 *names = NULL;
950         bool check_policy = true;
951
952         switch (p->hdr_req.opnum) {
953                 case NDR_LSA_LOOKUPSIDS3:
954                         check_policy = false;
955                         break;
956                 case NDR_LSA_LOOKUPSIDS2:
957                 default:
958                         check_policy = true;
959         }
960
961         if ((r->in.level < 1) || (r->in.level > 6)) {
962                 return NT_STATUS_INVALID_PARAMETER;
963         }
964
965         if (check_policy) {
966                 if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle)) {
967                         return NT_STATUS_INVALID_HANDLE;
968                 }
969
970                 /* check if the user have enough rights */
971                 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
972                         return NT_STATUS_ACCESS_DENIED;
973                 }
974         }
975
976         if (num_sids >  MAX_LOOKUP_SIDS) {
977                 DEBUG(5,("_lsa_LookupSids2: limit of %d exceeded, requested %d\n",
978                          MAX_LOOKUP_SIDS, num_sids));
979                 return NT_STATUS_NONE_MAPPED;
980         }
981
982         status = _lsa_lookup_sids_internal(p,
983                                            p->mem_ctx,
984                                            r->in.level,
985                                            num_sids,
986                                            r->in.sids->sids,
987                                            &domains,
988                                            &names,
989                                            &mapped_count);
990
991         *r->out.domains = domains;
992         r->out.names->count = num_sids;
993         r->out.names->names = names;
994         *r->out.count = mapped_count;
995
996         return status;
997 }
998
999 /***************************************************************************
1000  _lsa_LookupSids3
1001  ***************************************************************************/
1002
1003 NTSTATUS _lsa_LookupSids3(pipes_struct *p,
1004                           struct lsa_LookupSids3 *r)
1005 {
1006         struct lsa_LookupSids2 q;
1007
1008         /* No policy handle on this call. Restrict to crypto connections. */
1009         if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1010                 DEBUG(0,("_lsa_LookupSids3: client %s not using schannel for netlogon\n",
1011                         get_remote_machine_name() ));
1012                 return NT_STATUS_INVALID_PARAMETER;
1013         }
1014
1015         q.in.handle             = NULL;
1016         q.in.sids               = r->in.sids;
1017         q.in.level              = r->in.level;
1018         q.in.unknown1           = r->in.unknown1;
1019         q.in.unknown2           = r->in.unknown2;
1020         q.in.names              = r->in.names;
1021         q.in.count              = r->in.count;
1022
1023         q.out.domains           = r->out.domains;
1024         q.out.names             = r->out.names;
1025         q.out.count             = r->out.count;
1026
1027         return _lsa_LookupSids2(p, &q);
1028 }
1029
1030 /***************************************************************************
1031  ***************************************************************************/
1032
1033 static int lsa_lookup_level_to_flags(uint16 level)
1034 {
1035         int flags;
1036
1037         switch (level) {
1038                 case 1:
1039                         flags = LOOKUP_NAME_ALL;
1040                         break;
1041                 case 2:
1042                         flags = LOOKUP_NAME_DOMAIN|LOOKUP_NAME_REMOTE|LOOKUP_NAME_ISOLATED;
1043                         break;
1044                 case 3:
1045                         flags = LOOKUP_NAME_DOMAIN|LOOKUP_NAME_ISOLATED;
1046                         break;
1047                 case 4:
1048                 case 5:
1049                 case 6:
1050                 default:
1051                         flags = LOOKUP_NAME_NONE;
1052                         break;
1053         }
1054
1055         return flags;
1056 }
1057
1058 /***************************************************************************
1059  _lsa_LookupNames
1060  ***************************************************************************/
1061
1062 NTSTATUS _lsa_LookupNames(pipes_struct *p,
1063                           struct lsa_LookupNames *r)
1064 {
1065         NTSTATUS status = NT_STATUS_NONE_MAPPED;
1066         struct lsa_info *handle;
1067         struct lsa_String *names = r->in.names;
1068         uint32 num_entries = r->in.num_names;
1069         struct lsa_RefDomainList *domains = NULL;
1070         struct lsa_TranslatedSid *rids = NULL;
1071         uint32 mapped_count = 0;
1072         int flags = 0;
1073
1074         if (num_entries >  MAX_LOOKUP_SIDS) {
1075                 num_entries = MAX_LOOKUP_SIDS;
1076                 DEBUG(5,("_lsa_LookupNames: truncating name lookup list to %d\n",
1077                         num_entries));
1078         }
1079
1080         flags = lsa_lookup_level_to_flags(r->in.level);
1081
1082         domains = TALLOC_ZERO_P(p->mem_ctx, struct lsa_RefDomainList);
1083         if (!domains) {
1084                 return NT_STATUS_NO_MEMORY;
1085         }
1086
1087         if (num_entries) {
1088                 rids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_TranslatedSid,
1089                                          num_entries);
1090                 if (!rids) {
1091                         return NT_STATUS_NO_MEMORY;
1092                 }
1093         } else {
1094                 rids = NULL;
1095         }
1096
1097         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle)) {
1098                 status = NT_STATUS_INVALID_HANDLE;
1099                 goto done;
1100         }
1101
1102         /* check if the user have enough rights */
1103         if (!(handle->access & POLICY_LOOKUP_NAMES)) {
1104                 status = NT_STATUS_ACCESS_DENIED;
1105                 goto done;
1106         }
1107
1108         /* set up the LSA Lookup RIDs response */
1109         become_root(); /* lookup_name can require root privs */
1110         status = lookup_lsa_rids(p->mem_ctx, domains, rids, num_entries,
1111                                  names, flags, &mapped_count);
1112         unbecome_root();
1113
1114 done:
1115
1116         if (NT_STATUS_IS_OK(status) && (num_entries != 0) ) {
1117                 if (mapped_count == 0) {
1118                         status = NT_STATUS_NONE_MAPPED;
1119                 } else if (mapped_count != num_entries) {
1120                         status = STATUS_SOME_UNMAPPED;
1121                 }
1122         }
1123
1124         *r->out.count = num_entries;
1125         *r->out.domains = domains;
1126         r->out.sids->sids = rids;
1127         r->out.sids->count = mapped_count;
1128
1129         return status;
1130 }
1131
1132 /***************************************************************************
1133  _lsa_LookupNames2
1134  ***************************************************************************/
1135
1136 NTSTATUS _lsa_LookupNames2(pipes_struct *p,
1137                            struct lsa_LookupNames2 *r)
1138 {
1139         NTSTATUS status;
1140         struct lsa_LookupNames q;
1141         struct lsa_TransSidArray2 *sid_array2 = r->in.sids;
1142         struct lsa_TransSidArray *sid_array = NULL;
1143         uint32_t i;
1144
1145         sid_array = TALLOC_ZERO_P(p->mem_ctx, struct lsa_TransSidArray);
1146         if (!sid_array) {
1147                 return NT_STATUS_NO_MEMORY;
1148         }
1149
1150         q.in.handle             = r->in.handle;
1151         q.in.num_names          = r->in.num_names;
1152         q.in.names              = r->in.names;
1153         q.in.level              = r->in.level;
1154         q.in.sids               = sid_array;
1155         q.in.count              = r->in.count;
1156         /* we do not know what this is for */
1157         /*                      = r->in.unknown1; */
1158         /*                      = r->in.unknown2; */
1159
1160         q.out.domains           = r->out.domains;
1161         q.out.sids              = sid_array;
1162         q.out.count             = r->out.count;
1163
1164         status = _lsa_LookupNames(p, &q);
1165
1166         sid_array2->sids = TALLOC_ARRAY(p->mem_ctx, struct lsa_TranslatedSid2, sid_array->count);
1167         if (!sid_array2->sids) {
1168                 return NT_STATUS_NO_MEMORY;
1169         }
1170
1171         for (i=0; i<sid_array->count; i++) {
1172                 sid_array2->sids[i].sid_type  = sid_array->sids[i].sid_type;
1173                 sid_array2->sids[i].rid       = sid_array->sids[i].rid;
1174                 sid_array2->sids[i].sid_index = sid_array->sids[i].sid_index;
1175                 sid_array2->sids[i].unknown   = 0;
1176         }
1177
1178         r->out.sids = sid_array2;
1179
1180         return status;
1181 }
1182
1183 /***************************************************************************
1184  _lsa_LookupNames3
1185  ***************************************************************************/
1186
1187 NTSTATUS _lsa_LookupNames3(pipes_struct *p,
1188                            struct lsa_LookupNames3 *r)
1189 {
1190         NTSTATUS status;
1191         struct lsa_info *handle;
1192         struct lsa_String *names = r->in.names;
1193         uint32 num_entries = r->in.num_names;
1194         struct lsa_RefDomainList *domains = NULL;
1195         struct lsa_TranslatedSid3 *trans_sids = NULL;
1196         uint32 mapped_count = 0;
1197         int flags = 0;
1198         bool check_policy = true;
1199
1200         switch (p->hdr_req.opnum) {
1201                 case NDR_LSA_LOOKUPNAMES4:
1202                         check_policy = false;
1203                         break;
1204                 case NDR_LSA_LOOKUPNAMES3:
1205                 default:
1206                         check_policy = true;
1207         }
1208
1209         if (num_entries >  MAX_LOOKUP_SIDS) {
1210                 num_entries = MAX_LOOKUP_SIDS;
1211                 DEBUG(5,("_lsa_LookupNames3: truncating name lookup list to %d\n", num_entries));
1212         }
1213
1214         /* Probably the lookup_level is some sort of bitmask. */
1215         if (r->in.level == 1) {
1216                 flags = LOOKUP_NAME_ALL;
1217         }
1218
1219         domains = TALLOC_ZERO_P(p->mem_ctx, struct lsa_RefDomainList);
1220         if (!domains) {
1221                 return NT_STATUS_NO_MEMORY;
1222         }
1223
1224         if (num_entries) {
1225                 trans_sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_TranslatedSid3,
1226                                                num_entries);
1227                 if (!trans_sids) {
1228                         return NT_STATUS_NO_MEMORY;
1229                 }
1230         } else {
1231                 trans_sids = NULL;
1232         }
1233
1234         if (check_policy) {
1235
1236                 if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle)) {
1237                         status = NT_STATUS_INVALID_HANDLE;
1238                         goto done;
1239                 }
1240
1241                 /* check if the user have enough rights */
1242                 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
1243                         status = NT_STATUS_ACCESS_DENIED;
1244                         goto done;
1245                 }
1246         }
1247
1248         /* set up the LSA Lookup SIDs response */
1249         become_root(); /* lookup_name can require root privs */
1250         status = lookup_lsa_sids(p->mem_ctx, domains, trans_sids, num_entries,
1251                                  names, flags, &mapped_count);
1252         unbecome_root();
1253
1254 done:
1255
1256         if (NT_STATUS_IS_OK(status)) {
1257                 if (mapped_count == 0) {
1258                         status = NT_STATUS_NONE_MAPPED;
1259                 } else if (mapped_count != num_entries) {
1260                         status = STATUS_SOME_UNMAPPED;
1261                 }
1262         }
1263
1264         *r->out.count = num_entries;
1265         *r->out.domains = domains;
1266         r->out.sids->sids = trans_sids;
1267         r->out.sids->count = mapped_count;
1268
1269         return status;
1270 }
1271
1272 /***************************************************************************
1273  _lsa_LookupNames4
1274  ***************************************************************************/
1275
1276 NTSTATUS _lsa_LookupNames4(pipes_struct *p,
1277                            struct lsa_LookupNames4 *r)
1278 {
1279         struct lsa_LookupNames3 q;
1280
1281         /* No policy handle on this call. Restrict to crypto connections. */
1282         if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
1283                 DEBUG(0,("_lsa_lookup_names4: client %s not using schannel for netlogon\n",
1284                         get_remote_machine_name() ));
1285                 return NT_STATUS_INVALID_PARAMETER;
1286         }
1287
1288         q.in.handle             = NULL;
1289         q.in.num_names          = r->in.num_names;
1290         q.in.names              = r->in.names;
1291         q.in.level              = r->in.level;
1292         q.in.unknown1           = r->in.unknown1;
1293         q.in.unknown2           = r->in.unknown2;
1294         q.in.sids               = r->in.sids;
1295         q.in.count              = r->in.count;
1296
1297         q.out.domains           = r->out.domains;
1298         q.out.sids              = r->out.sids;
1299         q.out.count             = r->out.count;
1300
1301         return _lsa_LookupNames3(p, &q);
1302 }
1303
1304 /***************************************************************************
1305  _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
1306  ***************************************************************************/
1307
1308 NTSTATUS _lsa_Close(pipes_struct *p, struct lsa_Close *r)
1309 {
1310         if (!find_policy_by_hnd(p, r->in.handle, NULL)) {
1311                 return NT_STATUS_INVALID_HANDLE;
1312         }
1313
1314         close_policy_hnd(p, r->in.handle);
1315         return NT_STATUS_OK;
1316 }
1317
1318 /***************************************************************************
1319  ***************************************************************************/
1320
1321 NTSTATUS _lsa_OpenSecret(pipes_struct *p, struct lsa_OpenSecret *r)
1322 {
1323         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1324 }
1325
1326 /***************************************************************************
1327  ***************************************************************************/
1328
1329 NTSTATUS _lsa_OpenTrustedDomain(pipes_struct *p, struct lsa_OpenTrustedDomain *r)
1330 {
1331         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1332 }
1333
1334 /***************************************************************************
1335  ***************************************************************************/
1336
1337 NTSTATUS _lsa_CreateTrustedDomain(pipes_struct *p, struct lsa_CreateTrustedDomain *r)
1338 {
1339         return NT_STATUS_ACCESS_DENIED;
1340 }
1341
1342 /***************************************************************************
1343  ***************************************************************************/
1344
1345 NTSTATUS _lsa_CreateSecret(pipes_struct *p, struct lsa_CreateSecret *r)
1346 {
1347         return NT_STATUS_ACCESS_DENIED;
1348 }
1349
1350 /***************************************************************************
1351  ***************************************************************************/
1352
1353 NTSTATUS _lsa_SetSecret(pipes_struct *p, struct lsa_SetSecret *r)
1354 {
1355         return NT_STATUS_ACCESS_DENIED;
1356 }
1357
1358 /***************************************************************************
1359  _lsa_DeleteObject
1360  ***************************************************************************/
1361
1362 NTSTATUS _lsa_DeleteObject(pipes_struct *p,
1363                            struct lsa_DeleteObject *r)
1364 {
1365         return NT_STATUS_ACCESS_DENIED;
1366 }
1367
1368 /***************************************************************************
1369  _lsa_EnumPrivs
1370  ***************************************************************************/
1371
1372 NTSTATUS _lsa_EnumPrivs(pipes_struct *p,
1373                         struct lsa_EnumPrivs *r)
1374 {
1375         struct lsa_info *handle;
1376         uint32 i;
1377         uint32 enum_context = *r->in.resume_handle;
1378         int num_privs = count_all_privileges();
1379         struct lsa_PrivEntry *entries = NULL;
1380         LUID_ATTR luid;
1381
1382         /* remember that the enum_context starts at 0 and not 1 */
1383
1384         if ( enum_context >= num_privs )
1385                 return NT_STATUS_NO_MORE_ENTRIES;
1386
1387         DEBUG(10,("_lsa_EnumPrivs: enum_context:%d total entries:%d\n",
1388                 enum_context, num_privs));
1389
1390         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1391                 return NT_STATUS_INVALID_HANDLE;
1392
1393         /* check if the user have enough rights
1394            I don't know if it's the right one. not documented.  */
1395
1396         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1397                 return NT_STATUS_ACCESS_DENIED;
1398
1399         if (num_privs) {
1400                 entries = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_PrivEntry, num_privs);
1401                 if (!entries) {
1402                         return NT_STATUS_NO_MEMORY;
1403                 }
1404         } else {
1405                 entries = NULL;
1406         }
1407
1408         for (i = 0; i < num_privs; i++) {
1409                 if( i < enum_context) {
1410
1411                         init_lsa_StringLarge(&entries[i].name, NULL);
1412
1413                         entries[i].luid.low = 0;
1414                         entries[i].luid.high = 0;
1415                 } else {
1416
1417                         init_lsa_StringLarge(&entries[i].name, privs[i].name);
1418
1419                         luid = get_privilege_luid( &privs[i].se_priv );
1420
1421                         entries[i].luid.low = luid.luid.low;
1422                         entries[i].luid.high = luid.luid.high;
1423                 }
1424         }
1425
1426         enum_context = num_privs;
1427
1428         *r->out.resume_handle = enum_context;
1429         r->out.privs->count = num_privs;
1430         r->out.privs->privs = entries;
1431
1432         return NT_STATUS_OK;
1433 }
1434
1435 /***************************************************************************
1436  _lsa_LookupPrivDisplayName
1437  ***************************************************************************/
1438
1439 NTSTATUS _lsa_LookupPrivDisplayName(pipes_struct *p,
1440                                     struct lsa_LookupPrivDisplayName *r)
1441 {
1442         struct lsa_info *handle;
1443         const char *description;
1444         struct lsa_StringLarge *lsa_name;
1445
1446         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1447                 return NT_STATUS_INVALID_HANDLE;
1448
1449         /* check if the user have enough rights */
1450
1451         /*
1452          * I don't know if it's the right one. not documented.
1453          */
1454         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1455                 return NT_STATUS_ACCESS_DENIED;
1456
1457         DEBUG(10,("_lsa_LookupPrivDisplayName: name = %s\n", r->in.name->string));
1458
1459         description = get_privilege_dispname(r->in.name->string);
1460         if (!description) {
1461                 DEBUG(10,("_lsa_LookupPrivDisplayName: doesn't exist\n"));
1462                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1463         }
1464
1465         DEBUG(10,("_lsa_LookupPrivDisplayName: display name = %s\n", description));
1466
1467         lsa_name = TALLOC_ZERO_P(p->mem_ctx, struct lsa_StringLarge);
1468         if (!lsa_name) {
1469                 return NT_STATUS_NO_MEMORY;
1470         }
1471
1472         init_lsa_StringLarge(lsa_name, description);
1473
1474         *r->out.returned_language_id = r->in.language_id;
1475         *r->out.disp_name = lsa_name;
1476
1477         return NT_STATUS_OK;
1478 }
1479
1480 /***************************************************************************
1481  _lsa_EnumAccounts
1482  ***************************************************************************/
1483
1484 NTSTATUS _lsa_EnumAccounts(pipes_struct *p,
1485                            struct lsa_EnumAccounts *r)
1486 {
1487         struct lsa_info *handle;
1488         DOM_SID *sid_list;
1489         int i, j, num_entries;
1490         NTSTATUS status;
1491         struct lsa_SidPtr *sids = NULL;
1492
1493         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1494                 return NT_STATUS_INVALID_HANDLE;
1495
1496         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1497                 return NT_STATUS_ACCESS_DENIED;
1498
1499         sid_list = NULL;
1500         num_entries = 0;
1501
1502         /* The only way we can currently find out all the SIDs that have been
1503            privileged is to scan all privileges */
1504
1505         status = privilege_enumerate_accounts(&sid_list, &num_entries);
1506         if (!NT_STATUS_IS_OK(status)) {
1507                 return status;
1508         }
1509
1510         if (*r->in.resume_handle >= num_entries) {
1511                 return NT_STATUS_NO_MORE_ENTRIES;
1512         }
1513
1514         if (num_entries - *r->in.resume_handle) {
1515                 sids = TALLOC_ZERO_ARRAY(p->mem_ctx, struct lsa_SidPtr,
1516                                          num_entries - *r->in.resume_handle);
1517                 if (!sids) {
1518                         SAFE_FREE(sid_list);
1519                         return NT_STATUS_NO_MEMORY;
1520                 }
1521
1522                 for (i = *r->in.resume_handle, j = 0; i < num_entries; i++, j++) {
1523                         sids[j].sid = sid_dup_talloc(p->mem_ctx, &sid_list[i]);
1524                         if (!sids[j].sid) {
1525                                 SAFE_FREE(sid_list);
1526                                 return NT_STATUS_NO_MEMORY;
1527                         }
1528                 }
1529         }
1530
1531         talloc_free(sid_list);
1532
1533         *r->out.resume_handle = num_entries;
1534         r->out.sids->num_sids = num_entries;
1535         r->out.sids->sids = sids;
1536
1537         return NT_STATUS_OK;
1538 }
1539
1540 /***************************************************************************
1541  _lsa_GetUserName
1542  ***************************************************************************/
1543
1544 NTSTATUS _lsa_GetUserName(pipes_struct *p,
1545                           struct lsa_GetUserName *r)
1546 {
1547         const char *username, *domname;
1548         user_struct *vuser = get_valid_user_struct(p->vuid);
1549         struct lsa_String *account_name = NULL;
1550         struct lsa_String *authority_name = NULL;
1551
1552         if (vuser == NULL)
1553                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1554
1555         if (vuser->guest) {
1556                 /*
1557                  * I'm 99% sure this is not the right place to do this,
1558                  * global_sid_Anonymous should probably be put into the token
1559                  * instead of the guest id -- vl
1560                  */
1561                 if (!lookup_sid(p->mem_ctx, &global_sid_Anonymous,
1562                                 &domname, &username, NULL)) {
1563                         return NT_STATUS_NO_MEMORY;
1564                 }
1565         } else {
1566                 username = vuser->user.smb_name;
1567                 domname = vuser->user.domain;
1568         }
1569
1570         account_name = TALLOC_ZERO_P(p->mem_ctx, struct lsa_String);
1571         if (!account_name) {
1572                 return NT_STATUS_NO_MEMORY;
1573         }
1574
1575         authority_name = TALLOC_ZERO_P(p->mem_ctx, struct lsa_String);
1576         if (!authority_name) {
1577                 return NT_STATUS_NO_MEMORY;
1578         }
1579
1580         init_lsa_String(account_name, username);
1581         init_lsa_String(authority_name, domname);
1582
1583         *r->out.account_name = account_name;
1584         *r->out.authority_name = authority_name;
1585
1586         return NT_STATUS_OK;
1587 }
1588
1589 /***************************************************************************
1590  _lsa_CreateAccount
1591  ***************************************************************************/
1592
1593 NTSTATUS _lsa_CreateAccount(pipes_struct *p,
1594                             struct lsa_CreateAccount *r)
1595 {
1596         struct lsa_info *handle;
1597         struct lsa_info *info;
1598
1599         /* find the connection policy handle. */
1600         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1601                 return NT_STATUS_INVALID_HANDLE;
1602
1603         /* check if the user have enough rights */
1604
1605         /*
1606          * I don't know if it's the right one. not documented.
1607          * but guessed with rpcclient.
1608          */
1609         if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
1610                 return NT_STATUS_ACCESS_DENIED;
1611
1612         /* check to see if the pipe_user is a Domain Admin since
1613            account_pol.tdb was already opened as root, this is all we have */
1614
1615         if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1616                 return NT_STATUS_ACCESS_DENIED;
1617
1618         if ( is_privileged_sid( r->in.sid ) )
1619                 return NT_STATUS_OBJECT_NAME_COLLISION;
1620
1621         /* associate the user/group SID with the (unique) handle. */
1622
1623         if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
1624                 return NT_STATUS_NO_MEMORY;
1625
1626         ZERO_STRUCTP(info);
1627         info->sid = *r->in.sid;
1628         info->access = r->in.access_mask;
1629
1630         /* get a (unique) handle.  open a policy on it. */
1631         if (!create_policy_hnd(p, r->out.acct_handle, free_lsa_info, (void *)info))
1632                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1633
1634         return privilege_create_account( &info->sid );
1635 }
1636
1637
1638 /***************************************************************************
1639  _lsa_OpenAccount
1640  ***************************************************************************/
1641
1642 NTSTATUS _lsa_OpenAccount(pipes_struct *p,
1643                           struct lsa_OpenAccount *r)
1644 {
1645         struct lsa_info *handle;
1646         struct lsa_info *info;
1647
1648         /* find the connection policy handle. */
1649         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1650                 return NT_STATUS_INVALID_HANDLE;
1651
1652         /* check if the user have enough rights */
1653
1654         /*
1655          * I don't know if it's the right one. not documented.
1656          * but guessed with rpcclient.
1657          */
1658         if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
1659                 return NT_STATUS_ACCESS_DENIED;
1660
1661         /* TODO: Fis the parsing routine before reenabling this check! */
1662         #if 0
1663         if (!lookup_sid(&handle->sid, dom_name, name, &type))
1664                 return NT_STATUS_ACCESS_DENIED;
1665         #endif
1666         /* associate the user/group SID with the (unique) handle. */
1667         if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
1668                 return NT_STATUS_NO_MEMORY;
1669
1670         ZERO_STRUCTP(info);
1671         info->sid = *r->in.sid;
1672         info->access = r->in.access_mask;
1673
1674         /* get a (unique) handle.  open a policy on it. */
1675         if (!create_policy_hnd(p, r->out.acct_handle, free_lsa_info, (void *)info))
1676                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1677
1678         return NT_STATUS_OK;
1679 }
1680
1681 /***************************************************************************
1682  _lsa_EnumPrivsAccount
1683  For a given SID, enumerate all the privilege this account has.
1684  ***************************************************************************/
1685
1686 NTSTATUS _lsa_EnumPrivsAccount(pipes_struct *p,
1687                                struct lsa_EnumPrivsAccount *r)
1688 {
1689         NTSTATUS status = NT_STATUS_OK;
1690         struct lsa_info *info=NULL;
1691         SE_PRIV mask;
1692         PRIVILEGE_SET privileges;
1693         struct lsa_PrivilegeSet *priv_set = NULL;
1694         struct lsa_LUIDAttribute *luid_attrs = NULL;
1695         int i;
1696
1697         /* find the connection policy handle. */
1698         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1699                 return NT_STATUS_INVALID_HANDLE;
1700
1701         if ( !get_privileges_for_sids( &mask, &info->sid, 1 ) )
1702                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1703
1704         privilege_set_init( &privileges );
1705
1706         if ( se_priv_to_privilege_set( &privileges, &mask ) ) {
1707
1708                 DEBUG(10,("_lsa_EnumPrivsAccount: %s has %d privileges\n",
1709                           sid_string_dbg(&info->sid),
1710                           privileges.count));
1711
1712                 priv_set = TALLOC_ZERO_P(p->mem_ctx, struct lsa_PrivilegeSet);
1713                 if (!priv_set) {
1714                         status = NT_STATUS_NO_MEMORY;
1715                         goto done;
1716                 }
1717
1718                 luid_attrs = TALLOC_ZERO_ARRAY(p->mem_ctx,
1719                                                struct lsa_LUIDAttribute,
1720                                                privileges.count);
1721                 if (!luid_attrs) {
1722                         status = NT_STATUS_NO_MEMORY;
1723                         goto done;
1724                 }
1725
1726                 for (i=0; i<privileges.count; i++) {
1727                         luid_attrs[i].luid.low = privileges.set[i].luid.low;
1728                         luid_attrs[i].luid.high = privileges.set[i].luid.high;
1729                         luid_attrs[i].attribute = privileges.set[i].attr;
1730                 }
1731
1732                 priv_set->count = privileges.count;
1733                 priv_set->unknown = 0;
1734                 priv_set->set = luid_attrs;
1735
1736                 *r->out.privs = priv_set;
1737         } else {
1738                 status = NT_STATUS_NO_SUCH_PRIVILEGE;
1739         }
1740
1741  done:
1742         privilege_set_free( &privileges );
1743
1744         return status;
1745 }
1746
1747 /***************************************************************************
1748  _lsa_GetSystemAccessAccount
1749  ***************************************************************************/
1750
1751 NTSTATUS _lsa_GetSystemAccessAccount(pipes_struct *p,
1752                                      struct lsa_GetSystemAccessAccount *r)
1753 {
1754         struct lsa_info *info=NULL;
1755
1756         /* find the connection policy handle. */
1757
1758         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1759                 return NT_STATUS_INVALID_HANDLE;
1760
1761         if (!lookup_sid(p->mem_ctx, &info->sid, NULL, NULL, NULL))
1762                 return NT_STATUS_ACCESS_DENIED;
1763
1764         /*
1765           0x01 -> Log on locally
1766           0x02 -> Access this computer from network
1767           0x04 -> Log on as a batch job
1768           0x10 -> Log on as a service
1769
1770           they can be ORed together
1771         */
1772
1773         *r->out.access_mask = PR_LOG_ON_LOCALLY | PR_ACCESS_FROM_NETWORK;
1774
1775         return NT_STATUS_OK;
1776 }
1777
1778 /***************************************************************************
1779   update the systemaccount information
1780  ***************************************************************************/
1781
1782 NTSTATUS _lsa_SetSystemAccessAccount(pipes_struct *p,
1783                                      struct lsa_SetSystemAccessAccount *r)
1784 {
1785         struct lsa_info *info=NULL;
1786         GROUP_MAP map;
1787
1788         /* find the connection policy handle. */
1789         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1790                 return NT_STATUS_INVALID_HANDLE;
1791
1792         /* check to see if the pipe_user is a Domain Admin since
1793            account_pol.tdb was already opened as root, this is all we have */
1794
1795         if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1796                 return NT_STATUS_ACCESS_DENIED;
1797
1798         if (!pdb_getgrsid(&map, info->sid))
1799                 return NT_STATUS_NO_SUCH_GROUP;
1800
1801         return pdb_update_group_mapping_entry(&map);
1802 }
1803
1804 /***************************************************************************
1805  _lsa_AddPrivilegesToAccount
1806  For a given SID, add some privileges.
1807  ***************************************************************************/
1808
1809 NTSTATUS _lsa_AddPrivilegesToAccount(pipes_struct *p,
1810                                      struct lsa_AddPrivilegesToAccount *r)
1811 {
1812         struct lsa_info *info = NULL;
1813         SE_PRIV mask;
1814         struct lsa_PrivilegeSet *set = NULL;
1815
1816         /* find the connection policy handle. */
1817         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1818                 return NT_STATUS_INVALID_HANDLE;
1819
1820         /* check to see if the pipe_user is root or a Domain Admin since
1821            account_pol.tdb was already opened as root, this is all we have */
1822
1823         if ( p->pipe_user.ut.uid != sec_initial_uid()
1824                 && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1825         {
1826                 return NT_STATUS_ACCESS_DENIED;
1827         }
1828
1829         set = r->in.privs;
1830         if ( !privilege_set_to_se_priv( &mask, set ) )
1831                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1832
1833         if ( !grant_privilege( &info->sid, &mask ) ) {
1834                 DEBUG(3,("_lsa_AddPrivilegesToAccount: grant_privilege(%s) failed!\n",
1835                          sid_string_dbg(&info->sid) ));
1836                 DEBUG(3,("Privilege mask:\n"));
1837                 dump_se_priv( DBGC_ALL, 3, &mask );
1838                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1839         }
1840
1841         return NT_STATUS_OK;
1842 }
1843
1844 /***************************************************************************
1845  _lsa_RemovePrivilegesFromAccount
1846  For a given SID, remove some privileges.
1847  ***************************************************************************/
1848
1849 NTSTATUS _lsa_RemovePrivilegesFromAccount(pipes_struct *p,
1850                                           struct lsa_RemovePrivilegesFromAccount *r)
1851 {
1852         struct lsa_info *info = NULL;
1853         SE_PRIV mask;
1854         struct lsa_PrivilegeSet *set = NULL;
1855
1856         /* find the connection policy handle. */
1857         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
1858                 return NT_STATUS_INVALID_HANDLE;
1859
1860         /* check to see if the pipe_user is root or a Domain Admin since
1861            account_pol.tdb was already opened as root, this is all we have */
1862
1863         if ( p->pipe_user.ut.uid != sec_initial_uid()
1864                 && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1865         {
1866                 return NT_STATUS_ACCESS_DENIED;
1867         }
1868
1869         set = r->in.privs;
1870
1871         if ( !privilege_set_to_se_priv( &mask, set ) )
1872                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1873
1874         if ( !revoke_privilege( &info->sid, &mask ) ) {
1875                 DEBUG(3,("_lsa_RemovePrivilegesFromAccount: revoke_privilege(%s) failed!\n",
1876                          sid_string_dbg(&info->sid) ));
1877                 DEBUG(3,("Privilege mask:\n"));
1878                 dump_se_priv( DBGC_ALL, 3, &mask );
1879                 return NT_STATUS_NO_SUCH_PRIVILEGE;
1880         }
1881
1882         return NT_STATUS_OK;
1883 }
1884
1885 /***************************************************************************
1886  _lsa_QuerySecurity
1887  ***************************************************************************/
1888
1889 NTSTATUS _lsa_QuerySecurity(pipes_struct *p,
1890                             struct lsa_QuerySecurity *r)
1891 {
1892         struct lsa_info *handle=NULL;
1893         SEC_DESC *psd = NULL;
1894         size_t sd_size;
1895         NTSTATUS status;
1896
1897         /* find the connection policy handle. */
1898         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&handle))
1899                 return NT_STATUS_INVALID_HANDLE;
1900
1901         /* check if the user have enough rights */
1902         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1903                 return NT_STATUS_ACCESS_DENIED;
1904
1905
1906         switch (r->in.sec_info) {
1907         case 1:
1908                 /* SD contains only the owner */
1909
1910                 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1911                 if(!NT_STATUS_IS_OK(status))
1912                         return NT_STATUS_NO_MEMORY;
1913
1914
1915                 if((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1916                         return NT_STATUS_NO_MEMORY;
1917                 break;
1918         case 4:
1919                 /* SD contains only the ACL */
1920
1921                 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1922                 if(!NT_STATUS_IS_OK(status))
1923                         return NT_STATUS_NO_MEMORY;
1924
1925                 if((*r->out.sdbuf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1926                         return NT_STATUS_NO_MEMORY;
1927                 break;
1928         default:
1929                 return NT_STATUS_INVALID_LEVEL;
1930         }
1931
1932         return status;
1933 }
1934
1935 #if 0   /* AD DC work in ongoing in Samba 4 */
1936
1937 /***************************************************************************
1938  ***************************************************************************/
1939
1940  NTSTATUS _lsa_query_info2(pipes_struct *p, LSA_Q_QUERY_INFO2 *q_u, LSA_R_QUERY_INFO2 *r_u)
1941 {
1942         struct lsa_info *handle;
1943         const char *nb_name;
1944         char *dns_name = NULL;
1945         char *forest_name = NULL;
1946         DOM_SID *sid = NULL;
1947         struct GUID guid;
1948         fstring dnsdomname;
1949
1950         ZERO_STRUCT(guid);
1951         r_u->status = NT_STATUS_OK;
1952
1953         if (!find_policy_by_hnd(p, &q_u->pol, (void **)(void *)&handle))
1954                 return NT_STATUS_INVALID_HANDLE;
1955
1956         switch (q_u->info_class) {
1957         case 0x0c:
1958                 /* check if the user have enough rights */
1959                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1960                         return NT_STATUS_ACCESS_DENIED;
1961
1962                 /* Request PolicyPrimaryDomainInformation. */
1963                 switch (lp_server_role()) {
1964                         case ROLE_DOMAIN_PDC:
1965                         case ROLE_DOMAIN_BDC:
1966                                 nb_name = get_global_sam_name();
1967                                 /* ugly temp hack for these next two */
1968
1969                                 /* This should be a 'netbios domain -> DNS domain' mapping */
1970                                 dnsdomname = get_mydnsdomname(p->mem_ctx);
1971                                 if (!dnsdomname || !*dnsdomname) {
1972                                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1973                                 }
1974                                 strlower_m(dnsdomname);
1975
1976                                 dns_name = dnsdomname;
1977                                 forest_name = dnsdomname;
1978
1979                                 sid = get_global_sam_sid();
1980                                 secrets_fetch_domain_guid(lp_workgroup(), &guid);
1981                                 break;
1982                         default:
1983                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1984                 }
1985                 init_dns_dom_info(&r_u->info.dns_dom_info, nb_name, dns_name,
1986                                   forest_name,&guid,sid);
1987                 break;
1988         default:
1989                 DEBUG(0,("_lsa_query_info2: unknown info level in Lsa Query: %d\n", q_u->info_class));
1990                 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
1991                 break;
1992         }
1993
1994         if (NT_STATUS_IS_OK(r_u->status)) {
1995                 r_u->ptr = 0x1;
1996                 r_u->info_class = q_u->info_class;
1997         }
1998
1999         return r_u->status;
2000 }
2001 #endif  /* AD DC work in ongoing in Samba 4 */
2002
2003 /***************************************************************************
2004  _lsa_AddAccountRights
2005  ***************************************************************************/
2006
2007 NTSTATUS _lsa_AddAccountRights(pipes_struct *p,
2008                                struct lsa_AddAccountRights *r)
2009 {
2010         struct lsa_info *info = NULL;
2011         int i = 0;
2012         DOM_SID sid;
2013
2014         /* find the connection policy handle. */
2015         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
2016                 return NT_STATUS_INVALID_HANDLE;
2017
2018         /* check to see if the pipe_user is a Domain Admin since
2019            account_pol.tdb was already opened as root, this is all we have */
2020
2021         if ( p->pipe_user.ut.uid != sec_initial_uid()
2022                 && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
2023         {
2024                 return NT_STATUS_ACCESS_DENIED;
2025         }
2026
2027         /* according to an NT4 PDC, you can add privileges to SIDs even without
2028            call_lsa_create_account() first.  And you can use any arbitrary SID. */
2029
2030         sid_copy( &sid, r->in.sid );
2031
2032         for ( i=0; i < r->in.rights->count; i++ ) {
2033
2034                 const char *privname = r->in.rights->names[i].string;
2035
2036                 /* only try to add non-null strings */
2037
2038                 if ( !privname )
2039                         continue;
2040
2041                 if ( !grant_privilege_by_name( &sid, privname ) ) {
2042                         DEBUG(2,("_lsa_AddAccountRights: Failed to add privilege [%s]\n",
2043                                 privname ));
2044                         return NT_STATUS_NO_SUCH_PRIVILEGE;
2045                 }
2046         }
2047
2048         return NT_STATUS_OK;
2049 }
2050
2051 /***************************************************************************
2052  _lsa_RemoveAccountRights
2053  ***************************************************************************/
2054
2055 NTSTATUS _lsa_RemoveAccountRights(pipes_struct *p,
2056                                   struct lsa_RemoveAccountRights *r)
2057 {
2058         struct lsa_info *info = NULL;
2059         int i = 0;
2060         DOM_SID sid;
2061         const char *privname = NULL;
2062
2063         /* find the connection policy handle. */
2064         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
2065                 return NT_STATUS_INVALID_HANDLE;
2066
2067         /* check to see if the pipe_user is a Domain Admin since
2068            account_pol.tdb was already opened as root, this is all we have */
2069
2070         if ( p->pipe_user.ut.uid != sec_initial_uid()
2071                 && !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
2072         {
2073                 return NT_STATUS_ACCESS_DENIED;
2074         }
2075
2076         sid_copy( &sid, r->in.sid );
2077
2078         if ( r->in.remove_all ) {
2079                 if ( !revoke_all_privileges( &sid ) )
2080                         return NT_STATUS_ACCESS_DENIED;
2081
2082                 return NT_STATUS_OK;
2083         }
2084
2085         for ( i=0; i < r->in.rights->count; i++ ) {
2086
2087                 privname = r->in.rights->names[i].string;
2088
2089                 /* only try to add non-null strings */
2090
2091                 if ( !privname )
2092                         continue;
2093
2094                 if ( !revoke_privilege_by_name( &sid, privname ) ) {
2095                         DEBUG(2,("_lsa_RemoveAccountRights: Failed to revoke privilege [%s]\n",
2096                                 privname ));
2097                         return NT_STATUS_NO_SUCH_PRIVILEGE;
2098                 }
2099         }
2100
2101         return NT_STATUS_OK;
2102 }
2103
2104 /*******************************************************************
2105 ********************************************************************/
2106
2107 static NTSTATUS init_lsa_right_set(TALLOC_CTX *mem_ctx,
2108                                    struct lsa_RightSet *r,
2109                                    PRIVILEGE_SET *privileges)
2110 {
2111         uint32 i;
2112         const char *privname;
2113         const char **privname_array = NULL;
2114         int num_priv = 0;
2115
2116         for (i=0; i<privileges->count; i++) {
2117
2118                 privname = luid_to_privilege_name(&privileges->set[i].luid);
2119                 if (privname) {
2120                         if (!add_string_to_array(mem_ctx, privname,
2121                                                  &privname_array, &num_priv)) {
2122                                 return NT_STATUS_NO_MEMORY;
2123                         }
2124                 }
2125         }
2126
2127         if (num_priv) {
2128
2129                 r->names = TALLOC_ZERO_ARRAY(mem_ctx, struct lsa_StringLarge,
2130                                              num_priv);
2131                 if (!r->names) {
2132                         return NT_STATUS_NO_MEMORY;
2133                 }
2134
2135                 for (i=0; i<num_priv; i++) {
2136                         init_lsa_StringLarge(&r->names[i], privname_array[i]);
2137                 }
2138
2139                 r->count = num_priv;
2140         }
2141
2142         return NT_STATUS_OK;
2143 }
2144
2145 /***************************************************************************
2146  _lsa_EnumAccountRights
2147  ***************************************************************************/
2148
2149 NTSTATUS _lsa_EnumAccountRights(pipes_struct *p,
2150                                 struct lsa_EnumAccountRights *r)
2151 {
2152         NTSTATUS status;
2153         struct lsa_info *info = NULL;
2154         DOM_SID sid;
2155         PRIVILEGE_SET privileges;
2156         SE_PRIV mask;
2157
2158         /* find the connection policy handle. */
2159
2160         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
2161                 return NT_STATUS_INVALID_HANDLE;
2162
2163         /* according to an NT4 PDC, you can add privileges to SIDs even without
2164            call_lsa_create_account() first.  And you can use any arbitrary SID. */
2165
2166         sid_copy( &sid, r->in.sid );
2167
2168         if ( !get_privileges_for_sids( &mask, &sid, 1 ) )
2169                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2170
2171         privilege_set_init( &privileges );
2172
2173         if ( se_priv_to_privilege_set( &privileges, &mask ) ) {
2174
2175                 DEBUG(10,("_lsa_EnumAccountRights: %s has %d privileges\n",
2176                           sid_string_dbg(&sid), privileges.count));
2177
2178                 status = init_lsa_right_set(p->mem_ctx, r->out.rights, &privileges);
2179         } else {
2180                 status = NT_STATUS_NO_SUCH_PRIVILEGE;
2181         }
2182
2183         privilege_set_free( &privileges );
2184
2185         return status;
2186 }
2187
2188 /***************************************************************************
2189  _lsa_LookupPrivValue
2190  ***************************************************************************/
2191
2192 NTSTATUS _lsa_LookupPrivValue(pipes_struct *p,
2193                               struct lsa_LookupPrivValue *r)
2194 {
2195         struct lsa_info *info = NULL;
2196         const char *name = NULL;
2197         LUID_ATTR priv_luid;
2198         SE_PRIV mask;
2199
2200         /* find the connection policy handle. */
2201
2202         if (!find_policy_by_hnd(p, r->in.handle, (void **)(void *)&info))
2203                 return NT_STATUS_INVALID_HANDLE;
2204
2205         name = r->in.name->string;
2206
2207         DEBUG(10,("_lsa_lookup_priv_value: name = %s\n", name));
2208
2209         if ( !se_priv_from_name( name, &mask ) )
2210                 return NT_STATUS_NO_SUCH_PRIVILEGE;
2211
2212         priv_luid = get_privilege_luid( &mask );
2213
2214         r->out.luid->low = priv_luid.luid.low;
2215         r->out.luid->high = priv_luid.luid.high;
2216
2217         return NT_STATUS_OK;
2218 }
2219
2220 /*
2221  * From here on the server routines are just dummy ones to make smbd link with
2222  * librpc/gen_ndr/srv_lsa.c. These routines are actually never called, we are
2223  * pulling the server stubs across one by one.
2224  */
2225
2226 NTSTATUS _lsa_Delete(pipes_struct *p, struct lsa_Delete *r)
2227 {
2228         p->rng_fault_state = True;
2229         return NT_STATUS_NOT_IMPLEMENTED;
2230 }
2231
2232 NTSTATUS _lsa_SetSecObj(pipes_struct *p, struct lsa_SetSecObj *r)
2233 {
2234         p->rng_fault_state = True;
2235         return NT_STATUS_NOT_IMPLEMENTED;
2236 }
2237
2238 NTSTATUS _lsa_ChangePassword(pipes_struct *p, struct lsa_ChangePassword *r)
2239 {
2240         p->rng_fault_state = True;
2241         return NT_STATUS_NOT_IMPLEMENTED;
2242 }
2243
2244 NTSTATUS _lsa_SetInfoPolicy(pipes_struct *p, struct lsa_SetInfoPolicy *r)
2245 {
2246         p->rng_fault_state = True;
2247         return NT_STATUS_NOT_IMPLEMENTED;
2248 }
2249
2250 NTSTATUS _lsa_ClearAuditLog(pipes_struct *p, struct lsa_ClearAuditLog *r)
2251 {
2252         p->rng_fault_state = True;
2253         return NT_STATUS_NOT_IMPLEMENTED;
2254 }
2255
2256 NTSTATUS _lsa_GetQuotasForAccount(pipes_struct *p, struct lsa_GetQuotasForAccount *r)
2257 {
2258         p->rng_fault_state = True;
2259         return NT_STATUS_NOT_IMPLEMENTED;
2260 }
2261
2262 NTSTATUS _lsa_SetQuotasForAccount(pipes_struct *p, struct lsa_SetQuotasForAccount *r)
2263 {
2264         p->rng_fault_state = True;
2265         return NT_STATUS_NOT_IMPLEMENTED;
2266 }
2267
2268 NTSTATUS _lsa_QueryTrustedDomainInfo(pipes_struct *p, struct lsa_QueryTrustedDomainInfo *r)
2269 {
2270         p->rng_fault_state = True;
2271         return NT_STATUS_NOT_IMPLEMENTED;
2272 }
2273
2274 NTSTATUS _lsa_SetInformationTrustedDomain(pipes_struct *p, struct lsa_SetInformationTrustedDomain *r)
2275 {
2276         p->rng_fault_state = True;
2277         return NT_STATUS_NOT_IMPLEMENTED;
2278 }
2279
2280 NTSTATUS _lsa_QuerySecret(pipes_struct *p, struct lsa_QuerySecret *r)
2281 {
2282         p->rng_fault_state = True;
2283         return NT_STATUS_NOT_IMPLEMENTED;
2284 }
2285
2286 NTSTATUS _lsa_LookupPrivName(pipes_struct *p, struct lsa_LookupPrivName *r)
2287 {
2288         p->rng_fault_state = True;
2289         return NT_STATUS_NOT_IMPLEMENTED;
2290 }
2291
2292 NTSTATUS _lsa_EnumAccountsWithUserRight(pipes_struct *p, struct lsa_EnumAccountsWithUserRight *r)
2293 {
2294         p->rng_fault_state = True;
2295         return NT_STATUS_NOT_IMPLEMENTED;
2296 }
2297
2298 NTSTATUS _lsa_QueryTrustedDomainInfoBySid(pipes_struct *p, struct lsa_QueryTrustedDomainInfoBySid *r)
2299 {
2300         p->rng_fault_state = True;
2301         return NT_STATUS_NOT_IMPLEMENTED;
2302 }
2303
2304 NTSTATUS _lsa_SetTrustedDomainInfo(pipes_struct *p, struct lsa_SetTrustedDomainInfo *r)
2305 {
2306         p->rng_fault_state = True;
2307         return NT_STATUS_NOT_IMPLEMENTED;
2308 }
2309
2310 NTSTATUS _lsa_DeleteTrustedDomain(pipes_struct *p, struct lsa_DeleteTrustedDomain *r)
2311 {
2312         p->rng_fault_state = True;
2313         return NT_STATUS_NOT_IMPLEMENTED;
2314 }
2315
2316 NTSTATUS _lsa_StorePrivateData(pipes_struct *p, struct lsa_StorePrivateData *r)
2317 {
2318         p->rng_fault_state = True;
2319         return NT_STATUS_NOT_IMPLEMENTED;
2320 }
2321
2322 NTSTATUS _lsa_RetrievePrivateData(pipes_struct *p, struct lsa_RetrievePrivateData *r)
2323 {
2324         p->rng_fault_state = True;
2325         return NT_STATUS_NOT_IMPLEMENTED;
2326 }
2327
2328 NTSTATUS _lsa_QueryInfoPolicy2(pipes_struct *p, struct lsa_QueryInfoPolicy2 *r)
2329 {
2330         p->rng_fault_state = True;
2331         return NT_STATUS_NOT_IMPLEMENTED;
2332 }
2333
2334 NTSTATUS _lsa_SetInfoPolicy2(pipes_struct *p, struct lsa_SetInfoPolicy2 *r)
2335 {
2336         p->rng_fault_state = True;
2337         return NT_STATUS_NOT_IMPLEMENTED;
2338 }
2339
2340 NTSTATUS _lsa_QueryTrustedDomainInfoByName(pipes_struct *p, struct lsa_QueryTrustedDomainInfoByName *r)
2341 {
2342         p->rng_fault_state = True;
2343         return NT_STATUS_NOT_IMPLEMENTED;
2344 }
2345
2346 NTSTATUS _lsa_SetTrustedDomainInfoByName(pipes_struct *p, struct lsa_SetTrustedDomainInfoByName *r)
2347 {
2348         p->rng_fault_state = True;
2349         return NT_STATUS_NOT_IMPLEMENTED;
2350 }
2351
2352 NTSTATUS _lsa_EnumTrustedDomainsEx(pipes_struct *p, struct lsa_EnumTrustedDomainsEx *r)
2353 {
2354         p->rng_fault_state = True;
2355         return NT_STATUS_NOT_IMPLEMENTED;
2356 }
2357
2358 NTSTATUS _lsa_CreateTrustedDomainEx(pipes_struct *p, struct lsa_CreateTrustedDomainEx *r)
2359 {
2360         p->rng_fault_state = True;
2361         return NT_STATUS_NOT_IMPLEMENTED;
2362 }
2363
2364 NTSTATUS _lsa_CloseTrustedDomainEx(pipes_struct *p, struct lsa_CloseTrustedDomainEx *r)
2365 {
2366         p->rng_fault_state = True;
2367         return NT_STATUS_NOT_IMPLEMENTED;
2368 }
2369
2370 NTSTATUS _lsa_QueryDomainInformationPolicy(pipes_struct *p, struct lsa_QueryDomainInformationPolicy *r)
2371 {
2372         p->rng_fault_state = True;
2373         return NT_STATUS_NOT_IMPLEMENTED;
2374 }
2375
2376 NTSTATUS _lsa_SetDomainInformationPolicy(pipes_struct *p, struct lsa_SetDomainInformationPolicy *r)
2377 {
2378         p->rng_fault_state = True;
2379         return NT_STATUS_NOT_IMPLEMENTED;
2380 }
2381
2382 NTSTATUS _lsa_OpenTrustedDomainByName(pipes_struct *p, struct lsa_OpenTrustedDomainByName *r)
2383 {
2384         p->rng_fault_state = True;
2385         return NT_STATUS_NOT_IMPLEMENTED;
2386 }
2387
2388 NTSTATUS _lsa_TestCall(pipes_struct *p, struct lsa_TestCall *r)
2389 {
2390         p->rng_fault_state = True;
2391         return NT_STATUS_NOT_IMPLEMENTED;
2392 }
2393
2394 NTSTATUS _lsa_CreateTrustedDomainEx2(pipes_struct *p, struct lsa_CreateTrustedDomainEx2 *r)
2395 {
2396         p->rng_fault_state = True;
2397         return NT_STATUS_NOT_IMPLEMENTED;
2398 }
2399
2400 NTSTATUS _lsa_CREDRWRITE(pipes_struct *p, struct lsa_CREDRWRITE *r)
2401 {
2402         p->rng_fault_state = True;
2403         return NT_STATUS_NOT_IMPLEMENTED;
2404 }
2405
2406 NTSTATUS _lsa_CREDRREAD(pipes_struct *p, struct lsa_CREDRREAD *r)
2407 {
2408         p->rng_fault_state = True;
2409         return NT_STATUS_NOT_IMPLEMENTED;
2410 }
2411
2412 NTSTATUS _lsa_CREDRENUMERATE(pipes_struct *p, struct lsa_CREDRENUMERATE *r)
2413 {
2414         p->rng_fault_state = True;
2415         return NT_STATUS_NOT_IMPLEMENTED;
2416 }
2417
2418 NTSTATUS _lsa_CREDRWRITEDOMAINCREDENTIALS(pipes_struct *p, struct lsa_CREDRWRITEDOMAINCREDENTIALS *r)
2419 {
2420         p->rng_fault_state = True;
2421         return NT_STATUS_NOT_IMPLEMENTED;
2422 }
2423
2424 NTSTATUS _lsa_CREDRREADDOMAINCREDENTIALS(pipes_struct *p, struct lsa_CREDRREADDOMAINCREDENTIALS *r)
2425 {
2426         p->rng_fault_state = True;
2427         return NT_STATUS_NOT_IMPLEMENTED;
2428 }
2429
2430 NTSTATUS _lsa_CREDRDELETE(pipes_struct *p, struct lsa_CREDRDELETE *r)
2431 {
2432         p->rng_fault_state = True;
2433         return NT_STATUS_NOT_IMPLEMENTED;
2434 }
2435
2436 NTSTATUS _lsa_CREDRGETTARGETINFO(pipes_struct *p, struct lsa_CREDRGETTARGETINFO *r)
2437 {
2438         p->rng_fault_state = True;
2439         return NT_STATUS_NOT_IMPLEMENTED;
2440 }
2441
2442 NTSTATUS _lsa_CREDRPROFILELOADED(pipes_struct *p, struct lsa_CREDRPROFILELOADED *r)
2443 {
2444         p->rng_fault_state = True;
2445         return NT_STATUS_NOT_IMPLEMENTED;
2446 }
2447
2448 NTSTATUS _lsa_CREDRGETSESSIONTYPES(pipes_struct *p, struct lsa_CREDRGETSESSIONTYPES *r)
2449 {
2450         p->rng_fault_state = True;
2451         return NT_STATUS_NOT_IMPLEMENTED;
2452 }
2453
2454 NTSTATUS _lsa_LSARREGISTERAUDITEVENT(pipes_struct *p, struct lsa_LSARREGISTERAUDITEVENT *r)
2455 {
2456         p->rng_fault_state = True;
2457         return NT_STATUS_NOT_IMPLEMENTED;
2458 }
2459
2460 NTSTATUS _lsa_LSARGENAUDITEVENT(pipes_struct *p, struct lsa_LSARGENAUDITEVENT *r)
2461 {
2462         p->rng_fault_state = True;
2463         return NT_STATUS_NOT_IMPLEMENTED;
2464 }
2465
2466 NTSTATUS _lsa_LSARUNREGISTERAUDITEVENT(pipes_struct *p, struct lsa_LSARUNREGISTERAUDITEVENT *r)
2467 {
2468         p->rng_fault_state = True;
2469         return NT_STATUS_NOT_IMPLEMENTED;
2470 }
2471
2472 NTSTATUS _lsa_lsaRQueryForestTrustInformation(pipes_struct *p, struct lsa_lsaRQueryForestTrustInformation *r)
2473 {
2474         p->rng_fault_state = True;
2475         return NT_STATUS_NOT_IMPLEMENTED;
2476 }
2477
2478 NTSTATUS _lsa_LSARSETFORESTTRUSTINFORMATION(pipes_struct *p, struct lsa_LSARSETFORESTTRUSTINFORMATION *r)
2479 {
2480         p->rng_fault_state = True;
2481         return NT_STATUS_NOT_IMPLEMENTED;
2482 }
2483
2484 NTSTATUS _lsa_CREDRRENAME(pipes_struct *p, struct lsa_CREDRRENAME *r)
2485 {
2486         p->rng_fault_state = True;
2487         return NT_STATUS_NOT_IMPLEMENTED;
2488 }
2489
2490 NTSTATUS _lsa_LSAROPENPOLICYSCE(pipes_struct *p, struct lsa_LSAROPENPOLICYSCE *r)
2491 {
2492         p->rng_fault_state = True;
2493         return NT_STATUS_NOT_IMPLEMENTED;
2494 }
2495
2496 NTSTATUS _lsa_LSARADTREGISTERSECURITYEVENTSOURCE(pipes_struct *p, struct lsa_LSARADTREGISTERSECURITYEVENTSOURCE *r)
2497 {
2498         p->rng_fault_state = True;
2499         return NT_STATUS_NOT_IMPLEMENTED;
2500 }
2501
2502 NTSTATUS _lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE(pipes_struct *p, struct lsa_LSARADTUNREGISTERSECURITYEVENTSOURCE *r)
2503 {
2504         p->rng_fault_state = True;
2505         return NT_STATUS_NOT_IMPLEMENTED;
2506 }
2507
2508 NTSTATUS _lsa_LSARADTREPORTSECURITYEVENT(pipes_struct *p, struct lsa_LSARADTREPORTSECURITYEVENT *r)
2509 {
2510         p->rng_fault_state = True;
2511         return NT_STATUS_NOT_IMPLEMENTED;
2512 }