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