Fix based on Volker's code to correctly return code from lsa_lookup_sids
[samba.git] / source / 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,
8  *  Copyright (C) Rafal Szczesniak                  2002,
9  *  Copyright (C) Jim McDonough <jmcd@us.ibm.com>   2002.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *  
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *  
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 /* This is the implementation of the lsa server code. */
27
28 #include "includes.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_RPC_SRV
32
33 extern PRIVS privs[];
34
35 struct lsa_info {
36         DOM_SID sid;
37         uint32 access;
38 };
39
40 struct generic_mapping lsa_generic_mapping = {
41         POLICY_READ,
42         POLICY_WRITE,
43         POLICY_EXECUTE,
44         POLICY_ALL_ACCESS
45 };
46
47 /*******************************************************************
48  Function to free the per handle data.
49  ********************************************************************/
50
51 static void free_lsa_info(void *ptr)
52 {
53         struct lsa_info *lsa = (struct lsa_info *)ptr;
54
55         SAFE_FREE(lsa);
56 }
57
58 /***************************************************************************
59 Init dom_query
60  ***************************************************************************/
61
62 static void init_dom_query(DOM_QUERY *d_q, const char *dom_name, DOM_SID *dom_sid)
63 {
64         d_q->buffer_dom_name = (dom_name != NULL) ? 1 : 0; /* domain buffer pointer */
65         d_q->buffer_dom_sid = (dom_sid != NULL) ? 1 : 0;  /* domain sid pointer */
66
67         /* this string is supposed to be non-null terminated. */
68         /* But the maxlen in this UNISTR2 must include the terminating null. */
69         init_unistr2(&d_q->uni_domain_name, dom_name, UNI_BROKEN_NON_NULL);
70
71         /*
72          * I'm not sure why this really odd combination of length
73          * values works, but it does appear to. I need to look at
74          * this *much* more closely - but at the moment leave alone
75          * until it's understood. This allows a W2k client to join
76          * a domain with both odd and even length names... JRA.
77          */
78
79         /*
80          * IMPORTANT NOTE !!!!
81          * The two fields below probably are reversed in meaning, ie.
82          * the first field is probably the str_len, the second the max
83          * len. Both are measured in bytes anyway.
84          */
85
86         d_q->uni_dom_str_len = d_q->uni_domain_name.uni_max_len * 2;
87         d_q->uni_dom_max_len = d_q->uni_domain_name.uni_str_len * 2;
88
89         if (dom_sid != NULL)
90                 init_dom_sid2(&d_q->dom_sid, dom_sid);
91 }
92
93 /***************************************************************************
94  init_dom_ref - adds a domain if it's not already in, returns the index.
95 ***************************************************************************/
96
97 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
98 {
99         int num = 0;
100
101         if (dom_name != NULL) {
102                 for (num = 0; num < ref->num_ref_doms_1; num++) {
103                         fstring domname;
104                         rpcstr_pull(domname, &ref->ref_dom[num].uni_dom_name, sizeof(domname), -1, 0);
105                         if (strequal(domname, dom_name))
106                                 return num;
107                 }
108         } else {
109                 num = ref->num_ref_doms_1;
110         }
111
112         if (num >= MAX_REF_DOMAINS) {
113                 /* index not found, already at maximum domain limit */
114                 return -1;
115         }
116
117         ref->num_ref_doms_1 = num+1;
118         ref->ptr_ref_dom  = 1;
119         ref->max_entries = MAX_REF_DOMAINS;
120         ref->num_ref_doms_2 = num+1;
121
122         ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
123
124         init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, UNI_FLAGS_NONE);
125         init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, &ref->ref_dom[num].uni_dom_name);
126
127         init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
128
129         return num;
130 }
131
132 /***************************************************************************
133  init_lsa_rid2s
134  ***************************************************************************/
135
136 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
137                                 int num_entries, UNISTR2 *name,
138                                 uint32 *mapped_count, BOOL endian)
139 {
140         int i;
141         int total = 0;
142         *mapped_count = 0;
143
144         SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
145
146         become_root(); /* lookup_name can require root privs */
147
148         for (i = 0; i < num_entries; i++) {
149                 BOOL status = False;
150                 DOM_SID sid;
151                 uint32 rid = 0xffffffff;
152                 int dom_idx = -1;
153                 pstring full_name;
154                 fstring dom_name, user;
155                 enum SID_NAME_USE name_type = SID_NAME_UNKNOWN;
156
157                 /* Split name into domain and user component */
158
159                 unistr2_to_ascii(full_name, &name[i], sizeof(full_name));
160                 split_domain_name(full_name, dom_name, user);
161
162                 /* Lookup name */
163
164                 DEBUG(5, ("init_lsa_rid2s: looking up name %s\n", full_name));
165
166                 status = lookup_name(dom_name, user, &sid, &name_type);
167
168                 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" : 
169                           "not found"));
170
171                 if (status && name_type != SID_NAME_UNKNOWN) {
172                         sid_split_rid(&sid, &rid);
173                         dom_idx = init_dom_ref(ref, dom_name, &sid);
174                         (*mapped_count)++;
175                 } else {
176                         dom_idx = -1;
177                         rid = 0xffffffff;
178                         name_type = SID_NAME_UNKNOWN;
179                 }
180
181                 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
182                 total++;
183         }
184
185         unbecome_root();
186 }
187
188 /***************************************************************************
189  init_reply_lookup_names
190  ***************************************************************************/
191
192 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
193                 DOM_R_REF *ref, uint32 num_entries,
194                 DOM_RID2 *rid2, uint32 mapped_count)
195 {
196         r_l->ptr_dom_ref  = 1;
197         r_l->dom_ref      = ref;
198
199         r_l->num_entries  = num_entries;
200         r_l->ptr_entries  = 1;
201         r_l->num_entries2 = num_entries;
202         r_l->dom_rid      = rid2;
203
204         r_l->mapped_count = mapped_count;
205
206         if (mapped_count == 0)
207                 r_l->status = NT_STATUS_NONE_MAPPED;
208         else
209                 r_l->status = NT_STATUS_OK;
210 }
211
212 /***************************************************************************
213  Init lsa_trans_names.
214  ***************************************************************************/
215
216 static void init_lsa_trans_names(TALLOC_CTX *ctx, DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
217                                  int num_entries, DOM_SID2 *sid,
218                                  uint32 *mapped_count)
219 {
220         int i;
221         int total = 0;
222         *mapped_count = 0;
223
224         /* Allocate memory for list of names */
225
226         if (num_entries > 0) {
227                 if (!(trn->name = (LSA_TRANS_NAME *)talloc(ctx, sizeof(LSA_TRANS_NAME) *
228                                                           num_entries))) {
229                         DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
230                         return;
231                 }
232
233                 if (!(trn->uni_name = (UNISTR2 *)talloc(ctx, sizeof(UNISTR2) * 
234                                                         num_entries))) {
235                         DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
236                         return;
237                 }
238         }
239
240         become_root(); /* Need root to get to passdb to for local sids */
241
242         for (i = 0; i < num_entries; i++) {
243                 BOOL status = False;
244                 DOM_SID find_sid = sid[i].sid;
245                 uint32 rid = 0xffffffff;
246                 int dom_idx = -1;
247                 fstring name, dom_name;
248                 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
249
250                 sid_to_string(name, &find_sid);
251                 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
252
253                 /* Lookup sid from winbindd */
254
255                 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
256
257                 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" : 
258                           "not found"));
259
260                 if (!status) {
261                         sid_name_use = SID_NAME_UNKNOWN;
262                         memset(dom_name, '\0', sizeof(dom_name));
263                         sid_to_string(name, &find_sid);
264                         dom_idx = -1;
265
266                         DEBUG(10,("init_lsa_trans_names: added unknown user '%s' to "
267                                   "referenced list.\n", name ));
268                 } else {
269                         (*mapped_count)++;
270                         /* Store domain sid in ref array */
271                         if (find_sid.num_auths == 5) {
272                                 sid_split_rid(&find_sid, &rid);
273                         }
274                         dom_idx = init_dom_ref(ref, dom_name, &find_sid);
275
276                         DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
277                                   "referenced list.\n", dom_name, name ));
278
279                 }
280
281                 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
282                                         sid_name_use, name, dom_idx);
283                 total++;
284         }
285
286         unbecome_root();
287
288         trn->num_entries = total;
289         trn->ptr_trans_names = 1;
290         trn->num_entries2 = total;
291 }
292
293 /***************************************************************************
294  Init_reply_lookup_sids.
295  ***************************************************************************/
296
297 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
298                 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
299                 uint32 mapped_count)
300 {
301         r_l->ptr_dom_ref  = 1;
302         r_l->dom_ref      = ref;
303         r_l->names        = names;
304         r_l->mapped_count = mapped_count;
305 }
306
307 static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size)
308 {
309         extern DOM_SID global_sid_World;
310         extern DOM_SID global_sid_Builtin;
311         DOM_SID local_adm_sid;
312         DOM_SID adm_sid;
313
314         SEC_ACE ace[3];
315         SEC_ACCESS mask;
316
317         SEC_ACL *psa = NULL;
318
319         init_sec_access(&mask, POLICY_EXECUTE);
320         init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
321
322         sid_copy(&adm_sid, get_global_sam_sid());
323         sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
324         init_sec_access(&mask, POLICY_ALL_ACCESS);
325         init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
326
327         sid_copy(&local_adm_sid, &global_sid_Builtin);
328         sid_append_rid(&local_adm_sid, BUILTIN_ALIAS_RID_ADMINS);
329         init_sec_access(&mask, POLICY_ALL_ACCESS);
330         init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
331
332         if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
333                 return NT_STATUS_NO_MEMORY;
334
335         if((*sd = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, &adm_sid, NULL, NULL, psa, sd_size)) == NULL)
336                 return NT_STATUS_NO_MEMORY;
337
338         return NT_STATUS_OK;
339 }
340
341 /***************************************************************************
342  Init_dns_dom_info.
343 ***************************************************************************/
344
345 static void init_dns_dom_info(LSA_DNS_DOM_INFO *r_l, const char *nb_name,
346                               const char *dns_name, const char *forest_name,
347                               struct uuid *dom_guid, DOM_SID *dom_sid)
348 {
349         if (nb_name && *nb_name) {
350                 init_unistr2(&r_l->uni_nb_dom_name, nb_name, UNI_FLAGS_NONE);
351                 init_uni_hdr(&r_l->hdr_nb_dom_name, &r_l->uni_nb_dom_name);
352                 r_l->hdr_nb_dom_name.uni_max_len += 2;
353                 r_l->uni_nb_dom_name.uni_max_len += 1;
354         }
355         
356         if (dns_name && *dns_name) {
357                 init_unistr2(&r_l->uni_dns_dom_name, dns_name, UNI_FLAGS_NONE);
358                 init_uni_hdr(&r_l->hdr_dns_dom_name, &r_l->uni_dns_dom_name);
359                 r_l->hdr_dns_dom_name.uni_max_len += 2;
360                 r_l->uni_dns_dom_name.uni_max_len += 1;
361         }
362
363         if (forest_name && *forest_name) {
364                 init_unistr2(&r_l->uni_forest_name, forest_name, UNI_FLAGS_NONE);
365                 init_uni_hdr(&r_l->hdr_forest_name, &r_l->uni_forest_name);
366                 r_l->hdr_forest_name.uni_max_len += 2;
367                 r_l->uni_forest_name.uni_max_len += 1;
368         }
369
370         /* how do we init the guid ? probably should write an init fn */
371         if (dom_guid) {
372                 memcpy(&r_l->dom_guid, dom_guid, sizeof(struct uuid));
373         }
374         
375         if (dom_sid) {
376                 r_l->ptr_dom_sid = 1;
377                 init_dom_sid2(&r_l->dom_sid, dom_sid);
378         }
379 }
380
381 /***************************************************************************
382  _lsa_open_policy2.
383  ***************************************************************************/
384
385 NTSTATUS _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
386 {
387         struct lsa_info *info;
388         SEC_DESC *psd = NULL;
389         size_t sd_size;
390         uint32 des_access=q_u->des_access;
391         uint32 acc_granted;
392         NTSTATUS status;
393
394
395         /* map the generic bits to the lsa policy ones */
396         se_map_generic(&des_access, &lsa_generic_mapping);
397
398         /* get the generic lsa policy SD until we store it */
399         lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
400
401         if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
402                 if (geteuid() != 0) {
403                         return status;
404                 }
405                 DEBUG(4,("ACCESS should be DENIED (granted: %#010x;  required: %#010x)\n",
406                          acc_granted, des_access));
407                 DEBUGADD(4,("but overwritten by euid == 0\n"));
408                 acc_granted = des_access;
409         }
410
411
412         /* associate the domain SID with the (unique) handle. */
413         if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
414                 return NT_STATUS_NO_MEMORY;
415
416         ZERO_STRUCTP(info);
417         sid_copy(&info->sid,get_global_sam_sid());
418         info->access = acc_granted;
419
420         /* set up the LSA QUERY INFO response */
421         if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
422                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
423
424         return NT_STATUS_OK;
425 }
426
427 /***************************************************************************
428  _lsa_open_policy
429  ***************************************************************************/
430
431 NTSTATUS _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
432 {
433         struct lsa_info *info;
434         SEC_DESC *psd = NULL;
435         size_t sd_size;
436         uint32 des_access=q_u->des_access;
437         uint32 acc_granted;
438         NTSTATUS status;
439
440
441         /* map the generic bits to the lsa policy ones */
442         se_map_generic(&des_access, &lsa_generic_mapping);
443
444         /* get the generic lsa policy SD until we store it */
445         lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
446
447         if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
448                 if (geteuid() != 0) {
449                         return status;
450                 }
451                 DEBUG(4,("ACCESS should be DENIED (granted: %#010x;  required: %#010x)\n",
452                          acc_granted, des_access));
453                 DEBUGADD(4,("but overwritten by euid == 0\n"));
454                 acc_granted = des_access;
455         }
456
457         /* associate the domain SID with the (unique) handle. */
458         if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
459                 return NT_STATUS_NO_MEMORY;
460
461         ZERO_STRUCTP(info);
462         sid_copy(&info->sid,get_global_sam_sid());
463         info->access = acc_granted;
464
465         /* set up the LSA QUERY INFO response */
466         if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
467                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
468
469         return NT_STATUS_OK;
470 }
471
472 /***************************************************************************
473  _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
474  ufff, done :)  mimir
475  ***************************************************************************/
476
477 NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
478 {
479         struct lsa_info *info;
480         uint32 enum_context = q_u->enum_context;
481
482         /*
483          * preferred length is set to 5 as a "our" preferred length
484          * nt sets this parameter to 2
485          * update (20.08.2002): it's not preferred length, but preferred size!
486          * it needs further investigation how to optimally choose this value
487          */
488         uint32 max_num_domains = q_u->preferred_len < 5 ? q_u->preferred_len : 10;
489         TRUSTDOM **trust_doms;
490         uint32 num_domains;
491         NTSTATUS nt_status;
492
493         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
494                 return NT_STATUS_INVALID_HANDLE;
495
496         /* check if the user have enough rights */
497         if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
498                 return NT_STATUS_ACCESS_DENIED;
499
500         nt_status = secrets_get_trusted_domains(p->mem_ctx, (int *)&enum_context, max_num_domains, (int *)&num_domains, &trust_doms);
501
502         if (!NT_STATUS_IS_OK(nt_status) &&
503             !NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES) &&
504             !NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
505                 return nt_status;
506         } else {
507                 r_u->status = nt_status;
508         }
509
510         /* set up the lsa_enum_trust_dom response */
511         init_r_enum_trust_dom(p->mem_ctx, r_u, enum_context, max_num_domains, num_domains, trust_doms);
512
513         return r_u->status;
514 }
515
516 /***************************************************************************
517  _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
518  ***************************************************************************/
519
520 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
521 {
522         struct lsa_info *handle;
523         LSA_INFO_UNION *info = &r_u->dom;
524         DOM_SID domain_sid;
525         const char *name;
526         DOM_SID *sid = NULL;
527
528         r_u->status = NT_STATUS_OK;
529
530         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
531                 return NT_STATUS_INVALID_HANDLE;
532
533         switch (q_u->info_class) {
534         case 0x02:
535                 {
536                 unsigned int i;
537                 /* check if the user have enough rights */
538                 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
539                         return NT_STATUS_ACCESS_DENIED;
540
541                 /* fake info: We audit everything. ;) */
542                 info->id2.auditing_enabled = 1;
543                 info->id2.count1 = 7;
544                 info->id2.count2 = 7;
545                 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
546                         return NT_STATUS_NO_MEMORY;
547                 for (i = 0; i < 7; i++)
548                         info->id2.auditsettings[i] = 3;
549                 break;
550                 }
551         case 0x03:
552                 /* check if the user have enough rights */
553                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
554                         return NT_STATUS_ACCESS_DENIED;
555
556                 /* Request PolicyPrimaryDomainInformation. */
557                 switch (lp_server_role()) {
558                         case ROLE_DOMAIN_PDC:
559                         case ROLE_DOMAIN_BDC:
560                                 name = get_global_sam_name();
561                                 sid = get_global_sam_sid();
562                                 break;
563                         case ROLE_DOMAIN_MEMBER:
564                                 name = lp_workgroup();
565                                 /* We need to return the Domain SID here. */
566                                 if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid))
567                                         sid = &domain_sid;
568                                 else
569                                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
570                                 break;
571                         case ROLE_STANDALONE:
572                                 name = lp_workgroup();
573                                 sid = NULL;
574                                 break;
575                         default:
576                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
577                 }
578                 init_dom_query(&r_u->dom.id3, name, sid);
579                 break;
580         case 0x05:
581                 /* check if the user have enough rights */
582                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
583                         return NT_STATUS_ACCESS_DENIED;
584
585                 /* Request PolicyAccountDomainInformation. */
586                 name = get_global_sam_name();
587                 sid = get_global_sam_sid();
588                 init_dom_query(&r_u->dom.id5, name, sid);
589                 break;
590         case 0x06:
591                 /* check if the user have enough rights */
592                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
593                         return NT_STATUS_ACCESS_DENIED;
594
595                 switch (lp_server_role()) {
596                         case ROLE_DOMAIN_BDC:
597                                 /*
598                                  * only a BDC is a backup controller
599                                  * of the domain, it controls.
600                                  */
601                                 info->id6.server_role = 2;
602                                 break;
603                         default:
604                                 /*
605                                  * any other role is a primary
606                                  * of the domain, it controls.
607                                  */
608                                 info->id6.server_role = 3;
609                                 break; 
610                 }
611                 break;
612         default:
613                 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
614                 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
615                 break;
616         }
617
618         if (NT_STATUS_IS_OK(r_u->status)) {
619                 r_u->undoc_buffer = 0x22000000; /* bizarre */
620                 r_u->info_class = q_u->info_class;
621         }
622
623         return r_u->status;
624 }
625
626 /***************************************************************************
627  _lsa_lookup_sids
628  ***************************************************************************/
629
630 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
631 {
632         struct lsa_info *handle;
633         DOM_SID2 *sid = q_u->sids.sid;
634         int num_entries = q_u->sids.num_entries;
635         DOM_R_REF *ref = NULL;
636         LSA_TRANS_NAME_ENUM *names = NULL;
637         uint32 mapped_count = 0;
638
639         if (num_entries >  MAX_LOOKUP_SIDS) {
640                 num_entries = MAX_LOOKUP_SIDS;
641                 DEBUG(5,("_lsa_lookup_sids: truncating SID lookup list to %d\n", num_entries));
642         }
643
644         ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
645         names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
646
647         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
648                 r_u->status = NT_STATUS_INVALID_HANDLE;
649                 goto done;
650         }
651
652         /* check if the user have enough rights */
653         if (!(handle->access & POLICY_LOOKUP_NAMES)) {
654                 r_u->status = NT_STATUS_ACCESS_DENIED;
655                 goto done;
656         }
657         if (!ref || !names)
658                 return NT_STATUS_NO_MEMORY;
659
660 done:
661
662         /* set up the LSA Lookup SIDs response */
663         init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
664         if (mapped_count == 0)
665                 r_u->status = NT_STATUS_NONE_MAPPED;
666         else if (mapped_count != num_entries)
667                 r_u->status = STATUS_SOME_UNMAPPED;
668         else
669                 r_u->status = NT_STATUS_OK;
670         init_reply_lookup_sids(r_u, ref, names, mapped_count);
671
672         return r_u->status;
673 }
674
675 /***************************************************************************
676 lsa_reply_lookup_names
677  ***************************************************************************/
678
679 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
680 {
681         struct lsa_info *handle;
682         UNISTR2 *names = q_u->uni_name;
683         int num_entries = q_u->num_entries;
684         DOM_R_REF *ref;
685         DOM_RID2 *rids;
686         uint32 mapped_count = 0;
687
688         if (num_entries >  MAX_LOOKUP_SIDS) {
689                 num_entries = MAX_LOOKUP_SIDS;
690                 DEBUG(5,("_lsa_lookup_names: truncating name lookup list to %d\n", num_entries));
691         }
692                 
693         ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
694         rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*num_entries);
695
696         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
697                 r_u->status = NT_STATUS_INVALID_HANDLE;
698                 goto done;
699         }
700
701         /* check if the user have enough rights */
702         if (!(handle->access & POLICY_LOOKUP_NAMES)) {
703                 r_u->status = NT_STATUS_ACCESS_DENIED;
704                 goto done;
705         }
706
707         if (!ref || !rids)
708                 return NT_STATUS_NO_MEMORY;
709
710 done:
711
712         /* set up the LSA Lookup RIDs response */
713         init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
714         init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
715
716         return r_u->status;
717 }
718
719 /***************************************************************************
720  _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
721  ***************************************************************************/
722
723 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
724 {
725         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
726                 return NT_STATUS_INVALID_HANDLE;
727
728         close_policy_hnd(p, &q_u->pol);
729         return NT_STATUS_OK;
730 }
731
732 /***************************************************************************
733   "No more secrets Marty...." :-).
734  ***************************************************************************/
735
736 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
737 {
738         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
739 }
740
741 /***************************************************************************
742 _lsa_enum_privs.
743  ***************************************************************************/
744
745 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
746 {
747         struct lsa_info *handle;
748         uint32 i;
749
750         uint32 enum_context=q_u->enum_context;
751         LSA_PRIV_ENTRY *entry;
752         LSA_PRIV_ENTRY *entries=NULL;
753
754         if (enum_context >= PRIV_ALL_INDEX)
755                 return NT_STATUS_NO_MORE_ENTRIES;
756
757         entries = (LSA_PRIV_ENTRY *)talloc_zero(p->mem_ctx, sizeof(LSA_PRIV_ENTRY) * (PRIV_ALL_INDEX));
758         if (entries==NULL)
759                 return NT_STATUS_NO_MEMORY;
760
761         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
762                 return NT_STATUS_INVALID_HANDLE;
763
764         /* check if the user have enough rights */
765
766         /*
767          * I don't know if it's the right one. not documented.
768          */
769         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
770                 return NT_STATUS_ACCESS_DENIED;
771
772         entry = entries;
773         
774         DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", enum_context, PRIV_ALL_INDEX));
775
776         for (i = 0; i < PRIV_ALL_INDEX; i++, entry++) {
777                 if( i<enum_context) {
778                         init_unistr2(&entry->name, NULL, UNI_FLAGS_NONE);
779                         init_uni_hdr(&entry->hdr_name, &entry->name);
780                         entry->luid_low = 0;
781                         entry->luid_high = 0;
782                 } else {
783                         init_unistr2(&entry->name, privs[i+1].priv, UNI_FLAGS_NONE);
784                         init_uni_hdr(&entry->hdr_name, &entry->name);
785                         entry->luid_low = privs[i+1].se_priv;
786                         entry->luid_high = 0;
787                 }
788         }
789
790         enum_context = PRIV_ALL_INDEX;
791         init_lsa_r_enum_privs(r_u, enum_context, PRIV_ALL_INDEX, entries);
792
793         return NT_STATUS_OK;
794 }
795
796 /***************************************************************************
797 _lsa_priv_get_dispname.
798  ***************************************************************************/
799
800 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
801 {
802         struct lsa_info *handle;
803         fstring name_asc;
804         int i=1;
805
806         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
807                 return NT_STATUS_INVALID_HANDLE;
808
809         /* check if the user have enough rights */
810
811         /*
812          * I don't know if it's the right one. not documented.
813          */
814         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
815                 return NT_STATUS_ACCESS_DENIED;
816
817         unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
818
819         DEBUG(10,("_lsa_priv_get_dispname: %s", name_asc));
820
821         while (privs[i].se_priv!=SE_PRIV_ALL && strcmp(name_asc, privs[i].priv))
822                 i++;
823         
824         if (privs[i].se_priv!=SE_PRIV_ALL) {
825                 DEBUG(10,(": %s\n", privs[i].description));
826                 init_unistr2(&r_u->desc, privs[i].description, UNI_FLAGS_NONE);
827                 init_uni_hdr(&r_u->hdr_desc, &r_u->desc);
828
829                 r_u->ptr_info=0xdeadbeef;
830                 r_u->lang_id=q_u->lang_id;
831                 return NT_STATUS_OK;
832         } else {
833                 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
834                 r_u->ptr_info=0;
835                 return NT_STATUS_NO_SUCH_PRIVILEGE;
836         }
837 }
838
839 /***************************************************************************
840 _lsa_enum_accounts.
841  ***************************************************************************/
842
843 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
844 {
845         struct lsa_info *handle;
846         GROUP_MAP *map=NULL;
847         int num_entries=0;
848         LSA_SID_ENUM *sids=&r_u->sids;
849         int i=0,j=0;
850         BOOL ret;
851
852         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
853                 return NT_STATUS_INVALID_HANDLE;
854
855         /* check if the user have enough rights */
856
857         /*
858          * I don't know if it's the right one. not documented.
859          */
860         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
861                 return NT_STATUS_ACCESS_DENIED;
862
863         /* get the list of mapped groups (domain, local, builtin) */
864         become_root();
865         ret = pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, ENUM_ONLY_MAPPED);
866         unbecome_root();
867         if( !ret ) {
868                 DEBUG(3,("_lsa_enum_accounts: enumeration of groups failed!\n"));
869                 return NT_STATUS_OK;
870         }
871         
872
873         if (q_u->enum_context >= num_entries)
874                 return NT_STATUS_NO_MORE_ENTRIES;
875
876         sids->ptr_sid = (uint32 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(uint32));
877         sids->sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(DOM_SID2));
878
879         if (sids->ptr_sid==NULL || sids->sid==NULL) {
880                 SAFE_FREE(map);
881                 return NT_STATUS_NO_MEMORY;
882         }
883
884         for (i=q_u->enum_context, j=0; i<num_entries; i++) {
885                 init_dom_sid2( &(*sids).sid[j],  &map[i].sid);
886                 (*sids).ptr_sid[j]=1;
887                 j++;
888         }
889
890         SAFE_FREE(map);
891
892         init_lsa_r_enum_accounts(r_u, j);
893
894         return NT_STATUS_OK;
895 }
896
897
898 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
899 {
900         fstring username, domname;
901         user_struct *vuser = get_valid_user_struct(p->vuid);
902   
903         if (vuser == NULL)
904                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
905   
906         fstrcpy(username, vuser->user.smb_name);
907         fstrcpy(domname, vuser->user.domain);
908   
909         r_u->ptr_user_name = 1;
910         init_unistr2(&r_u->uni2_user_name, username, UNI_STR_TERMINATE);
911         init_uni_hdr(&r_u->hdr_user_name, &r_u->uni2_user_name);
912
913         r_u->unk1 = 1;
914   
915         r_u->ptr_dom_name = 1;
916         init_unistr2(&r_u->uni2_dom_name, domname,  UNI_STR_TERMINATE);
917         init_uni_hdr(&r_u->hdr_dom_name, &r_u->uni2_dom_name);
918
919         r_u->status = NT_STATUS_OK;
920   
921         return r_u->status;
922 }
923
924 /***************************************************************************
925  
926  ***************************************************************************/
927
928 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
929 {
930         struct lsa_info *handle;
931         struct lsa_info *info;
932
933         r_u->status = NT_STATUS_OK;
934
935         /* find the connection policy handle. */
936         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
937                 return NT_STATUS_INVALID_HANDLE;
938
939         /* check if the user have enough rights */
940
941         /*
942          * I don't know if it's the right one. not documented.
943          * but guessed with rpcclient.
944          */
945         if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
946                 return NT_STATUS_ACCESS_DENIED;
947
948         /* associate the user/group SID with the (unique) handle. */
949         if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
950                 return NT_STATUS_NO_MEMORY;
951
952         ZERO_STRUCTP(info);
953         info->sid = q_u->sid.sid;
954         info->access = q_u->access;
955
956         /* get a (unique) handle.  open a policy on it. */
957         if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
958                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
959
960         return r_u->status;
961 }
962
963 /***************************************************************************
964  For a given SID, enumerate all the privilege this account has.
965  ***************************************************************************/
966
967 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, prs_struct *ps, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
968 {
969         struct lsa_info *info=NULL;
970         GROUP_MAP map;
971         LUID_ATTR *set=NULL;
972
973         r_u->status = NT_STATUS_OK;
974
975         /* find the connection policy handle. */
976         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
977                 return NT_STATUS_INVALID_HANDLE;
978
979         if (!pdb_getgrsid(&map, info->sid))
980                 return NT_STATUS_NO_SUCH_GROUP;
981
982 #if 0 /* privileges currently not implemented! */
983         DEBUG(10,("_lsa_enum_privsaccount: %d privileges\n", map.priv_set->count));
984         if (map.priv_set->count!=0) {
985         
986                 set=(LUID_ATTR *)talloc(map.priv_set->mem_ctx, map.priv_set.count*sizeof(LUID_ATTR));
987                 if (set == NULL) {
988                         destroy_privilege(&map.priv_set);
989                         return NT_STATUS_NO_MEMORY;
990                 }
991
992                 for (i = 0; i < map.priv_set.count; i++) {
993                         set[i].luid.low = map.priv_set->set[i].luid.low;
994                         set[i].luid.high = map.priv_set->set[i].luid.high;
995                         set[i].attr = map.priv_set->set[i].attr;
996                         DEBUG(10,("_lsa_enum_privsaccount: priv %d: %d:%d:%d\n", i, 
997                                    set[i].luid.high, set[i].luid.low, set[i].attr));
998                 }
999         }
1000
1001         init_lsa_r_enum_privsaccount(ps->mem_ctx, r_u, set, map.priv_set->count, 0);    
1002         destroy_privilege(&map.priv_set);       
1003 #endif
1004
1005         init_lsa_r_enum_privsaccount(ps->mem_ctx, r_u, set, 0, 0);
1006
1007         return r_u->status;
1008 }
1009
1010 /***************************************************************************
1011  
1012  ***************************************************************************/
1013
1014 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
1015 {
1016         struct lsa_info *info=NULL;
1017         GROUP_MAP map;
1018         r_u->status = NT_STATUS_OK;
1019
1020         /* find the connection policy handle. */
1021         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1022                 return NT_STATUS_INVALID_HANDLE;
1023
1024         if (!pdb_getgrsid(&map, info->sid))
1025                 return NT_STATUS_NO_SUCH_GROUP;
1026
1027         /*
1028           0x01 -> Log on locally
1029           0x02 -> Access this computer from network
1030           0x04 -> Log on as a batch job
1031           0x10 -> Log on as a service
1032           
1033           they can be ORed together
1034         */
1035
1036         r_u->access = PR_LOG_ON_LOCALLY | PR_ACCESS_FROM_NETWORK;
1037
1038         return r_u->status;
1039 }
1040
1041 /***************************************************************************
1042   update the systemaccount information
1043  ***************************************************************************/
1044
1045 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
1046 {
1047         struct lsa_info *info=NULL;
1048         GROUP_MAP map;
1049         r_u->status = NT_STATUS_OK;
1050
1051         /* find the connection policy handle. */
1052         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1053                 return NT_STATUS_INVALID_HANDLE;
1054
1055         if (!pdb_getgrsid(&map, info->sid))
1056                 return NT_STATUS_NO_SUCH_GROUP;
1057
1058         if(!pdb_update_group_mapping_entry(&map))
1059                 return NT_STATUS_NO_SUCH_GROUP;
1060
1061         return r_u->status;
1062 }
1063
1064 /***************************************************************************
1065  For a given SID, add some privileges.
1066  ***************************************************************************/
1067
1068 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
1069 {
1070 #if 0
1071         struct lsa_info *info = NULL;
1072         GROUP_MAP map;
1073         int i = 0;
1074         LUID_ATTR *luid_attr = NULL;
1075         PRIVILEGE_SET *set = NULL;
1076 #endif
1077
1078         r_u->status = NT_STATUS_OK;
1079
1080 #if 0 /* privileges are not implemented */
1081         /* find the connection policy handle. */
1082         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1083                 return NT_STATUS_INVALID_HANDLE;
1084
1085         if (!pdb_getgrsid(&map, info->sid))
1086                 return NT_STATUS_NO_SUCH_GROUP;
1087
1088         set = &q_u->set;
1089
1090         for (i = 0; i < set->count; i++) {
1091                 luid_attr = &set->set[i];
1092                 
1093                 /* check if the privilege is already there */
1094                 if (check_priv_in_privilege(map.priv_set, *luid_attr)){
1095                         destroy_privilege(&map.priv_set);
1096                         return NT_STATUS_NO_SUCH_PRIVILEGE;
1097                 }
1098                 
1099                 add_privilege(map.priv_set, *luid_attr);
1100         }
1101
1102         if(!pdb_update_group_mapping_entry(&map))
1103                 return NT_STATUS_NO_SUCH_GROUP;
1104         
1105         destroy_privilege(&map.priv_set);       
1106
1107 #endif
1108         return r_u->status;
1109 }
1110
1111 /***************************************************************************
1112  For a given SID, remove some privileges.
1113  ***************************************************************************/
1114
1115 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1116 {
1117 #if 0
1118         struct lsa_info *info = NULL;
1119         GROUP_MAP map;
1120         int i=0;
1121         LUID_ATTR *luid_attr = NULL;
1122         PRIVILEGE_SET *set = NULL;
1123 #endif
1124
1125         r_u->status = NT_STATUS_OK;
1126
1127 #if 0 /* privileges are not implemented */
1128         /* find the connection policy handle. */
1129         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1130                 return NT_STATUS_INVALID_HANDLE;
1131
1132         if (!pdb_getgrsid(&map, info->sid))
1133                 return NT_STATUS_NO_SUCH_GROUP;
1134
1135         if (q_u->allrights != 0) {
1136                 /* log it and return, until I see one myself don't do anything */
1137                 DEBUG(5,("_lsa_removeprivs: trying to remove all privileges ?\n"));
1138                 return NT_STATUS_OK;
1139         }
1140
1141         if (q_u->ptr == 0) {
1142                 /* log it and return, until I see one myself don't do anything */
1143                 DEBUG(5,("_lsa_removeprivs: no privileges to remove ?\n"));
1144                 return NT_STATUS_OK;
1145         }
1146
1147         set = &q_u->set;
1148
1149         for (i = 0; i < set->count; i++) {
1150                 luid_attr = &set->set[i];
1151                 
1152                 /* if we don't have the privilege, we're trying to remove, give up */
1153                 /* what else can we do ??? JFM. */
1154                 if (!check_priv_in_privilege(map.priv_set, *luid_attr)){
1155                         destroy_privilege(&map.priv_set);
1156                         return NT_STATUS_NO_SUCH_PRIVILEGE;
1157                 }
1158                 
1159                 remove_privilege(map.priv_set, *luid_attr);
1160         }
1161
1162         if(!pdb_update_group_mapping_entry(&map))
1163                 return NT_STATUS_NO_SUCH_GROUP;
1164         
1165         destroy_privilege(&map.priv_set);       
1166 #endif
1167         return r_u->status;
1168 }
1169
1170 /***************************************************************************
1171  For a given SID, remove some privileges.
1172  ***************************************************************************/
1173
1174 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1175 {
1176         struct lsa_info *handle=NULL;
1177         SEC_DESC *psd = NULL;
1178         size_t sd_size;
1179         NTSTATUS status;
1180
1181         r_u->status = NT_STATUS_OK;
1182
1183         /* find the connection policy handle. */
1184         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1185                 return NT_STATUS_INVALID_HANDLE;
1186
1187         /* check if the user have enough rights */
1188         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1189                 return NT_STATUS_ACCESS_DENIED;
1190
1191
1192         switch (q_u->sec_info) {
1193         case 1:
1194                 /* SD contains only the owner */
1195
1196                 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1197                 if(!NT_STATUS_IS_OK(status))
1198                         return NT_STATUS_NO_MEMORY;
1199
1200
1201                 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1202                         return NT_STATUS_NO_MEMORY;
1203                 break;
1204         case 4:
1205                 /* SD contains only the ACL */
1206
1207                 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1208                 if(!NT_STATUS_IS_OK(status))
1209                         return NT_STATUS_NO_MEMORY;
1210
1211                 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1212                         return NT_STATUS_NO_MEMORY;
1213                 break;
1214         default:
1215                 return NT_STATUS_INVALID_LEVEL;
1216         }
1217
1218         r_u->ptr=1;
1219
1220         return r_u->status;
1221 }
1222
1223
1224 NTSTATUS _lsa_query_info2(pipes_struct *p, LSA_Q_QUERY_INFO2 *q_u, LSA_R_QUERY_INFO2 *r_u)
1225 {
1226         struct lsa_info *handle;
1227         const char *nb_name;
1228         char *dns_name = NULL;
1229         char *forest_name = NULL;
1230         DOM_SID *sid = NULL;
1231         struct uuid guid;
1232         fstring dnsdomname;
1233
1234         ZERO_STRUCT(guid);
1235         r_u->status = NT_STATUS_OK;
1236
1237         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1238                 return NT_STATUS_INVALID_HANDLE;
1239
1240         switch (q_u->info_class) {
1241         case 0x0c:
1242                 /* check if the user have enough rights */
1243                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1244                         return NT_STATUS_ACCESS_DENIED;
1245
1246                 /* Request PolicyPrimaryDomainInformation. */
1247                 switch (lp_server_role()) {
1248                         case ROLE_DOMAIN_PDC:
1249                         case ROLE_DOMAIN_BDC:
1250                                 nb_name = get_global_sam_name();
1251                                 /* ugly temp hack for these next two */
1252
1253                                 /* This should be a 'netbios domain -> DNS domain' mapping */
1254                                 dnsdomname[0] = '\0';
1255                                 get_mydnsdomname(dnsdomname);
1256                                 strlower_m(dnsdomname);
1257                                 
1258                                 dns_name = dnsdomname;
1259                                 forest_name = dnsdomname;
1260
1261                                 sid = get_global_sam_sid();
1262                                 secrets_fetch_domain_guid(lp_workgroup(), &guid);
1263                                 break;
1264                         default:
1265                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1266                 }
1267                 init_dns_dom_info(&r_u->info.dns_dom_info, nb_name, dns_name, 
1268                                   forest_name,&guid,sid);
1269                 break;
1270         default:
1271                 DEBUG(0,("_lsa_query_info2: unknown info level in Lsa Query: %d\n", q_u->info_class));
1272                 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
1273                 break;
1274         }
1275
1276         if (NT_STATUS_IS_OK(r_u->status)) {
1277                 r_u->ptr = 0x1;
1278                 r_u->info_class = q_u->info_class;
1279         }
1280
1281         return r_u->status;
1282 }