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 extern DOM_SID global_sid_nonexistent;
442 struct lsa_info *handle;
443 LSA_INFO_UNION *info = &r_u->dom;
448 r_u->status = NT_STATUS_OK;
450 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
451 return NT_STATUS_INVALID_HANDLE;
453 switch (q_u->info_class) {
457 /* check if the user have enough rights */
458 if (!(handle->access & POLICY_VIEW_AUDIT_INFORMATION))
459 return NT_STATUS_ACCESS_DENIED;
461 /* fake info: We audit everything. ;) */
462 info->id2.auditing_enabled = 1;
463 info->id2.count1 = 7;
464 info->id2.count2 = 7;
465 if ((info->id2.auditsettings = (uint32 *)talloc(p->mem_ctx,7*sizeof(uint32))) == NULL)
466 return NT_STATUS_NO_MEMORY;
467 for (i = 0; i < 7; i++)
468 info->id2.auditsettings[i] = 3;
472 /* check if the user have enough rights */
473 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
474 return NT_STATUS_ACCESS_DENIED;
476 /* Request PolicyPrimaryDomainInformation. */
477 switch (lp_server_role()) {
478 case ROLE_DOMAIN_PDC:
479 case ROLE_DOMAIN_BDC:
480 name = global_myworkgroup;
481 sid = &global_sam_sid;
483 case ROLE_DOMAIN_MEMBER:
484 name = global_myworkgroup;
485 /* We need to return the Domain SID here. */
486 if (secrets_fetch_domain_sid(global_myworkgroup,
490 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
492 case ROLE_STANDALONE:
493 name = global_myworkgroup;
494 sid = &global_sid_nonexistent;
497 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
499 init_dom_query(&r_u->dom.id3, name, sid);
502 /* check if the user have enough rights */
503 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
504 return NT_STATUS_ACCESS_DENIED;
506 /* Request PolicyAccountDomainInformation. */
507 switch (lp_server_role()) {
508 case ROLE_DOMAIN_PDC:
509 case ROLE_DOMAIN_BDC:
510 name = global_myworkgroup;
511 sid = &global_sam_sid;
513 case ROLE_DOMAIN_MEMBER:
514 name = global_myname;
515 sid = &global_sam_sid;
517 case ROLE_STANDALONE:
518 name = global_myname;
519 sid = &global_sam_sid;
522 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
524 init_dom_query(&r_u->dom.id5, name, sid);
527 /* check if the user have enough rights */
528 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
529 return NT_STATUS_ACCESS_DENIED;
531 switch (lp_server_role()) {
532 case ROLE_DOMAIN_BDC:
534 * only a BDC is a backup controller
535 * of the domain, it controls.
537 info->id6.server_role = 2;
541 * any other role is a primary
542 * of the domain, it controls.
544 info->id6.server_role = 3;
549 DEBUG(0,("_lsa_query_info: unknown info level in Lsa Query: %d\n", q_u->info_class));
550 r_u->status = NT_STATUS_INVALID_INFO_CLASS;
554 if (NT_STATUS_IS_OK(r_u->status)) {
555 r_u->undoc_buffer = 0x22000000; /* bizarre */
556 r_u->info_class = q_u->info_class;
562 /***************************************************************************
564 ***************************************************************************/
566 NTSTATUS _lsa_lookup_sids(pipes_struct *p, LSA_Q_LOOKUP_SIDS *q_u, LSA_R_LOOKUP_SIDS *r_u)
568 struct lsa_info *handle;
569 DOM_SID2 *sid = q_u->sids.sid;
570 int num_entries = q_u->sids.num_entries;
571 DOM_R_REF *ref = NULL;
572 LSA_TRANS_NAME_ENUM *names = NULL;
573 uint32 mapped_count = 0;
575 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
576 return NT_STATUS_INVALID_HANDLE;
578 /* check if the user have enough rights */
579 if (!(handle->access & POLICY_LOOKUP_NAMES))
580 return NT_STATUS_ACCESS_DENIED;
582 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
583 names = (LSA_TRANS_NAME_ENUM *)talloc_zero(p->mem_ctx, sizeof(LSA_TRANS_NAME_ENUM));
586 return NT_STATUS_NO_MEMORY;
588 /* set up the LSA Lookup SIDs response */
589 init_lsa_trans_names(p->mem_ctx, ref, names, num_entries, sid, &mapped_count);
590 init_reply_lookup_sids(r_u, ref, names, mapped_count);
595 /***************************************************************************
596 lsa_reply_lookup_names
597 ***************************************************************************/
599 NTSTATUS _lsa_lookup_names(pipes_struct *p,LSA_Q_LOOKUP_NAMES *q_u, LSA_R_LOOKUP_NAMES *r_u)
601 struct lsa_info *handle;
602 UNISTR2 *names = q_u->uni_name;
603 int num_entries = q_u->num_entries;
606 uint32 mapped_count = 0;
608 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
609 return NT_STATUS_INVALID_HANDLE;
611 /* check if the user have enough rights */
612 if (!(handle->access & POLICY_LOOKUP_NAMES))
613 return NT_STATUS_ACCESS_DENIED;
615 ref = (DOM_R_REF *)talloc_zero(p->mem_ctx, sizeof(DOM_R_REF));
616 rids = (DOM_RID2 *)talloc_zero(p->mem_ctx, sizeof(DOM_RID2)*MAX_LOOKUP_SIDS);
619 return NT_STATUS_NO_MEMORY;
621 /* set up the LSA Lookup RIDs response */
622 init_lsa_rid2s(ref, rids, num_entries, names, &mapped_count, p->endian);
623 init_reply_lookup_names(r_u, ref, num_entries, rids, mapped_count);
628 /***************************************************************************
629 _lsa_close. Also weird - needs to check if lsa handle is correct. JRA.
630 ***************************************************************************/
632 NTSTATUS _lsa_close(pipes_struct *p, LSA_Q_CLOSE *q_u, LSA_R_CLOSE *r_u)
634 if (!find_policy_by_hnd(p, &q_u->pol, NULL))
635 return NT_STATUS_INVALID_HANDLE;
637 close_policy_hnd(p, &q_u->pol);
641 /***************************************************************************
642 "No more secrets Marty...." :-).
643 ***************************************************************************/
645 NTSTATUS _lsa_open_secret(pipes_struct *p, LSA_Q_OPEN_SECRET *q_u, LSA_R_OPEN_SECRET *r_u)
647 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
650 /***************************************************************************
652 ***************************************************************************/
654 NTSTATUS _lsa_enum_privs(pipes_struct *p, LSA_Q_ENUM_PRIVS *q_u, LSA_R_ENUM_PRIVS *r_u)
656 struct lsa_info *handle;
659 uint32 enum_context=q_u->enum_context;
660 LSA_PRIV_ENTRY *entry;
661 LSA_PRIV_ENTRY *entries=NULL;
663 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
664 return NT_STATUS_INVALID_HANDLE;
666 /* check if the user have enough rights */
669 * I don't know if it's the right one. not documented.
671 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
672 return NT_STATUS_ACCESS_DENIED;
674 if (enum_context >= PRIV_ALL_INDEX)
675 return NT_STATUS_NO_MORE_ENTRIES;
677 entries = (LSA_PRIV_ENTRY *)talloc_zero(p->mem_ctx, sizeof(LSA_PRIV_ENTRY) * (PRIV_ALL_INDEX));
679 return NT_STATUS_NO_MEMORY;
683 DEBUG(10,("_lsa_enum_privs: enum_context:%d total entries:%d\n", enum_context, PRIV_ALL_INDEX));
685 for (i = 0; i < PRIV_ALL_INDEX; i++, entry++) {
686 if( i<enum_context) {
687 init_uni_hdr(&entry->hdr_name, 0);
688 init_unistr2(&entry->name, NULL, 0 );
690 entry->luid_high = 0;
692 init_uni_hdr(&entry->hdr_name, strlen(privs[i+1].priv));
693 init_unistr2(&entry->name, privs[i+1].priv, strlen(privs[i+1].priv) );
694 entry->luid_low = privs[i+1].se_priv;
695 entry->luid_high = 0;
699 enum_context = PRIV_ALL_INDEX;
700 init_lsa_r_enum_privs(r_u, enum_context, PRIV_ALL_INDEX, entries);
705 /***************************************************************************
706 _lsa_priv_get_dispname.
707 ***************************************************************************/
709 NTSTATUS _lsa_priv_get_dispname(pipes_struct *p, LSA_Q_PRIV_GET_DISPNAME *q_u, LSA_R_PRIV_GET_DISPNAME *r_u)
711 struct lsa_info *handle;
715 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
716 return NT_STATUS_INVALID_HANDLE;
718 /* check if the user have enough rights */
721 * I don't know if it's the right one. not documented.
723 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
724 return NT_STATUS_ACCESS_DENIED;
726 unistr2_to_ascii(name_asc, &q_u->name, sizeof(name_asc));
728 DEBUG(10,("_lsa_priv_get_dispname: %s", name_asc));
730 while (privs[i].se_priv!=SE_PRIV_ALL && strcmp(name_asc, privs[i].priv))
733 if (privs[i].se_priv!=SE_PRIV_ALL) {
734 DEBUG(10,(": %s\n", privs[i].description));
735 init_uni_hdr(&r_u->hdr_desc, strlen(privs[i].description));
736 init_unistr2(&r_u->desc, privs[i].description, strlen(privs[i].description) );
738 r_u->ptr_info=0xdeadbeef;
739 r_u->lang_id=q_u->lang_id;
742 DEBUG(10,("_lsa_priv_get_dispname: doesn't exist\n"));
744 return NT_STATUS_NO_SUCH_PRIVILEGE;
748 /***************************************************************************
750 ***************************************************************************/
752 NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENUM_ACCOUNTS *r_u)
754 struct lsa_info *handle;
757 LSA_SID_ENUM *sids=&r_u->sids;
760 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
761 return NT_STATUS_INVALID_HANDLE;
763 /* check if the user have enough rights */
766 * I don't know if it's the right one. not documented.
768 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
769 return NT_STATUS_ACCESS_DENIED;
771 /* get the list of mapped groups (domain, local, builtin) */
772 if(!enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
775 if (q_u->enum_context >= num_entries)
776 return NT_STATUS_NO_MORE_ENTRIES;
778 sids->ptr_sid = (uint32 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(uint32));
779 sids->sid = (DOM_SID2 *)talloc_zero(p->mem_ctx, (num_entries-q_u->enum_context)*sizeof(DOM_SID2));
781 if (sids->ptr_sid==NULL || sids->sid==NULL) {
783 return NT_STATUS_NO_MEMORY;
786 for (i=q_u->enum_context, j=0; i<num_entries; i++) {
787 init_dom_sid2( &(*sids).sid[j], &map[i].sid);
788 (*sids).ptr_sid[j]=1;
794 init_lsa_r_enum_accounts(r_u, j);
800 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
802 fstring username, domname;
804 user_struct *vuser = get_valid_user_struct(p->vuid);
807 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
809 fstrcpy(username, vuser->user.smb_name);
810 fstrcpy(domname, vuser->user.domain);
812 ulen = strlen(username) + 1;
813 dlen = strlen(domname) + 1;
815 init_uni_hdr(&r_u->hdr_user_name, ulen);
816 r_u->ptr_user_name = 1;
817 init_unistr2(&r_u->uni2_user_name, username, ulen);
821 init_uni_hdr(&r_u->hdr_dom_name, dlen);
822 r_u->ptr_dom_name = 1;
823 init_unistr2(&r_u->uni2_dom_name, domname, dlen);
825 r_u->status = NT_STATUS_OK;
830 /***************************************************************************
832 ***************************************************************************/
834 NTSTATUS _lsa_open_account(pipes_struct *p, LSA_Q_OPENACCOUNT *q_u, LSA_R_OPENACCOUNT *r_u)
836 struct lsa_info *handle;
837 struct lsa_info *info;
839 r_u->status = NT_STATUS_OK;
841 /* find the connection policy handle. */
842 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
843 return NT_STATUS_INVALID_HANDLE;
845 /* check if the user have enough rights */
848 * I don't know if it's the right one. not documented.
849 * but guessed with rpcclient.
851 if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
852 return NT_STATUS_ACCESS_DENIED;
854 /* associate the user/group SID with the (unique) handle. */
855 if ((info = (struct lsa_info *)malloc(sizeof(struct lsa_info))) == NULL)
856 return NT_STATUS_NO_MEMORY;
859 info->sid = q_u->sid.sid;
860 info->access = q_u->access;
862 /* get a (unique) handle. open a policy on it. */
863 if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
864 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
869 /***************************************************************************
870 For a given SID, enumerate all the privilege this account has.
871 ***************************************************************************/
873 NTSTATUS _lsa_enum_privsaccount(pipes_struct *p, LSA_Q_ENUMPRIVSACCOUNT *q_u, LSA_R_ENUMPRIVSACCOUNT *r_u)
875 struct lsa_info *info=NULL;
881 r_u->status = NT_STATUS_OK;
883 /* find the connection policy handle. */
884 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
885 return NT_STATUS_INVALID_HANDLE;
887 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
888 return NT_STATUS_NO_SUCH_GROUP;
890 DEBUG(10,("_lsa_enum_privsaccount: %d privileges\n", map.priv_set.count));
891 if (map.priv_set.count!=0) {
893 set=(LUID_ATTR *)talloc(p->mem_ctx, map.priv_set.count*sizeof(LUID_ATTR));
895 free_privilege(&map.priv_set);
896 return NT_STATUS_NO_MEMORY;
899 for (i=0; i<map.priv_set.count; i++) {
900 set[i].luid.low=map.priv_set.set[i].luid.low;
901 set[i].luid.high=map.priv_set.set[i].luid.high;
902 set[i].attr=map.priv_set.set[i].attr;
903 DEBUG(10,("_lsa_enum_privsaccount: priv %d: %d:%d:%d\n", i,
904 set[i].luid.high, set[i].luid.low, set[i].attr));
908 init_lsa_r_enum_privsaccount(r_u, set, map.priv_set.count, 0);
909 free_privilege(&map.priv_set);
914 /***************************************************************************
916 ***************************************************************************/
918 NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA_R_GETSYSTEMACCOUNT *r_u)
920 struct lsa_info *info=NULL;
922 r_u->status = NT_STATUS_OK;
924 /* find the connection policy handle. */
925 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
926 return NT_STATUS_INVALID_HANDLE;
928 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITHOUT_PRIV))
929 return NT_STATUS_NO_SUCH_GROUP;
932 0x01 -> Log on locally
933 0x02 -> Access this computer from network
934 0x04 -> Log on as a batch job
935 0x10 -> Log on as a service
937 they can be ORed together
940 r_u->access=map.systemaccount;
945 /***************************************************************************
946 update the systemaccount information
947 ***************************************************************************/
949 NTSTATUS _lsa_setsystemaccount(pipes_struct *p, LSA_Q_SETSYSTEMACCOUNT *q_u, LSA_R_SETSYSTEMACCOUNT *r_u)
951 struct lsa_info *info=NULL;
953 r_u->status = NT_STATUS_OK;
955 /* find the connection policy handle. */
956 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
957 return NT_STATUS_INVALID_HANDLE;
959 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
960 return NT_STATUS_NO_SUCH_GROUP;
962 map.systemaccount=q_u->access;
964 if(!add_mapping_entry(&map, TDB_REPLACE))
965 return NT_STATUS_NO_SUCH_GROUP;
967 free_privilege(&map.priv_set);
972 /***************************************************************************
973 For a given SID, add some privileges.
974 ***************************************************************************/
976 NTSTATUS _lsa_addprivs(pipes_struct *p, LSA_Q_ADDPRIVS *q_u, LSA_R_ADDPRIVS *r_u)
978 struct lsa_info *info=NULL;
982 LUID_ATTR *luid_attr=NULL;
983 PRIVILEGE_SET *set=NULL;
985 r_u->status = NT_STATUS_OK;
987 /* find the connection policy handle. */
988 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
989 return NT_STATUS_INVALID_HANDLE;
991 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
992 return NT_STATUS_NO_SUCH_GROUP;
996 for (i=0; i<set->count; i++) {
997 luid_attr=&set->set[i];
999 /* check if the privilege is already there */
1000 if (check_priv_in_privilege(&map.priv_set, *luid_attr)){
1001 free_privilege(&map.priv_set);
1002 return NT_STATUS_NO_SUCH_PRIVILEGE;
1005 add_privilege(&map.priv_set, *luid_attr);
1008 if(!add_mapping_entry(&map, TDB_REPLACE))
1009 return NT_STATUS_NO_SUCH_GROUP;
1011 free_privilege(&map.priv_set);
1016 /***************************************************************************
1017 For a given SID, remove some privileges.
1018 ***************************************************************************/
1020 NTSTATUS _lsa_removeprivs(pipes_struct *p, LSA_Q_REMOVEPRIVS *q_u, LSA_R_REMOVEPRIVS *r_u)
1022 struct lsa_info *info=NULL;
1026 LUID_ATTR *luid_attr=NULL;
1027 PRIVILEGE_SET *set=NULL;
1029 r_u->status = NT_STATUS_OK;
1031 /* find the connection policy handle. */
1032 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&info))
1033 return NT_STATUS_INVALID_HANDLE;
1035 if (!get_group_map_from_sid(info->sid, &map, MAPPING_WITH_PRIV))
1036 return NT_STATUS_NO_SUCH_GROUP;
1038 if (q_u->allrights!=0) {
1039 /* log it and return, until I see one myself don't do anything */
1040 DEBUG(5,("_lsa_removeprivs: trying to remove all privileges ?\n"));
1041 return NT_STATUS_OK;
1045 /* log it and return, until I see one myself don't do anything */
1046 DEBUG(5,("_lsa_removeprivs: no privileges to remove ?\n"));
1047 return NT_STATUS_OK;
1052 for (i=0; i<set->count; i++) {
1053 luid_attr=&set->set[i];
1055 /* if we don't have the privilege, we're trying to remove, give up */
1056 /* what else can we do ??? JFM. */
1057 if (!check_priv_in_privilege(&map.priv_set, *luid_attr)){
1058 free_privilege(&map.priv_set);
1059 return NT_STATUS_NO_SUCH_PRIVILEGE;
1062 remove_privilege(&map.priv_set, *luid_attr);
1065 if(!add_mapping_entry(&map, TDB_REPLACE))
1066 return NT_STATUS_NO_SUCH_GROUP;
1068 free_privilege(&map.priv_set);
1073 /***************************************************************************
1074 For a given SID, remove some privileges.
1075 ***************************************************************************/
1077 NTSTATUS _lsa_query_secobj(pipes_struct *p, LSA_Q_QUERY_SEC_OBJ *q_u, LSA_R_QUERY_SEC_OBJ *r_u)
1079 struct lsa_info *handle=NULL;
1080 SEC_DESC *psd = NULL;
1084 r_u->status = NT_STATUS_OK;
1086 /* find the connection policy handle. */
1087 if (!find_policy_by_hnd(p, &q_u->pol, (void **)&handle))
1088 return NT_STATUS_INVALID_HANDLE;
1090 /* check if the user have enough rights */
1091 if (!(handle->access & POLICY_VIEW_LOCAL_INFORMATION))
1092 return NT_STATUS_ACCESS_DENIED;
1095 switch (q_u->sec_info) {
1097 /* SD contains only the owner */
1099 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1100 if(!NT_STATUS_IS_OK(status))
1101 return NT_STATUS_NO_MEMORY;
1104 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1105 return NT_STATUS_NO_MEMORY;
1108 /* SD contains only the ACL */
1110 status=lsa_get_generic_sd(p->mem_ctx, &psd, &sd_size);
1111 if(!NT_STATUS_IS_OK(status))
1112 return NT_STATUS_NO_MEMORY;
1114 if((r_u->buf = make_sec_desc_buf(p->mem_ctx, sd_size, psd)) == NULL)
1115 return NT_STATUS_NO_MEMORY;
1118 return NT_STATUS_INVALID_LEVEL;