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.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* This is the implementation of the lsa server code. */
28 extern DOM_SID global_sam_sid;
29 extern fstring global_myworkgroup;
30 extern pstring global_myname;
38 struct generic_mapping lsa_generic_mapping = {
45 /*******************************************************************
46 Function to free the per handle data.
47 ********************************************************************/
49 static void free_lsa_info(void *ptr)
51 struct lsa_info *lsa = (struct lsa_info *)ptr;
56 /***************************************************************************
58 ***************************************************************************/
60 static void init_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
62 int domlen = (dom_name != NULL) ? strlen(dom_name) : 0;
65 * I'm not sure why this really odd combination of length
66 * values works, but it does appear to. I need to look at
67 * this *much* more closely - but at the moment leave alone
68 * until it's understood. This allows a W2k client to join
69 * a domain with both odd and even length names... JRA.
72 d_q->uni_dom_str_len = domlen ? ((domlen + 1) * 2) : 0;
73 d_q->uni_dom_max_len = domlen * 2;
74 d_q->buffer_dom_name = domlen != 0 ? 1 : 0; /* domain buffer pointer */
75 d_q->buffer_dom_sid = dom_sid != NULL ? 1 : 0; /* domain sid pointer */
77 /* this string is supposed to be character short */
78 init_unistr2(&d_q->uni_domain_name, dom_name, domlen);
79 d_q->uni_domain_name.uni_max_len++;
82 init_dom_sid2(&d_q->dom_sid, dom_sid);
85 /***************************************************************************
86 init_dom_ref - adds a domain if it's not already in, returns the index.
87 ***************************************************************************/
89 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
94 if (dom_name != NULL) {
95 for (num = 0; num < ref->num_ref_doms_1; num++) {
97 rpcstr_pull(domname, &ref->ref_dom[num].uni_dom_name, sizeof(domname), -1, 0);
98 if (strequal(domname, dom_name))
102 num = ref->num_ref_doms_1;
105 if (num >= MAX_REF_DOMAINS) {
106 /* index not found, already at maximum domain limit */
110 ref->num_ref_doms_1 = num+1;
111 ref->ptr_ref_dom = 1;
112 ref->max_entries = MAX_REF_DOMAINS;
113 ref->num_ref_doms_2 = num+1;
115 len = (dom_name != NULL) ? strlen(dom_name) : 0;
116 if(dom_name != NULL && len == 0)
119 init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, len);
120 ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
122 init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, len);
123 init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
128 /***************************************************************************
130 ***************************************************************************/
132 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
133 int num_entries, UNISTR2 *name,
134 uint32 *mapped_count, BOOL endian)
140 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
142 become_root(); /* lookup_name can require root privs */
144 for (i = 0; i < num_entries; i++) {
147 uint32 rid = 0xffffffff;
150 fstring dom_name, user;
151 enum SID_NAME_USE name_type = SID_NAME_UNKNOWN;
153 /* Split name into domain and user component */
155 unistr2_to_ascii(full_name, &name[i], sizeof(full_name));
156 split_domain_name(full_name, dom_name, user);
160 DEBUG(5, ("init_lsa_rid2s: looking up name %s\n", full_name));
162 status = lookup_name(dom_name, user, &sid, &name_type);
164 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" :
168 sid_split_rid(&sid, &rid);
169 dom_idx = init_dom_ref(ref, dom_name, &sid);
174 name_type = SID_NAME_UNKNOWN;
177 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
184 /***************************************************************************
185 init_reply_lookup_names
186 ***************************************************************************/
188 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
189 DOM_R_REF *ref, uint32 num_entries,
190 DOM_RID2 *rid2, uint32 mapped_count)
192 r_l->ptr_dom_ref = 1;
195 r_l->num_entries = num_entries;
196 r_l->ptr_entries = 1;
197 r_l->num_entries2 = num_entries;
200 r_l->mapped_count = mapped_count;
202 if (mapped_count == 0)
203 r_l->status = NT_STATUS_NONE_MAPPED;
205 r_l->status = NT_STATUS_OK;
208 /***************************************************************************
209 Init lsa_trans_names.
210 ***************************************************************************/
212 static void init_lsa_trans_names(TALLOC_CTX *ctx, DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
213 int num_entries, DOM_SID2 *sid,
214 uint32 *mapped_count)
220 /* Allocate memory for list of names */
222 if (num_entries > 0) {
223 if (!(trn->name = (LSA_TRANS_NAME *)talloc(ctx, sizeof(LSA_TRANS_NAME) *
225 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
229 if (!(trn->uni_name = (UNISTR2 *)talloc(ctx, sizeof(UNISTR2) *
231 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
236 become_root(); /* Need root to get to passdb to for local sids */
238 for (i = 0; i < num_entries; i++) {
240 DOM_SID find_sid = sid[i].sid;
241 uint32 rid = 0xffffffff;
243 fstring name, dom_name;
244 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
246 sid_to_string(name, &find_sid);
247 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
249 /* Lookup sid from winbindd */
251 memset(dom_name, '\0', sizeof(dom_name));
252 memset(name, '\0', sizeof(name));
254 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
256 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" :
260 sid_name_use = SID_NAME_UNKNOWN;
263 /* Store domain sid in ref array */
265 if (find_sid.num_auths == 5) {
266 sid_split_rid(&find_sid, &rid);
269 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
271 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
272 "referenced list.\n", dom_name, name ));
276 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
277 sid_name_use, name, dom_idx);
283 trn->num_entries = total;
284 trn->ptr_trans_names = 1;
285 trn->num_entries2 = total;
288 /***************************************************************************
289 Init_reply_lookup_sids.
290 ***************************************************************************/
292 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
293 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
296 r_l->ptr_dom_ref = 1;
299 r_l->mapped_count = mapped_count;
301 if (mapped_count == 0)
302 r_l->status = NT_STATUS_NONE_MAPPED;
304 r_l->status = NT_STATUS_OK;
307 static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size)
309 extern DOM_SID global_sid_World;
310 extern DOM_SID global_sid_Builtin;
311 DOM_SID local_adm_sid;
319 init_sec_access(&mask, POLICY_EXECUTE);
320 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
322 sid_copy(&adm_sid, &global_sam_sid);
323 sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
324 init_sec_access(&mask, POLICY_ALL_ACCESS);
325 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
327 sid_copy(&local_adm_sid, &global_sid_Builtin);
328 sid_append_rid(&local_adm_sid, BUILTIN_ALIAS_RID_ADMINS);
329 init_sec_access(&mask, POLICY_ALL_ACCESS);
330 init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
332 if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
333 return NT_STATUS_NO_MEMORY;
335 if((*sd = make_sec_desc(mem_ctx, SEC_DESC_REVISION, &adm_sid, NULL, NULL, psa, sd_size)) == NULL)
336 return NT_STATUS_NO_MEMORY;
341 /***************************************************************************
343 ***************************************************************************/
345 NTSTATUS _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
347 struct lsa_info *info;
348 SEC_DESC *psd = NULL;
350 uint32 des_access=q_u->des_access;
355 /* map the generic bits to the lsa policy ones */
356 se_map_generic(&des_access, &lsa_generic_mapping);
358 /* get the generic lsa policy SD until we store it */
359 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
361 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status))
364 /* associate the domain SID with the (unique) handle. */
365 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
366 return NT_STATUS_NO_MEMORY;
369 info->sid = global_sam_sid;
370 info->access = acc_granted;
372 /* set up the LSA QUERY INFO response */
373 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
374 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
379 /***************************************************************************
381 ***************************************************************************/
383 NTSTATUS _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
385 struct lsa_info *info;
386 SEC_DESC *psd = NULL;
388 uint32 des_access=q_u->des_access;
393 /* map the generic bits to the lsa policy ones */
394 se_map_generic(&des_access, &lsa_generic_mapping);
396 /* get the generic lsa policy SD until we store it */
397 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
399 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status))
402 /* associate the domain SID with the (unique) handle. */
403 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
404 return NT_STATUS_NO_MEMORY;
407 info->sid = global_sam_sid;
408 info->access = acc_granted;
410 /* set up the LSA QUERY INFO response */
411 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
412 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
417 /***************************************************************************
418 _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
419 ***************************************************************************/
421 NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
423 struct lsa_info *info;
424 uint32 enum_context = 0;
425 char *dom_name = NULL;
426 DOM_SID *dom_sid = NULL;
428 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
429 return NT_STATUS_INVALID_HANDLE;
431 /* check if the user have enough rights */
432 if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
433 return NT_STATUS_ACCESS_DENIED;
435 /* set up the LSA QUERY INFO response */
436 init_r_enum_trust_dom(p->mem_ctx, r_u, enum_context, dom_name, dom_sid,
437 dom_name != NULL ? NT_STATUS_OK : NT_STATUS_NO_MORE_ENTRIES);
442 /***************************************************************************
443 _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
444 ***************************************************************************/
446 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
448 struct lsa_info *handle;
449 LSA_INFO_UNION *info = &r_u->dom;
454 r_u->status = NT_STATUS_OK;
456 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
457 return NT_STATUS_INVALID_HANDLE;
459 switch (q_u->info_class) {
463 /* check if the user have enough rights */
464 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
465 return NT_STATUS_ACCESS_DENIED;
467 /* fake info: We audit everything. ;) */
468 info->id2.auditing_enabled = 1;
469 info->id2.count1 = 7;
470 info->id2.count2 = 7;
471 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
472 return NT_STATUS_NO_MEMORY;
473 for (i = 0; i < 7; i++)
474 info->id2.auditsettings[i] = 3;
478 /* check if the user have enough rights */
479 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
480 return NT_STATUS_ACCESS_DENIED;
482 /* Request PolicyPrimaryDomainInformation. */
483 switch (lp_server_role()) {
484 case ROLE_DOMAIN_PDC:
485 case ROLE_DOMAIN_BDC:
486 name = global_myworkgroup;
487 sid = &global_sam_sid;
489 case ROLE_DOMAIN_MEMBER:
490 name = global_myworkgroup;
491 /* We need to return the Domain SID here. */
492 if (secrets_fetch_domain_sid(global_myworkgroup,
496 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
498 case ROLE_STANDALONE:
499 name = global_myworkgroup;
503 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
505 init_dom_query(&r_u->dom.id3, name, sid);
508 /* check if the user have enough rights */
509 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
510 return NT_STATUS_ACCESS_DENIED;
512 /* Request PolicyAccountDomainInformation. */
513 switch (lp_server_role()) {
514 case ROLE_DOMAIN_PDC:
515 case ROLE_DOMAIN_BDC:
516 name = global_myworkgroup;
517 sid = &global_sam_sid;
519 case ROLE_DOMAIN_MEMBER:
520 name = global_myname;
521 sid = &global_sam_sid;
523 case ROLE_STANDALONE:
524 name = global_myname;
525 sid = &global_sam_sid;
528 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
530 init_dom_query(&r_u->dom.id5, name, sid);
533 /* check if the user have enough rights */
534 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
535 return NT_STATUS_ACCESS_DENIED;
537 switch (lp_server_role()) {
538 case ROLE_DOMAIN_BDC:
540 * only a BDC is a backup controller
541 * of the domain, it controls.
543 info->id6.server_role = 2;
547 * any other role is a primary
548 * of the domain, it controls.
550 info->id6.server_role = 3;
555 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
556 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
560 if (NT_STATUS_IS_OK(r_u->status)) {
561 r_u->undoc_buffer = 0x22000000; /* bizarre */
562 r_u->info_class = q_u->info_class;
568 /***************************************************************************
570 ***************************************************************************/
572 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
574 struct lsa_info *handle;
575 DOM_SID2 *sid = q_u->sids.sid;
576 int num_entries = q_u->sids.num_entries;
577 DOM_R_REF *ref = NULL;
578 LSA_TRANS_NAME_ENUM *names = NULL;
579 uint32 mapped_count = 0;
581 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
582 names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
584 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
585 r_u->status = NT_STATUS_INVALID_HANDLE;
589 /* check if the user have enough rights */
590 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
591 r_u->status = NT_STATUS_ACCESS_DENIED;
595 return NT_STATUS_NO_MEMORY;
599 /* set up the LSA Lookup SIDs response */
600 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
601 init_reply_lookup_sids(r_u, ref, names, mapped_count);
606 /***************************************************************************
607 lsa_reply_lookup_names
608 ***************************************************************************/
610 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
612 struct lsa_info *handle;
613 UNISTR2 *names = q_u->uni_name;
614 int num_entries = q_u->num_entries;
617 uint32 mapped_count = 0;
619 if (num_entries > MAX_LOOKUP_SIDS) {
620 num_entries = MAX_LOOKUP_SIDS;
621 DEBUG(5,("_lsa_lookup_names: truncating name lookup list to %d\n", num_entries));
624 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
625 rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*num_entries);
627 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle)) {
628 r_u->status = NT_STATUS_INVALID_HANDLE;
632 /* check if the user have enough rights */
633 if (!(handle->access & POLICY_LOOKUP_NAMES)) {
634 r_u->status = NT_STATUS_ACCESS_DENIED;
639 return NT_STATUS_NO_MEMORY;
643 /* set up the LSA Lookup RIDs response */
644 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
645 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
650 /***************************************************************************
651 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
652 ***************************************************************************/
654 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
656 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
657 return NT_STATUS_INVALID_HANDLE;
659 close_policy_hnd(p, &q_u->pol);
663 /***************************************************************************
664 "No more secrets Marty...." :-).
665 ***************************************************************************/
667 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
669 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
672 /***************************************************************************
674 ***************************************************************************/
676 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
678 struct lsa_info *handle;
681 uint32 enum_context=q_u->enum_context;
682 LSA_PRIV_ENTRY *entry;
683 LSA_PRIV_ENTRY *entries=NULL;
685 if (enum_context >= PRIV_ALL_INDEX)
686 return NT_STATUS_NO_MORE_ENTRIES;
688 entries = (LSA_PRIV_ENTRY *)talloc_zero(p->mem_ctx, sizeof(LSA_PRIV_ENTRY) * (PRIV_ALL_INDEX));
690 return NT_STATUS_NO_MEMORY;
692 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
693 return NT_STATUS_INVALID_HANDLE;
695 /* check if the user have enough rights */
698 * I don't know if it's the right one. not documented.
700 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
701 return NT_STATUS_ACCESS_DENIED;
705 DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", enum_context, PRIV_ALL_INDEX));
707 for (i = 0; i < PRIV_ALL_INDEX; i++, entry++) {
708 if( i<enum_context) {
709 init_uni_hdr(&entry->hdr_name, 0);
710 init_unistr2(&entry->name, NULL, 0 );
712 entry->luid_high = 0;
714 init_uni_hdr(&entry->hdr_name, strlen(privs[i+1].priv));
715 init_unistr2(&entry->name, privs[i+1].priv, strlen(privs[i+1].priv) );
716 entry->luid_low = privs[i+1].se_priv;
717 entry->luid_high = 0;
721 enum_context = PRIV_ALL_INDEX;
722 init_lsa_r_enum_privs(r_u, enum_context, PRIV_ALL_INDEX, entries);
727 /***************************************************************************
728 _lsa_priv_get_dispname.
729 ***************************************************************************/
731 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
733 struct lsa_info *handle;
737 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
738 return NT_STATUS_INVALID_HANDLE;
740 /* check if the user have enough rights */
743 * I don't know if it's the right one. not documented.
745 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
746 return NT_STATUS_ACCESS_DENIED;
748 unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
750 DEBUG(10,("_lsa_priv_get_dispname: %s", name_asc));
752 while (privs[i].se_priv!=SE_PRIV_ALL && strcmp(name_asc, privs[i].priv))
755 if (privs[i].se_priv!=SE_PRIV_ALL) {
756 DEBUG(10,(": %s\n", privs[i].description));
757 init_uni_hdr(&r_u->hdr_desc, strlen(privs[i].description));
758 init_unistr2(&r_u->desc, privs[i].description, strlen(privs[i].description) );
760 r_u->ptr_info=0xdeadbeef;
761 r_u->lang_id=q_u->lang_id;
764 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
766 return NT_STATUS_NO_SUCH_PRIVILEGE;
770 /***************************************************************************
772 ***************************************************************************/
774 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
776 struct lsa_info *handle;
779 LSA_SID_ENUM *sids=&r_u->sids;
782 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
783 return NT_STATUS_INVALID_HANDLE;
785 /* check if the user have enough rights */
788 * I don't know if it's the right one. not documented.
790 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
791 return NT_STATUS_ACCESS_DENIED;
793 /* get the list of mapped groups (domain, local, builtin) */
794 if(!enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
797 if (q_u->enum_context >= num_entries)
798 return NT_STATUS_NO_MORE_ENTRIES;
800 sids->ptr_sid = (uint32 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(uint32));
801 sids->sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(DOM_SID2));
803 if (sids->ptr_sid==NULL || sids->sid==NULL) {
805 return NT_STATUS_NO_MEMORY;
808 for (i=q_u->enum_context, j=0; i<num_entries; i++) {
809 init_dom_sid2( &(*sids).sid[j], &map[i].sid);
810 (*sids).ptr_sid[j]=1;
816 init_lsa_r_enum_accounts(r_u, j);
822 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
824 fstring username, domname;
826 user_struct *vuser = get_valid_user_struct(p->vuid);
829 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
831 fstrcpy(username, vuser->user.smb_name);
832 fstrcpy(domname, vuser->user.domain);
834 ulen = strlen(username) + 1;
835 dlen = strlen(domname) + 1;
837 init_uni_hdr(&r_u->hdr_user_name, ulen);
838 r_u->ptr_user_name = 1;
839 init_unistr2(&r_u->uni2_user_name, username, ulen);
843 init_uni_hdr(&r_u->hdr_dom_name, dlen);
844 r_u->ptr_dom_name = 1;
845 init_unistr2(&r_u->uni2_dom_name, domname, dlen);
847 r_u->status = NT_STATUS_OK;
852 /***************************************************************************
854 ***************************************************************************/
856 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
858 struct lsa_info *handle;
859 struct lsa_info *info;
861 r_u->status = NT_STATUS_OK;
863 /* find the connection policy handle. */
864 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
865 return NT_STATUS_INVALID_HANDLE;
867 /* check if the user have enough rights */
870 * I don't know if it's the right one. not documented.
871 * but guessed with rpcclient.
873 if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
874 return NT_STATUS_ACCESS_DENIED;
876 /* associate the user/group SID with the (unique) handle. */
877 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
878 return NT_STATUS_NO_MEMORY;
881 info->sid = q_u->sid.sid;
882 info->access = q_u->access;
884 /* get a (unique) handle. open a policy on it. */
885 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
886 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
891 /***************************************************************************
892 For a given SID, enumerate all the privilege this account has.
893 ***************************************************************************/
895 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
897 struct lsa_info *info=NULL;
903 r_u->status = NT_STATUS_OK;
905 /* find the connection policy handle. */
906 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
907 return NT_STATUS_INVALID_HANDLE;
909 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
910 return NT_STATUS_NO_SUCH_GROUP;
912 DEBUG(10,("_lsa_enum_privsaccount: %d privileges\n", map.priv_set.count));
913 if (map.priv_set.count!=0) {
915 set=(LUID_ATTR *)talloc(p->mem_ctx, map.priv_set.count*sizeof(LUID_ATTR));
917 free_privilege(&map.priv_set);
918 return NT_STATUS_NO_MEMORY;
921 for (i=0; i<map.priv_set.count; i++) {
922 set[i].luid.low=map.priv_set.set[i].luid.low;
923 set[i].luid.high=map.priv_set.set[i].luid.high;
924 set[i].attr=map.priv_set.set[i].attr;
925 DEBUG(10,("_lsa_enum_privsaccount: priv %d: %d:%d:%d\n", i,
926 set[i].luid.high, set[i].luid.low, set[i].attr));
930 init_lsa_r_enum_privsaccount(r_u, set, map.priv_set.count, 0);
931 free_privilege(&map.priv_set);
936 /***************************************************************************
938 ***************************************************************************/
940 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
942 struct lsa_info *info=NULL;
944 r_u->status = NT_STATUS_OK;
946 /* find the connection policy handle. */
947 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
948 return NT_STATUS_INVALID_HANDLE;
950 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITHOUT_PRIV))
951 return NT_STATUS_NO_SUCH_GROUP;
954 0x01 -> Log on locally
955 0x02 -> Access this computer from network
956 0x04 -> Log on as a batch job
957 0x10 -> Log on as a service
959 they can be ORed together
962 r_u->access=map.systemaccount;
967 /***************************************************************************
968 update the systemaccount information
969 ***************************************************************************/
971 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
973 struct lsa_info *info=NULL;
975 r_u->status = NT_STATUS_OK;
977 /* find the connection policy handle. */
978 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
979 return NT_STATUS_INVALID_HANDLE;
981 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
982 return NT_STATUS_NO_SUCH_GROUP;
984 map.systemaccount=q_u->access;
986 if(!add_mapping_entry(&map, TDB_REPLACE))
987 return NT_STATUS_NO_SUCH_GROUP;
989 free_privilege(&map.priv_set);
994 /***************************************************************************
995 For a given SID, add some privileges.
996 ***************************************************************************/
998 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
1000 struct lsa_info *info=NULL;
1004 LUID_ATTR *luid_attr=NULL;
1005 PRIVILEGE_SET *set=NULL;
1007 r_u->status = NT_STATUS_OK;
1009 /* find the connection policy handle. */
1010 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1011 return NT_STATUS_INVALID_HANDLE;
1013 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1014 return NT_STATUS_NO_SUCH_GROUP;
1018 for (i=0; i<set->count; i++) {
1019 luid_attr=&set->set[i];
1021 /* check if the privilege is already there */
1022 if (check_priv_in_privilege(&map.priv_set, *luid_attr)){
1023 free_privilege(&map.priv_set);
1024 return NT_STATUS_NO_SUCH_PRIVILEGE;
1027 add_privilege(&map.priv_set, *luid_attr);
1030 if(!add_mapping_entry(&map, TDB_REPLACE))
1031 return NT_STATUS_NO_SUCH_GROUP;
1033 free_privilege(&map.priv_set);
1038 /***************************************************************************
1039 For a given SID, remove some privileges.
1040 ***************************************************************************/
1042 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1044 struct lsa_info *info=NULL;
1048 LUID_ATTR *luid_attr=NULL;
1049 PRIVILEGE_SET *set=NULL;
1051 r_u->status = NT_STATUS_OK;
1053 /* find the connection policy handle. */
1054 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1055 return NT_STATUS_INVALID_HANDLE;
1057 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1058 return NT_STATUS_NO_SUCH_GROUP;
1060 if (q_u->allrights!=0) {
1061 /* log it and return, until I see one myself don't do anything */
1062 DEBUG(5,("_lsa_removeprivs: trying to remove all privileges ?\n"));
1063 return NT_STATUS_OK;
1067 /* log it and return, until I see one myself don't do anything */
1068 DEBUG(5,("_lsa_removeprivs: no privileges to remove ?\n"));
1069 return NT_STATUS_OK;
1074 for (i=0; i<set->count; i++) {
1075 luid_attr=&set->set[i];
1077 /* if we don't have the privilege, we're trying to remove, give up */
1078 /* what else can we do ??? JFM. */
1079 if (!check_priv_in_privilege(&map.priv_set, *luid_attr)){
1080 free_privilege(&map.priv_set);
1081 return NT_STATUS_NO_SUCH_PRIVILEGE;
1084 remove_privilege(&map.priv_set, *luid_attr);
1087 if(!add_mapping_entry(&map, TDB_REPLACE))
1088 return NT_STATUS_NO_SUCH_GROUP;
1090 free_privilege(&map.priv_set);
1095 /***************************************************************************
1096 For a given SID, remove some privileges.
1097 ***************************************************************************/
1099 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1101 struct lsa_info *handle=NULL;
1102 SEC_DESC *psd = NULL;
1106 r_u->status = NT_STATUS_OK;
1108 /* find the connection policy handle. */
1109 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1110 return NT_STATUS_INVALID_HANDLE;
1112 /* check if the user have enough rights */
1113 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1114 return NT_STATUS_ACCESS_DENIED;
1117 switch (q_u->sec_info) {
1119 /* SD contains only the owner */
1121 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1122 if(!NT_STATUS_IS_OK(status))
1123 return NT_STATUS_NO_MEMORY;
1126 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1127 return NT_STATUS_NO_MEMORY;
1130 /* SD contains only the ACL */
1132 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1133 if(!NT_STATUS_IS_OK(status))
1134 return NT_STATUS_NO_MEMORY;
1136 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1137 return NT_STATUS_NO_MEMORY;
1140 return NT_STATUS_INVALID_LEVEL;