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. */
31 #define DBGC_CLASS DBGC_RPC_SRV
40 struct generic_mapping lsa_generic_mapping = {
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;
58 /***************************************************************************
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.
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;
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)
101 if (dom_name != NULL) {
102 for (num = 0; num < ref->num_ref_doms_1; num++) {
104 rpcstr_pull(domname, &ref->ref_dom[num].uni_dom_name, sizeof(domname), -1, 0);
105 if (strequal(domname, dom_name))
109 num = ref->num_ref_doms_1;
112 if (num >= MAX_REF_DOMAINS) {
113 /* index not found, already at maximum domain limit */
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 );
132 /***************************************************************************
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)
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++) {
151 uint32 rid = 0xffffffff;
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);
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 #if 0 /* This is not true. */
175 if (name_type == SID_NAME_WKN_GRP) {
176 /* BUILTIN aliases are still aliases :-) */
177 name_type = SID_NAME_ALIAS;
181 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" :
184 if (status && name_type != SID_NAME_UNKNOWN) {
185 sid_split_rid(&sid, &rid);
186 dom_idx = init_dom_ref(ref, dom_name, &sid);
191 name_type = SID_NAME_UNKNOWN;
194 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
201 /***************************************************************************
202 init_reply_lookup_names
203 ***************************************************************************/
205 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
206 DOM_R_REF *ref, uint32 num_entries,
207 DOM_RID2 *rid2, uint32 mapped_count)
209 r_l->ptr_dom_ref = 1;
212 r_l->num_entries = num_entries;
213 r_l->ptr_entries = 1;
214 r_l->num_entries2 = num_entries;
217 r_l->mapped_count = mapped_count;
220 /***************************************************************************
221 Init lsa_trans_names.
222 ***************************************************************************/
224 static void init_lsa_trans_names(TALLOC_CTX *ctx, DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
225 int num_entries, DOM_SID2 *sid,
226 uint32 *mapped_count)
232 /* Allocate memory for list of names */
234 if (num_entries > 0) {
235 if (!(trn->name = (LSA_TRANS_NAME *)talloc(ctx, sizeof(LSA_TRANS_NAME) *
237 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
241 if (!(trn->uni_name = (UNISTR2 *)talloc(ctx, sizeof(UNISTR2) *
243 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
248 become_root(); /* Need root to get to passdb to for local sids */
250 for (i = 0; i < num_entries; i++) {
252 DOM_SID find_sid = sid[i].sid;
253 uint32 rid = 0xffffffff;
255 fstring name, dom_name;
256 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
258 sid_to_string(name, &find_sid);
259 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
261 /* Lookup sid from winbindd */
263 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
265 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" :
269 sid_name_use = SID_NAME_UNKNOWN;
270 memset(dom_name, '\0', sizeof(dom_name));
271 sid_to_string(name, &find_sid);
274 DEBUG(10,("init_lsa_trans_names: added unknown user '%s' to "
275 "referenced list.\n", name ));
278 /* Store domain sid in ref array */
279 if (find_sid.num_auths == 5) {
280 sid_split_rid(&find_sid, &rid);
282 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
284 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
285 "referenced list.\n", dom_name, name ));
289 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
290 sid_name_use, name, dom_idx);
296 trn->num_entries = total;
297 trn->ptr_trans_names = 1;
298 trn->num_entries2 = total;
301 /***************************************************************************
302 Init_reply_lookup_sids.
303 ***************************************************************************/
305 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
306 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
309 r_l->ptr_dom_ref = 1;
312 r_l->mapped_count = mapped_count;
315 static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size)
317 extern DOM_SID global_sid_World;
318 extern DOM_SID global_sid_Builtin;
319 DOM_SID local_adm_sid;
327 init_sec_access(&mask, POLICY_EXECUTE);
328 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
330 sid_copy(&adm_sid, get_global_sam_sid());
331 sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
332 init_sec_access(&mask, POLICY_ALL_ACCESS);
333 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
335 sid_copy(&local_adm_sid, &global_sid_Builtin);
336 sid_append_rid(&local_adm_sid, BUILTIN_ALIAS_RID_ADMINS);
337 init_sec_access(&mask, POLICY_ALL_ACCESS);
338 init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
340 if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
341 return NT_STATUS_NO_MEMORY;
343 if((*sd = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, &adm_sid, NULL, NULL, psa, sd_size)) == NULL)
344 return NT_STATUS_NO_MEMORY;
349 /***************************************************************************
351 ***************************************************************************/
353 static void init_dns_dom_info(LSA_DNS_DOM_INFO *r_l, const char *nb_name,
354 const char *dns_name, const char *forest_name,
355 struct uuid *dom_guid, DOM_SID *dom_sid)
357 if (nb_name && *nb_name) {
358 init_unistr2(&r_l->uni_nb_dom_name, nb_name, UNI_FLAGS_NONE);
359 init_uni_hdr(&r_l->hdr_nb_dom_name, &r_l->uni_nb_dom_name);
360 r_l->hdr_nb_dom_name.uni_max_len += 2;
361 r_l->uni_nb_dom_name.uni_max_len += 1;
364 if (dns_name && *dns_name) {
365 init_unistr2(&r_l->uni_dns_dom_name, dns_name, UNI_FLAGS_NONE);
366 init_uni_hdr(&r_l->hdr_dns_dom_name, &r_l->uni_dns_dom_name);
367 r_l->hdr_dns_dom_name.uni_max_len += 2;
368 r_l->uni_dns_dom_name.uni_max_len += 1;
371 if (forest_name && *forest_name) {
372 init_unistr2(&r_l->uni_forest_name, forest_name, UNI_FLAGS_NONE);
373 init_uni_hdr(&r_l->hdr_forest_name, &r_l->uni_forest_name);
374 r_l->hdr_forest_name.uni_max_len += 2;
375 r_l->uni_forest_name.uni_max_len += 1;
378 /* how do we init the guid ? probably should write an init fn */
380 memcpy(&r_l->dom_guid, dom_guid, sizeof(struct uuid));
384 r_l->ptr_dom_sid = 1;
385 init_dom_sid2(&r_l->dom_sid, dom_sid);
389 /***************************************************************************
391 ***************************************************************************/
393 NTSTATUS _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
395 struct lsa_info *info;
396 SEC_DESC *psd = NULL;
398 uint32 des_access=q_u->des_access;
403 /* map the generic bits to the lsa policy ones */
404 se_map_generic(&des_access, &lsa_generic_mapping);
406 /* get the generic lsa policy SD until we store it */
407 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
409 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
410 if (geteuid() != 0) {
413 DEBUG(4,("ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
414 acc_granted, des_access));
415 DEBUGADD(4,("but overwritten by euid == 0\n"));
416 acc_granted = des_access;
420 /* associate the domain SID with the (unique) handle. */
421 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
422 return NT_STATUS_NO_MEMORY;
425 sid_copy(&info->sid,get_global_sam_sid());
426 info->access = acc_granted;
428 /* set up the LSA QUERY INFO response */
429 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
430 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
435 /***************************************************************************
437 ***************************************************************************/
439 NTSTATUS _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
441 struct lsa_info *info;
442 SEC_DESC *psd = NULL;
444 uint32 des_access=q_u->des_access;
449 /* map the generic bits to the lsa policy ones */
450 se_map_generic(&des_access, &lsa_generic_mapping);
452 /* get the generic lsa policy SD until we store it */
453 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
455 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status)) {
456 if (geteuid() != 0) {
459 DEBUG(4,("ACCESS should be DENIED (granted: %#010x; required: %#010x)\n",
460 acc_granted, des_access));
461 DEBUGADD(4,("but overwritten by euid == 0\n"));
462 acc_granted = des_access;
465 /* associate the domain SID with the (unique) handle. */
466 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
467 return NT_STATUS_NO_MEMORY;
470 sid_copy(&info->sid,get_global_sam_sid());
471 info->access = acc_granted;
473 /* set up the LSA QUERY INFO response */
474 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
475 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
480 /***************************************************************************
481 _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
483 ***************************************************************************/
485 NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
487 struct lsa_info *info;
488 uint32 enum_context = q_u->enum_context;
491 * preferred length is set to 5 as a "our" preferred length
492 * nt sets this parameter to 2
493 * update (20.08.2002): it's not preferred length, but preferred size!
494 * it needs further investigation how to optimally choose this value
496 uint32 max_num_domains = q_u->preferred_len < 5 ? q_u->preferred_len : 10;
497 TRUSTDOM **trust_doms;
501 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
502 return NT_STATUS_INVALID_HANDLE;
504 /* check if the user have enough rights */
505 if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
506 return NT_STATUS_ACCESS_DENIED;
508 nt_status = secrets_get_trusted_domains(p->mem_ctx, (int *)&enum_context, max_num_domains, (int *)&num_domains, &trust_doms);
510 if (!NT_STATUS_IS_OK(nt_status) &&
511 !NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES) &&
512 !NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
515 r_u->status = nt_status;
518 /* set up the lsa_enum_trust_dom response */
519 init_r_enum_trust_dom(p->mem_ctx, r_u, enum_context, max_num_domains, num_domains, trust_doms);
524 /***************************************************************************
525 _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
526 ***************************************************************************/
528 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
530 struct lsa_info *handle;
531 LSA_INFO_UNION *info = &r_u->dom;
536 r_u->status = NT_STATUS_OK;
538 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
539 return NT_STATUS_INVALID_HANDLE;
541 switch (q_u->info_class) {
545 /* check if the user have enough rights */
546 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
547 return NT_STATUS_ACCESS_DENIED;
549 /* fake info: We audit everything. ;) */
550 info->id2.auditing_enabled = 1;
551 info->id2.count1 = 7;
552 info->id2.count2 = 7;
553 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
554 return NT_STATUS_NO_MEMORY;
555 for (i = 0; i < 7; i++)
556 info->id2.auditsettings[i] = 3;
560 /* check if the user have enough rights */
561 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
562 return NT_STATUS_ACCESS_DENIED;
564 /* Request PolicyPrimaryDomainInformation. */
565 switch (lp_server_role()) {
566 case ROLE_DOMAIN_PDC:
567 case ROLE_DOMAIN_BDC:
568 name = get_global_sam_name();
569 sid = get_global_sam_sid();
571 case ROLE_DOMAIN_MEMBER:
572 name = lp_workgroup();
573 /* We need to return the Domain SID here. */
574 if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid))
577 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
579 case ROLE_STANDALONE:
580 name = lp_workgroup();
584 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
586 init_dom_query(&r_u->dom.id3, name, sid);
589 /* check if the user have enough rights */
590 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
591 return NT_STATUS_ACCESS_DENIED;
593 /* Request PolicyAccountDomainInformation. */
594 name = get_global_sam_name();
595 sid = get_global_sam_sid();
596 init_dom_query(&r_u->dom.id5, name, sid);
599 /* check if the user have enough rights */
600 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
601 return NT_STATUS_ACCESS_DENIED;
603 switch (lp_server_role()) {
604 case ROLE_DOMAIN_BDC:
606 * only a BDC is a backup controller
607 * of the domain, it controls.
609 info->id6.server_role = 2;
613 * any other role is a primary
614 * of the domain, it controls.
616 info->id6.server_role = 3;
621 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
622 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
626 if (NT_STATUS_IS_OK(r_u->status)) {
627 r_u->undoc_buffer = 0x22000000; /* bizarre */
628 r_u->info_class = q_u->info_class;
634 /***************************************************************************
636 ***************************************************************************/
638 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
640 struct lsa_info *handle;
641 DOM_SID2 *sid = q_u->sids.sid;
642 int num_entries = q_u->sids.num_entries;
643 DOM_R_REF *ref = NULL;
644 LSA_TRANS_NAME_ENUM *names = NULL;
645 uint32 mapped_count = 0;
647 if (num_entries > MAX_LOOKUP_SIDS) {
648 num_entries = MAX_LOOKUP_SIDS;
649 DEBUG(5,("_lsa_lookup_sids: truncating SID lookup list to %d\n", num_entries));
652 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
653 names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
655 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
656 r_u->status = NT_STATUS_INVALID_HANDLE;
660 /* check if the user have enough rights */
661 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
662 r_u->status = NT_STATUS_ACCESS_DENIED;
666 return NT_STATUS_NO_MEMORY;
670 /* set up the LSA Lookup SIDs response */
671 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
672 if (NT_STATUS_IS_OK(r_u->status)) {
673 if (mapped_count == 0)
674 r_u->status = NT_STATUS_NONE_MAPPED;
675 else if (mapped_count != num_entries)
676 r_u->status = STATUS_SOME_UNMAPPED;
678 init_reply_lookup_sids(r_u, ref, names, mapped_count);
683 /***************************************************************************
684 lsa_reply_lookup_names
685 ***************************************************************************/
687 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
689 struct lsa_info *handle;
690 UNISTR2 *names = q_u->uni_name;
691 int num_entries = q_u->num_entries;
694 uint32 mapped_count = 0;
696 if (num_entries > MAX_LOOKUP_SIDS) {
697 num_entries = MAX_LOOKUP_SIDS;
698 DEBUG(5,("_lsa_lookup_names: truncating name lookup list to %d\n", num_entries));
701 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
702 rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*num_entries);
704 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
705 r_u->status = NT_STATUS_INVALID_HANDLE;
709 /* check if the user have enough rights */
710 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
711 r_u->status = NT_STATUS_ACCESS_DENIED;
716 return NT_STATUS_NO_MEMORY;
720 /* set up the LSA Lookup RIDs response */
721 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
722 if (NT_STATUS_IS_OK(r_u->status)) {
723 if (mapped_count == 0)
724 r_u->status = NT_STATUS_NONE_MAPPED;
725 else if (mapped_count != num_entries)
726 r_u->status = STATUS_SOME_UNMAPPED;
728 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
733 /***************************************************************************
734 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
735 ***************************************************************************/
737 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
739 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
740 return NT_STATUS_INVALID_HANDLE;
742 close_policy_hnd(p, &q_u->pol);
746 /***************************************************************************
747 "No more secrets Marty...." :-).
748 ***************************************************************************/
750 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
752 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
755 /***************************************************************************
757 ***************************************************************************/
759 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
761 struct lsa_info *handle;
764 uint32 enum_context=q_u->enum_context;
765 LSA_PRIV_ENTRY *entry;
766 LSA_PRIV_ENTRY *entries=NULL;
768 if (enum_context >= PRIV_ALL_INDEX)
769 return NT_STATUS_NO_MORE_ENTRIES;
771 entries = (LSA_PRIV_ENTRY *)talloc_zero(p->mem_ctx, sizeof(LSA_PRIV_ENTRY) * (PRIV_ALL_INDEX));
773 return NT_STATUS_NO_MEMORY;
775 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
776 return NT_STATUS_INVALID_HANDLE;
778 /* check if the user have enough rights */
781 * I don't know if it's the right one. not documented.
783 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
784 return NT_STATUS_ACCESS_DENIED;
788 DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", enum_context, PRIV_ALL_INDEX));
790 for (i = 0; i < PRIV_ALL_INDEX; i++, entry++) {
791 if( i<enum_context) {
792 init_unistr2(&entry->name, NULL, UNI_FLAGS_NONE);
793 init_uni_hdr(&entry->hdr_name, &entry->name);
795 entry->luid_high = 0;
797 init_unistr2(&entry->name, privs[i+1].priv, UNI_FLAGS_NONE);
798 init_uni_hdr(&entry->hdr_name, &entry->name);
799 entry->luid_low = privs[i+1].se_priv;
800 entry->luid_high = 0;
804 enum_context = PRIV_ALL_INDEX;
805 init_lsa_r_enum_privs(r_u, enum_context, PRIV_ALL_INDEX, entries);
810 /***************************************************************************
811 _lsa_priv_get_dispname.
812 ***************************************************************************/
814 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
816 struct lsa_info *handle;
820 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
821 return NT_STATUS_INVALID_HANDLE;
823 /* check if the user have enough rights */
826 * I don't know if it's the right one. not documented.
828 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
829 return NT_STATUS_ACCESS_DENIED;
831 unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
833 DEBUG(10,("_lsa_priv_get_dispname: %s", name_asc));
835 while (privs[i].se_priv!=SE_PRIV_ALL && strcmp(name_asc, privs[i].priv))
838 if (privs[i].se_priv!=SE_PRIV_ALL) {
839 DEBUG(10,(": %s\n", privs[i].description));
840 init_unistr2(&r_u->desc, privs[i].description, UNI_FLAGS_NONE);
841 init_uni_hdr(&r_u->hdr_desc, &r_u->desc);
843 r_u->ptr_info=0xdeadbeef;
844 r_u->lang_id=q_u->lang_id;
847 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
849 return NT_STATUS_NO_SUCH_PRIVILEGE;
853 /***************************************************************************
855 ***************************************************************************/
857 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
859 struct lsa_info *handle;
862 LSA_SID_ENUM *sids=&r_u->sids;
866 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
867 return NT_STATUS_INVALID_HANDLE;
869 /* check if the user have enough rights */
872 * I don't know if it's the right one. not documented.
874 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
875 return NT_STATUS_ACCESS_DENIED;
877 /* get the list of mapped groups (domain, local, builtin) */
879 ret = pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, ENUM_ONLY_MAPPED);
882 DEBUG(3,("_lsa_enum_accounts: enumeration of groups failed!\n"));
887 if (q_u->enum_context >= num_entries)
888 return NT_STATUS_NO_MORE_ENTRIES;
890 sids->ptr_sid = (uint32 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(uint32));
891 sids->sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(DOM_SID2));
893 if (sids->ptr_sid==NULL || sids->sid==NULL) {
895 return NT_STATUS_NO_MEMORY;
898 for (i=q_u->enum_context, j=0; i<num_entries; i++) {
899 init_dom_sid2( &(*sids).sid[j], &map[i].sid);
900 (*sids).ptr_sid[j]=1;
906 init_lsa_r_enum_accounts(r_u, j);
912 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
914 fstring username, domname;
915 user_struct *vuser = get_valid_user_struct(p->vuid);
918 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
920 fstrcpy(username, vuser->user.smb_name);
921 fstrcpy(domname, vuser->user.domain);
923 r_u->ptr_user_name = 1;
924 init_unistr2(&r_u->uni2_user_name, username, UNI_STR_TERMINATE);
925 init_uni_hdr(&r_u->hdr_user_name, &r_u->uni2_user_name);
929 r_u->ptr_dom_name = 1;
930 init_unistr2(&r_u->uni2_dom_name, domname, UNI_STR_TERMINATE);
931 init_uni_hdr(&r_u->hdr_dom_name, &r_u->uni2_dom_name);
933 r_u->status = NT_STATUS_OK;
938 /***************************************************************************
940 ***************************************************************************/
942 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
944 struct lsa_info *handle;
945 struct lsa_info *info;
947 r_u->status = NT_STATUS_OK;
949 /* find the connection policy handle. */
950 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
951 return NT_STATUS_INVALID_HANDLE;
953 /* check if the user have enough rights */
956 * I don't know if it's the right one. not documented.
957 * but guessed with rpcclient.
959 if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
960 return NT_STATUS_ACCESS_DENIED;
962 /* associate the user/group SID with the (unique) handle. */
963 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
964 return NT_STATUS_NO_MEMORY;
967 info->sid = q_u->sid.sid;
968 info->access = q_u->access;
970 /* get a (unique) handle. open a policy on it. */
971 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
972 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
977 /***************************************************************************
978 For a given SID, enumerate all the privilege this account has.
979 ***************************************************************************/
981 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, prs_struct *ps, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
983 struct lsa_info *info=NULL;
987 r_u->status = NT_STATUS_OK;
989 /* find the connection policy handle. */
990 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
991 return NT_STATUS_INVALID_HANDLE;
993 if (!pdb_getgrsid(&map, info->sid))
994 return NT_STATUS_NO_SUCH_GROUP;
996 #if 0 /* privileges currently not implemented! */
997 DEBUG(10,("_lsa_enum_privsaccount: %d privileges\n", map.priv_set->count));
998 if (map.priv_set->count!=0) {
1000 set=(LUID_ATTR *)talloc(map.priv_set->mem_ctx, map.priv_set.count*sizeof(LUID_ATTR));
1002 destroy_privilege(&map.priv_set);
1003 return NT_STATUS_NO_MEMORY;
1006 for (i = 0; i < map.priv_set.count; i++) {
1007 set[i].luid.low = map.priv_set->set[i].luid.low;
1008 set[i].luid.high = map.priv_set->set[i].luid.high;
1009 set[i].attr = map.priv_set->set[i].attr;
1010 DEBUG(10,("_lsa_enum_privsaccount: priv %d: %d:%d:%d\n", i,
1011 set[i].luid.high, set[i].luid.low, set[i].attr));
1015 init_lsa_r_enum_privsaccount(ps->mem_ctx, r_u, set, map.priv_set->count, 0);
1016 destroy_privilege(&map.priv_set);
1019 init_lsa_r_enum_privsaccount(ps->mem_ctx, r_u, set, 0, 0);
1024 /***************************************************************************
1026 ***************************************************************************/
1028 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
1030 struct lsa_info *info=NULL;
1032 r_u->status = NT_STATUS_OK;
1034 /* find the connection policy handle. */
1035 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1036 return NT_STATUS_INVALID_HANDLE;
1038 if (!pdb_getgrsid(&map, info->sid))
1039 return NT_STATUS_NO_SUCH_GROUP;
1042 0x01 -> Log on locally
1043 0x02 -> Access this computer from network
1044 0x04 -> Log on as a batch job
1045 0x10 -> Log on as a service
1047 they can be ORed together
1050 r_u->access = PR_LOG_ON_LOCALLY | PR_ACCESS_FROM_NETWORK;
1055 /***************************************************************************
1056 update the systemaccount information
1057 ***************************************************************************/
1059 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
1061 struct lsa_info *info=NULL;
1063 r_u->status = NT_STATUS_OK;
1065 /* find the connection policy handle. */
1066 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1067 return NT_STATUS_INVALID_HANDLE;
1069 if (!pdb_getgrsid(&map, info->sid))
1070 return NT_STATUS_NO_SUCH_GROUP;
1072 if(!pdb_update_group_mapping_entry(&map))
1073 return NT_STATUS_NO_SUCH_GROUP;
1078 /***************************************************************************
1079 For a given SID, add some privileges.
1080 ***************************************************************************/
1082 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
1085 struct lsa_info *info = NULL;
1088 LUID_ATTR *luid_attr = NULL;
1089 PRIVILEGE_SET *set = NULL;
1092 r_u->status = NT_STATUS_OK;
1094 #if 0 /* privileges are not implemented */
1095 /* find the connection policy handle. */
1096 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1097 return NT_STATUS_INVALID_HANDLE;
1099 if (!pdb_getgrsid(&map, info->sid))
1100 return NT_STATUS_NO_SUCH_GROUP;
1104 for (i = 0; i < set->count; i++) {
1105 luid_attr = &set->set[i];
1107 /* check if the privilege is already there */
1108 if (check_priv_in_privilege(map.priv_set, *luid_attr)){
1109 destroy_privilege(&map.priv_set);
1112 add_privilege(map.priv_set, *luid_attr);
1115 if(!pdb_update_group_mapping_entry(&map))
1116 return NT_STATUS_NO_SUCH_GROUP;
1118 destroy_privilege(&map.priv_set);
1124 /***************************************************************************
1125 For a given SID, remove some privileges.
1126 ***************************************************************************/
1128 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1131 struct lsa_info *info = NULL;
1134 LUID_ATTR *luid_attr = NULL;
1135 PRIVILEGE_SET *set = NULL;
1138 r_u->status = NT_STATUS_OK;
1140 #if 0 /* privileges are not implemented */
1141 /* find the connection policy handle. */
1142 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1143 return NT_STATUS_INVALID_HANDLE;
1145 if (!pdb_getgrsid(&map, info->sid))
1146 return NT_STATUS_NO_SUCH_GROUP;
1148 if (q_u->allrights != 0) {
1149 /* log it and return, until I see one myself don't do anything */
1150 DEBUG(5,("_lsa_removeprivs: trying to remove all privileges ?\n"));
1151 return NT_STATUS_OK;
1154 if (q_u->ptr == 0) {
1155 /* log it and return, until I see one myself don't do anything */
1156 DEBUG(5,("_lsa_removeprivs: no privileges to remove ?\n"));
1157 return NT_STATUS_OK;
1162 for (i = 0; i < set->count; i++) {
1163 luid_attr = &set->set[i];
1165 /* if we don't have the privilege, we're trying to remove, give up */
1166 /* what else can we do ??? JFM. */
1167 if (!check_priv_in_privilege(map.priv_set, *luid_attr)){
1168 destroy_privilege(&map.priv_set);
1169 return NT_STATUS_NO_SUCH_PRIVILEGE;
1172 remove_privilege(map.priv_set, *luid_attr);
1175 if(!pdb_update_group_mapping_entry(&map))
1176 return NT_STATUS_NO_SUCH_GROUP;
1178 destroy_privilege(&map.priv_set);
1183 /***************************************************************************
1184 For a given SID, remove some privileges.
1185 ***************************************************************************/
1187 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1189 struct lsa_info *handle=NULL;
1190 SEC_DESC *psd = NULL;
1194 r_u->status = NT_STATUS_OK;
1196 /* find the connection policy handle. */
1197 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1198 return NT_STATUS_INVALID_HANDLE;
1200 /* check if the user have enough rights */
1201 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1202 return NT_STATUS_ACCESS_DENIED;
1205 switch (q_u->sec_info) {
1207 /* SD contains only the owner */
1209 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1210 if(!NT_STATUS_IS_OK(status))
1211 return NT_STATUS_NO_MEMORY;
1214 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1215 return NT_STATUS_NO_MEMORY;
1218 /* SD contains only the ACL */
1220 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1221 if(!NT_STATUS_IS_OK(status))
1222 return NT_STATUS_NO_MEMORY;
1224 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1225 return NT_STATUS_NO_MEMORY;
1228 return NT_STATUS_INVALID_LEVEL;
1237 NTSTATUS _lsa_query_info2(pipes_struct *p, LSA_Q_QUERY_INFO2 *q_u, LSA_R_QUERY_INFO2 *r_u)
1239 struct lsa_info *handle;
1240 const char *nb_name;
1241 char *dns_name = NULL;
1242 char *forest_name = NULL;
1243 DOM_SID *sid = NULL;
1248 r_u->status = NT_STATUS_OK;
1250 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1251 return NT_STATUS_INVALID_HANDLE;
1253 switch (q_u->info_class) {
1255 /* check if the user have enough rights */
1256 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1257 return NT_STATUS_ACCESS_DENIED;
1259 /* Request PolicyPrimaryDomainInformation. */
1260 switch (lp_server_role()) {
1261 case ROLE_DOMAIN_PDC:
1262 case ROLE_DOMAIN_BDC:
1263 nb_name = get_global_sam_name();
1264 /* ugly temp hack for these next two */
1266 /* This should be a 'netbios domain -> DNS domain' mapping */
1267 dnsdomname[0] = '\0';
1268 get_mydnsdomname(dnsdomname);
1269 strlower_m(dnsdomname);
1271 dns_name = dnsdomname;
1272 forest_name = dnsdomname;
1274 sid = get_global_sam_sid();
1275 secrets_fetch_domain_guid(lp_workgroup(), &guid);
1278 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1280 init_dns_dom_info(&r_u->info.dns_dom_info, nb_name, dns_name,
1281 forest_name,&guid,sid);
1284 DEBUG(0,("_lsa_query_info2: unknown info level in Lsa Query: %d\n", q_u->info_class));
1285 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
1289 if (NT_STATUS_IS_OK(r_u->status)) {
1291 r_u->info_class = q_u->info_class;