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