2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1997,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7 * Copyright (C) Paul Ashton 1997.
8 * Copyright (C) Jeremy Allison 2001.
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 for (i = 0; i < num_entries; i++) {
146 uint32 rid = 0xffffffff;
149 fstring dom_name, user;
150 enum SID_NAME_USE name_type = SID_NAME_UNKNOWN;
152 /* Split name into domain and user component */
154 unistr2_to_ascii(full_name, &name[i], sizeof(full_name));
155 split_domain_name(full_name, dom_name, user);
159 DEBUG(5, ("init_lsa_rid2s: looking up name %s\n", full_name));
161 status = lookup_name(full_name, &sid, &name_type);
163 DEBUG(5, ("init_lsa_rid2s: %s\n", status ? "found" :
167 sid_split_rid(&sid, &rid);
168 dom_idx = init_dom_ref(ref, dom_name, &sid);
173 name_type = SID_NAME_UNKNOWN;
176 init_dom_rid2(&rid2[total], rid, name_type, dom_idx);
181 /***************************************************************************
182 init_reply_lookup_names
183 ***************************************************************************/
185 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
186 DOM_R_REF *ref, uint32 num_entries,
187 DOM_RID2 *rid2, uint32 mapped_count)
189 r_l->ptr_dom_ref = 1;
192 r_l->num_entries = num_entries;
193 r_l->ptr_entries = 1;
194 r_l->num_entries2 = num_entries;
197 r_l->mapped_count = mapped_count;
199 if (mapped_count == 0)
200 r_l->status = NT_STATUS_NONE_MAPPED;
202 r_l->status = NT_STATUS_OK;
205 /***************************************************************************
206 Init lsa_trans_names.
207 ***************************************************************************/
209 static void init_lsa_trans_names(TALLOC_CTX *ctx, DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
210 int num_entries, DOM_SID2 *sid,
211 uint32 *mapped_count)
217 /* Allocate memory for list of names */
219 if (num_entries > 0) {
220 if (!(trn->name = (LSA_TRANS_NAME *)talloc(ctx, sizeof(LSA_TRANS_NAME) *
222 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
226 if (!(trn->uni_name = (UNISTR2 *)talloc(ctx, sizeof(UNISTR2) *
228 DEBUG(0, ("init_lsa_trans_names(): out of memory\n"));
233 for (i = 0; i < num_entries; i++) {
235 DOM_SID find_sid = sid[i].sid;
236 uint32 rid = 0xffffffff;
238 fstring name, dom_name;
239 enum SID_NAME_USE sid_name_use = (enum SID_NAME_USE)0;
241 sid_to_string(name, &find_sid);
242 DEBUG(5, ("init_lsa_trans_names: looking up sid %s\n", name));
244 /* Lookup sid from winbindd */
246 memset(dom_name, '\0', sizeof(dom_name));
247 memset(name, '\0', sizeof(name));
249 status = lookup_sid(&find_sid, dom_name, name, &sid_name_use);
251 DEBUG(5, ("init_lsa_trans_names: %s\n", status ? "found" :
255 sid_name_use = SID_NAME_UNKNOWN;
258 /* Store domain sid in ref array */
260 if (find_sid.num_auths == 5) {
261 sid_split_rid(&find_sid, &rid);
264 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
266 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to "
267 "referenced list.\n", dom_name, name ));
271 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
272 sid_name_use, name, dom_idx);
276 trn->num_entries = total;
277 trn->ptr_trans_names = 1;
278 trn->num_entries2 = total;
281 /***************************************************************************
282 Init_reply_lookup_sids.
283 ***************************************************************************/
285 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
286 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
289 r_l->ptr_dom_ref = 1;
292 r_l->mapped_count = mapped_count;
294 if (mapped_count == 0)
295 r_l->status = NT_STATUS_NONE_MAPPED;
297 r_l->status = NT_STATUS_OK;
300 static NTSTATUS lsa_get_generic_sd(TALLOC_CTX *mem_ctx, SEC_DESC **sd, size_t *sd_size)
302 extern DOM_SID global_sid_World;
303 extern DOM_SID global_sid_Builtin;
304 DOM_SID local_adm_sid;
312 init_sec_access(&mask, POLICY_EXECUTE);
313 init_sec_ace(&ace[0], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
315 sid_copy(&adm_sid, &global_sam_sid);
316 sid_append_rid(&adm_sid, DOMAIN_GROUP_RID_ADMINS);
317 init_sec_access(&mask, POLICY_ALL_ACCESS);
318 init_sec_ace(&ace[1], &adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
320 sid_copy(&local_adm_sid, &global_sid_Builtin);
321 sid_append_rid(&local_adm_sid, BUILTIN_ALIAS_RID_ADMINS);
322 init_sec_access(&mask, POLICY_ALL_ACCESS);
323 init_sec_ace(&ace[2], &local_adm_sid, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
325 if((psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, 3, ace)) == NULL)
326 return NT_STATUS_NO_MEMORY;
328 if((*sd = make_sec_desc(mem_ctx, SEC_DESC_REVISION, &adm_sid, NULL, NULL, psa, sd_size)) == NULL)
329 return NT_STATUS_NO_MEMORY;
334 /***************************************************************************
336 ***************************************************************************/
338 NTSTATUS _lsa_open_policy2(pipes_struct *p, LSA_Q_OPEN_POL2 *q_u, LSA_R_OPEN_POL2 *r_u)
340 struct lsa_info *info;
341 SEC_DESC *psd = NULL;
343 uint32 des_access=q_u->des_access;
348 /* map the generic bits to the lsa policy ones */
349 se_map_generic(&des_access, &lsa_generic_mapping);
351 /* get the generic lsa policy SD until we store it */
352 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
354 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status))
357 /* associate the domain SID with the (unique) handle. */
358 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
359 return NT_STATUS_NO_MEMORY;
362 info->sid = global_sam_sid;
363 info->access = acc_granted;
365 /* set up the LSA QUERY INFO response */
366 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
367 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
372 /***************************************************************************
374 ***************************************************************************/
376 NTSTATUS _lsa_open_policy(pipes_struct *p, LSA_Q_OPEN_POL *q_u, LSA_R_OPEN_POL *r_u)
378 struct lsa_info *info;
379 SEC_DESC *psd = NULL;
381 uint32 des_access=q_u->des_access;
386 /* map the generic bits to the lsa policy ones */
387 se_map_generic(&des_access, &lsa_generic_mapping);
389 /* get the generic lsa policy SD until we store it */
390 lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
392 if(!se_access_check(psd, p->pipe_user.nt_user_token, des_access, &acc_granted, &status))
395 /* associate the domain SID with the (unique) handle. */
396 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
397 return NT_STATUS_NO_MEMORY;
400 info->sid = global_sam_sid;
401 info->access = acc_granted;
403 /* set up the LSA QUERY INFO response */
404 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
405 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
410 /***************************************************************************
411 _lsa_enum_trust_dom - this needs fixing to do more than return NULL ! JRA.
412 ***************************************************************************/
414 NTSTATUS _lsa_enum_trust_dom(pipes_struct *p, LSA_Q_ENUM_TRUST_DOM *q_u, LSA_R_ENUM_TRUST_DOM *r_u)
416 struct lsa_info *info;
417 uint32 enum_context = 0;
418 char *dom_name = NULL;
419 DOM_SID *dom_sid = NULL;
421 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
422 return NT_STATUS_INVALID_HANDLE;
424 /* check if the user have enough rights */
425 if (!(info->access & POLICY_VIEW_LOCAL_INFORMATION))
426 return NT_STATUS_ACCESS_DENIED;
428 /* set up the LSA QUERY INFO response */
429 init_r_enum_trust_dom(p->mem_ctx, r_u, enum_context, dom_name, dom_sid,
430 dom_name != NULL ? NT_STATUS_OK : NT_STATUS_NO_MORE_ENTRIES);
435 /***************************************************************************
436 _lsa_query_info. See the POLICY_INFOMATION_CLASS docs at msdn.
437 ***************************************************************************/
439 NTSTATUS _lsa_query_info(pipes_struct *p, LSA_Q_QUERY_INFO *q_u, LSA_R_QUERY_INFO *r_u)
441 struct lsa_info *handle;
442 LSA_INFO_UNION *info = &r_u->dom;
447 r_u->status = NT_STATUS_OK;
449 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
450 return NT_STATUS_INVALID_HANDLE;
452 switch (q_u->info_class) {
456 /* check if the user have enough rights */
457 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
458 return NT_STATUS_ACCESS_DENIED;
460 /* fake info: We audit everything. ;) */
461 info->id2.auditing_enabled = 1;
462 info->id2.count1 = 7;
463 info->id2.count2 = 7;
464 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
465 return NT_STATUS_NO_MEMORY;
466 for (i = 0; i < 7; i++)
467 info->id2.auditsettings[i] = 3;
471 /* check if the user have enough rights */
472 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
473 return NT_STATUS_ACCESS_DENIED;
475 /* Request PolicyPrimaryDomainInformation. */
476 switch (lp_server_role()) {
477 case ROLE_DOMAIN_PDC:
478 case ROLE_DOMAIN_BDC:
479 name = global_myworkgroup;
480 sid = &global_sam_sid;
482 case ROLE_DOMAIN_MEMBER:
483 name = global_myworkgroup;
484 /* We need to return the Domain SID here. */
485 if (secrets_fetch_domain_sid(global_myworkgroup,
489 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
491 case ROLE_STANDALONE:
492 name = global_myworkgroup;
493 sid = NULL; /* Tell it we're not in a domain. */
496 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
498 init_dom_query(&r_u->dom.id3, name, sid);
501 /* check if the user have enough rights */
502 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
503 return NT_STATUS_ACCESS_DENIED;
505 /* Request PolicyAccountDomainInformation. */
506 switch (lp_server_role()) {
507 case ROLE_DOMAIN_PDC:
508 case ROLE_DOMAIN_BDC:
509 name = global_myworkgroup;
510 sid = &global_sam_sid;
512 case ROLE_DOMAIN_MEMBER:
513 name = global_myworkgroup;
514 sid = &global_sam_sid;
516 case ROLE_STANDALONE:
517 name = global_myworkgroup;
518 sid = &global_sam_sid;
521 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
523 init_dom_query(&r_u->dom.id5, 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 switch (lp_server_role()) {
531 case ROLE_DOMAIN_BDC:
533 * only a BDC is a backup controller
534 * of the domain, it controls.
536 info->id6.server_role = 2;
540 * any other role is a primary
541 * of the domain, it controls.
543 info->id6.server_role = 3;
548 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
549 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
553 if (NT_STATUS_IS_OK(r_u->status)) {
554 r_u->undoc_buffer = 0x22000000; /* bizarre */
555 r_u->info_class = q_u->info_class;
561 /***************************************************************************
563 ***************************************************************************/
565 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
567 struct lsa_info *handle;
568 DOM_SID2 *sid = q_u->sids.sid;
569 int num_entries = q_u->sids.num_entries;
570 DOM_R_REF *ref = NULL;
571 LSA_TRANS_NAME_ENUM *names = NULL;
572 uint32 mapped_count = 0;
574 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
575 return NT_STATUS_INVALID_HANDLE;
577 /* check if the user have enough rights */
578 if (!(handle->access & POLICY_LOOKUP_NAMES))
579 return NT_STATUS_ACCESS_DENIED;
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));
585 return NT_STATUS_NO_MEMORY;
587 /* set up the LSA Lookup SIDs response */
588 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
589 init_reply_lookup_sids(r_u, ref, names, mapped_count);
594 /***************************************************************************
595 lsa_reply_lookup_names
596 ***************************************************************************/
598 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
600 struct lsa_info *handle;
601 UNISTR2 *names = q_u->uni_name;
602 int num_entries = q_u->num_entries;
605 uint32 mapped_count = 0;
607 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
608 return NT_STATUS_INVALID_HANDLE;
610 /* check if the user have enough rights */
611 if (!(handle->access & POLICY_LOOKUP_NAMES))
612 return NT_STATUS_ACCESS_DENIED;
614 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
615 rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*MAX_LOOKUP_SIDS);
618 return NT_STATUS_NO_MEMORY;
620 /* set up the LSA Lookup RIDs response */
621 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
622 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
627 /***************************************************************************
628 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
629 ***************************************************************************/
631 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
633 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
634 return NT_STATUS_INVALID_HANDLE;
636 close_policy_hnd(p, &q_u->pol);
640 /***************************************************************************
641 "No more secrets Marty...." :-).
642 ***************************************************************************/
644 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
646 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
649 /***************************************************************************
651 ***************************************************************************/
653 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
655 struct lsa_info *handle;
658 uint32 enum_context=q_u->enum_context;
659 LSA_PRIV_ENTRY *entry;
660 LSA_PRIV_ENTRY *entries=NULL;
662 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
663 return NT_STATUS_INVALID_HANDLE;
665 /* check if the user have enough rights */
668 * I don't know if it's the right one. not documented.
670 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
671 return NT_STATUS_ACCESS_DENIED;
673 if (enum_context >= PRIV_ALL_INDEX)
674 return NT_STATUS_NO_MORE_ENTRIES;
676 entries = (LSA_PRIV_ENTRY *)talloc_zero(p->mem_ctx, sizeof(LSA_PRIV_ENTRY) * (PRIV_ALL_INDEX));
678 return NT_STATUS_NO_MEMORY;
682 DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", enum_context, PRIV_ALL_INDEX));
684 for (i = 0; i < PRIV_ALL_INDEX; i++, entry++) {
685 if( i<enum_context) {
686 init_uni_hdr(&entry->hdr_name, 0);
687 init_unistr2(&entry->name, NULL, 0 );
689 entry->luid_high = 0;
691 init_uni_hdr(&entry->hdr_name, strlen(privs[i+1].priv));
692 init_unistr2(&entry->name, privs[i+1].priv, strlen(privs[i+1].priv) );
693 entry->luid_low = privs[i+1].se_priv;
694 entry->luid_high = 0;
698 enum_context = PRIV_ALL_INDEX;
699 init_lsa_r_enum_privs(r_u, enum_context, PRIV_ALL_INDEX, entries);
704 /***************************************************************************
705 _lsa_priv_get_dispname.
706 ***************************************************************************/
708 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
710 struct lsa_info *handle;
714 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
715 return NT_STATUS_INVALID_HANDLE;
717 /* check if the user have enough rights */
720 * I don't know if it's the right one. not documented.
722 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
723 return NT_STATUS_ACCESS_DENIED;
725 unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
727 DEBUG(10,("_lsa_priv_get_dispname: %s", name_asc));
729 while (privs[i].se_priv!=SE_PRIV_ALL && strcmp(name_asc, privs[i].priv))
732 if (privs[i].se_priv!=SE_PRIV_ALL) {
733 DEBUG(10,(": %s\n", privs[i].description));
734 init_uni_hdr(&r_u->hdr_desc, strlen(privs[i].description));
735 init_unistr2(&r_u->desc, privs[i].description, strlen(privs[i].description) );
737 r_u->ptr_info=0xdeadbeef;
738 r_u->lang_id=q_u->lang_id;
741 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
743 return NT_STATUS_NO_SUCH_PRIVILEGE;
747 /***************************************************************************
749 ***************************************************************************/
751 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
753 struct lsa_info *handle;
756 LSA_SID_ENUM *sids=&r_u->sids;
759 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
760 return NT_STATUS_INVALID_HANDLE;
762 /* check if the user have enough rights */
765 * I don't know if it's the right one. not documented.
767 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
768 return NT_STATUS_ACCESS_DENIED;
770 /* get the list of mapped groups (domain, local, builtin) */
771 if(!enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
774 if (q_u->enum_context >= num_entries)
775 return NT_STATUS_NO_MORE_ENTRIES;
777 sids->ptr_sid = (uint32 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(uint32));
778 sids->sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(DOM_SID2));
780 if (sids->ptr_sid==NULL || sids->sid==NULL) {
782 return NT_STATUS_NO_MEMORY;
785 for (i=q_u->enum_context, j=0; i<num_entries; i++) {
786 init_dom_sid2( &(*sids).sid[j], &map[i].sid);
787 (*sids).ptr_sid[j]=1;
793 init_lsa_r_enum_accounts(r_u, j);
799 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
801 fstring username, domname;
803 user_struct *vuser = get_valid_user_struct(p->vuid);
806 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
808 fstrcpy(username, vuser->user.smb_name);
809 fstrcpy(domname, vuser->user.domain);
811 ulen = strlen(username) + 1;
812 dlen = strlen(domname) + 1;
814 init_uni_hdr(&r_u->hdr_user_name, ulen);
815 r_u->ptr_user_name = 1;
816 init_unistr2(&r_u->uni2_user_name, username, ulen);
820 init_uni_hdr(&r_u->hdr_dom_name, dlen);
821 r_u->ptr_dom_name = 1;
822 init_unistr2(&r_u->uni2_dom_name, domname, dlen);
824 r_u->status = NT_STATUS_OK;
829 /***************************************************************************
831 ***************************************************************************/
833 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
835 struct lsa_info *handle;
836 struct lsa_info *info;
838 r_u->status = NT_STATUS_OK;
840 /* find the connection policy handle. */
841 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
842 return NT_STATUS_INVALID_HANDLE;
844 /* check if the user have enough rights */
847 * I don't know if it's the right one. not documented.
848 * but guessed with rpcclient.
850 if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
851 return NT_STATUS_ACCESS_DENIED;
853 /* associate the user/group SID with the (unique) handle. */
854 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
855 return NT_STATUS_NO_MEMORY;
858 info->sid = q_u->sid.sid;
859 info->access = q_u->access;
861 /* get a (unique) handle. open a policy on it. */
862 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
863 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
868 /***************************************************************************
869 For a given SID, enumerate all the privilege this account has.
870 ***************************************************************************/
872 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
874 struct lsa_info *info=NULL;
880 r_u->status = NT_STATUS_OK;
882 /* find the connection policy handle. */
883 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
884 return NT_STATUS_INVALID_HANDLE;
886 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
887 return NT_STATUS_NO_SUCH_GROUP;
889 DEBUG(10,("_lsa_enum_privsaccount: %d privileges\n", map.priv_set.count));
890 if (map.priv_set.count!=0) {
892 set=(LUID_ATTR *)talloc(p->mem_ctx, map.priv_set.count*sizeof(LUID_ATTR));
894 free_privilege(&map.priv_set);
895 return NT_STATUS_NO_MEMORY;
898 for (i=0; i<map.priv_set.count; i++) {
899 set[i].luid.low=map.priv_set.set[i].luid.low;
900 set[i].luid.high=map.priv_set.set[i].luid.high;
901 set[i].attr=map.priv_set.set[i].attr;
902 DEBUG(10,("_lsa_enum_privsaccount: priv %d: %d:%d:%d\n", i,
903 set[i].luid.high, set[i].luid.low, set[i].attr));
907 init_lsa_r_enum_privsaccount(r_u, set, map.priv_set.count, 0);
908 free_privilege(&map.priv_set);
913 /***************************************************************************
915 ***************************************************************************/
917 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
919 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_WITHOUT_PRIV))
928 return NT_STATUS_NO_SUCH_GROUP;
931 0x01 -> Log on locally
932 0x02 -> Access this computer from network
933 0x04 -> Log on as a batch job
934 0x10 -> Log on as a service
936 they can be ORed together
939 r_u->access=map.systemaccount;
944 /***************************************************************************
945 update the systemaccount information
946 ***************************************************************************/
948 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
950 struct lsa_info *info=NULL;
952 r_u->status = NT_STATUS_OK;
954 /* find the connection policy handle. */
955 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
956 return NT_STATUS_INVALID_HANDLE;
958 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
959 return NT_STATUS_NO_SUCH_GROUP;
961 map.systemaccount=q_u->access;
963 if(!add_mapping_entry(&map, TDB_REPLACE))
964 return NT_STATUS_NO_SUCH_GROUP;
966 free_privilege(&map.priv_set);
971 /***************************************************************************
972 For a given SID, add some privileges.
973 ***************************************************************************/
975 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
977 struct lsa_info *info=NULL;
981 LUID_ATTR *luid_attr=NULL;
982 PRIVILEGE_SET *set=NULL;
984 r_u->status = NT_STATUS_OK;
986 /* find the connection policy handle. */
987 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
988 return NT_STATUS_INVALID_HANDLE;
990 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
991 return NT_STATUS_NO_SUCH_GROUP;
995 for (i=0; i<set->count; i++) {
996 luid_attr=&set->set[i];
998 /* check if the privilege is already there */
999 if (check_priv_in_privilege(&map.priv_set, *luid_attr)){
1000 free_privilege(&map.priv_set);
1001 return NT_STATUS_NO_SUCH_PRIVILEGE;
1004 add_privilege(&map.priv_set, *luid_attr);
1007 if(!add_mapping_entry(&map, TDB_REPLACE))
1008 return NT_STATUS_NO_SUCH_GROUP;
1010 free_privilege(&map.priv_set);
1015 /***************************************************************************
1016 For a given SID, remove some privileges.
1017 ***************************************************************************/
1019 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1021 struct lsa_info *info=NULL;
1025 LUID_ATTR *luid_attr=NULL;
1026 PRIVILEGE_SET *set=NULL;
1028 r_u->status = NT_STATUS_OK;
1030 /* find the connection policy handle. */
1031 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1032 return NT_STATUS_INVALID_HANDLE;
1034 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1035 return NT_STATUS_NO_SUCH_GROUP;
1037 if (q_u->allrights!=0) {
1038 /* log it and return, until I see one myself don't do anything */
1039 DEBUG(5,("_lsa_removeprivs: trying to remove all privileges ?\n"));
1040 return NT_STATUS_OK;
1044 /* log it and return, until I see one myself don't do anything */
1045 DEBUG(5,("_lsa_removeprivs: no privileges to remove ?\n"));
1046 return NT_STATUS_OK;
1051 for (i=0; i<set->count; i++) {
1052 luid_attr=&set->set[i];
1054 /* if we don't have the privilege, we're trying to remove, give up */
1055 /* what else can we do ??? JFM. */
1056 if (!check_priv_in_privilege(&map.priv_set, *luid_attr)){
1057 free_privilege(&map.priv_set);
1058 return NT_STATUS_NO_SUCH_PRIVILEGE;
1061 remove_privilege(&map.priv_set, *luid_attr);
1064 if(!add_mapping_entry(&map, TDB_REPLACE))
1065 return NT_STATUS_NO_SUCH_GROUP;
1067 free_privilege(&map.priv_set);
1072 /***************************************************************************
1073 For a given SID, remove some privileges.
1074 ***************************************************************************/
1076 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1078 struct lsa_info *handle=NULL;
1079 SEC_DESC *psd = NULL;
1083 r_u->status = NT_STATUS_OK;
1085 /* find the connection policy handle. */
1086 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1087 return NT_STATUS_INVALID_HANDLE;
1089 /* check if the user have enough rights */
1090 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1091 return NT_STATUS_ACCESS_DENIED;
1094 switch (q_u->sec_info) {
1096 /* SD contains only the owner */
1098 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1099 if(!NT_STATUS_IS_OK(status))
1100 return NT_STATUS_NO_MEMORY;
1103 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1104 return NT_STATUS_NO_MEMORY;
1107 /* SD contains only the ACL */
1109 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1110 if(!NT_STATUS_IS_OK(status))
1111 return NT_STATUS_NO_MEMORY;
1113 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1114 return NT_STATUS_NO_MEMORY;
1117 return NT_STATUS_INVALID_LEVEL;