3 * Unix SMB/Netbios implementation.
5 * RPC Pipe client / server routines
6 * Copyright (C) Andrew Tridgell 1992-1997,
7 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
8 * Copyright (C) Paul Ashton 1997.
9 * Copyright (C) Jeremy Allison 1998.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 extern int DEBUGLEVEL;
29 extern DOM_SID global_sam_sid;
30 extern fstring global_myworkgroup;
31 extern pstring global_myname;
33 /***************************************************************************
34 lsa_reply_open_policy2
35 ***************************************************************************/
37 static BOOL lsa_reply_open_policy2(prs_struct *rdata)
44 /* set up the LSA QUERY INFO response */
46 for (i = 4; i < POL_HND_SIZE; i++)
50 /* store the response in the SMB stream */
51 if(!lsa_io_r_open_pol2("", &r_o, rdata, 0)) {
52 DEBUG(0,("lsa_reply_open_policy2: unable to marshall LSA_R_OPEN_POL2.\n"));
59 /***************************************************************************
61 ***************************************************************************/
63 static BOOL lsa_reply_open_policy(prs_struct *rdata)
70 /* set up the LSA QUERY INFO response */
72 for (i = 4; i < POL_HND_SIZE; i++)
76 /* store the response in the SMB stream */
77 if(!lsa_io_r_open_pol("", &r_o, rdata, 0)) {
78 DEBUG(0,("lsa_reply_open_policy: unable to marshall LSA_R_OPEN_POL.\n"));
85 /***************************************************************************
87 ***************************************************************************/
89 static void init_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
91 int domlen = (dom_name != NULL) ? strlen(dom_name) : 0;
93 d_q->uni_dom_max_len = domlen * 2;
94 d_q->uni_dom_str_len = domlen * 2;
96 d_q->buffer_dom_name = (dom_name != 0) ? 1 : 0;
97 d_q->buffer_dom_sid = (dom_sid != NULL) ? 1 : 0;
99 /* this string is supposed to be character short */
100 init_unistr2(&d_q->uni_domain_name, dom_name, domlen);
102 init_dom_sid2(&d_q->dom_sid, dom_sid);
105 /***************************************************************************
106 lsa_reply_enum_trust_dom
107 ***************************************************************************/
109 static void lsa_reply_enum_trust_dom(LSA_Q_ENUM_TRUST_DOM *q_e,
111 uint32 enum_context, char *dom_name, DOM_SID *dom_sid)
113 LSA_R_ENUM_TRUST_DOM r_e;
117 /* set up the LSA QUERY INFO response */
118 init_r_enum_trust_dom(&r_e, enum_context, dom_name, dom_sid,
119 dom_name != NULL ? 0x0 : 0x80000000 | NT_STATUS_UNABLE_TO_FREE_VM);
121 /* store the response in the SMB stream */
122 lsa_io_r_enum_trust_dom("", &r_e, rdata, 0);
125 /***************************************************************************
127 ***************************************************************************/
129 static BOOL lsa_reply_query_info(LSA_Q_QUERY_INFO *q_q, prs_struct *rdata,
130 char *dom_name, DOM_SID *dom_sid, uint32 status_code)
132 LSA_R_QUERY_INFO r_q;
136 /* set up the LSA QUERY INFO response */
138 if(status_code == 0) {
139 r_q.undoc_buffer = 0x22000000; /* bizarre */
140 r_q.info_class = q_q->info_class;
142 init_dom_query(&r_q.dom.id5, dom_name, dom_sid);
145 r_q.status = status_code;
147 /* store the response in the SMB stream */
148 if(!lsa_io_r_query("", &r_q, rdata, 0)) {
149 DEBUG(0,("lsa_reply_query_info: failed to marshall LSA_R_QUERY_INFO.\n"));
156 /***************************************************************************
157 init_dom_ref - adds a domain if it's not already in, returns the index.
158 ***************************************************************************/
160 static int init_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
165 if (dom_name != NULL) {
166 for (num = 0; num < ref->num_ref_doms_1; num++) {
168 fstrcpy(domname, dos_unistr2_to_str(&ref->ref_dom[num].uni_dom_name));
169 if (strequal(domname, dom_name))
173 num = ref->num_ref_doms_1;
176 if (num >= MAX_REF_DOMAINS) {
177 /* index not found, already at maximum domain limit */
181 ref->num_ref_doms_1 = num+1;
182 ref->ptr_ref_dom = 1;
183 ref->max_entries = MAX_REF_DOMAINS;
184 ref->num_ref_doms_2 = num+1;
186 len = (dom_name != NULL) ? strlen(dom_name) : 0;
187 if(dom_name != NULL && len == 0)
190 init_uni_hdr(&ref->hdr_ref_dom[num].hdr_dom_name, len);
191 ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
193 init_unistr2(&ref->ref_dom[num].uni_dom_name, dom_name, len);
194 init_dom_sid2(&ref->ref_dom[num].ref_dom, dom_sid );
199 /***************************************************************************
201 ***************************************************************************/
203 static void init_lsa_rid2s(DOM_R_REF *ref, DOM_RID2 *rid2,
204 int num_entries, UNISTR2 name[MAX_LOOKUP_SIDS],
205 uint32 *mapped_count)
211 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
213 for (i = 0; i < num_entries; i++) {
217 uint32 rid = 0xffffffff;
222 uint8 sid_name_use = SID_NAME_UNKNOWN;
224 pstrcpy(full_name, dos_unistr2_to_str(&name[i]));
227 * Try and split the name into a DOMAIN and
231 split_domain_name(full_name, dom_name, user);
234 * We only do anything with this name if we
235 * can map the Domain into a SID we know.
238 if (map_domain_name_to_sid(&dom_sid, dom_name)) {
239 dom_idx = init_dom_ref(ref, dom_name, &dom_sid);
241 if (lookup_local_name(dom_name, user, &sid, &sid_name_use) && sid_split_rid(&sid, &rid))
250 sid_name_use = SID_NAME_UNKNOWN;
253 init_dom_rid2(&rid2[total], rid, sid_name_use, dom_idx);
258 /***************************************************************************
259 init_reply_lookup_names
260 ***************************************************************************/
262 static void init_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
263 DOM_R_REF *ref, uint32 num_entries,
264 DOM_RID2 *rid2, uint32 mapped_count)
266 r_l->ptr_dom_ref = 1;
269 r_l->num_entries = num_entries;
270 r_l->ptr_entries = 1;
271 r_l->num_entries2 = num_entries;
274 r_l->mapped_count = mapped_count;
276 if (mapped_count == 0)
277 r_l->status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
282 /* Call winbindd to convert sid to name */
284 BOOL winbind_lookup_sid(DOM_SID *sid, fstring dom_name, fstring name,
287 struct winbindd_request request;
288 struct winbindd_response response;
289 enum winbindd_result result;
294 if (!name_type) return False;
296 /* Check if this is our own sid. This should perhaps be done by
297 winbind? For the moment handle it here. */
299 if (sid->num_auths == 5) {
300 sid_copy(&tmp_sid, sid);
301 sid_split_rid(&tmp_sid, &rid);
303 if (sid_equal(&global_sam_sid, &tmp_sid)) {
305 return map_domain_sid_to_name(&tmp_sid, dom_name) &&
306 lookup_local_rid(rid, name, name_type);
310 /* Initialise request */
312 ZERO_STRUCT(request);
313 ZERO_STRUCT(response);
315 sid_to_string(sid_str, sid);
316 fstrcpy(request.data.sid, sid_str);
320 result = generic_request(WINBINDD_LOOKUPSID, &request, &response);
322 /* Copy out result */
324 if (result == WINBINDD_OK) {
325 parse_domain_user(response.data.name.name, dom_name, name);
326 *name_type = response.data.name.type;
329 return (result == WINBINDD_OK);
332 /***************************************************************************
333 Init lsa_trans_names.
334 ***************************************************************************/
336 static void init_lsa_trans_names(DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *trn,
337 int num_entries, DOM_SID2 sid[MAX_LOOKUP_SIDS], uint32 *mapped_count)
343 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
345 for (i = 0; i < num_entries; i++) {
347 DOM_SID find_sid = sid[i].sid;
348 uint32 rid = 0xffffffff;
350 fstring name, dom_name;
351 uint8 sid_name_use = 0;
353 /* Lookup sid from winbindd */
355 memset(dom_name, '\0', sizeof(dom_name));
356 memset(name, '\0', sizeof(name));
358 status = winbind_lookup_sid(&find_sid, dom_name, name,
362 sid_name_use = SID_NAME_UNKNOWN;
365 /* Store domain sid in ref array */
367 if (find_sid.num_auths == 5) {
368 sid_split_rid(&find_sid, &rid);
371 dom_idx = init_dom_ref(ref, dom_name, &find_sid);
373 DEBUG(10,("init_lsa_trans_names: added user '%s\\%s' to referenced list.\n", dom_name, name ));
377 init_lsa_trans_name(&trn->name[total], &trn->uni_name[total],
378 sid_name_use, name, dom_idx);
382 trn->num_entries = total;
383 trn->ptr_trans_names = 1;
384 trn->num_entries2 = total;
387 /***************************************************************************
388 Init_reply_lookup_sids.
389 ***************************************************************************/
391 static void init_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
392 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
395 r_l->ptr_dom_ref = 1;
398 r_l->mapped_count = mapped_count;
400 if (mapped_count == 0)
401 r_l->status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
406 /***************************************************************************
407 lsa_reply_lookup_sids
408 ***************************************************************************/
410 static BOOL lsa_reply_lookup_sids(prs_struct *rdata, DOM_SID2 *sid, int num_entries)
412 LSA_R_LOOKUP_SIDS r_l;
414 LSA_TRANS_NAME_ENUM names;
415 uint32 mapped_count = 0;
421 /* set up the LSA Lookup SIDs response */
422 init_lsa_trans_names(&ref, &names, num_entries, sid, &mapped_count);
423 init_reply_lookup_sids(&r_l, &ref, &names, mapped_count);
425 /* store the response in the SMB stream */
426 if(!lsa_io_r_lookup_sids("", &r_l, rdata, 0)) {
427 DEBUG(0,("lsa_reply_lookup_sids: Failed to marshall LSA_R_LOOKUP_SIDS.\n"));
434 /***************************************************************************
435 lsa_reply_lookup_names
436 ***************************************************************************/
438 static BOOL lsa_reply_lookup_names(prs_struct *rdata,
439 UNISTR2 names[MAX_LOOKUP_SIDS], int num_entries)
441 LSA_R_LOOKUP_NAMES r_l;
443 DOM_RID2 rids[MAX_LOOKUP_SIDS];
444 uint32 mapped_count = 0;
450 /* set up the LSA Lookup RIDs response */
451 init_lsa_rid2s(&ref, rids, num_entries, names, &mapped_count);
452 init_reply_lookup_names(&r_l, &ref, num_entries, rids, mapped_count);
454 /* store the response in the SMB stream */
455 if(!lsa_io_r_lookup_names("", &r_l, rdata, 0)) {
456 DEBUG(0,("lsa_reply_lookup_names: Failed to marshall LSA_R_LOOKUP_NAMES.\n"));
463 /***************************************************************************
465 ***************************************************************************/
467 static BOOL api_lsa_open_policy2(pipes_struct *p)
469 prs_struct *data = &p->in_data.data;
470 prs_struct *rdata = &p->out_data.rdata;
476 /* grab the server, object attributes and desired access flag...*/
477 if(!lsa_io_q_open_pol2("", &q_o, data, 0)) {
478 DEBUG(0,("api_lsa_open_policy2: unable to unmarshall LSA_Q_OPEN_POL2.\n"));
482 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
484 /* return a 20 byte policy handle */
485 if(!lsa_reply_open_policy2(rdata))
491 /***************************************************************************
493 ***************************************************************************/
494 static BOOL api_lsa_open_policy(pipes_struct *p)
496 prs_struct *data = &p->in_data.data;
497 prs_struct *rdata = &p->out_data.rdata;
503 /* grab the server, object attributes and desired access flag...*/
504 if(!lsa_io_q_open_pol("", &q_o, data, 0)) {
505 DEBUG(0,("api_lsa_open_policy: unable to unmarshall LSA_Q_OPEN_POL.\n"));
509 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
511 /* return a 20 byte policy handle */
512 if(!lsa_reply_open_policy(rdata))
518 /***************************************************************************
519 api_lsa_enum_trust_dom
520 ***************************************************************************/
521 static BOOL api_lsa_enum_trust_dom(pipes_struct *p)
523 LSA_Q_ENUM_TRUST_DOM q_e;
524 prs_struct *data = &p->in_data.data;
525 prs_struct *rdata = &p->out_data.rdata;
529 /* grab the enum trust domain context etc. */
530 if(!lsa_io_q_enum_trust_dom("", &q_e, data, 0))
533 /* construct reply. return status is always 0x0 */
534 lsa_reply_enum_trust_dom(&q_e, rdata, 0, NULL, NULL);
539 /***************************************************************************
541 ***************************************************************************/
542 static BOOL api_lsa_query_info(pipes_struct *p)
544 LSA_Q_QUERY_INFO q_i;
548 uint32 status_code = 0;
549 prs_struct *data = &p->in_data.data;
550 prs_struct *rdata = &p->out_data.rdata;
554 /* grab the info class and policy handle */
555 if(!lsa_io_q_query("", &q_i, data, 0)) {
556 DEBUG(0,("api_lsa_query_info: failed to unmarshall LSA_Q_QUERY_INFO.\n"));
560 switch (q_i.info_class) {
562 switch (lp_server_role())
564 case ROLE_DOMAIN_PDC:
565 case ROLE_DOMAIN_BDC:
566 name = global_myworkgroup;
567 sid = &global_sam_sid;
569 case ROLE_DOMAIN_MEMBER:
570 if (secrets_fetch_domain_sid(global_myworkgroup,
573 name = global_myworkgroup;
581 name = global_myname;
582 sid = &global_sam_sid;
585 DEBUG(0,("api_lsa_query_info: unknown info level in Lsa Query: %d\n", q_i.info_class));
586 status_code = (NT_STATUS_INVALID_INFO_CLASS | 0xC0000000);
590 /* construct reply. return status is always 0x0 */
591 if(!lsa_reply_query_info(&q_i, rdata, name, sid, status_code))
597 /***************************************************************************
599 ***************************************************************************/
601 static BOOL api_lsa_lookup_sids(pipes_struct *p)
603 LSA_Q_LOOKUP_SIDS q_l;
604 prs_struct *data = &p->in_data.data;
605 prs_struct *rdata = &p->out_data.rdata;
609 /* grab the info class and policy handle */
610 if(!lsa_io_q_lookup_sids("", &q_l, data, 0)) {
611 DEBUG(0,("api_lsa_lookup_sids: failed to unmarshall LSA_Q_LOOKUP_SIDS.\n"));
615 /* construct reply. return status is always 0x0 */
616 if(!lsa_reply_lookup_sids(rdata, q_l.sids.sid, q_l.sids.num_entries))
622 /***************************************************************************
624 ***************************************************************************/
626 static BOOL api_lsa_lookup_names(pipes_struct *p)
628 LSA_Q_LOOKUP_NAMES q_l;
629 prs_struct *data = &p->in_data.data;
630 prs_struct *rdata = &p->out_data.rdata;
634 /* grab the info class and policy handle */
635 if(!lsa_io_q_lookup_names("", &q_l, data, 0)) {
636 DEBUG(0,("api_lsa_lookup_names: failed to unmarshall LSA_Q_LOOKUP_NAMES.\n"));
640 SMB_ASSERT_ARRAY(q_l.uni_name, q_l.num_entries);
642 return lsa_reply_lookup_names(rdata, q_l.uni_name, q_l.num_entries);
645 /***************************************************************************
647 ***************************************************************************/
648 static BOOL api_lsa_close(pipes_struct *p)
651 prs_struct *data = &p->in_data.data;
652 prs_struct *rdata = &p->out_data.rdata;
656 /* store the response in the SMB stream */
657 if (!lsa_io_r_close("", &r_c, rdata, 0)) {
658 DEBUG(0,("api_lsa_close: lsa_io_r_close failed.\n"));
665 /***************************************************************************
667 ***************************************************************************/
668 static BOOL api_lsa_open_secret(pipes_struct *p)
670 /* XXXX this is NOT good */
673 prs_struct *data = &p->in_data.data;
674 prs_struct *rdata = &p->out_data.rdata;
676 for(i =0; i < 4; i++) {
677 if(!prs_uint32("api_lsa_close", rdata, 1, &dummy)) {
678 DEBUG(0,("api_lsa_open_secret: prs_uint32 %d failed.\n",
684 dummy = 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND;
685 if(!prs_uint32("api_lsa_close", rdata, 1, &dummy)) {
686 DEBUG(0,("api_lsa_open_secret: prs_uint32 status failed.\n"));
693 /***************************************************************************
695 ***************************************************************************/
696 static struct api_struct api_lsa_cmds[] =
698 { "LSA_OPENPOLICY2" , LSA_OPENPOLICY2 , api_lsa_open_policy2 },
699 { "LSA_OPENPOLICY" , LSA_OPENPOLICY , api_lsa_open_policy },
700 { "LSA_QUERYINFOPOLICY" , LSA_QUERYINFOPOLICY , api_lsa_query_info },
701 { "LSA_ENUMTRUSTDOM" , LSA_ENUMTRUSTDOM , api_lsa_enum_trust_dom },
702 { "LSA_CLOSE" , LSA_CLOSE , api_lsa_close },
703 { "LSA_OPENSECRET" , LSA_OPENSECRET , api_lsa_open_secret },
704 { "LSA_LOOKUPSIDS" , LSA_LOOKUPSIDS , api_lsa_lookup_sids },
705 { "LSA_LOOKUPNAMES" , LSA_LOOKUPNAMES , api_lsa_lookup_names },
709 /***************************************************************************
711 ***************************************************************************/
712 BOOL api_ntlsa_rpc(pipes_struct *p)
714 return api_rpcTNP(p, "api_ntlsa_rpc", api_lsa_cmds);