r91: Fix lsalookupnames. Previously we'd fail if we didn't find the name, but
[Samba/gebeck_regimport.git] / source / rpc_server / srv_lsa_nt.c
blob9801ce47f8fab2a7c16d2c3f947c33c031fb5264
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 * Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* This is the implementation of the lsa server code. */
28 #include "includes.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_RPC_SRV
33 extern PRIVS privs[];
35 struct lsa_info {
36 DOM_SID sid;
37 uint32 access;
40 struct generic_mapping lsa_generic_mapping = {
41 POLICY_READ,
42 POLICY_WRITE,
43 POLICY_EXECUTE,
44 POLICY_ALL_ACCESS
47 /*******************************************************************
48 Function to free the per handle data.
49 ********************************************************************/
51 static void free_lsa_info(void *ptr)
53 struct lsa_info *lsa = (struct lsa_info *)ptr;
55 SAFE_FREE(lsa);
58 /***************************************************************************
59 Init dom_query
60 ***************************************************************************/
62 static void init_dom_query(DOM_QUERY *d_q, const char *dom_name, DOM_SID *dom_sid)
64 d_q->buffer_dom_name = (dom_name != NULL) ? 1 : 0; /* domain buffer pointer */
65 d_q->buffer_dom_sid = (dom_sid != NULL) ? 1 : 0; /* domain sid pointer */
67 /* this string is supposed to be non-null terminated. */
68 /* But the maxlen in this UNISTR2 must include the terminating null. */
69 init_unistr2(&d_q->uni_domain_name, dom_name, UNI_BROKEN_NON_NULL);
72 * I'm not sure why this really odd combination of length
73 * values works, but it does appear to. I need to look at
74 * this *much* more closely - but at the moment leave alone
75 * until it's understood. This allows a W2k client to join
76 * a domain with both odd and even length names... JRA.
80 * IMPORTANT NOTE !!!!
81 * The two fields below probably are reversed in meaning, ie.
82 * the first field is probably the str_len, the second the max
83 * len. Both are measured in bytes anyway.
86 d_q->uni_dom_str_len = d_q->uni_domain_name.uni_max_len * 2;
87 d_q->uni_dom_max_len = d_q->uni_domain_name.uni_str_len * 2;
89 if (dom_sid != NULL)
90 init_dom_sid2(&d_q->dom_sid, dom_sid);
93 /***************************************************************************
94 init_dom_ref - adds a domain if it's not already in, returns the index.
95 ***************************************************************************/
97 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
99 int num = 0;
101 if (dom_name != NULL) {
102 for (num = 0; num < ref->num_ref_doms_1; num++) {
103 fstring domname;
104 rpcstr_pull(domname, &ref->ref_dom[num].uni_dom_name, sizeof(domname), -1, 0);
105 if (strequal(domname, dom_name))
106 return num;
108 } else {
109 num = ref->num_ref_doms_1;
112 if (num >= MAX_REF_DOMAINS) {
113 /* index not found, already at maximum domain limit */
114 return -1;
117 ref->num_ref_doms_1 = num+1;
118 ref->ptr_ref_dom = 1;
119 ref->max_entries = MAX_REF_DOMAINS;
120 ref->num_ref_doms_2 = num+1;
122 ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
124 init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, UNI_FLAGS_NONE);
125 init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, &ref->ref_dom[num].uni_dom_name);
127 init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
129 return num;
132 /***************************************************************************
133 init_lsa_rid2s
134 ***************************************************************************/
136 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
137 int num_entries, UNISTR2 *name,
138 uint32 *mapped_count, BOOL endian)
140 int i;
141 int total = 0;
142 *mapped_count = 0;
144 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
146 become_root(); /* lookup_name can require root privs */
148 for (i = 0; i < num_entries; i++) {
149 BOOL status = False;
150 DOM_SID sid;
151 uint32 rid = 0xffffffff;
152 int dom_idx = -1;
153 pstring full_name;
154 fstring dom_name, user;
155 enum SID_NAME_USE name_type = SID_NAME_UNKNOWN;
157 /* Split name into domain and user component */
159 unistr2_to_ascii(full_name, &name[i], sizeof(full_name));
160 split_domain_name(full_name, dom_name, user);
162 /* Lookup name */
164 DEBUG(5, ("init_lsa_rid2s: looking up name %s\n", full_name));
166 status = lookup_name(dom_name, user, &sid, &name_type);
168 if((name_type == SID_NAME_UNKNOWN) && (lp_server_role() == ROLE_DOMAIN_MEMBER) && (strncmp(dom_name, full_name, strlen(dom_name)) != 0)) {
169 DEBUG(5, ("init_lsa_rid2s: domain name not provided and local account not found, using member domain\n"));
170 fstrcpy(dom_name, lp_workgroup());
171 status = lookup_name(dom_name, user, &sid, &name_type);
174 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" :
175 "not found"));
177 if (status && name_type != SID_NAME_UNKNOWN) {
178 sid_split_rid(&sid, &rid);
179 dom_idx = init_dom_ref(ref, dom_name, &sid);
180 (*mapped_count)++;
181 } else {
182 dom_idx = -1;
183 rid = 0;
184 name_type = SID_NAME_UNKNOWN;
187 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
188 total++;
191 unbecome_root();
194 /***************************************************************************
195 init_reply_lookup_names
196 ***************************************************************************/
198 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
199 DOM_R_REF *ref, uint32 num_entries,
200 DOM_RID2 *rid2, uint32 mapped_count)
202 r_l->ptr_dom_ref = 1;
203 r_l->dom_ref = ref;
205 r_l->num_entries = num_entries;
206 r_l->ptr_entries = 1;
207 r_l->num_entries2 = num_entries;
208 r_l->dom_rid = rid2;
210 r_l->mapped_count = mapped_count;
213 /***************************************************************************
214 Init lsa_trans_names.
215 ***************************************************************************/
217 static void init_lsa_trans_names(TALLOC_CTX *ctx, DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
218 int num_entries, DOM_SID2 *sid,
219 uint32 *mapped_count)
221 int i;
222 int total = 0;
223 *mapped_count = 0;
225 /* Allocate memory for list of names */
227 if (num_entries > 0) {
228 if (!(trn->name = (LSA_TRANS_NAME *)talloc(ctx, sizeof(LSA_TRANS_NAME) *
229 num_entries))) {
230 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
231 return;
234 if (!(trn->uni_name = (UNISTR2 *)talloc(ctx, sizeof(UNISTR2) *
235 num_entries))) {
236 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
237 return;
241 become_root(); /* Need root to get to passdb to for local sids */
243 for (i = 0; i < num_entries; i++) {
244 BOOL status = False;
245 DOM_SID find_sid = sid[i].sid;
246 uint32 rid = 0xffffffff;
247 int dom_idx = -1;
248 fstring name, dom_name;
249 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
251 sid_to_string(name, &find_sid);
252 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
254 /* Lookup sid from winbindd */
256 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
258 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" :
259 "not found"));
261 if (!status) {
262 sid_name_use = SID_NAME_UNKNOWN;
263 memset(dom_name, '\0', sizeof(dom_name));
264 sid_to_string(name, &find_sid);
265 dom_idx = -1;
267 DEBUG(10,("init_lsa_trans_names: added unknown user '%s' to "
268 "referenced list.\n", name ));
269 } else {
270 (*mapped_count)++;
271 /* Store domain sid in ref array */
272 if (find_sid.num_auths == 5) {
273 sid_split_rid(&find_sid, &rid);
275 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
277 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
278 "referenced list.\n", dom_name, name ));
282 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
283 sid_name_use, name, dom_idx);
284 total++;
287 unbecome_root();
289 trn->num_entries = total;
290 trn->ptr_trans_names = 1;
291 trn->num_entries2 = total;
294 /***************************************************************************
295 Init_reply_lookup_sids.
296 ***************************************************************************/
298 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
299 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
300 uint32 mapped_count)
302 r_l->ptr_dom_ref = 1;
303 r_l->dom_ref = ref;
304 r_l->names = names;
305 r_l->mapped_count = mapped_count;
308 static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size)
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;
315 SEC_ACE ace[3];
316 SEC_ACCESS mask;
318 SEC_ACL *psa = NULL;
320 init_sec_access(&mask, POLICY_EXECUTE);
321 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
323 sid_copy(&adm_sid, get_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);
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);
333 if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
334 return NT_STATUS_NO_MEMORY;
336 if((*sd = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, &adm_sid, NULL, NULL, psa, sd_size)) == NULL)
337 return NT_STATUS_NO_MEMORY;
339 return NT_STATUS_OK;
342 /***************************************************************************
343 Init_dns_dom_info.
344 ***************************************************************************/
346 static void init_dns_dom_info(LSA_DNS_DOM_INFO *r_l, const char *nb_name,
347 const char *dns_name, const char *forest_name,
348 GUID *dom_guid, DOM_SID *dom_sid)
350 if (nb_name && *nb_name) {
351 init_unistr2(&r_l->uni_nb_dom_name, nb_name, UNI_FLAGS_NONE);
352 init_uni_hdr(&r_l->hdr_nb_dom_name, &r_l->uni_nb_dom_name);
353 r_l->hdr_nb_dom_name.uni_max_len += 2;
354 r_l->uni_nb_dom_name.uni_max_len += 1;
357 if (dns_name && *dns_name) {
358 init_unistr2(&r_l->uni_dns_dom_name, dns_name, UNI_FLAGS_NONE);
359 init_uni_hdr(&r_l->hdr_dns_dom_name, &r_l->uni_dns_dom_name);
360 r_l->hdr_dns_dom_name.uni_max_len += 2;
361 r_l->uni_dns_dom_name.uni_max_len += 1;
364 if (forest_name && *forest_name) {
365 init_unistr2(&r_l->uni_forest_name, forest_name, UNI_FLAGS_NONE);
366 init_uni_hdr(&r_l->hdr_forest_name, &r_l->uni_forest_name);
367 r_l->hdr_forest_name.uni_max_len += 2;
368 r_l->uni_forest_name.uni_max_len += 1;
371 /* how do we init the guid ? probably should write an init fn */
372 if (dom_guid) {
373 memcpy(&r_l->dom_guid, dom_guid, sizeof(GUID));
376 if (dom_sid) {
377 r_l->ptr_dom_sid = 1;
378 init_dom_sid2(&r_l->dom_sid, dom_sid);
382 /***************************************************************************
383 _lsa_open_policy2.
384 ***************************************************************************/
386 NTSTATUS _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
388 struct lsa_info *info;
389 SEC_DESC *psd = NULL;
390 size_t sd_size;
391 uint32 des_access=q_u->des_access;
392 uint32 acc_granted;
393 NTSTATUS status;
396 /* map the generic bits to the lsa policy ones */
397 se_map_generic(&des_access, &lsa_generic_mapping);
399 /* get the generic lsa policy SD until we store it */
400 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
402 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
403 if (geteuid() != 0) {
404 return status;
406 DEBUG(4,("ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
407 acc_granted, des_access));
408 DEBUGADD(4,("but overwritten by euid == 0\n"));
409 acc_granted = des_access;
413 /* associate the domain SID with the (unique) handle. */
414 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
415 return NT_STATUS_NO_MEMORY;
417 ZERO_STRUCTP(info);
418 sid_copy(&info->sid,get_global_sam_sid());
419 info->access = acc_granted;
421 /* set up the LSA QUERY INFO response */
422 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
423 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
425 return NT_STATUS_OK;
428 /***************************************************************************
429 _lsa_open_policy
430 ***************************************************************************/
432 NTSTATUS _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
434 struct lsa_info *info;
435 SEC_DESC *psd = NULL;
436 size_t sd_size;
437 uint32 des_access=q_u->des_access;
438 uint32 acc_granted;
439 NTSTATUS status;
442 /* map the generic bits to the lsa policy ones */
443 se_map_generic(&des_access, &lsa_generic_mapping);
445 /* get the generic lsa policy SD until we store it */
446 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
448 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
449 if (geteuid() != 0) {
450 return status;
452 DEBUG(4,("ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
453 acc_granted, des_access));
454 DEBUGADD(4,("but overwritten by euid == 0\n"));
455 acc_granted = des_access;
458 /* associate the domain SID with the (unique) handle. */
459 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
460 return NT_STATUS_NO_MEMORY;
462 ZERO_STRUCTP(info);
463 sid_copy(&info->sid,get_global_sam_sid());
464 info->access = acc_granted;
466 /* set up the LSA QUERY INFO response */
467 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
468 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
470 return NT_STATUS_OK;
473 /***************************************************************************
474 _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
475 ufff, done :) mimir
476 ***************************************************************************/
478 NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
480 struct lsa_info *info;
481 uint32 enum_context = q_u->enum_context;
484 * preferred length is set to 5 as a "our" preferred length
485 * nt sets this parameter to 2
486 * update (20.08.2002): it's not preferred length, but preferred size!
487 * it needs further investigation how to optimally choose this value
489 uint32 max_num_domains = q_u->preferred_len < 5 ? q_u->preferred_len : 10;
490 TRUSTDOM **trust_doms;
491 uint32 num_domains;
492 NTSTATUS nt_status;
494 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
495 return NT_STATUS_INVALID_HANDLE;
497 /* check if the user have enough rights */
498 if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
499 return NT_STATUS_ACCESS_DENIED;
501 nt_status = secrets_get_trusted_domains(p->mem_ctx, (int *)&enum_context, max_num_domains, (int *)&num_domains, &trust_doms);
503 if (!NT_STATUS_IS_OK(nt_status) &&
504 !NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES) &&
505 !NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
506 return nt_status;
507 } else {
508 r_u->status = nt_status;
511 /* set up the lsa_enum_trust_dom response */
512 init_r_enum_trust_dom(p->mem_ctx, r_u, enum_context, max_num_domains, num_domains, trust_doms);
514 return r_u->status;
517 /***************************************************************************
518 _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
519 ***************************************************************************/
521 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
523 struct lsa_info *handle;
524 LSA_INFO_UNION *info = &r_u->dom;
525 DOM_SID domain_sid;
526 const char *name;
527 DOM_SID *sid = NULL;
529 r_u->status = NT_STATUS_OK;
531 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
532 return NT_STATUS_INVALID_HANDLE;
534 switch (q_u->info_class) {
535 case 0x02:
537 unsigned int i;
538 /* check if the user have enough rights */
539 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
540 return NT_STATUS_ACCESS_DENIED;
542 /* fake info: We audit everything. ;) */
543 info->id2.auditing_enabled = 1;
544 info->id2.count1 = 7;
545 info->id2.count2 = 7;
546 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
547 return NT_STATUS_NO_MEMORY;
548 for (i = 0; i < 7; i++)
549 info->id2.auditsettings[i] = 3;
550 break;
552 case 0x03:
553 /* check if the user have enough rights */
554 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
555 return NT_STATUS_ACCESS_DENIED;
557 /* Request PolicyPrimaryDomainInformation. */
558 switch (lp_server_role()) {
559 case ROLE_DOMAIN_PDC:
560 case ROLE_DOMAIN_BDC:
561 name = get_global_sam_name();
562 sid = get_global_sam_sid();
563 break;
564 case ROLE_DOMAIN_MEMBER:
565 name = lp_workgroup();
566 /* We need to return the Domain SID here. */
567 if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid))
568 sid = &domain_sid;
569 else
570 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
571 break;
572 case ROLE_STANDALONE:
573 name = lp_workgroup();
574 sid = NULL;
575 break;
576 default:
577 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
579 init_dom_query(&r_u->dom.id3, name, sid);
580 break;
581 case 0x05:
582 /* check if the user have enough rights */
583 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
584 return NT_STATUS_ACCESS_DENIED;
586 /* Request PolicyAccountDomainInformation. */
587 name = get_global_sam_name();
588 sid = get_global_sam_sid();
589 init_dom_query(&r_u->dom.id5, name, sid);
590 break;
591 case 0x06:
592 /* check if the user have enough rights */
593 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
594 return NT_STATUS_ACCESS_DENIED;
596 switch (lp_server_role()) {
597 case ROLE_DOMAIN_BDC:
599 * only a BDC is a backup controller
600 * of the domain, it controls.
602 info->id6.server_role = 2;
603 break;
604 default:
606 * any other role is a primary
607 * of the domain, it controls.
609 info->id6.server_role = 3;
610 break;
612 break;
613 default:
614 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
615 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
616 break;
619 if (NT_STATUS_IS_OK(r_u->status)) {
620 r_u->undoc_buffer = 0x22000000; /* bizarre */
621 r_u->info_class = q_u->info_class;
624 return r_u->status;
627 /***************************************************************************
628 _lsa_lookup_sids
629 ***************************************************************************/
631 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
633 struct lsa_info *handle;
634 DOM_SID2 *sid = q_u->sids.sid;
635 int num_entries = q_u->sids.num_entries;
636 DOM_R_REF *ref = NULL;
637 LSA_TRANS_NAME_ENUM *names = NULL;
638 uint32 mapped_count = 0;
640 if (num_entries > MAX_LOOKUP_SIDS) {
641 num_entries = MAX_LOOKUP_SIDS;
642 DEBUG(5,("_lsa_lookup_sids: truncating SID lookup list to %d\n", num_entries));
645 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
646 names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
648 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
649 r_u->status = NT_STATUS_INVALID_HANDLE;
650 goto done;
653 /* check if the user have enough rights */
654 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
655 r_u->status = NT_STATUS_ACCESS_DENIED;
656 goto done;
658 if (!ref || !names)
659 return NT_STATUS_NO_MEMORY;
661 done:
663 /* set up the LSA Lookup SIDs response */
664 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
665 if (mapped_count == 0)
666 r_u->status = NT_STATUS_NONE_MAPPED;
667 else if (mapped_count != num_entries)
668 r_u->status = STATUS_SOME_UNMAPPED;
669 else
670 r_u->status = NT_STATUS_OK;
671 init_reply_lookup_sids(r_u, ref, names, mapped_count);
673 return r_u->status;
676 /***************************************************************************
677 lsa_reply_lookup_names
678 ***************************************************************************/
680 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
682 struct lsa_info *handle;
683 UNISTR2 *names = q_u->uni_name;
684 int num_entries = q_u->num_entries;
685 DOM_R_REF *ref;
686 DOM_RID2 *rids;
687 uint32 mapped_count = 0;
689 if (num_entries > MAX_LOOKUP_SIDS) {
690 num_entries = MAX_LOOKUP_SIDS;
691 DEBUG(5,("_lsa_lookup_names: truncating name lookup list to %d\n", num_entries));
694 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
695 rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*num_entries);
697 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
698 r_u->status = NT_STATUS_INVALID_HANDLE;
699 goto done;
702 /* check if the user have enough rights */
703 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
704 r_u->status = NT_STATUS_ACCESS_DENIED;
705 goto done;
708 if (!ref || !rids)
709 return NT_STATUS_NO_MEMORY;
711 done:
713 /* set up the LSA Lookup RIDs response */
714 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
715 if (mapped_count == 0)
716 r_u->status = NT_STATUS_NONE_MAPPED;
717 else if (mapped_count != num_entries)
718 r_u->status = STATUS_SOME_UNMAPPED;
719 else
720 r_u->status = NT_STATUS_OK;
721 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
723 return r_u->status;
726 /***************************************************************************
727 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
728 ***************************************************************************/
730 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
732 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
733 return NT_STATUS_INVALID_HANDLE;
735 close_policy_hnd(p, &q_u->pol);
736 return NT_STATUS_OK;
739 /***************************************************************************
740 "No more secrets Marty...." :-).
741 ***************************************************************************/
743 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
745 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
748 /***************************************************************************
749 _lsa_enum_privs.
750 ***************************************************************************/
752 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
754 struct lsa_info *handle;
755 uint32 i;
757 uint32 enum_context=q_u->enum_context;
758 LSA_PRIV_ENTRY *entry;
759 LSA_PRIV_ENTRY *entries=NULL;
761 if (enum_context >= PRIV_ALL_INDEX)
762 return NT_STATUS_NO_MORE_ENTRIES;
764 entries = (LSA_PRIV_ENTRY *)talloc_zero(p->mem_ctx, sizeof(LSA_PRIV_ENTRY) * (PRIV_ALL_INDEX));
765 if (entries==NULL)
766 return NT_STATUS_NO_MEMORY;
768 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
769 return NT_STATUS_INVALID_HANDLE;
771 /* check if the user have enough rights */
774 * I don't know if it's the right one. not documented.
776 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
777 return NT_STATUS_ACCESS_DENIED;
779 entry = entries;
781 DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", enum_context, PRIV_ALL_INDEX));
783 for (i = 0; i < PRIV_ALL_INDEX; i++, entry++) {
784 if( i<enum_context) {
785 init_unistr2(&entry->name, NULL, UNI_FLAGS_NONE);
786 init_uni_hdr(&entry->hdr_name, &entry->name);
787 entry->luid_low = 0;
788 entry->luid_high = 0;
789 } else {
790 init_unistr2(&entry->name, privs[i+1].priv, UNI_FLAGS_NONE);
791 init_uni_hdr(&entry->hdr_name, &entry->name);
792 entry->luid_low = privs[i+1].se_priv;
793 entry->luid_high = 0;
797 enum_context = PRIV_ALL_INDEX;
798 init_lsa_r_enum_privs(r_u, enum_context, PRIV_ALL_INDEX, entries);
800 return NT_STATUS_OK;
803 /***************************************************************************
804 _lsa_priv_get_dispname.
805 ***************************************************************************/
807 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
809 struct lsa_info *handle;
810 fstring name_asc;
811 int i=1;
813 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
814 return NT_STATUS_INVALID_HANDLE;
816 /* check if the user have enough rights */
819 * I don't know if it's the right one. not documented.
821 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
822 return NT_STATUS_ACCESS_DENIED;
824 unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
826 DEBUG(10,("_lsa_priv_get_dispname: %s", name_asc));
828 while (privs[i].se_priv!=SE_PRIV_ALL && strcmp(name_asc, privs[i].priv))
829 i++;
831 if (privs[i].se_priv!=SE_PRIV_ALL) {
832 DEBUG(10,(": %s\n", privs[i].description));
833 init_unistr2(&r_u->desc, privs[i].description, UNI_FLAGS_NONE);
834 init_uni_hdr(&r_u->hdr_desc, &r_u->desc);
836 r_u->ptr_info=0xdeadbeef;
837 r_u->lang_id=q_u->lang_id;
838 return NT_STATUS_OK;
839 } else {
840 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
841 r_u->ptr_info=0;
842 return NT_STATUS_NO_SUCH_PRIVILEGE;
846 /***************************************************************************
847 _lsa_enum_accounts.
848 ***************************************************************************/
850 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
852 struct lsa_info *handle;
853 GROUP_MAP *map=NULL;
854 int num_entries=0;
855 LSA_SID_ENUM *sids=&r_u->sids;
856 int i=0,j=0;
857 BOOL ret;
859 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
860 return NT_STATUS_INVALID_HANDLE;
862 /* check if the user have enough rights */
865 * I don't know if it's the right one. not documented.
867 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
868 return NT_STATUS_ACCESS_DENIED;
870 /* get the list of mapped groups (domain, local, builtin) */
871 become_root();
872 ret = pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, ENUM_ONLY_MAPPED);
873 unbecome_root();
874 if( !ret ) {
875 DEBUG(3,("_lsa_enum_accounts: enumeration of groups failed!\n"));
876 return NT_STATUS_OK;
880 if (q_u->enum_context >= num_entries)
881 return NT_STATUS_NO_MORE_ENTRIES;
883 sids->ptr_sid = (uint32 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(uint32));
884 sids->sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(DOM_SID2));
886 if (sids->ptr_sid==NULL || sids->sid==NULL) {
887 SAFE_FREE(map);
888 return NT_STATUS_NO_MEMORY;
891 for (i=q_u->enum_context, j=0; i<num_entries; i++) {
892 init_dom_sid2( &(*sids).sid[j], &map[i].sid);
893 (*sids).ptr_sid[j]=1;
894 j++;
897 SAFE_FREE(map);
899 init_lsa_r_enum_accounts(r_u, j);
901 return NT_STATUS_OK;
905 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
907 fstring username, domname;
908 user_struct *vuser = get_valid_user_struct(p->vuid);
910 if (vuser == NULL)
911 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
913 fstrcpy(username, vuser->user.smb_name);
914 fstrcpy(domname, vuser->user.domain);
916 r_u->ptr_user_name = 1;
917 init_unistr2(&r_u->uni2_user_name, username, UNI_STR_TERMINATE);
918 init_uni_hdr(&r_u->hdr_user_name, &r_u->uni2_user_name);
920 r_u->unk1 = 1;
922 r_u->ptr_dom_name = 1;
923 init_unistr2(&r_u->uni2_dom_name, domname, UNI_STR_TERMINATE);
924 init_uni_hdr(&r_u->hdr_dom_name, &r_u->uni2_dom_name);
926 r_u->status = NT_STATUS_OK;
928 return r_u->status;
931 /***************************************************************************
933 ***************************************************************************/
935 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
937 struct lsa_info *handle;
938 struct lsa_info *info;
940 r_u->status = NT_STATUS_OK;
942 /* find the connection policy handle. */
943 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
944 return NT_STATUS_INVALID_HANDLE;
946 /* check if the user have enough rights */
949 * I don't know if it's the right one. not documented.
950 * but guessed with rpcclient.
952 if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
953 return NT_STATUS_ACCESS_DENIED;
955 /* associate the user/group SID with the (unique) handle. */
956 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
957 return NT_STATUS_NO_MEMORY;
959 ZERO_STRUCTP(info);
960 info->sid = q_u->sid.sid;
961 info->access = q_u->access;
963 /* get a (unique) handle. open a policy on it. */
964 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
965 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
967 return r_u->status;
970 /***************************************************************************
971 For a given SID, enumerate all the privilege this account has.
972 ***************************************************************************/
974 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, prs_struct *ps, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
976 struct lsa_info *info=NULL;
977 GROUP_MAP map;
978 LUID_ATTR *set=NULL;
980 r_u->status = NT_STATUS_OK;
982 /* find the connection policy handle. */
983 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
984 return NT_STATUS_INVALID_HANDLE;
986 if (!pdb_getgrsid(&map, info->sid))
987 return NT_STATUS_NO_SUCH_GROUP;
989 #if 0 /* privileges currently not implemented! */
990 DEBUG(10,("_lsa_enum_privsaccount: %d privileges\n", map.priv_set->count));
991 if (map.priv_set->count!=0) {
993 set=(LUID_ATTR *)talloc(map.priv_set->mem_ctx, map.priv_set.count*sizeof(LUID_ATTR));
994 if (set == NULL) {
995 destroy_privilege(&map.priv_set);
996 return NT_STATUS_NO_MEMORY;
999 for (i = 0; i < map.priv_set.count; i++) {
1000 set[i].luid.low = map.priv_set->set[i].luid.low;
1001 set[i].luid.high = map.priv_set->set[i].luid.high;
1002 set[i].attr = map.priv_set->set[i].attr;
1003 DEBUG(10,("_lsa_enum_privsaccount: priv %d: %d:%d:%d\n", i,
1004 set[i].luid.high, set[i].luid.low, set[i].attr));
1008 init_lsa_r_enum_privsaccount(ps->mem_ctx, r_u, set, map.priv_set->count, 0);
1009 destroy_privilege(&map.priv_set);
1010 #endif
1012 init_lsa_r_enum_privsaccount(ps->mem_ctx, r_u, set, 0, 0);
1014 return r_u->status;
1017 /***************************************************************************
1019 ***************************************************************************/
1021 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
1023 struct lsa_info *info=NULL;
1024 GROUP_MAP map;
1025 r_u->status = NT_STATUS_OK;
1027 /* find the connection policy handle. */
1028 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1029 return NT_STATUS_INVALID_HANDLE;
1031 if (!pdb_getgrsid(&map, info->sid))
1032 return NT_STATUS_NO_SUCH_GROUP;
1035 0x01 -> Log on locally
1036 0x02 -> Access this computer from network
1037 0x04 -> Log on as a batch job
1038 0x10 -> Log on as a service
1040 they can be ORed together
1043 r_u->access = PR_LOG_ON_LOCALLY | PR_ACCESS_FROM_NETWORK;
1045 return r_u->status;
1048 /***************************************************************************
1049 update the systemaccount information
1050 ***************************************************************************/
1052 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
1054 struct lsa_info *info=NULL;
1055 GROUP_MAP map;
1056 r_u->status = NT_STATUS_OK;
1058 /* find the connection policy handle. */
1059 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1060 return NT_STATUS_INVALID_HANDLE;
1062 if (!pdb_getgrsid(&map, info->sid))
1063 return NT_STATUS_NO_SUCH_GROUP;
1065 if(!pdb_update_group_mapping_entry(&map))
1066 return NT_STATUS_NO_SUCH_GROUP;
1068 return r_u->status;
1071 /***************************************************************************
1072 For a given SID, add some privileges.
1073 ***************************************************************************/
1075 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
1077 #if 0
1078 struct lsa_info *info = NULL;
1079 GROUP_MAP map;
1080 int i = 0;
1081 LUID_ATTR *luid_attr = NULL;
1082 PRIVILEGE_SET *set = NULL;
1083 #endif
1085 r_u->status = NT_STATUS_OK;
1087 #if 0 /* privileges are not implemented */
1088 /* find the connection policy handle. */
1089 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1090 return NT_STATUS_INVALID_HANDLE;
1092 if (!pdb_getgrsid(&map, info->sid))
1093 return NT_STATUS_NO_SUCH_GROUP;
1095 set = &q_u->set;
1097 for (i = 0; i < set->count; i++) {
1098 luid_attr = &set->set[i];
1100 /* check if the privilege is already there */
1101 if (check_priv_in_privilege(map.priv_set, *luid_attr)){
1102 destroy_privilege(&map.priv_set);
1103 return NT_STATUS_NO_SUCH_PRIVILEGE;
1106 add_privilege(map.priv_set, *luid_attr);
1109 if(!pdb_update_group_mapping_entry(&map))
1110 return NT_STATUS_NO_SUCH_GROUP;
1112 destroy_privilege(&map.priv_set);
1114 #endif
1115 return r_u->status;
1118 /***************************************************************************
1119 For a given SID, remove some privileges.
1120 ***************************************************************************/
1122 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1124 #if 0
1125 struct lsa_info *info = NULL;
1126 GROUP_MAP map;
1127 int i=0;
1128 LUID_ATTR *luid_attr = NULL;
1129 PRIVILEGE_SET *set = NULL;
1130 #endif
1132 r_u->status = NT_STATUS_OK;
1134 #if 0 /* privileges are not implemented */
1135 /* find the connection policy handle. */
1136 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1137 return NT_STATUS_INVALID_HANDLE;
1139 if (!pdb_getgrsid(&map, info->sid))
1140 return NT_STATUS_NO_SUCH_GROUP;
1142 if (q_u->allrights != 0) {
1143 /* log it and return, until I see one myself don't do anything */
1144 DEBUG(5,("_lsa_removeprivs: trying to remove all privileges ?\n"));
1145 return NT_STATUS_OK;
1148 if (q_u->ptr == 0) {
1149 /* log it and return, until I see one myself don't do anything */
1150 DEBUG(5,("_lsa_removeprivs: no privileges to remove ?\n"));
1151 return NT_STATUS_OK;
1154 set = &q_u->set;
1156 for (i = 0; i < set->count; i++) {
1157 luid_attr = &set->set[i];
1159 /* if we don't have the privilege, we're trying to remove, give up */
1160 /* what else can we do ??? JFM. */
1161 if (!check_priv_in_privilege(map.priv_set, *luid_attr)){
1162 destroy_privilege(&map.priv_set);
1163 return NT_STATUS_NO_SUCH_PRIVILEGE;
1166 remove_privilege(map.priv_set, *luid_attr);
1169 if(!pdb_update_group_mapping_entry(&map))
1170 return NT_STATUS_NO_SUCH_GROUP;
1172 destroy_privilege(&map.priv_set);
1173 #endif
1174 return r_u->status;
1177 /***************************************************************************
1178 For a given SID, remove some privileges.
1179 ***************************************************************************/
1181 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1183 struct lsa_info *handle=NULL;
1184 SEC_DESC *psd = NULL;
1185 size_t sd_size;
1186 NTSTATUS status;
1188 r_u->status = NT_STATUS_OK;
1190 /* find the connection policy handle. */
1191 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1192 return NT_STATUS_INVALID_HANDLE;
1194 /* check if the user have enough rights */
1195 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1196 return NT_STATUS_ACCESS_DENIED;
1199 switch (q_u->sec_info) {
1200 case 1:
1201 /* SD contains only the owner */
1203 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1204 if(!NT_STATUS_IS_OK(status))
1205 return NT_STATUS_NO_MEMORY;
1208 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1209 return NT_STATUS_NO_MEMORY;
1210 break;
1211 case 4:
1212 /* SD contains only the ACL */
1214 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1215 if(!NT_STATUS_IS_OK(status))
1216 return NT_STATUS_NO_MEMORY;
1218 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1219 return NT_STATUS_NO_MEMORY;
1220 break;
1221 default:
1222 return NT_STATUS_INVALID_LEVEL;
1225 r_u->ptr=1;
1227 return r_u->status;
1231 NTSTATUS _lsa_query_info2(pipes_struct *p, LSA_Q_QUERY_INFO2 *q_u, LSA_R_QUERY_INFO2 *r_u)
1233 struct lsa_info *handle;
1234 const char *nb_name;
1235 char *dns_name = NULL;
1236 char *forest_name = NULL;
1237 DOM_SID *sid = NULL;
1238 GUID guid;
1239 fstring dnsdomname;
1241 ZERO_STRUCT(guid);
1242 r_u->status = NT_STATUS_OK;
1244 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1245 return NT_STATUS_INVALID_HANDLE;
1247 switch (q_u->info_class) {
1248 case 0x0c:
1249 /* check if the user have enough rights */
1250 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1251 return NT_STATUS_ACCESS_DENIED;
1253 /* Request PolicyPrimaryDomainInformation. */
1254 switch (lp_server_role()) {
1255 case ROLE_DOMAIN_PDC:
1256 case ROLE_DOMAIN_BDC:
1257 nb_name = get_global_sam_name();
1258 /* ugly temp hack for these next two */
1260 /* This should be a 'netbios domain -> DNS domain' mapping */
1261 dnsdomname[0] = '\0';
1262 get_mydnsdomname(dnsdomname);
1263 strlower_m(dnsdomname);
1265 dns_name = dnsdomname;
1266 forest_name = dnsdomname;
1268 sid = get_global_sam_sid();
1269 secrets_fetch_domain_guid(lp_workgroup(), &guid);
1270 break;
1271 default:
1272 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1274 init_dns_dom_info(&r_u->info.dns_dom_info, nb_name, dns_name,
1275 forest_name,&guid,sid);
1276 break;
1277 default:
1278 DEBUG(0,("_lsa_query_info2: unknown info level in Lsa Query: %d\n", q_u->info_class));
1279 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
1280 break;
1283 if (NT_STATUS_IS_OK(r_u->status)) {
1284 r_u->ptr = 0x1;
1285 r_u->info_class = q_u->info_class;
1288 return r_u->status;