- portablitity fixes for cc -64 on irix
[nivanova/samba-autobuild/.git] / source / 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         extern DOM_SID global_sid_nonexistent;
442         struct lsa_info *handle;
443         LSA_INFO_UNION *info = &r_u->dom;
444         DOM_SID domain_sid;
445         char *name = NULL;
446         DOM_SID *sid = NULL;
447
448         r_u->status = NT_STATUS_OK;
449
450         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
451                 return NT_STATUS_INVALID_HANDLE;
452
453         switch (q_u->info_class) {
454         case 0x02:
455                 {
456                 unsigned int i;
457                 /* check if the user have enough rights */
458                 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
459                         return NT_STATUS_ACCESS_DENIED;
460
461                 /* fake info: We audit everything. ;) */
462                 info->id2.auditing_enabled = 1;
463                 info->id2.count1 = 7;
464                 info->id2.count2 = 7;
465                 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
466                         return NT_STATUS_NO_MEMORY;
467                 for (i = 0; i < 7; i++)
468                         info->id2.auditsettings[i] = 3;
469                 break;
470                 }
471         case 0x03:
472                 /* check if the user have enough rights */
473                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
474                         return NT_STATUS_ACCESS_DENIED;
475
476                 /* Request PolicyPrimaryDomainInformation. */
477                 switch (lp_server_role()) {
478                         case ROLE_DOMAIN_PDC:
479                         case ROLE_DOMAIN_BDC:
480                                 name = global_myworkgroup;
481                                 sid = &global_sam_sid;
482                                 break;
483                         case ROLE_DOMAIN_MEMBER:
484                                 name = global_myworkgroup;
485                                 /* We need to return the Domain SID here. */
486                                 if (secrets_fetch_domain_sid(global_myworkgroup,
487                                                              &domain_sid))
488                                         sid = &domain_sid;
489                                 else
490                                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
491                                 break;
492                         case ROLE_STANDALONE:
493                                 name = global_myworkgroup;
494                                 sid = &global_sid_nonexistent;
495                                 break;
496                         default:
497                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
498                 }
499                 init_dom_query(&r_u->dom.id3, name, sid);
500                 break;
501         case 0x05:
502                 /* check if the user have enough rights */
503                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
504                         return NT_STATUS_ACCESS_DENIED;
505
506                 /* Request PolicyAccountDomainInformation. */
507                 switch (lp_server_role()) {
508                         case ROLE_DOMAIN_PDC:
509                         case ROLE_DOMAIN_BDC:
510                                 name = global_myworkgroup;
511                                 sid = &global_sam_sid;
512                                 break;
513                         case ROLE_DOMAIN_MEMBER:
514                                 name = global_myname;
515                                 sid = &global_sam_sid;
516                                 break;
517                         case ROLE_STANDALONE:
518                                 name = global_myname;
519                                 sid = &global_sam_sid;
520                                 break;
521                         default:
522                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
523                 }
524                 init_dom_query(&r_u->dom.id5, name, sid);
525                 break;
526         case 0x06:
527                 /* check if the user have enough rights */
528                 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
529                         return NT_STATUS_ACCESS_DENIED;
530
531                 switch (lp_server_role()) {
532                         case ROLE_DOMAIN_BDC:
533                                 /*
534                                  * only a BDC is a backup controller
535                                  * of the domain, it controls.
536                                  */
537                                 info->id6.server_role = 2;
538                                 break;
539                         default:
540                                 /*
541                                  * any other role is a primary
542                                  * of the domain, it controls.
543                                  */
544                                 info->id6.server_role = 3;
545                                 break; 
546                 }
547                 break;
548         default:
549                 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
550                 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
551                 break;
552         }
553
554         if (NT_STATUS_IS_OK(r_u->status)) {
555                 r_u->undoc_buffer = 0x22000000; /* bizarre */
556                 r_u->info_class = q_u->info_class;
557         }
558
559         return r_u->status;
560 }
561
562 /***************************************************************************
563  _lsa_lookup_sids
564  ***************************************************************************/
565
566 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
567 {
568         struct lsa_info *handle;
569         DOM_SID2 *sid = q_u->sids.sid;
570         int num_entries = q_u->sids.num_entries;
571         DOM_R_REF *ref = NULL;
572         LSA_TRANS_NAME_ENUM *names = NULL;
573         uint32 mapped_count = 0;
574
575         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
576                 return NT_STATUS_INVALID_HANDLE;
577
578         /* check if the user have enough rights */
579         if (!(handle->access & POLICY_LOOKUP_NAMES))
580                 return NT_STATUS_ACCESS_DENIED;
581
582         ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
583         names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
584
585         if (!ref || !names)
586                 return NT_STATUS_NO_MEMORY;
587
588         /* set up the LSA Lookup SIDs response */
589         init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
590         init_reply_lookup_sids(r_u, ref, names, mapped_count);
591
592         return r_u->status;
593 }
594
595 /***************************************************************************
596 lsa_reply_lookup_names
597  ***************************************************************************/
598
599 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
600 {
601         struct lsa_info *handle;
602         UNISTR2 *names = q_u->uni_name;
603         int num_entries = q_u->num_entries;
604         DOM_R_REF *ref;
605         DOM_RID2 *rids;
606         uint32 mapped_count = 0;
607
608         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
609                 return NT_STATUS_INVALID_HANDLE;
610
611         /* check if the user have enough rights */
612         if (!(handle->access & POLICY_LOOKUP_NAMES))
613                 return NT_STATUS_ACCESS_DENIED;
614
615         ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
616         rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*MAX_LOOKUP_SIDS);
617
618         if (!ref || !rids)
619                 return NT_STATUS_NO_MEMORY;
620
621         /* set up the LSA Lookup RIDs response */
622         init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
623         init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
624
625         return r_u->status;
626 }
627
628 /***************************************************************************
629  _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
630  ***************************************************************************/
631
632 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
633 {
634         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
635                 return NT_STATUS_INVALID_HANDLE;
636
637         close_policy_hnd(p, &q_u->pol);
638         return NT_STATUS_OK;
639 }
640
641 /***************************************************************************
642   "No more secrets Marty...." :-).
643  ***************************************************************************/
644
645 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
646 {
647         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
648 }
649
650 /***************************************************************************
651 _lsa_enum_privs.
652  ***************************************************************************/
653
654 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
655 {
656         struct lsa_info *handle;
657         uint32 i;
658
659         uint32 enum_context=q_u->enum_context;
660         LSA_PRIV_ENTRY *entry;
661         LSA_PRIV_ENTRY *entries=NULL;
662
663         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
664                 return NT_STATUS_INVALID_HANDLE;
665
666         /* check if the user have enough rights */
667
668         /*
669          * I don't know if it's the right one. not documented.
670          */
671         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
672                 return NT_STATUS_ACCESS_DENIED;
673
674         if (enum_context >= PRIV_ALL_INDEX)
675                 return NT_STATUS_NO_MORE_ENTRIES;
676
677         entries = (LSA_PRIV_ENTRY *)talloc_zero(p->mem_ctx, sizeof(LSA_PRIV_ENTRY) * (PRIV_ALL_INDEX));
678         if (entries==NULL)
679                 return NT_STATUS_NO_MEMORY;
680
681         entry = entries;
682         
683         DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", enum_context, PRIV_ALL_INDEX));
684
685         for (i = 0; i < PRIV_ALL_INDEX; i++, entry++) {
686                 if( i<enum_context) {
687                         init_uni_hdr(&entry->hdr_name, 0);
688                         init_unistr2(&entry->name, NULL, 0 );
689                         entry->luid_low = 0;
690                         entry->luid_high = 0;
691                 } else {
692                         init_uni_hdr(&entry->hdr_name, strlen(privs[i+1].priv));
693                         init_unistr2(&entry->name, privs[i+1].priv, strlen(privs[i+1].priv) );
694                         entry->luid_low = privs[i+1].se_priv;
695                         entry->luid_high = 0;
696                 }
697         }
698
699         enum_context = PRIV_ALL_INDEX;
700         init_lsa_r_enum_privs(r_u, enum_context, PRIV_ALL_INDEX, entries);
701
702         return NT_STATUS_OK;
703 }
704
705 /***************************************************************************
706 _lsa_priv_get_dispname.
707  ***************************************************************************/
708
709 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
710 {
711         struct lsa_info *handle;
712         fstring name_asc;
713         int i=1;
714
715         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
716                 return NT_STATUS_INVALID_HANDLE;
717
718         /* check if the user have enough rights */
719
720         /*
721          * I don't know if it's the right one. not documented.
722          */
723         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
724                 return NT_STATUS_ACCESS_DENIED;
725
726         unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
727
728         DEBUG(10,("_lsa_priv_get_dispname: %s", name_asc));
729
730         while (privs[i].se_priv!=SE_PRIV_ALL && strcmp(name_asc, privs[i].priv))
731                 i++;
732         
733         if (privs[i].se_priv!=SE_PRIV_ALL) {
734                 DEBUG(10,(": %s\n", privs[i].description));
735                 init_uni_hdr(&r_u->hdr_desc, strlen(privs[i].description));
736                 init_unistr2(&r_u->desc, privs[i].description, strlen(privs[i].description) );
737
738                 r_u->ptr_info=0xdeadbeef;
739                 r_u->lang_id=q_u->lang_id;
740                 return NT_STATUS_OK;
741         } else {
742                 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
743                 r_u->ptr_info=0;
744                 return NT_STATUS_NO_SUCH_PRIVILEGE;
745         }
746 }
747
748 /***************************************************************************
749 _lsa_enum_accounts.
750  ***************************************************************************/
751
752 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
753 {
754         struct lsa_info *handle;
755         GROUP_MAP *map=NULL;
756         int num_entries=0;
757         LSA_SID_ENUM *sids=&r_u->sids;
758         int i=0,j=0;
759
760         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
761                 return NT_STATUS_INVALID_HANDLE;
762
763         /* check if the user have enough rights */
764
765         /*
766          * I don't know if it's the right one. not documented.
767          */
768         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
769                 return NT_STATUS_ACCESS_DENIED;
770
771         /* get the list of mapped groups (domain, local, builtin) */
772         if(!enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
773                 return NT_STATUS_OK;
774
775         if (q_u->enum_context >= num_entries)
776                 return NT_STATUS_NO_MORE_ENTRIES;
777
778         sids->ptr_sid = (uint32 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(uint32));
779         sids->sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(DOM_SID2));
780
781         if (sids->ptr_sid==NULL || sids->sid==NULL) {
782                 SAFE_FREE(map);
783                 return NT_STATUS_NO_MEMORY;
784         }
785
786         for (i=q_u->enum_context, j=0; i<num_entries; i++) {
787                 init_dom_sid2( &(*sids).sid[j],  &map[i].sid);
788                 (*sids).ptr_sid[j]=1;
789                 j++;
790         }
791
792         SAFE_FREE(map);
793
794         init_lsa_r_enum_accounts(r_u, j);
795
796         return NT_STATUS_OK;
797 }
798
799
800 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
801 {
802         fstring username, domname;
803         int ulen, dlen;
804         user_struct *vuser = get_valid_user_struct(p->vuid);
805   
806         if (vuser == NULL)
807                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
808   
809         fstrcpy(username, vuser->user.smb_name);
810         fstrcpy(domname, vuser->user.domain);
811   
812         ulen = strlen(username) + 1;
813         dlen = strlen(domname) + 1;
814   
815         init_uni_hdr(&r_u->hdr_user_name, ulen);
816         r_u->ptr_user_name = 1;
817         init_unistr2(&r_u->uni2_user_name, username, ulen);
818
819         r_u->unk1 = 1;
820   
821         init_uni_hdr(&r_u->hdr_dom_name, dlen);
822         r_u->ptr_dom_name = 1;
823         init_unistr2(&r_u->uni2_dom_name, domname, dlen);
824
825         r_u->status = NT_STATUS_OK;
826   
827         return r_u->status;
828 }
829
830 /***************************************************************************
831  
832  ***************************************************************************/
833
834 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
835 {
836         struct lsa_info *handle;
837         struct lsa_info *info;
838
839         r_u->status = NT_STATUS_OK;
840
841         /* find the connection policy handle. */
842         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
843                 return NT_STATUS_INVALID_HANDLE;
844
845         /* check if the user have enough rights */
846
847         /*
848          * I don't know if it's the right one. not documented.
849          * but guessed with rpcclient.
850          */
851         if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
852                 return NT_STATUS_ACCESS_DENIED;
853
854         /* associate the user/group SID with the (unique) handle. */
855         if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
856                 return NT_STATUS_NO_MEMORY;
857
858         ZERO_STRUCTP(info);
859         info->sid = q_u->sid.sid;
860         info->access = q_u->access;
861
862         /* get a (unique) handle.  open a policy on it. */
863         if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
864                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
865
866         return r_u->status;
867 }
868
869 /***************************************************************************
870  For a given SID, enumerate all the privilege this account has.
871  ***************************************************************************/
872
873 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
874 {
875         struct lsa_info *info=NULL;
876         GROUP_MAP map;
877         int i=0;
878
879         LUID_ATTR *set=NULL;
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 **)&info))
885                 return NT_STATUS_INVALID_HANDLE;
886
887         if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
888                 return NT_STATUS_NO_SUCH_GROUP;
889
890         DEBUG(10,("_lsa_enum_privsaccount: %d privileges\n", map.priv_set.count));
891         if (map.priv_set.count!=0) {
892         
893                 set=(LUID_ATTR *)talloc(p->mem_ctx, map.priv_set.count*sizeof(LUID_ATTR));
894                 if (set == NULL) {
895                         free_privilege(&map.priv_set);  
896                         return NT_STATUS_NO_MEMORY;
897                 }
898
899                 for (i=0; i<map.priv_set.count; i++) {
900                         set[i].luid.low=map.priv_set.set[i].luid.low;
901                         set[i].luid.high=map.priv_set.set[i].luid.high;
902                         set[i].attr=map.priv_set.set[i].attr;
903                         DEBUG(10,("_lsa_enum_privsaccount: priv %d: %d:%d:%d\n", i, 
904                                    set[i].luid.high, set[i].luid.low, set[i].attr));
905                 }
906         }
907
908         init_lsa_r_enum_privsaccount(r_u, set, map.priv_set.count, 0);  
909         free_privilege(&map.priv_set);  
910
911         return r_u->status;
912 }
913
914 /***************************************************************************
915  
916  ***************************************************************************/
917
918 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
919 {
920         struct lsa_info *info=NULL;
921         GROUP_MAP map;
922         r_u->status = NT_STATUS_OK;
923
924         /* find the connection policy handle. */
925         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
926                 return NT_STATUS_INVALID_HANDLE;
927
928         if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITHOUT_PRIV))
929                 return NT_STATUS_NO_SUCH_GROUP;
930
931         /*
932           0x01 -> Log on locally
933           0x02 -> Access this computer from network
934           0x04 -> Log on as a batch job
935           0x10 -> Log on as a service
936           
937           they can be ORed together
938         */
939
940         r_u->access=map.systemaccount;
941
942         return r_u->status;
943 }
944
945 /***************************************************************************
946   update the systemaccount information
947  ***************************************************************************/
948
949 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
950 {
951         struct lsa_info *info=NULL;
952         GROUP_MAP map;
953         r_u->status = NT_STATUS_OK;
954
955         /* find the connection policy handle. */
956         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
957                 return NT_STATUS_INVALID_HANDLE;
958
959         if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
960                 return NT_STATUS_NO_SUCH_GROUP;
961
962         map.systemaccount=q_u->access;
963
964         if(!add_mapping_entry(&map, TDB_REPLACE))
965                 return NT_STATUS_NO_SUCH_GROUP;
966
967         free_privilege(&map.priv_set);
968
969         return r_u->status;
970 }
971
972 /***************************************************************************
973  For a given SID, add some privileges.
974  ***************************************************************************/
975
976 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
977 {
978         struct lsa_info *info=NULL;
979         GROUP_MAP map;
980         int i=0;
981
982         LUID_ATTR *luid_attr=NULL;
983         PRIVILEGE_SET *set=NULL;
984
985         r_u->status = NT_STATUS_OK;
986
987         /* find the connection policy handle. */
988         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
989                 return NT_STATUS_INVALID_HANDLE;
990
991         if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
992                 return NT_STATUS_NO_SUCH_GROUP;
993
994         set=&q_u->set;
995
996         for (i=0; i<set->count; i++) {
997                 luid_attr=&set->set[i];
998                 
999                 /* check if the privilege is already there */
1000                 if (check_priv_in_privilege(&map.priv_set, *luid_attr)){
1001                         free_privilege(&map.priv_set);
1002                         return NT_STATUS_NO_SUCH_PRIVILEGE;
1003                 }
1004                 
1005                 add_privilege(&map.priv_set, *luid_attr);
1006         }
1007
1008         if(!add_mapping_entry(&map, TDB_REPLACE))
1009                 return NT_STATUS_NO_SUCH_GROUP;
1010         
1011         free_privilege(&map.priv_set);  
1012
1013         return r_u->status;
1014 }
1015
1016 /***************************************************************************
1017  For a given SID, remove some privileges.
1018  ***************************************************************************/
1019
1020 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1021 {
1022         struct lsa_info *info=NULL;
1023         GROUP_MAP map;
1024         int i=0;
1025
1026         LUID_ATTR *luid_attr=NULL;
1027         PRIVILEGE_SET *set=NULL;
1028
1029         r_u->status = NT_STATUS_OK;
1030
1031         /* find the connection policy handle. */
1032         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1033                 return NT_STATUS_INVALID_HANDLE;
1034
1035         if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1036                 return NT_STATUS_NO_SUCH_GROUP;
1037
1038         if (q_u->allrights!=0) {
1039                 /* log it and return, until I see one myself don't do anything */
1040                 DEBUG(5,("_lsa_removeprivs: trying to remove all privileges ?\n"));
1041                 return NT_STATUS_OK;
1042         }
1043
1044         if (q_u->ptr==0) {
1045                 /* log it and return, until I see one myself don't do anything */
1046                 DEBUG(5,("_lsa_removeprivs: no privileges to remove ?\n"));
1047                 return NT_STATUS_OK;
1048         }
1049
1050         set=&q_u->set;
1051
1052         for (i=0; i<set->count; i++) {
1053                 luid_attr=&set->set[i];
1054                 
1055                 /* if we don't have the privilege, we're trying to remove, give up */
1056                 /* what else can we do ??? JFM. */
1057                 if (!check_priv_in_privilege(&map.priv_set, *luid_attr)){
1058                         free_privilege(&map.priv_set);
1059                         return NT_STATUS_NO_SUCH_PRIVILEGE;
1060                 }
1061                 
1062                 remove_privilege(&map.priv_set, *luid_attr);
1063         }
1064
1065         if(!add_mapping_entry(&map, TDB_REPLACE))
1066                 return NT_STATUS_NO_SUCH_GROUP;
1067         
1068         free_privilege(&map.priv_set);  
1069
1070         return r_u->status;
1071 }
1072
1073 /***************************************************************************
1074  For a given SID, remove some privileges.
1075  ***************************************************************************/
1076
1077 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1078 {
1079         struct lsa_info *handle=NULL;
1080         SEC_DESC *psd = NULL;
1081         size_t sd_size;
1082         NTSTATUS status;
1083
1084         r_u->status = NT_STATUS_OK;
1085
1086         /* find the connection policy handle. */
1087         if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1088                 return NT_STATUS_INVALID_HANDLE;
1089
1090         /* check if the user have enough rights */
1091         if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1092                 return NT_STATUS_ACCESS_DENIED;
1093
1094
1095         switch (q_u->sec_info) {
1096         case 1:
1097                 /* SD contains only the owner */
1098
1099                 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1100                 if(!NT_STATUS_IS_OK(status))
1101                         return NT_STATUS_NO_MEMORY;
1102
1103
1104                 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1105                         return NT_STATUS_NO_MEMORY;
1106                 break;
1107         case 4:
1108                 /* SD contains only the ACL */
1109
1110                 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1111                 if(!NT_STATUS_IS_OK(status))
1112                         return NT_STATUS_NO_MEMORY;
1113
1114                 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1115                         return NT_STATUS_NO_MEMORY;
1116                 break;
1117         default:
1118                 return NT_STATUS_INVALID_LEVEL;
1119         }
1120
1121         r_u->ptr=1;
1122
1123         return r_u->status;
1124 }
1125
1126