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