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