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