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