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.
30 extern int DEBUGLEVEL;
31 extern DOM_SID global_sam_sid;
32 extern fstring global_sam_name;
33 extern DOM_SID global_member_sid;
34 extern fstring global_myworkgroup;
35 extern DOM_SID global_sid_S_1_5_20;
37 /***************************************************************************
38 lsa_reply_open_policy2
39 ***************************************************************************/
40 static void lsa_reply_open_policy2(prs_struct *rdata)
47 /* set up the LSA QUERY INFO response */
49 for (i = 4; i < POL_HND_SIZE; i++)
55 /* store the response in the SMB stream */
56 lsa_io_r_open_pol2("", &r_o, rdata, 0);
59 /***************************************************************************
61 ***************************************************************************/
62 static void lsa_reply_open_policy(prs_struct *rdata)
69 /* set up the LSA QUERY INFO response */
71 for (i = 4; i < POL_HND_SIZE; i++)
77 /* store the response in the SMB stream */
78 lsa_io_r_open_pol("", &r_o, rdata, 0);
81 /***************************************************************************
83 ***************************************************************************/
84 static void make_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
87 int domlen = strlen(dom_name);
89 d_q->uni_dom_max_len = domlen * 2;
90 d_q->uni_dom_str_len = domlen * 2;
92 d_q->buffer_dom_name = domlen != 0 ? 1 : 0; /* domain buffer pointer */
93 d_q->buffer_dom_sid = dom_sid != NULL ? 1 : 0; /* domain sid pointer */
95 /* this string is supposed to be character short */
96 make_unistr2(&(d_q->uni_domain_name), dom_name, domlen);
98 sid_to_string(sid_str, dom_sid);
99 make_dom_sid2(&(d_q->dom_sid), dom_sid);
102 /***************************************************************************
104 ***************************************************************************/
105 static void lsa_reply_enum_trust_dom(LSA_Q_ENUM_TRUST_DOM *q_e,
107 uint32 enum_context, char *dom_name, DOM_SID *dom_sid)
109 LSA_R_ENUM_TRUST_DOM r_e;
113 /* set up the LSA QUERY INFO response */
114 make_r_enum_trust_dom(&r_e, enum_context, dom_name, dom_sid,
115 dom_name != NULL ? 0x0 : 0x80000000 | NT_STATUS_UNABLE_TO_FREE_VM);
117 /* store the response in the SMB stream */
118 lsa_io_r_enum_trust_dom("", &r_e, rdata, 0);
121 /***************************************************************************
123 ***************************************************************************/
124 static void lsa_reply_query_info(LSA_Q_QUERY_INFO *q_q, prs_struct *rdata,
125 char *dom_name, DOM_SID *dom_sid)
127 LSA_R_QUERY_INFO r_q;
131 /* set up the LSA QUERY INFO response */
133 r_q.undoc_buffer = 0x22000000; /* bizarre */
134 r_q.info_class = q_q->info_class;
136 make_dom_query(&r_q.dom.id5, dom_name, dom_sid);
140 /* store the response in the SMB stream */
141 lsa_io_r_query("", &r_q, rdata, 0);
145 /***************************************************************************
146 make_dom_ref - adds a domain if it's not already in, returns the index
147 ***************************************************************************/
148 static int make_dom_ref(DOM_R_REF *ref, char *dom_name, DOM_SID *dom_sid)
154 if (dom_name != NULL)
156 for (num = 0; num < ref->num_ref_doms_1; num++)
159 fstrcpy(domname, unistr2_to_str(&ref->ref_dom[num].uni_dom_name));
160 if (strequal(domname, dom_name))
169 num = ref->num_ref_doms_1;
172 if (num >= MAX_REF_DOMAINS)
174 /* index not found, already at maximum domain limit */
178 ref->undoc_buffer = 1;
179 ref->num_ref_doms_1 = num+1;
180 ref->undoc_buffer2 = 1;
181 ref->max_entries = MAX_REF_DOMAINS;
182 ref->num_ref_doms_2 = num+1;
184 len = dom_name != NULL ? strlen(dom_name) : 0;
186 make_uni_hdr(&(ref->hdr_ref_dom[num].hdr_dom_name), len, len, len != 0 ? 1 : 0);
187 ref->hdr_ref_dom[num].ptr_dom_sid = dom_sid != NULL ? 1 : 0;
189 make_unistr2 (&(ref->ref_dom[num].uni_dom_name), dom_name, len);
190 make_dom_sid2(&(ref->ref_dom[num].ref_dom ), dom_sid );
195 /***************************************************************************
196 make_reply_lookup_names
197 ***************************************************************************/
198 static void make_reply_lookup_names(LSA_R_LOOKUP_NAMES *r_l,
200 DOM_SID dom_sids [MAX_LOOKUP_SIDS],
201 uint8 dom_types[MAX_LOOKUP_SIDS])
205 r_l->num_entries = 0;
206 r_l->undoc_buffer = 0;
207 r_l->num_entries2 = 0;
210 r_l->num_entries = num_entries;
211 r_l->undoc_buffer = 1;
212 r_l->num_entries2 = num_entries;
214 SMB_ASSERT_ARRAY(r_l->dom_rid, num_entries);
216 for (i = 0; i < num_entries; i++)
218 DOM_SID sid = dom_sids[i];
220 sid_split_rid(&sid, &rid);
221 make_dom_ref(&(r_l->dom_ref), dom_name, dom_sid);
222 make_dom_rid2(&(r_l->dom_rid[i]), rid, dom_types[i]);
225 r_l->num_entries3 = num_entries;
229 /***************************************************************************
231 ***************************************************************************/
232 static void make_lsa_trans_names(DOM_R_REF *ref,
233 LSA_TRANS_NAME_ENUM *trn,
234 int num_entries, DOM_SID2 sid[MAX_LOOKUP_SIDS],
235 uint32 *mapped_count)
241 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
243 for (i = 0; i < num_entries; i++)
246 DOM_SID find_sid = sid[i].sid;
247 DOM_SID tmp_sid = sid[i].sid;
248 uint32 rid = 0xffffffff;
252 uint8 sid_name_use = 0;
254 memset(dom_name, 0, sizeof(dom_name));
255 memset(name , 0, sizeof(name ));
257 if (map_domain_sid_to_name(&find_sid, dom_name))
259 sid_name_use = SID_NAME_DOMAIN;
260 dom_idx = make_dom_ref(ref, dom_name, &find_sid);
262 else if (sid_split_rid (&find_sid, &rid) &&
263 map_domain_sid_to_name(&find_sid, dom_name))
265 if (sid_equal(&find_sid, &global_sam_sid) ||
266 sid_equal(&find_sid, &global_sid_S_1_5_20))
268 status = lookup_sid(&tmp_sid, name, &sid_name_use);
272 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
277 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
280 dom_idx = make_dom_ref(ref, dom_name, &find_sid);
288 snprintf(name, sizeof(name), "%08x", rid);
289 sid_name_use = SID_NAME_UNKNOWN;
292 make_lsa_trans_name(&(trn->name [total]),
293 &(trn->uni_name[total]),
294 sid_name_use, name, dom_idx);
298 trn->num_entries = total;
299 trn->ptr_trans_names = 1;
300 trn->num_entries2 = total;
303 /***************************************************************************
304 make_reply_lookup_sids
305 ***************************************************************************/
306 static void make_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
307 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
308 uint32 mapped_count, uint32 status)
312 r_l->mapped_count = mapped_count;
313 r_l->status = status;
316 /***************************************************************************
317 lsa_reply_lookup_sids
318 ***************************************************************************/
319 static void lsa_reply_lookup_sids(prs_struct *rdata,
320 DOM_SID2 *sid, int num_entries)
322 LSA_R_LOOKUP_SIDS r_l;
324 LSA_TRANS_NAME_ENUM names;
325 uint32 mapped_count = 0;
331 /* set up the LSA Lookup SIDs response */
332 make_lsa_trans_names(&ref, &names, num_entries, sid, &mapped_count);
333 make_reply_lookup_sids(&r_l, &ref, &names, mapped_count, 0x0);
335 /* store the response in the SMB stream */
336 lsa_io_r_lookup_sids("", &r_l, rdata, 0);
339 /***************************************************************************
340 lsa_reply_lookup_names
341 ***************************************************************************/
342 static void lsa_reply_lookup_names(prs_struct *rdata,
344 DOM_SID dom_sids [MAX_LOOKUP_SIDS],
345 uint8 dom_types[MAX_LOOKUP_SIDS])
347 LSA_R_LOOKUP_NAMES r_l;
351 /* set up the LSA Lookup RIDs response */
352 make_reply_lookup_names(&r_l, num_entries, dom_sids, dom_types);
356 /* store the response in the SMB stream */
357 lsa_io_r_lookup_names("", &r_l, rdata, 0);
360 /***************************************************************************
362 ***************************************************************************/
363 static void api_lsa_open_policy2( uint16 vuid, prs_struct *data,
370 /* grab the server, object attributes and desired access flag...*/
371 lsa_io_q_open_pol2("", &q_o, data, 0);
373 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
375 /* return a 20 byte policy handle */
376 lsa_reply_open_policy2(rdata);
379 /***************************************************************************
381 ***************************************************************************/
382 static void api_lsa_open_policy( uint16 vuid, prs_struct *data,
389 /* grab the server, object attributes and desired access flag...*/
390 lsa_io_q_open_pol("", &q_o, data, 0);
392 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
394 /* return a 20 byte policy handle */
395 lsa_reply_open_policy(rdata);
398 /***************************************************************************
399 api_lsa_enum_trust_dom
400 ***************************************************************************/
401 static void api_lsa_enum_trust_dom( uint16 vuid, prs_struct *data,
404 LSA_Q_ENUM_TRUST_DOM q_e;
408 /* grab the enum trust domain context etc. */
409 lsa_io_q_enum_trust_dom("", &q_e, data, 0);
411 /* construct reply. return status is always 0x0 */
412 lsa_reply_enum_trust_dom(&q_e, rdata, 0, NULL, NULL);
415 /***************************************************************************
417 ***************************************************************************/
418 static void api_lsa_query_info( uint16 vuid, prs_struct *data,
421 LSA_Q_QUERY_INFO q_i;
424 memset(name, 0, sizeof(name));
428 /* grab the info class and policy handle */
429 lsa_io_q_query("", &q_i, data, 0);
431 switch (q_i.info_class)
435 fstrcpy(name, global_myworkgroup);
436 sid = &global_member_sid;
441 fstrcpy(name, global_sam_name);
442 sid = &global_sam_sid;
447 DEBUG(5,("unknown info level in Lsa Query: %d\n",
453 /* construct reply. return status is always 0x0 */
454 lsa_reply_query_info(&q_i, rdata, name, sid);
457 /***************************************************************************
459 ***************************************************************************/
460 static void api_lsa_lookup_sids( uint16 vuid, prs_struct *data,
463 LSA_Q_LOOKUP_SIDS q_l;
466 /* grab the info class and policy handle */
467 lsa_io_q_lookup_sids("", &q_l, data, 0);
469 /* construct reply. return status is always 0x0 */
470 lsa_reply_lookup_sids(rdata, q_l.sids.sid, q_l.sids.num_entries);
473 /***************************************************************************
475 ***************************************************************************/
476 static void api_lsa_lookup_names( uint16 vuid, prs_struct *data,
480 LSA_Q_LOOKUP_NAMES q_l;
481 DOM_SID dom_sids [MAX_LOOKUP_SIDS];
482 uint8 dom_types[MAX_LOOKUP_SIDS];
485 ZERO_ARRAY(dom_sids);
487 /* grab the info class and policy handle */
488 lsa_io_q_lookup_names("", &q_l, data, 0);
490 SMB_ASSERT_ARRAY(q_l.uni_name, q_l.num_entries);
492 /* convert received RIDs to strings, so we can do them. */
493 for (i = 0; i < q_l.num_entries; i++)
496 fstrcpy(name, unistr2_to_str(&q_l.uni_name[i]));
498 if (!lookup_name(name, &dom_sids[i], &dom_types[i]))
500 dom_types[i] = SID_NAME_UNKNOWN;
504 /* construct reply. return status is always 0x0 */
505 lsa_reply_lookup_names(rdata,
507 dom_sids, /* text-converted SIDs */
508 dom_types); /* SID_NAME_USE types */
511 /***************************************************************************
513 ***************************************************************************/
514 static void api_lsa_close( uint16 vuid, prs_struct *data,
517 /* XXXX this is NOT good */
518 char *q = mem_data(&(rdata->data), rdata->offset);
536 /***************************************************************************
538 ***************************************************************************/
539 static void api_lsa_open_secret( uint16 vuid, prs_struct *data,
542 /* XXXX this is NOT good */
543 char *q = mem_data(&(rdata->data), rdata->offset);
555 SIVAL(q, 0, 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND);
561 /***************************************************************************
563 ***************************************************************************/
564 static struct api_struct api_lsa_cmds[] =
566 { "LSA_OPENPOLICY2" , LSA_OPENPOLICY2 , api_lsa_open_policy2 },
567 { "LSA_OPENPOLICY" , LSA_OPENPOLICY , api_lsa_open_policy },
568 { "LSA_QUERYINFOPOLICY" , LSA_QUERYINFOPOLICY , api_lsa_query_info },
569 { "LSA_ENUMTRUSTDOM" , LSA_ENUMTRUSTDOM , api_lsa_enum_trust_dom },
570 { "LSA_CLOSE" , LSA_CLOSE , api_lsa_close },
571 { "LSA_OPENSECRET" , LSA_OPENSECRET , api_lsa_open_secret },
572 { "LSA_LOOKUPSIDS" , LSA_LOOKUPSIDS , api_lsa_lookup_sids },
573 { "LSA_LOOKUPNAMES" , LSA_LOOKUPNAMES , api_lsa_lookup_names },
577 /***************************************************************************
579 ***************************************************************************/
580 BOOL api_ntlsa_rpc(pipes_struct *p, prs_struct *data)
582 return api_rpcTNP(p, "api_ntlsa_rpc", api_lsa_cmds, data);