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. */
29 extern DOM_SID global_sam_sid;
30 extern fstring global_myworkgroup;
31 extern pstring global_myname;
39 struct generic_mapping lsa_generic_mapping = {
46 /*******************************************************************
47 Function to free the per handle data.
48 ********************************************************************/
50 static void free_lsa_info(void *ptr)
52 struct lsa_info *lsa = (struct lsa_info *)ptr;
57 /***************************************************************************
59 ***************************************************************************/
61 static void init_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
63 int domlen = (dom_name != NULL) ? strlen(dom_name) : 0;
66 * I'm not sure why this really odd combination of length
67 * values works, but it does appear to. I need to look at
68 * this *much* more closely - but at the moment leave alone
69 * until it's understood. This allows a W2k client to join
70 * a domain with both odd and even length names... JRA.
73 d_q->uni_dom_str_len = domlen ? ((domlen + 1) * 2) : 0;
74 d_q->uni_dom_max_len = domlen * 2;
75 d_q->buffer_dom_name = domlen != 0 ? 1 : 0; /* domain buffer pointer */
76 d_q->buffer_dom_sid = dom_sid != NULL ? 1 : 0; /* domain sid pointer */
78 /* this string is supposed to be character short */
79 init_unistr2(&d_q->uni_domain_name, dom_name, domlen);
80 d_q->uni_domain_name.uni_max_len++;
83 init_dom_sid2(&d_q->dom_sid, dom_sid);
86 /***************************************************************************
87 init_dom_ref - adds a domain if it's not already in, returns the index.
88 ***************************************************************************/
90 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
95 if (dom_name != NULL) {
96 for (num = 0; num < ref->num_ref_doms_1; num++) {
98 rpcstr_pull(domname, &ref->ref_dom[num].uni_dom_name, sizeof(domname), -1, 0);
99 if (strequal(domname, dom_name))
103 num = ref->num_ref_doms_1;
106 if (num >= MAX_REF_DOMAINS) {
107 /* index not found, already at maximum domain limit */
111 ref->num_ref_doms_1 = num+1;
112 ref->ptr_ref_dom = 1;
113 ref->max_entries = MAX_REF_DOMAINS;
114 ref->num_ref_doms_2 = num+1;
116 len = (dom_name != NULL) ? strlen(dom_name) : 0;
117 if(dom_name != NULL && len == 0)
120 init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, len);
121 ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
123 init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, len);
124 init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
129 /***************************************************************************
131 ***************************************************************************/
133 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
134 int num_entries, UNISTR2 *name,
135 uint32 *mapped_count, BOOL endian)
141 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
143 become_root(); /* lookup_name can require root privs */
145 for (i = 0; i < num_entries; i++) {
148 uint32 rid = 0xffffffff;
151 fstring dom_name, user;
152 enum SID_NAME_USE name_type = SID_NAME_UNKNOWN;
154 /* Split name into domain and user component */
156 unistr2_to_ascii(full_name, &name[i], sizeof(full_name));
157 split_domain_name(full_name, dom_name, user);
161 DEBUG(5, ("init_lsa_rid2s: looking up name %s\n", full_name));
163 status = lookup_name(dom_name, user, &sid, &name_type);
165 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" :
169 sid_split_rid(&sid, &rid);
170 dom_idx = init_dom_ref(ref, dom_name, &sid);
175 name_type = SID_NAME_UNKNOWN;
178 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
185 /***************************************************************************
186 init_reply_lookup_names
187 ***************************************************************************/
189 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
190 DOM_R_REF *ref, uint32 num_entries,
191 DOM_RID2 *rid2, uint32 mapped_count)
193 r_l->ptr_dom_ref = 1;
196 r_l->num_entries = num_entries;
197 r_l->ptr_entries = 1;
198 r_l->num_entries2 = num_entries;
201 r_l->mapped_count = mapped_count;
203 if (mapped_count == 0)
204 r_l->status = NT_STATUS_NONE_MAPPED;
206 r_l->status = NT_STATUS_OK;
209 /***************************************************************************
210 Init lsa_trans_names.
211 ***************************************************************************/
213 static void init_lsa_trans_names(TALLOC_CTX *ctx, DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
214 int num_entries, DOM_SID2 *sid,
215 uint32 *mapped_count)
221 /* Allocate memory for list of names */
223 if (num_entries > 0) {
224 if (!(trn->name = (LSA_TRANS_NAME *)talloc(ctx, sizeof(LSA_TRANS_NAME) *
226 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
230 if (!(trn->uni_name = (UNISTR2 *)talloc(ctx, sizeof(UNISTR2) *
232 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
237 become_root(); /* Need root to get to passdb to for local sids */
239 for (i = 0; i < num_entries; i++) {
241 DOM_SID find_sid = sid[i].sid;
242 uint32 rid = 0xffffffff;
244 fstring name, dom_name;
245 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
247 sid_to_string(name, &find_sid);
248 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
250 /* Lookup sid from winbindd */
252 memset(dom_name, '\0', sizeof(dom_name));
253 memset(name, '\0', sizeof(name));
255 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
257 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" :
261 sid_name_use = SID_NAME_UNKNOWN;
266 /* Store domain sid in ref array */
268 if (find_sid.num_auths == 5) {
269 sid_split_rid(&find_sid, &rid);
272 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
274 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
275 "referenced list.\n", dom_name, name ));
277 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
278 sid_name_use, name, dom_idx);
284 trn->num_entries = total;
285 trn->ptr_trans_names = 1;
286 trn->num_entries2 = total;
289 /***************************************************************************
290 Init_reply_lookup_sids.
291 ***************************************************************************/
293 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
294 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
297 r_l->ptr_dom_ref = 1;
300 r_l->mapped_count = mapped_count;
302 if (mapped_count == 0)
303 r_l->status = NT_STATUS_NONE_MAPPED;
305 r_l->status = NT_STATUS_OK;
308 static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size)
310 extern DOM_SID global_sid_World;
311 extern DOM_SID global_sid_Builtin;
312 DOM_SID local_adm_sid;
320 init_sec_access(&mask, POLICY_EXECUTE);
321 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
323 sid_copy(&adm_sid, &global_sam_sid);
324 sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
325 init_sec_access(&mask, POLICY_ALL_ACCESS);
326 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
328 sid_copy(&local_adm_sid, &global_sid_Builtin);
329 sid_append_rid(&local_adm_sid, BUILTIN_ALIAS_RID_ADMINS);
330 init_sec_access(&mask, POLICY_ALL_ACCESS);
331 init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
333 if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
334 return NT_STATUS_NO_MEMORY;
336 if((*sd = make_sec_desc(mem_ctx, SEC_DESC_REVISION, &adm_sid, NULL, NULL, psa, sd_size)) == NULL)
337 return NT_STATUS_NO_MEMORY;
342 /***************************************************************************
344 ***************************************************************************/
346 NTSTATUS _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
348 struct lsa_info *info;
349 SEC_DESC *psd = NULL;
351 uint32 des_access=q_u->des_access;
356 /* map the generic bits to the lsa policy ones */
357 se_map_generic(&des_access, &lsa_generic_mapping);
359 /* get the generic lsa policy SD until we store it */
360 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
362 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status))
365 /* associate the domain SID with the (unique) handle. */
366 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
367 return NT_STATUS_NO_MEMORY;
370 info->sid = global_sam_sid;
371 info->access = acc_granted;
373 /* set up the LSA QUERY INFO response */
374 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
375 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
380 /***************************************************************************
382 ***************************************************************************/
384 NTSTATUS _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
386 struct lsa_info *info;
387 SEC_DESC *psd = NULL;
389 uint32 des_access=q_u->des_access;
394 /* map the generic bits to the lsa policy ones */
395 se_map_generic(&des_access, &lsa_generic_mapping);
397 /* get the generic lsa policy SD until we store it */
398 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
400 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status))
403 /* associate the domain SID with the (unique) handle. */
404 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
405 return NT_STATUS_NO_MEMORY;
408 info->sid = global_sam_sid;
409 info->access = acc_granted;
411 /* set up the LSA QUERY INFO response */
412 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
413 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
418 /***************************************************************************
419 _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
421 ***************************************************************************/
423 NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
425 struct lsa_info *info;
426 uint32 enum_context = q_u->enum_context;
429 * preferred length is set to 5 as a "our" preferred length
430 * nt sets this parameter to 2
432 uint32 max_num_domains = q_u->preferred_len < 5 ? q_u->preferred_len : 10;
433 TRUSTDOM **trust_doms;
437 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
438 return NT_STATUS_INVALID_HANDLE;
440 /* check if the user have enough rights */
441 if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
442 return NT_STATUS_ACCESS_DENIED;
444 nt_status = secrets_get_trusted_domains(p->mem_ctx, &enum_context, max_num_domains, &num_domains, &trust_doms);
446 if (!NT_STATUS_IS_OK(nt_status) &&
447 !NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES) &&
448 !NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
451 r_u->status = nt_status;
454 /* set up the lsa_enum_trust_dom response */
455 init_r_enum_trust_dom(p->mem_ctx, r_u, enum_context, max_num_domains, num_domains, trust_doms);
460 /***************************************************************************
461 _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
462 ***************************************************************************/
464 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
466 struct lsa_info *handle;
467 LSA_INFO_UNION *info = &r_u->dom;
472 r_u->status = NT_STATUS_OK;
474 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
475 return NT_STATUS_INVALID_HANDLE;
477 switch (q_u->info_class) {
481 /* check if the user have enough rights */
482 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
483 return NT_STATUS_ACCESS_DENIED;
485 /* fake info: We audit everything. ;) */
486 info->id2.auditing_enabled = 1;
487 info->id2.count1 = 7;
488 info->id2.count2 = 7;
489 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
490 return NT_STATUS_NO_MEMORY;
491 for (i = 0; i < 7; i++)
492 info->id2.auditsettings[i] = 3;
496 /* check if the user have enough rights */
497 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
498 return NT_STATUS_ACCESS_DENIED;
500 /* Request PolicyPrimaryDomainInformation. */
501 switch (lp_server_role()) {
502 case ROLE_DOMAIN_PDC:
503 case ROLE_DOMAIN_BDC:
504 name = global_myworkgroup;
505 sid = &global_sam_sid;
507 case ROLE_DOMAIN_MEMBER:
508 name = global_myworkgroup;
509 /* We need to return the Domain SID here. */
510 if (secrets_fetch_domain_sid(global_myworkgroup,
514 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
516 case ROLE_STANDALONE:
517 name = global_myworkgroup;
521 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
523 init_dom_query(&r_u->dom.id3, name, sid);
526 /* check if the user have enough rights */
527 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
528 return NT_STATUS_ACCESS_DENIED;
530 /* Request PolicyAccountDomainInformation. */
531 switch (lp_server_role()) {
532 case ROLE_DOMAIN_PDC:
533 case ROLE_DOMAIN_BDC:
534 name = global_myworkgroup;
535 sid = &global_sam_sid;
537 case ROLE_DOMAIN_MEMBER:
538 name = global_myname;
539 sid = &global_sam_sid;
541 case ROLE_STANDALONE:
542 name = global_myname;
543 sid = &global_sam_sid;
546 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
548 init_dom_query(&r_u->dom.id5, name, sid);
551 /* check if the user have enough rights */
552 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
553 return NT_STATUS_ACCESS_DENIED;
555 switch (lp_server_role()) {
556 case ROLE_DOMAIN_BDC:
558 * only a BDC is a backup controller
559 * of the domain, it controls.
561 info->id6.server_role = 2;
565 * any other role is a primary
566 * of the domain, it controls.
568 info->id6.server_role = 3;
573 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
574 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
578 if (NT_STATUS_IS_OK(r_u->status)) {
579 r_u->undoc_buffer = 0x22000000; /* bizarre */
580 r_u->info_class = q_u->info_class;
586 /***************************************************************************
588 ***************************************************************************/
590 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
592 struct lsa_info *handle;
593 DOM_SID2 *sid = q_u->sids.sid;
594 int num_entries = q_u->sids.num_entries;
595 DOM_R_REF *ref = NULL;
596 LSA_TRANS_NAME_ENUM *names = NULL;
597 uint32 mapped_count = 0;
599 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
600 names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
602 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
603 r_u->status = NT_STATUS_INVALID_HANDLE;
607 /* check if the user have enough rights */
608 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
609 r_u->status = NT_STATUS_ACCESS_DENIED;
613 return NT_STATUS_NO_MEMORY;
617 /* set up the LSA Lookup SIDs response */
618 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
619 init_reply_lookup_sids(r_u, ref, names, mapped_count);
624 /***************************************************************************
625 lsa_reply_lookup_names
626 ***************************************************************************/
628 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
630 struct lsa_info *handle;
631 UNISTR2 *names = q_u->uni_name;
632 int num_entries = q_u->num_entries;
635 uint32 mapped_count = 0;
637 if (num_entries > MAX_LOOKUP_SIDS) {
638 num_entries = MAX_LOOKUP_SIDS;
639 DEBUG(5,("_lsa_lookup_names: truncating name lookup list to %d\n", num_entries));
642 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
643 rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*num_entries);
645 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
646 r_u->status = NT_STATUS_INVALID_HANDLE;
650 /* check if the user have enough rights */
651 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
652 r_u->status = NT_STATUS_ACCESS_DENIED;
657 return NT_STATUS_NO_MEMORY;
661 /* set up the LSA Lookup RIDs response */
662 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
663 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
668 /***************************************************************************
669 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
670 ***************************************************************************/
672 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
674 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
675 return NT_STATUS_INVALID_HANDLE;
677 close_policy_hnd(p, &q_u->pol);
681 /***************************************************************************
682 "No more secrets Marty...." :-).
683 ***************************************************************************/
685 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
687 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
690 /***************************************************************************
692 ***************************************************************************/
694 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
696 struct lsa_info *handle;
699 uint32 enum_context=q_u->enum_context;
700 LSA_PRIV_ENTRY *entry;
701 LSA_PRIV_ENTRY *entries=NULL;
703 if (enum_context >= PRIV_ALL_INDEX)
704 return NT_STATUS_NO_MORE_ENTRIES;
706 entries = (LSA_PRIV_ENTRY *)talloc_zero(p->mem_ctx, sizeof(LSA_PRIV_ENTRY) * (PRIV_ALL_INDEX));
708 return NT_STATUS_NO_MEMORY;
710 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
711 return NT_STATUS_INVALID_HANDLE;
713 /* check if the user have enough rights */
716 * I don't know if it's the right one. not documented.
718 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
719 return NT_STATUS_ACCESS_DENIED;
723 DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", enum_context, PRIV_ALL_INDEX));
725 for (i = 0; i < PRIV_ALL_INDEX; i++, entry++) {
726 if( i<enum_context) {
727 init_uni_hdr(&entry->hdr_name, 0);
728 init_unistr2(&entry->name, NULL, 0 );
730 entry->luid_high = 0;
732 init_uni_hdr(&entry->hdr_name, strlen(privs[i+1].priv));
733 init_unistr2(&entry->name, privs[i+1].priv, strlen(privs[i+1].priv) );
734 entry->luid_low = privs[i+1].se_priv;
735 entry->luid_high = 0;
739 enum_context = PRIV_ALL_INDEX;
740 init_lsa_r_enum_privs(r_u, enum_context, PRIV_ALL_INDEX, entries);
745 /***************************************************************************
746 _lsa_priv_get_dispname.
747 ***************************************************************************/
749 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
751 struct lsa_info *handle;
755 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
756 return NT_STATUS_INVALID_HANDLE;
758 /* check if the user have enough rights */
761 * I don't know if it's the right one. not documented.
763 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
764 return NT_STATUS_ACCESS_DENIED;
766 unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
768 DEBUG(10,("_lsa_priv_get_dispname: %s", name_asc));
770 while (privs[i].se_priv!=SE_PRIV_ALL && strcmp(name_asc, privs[i].priv))
773 if (privs[i].se_priv!=SE_PRIV_ALL) {
774 DEBUG(10,(": %s\n", privs[i].description));
775 init_uni_hdr(&r_u->hdr_desc, strlen(privs[i].description));
776 init_unistr2(&r_u->desc, privs[i].description, strlen(privs[i].description) );
778 r_u->ptr_info=0xdeadbeef;
779 r_u->lang_id=q_u->lang_id;
782 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
784 return NT_STATUS_NO_SUCH_PRIVILEGE;
788 /***************************************************************************
790 ***************************************************************************/
792 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
794 struct lsa_info *handle;
797 LSA_SID_ENUM *sids=&r_u->sids;
800 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
801 return NT_STATUS_INVALID_HANDLE;
803 /* check if the user have enough rights */
806 * I don't know if it's the right one. not documented.
808 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
809 return NT_STATUS_ACCESS_DENIED;
811 /* get the list of mapped groups (domain, local, builtin) */
812 if(!enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
815 if (q_u->enum_context >= num_entries)
816 return NT_STATUS_NO_MORE_ENTRIES;
818 sids->ptr_sid = (uint32 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(uint32));
819 sids->sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(DOM_SID2));
821 if (sids->ptr_sid==NULL || sids->sid==NULL) {
823 return NT_STATUS_NO_MEMORY;
826 for (i=q_u->enum_context, j=0; i<num_entries; i++) {
827 init_dom_sid2( &(*sids).sid[j], &map[i].sid);
828 (*sids).ptr_sid[j]=1;
834 init_lsa_r_enum_accounts(r_u, j);
840 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
842 fstring username, domname;
844 user_struct *vuser = get_valid_user_struct(p->vuid);
847 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
849 fstrcpy(username, vuser->user.smb_name);
850 fstrcpy(domname, vuser->user.domain);
852 ulen = strlen(username) + 1;
853 dlen = strlen(domname) + 1;
855 init_uni_hdr(&r_u->hdr_user_name, ulen);
856 r_u->ptr_user_name = 1;
857 init_unistr2(&r_u->uni2_user_name, username, ulen);
861 init_uni_hdr(&r_u->hdr_dom_name, dlen);
862 r_u->ptr_dom_name = 1;
863 init_unistr2(&r_u->uni2_dom_name, domname, dlen);
865 r_u->status = NT_STATUS_OK;
870 /***************************************************************************
872 ***************************************************************************/
874 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
876 struct lsa_info *handle;
877 struct lsa_info *info;
879 r_u->status = NT_STATUS_OK;
881 /* find the connection policy handle. */
882 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
883 return NT_STATUS_INVALID_HANDLE;
885 /* check if the user have enough rights */
888 * I don't know if it's the right one. not documented.
889 * but guessed with rpcclient.
891 if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
892 return NT_STATUS_ACCESS_DENIED;
894 /* associate the user/group SID with the (unique) handle. */
895 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
896 return NT_STATUS_NO_MEMORY;
899 info->sid = q_u->sid.sid;
900 info->access = q_u->access;
902 /* get a (unique) handle. open a policy on it. */
903 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
904 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
909 /***************************************************************************
910 For a given SID, enumerate all the privilege this account has.
911 ***************************************************************************/
913 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
915 struct lsa_info *info=NULL;
921 r_u->status = NT_STATUS_OK;
923 /* find the connection policy handle. */
924 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
925 return NT_STATUS_INVALID_HANDLE;
927 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
928 return NT_STATUS_NO_SUCH_GROUP;
930 DEBUG(10,("_lsa_enum_privsaccount: %d privileges\n", map.priv_set.count));
931 if (map.priv_set.count!=0) {
933 set=(LUID_ATTR *)talloc(p->mem_ctx, map.priv_set.count*sizeof(LUID_ATTR));
935 free_privilege(&map.priv_set);
936 return NT_STATUS_NO_MEMORY;
939 for (i=0; i<map.priv_set.count; i++) {
940 set[i].luid.low=map.priv_set.set[i].luid.low;
941 set[i].luid.high=map.priv_set.set[i].luid.high;
942 set[i].attr=map.priv_set.set[i].attr;
943 DEBUG(10,("_lsa_enum_privsaccount: priv %d: %d:%d:%d\n", i,
944 set[i].luid.high, set[i].luid.low, set[i].attr));
948 init_lsa_r_enum_privsaccount(r_u, set, map.priv_set.count, 0);
949 free_privilege(&map.priv_set);
954 /***************************************************************************
956 ***************************************************************************/
958 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
960 struct lsa_info *info=NULL;
962 r_u->status = NT_STATUS_OK;
964 /* find the connection policy handle. */
965 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
966 return NT_STATUS_INVALID_HANDLE;
968 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITHOUT_PRIV))
969 return NT_STATUS_NO_SUCH_GROUP;
972 0x01 -> Log on locally
973 0x02 -> Access this computer from network
974 0x04 -> Log on as a batch job
975 0x10 -> Log on as a service
977 they can be ORed together
980 r_u->access=map.systemaccount;
985 /***************************************************************************
986 update the systemaccount information
987 ***************************************************************************/
989 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
991 struct lsa_info *info=NULL;
993 r_u->status = NT_STATUS_OK;
995 /* find the connection policy handle. */
996 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
997 return NT_STATUS_INVALID_HANDLE;
999 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1000 return NT_STATUS_NO_SUCH_GROUP;
1002 map.systemaccount=q_u->access;
1004 if(!add_mapping_entry(&map, TDB_REPLACE))
1005 return NT_STATUS_NO_SUCH_GROUP;
1007 free_privilege(&map.priv_set);
1012 /***************************************************************************
1013 For a given SID, add some privileges.
1014 ***************************************************************************/
1016 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
1018 struct lsa_info *info=NULL;
1022 LUID_ATTR *luid_attr=NULL;
1023 PRIVILEGE_SET *set=NULL;
1025 r_u->status = NT_STATUS_OK;
1027 /* find the connection policy handle. */
1028 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1029 return NT_STATUS_INVALID_HANDLE;
1031 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1032 return NT_STATUS_NO_SUCH_GROUP;
1036 for (i=0; i<set->count; i++) {
1037 luid_attr=&set->set[i];
1039 /* check if the privilege is already there */
1040 if (check_priv_in_privilege(&map.priv_set, *luid_attr)){
1041 free_privilege(&map.priv_set);
1042 return NT_STATUS_NO_SUCH_PRIVILEGE;
1045 add_privilege(&map.priv_set, *luid_attr);
1048 if(!add_mapping_entry(&map, TDB_REPLACE))
1049 return NT_STATUS_NO_SUCH_GROUP;
1051 free_privilege(&map.priv_set);
1056 /***************************************************************************
1057 For a given SID, remove some privileges.
1058 ***************************************************************************/
1060 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1062 struct lsa_info *info=NULL;
1066 LUID_ATTR *luid_attr=NULL;
1067 PRIVILEGE_SET *set=NULL;
1069 r_u->status = NT_STATUS_OK;
1071 /* find the connection policy handle. */
1072 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1073 return NT_STATUS_INVALID_HANDLE;
1075 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1076 return NT_STATUS_NO_SUCH_GROUP;
1078 if (q_u->allrights!=0) {
1079 /* log it and return, until I see one myself don't do anything */
1080 DEBUG(5,("_lsa_removeprivs: trying to remove all privileges ?\n"));
1081 return NT_STATUS_OK;
1085 /* log it and return, until I see one myself don't do anything */
1086 DEBUG(5,("_lsa_removeprivs: no privileges to remove ?\n"));
1087 return NT_STATUS_OK;
1092 for (i=0; i<set->count; i++) {
1093 luid_attr=&set->set[i];
1095 /* if we don't have the privilege, we're trying to remove, give up */
1096 /* what else can we do ??? JFM. */
1097 if (!check_priv_in_privilege(&map.priv_set, *luid_attr)){
1098 free_privilege(&map.priv_set);
1099 return NT_STATUS_NO_SUCH_PRIVILEGE;
1102 remove_privilege(&map.priv_set, *luid_attr);
1105 if(!add_mapping_entry(&map, TDB_REPLACE))
1106 return NT_STATUS_NO_SUCH_GROUP;
1108 free_privilege(&map.priv_set);
1113 /***************************************************************************
1114 For a given SID, remove some privileges.
1115 ***************************************************************************/
1117 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1119 struct lsa_info *handle=NULL;
1120 SEC_DESC *psd = NULL;
1124 r_u->status = NT_STATUS_OK;
1126 /* find the connection policy handle. */
1127 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1128 return NT_STATUS_INVALID_HANDLE;
1130 /* check if the user have enough rights */
1131 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1132 return NT_STATUS_ACCESS_DENIED;
1135 switch (q_u->sec_info) {
1137 /* SD contains only the owner */
1139 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1140 if(!NT_STATUS_IS_OK(status))
1141 return NT_STATUS_NO_MEMORY;
1144 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1145 return NT_STATUS_NO_MEMORY;
1148 /* SD contains only the ACL */
1150 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1151 if(!NT_STATUS_IS_OK(status))
1152 return NT_STATUS_NO_MEMORY;
1154 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1155 return NT_STATUS_NO_MEMORY;
1158 return NT_STATUS_INVALID_LEVEL;