* fix to display correct form information in REG_BINARY information
[Samba.git] / source / rpc_server / srv_lsa_nt.c
blobd072061a5f63effe8622a05b7baa86a4c6673080
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.
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.
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.
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.
25 /* This is the implementation of the lsa server code. */
27 #include "includes.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_RPC_SRV
32 extern fstring global_myworkgroup;
33 extern pstring global_myname;
34 extern PRIVS privs[];
36 struct lsa_info {
37 DOM_SID sid;
38 uint32 access;
41 struct generic_mapping lsa_generic_mapping = {
42 POLICY_READ,
43 POLICY_WRITE,
44 POLICY_EXECUTE,
45 POLICY_ALL_ACCESS
48 /*******************************************************************
49 Function to free the per handle data.
50 ********************************************************************/
52 static void free_lsa_info(void *ptr)
54 struct lsa_info *lsa = (struct lsa_info *)ptr;
56 SAFE_FREE(lsa);
59 /***************************************************************************
60 Init dom_query
61 ***************************************************************************/
63 static void init_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
65 int domlen = (dom_name != NULL) ? strlen(dom_name) : 0;
68 * I'm not sure why this really odd combination of length
69 * values works, but it does appear to. I need to look at
70 * this *much* more closely - but at the moment leave alone
71 * until it's understood. This allows a W2k client to join
72 * a domain with both odd and even length names... JRA.
75 d_q->uni_dom_str_len = domlen ? ((domlen + 1) * 2) : 0;
76 d_q->uni_dom_max_len = domlen * 2;
77 d_q->buffer_dom_name = domlen != 0 ? 1 : 0; /* domain buffer pointer */
78 d_q->buffer_dom_sid = dom_sid != NULL ? 1 : 0; /* domain sid pointer */
80 /* this string is supposed to be character short */
81 init_unistr2(&d_q->uni_domain_name, dom_name, domlen);
82 d_q->uni_domain_name.uni_max_len++;
84 if (dom_sid != NULL)
85 init_dom_sid2(&d_q->dom_sid, dom_sid);
88 /***************************************************************************
89 init_dom_ref - adds a domain if it's not already in, returns the index.
90 ***************************************************************************/
92 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
94 int num = 0;
95 int len;
97 if (dom_name != NULL) {
98 for (num = 0; num < ref->num_ref_doms_1; num++) {
99 fstring domname;
100 rpcstr_pull(domname, &ref->ref_dom[num].uni_dom_name, sizeof(domname), -1, 0);
101 if (strequal(domname, dom_name))
102 return num;
104 } else {
105 num = ref->num_ref_doms_1;
108 if (num >= MAX_REF_DOMAINS) {
109 /* index not found, already at maximum domain limit */
110 return -1;
113 ref->num_ref_doms_1 = num+1;
114 ref->ptr_ref_dom = 1;
115 ref->max_entries = MAX_REF_DOMAINS;
116 ref->num_ref_doms_2 = num+1;
118 len = (dom_name != NULL) ? strlen(dom_name) : 0;
119 if(dom_name != NULL && len == 0)
120 len = 1;
122 init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, len);
123 ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
125 init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, len);
126 init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
128 return num;
131 /***************************************************************************
132 init_lsa_rid2s
133 ***************************************************************************/
135 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
136 int num_entries, UNISTR2 *name,
137 uint32 *mapped_count, BOOL endian)
139 int i;
140 int total = 0;
141 *mapped_count = 0;
143 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
145 become_root(); /* lookup_name can require root privs */
147 for (i = 0; i < num_entries; i++) {
148 BOOL status = False;
149 DOM_SID sid;
150 uint32 rid = 0xffffffff;
151 int dom_idx = -1;
152 pstring full_name;
153 fstring dom_name, user;
154 enum SID_NAME_USE name_type = SID_NAME_UNKNOWN;
156 /* Split name into domain and user component */
158 unistr2_to_ascii(full_name, &name[i], sizeof(full_name));
159 split_domain_name(full_name, dom_name, user);
161 /* Lookup name */
163 DEBUG(5, ("init_lsa_rid2s: looking up name %s\n", full_name));
165 status = lookup_name(dom_name, user, &sid, &name_type);
167 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" :
168 "not found"));
170 if (status && name_type != SID_NAME_UNKNOWN) {
171 sid_split_rid(&sid, &rid);
172 dom_idx = init_dom_ref(ref, dom_name, &sid);
173 (*mapped_count)++;
174 } else {
175 dom_idx = -1;
176 rid = 0xffffffff;
177 name_type = SID_NAME_UNKNOWN;
180 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
181 total++;
184 unbecome_root();
187 /***************************************************************************
188 init_reply_lookup_names
189 ***************************************************************************/
191 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
192 DOM_R_REF *ref, uint32 num_entries,
193 DOM_RID2 *rid2, uint32 mapped_count)
195 r_l->ptr_dom_ref = 1;
196 r_l->dom_ref = ref;
198 r_l->num_entries = num_entries;
199 r_l->ptr_entries = 1;
200 r_l->num_entries2 = num_entries;
201 r_l->dom_rid = rid2;
203 r_l->mapped_count = mapped_count;
205 if (mapped_count == 0)
206 r_l->status = NT_STATUS_NONE_MAPPED;
207 else
208 r_l->status = NT_STATUS_OK;
211 /***************************************************************************
212 Init lsa_trans_names.
213 ***************************************************************************/
215 static void init_lsa_trans_names(TALLOC_CTX *ctx, DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
216 int num_entries, DOM_SID2 *sid,
217 uint32 *mapped_count)
219 int i;
220 int total = 0;
221 *mapped_count = 0;
223 /* Allocate memory for list of names */
225 if (num_entries > 0) {
226 if (!(trn->name = (LSA_TRANS_NAME *)talloc(ctx, sizeof(LSA_TRANS_NAME) *
227 num_entries))) {
228 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
229 return;
232 if (!(trn->uni_name = (UNISTR2 *)talloc(ctx, sizeof(UNISTR2) *
233 num_entries))) {
234 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
235 return;
239 become_root(); /* Need root to get to passdb to for local sids */
241 for (i = 0; i < num_entries; i++) {
242 BOOL status = False;
243 DOM_SID find_sid = sid[i].sid;
244 uint32 rid = 0xffffffff;
245 int dom_idx = -1;
246 fstring name, dom_name;
247 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
249 sid_to_string(name, &find_sid);
250 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
252 /* Lookup sid from winbindd */
254 memset(dom_name, '\0', sizeof(dom_name));
255 memset(name, '\0', sizeof(name));
257 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
259 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" :
260 "not found"));
262 if (!status) {
263 sid_name_use = SID_NAME_UNKNOWN;
264 } else {
265 (*mapped_count)++;
268 /* Store domain sid in ref array */
270 if (find_sid.num_auths == 5) {
271 sid_split_rid(&find_sid, &rid);
274 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
276 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
277 "referenced list.\n", dom_name, name ));
279 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
280 sid_name_use, name, dom_idx);
281 total++;
284 unbecome_root();
286 trn->num_entries = total;
287 trn->ptr_trans_names = 1;
288 trn->num_entries2 = total;
291 /***************************************************************************
292 Init_reply_lookup_sids.
293 ***************************************************************************/
295 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
296 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
297 uint32 mapped_count)
299 r_l->ptr_dom_ref = 1;
300 r_l->dom_ref = ref;
301 r_l->names = names;
302 r_l->mapped_count = mapped_count;
304 if (mapped_count == 0)
305 r_l->status = NT_STATUS_NONE_MAPPED;
306 else
307 r_l->status = NT_STATUS_OK;
310 static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size)
312 extern DOM_SID global_sid_World;
313 extern DOM_SID global_sid_Builtin;
314 DOM_SID local_adm_sid;
315 DOM_SID adm_sid;
317 SEC_ACE ace[3];
318 SEC_ACCESS mask;
320 SEC_ACL *psa = NULL;
322 init_sec_access(&mask, POLICY_EXECUTE);
323 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
325 sid_copy(&adm_sid, get_global_sam_sid());
326 sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
327 init_sec_access(&mask, POLICY_ALL_ACCESS);
328 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
330 sid_copy(&local_adm_sid, &global_sid_Builtin);
331 sid_append_rid(&local_adm_sid, BUILTIN_ALIAS_RID_ADMINS);
332 init_sec_access(&mask, POLICY_ALL_ACCESS);
333 init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
335 if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
336 return NT_STATUS_NO_MEMORY;
338 if((*sd = make_sec_desc(mem_ctx, SEC_DESC_REVISION, &adm_sid, NULL, NULL, psa, sd_size)) == NULL)
339 return NT_STATUS_NO_MEMORY;
341 return NT_STATUS_OK;
344 /***************************************************************************
345 _lsa_open_policy2.
346 ***************************************************************************/
348 NTSTATUS _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
350 struct lsa_info *info;
351 SEC_DESC *psd = NULL;
352 size_t sd_size;
353 uint32 des_access=q_u->des_access;
354 uint32 acc_granted;
355 NTSTATUS status;
358 /* map the generic bits to the lsa policy ones */
359 se_map_generic(&des_access, &lsa_generic_mapping);
361 /* get the generic lsa policy SD until we store it */
362 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
364 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status))
365 return status;
367 /* associate the domain SID with the (unique) handle. */
368 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
369 return NT_STATUS_NO_MEMORY;
371 ZERO_STRUCTP(info);
372 sid_copy(&info->sid,get_global_sam_sid());
373 info->access = acc_granted;
375 /* set up the LSA QUERY INFO response */
376 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
377 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
379 return NT_STATUS_OK;
382 /***************************************************************************
383 _lsa_open_policy
384 ***************************************************************************/
386 NTSTATUS _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *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 return status;
405 /* associate the domain SID with the (unique) handle. */
406 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
407 return NT_STATUS_NO_MEMORY;
409 ZERO_STRUCTP(info);
410 sid_copy(&info->sid,get_global_sam_sid());
411 info->access = acc_granted;
413 /* set up the LSA QUERY INFO response */
414 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
415 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
417 return NT_STATUS_OK;
420 /***************************************************************************
421 _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
422 ufff, done :) mimir
423 ***************************************************************************/
425 NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
427 struct lsa_info *info;
428 uint32 enum_context = q_u->enum_context;
431 * preferred length is set to 5 as a "our" preferred length
432 * nt sets this parameter to 2
434 uint32 max_num_domains = q_u->preferred_len < 5 ? q_u->preferred_len : 10;
435 TRUSTDOM **trust_doms;
436 uint32 num_domains;
437 NTSTATUS nt_status;
439 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
440 return NT_STATUS_INVALID_HANDLE;
442 /* check if the user have enough rights */
443 if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
444 return NT_STATUS_ACCESS_DENIED;
446 nt_status = secrets_get_trusted_domains(p->mem_ctx, &enum_context, max_num_domains, &num_domains, &trust_doms);
448 if (!NT_STATUS_IS_OK(nt_status) &&
449 !NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES) &&
450 !NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
451 return nt_status;
452 } else {
453 r_u->status = nt_status;
456 /* set up the lsa_enum_trust_dom response */
457 init_r_enum_trust_dom(p->mem_ctx, r_u, enum_context, max_num_domains, num_domains, trust_doms);
459 return r_u->status;
462 /***************************************************************************
463 _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
464 ***************************************************************************/
466 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
468 struct lsa_info *handle;
469 LSA_INFO_UNION *info = &r_u->dom;
470 DOM_SID domain_sid;
471 char *name = NULL;
472 DOM_SID *sid = NULL;
474 r_u->status = NT_STATUS_OK;
476 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
477 return NT_STATUS_INVALID_HANDLE;
479 switch (q_u->info_class) {
480 case 0x02:
482 unsigned int i;
483 /* check if the user have enough rights */
484 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
485 return NT_STATUS_ACCESS_DENIED;
487 /* fake info: We audit everything. ;) */
488 info->id2.auditing_enabled = 1;
489 info->id2.count1 = 7;
490 info->id2.count2 = 7;
491 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
492 return NT_STATUS_NO_MEMORY;
493 for (i = 0; i < 7; i++)
494 info->id2.auditsettings[i] = 3;
495 break;
497 case 0x03:
498 /* check if the user have enough rights */
499 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
500 return NT_STATUS_ACCESS_DENIED;
502 /* Request PolicyPrimaryDomainInformation. */
503 switch (lp_server_role()) {
504 case ROLE_DOMAIN_PDC:
505 case ROLE_DOMAIN_BDC:
506 name = global_myworkgroup;
507 sid = get_global_sam_sid();
508 break;
509 case ROLE_DOMAIN_MEMBER:
510 name = global_myworkgroup;
511 /* We need to return the Domain SID here. */
512 if (secrets_fetch_domain_sid(global_myworkgroup,
513 &domain_sid))
514 sid = &domain_sid;
515 else
516 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
517 break;
518 case ROLE_STANDALONE:
519 name = global_myworkgroup;
520 sid = NULL;
521 break;
522 default:
523 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
525 init_dom_query(&r_u->dom.id3, name, sid);
526 break;
527 case 0x05:
528 /* check if the user have enough rights */
529 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
530 return NT_STATUS_ACCESS_DENIED;
532 /* Request PolicyAccountDomainInformation. */
533 switch (lp_server_role()) {
534 case ROLE_DOMAIN_PDC:
535 case ROLE_DOMAIN_BDC:
536 name = global_myworkgroup;
537 sid = get_global_sam_sid();
538 break;
539 case ROLE_DOMAIN_MEMBER:
540 name = global_myname;
541 sid = get_global_sam_sid();
542 break;
543 case ROLE_STANDALONE:
544 name = global_myname;
545 sid = get_global_sam_sid();
546 break;
547 default:
548 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
550 init_dom_query(&r_u->dom.id5, name, sid);
551 break;
552 case 0x06:
553 /* check if the user have enough rights */
554 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
555 return NT_STATUS_ACCESS_DENIED;
557 switch (lp_server_role()) {
558 case ROLE_DOMAIN_BDC:
560 * only a BDC is a backup controller
561 * of the domain, it controls.
563 info->id6.server_role = 2;
564 break;
565 default:
567 * any other role is a primary
568 * of the domain, it controls.
570 info->id6.server_role = 3;
571 break;
573 break;
574 default:
575 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
576 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
577 break;
580 if (NT_STATUS_IS_OK(r_u->status)) {
581 r_u->undoc_buffer = 0x22000000; /* bizarre */
582 r_u->info_class = q_u->info_class;
585 return r_u->status;
588 /***************************************************************************
589 _lsa_lookup_sids
590 ***************************************************************************/
592 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
594 struct lsa_info *handle;
595 DOM_SID2 *sid = q_u->sids.sid;
596 int num_entries = q_u->sids.num_entries;
597 DOM_R_REF *ref = NULL;
598 LSA_TRANS_NAME_ENUM *names = NULL;
599 uint32 mapped_count = 0;
601 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
602 names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
604 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
605 r_u->status = NT_STATUS_INVALID_HANDLE;
606 goto done;
609 /* check if the user have enough rights */
610 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
611 r_u->status = NT_STATUS_ACCESS_DENIED;
612 goto done;
614 if (!ref || !names)
615 return NT_STATUS_NO_MEMORY;
617 done:
619 /* set up the LSA Lookup SIDs response */
620 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
621 init_reply_lookup_sids(r_u, ref, names, mapped_count);
623 return r_u->status;
626 /***************************************************************************
627 lsa_reply_lookup_names
628 ***************************************************************************/
630 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
632 struct lsa_info *handle;
633 UNISTR2 *names = q_u->uni_name;
634 int num_entries = q_u->num_entries;
635 DOM_R_REF *ref;
636 DOM_RID2 *rids;
637 uint32 mapped_count = 0;
639 if (num_entries > MAX_LOOKUP_SIDS) {
640 num_entries = MAX_LOOKUP_SIDS;
641 DEBUG(5,("_lsa_lookup_names: truncating name lookup list to %d\n", num_entries));
644 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
645 rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*num_entries);
647 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
648 r_u->status = NT_STATUS_INVALID_HANDLE;
649 goto done;
652 /* check if the user have enough rights */
653 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
654 r_u->status = NT_STATUS_ACCESS_DENIED;
655 goto done;
658 if (!ref || !rids)
659 return NT_STATUS_NO_MEMORY;
661 done:
663 /* set up the LSA Lookup RIDs response */
664 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
665 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
667 return r_u->status;
670 /***************************************************************************
671 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
672 ***************************************************************************/
674 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
676 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
677 return NT_STATUS_INVALID_HANDLE;
679 close_policy_hnd(p, &q_u->pol);
680 return NT_STATUS_OK;
683 /***************************************************************************
684 "No more secrets Marty...." :-).
685 ***************************************************************************/
687 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
689 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
692 /***************************************************************************
693 _lsa_enum_privs.
694 ***************************************************************************/
696 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
698 struct lsa_info *handle;
699 uint32 i;
701 uint32 enum_context=q_u->enum_context;
702 LSA_PRIV_ENTRY *entry;
703 LSA_PRIV_ENTRY *entries=NULL;
705 if (enum_context >= PRIV_ALL_INDEX)
706 return NT_STATUS_NO_MORE_ENTRIES;
708 entries = (LSA_PRIV_ENTRY *)talloc_zero(p->mem_ctx, sizeof(LSA_PRIV_ENTRY) * (PRIV_ALL_INDEX));
709 if (entries==NULL)
710 return NT_STATUS_NO_MEMORY;
712 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
713 return NT_STATUS_INVALID_HANDLE;
715 /* check if the user have enough rights */
718 * I don't know if it's the right one. not documented.
720 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
721 return NT_STATUS_ACCESS_DENIED;
723 entry = entries;
725 DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", enum_context, PRIV_ALL_INDEX));
727 for (i = 0; i < PRIV_ALL_INDEX; i++, entry++) {
728 if( i<enum_context) {
729 init_uni_hdr(&entry->hdr_name, 0);
730 init_unistr2(&entry->name, NULL, 0 );
731 entry->luid_low = 0;
732 entry->luid_high = 0;
733 } else {
734 init_uni_hdr(&entry->hdr_name, strlen(privs[i+1].priv));
735 init_unistr2(&entry->name, privs[i+1].priv, strlen(privs[i+1].priv) );
736 entry->luid_low = privs[i+1].se_priv;
737 entry->luid_high = 0;
741 enum_context = PRIV_ALL_INDEX;
742 init_lsa_r_enum_privs(r_u, enum_context, PRIV_ALL_INDEX, entries);
744 return NT_STATUS_OK;
747 /***************************************************************************
748 _lsa_priv_get_dispname.
749 ***************************************************************************/
751 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
753 struct lsa_info *handle;
754 fstring name_asc;
755 int i=1;
757 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
758 return NT_STATUS_INVALID_HANDLE;
760 /* check if the user have enough rights */
763 * I don't know if it's the right one. not documented.
765 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
766 return NT_STATUS_ACCESS_DENIED;
768 unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
770 DEBUG(10,("_lsa_priv_get_dispname: %s", name_asc));
772 while (privs[i].se_priv!=SE_PRIV_ALL && strcmp(name_asc, privs[i].priv))
773 i++;
775 if (privs[i].se_priv!=SE_PRIV_ALL) {
776 DEBUG(10,(": %s\n", privs[i].description));
777 init_uni_hdr(&r_u->hdr_desc, strlen(privs[i].description));
778 init_unistr2(&r_u->desc, privs[i].description, strlen(privs[i].description) );
780 r_u->ptr_info=0xdeadbeef;
781 r_u->lang_id=q_u->lang_id;
782 return NT_STATUS_OK;
783 } else {
784 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
785 r_u->ptr_info=0;
786 return NT_STATUS_NO_SUCH_PRIVILEGE;
790 /***************************************************************************
791 _lsa_enum_accounts.
792 ***************************************************************************/
794 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
796 struct lsa_info *handle;
797 GROUP_MAP *map=NULL;
798 int num_entries=0;
799 LSA_SID_ENUM *sids=&r_u->sids;
800 int i=0,j=0;
802 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
803 return NT_STATUS_INVALID_HANDLE;
805 /* check if the user have enough rights */
808 * I don't know if it's the right one. not documented.
810 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
811 return NT_STATUS_ACCESS_DENIED;
813 /* get the list of mapped groups (domain, local, builtin) */
814 if(!enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
815 return NT_STATUS_OK;
817 if (q_u->enum_context >= num_entries)
818 return NT_STATUS_NO_MORE_ENTRIES;
820 sids->ptr_sid = (uint32 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(uint32));
821 sids->sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(DOM_SID2));
823 if (sids->ptr_sid==NULL || sids->sid==NULL) {
824 SAFE_FREE(map);
825 return NT_STATUS_NO_MEMORY;
828 for (i=q_u->enum_context, j=0; i<num_entries; i++) {
829 init_dom_sid2( &(*sids).sid[j], &map[i].sid);
830 (*sids).ptr_sid[j]=1;
831 j++;
834 SAFE_FREE(map);
836 init_lsa_r_enum_accounts(r_u, j);
838 return NT_STATUS_OK;
842 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
844 fstring username, domname;
845 int ulen, dlen;
846 user_struct *vuser = get_valid_user_struct(p->vuid);
848 if (vuser == NULL)
849 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
851 fstrcpy(username, vuser->user.smb_name);
852 fstrcpy(domname, vuser->user.domain);
854 ulen = strlen(username) + 1;
855 dlen = strlen(domname) + 1;
857 init_uni_hdr(&r_u->hdr_user_name, ulen);
858 r_u->ptr_user_name = 1;
859 init_unistr2(&r_u->uni2_user_name, username, ulen);
861 r_u->unk1 = 1;
863 init_uni_hdr(&r_u->hdr_dom_name, dlen);
864 r_u->ptr_dom_name = 1;
865 init_unistr2(&r_u->uni2_dom_name, domname, dlen);
867 r_u->status = NT_STATUS_OK;
869 return r_u->status;
872 /***************************************************************************
874 ***************************************************************************/
876 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
878 struct lsa_info *handle;
879 struct lsa_info *info;
881 r_u->status = NT_STATUS_OK;
883 /* find the connection policy handle. */
884 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
885 return NT_STATUS_INVALID_HANDLE;
887 /* check if the user have enough rights */
890 * I don't know if it's the right one. not documented.
891 * but guessed with rpcclient.
893 if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
894 return NT_STATUS_ACCESS_DENIED;
896 /* associate the user/group SID with the (unique) handle. */
897 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
898 return NT_STATUS_NO_MEMORY;
900 ZERO_STRUCTP(info);
901 info->sid = q_u->sid.sid;
902 info->access = q_u->access;
904 /* get a (unique) handle. open a policy on it. */
905 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
906 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
908 return r_u->status;
911 /***************************************************************************
912 For a given SID, enumerate all the privilege this account has.
913 ***************************************************************************/
915 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
917 struct lsa_info *info=NULL;
918 GROUP_MAP map;
919 int i=0;
921 LUID_ATTR *set=NULL;
923 r_u->status = NT_STATUS_OK;
925 /* find the connection policy handle. */
926 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
927 return NT_STATUS_INVALID_HANDLE;
929 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
930 return NT_STATUS_NO_SUCH_GROUP;
932 DEBUG(10,("_lsa_enum_privsaccount: %d privileges\n", map.priv_set.count));
933 if (map.priv_set.count!=0) {
935 set=(LUID_ATTR *)talloc(p->mem_ctx, map.priv_set.count*sizeof(LUID_ATTR));
936 if (set == NULL) {
937 free_privilege(&map.priv_set);
938 return NT_STATUS_NO_MEMORY;
941 for (i=0; i<map.priv_set.count; i++) {
942 set[i].luid.low=map.priv_set.set[i].luid.low;
943 set[i].luid.high=map.priv_set.set[i].luid.high;
944 set[i].attr=map.priv_set.set[i].attr;
945 DEBUG(10,("_lsa_enum_privsaccount: priv %d: %d:%d:%d\n", i,
946 set[i].luid.high, set[i].luid.low, set[i].attr));
950 init_lsa_r_enum_privsaccount(r_u, set, map.priv_set.count, 0);
951 free_privilege(&map.priv_set);
953 return r_u->status;
956 /***************************************************************************
958 ***************************************************************************/
960 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
962 struct lsa_info *info=NULL;
963 GROUP_MAP map;
964 r_u->status = NT_STATUS_OK;
966 /* find the connection policy handle. */
967 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
968 return NT_STATUS_INVALID_HANDLE;
970 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITHOUT_PRIV))
971 return NT_STATUS_NO_SUCH_GROUP;
974 0x01 -> Log on locally
975 0x02 -> Access this computer from network
976 0x04 -> Log on as a batch job
977 0x10 -> Log on as a service
979 they can be ORed together
982 r_u->access=map.systemaccount;
984 return r_u->status;
987 /***************************************************************************
988 update the systemaccount information
989 ***************************************************************************/
991 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
993 struct lsa_info *info=NULL;
994 GROUP_MAP map;
995 r_u->status = NT_STATUS_OK;
997 /* find the connection policy handle. */
998 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
999 return NT_STATUS_INVALID_HANDLE;
1001 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1002 return NT_STATUS_NO_SUCH_GROUP;
1004 map.systemaccount=q_u->access;
1006 if(!add_mapping_entry(&map, TDB_REPLACE))
1007 return NT_STATUS_NO_SUCH_GROUP;
1009 free_privilege(&map.priv_set);
1011 return r_u->status;
1014 /***************************************************************************
1015 For a given SID, add some privileges.
1016 ***************************************************************************/
1018 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
1020 struct lsa_info *info=NULL;
1021 GROUP_MAP map;
1022 int i=0;
1024 LUID_ATTR *luid_attr=NULL;
1025 PRIVILEGE_SET *set=NULL;
1027 r_u->status = NT_STATUS_OK;
1029 /* find the connection policy handle. */
1030 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1031 return NT_STATUS_INVALID_HANDLE;
1033 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1034 return NT_STATUS_NO_SUCH_GROUP;
1036 set=&q_u->set;
1038 for (i=0; i<set->count; i++) {
1039 luid_attr=&set->set[i];
1041 /* check if the privilege is already there */
1042 if (check_priv_in_privilege(&map.priv_set, *luid_attr)){
1043 free_privilege(&map.priv_set);
1044 return NT_STATUS_NO_SUCH_PRIVILEGE;
1047 add_privilege(&map.priv_set, *luid_attr);
1050 if(!add_mapping_entry(&map, TDB_REPLACE))
1051 return NT_STATUS_NO_SUCH_GROUP;
1053 free_privilege(&map.priv_set);
1055 return r_u->status;
1058 /***************************************************************************
1059 For a given SID, remove some privileges.
1060 ***************************************************************************/
1062 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1064 struct lsa_info *info=NULL;
1065 GROUP_MAP map;
1066 int i=0;
1068 LUID_ATTR *luid_attr=NULL;
1069 PRIVILEGE_SET *set=NULL;
1071 r_u->status = NT_STATUS_OK;
1073 /* find the connection policy handle. */
1074 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1075 return NT_STATUS_INVALID_HANDLE;
1077 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1078 return NT_STATUS_NO_SUCH_GROUP;
1080 if (q_u->allrights!=0) {
1081 /* log it and return, until I see one myself don't do anything */
1082 DEBUG(5,("_lsa_removeprivs: trying to remove all privileges ?\n"));
1083 return NT_STATUS_OK;
1086 if (q_u->ptr==0) {
1087 /* log it and return, until I see one myself don't do anything */
1088 DEBUG(5,("_lsa_removeprivs: no privileges to remove ?\n"));
1089 return NT_STATUS_OK;
1092 set=&q_u->set;
1094 for (i=0; i<set->count; i++) {
1095 luid_attr=&set->set[i];
1097 /* if we don't have the privilege, we're trying to remove, give up */
1098 /* what else can we do ??? JFM. */
1099 if (!check_priv_in_privilege(&map.priv_set, *luid_attr)){
1100 free_privilege(&map.priv_set);
1101 return NT_STATUS_NO_SUCH_PRIVILEGE;
1104 remove_privilege(&map.priv_set, *luid_attr);
1107 if(!add_mapping_entry(&map, TDB_REPLACE))
1108 return NT_STATUS_NO_SUCH_GROUP;
1110 free_privilege(&map.priv_set);
1112 return r_u->status;
1115 /***************************************************************************
1116 For a given SID, remove some privileges.
1117 ***************************************************************************/
1119 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1121 struct lsa_info *handle=NULL;
1122 SEC_DESC *psd = NULL;
1123 size_t sd_size;
1124 NTSTATUS status;
1126 r_u->status = NT_STATUS_OK;
1128 /* find the connection policy handle. */
1129 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1130 return NT_STATUS_INVALID_HANDLE;
1132 /* check if the user have enough rights */
1133 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1134 return NT_STATUS_ACCESS_DENIED;
1137 switch (q_u->sec_info) {
1138 case 1:
1139 /* SD contains only the owner */
1141 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1142 if(!NT_STATUS_IS_OK(status))
1143 return NT_STATUS_NO_MEMORY;
1146 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1147 return NT_STATUS_NO_MEMORY;
1148 break;
1149 case 4:
1150 /* SD contains only the ACL */
1152 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1153 if(!NT_STATUS_IS_OK(status))
1154 return NT_STATUS_NO_MEMORY;
1156 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1157 return NT_STATUS_NO_MEMORY;
1158 break;
1159 default:
1160 return NT_STATUS_INVALID_LEVEL;
1163 r_u->ptr=1;
1165 return r_u->status;