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,
10 * Copyright (C) Simo Sorce 2003.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /* This is the implementation of the lsa server code. */
32 #define DBGC_CLASS DBGC_RPC_SRV
41 struct generic_mapping lsa_generic_mapping = {
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;
59 /***************************************************************************
61 ***************************************************************************/
63 static void init_dom_query(DOM_QUERY *d_q, const char *dom_name, DOM_SID *dom_sid)
65 d_q->buffer_dom_name = (dom_name != NULL) ? 1 : 0; /* domain buffer pointer */
66 d_q->buffer_dom_sid = (dom_sid != NULL) ? 1 : 0; /* domain sid pointer */
68 /* this string is supposed to be non-null terminated. */
69 /* But the maxlen in this UNISTR2 must include the terminating null. */
70 init_unistr2(&d_q->uni_domain_name, dom_name, UNI_BROKEN_NON_NULL);
73 * I'm not sure why this really odd combination of length
74 * values works, but it does appear to. I need to look at
75 * this *much* more closely - but at the moment leave alone
76 * until it's understood. This allows a W2k client to join
77 * a domain with both odd and even length names... JRA.
82 * The two fields below probably are reversed in meaning, ie.
83 * the first field is probably the str_len, the second the max
84 * len. Both are measured in bytes anyway.
87 d_q->uni_dom_str_len = d_q->uni_domain_name.uni_max_len * 2;
88 d_q->uni_dom_max_len = d_q->uni_domain_name.uni_str_len * 2;
91 init_dom_sid2(&d_q->dom_sid, dom_sid);
94 /***************************************************************************
95 init_dom_ref - adds a domain if it's not already in, returns the index.
96 ***************************************************************************/
98 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
102 if (dom_name != NULL) {
103 for (num = 0; num < ref->num_ref_doms_1; num++) {
105 rpcstr_pull(domname, ref->ref_dom[num].uni_dom_name.buffer, sizeof(domname), -1, 0);
106 if (strequal(domname, dom_name))
110 num = ref->num_ref_doms_1;
113 if (num >= MAX_REF_DOMAINS) {
114 /* index not found, already at maximum domain limit */
118 ref->num_ref_doms_1 = num+1;
119 ref->ptr_ref_dom = 1;
120 ref->max_entries = MAX_REF_DOMAINS;
121 ref->num_ref_doms_2 = num+1;
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, UNI_FLAGS_NONE);
126 init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, &ref->ref_dom[num].uni_dom_name);
128 init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
133 /***************************************************************************
135 ***************************************************************************/
137 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
138 int num_entries, UNISTR2 *name,
139 uint32 *mapped_count, BOOL endian)
145 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
147 become_root(); /* lookup_name can require root privs */
149 for (i = 0; i < num_entries; i++) {
152 uint32 rid = 0xffffffff;
155 fstring dom_name, user;
156 enum SID_NAME_USE name_type = SID_NAME_UNKNOWN;
158 /* Split name into domain and user component */
160 unistr2_to_ascii(full_name, &name[i], sizeof(full_name));
161 split_domain_name(full_name, dom_name, user);
165 DEBUG(5, ("init_lsa_rid2s: looking up name %s\n", full_name));
167 status = lookup_name(dom_name, user, &sid, &name_type);
169 if((name_type == SID_NAME_UNKNOWN) && (lp_server_role() == ROLE_DOMAIN_MEMBER) && (strncmp(dom_name, full_name, strlen(dom_name)) != 0)) {
170 DEBUG(5, ("init_lsa_rid2s: domain name not provided and local account not found, using member domain\n"));
171 fstrcpy(dom_name, lp_workgroup());
172 status = lookup_name(dom_name, user, &sid, &name_type);
175 #if 0 /* This is not true. */
176 if (name_type == SID_NAME_WKN_GRP) {
177 /* BUILTIN aliases are still aliases :-) */
178 name_type = SID_NAME_ALIAS;
182 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" :
185 if (status && name_type != SID_NAME_UNKNOWN) {
186 sid_split_rid(&sid, &rid);
187 dom_idx = init_dom_ref(ref, dom_name, &sid);
192 name_type = SID_NAME_UNKNOWN;
195 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
202 /***************************************************************************
203 init_reply_lookup_names
204 ***************************************************************************/
206 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
207 DOM_R_REF *ref, uint32 num_entries,
208 DOM_RID2 *rid2, uint32 mapped_count)
210 r_l->ptr_dom_ref = 1;
213 r_l->num_entries = num_entries;
214 r_l->ptr_entries = 1;
215 r_l->num_entries2 = num_entries;
218 r_l->mapped_count = mapped_count;
221 /***************************************************************************
222 Init lsa_trans_names.
223 ***************************************************************************/
225 static void init_lsa_trans_names(TALLOC_CTX *ctx, DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
226 int num_entries, DOM_SID2 *sid,
227 uint32 *mapped_count)
233 /* Allocate memory for list of names */
235 if (num_entries > 0) {
236 if (!(trn->name = TALLOC_ARRAY(ctx, LSA_TRANS_NAME, num_entries))) {
237 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
241 if (!(trn->uni_name = TALLOC_ARRAY(ctx, UNISTR2, num_entries))) {
242 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
247 become_root(); /* Need root to get to passdb to for local sids */
249 for (i = 0; i < num_entries; i++) {
251 DOM_SID find_sid = sid[i].sid;
252 uint32 rid = 0xffffffff;
254 fstring name, dom_name;
255 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
257 sid_to_string(name, &find_sid);
258 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
260 /* Lookup sid from winbindd */
262 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
264 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" :
268 sid_name_use = SID_NAME_UNKNOWN;
269 memset(dom_name, '\0', sizeof(dom_name));
270 sid_to_string(name, &find_sid);
273 DEBUG(10,("init_lsa_trans_names: added unknown user '%s' to "
274 "referenced list.\n", name ));
277 /* Store domain sid in ref array */
278 if (find_sid.num_auths == 5) {
279 sid_split_rid(&find_sid, &rid);
281 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
283 DEBUG(10,("init_lsa_trans_names: added %s '%s\\%s' (%d) to referenced list.\n",
284 sid_type_lookup(sid_name_use), dom_name, name, sid_name_use ));
288 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
289 sid_name_use, name, dom_idx);
295 trn->num_entries = total;
296 trn->ptr_trans_names = 1;
297 trn->num_entries2 = total;
300 /***************************************************************************
301 Init_reply_lookup_sids.
302 ***************************************************************************/
304 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
305 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
308 r_l->ptr_dom_ref = 1;
311 r_l->mapped_count = mapped_count;
314 static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size)
316 extern DOM_SID global_sid_World;
317 extern DOM_SID global_sid_Builtin;
318 DOM_SID local_adm_sid;
326 init_sec_access(&mask, POLICY_EXECUTE);
327 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
329 sid_copy(&adm_sid, get_global_sam_sid());
330 sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
331 init_sec_access(&mask, POLICY_ALL_ACCESS);
332 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
334 sid_copy(&local_adm_sid, &global_sid_Builtin);
335 sid_append_rid(&local_adm_sid, BUILTIN_ALIAS_RID_ADMINS);
336 init_sec_access(&mask, POLICY_ALL_ACCESS);
337 init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
339 if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
340 return NT_STATUS_NO_MEMORY;
342 if((*sd = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, &adm_sid, NULL, NULL, psa, sd_size)) == NULL)
343 return NT_STATUS_NO_MEMORY;
348 /***************************************************************************
350 ***************************************************************************/
352 static void init_dns_dom_info(LSA_DNS_DOM_INFO *r_l, const char *nb_name,
353 const char *dns_name, const char *forest_name,
354 struct uuid *dom_guid, DOM_SID *dom_sid)
356 if (nb_name && *nb_name) {
357 init_unistr2(&r_l->uni_nb_dom_name, nb_name, UNI_FLAGS_NONE);
358 init_uni_hdr(&r_l->hdr_nb_dom_name, &r_l->uni_nb_dom_name);
359 r_l->hdr_nb_dom_name.uni_max_len += 2;
360 r_l->uni_nb_dom_name.uni_max_len += 1;
363 if (dns_name && *dns_name) {
364 init_unistr2(&r_l->uni_dns_dom_name, dns_name, UNI_FLAGS_NONE);
365 init_uni_hdr(&r_l->hdr_dns_dom_name, &r_l->uni_dns_dom_name);
366 r_l->hdr_dns_dom_name.uni_max_len += 2;
367 r_l->uni_dns_dom_name.uni_max_len += 1;
370 if (forest_name && *forest_name) {
371 init_unistr2(&r_l->uni_forest_name, forest_name, UNI_FLAGS_NONE);
372 init_uni_hdr(&r_l->hdr_forest_name, &r_l->uni_forest_name);
373 r_l->hdr_forest_name.uni_max_len += 2;
374 r_l->uni_forest_name.uni_max_len += 1;
377 /* how do we init the guid ? probably should write an init fn */
379 memcpy(&r_l->dom_guid, dom_guid, sizeof(struct uuid));
383 r_l->ptr_dom_sid = 1;
384 init_dom_sid2(&r_l->dom_sid, dom_sid);
388 /***************************************************************************
390 ***************************************************************************/
392 NTSTATUS _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
394 struct lsa_info *info;
395 SEC_DESC *psd = NULL;
397 uint32 des_access=q_u->des_access;
402 /* map the generic bits to the lsa policy ones */
403 se_map_generic(&des_access, &lsa_generic_mapping);
405 /* get the generic lsa policy SD until we store it */
406 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
408 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
409 if (geteuid() != 0) {
412 DEBUG(4,("ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
413 acc_granted, des_access));
414 DEBUGADD(4,("but overwritten by euid == 0\n"));
417 /* This is needed for lsa_open_account and rpcclient .... :-) */
420 acc_granted = POLICY_ALL_ACCESS;
422 /* associate the domain SID with the (unique) handle. */
423 if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
424 return NT_STATUS_NO_MEMORY;
427 sid_copy(&info->sid,get_global_sam_sid());
428 info->access = acc_granted;
430 /* set up the LSA QUERY INFO response */
431 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
432 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
437 /***************************************************************************
439 ***************************************************************************/
441 NTSTATUS _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
443 struct lsa_info *info;
444 SEC_DESC *psd = NULL;
446 uint32 des_access=q_u->des_access;
451 /* map the generic bits to the lsa policy ones */
452 se_map_generic(&des_access, &lsa_generic_mapping);
454 /* get the generic lsa policy SD until we store it */
455 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
457 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
458 if (geteuid() != 0) {
461 DEBUG(4,("ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
462 acc_granted, des_access));
463 DEBUGADD(4,("but overwritten by euid == 0\n"));
464 acc_granted = des_access;
467 /* associate the domain SID with the (unique) handle. */
468 if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
469 return NT_STATUS_NO_MEMORY;
472 sid_copy(&info->sid,get_global_sam_sid());
473 info->access = acc_granted;
475 /* set up the LSA QUERY INFO response */
476 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
477 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
482 /***************************************************************************
483 _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
485 ***************************************************************************/
487 NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
489 struct lsa_info *info;
490 uint32 enum_context = q_u->enum_context;
493 * preferred length is set to 5 as a "our" preferred length
494 * nt sets this parameter to 2
495 * update (20.08.2002): it's not preferred length, but preferred size!
496 * it needs further investigation how to optimally choose this value
498 uint32 max_num_domains = q_u->preferred_len < 5 ? q_u->preferred_len : 10;
499 TRUSTDOM **trust_doms;
503 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
504 return NT_STATUS_INVALID_HANDLE;
506 /* check if the user have enough rights */
507 if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
508 return NT_STATUS_ACCESS_DENIED;
510 nt_status = secrets_get_trusted_domains(p->mem_ctx, (int *)&enum_context, max_num_domains, (int *)&num_domains, &trust_doms);
512 if (!NT_STATUS_IS_OK(nt_status) &&
513 !NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES) &&
514 !NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
517 r_u->status = nt_status;
520 /* set up the lsa_enum_trust_dom response */
521 init_r_enum_trust_dom(p->mem_ctx, r_u, enum_context, max_num_domains, num_domains, trust_doms);
526 /***************************************************************************
527 _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
528 ***************************************************************************/
530 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
532 struct lsa_info *handle;
533 LSA_INFO_UNION *info = &r_u->dom;
538 r_u->status = NT_STATUS_OK;
540 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
541 return NT_STATUS_INVALID_HANDLE;
543 switch (q_u->info_class) {
547 /* check if the user have enough rights */
548 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
549 return NT_STATUS_ACCESS_DENIED;
551 /* fake info: We audit everything. ;) */
552 info->id2.auditing_enabled = 1;
553 info->id2.count1 = 7;
554 info->id2.count2 = 7;
555 if ((info->id2.auditsettings = TALLOC_ARRAY(p->mem_ctx,uint32, 7)) == NULL)
556 return NT_STATUS_NO_MEMORY;
557 for (i = 0; i < 7; i++)
558 info->id2.auditsettings[i] = 3;
562 /* check if the user have enough rights */
563 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
564 return NT_STATUS_ACCESS_DENIED;
566 /* Request PolicyPrimaryDomainInformation. */
567 switch (lp_server_role()) {
568 case ROLE_DOMAIN_PDC:
569 case ROLE_DOMAIN_BDC:
570 name = get_global_sam_name();
571 sid = get_global_sam_sid();
573 case ROLE_DOMAIN_MEMBER:
574 name = lp_workgroup();
575 /* We need to return the Domain SID here. */
576 if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid))
579 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
581 case ROLE_STANDALONE:
582 name = lp_workgroup();
586 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
588 init_dom_query(&r_u->dom.id3, name, sid);
591 /* check if the user have enough rights */
592 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
593 return NT_STATUS_ACCESS_DENIED;
595 /* Request PolicyAccountDomainInformation. */
596 name = get_global_sam_name();
597 sid = get_global_sam_sid();
598 init_dom_query(&r_u->dom.id5, name, sid);
601 /* check if the user have enough rights */
602 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
603 return NT_STATUS_ACCESS_DENIED;
605 switch (lp_server_role()) {
606 case ROLE_DOMAIN_BDC:
608 * only a BDC is a backup controller
609 * of the domain, it controls.
611 info->id6.server_role = 2;
615 * any other role is a primary
616 * of the domain, it controls.
618 info->id6.server_role = 3;
623 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
624 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
628 if (NT_STATUS_IS_OK(r_u->status)) {
629 r_u->undoc_buffer = 0x22000000; /* bizarre */
630 r_u->info_class = q_u->info_class;
636 /***************************************************************************
638 ***************************************************************************/
640 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
642 struct lsa_info *handle;
643 DOM_SID2 *sid = q_u->sids.sid;
644 int num_entries = q_u->sids.num_entries;
645 DOM_R_REF *ref = NULL;
646 LSA_TRANS_NAME_ENUM *names = NULL;
647 uint32 mapped_count = 0;
649 if (num_entries > MAX_LOOKUP_SIDS) {
650 num_entries = MAX_LOOKUP_SIDS;
651 DEBUG(5,("_lsa_lookup_sids: truncating SID lookup list to %d\n", num_entries));
654 ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
655 names = TALLOC_ZERO_P(p->mem_ctx, LSA_TRANS_NAME_ENUM);
657 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
658 r_u->status = NT_STATUS_INVALID_HANDLE;
662 /* check if the user have enough rights */
663 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
664 r_u->status = NT_STATUS_ACCESS_DENIED;
668 return NT_STATUS_NO_MEMORY;
672 /* set up the LSA Lookup SIDs response */
673 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
674 if (NT_STATUS_IS_OK(r_u->status)) {
675 if (mapped_count == 0)
676 r_u->status = NT_STATUS_NONE_MAPPED;
677 else if (mapped_count != num_entries)
678 r_u->status = STATUS_SOME_UNMAPPED;
680 init_reply_lookup_sids(r_u, ref, names, mapped_count);
685 /***************************************************************************
686 lsa_reply_lookup_names
687 ***************************************************************************/
689 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
691 struct lsa_info *handle;
692 UNISTR2 *names = q_u->uni_name;
693 int num_entries = q_u->num_entries;
696 uint32 mapped_count = 0;
698 if (num_entries > MAX_LOOKUP_SIDS) {
699 num_entries = MAX_LOOKUP_SIDS;
700 DEBUG(5,("_lsa_lookup_names: truncating name lookup list to %d\n", num_entries));
703 ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
704 rids = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_RID2, num_entries);
706 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
707 r_u->status = NT_STATUS_INVALID_HANDLE;
711 /* check if the user have enough rights */
712 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
713 r_u->status = NT_STATUS_ACCESS_DENIED;
718 return NT_STATUS_NO_MEMORY;
722 /* set up the LSA Lookup RIDs response */
723 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
724 if (NT_STATUS_IS_OK(r_u->status)) {
725 if (mapped_count == 0)
726 r_u->status = NT_STATUS_NONE_MAPPED;
727 else if (mapped_count != num_entries)
728 r_u->status = STATUS_SOME_UNMAPPED;
730 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
735 /***************************************************************************
736 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
737 ***************************************************************************/
739 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
741 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
742 return NT_STATUS_INVALID_HANDLE;
744 close_policy_hnd(p, &q_u->pol);
748 /***************************************************************************
749 "No more secrets Marty...." :-).
750 ***************************************************************************/
752 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
754 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
757 /***************************************************************************
759 ***************************************************************************/
761 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
763 struct lsa_info *handle;
765 uint32 enum_context = q_u->enum_context;
766 int num_privs = count_all_privileges();
767 LSA_PRIV_ENTRY *entries = NULL;
769 /* remember that the enum_context starts at 0 and not 1 */
771 if ( enum_context >= num_privs )
772 return NT_STATUS_NO_MORE_ENTRIES;
774 DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n",
775 enum_context, num_privs));
777 if ( !(entries = TALLOC_ZERO_ARRAY(p->mem_ctx, LSA_PRIV_ENTRY, num_privs + 1)))
778 return NT_STATUS_NO_MEMORY;
780 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
781 return NT_STATUS_INVALID_HANDLE;
783 /* check if the user have enough rights
784 I don't know if it's the right one. not documented. */
786 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
787 return NT_STATUS_ACCESS_DENIED;
789 if ( !(entries = TALLOC_ZERO_ARRAY(p->mem_ctx, LSA_PRIV_ENTRY, num_privs )) )
790 return NT_STATUS_NO_MEMORY;
793 for (i = 0; i < num_privs; i++) {
794 if( i < enum_context) {
795 init_unistr2(&entries[i].name, NULL, UNI_FLAGS_NONE);
796 init_uni_hdr(&entries[i].hdr_name, &entries[i].name);
797 entries[i].luid_low = 0;
798 entries[i].luid_high = 0;
800 init_unistr2(&entries[i].name, privs[i].name, UNI_FLAGS_NONE);
801 init_uni_hdr(&entries[i].hdr_name, &entries[i].name);
802 entries[i].luid_low = privs[i].se_priv;
803 entries[i].luid_high = 0;
807 enum_context = num_privs;
809 init_lsa_r_enum_privs(r_u, enum_context, num_privs, entries);
814 /***************************************************************************
815 _lsa_priv_get_dispname.
816 ***************************************************************************/
818 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
820 struct lsa_info *handle;
824 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
825 return NT_STATUS_INVALID_HANDLE;
827 /* check if the user have enough rights */
830 * I don't know if it's the right one. not documented.
832 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
833 return NT_STATUS_ACCESS_DENIED;
835 unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
837 DEBUG(10,("_lsa_priv_get_dispname: %s\n", name_asc));
839 while (privs[i].se_priv != SE_END && !strequal(name_asc, privs[i].name))
842 if (privs[i].se_priv != SE_END) {
843 DEBUG(10,(": %s\n", privs[i].description));
844 init_unistr2(&r_u->desc, privs[i].description, UNI_FLAGS_NONE);
845 init_uni_hdr(&r_u->hdr_desc, &r_u->desc);
847 r_u->ptr_info = 0xdeadbeef;
848 r_u->lang_id = q_u->lang_id;
851 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
853 return NT_STATUS_NO_SUCH_PRIVILEGE;
857 /***************************************************************************
859 ***************************************************************************/
861 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
863 struct lsa_info *handle;
865 int i, j, num_entries;
866 LSA_SID_ENUM *sids=&r_u->sids;
869 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
870 return NT_STATUS_INVALID_HANDLE;
872 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
873 return NT_STATUS_ACCESS_DENIED;
878 /* The only way we can currently find out all the SIDs that have been
879 privileged is to scan all privileges */
881 if (!NT_STATUS_IS_OK(ret = privilege_enumerate_accounts(&sid_list, &num_entries))) {
885 if (q_u->enum_context >= num_entries)
886 return NT_STATUS_NO_MORE_ENTRIES;
888 sids->ptr_sid = TALLOC_ZERO_ARRAY(p->mem_ctx, uint32, num_entries-q_u->enum_context);
889 sids->sid = TALLOC_ZERO_ARRAY(p->mem_ctx, DOM_SID2, num_entries-q_u->enum_context);
891 if (sids->ptr_sid==NULL || sids->sid==NULL) {
893 return NT_STATUS_NO_MEMORY;
896 for (i = q_u->enum_context, j = 0; i < num_entries; i++, j++) {
897 init_dom_sid2(&(*sids).sid[j], &sid_list[i]);
898 (*sids).ptr_sid[j] = 1;
903 init_lsa_r_enum_accounts(r_u, num_entries);
909 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
911 fstring username, domname;
912 user_struct *vuser = get_valid_user_struct(p->vuid);
915 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
917 fstrcpy(username, vuser->user.smb_name);
918 fstrcpy(domname, vuser->user.domain);
920 r_u->ptr_user_name = 1;
921 init_unistr2(&r_u->uni2_user_name, username, UNI_STR_TERMINATE);
922 init_uni_hdr(&r_u->hdr_user_name, &r_u->uni2_user_name);
926 r_u->ptr_dom_name = 1;
927 init_unistr2(&r_u->uni2_dom_name, domname, UNI_STR_TERMINATE);
928 init_uni_hdr(&r_u->hdr_dom_name, &r_u->uni2_dom_name);
930 r_u->status = NT_STATUS_OK;
935 /***************************************************************************
937 ***************************************************************************/
939 NTSTATUS _lsa_create_account(pipes_struct *p, LSA_Q_CREATEACCOUNT *q_u, LSA_R_CREATEACCOUNT *r_u)
941 struct lsa_info *handle;
942 struct lsa_info *info;
944 /* find the connection policy handle. */
945 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
946 return NT_STATUS_INVALID_HANDLE;
948 /* check if the user have enough rights */
951 * I don't know if it's the right one. not documented.
952 * but guessed with rpcclient.
954 if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
955 return NT_STATUS_ACCESS_DENIED;
957 /* check to see if the pipe_user is a Domain Admin since
958 account_pol.tdb was already opened as root, this is all we have */
960 if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
961 return NT_STATUS_ACCESS_DENIED;
963 /* associate the user/group SID with the (unique) handle. */
965 if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
966 return NT_STATUS_NO_MEMORY;
969 info->sid = q_u->sid.sid;
970 info->access = q_u->access;
972 /* get a (unique) handle. open a policy on it. */
973 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
974 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
976 return privilege_create_account( &info->sid );
980 /***************************************************************************
982 ***************************************************************************/
984 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
986 struct lsa_info *handle;
987 struct lsa_info *info;
989 /* find the connection policy handle. */
990 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
991 return NT_STATUS_INVALID_HANDLE;
993 /* check if the user have enough rights */
996 * I don't know if it's the right one. not documented.
997 * but guessed with rpcclient.
999 if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
1000 return NT_STATUS_ACCESS_DENIED;
1002 /* TODO: Fis the parsing routine before reenabling this check! */
1004 if (!lookup_sid(&handle->sid, dom_name, name, &type))
1005 return NT_STATUS_ACCESS_DENIED;
1007 /* associate the user/group SID with the (unique) handle. */
1008 if ((info = SMB_MALLOC_P(struct lsa_info)) == NULL)
1009 return NT_STATUS_NO_MEMORY;
1012 info->sid = q_u->sid.sid;
1013 info->access = q_u->access;
1015 /* get a (unique) handle. open a policy on it. */
1016 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
1017 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1019 return NT_STATUS_OK;
1022 /***************************************************************************
1023 For a given SID, enumerate all the privilege this account has.
1024 ***************************************************************************/
1026 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, prs_struct *ps, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
1028 struct lsa_info *info=NULL;
1031 /* find the connection policy handle. */
1032 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1033 return NT_STATUS_INVALID_HANDLE;
1035 privilege_set_init( &priv );
1037 get_privileges_for_sids( &priv, &info->sid, 1 );
1039 DEBUG(10,("_lsa_enum_privsaccount: %s has %d privileges\n",
1040 sid_string_static(&info->sid), priv.count));
1042 init_lsa_r_enum_privsaccount(ps->mem_ctx, r_u, priv.set, priv.count, 0);
1044 privilege_set_free( &priv );
1046 return NT_STATUS_OK;
1049 /***************************************************************************
1051 ***************************************************************************/
1053 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
1055 struct lsa_info *info=NULL;
1056 fstring name, dom_name;
1057 enum SID_NAME_USE type;
1059 /* find the connection policy handle. */
1061 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1062 return NT_STATUS_INVALID_HANDLE;
1064 if (!lookup_sid(&info->sid, dom_name, name, &type))
1065 return NT_STATUS_ACCESS_DENIED;
1068 0x01 -> Log on locally
1069 0x02 -> Access this computer from network
1070 0x04 -> Log on as a batch job
1071 0x10 -> Log on as a service
1073 they can be ORed together
1076 r_u->access = PR_LOG_ON_LOCALLY | PR_ACCESS_FROM_NETWORK;
1078 return NT_STATUS_OK;
1081 /***************************************************************************
1082 update the systemaccount information
1083 ***************************************************************************/
1085 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
1087 struct lsa_info *info=NULL;
1089 r_u->status = NT_STATUS_OK;
1091 /* find the connection policy handle. */
1092 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1093 return NT_STATUS_INVALID_HANDLE;
1095 /* check to see if the pipe_user is a Domain Admin since
1096 account_pol.tdb was already opened as root, this is all we have */
1098 if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1099 return NT_STATUS_ACCESS_DENIED;
1101 if (!pdb_getgrsid(&map, info->sid))
1102 return NT_STATUS_NO_SUCH_GROUP;
1104 if(!pdb_update_group_mapping_entry(&map))
1105 return NT_STATUS_NO_SUCH_GROUP;
1110 /***************************************************************************
1111 For a given SID, add some privileges.
1112 ***************************************************************************/
1114 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
1116 struct lsa_info *info = NULL;
1119 PRIVILEGE_SET *set = NULL;
1121 /* find the connection policy handle. */
1122 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1123 return NT_STATUS_INVALID_HANDLE;
1125 /* check to see if the pipe_user is a Domain Admin since
1126 account_pol.tdb was already opened as root, this is all we have */
1128 if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1129 return NT_STATUS_ACCESS_DENIED;
1133 for (i = 0; i < set->count; i++) {
1135 mask = luid_to_privilege_mask( &(set->set[i].luid) );
1137 if ( mask != SE_END ) {
1138 if ( !grant_privilege( &info->sid, mask ) ) {
1139 DEBUG(3,("_lsa_addprivs: grant_privilege( %s, 0x%x) failed!\n",
1140 sid_string_static(&info->sid), mask ));
1141 return NT_STATUS_NO_SUCH_PRIVILEGE;
1146 return NT_STATUS_OK;
1149 /***************************************************************************
1150 For a given SID, remove some privileges.
1151 ***************************************************************************/
1153 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1155 struct lsa_info *info = NULL;
1158 PRIVILEGE_SET *set = NULL;
1160 /* find the connection policy handle. */
1161 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1162 return NT_STATUS_INVALID_HANDLE;
1164 /* check to see if the pipe_user is a Domain Admin since
1165 account_pol.tdb was already opened as root, this is all we have */
1167 if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
1168 return NT_STATUS_ACCESS_DENIED;
1172 for (i = 0; i < set->count; i++) {
1173 mask = luid_to_privilege_mask( &(set->set[i].luid) );
1175 if ( mask != SE_END ) {
1176 if ( !revoke_privilege( &info->sid, mask ) ) {
1177 DEBUG(3,("_lsa_removeprivs: revoke_privilege( %s, 0x%x) failed!\n",
1178 sid_string_static(&info->sid), mask ));
1179 return NT_STATUS_NO_SUCH_PRIVILEGE;
1184 return NT_STATUS_OK;
1187 /***************************************************************************
1188 For a given SID, remove some privileges.
1189 ***************************************************************************/
1191 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1193 struct lsa_info *handle=NULL;
1194 SEC_DESC *psd = NULL;
1198 r_u->status = NT_STATUS_OK;
1200 /* find the connection policy handle. */
1201 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1202 return NT_STATUS_INVALID_HANDLE;
1204 /* check if the user have enough rights */
1205 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1206 return NT_STATUS_ACCESS_DENIED;
1209 switch (q_u->sec_info) {
1211 /* SD contains only the owner */
1213 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1214 if(!NT_STATUS_IS_OK(status))
1215 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;
1222 /* SD contains only the ACL */
1224 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1225 if(!NT_STATUS_IS_OK(status))
1226 return NT_STATUS_NO_MEMORY;
1228 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1229 return NT_STATUS_NO_MEMORY;
1232 return NT_STATUS_INVALID_LEVEL;
1241 NTSTATUS _lsa_query_info2(pipes_struct *p, LSA_Q_QUERY_INFO2 *q_u, LSA_R_QUERY_INFO2 *r_u)
1243 struct lsa_info *handle;
1244 const char *nb_name;
1245 char *dns_name = NULL;
1246 char *forest_name = NULL;
1247 DOM_SID *sid = NULL;
1252 r_u->status = NT_STATUS_OK;
1254 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1255 return NT_STATUS_INVALID_HANDLE;
1257 switch (q_u->info_class) {
1259 /* check if the user have enough rights */
1260 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1261 return NT_STATUS_ACCESS_DENIED;
1263 /* Request PolicyPrimaryDomainInformation. */
1264 switch (lp_server_role()) {
1265 case ROLE_DOMAIN_PDC:
1266 case ROLE_DOMAIN_BDC:
1267 nb_name = get_global_sam_name();
1268 /* ugly temp hack for these next two */
1270 /* This should be a 'netbios domain -> DNS domain' mapping */
1271 dnsdomname[0] = '\0';
1272 get_mydnsdomname(dnsdomname);
1273 strlower_m(dnsdomname);
1275 dns_name = dnsdomname;
1276 forest_name = dnsdomname;
1278 sid = get_global_sam_sid();
1279 secrets_fetch_domain_guid(lp_workgroup(), &guid);
1282 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1284 init_dns_dom_info(&r_u->info.dns_dom_info, nb_name, dns_name,
1285 forest_name,&guid,sid);
1288 DEBUG(0,("_lsa_query_info2: unknown info level in Lsa Query: %d\n", q_u->info_class));
1289 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
1293 if (NT_STATUS_IS_OK(r_u->status)) {
1295 r_u->info_class = q_u->info_class;