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