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_1;
36 extern DOM_SID global_sid_S_1_3;
37 extern DOM_SID global_sid_S_1_5;
39 /***************************************************************************
40 lsa_reply_open_policy2
41 ***************************************************************************/
42 static void lsa_reply_open_policy2(prs_struct *rdata)
49 /* set up the LSA QUERY INFO response */
51 for (i = 4; i < POL_HND_SIZE; i++)
57 /* store the response in the SMB stream */
58 lsa_io_r_open_pol2("", &r_o, rdata, 0);
61 /***************************************************************************
63 ***************************************************************************/
64 static void lsa_reply_open_policy(prs_struct *rdata)
71 /* set up the LSA QUERY INFO response */
73 for (i = 4; i < POL_HND_SIZE; i++)
79 /* store the response in the SMB stream */
80 lsa_io_r_open_pol("", &r_o, rdata, 0);
83 /***************************************************************************
85 ***************************************************************************/
86 static void make_dom_query(DOM_QUERY *d_q, char *dom_name, DOM_SID *dom_sid)
88 int domlen = strlen(dom_name);
90 d_q->uni_dom_max_len = domlen * 2;
91 d_q->uni_dom_str_len = domlen * 2;
93 d_q->buffer_dom_name = domlen != 0 ? 1 : 0; /* domain buffer pointer */
94 d_q->buffer_dom_sid = dom_sid != NULL ? 1 : 0; /* domain sid pointer */
96 /* this string is supposed to be character short */
97 make_unistr2(&(d_q->uni_domain_name), dom_name, domlen);
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_rids
197 ***************************************************************************/
198 static void make_reply_lookup_rids(LSA_R_LOOKUP_RIDS *r_l,
200 uint32 dom_rids[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 make_dom_ref(&(r_l->dom_ref), dom_name, dom_sid);
219 make_dom_rid2(&(r_l->dom_rid[i]), dom_rids[i], dom_types[i]);
222 r_l->num_entries3 = num_entries;
226 /***************************************************************************
228 ***************************************************************************/
229 static void make_lsa_trans_names(DOM_R_REF *ref,
230 LSA_TRANS_NAME_ENUM *trn,
231 int num_entries, DOM_SID2 sid[MAX_LOOKUP_SIDS],
232 uint32 *mapped_count)
238 SMB_ASSERT(num_entries <= MAX_LOOKUP_SIDS);
240 for (i = 0; i < num_entries; i++)
243 DOM_SID find_sid = sid[i].sid;
244 DOM_SID tmp = sid[i].sid;
245 uint32 rid = 0xffffffff;
249 uint8 sid_name_use = 0;
251 memset(dom_name, 0, sizeof(dom_name));
252 memset(name , 0, sizeof(name ));
254 if (map_domain_sid_to_name(&find_sid, dom_name))
256 sid_name_use = SID_NAME_DOMAIN;
257 dom_idx = make_dom_ref(ref, dom_name, &find_sid);
259 else if (sid_split_rid (&find_sid, &rid) &&
260 map_domain_sid_to_name(&find_sid, dom_name))
262 if (sid_equal(&find_sid, &global_sam_sid))
264 status = lookup_name(&tmp, name, &sid_name_use);
268 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
273 status = 0xC0000000 | NT_STATUS_NONE_MAPPED;
276 dom_idx = make_dom_ref(ref, dom_name, &find_sid);
284 snprintf(name, sizeof(name), "%08x", rid);
285 sid_name_use = SID_NAME_UNKNOWN;
288 make_lsa_trans_name(&(trn->name [total]),
289 &(trn->uni_name[total]),
290 sid_name_use, name, dom_idx);
294 trn->num_entries = total;
295 trn->ptr_trans_names = 1;
296 trn->num_entries2 = total;
299 /***************************************************************************
300 make_reply_lookup_sids
301 ***************************************************************************/
302 static void make_reply_lookup_sids(LSA_R_LOOKUP_SIDS *r_l,
303 DOM_R_REF *ref, LSA_TRANS_NAME_ENUM *names,
304 uint32 mapped_count, uint32 status)
308 r_l->mapped_count = mapped_count;
309 r_l->status = status;
312 /***************************************************************************
313 lsa_reply_lookup_sids
314 ***************************************************************************/
315 static void lsa_reply_lookup_sids(prs_struct *rdata,
316 DOM_SID2 *sid, int num_entries)
318 LSA_R_LOOKUP_SIDS r_l;
320 LSA_TRANS_NAME_ENUM names;
321 uint32 mapped_count = 0;
327 /* set up the LSA Lookup SIDs response */
328 make_lsa_trans_names(&ref, &names, num_entries, sid, &mapped_count);
329 make_reply_lookup_sids(&r_l, &ref, &names, mapped_count, 0x0);
331 /* store the response in the SMB stream */
332 lsa_io_r_lookup_sids("", &r_l, rdata, 0);
335 /***************************************************************************
336 lsa_reply_lookup_rids
337 ***************************************************************************/
338 static void lsa_reply_lookup_rids(prs_struct *rdata,
340 uint32 dom_rids[MAX_LOOKUP_SIDS],
341 uint8 dom_types[MAX_LOOKUP_SIDS])
343 LSA_R_LOOKUP_RIDS r_l;
347 /* set up the LSA Lookup RIDs response */
348 make_reply_lookup_rids(&r_l, num_entries, dom_rids, dom_types);
352 /* store the response in the SMB stream */
353 lsa_io_r_lookup_rids("", &r_l, rdata, 0);
356 /***************************************************************************
358 ***************************************************************************/
359 static void api_lsa_open_policy2( uint16 vuid, prs_struct *data,
366 /* grab the server, object attributes and desired access flag...*/
367 lsa_io_q_open_pol2("", &q_o, data, 0);
369 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
371 /* return a 20 byte policy handle */
372 lsa_reply_open_policy2(rdata);
375 /***************************************************************************
377 ***************************************************************************/
378 static void api_lsa_open_policy( uint16 vuid, prs_struct *data,
385 /* grab the server, object attributes and desired access flag...*/
386 lsa_io_q_open_pol("", &q_o, data, 0);
388 /* lkclXXXX having decoded it, ignore all fields in the open policy! */
390 /* return a 20 byte policy handle */
391 lsa_reply_open_policy(rdata);
394 /***************************************************************************
395 api_lsa_enum_trust_dom
396 ***************************************************************************/
397 static void api_lsa_enum_trust_dom( uint16 vuid, prs_struct *data,
400 LSA_Q_ENUM_TRUST_DOM q_e;
404 /* grab the enum trust domain context etc. */
405 lsa_io_q_enum_trust_dom("", &q_e, data, 0);
407 /* construct reply. return status is always 0x0 */
408 lsa_reply_enum_trust_dom(&q_e, rdata, 0, NULL, NULL);
411 /***************************************************************************
413 ***************************************************************************/
414 static void api_lsa_query_info( uint16 vuid, prs_struct *data,
417 LSA_Q_QUERY_INFO q_i;
420 memset(name, 0, sizeof(name));
424 /* grab the info class and policy handle */
425 lsa_io_q_query("", &q_i, data, 0);
427 switch (q_i.info_class)
431 fstrcpy(name, global_myworkgroup);
432 sid = &global_member_sid;
437 fstrcpy(name, global_sam_name);
438 sid = &global_sam_sid;
443 DEBUG(5,("unknown info level in Lsa Query: %d\n",
449 /* construct reply. return status is always 0x0 */
450 lsa_reply_query_info(&q_i, rdata, name, sid);
453 /***************************************************************************
455 ***************************************************************************/
456 static void api_lsa_lookup_sids( uint16 vuid, prs_struct *data,
459 LSA_Q_LOOKUP_SIDS q_l;
462 /* grab the info class and policy handle */
463 lsa_io_q_lookup_sids("", &q_l, data, 0);
465 /* construct reply. return status is always 0x0 */
466 lsa_reply_lookup_sids(rdata, q_l.sids.sid, q_l.sids.num_entries);
469 /***************************************************************************
471 ***************************************************************************/
472 static void api_lsa_lookup_names( uint16 vuid, prs_struct *data,
476 LSA_Q_LOOKUP_RIDS q_l;
477 uint32 dom_rids[MAX_LOOKUP_SIDS];
478 uint8 dom_types[MAX_LOOKUP_SIDS];
481 ZERO_ARRAY(dom_rids);
483 /* grab the info class and policy handle */
484 lsa_io_q_lookup_rids("", &q_l, data, 0);
486 SMB_ASSERT_ARRAY(q_l.lookup_name, q_l.num_entries);
488 /* convert received RIDs to strings, so we can do them. */
489 for (i = 0; i < q_l.num_entries; i++)
492 fstrcpy(name, unistr2(q_l.lookup_name[i].str.buffer));
494 if (lookup_rid(name, &dom_rids[i], &dom_types[i]))
496 /* WHOOPS! we should really do something about this... */
501 /* construct reply. return status is always 0x0 */
502 lsa_reply_lookup_rids(rdata,
504 dom_rids, /* text-converted SIDs */
505 dom_types); /* SID_NAME_USE types */
508 /***************************************************************************
510 ***************************************************************************/
511 static void api_lsa_close( uint16 vuid, prs_struct *data,
514 /* XXXX this is NOT good */
515 char *q = mem_data(&(rdata->data), rdata->offset);
533 /***************************************************************************
535 ***************************************************************************/
536 static void api_lsa_open_secret( uint16 vuid, prs_struct *data,
539 /* XXXX this is NOT good */
540 char *q = mem_data(&(rdata->data), rdata->offset);
552 SIVAL(q, 0, 0xC0000000 | NT_STATUS_OBJECT_NAME_NOT_FOUND);
558 /***************************************************************************
560 ***************************************************************************/
561 static struct api_struct api_lsa_cmds[] =
563 { "LSA_OPENPOLICY2" , LSA_OPENPOLICY2 , api_lsa_open_policy2 },
564 { "LSA_OPENPOLICY" , LSA_OPENPOLICY , api_lsa_open_policy },
565 { "LSA_QUERYINFOPOLICY" , LSA_QUERYINFOPOLICY , api_lsa_query_info },
566 { "LSA_ENUMTRUSTDOM" , LSA_ENUMTRUSTDOM , api_lsa_enum_trust_dom },
567 { "LSA_CLOSE" , LSA_CLOSE , api_lsa_close },
568 { "LSA_OPENSECRET" , LSA_OPENSECRET , api_lsa_open_secret },
569 { "LSA_LOOKUPSIDS" , LSA_LOOKUPSIDS , api_lsa_lookup_sids },
570 { "LSA_LOOKUPNAMES" , LSA_LOOKUPNAMES , api_lsa_lookup_names },
574 /***************************************************************************
576 ***************************************************************************/
577 BOOL api_ntlsa_rpc(pipes_struct *p, prs_struct *data)
579 return api_rpcTNP(p, "api_ntlsa_rpc", api_lsa_cmds, data);