dnscrypto-proxy: Support files updated.
[tomato.git] / release / src / router / samba / source / rpc_client / cli_lsarpc.c
blobcfd562785b34650a59c2753841827b10e1798bf1
2 /*
3 * Unix SMB/Netbios implementation.
4 * Version 1.9.
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 1999.
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.
27 #ifdef SYSLOG
28 #undef SYSLOG
29 #endif
31 #include "includes.h"
33 extern int DEBUGLEVEL;
36 /****************************************************************************
37 do a LSA Open Policy
38 ****************************************************************************/
40 BOOL do_lsa_open_policy(struct cli_state *cli,
41 char *server_name, POLICY_HND *hnd,
42 BOOL sec_qos)
44 prs_struct rbuf;
45 prs_struct buf;
46 LSA_Q_OPEN_POL q_o;
47 LSA_SEC_QOS qos;
48 LSA_R_OPEN_POL r_o;
50 if (hnd == NULL)
51 return False;
53 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
54 prs_init(&rbuf, 0, 4, UNMARSHALL );
56 /* create and send a MSRPC command with api LSA_OPENPOLICY */
58 DEBUG(4,("LSA Open Policy\n"));
60 /* store the parameters */
61 if (sec_qos) {
62 init_lsa_sec_qos(&qos, 2, 1, 0, 0x20000000);
63 init_q_open_pol(&q_o, 0x5c, 0, 0, &qos);
64 } else {
65 init_q_open_pol(&q_o, 0x5c, 0, 0x1, NULL);
68 /* turn parameters into data stream */
69 if(!lsa_io_q_open_pol("", &q_o, &buf, 0)) {
70 prs_mem_free(&buf);
71 prs_mem_free(&rbuf);
72 return False;
75 /* send the data on \PIPE\ */
76 if (!rpc_api_pipe_req(cli, LSA_OPENPOLICY, &buf, &rbuf)) {
77 prs_mem_free(&buf);
78 prs_mem_free(&rbuf);
79 return False;
82 prs_mem_free(&buf);
84 if(!lsa_io_r_open_pol("", &r_o, &rbuf, 0)) {
85 DEBUG(0,("do_lsa_open_policy: Failed to unmarshall LSA_R_OPEN_POL\n"));
86 prs_mem_free(&rbuf);
87 return False;
90 if (r_o.status != 0) {
91 /* report error code */
92 DEBUG(0,("LSA_OPENPOLICY: %s\n", get_nt_error_msg(r_o.status)));
93 prs_mem_free(&rbuf);
94 return False;
95 } else {
96 /* ok, at last: we're happy. return the policy handle */
97 memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
100 prs_mem_free(&rbuf);
102 return True;
105 /****************************************************************************
106 do a LSA Lookup SIDs
107 ****************************************************************************/
109 BOOL do_lsa_lookup_sids(struct cli_state *cli,
110 POLICY_HND *hnd,
111 int num_sids,
112 DOM_SID **sids,
113 char ***names,
114 int *num_names)
116 prs_struct rbuf;
117 prs_struct buf;
118 LSA_Q_LOOKUP_SIDS q_l;
119 LSA_R_LOOKUP_SIDS r_l;
120 DOM_R_REF ref;
121 LSA_TRANS_NAME_ENUM t_names;
122 int i;
123 BOOL valid_response = False;
125 if (hnd == NULL || num_sids == 0 || sids == NULL)
126 return False;
128 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
129 prs_init(&rbuf, 0, 4, UNMARSHALL );
131 /* create and send a MSRPC command with api LSA_LOOKUP_SIDS */
133 DEBUG(4,("LSA Lookup SIDs\n"));
135 /* store the parameters */
136 init_q_lookup_sids(&q_l, hnd, num_sids, sids, 1);
138 /* turn parameters into data stream */
139 if(!lsa_io_q_lookup_sids("", &q_l, &buf, 0)) {
140 prs_mem_free(&buf);
141 prs_mem_free(&rbuf);
142 return False;
145 /* send the data on \PIPE\ */
146 if (!rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &buf, &rbuf)) {
147 prs_mem_free(&buf);
148 prs_mem_free(&rbuf);
149 return False;
152 prs_mem_free(&buf);
154 r_l.dom_ref = &ref;
155 r_l.names = &t_names;
157 if(!lsa_io_r_lookup_sids("", &r_l, &rbuf, 0)) {
158 DEBUG(0,("do_lsa_lookup_sids: Failed to unmarshall LSA_R_LOOKUP_SIDS\n"));
159 prs_mem_free(&rbuf);
160 return False;
164 if (r_l.status != 0) {
165 /* report error code */
166 DEBUG(0,("LSA_LOOKUP_SIDS: %s\n", get_nt_error_msg(r_l.status)));
167 } else {
168 if (t_names.ptr_trans_names != 0)
169 valid_response = True;
172 if(!valid_response) {
173 prs_mem_free(&rbuf);
174 return False;
177 if (num_names != NULL)
178 (*num_names) = t_names.num_entries;
180 for (i = 0; i < t_names.num_entries; i++) {
181 if (t_names.name[i].domain_idx >= ref.num_ref_doms_1) {
182 DEBUG(0,("LSA_LOOKUP_SIDS: domain index out of bounds\n"));
183 prs_mem_free(&rbuf);
184 return False;
188 if (names != NULL && t_names.num_entries != 0)
189 (*names) = (char**)malloc((*num_names) * sizeof(char*));
191 if (names != NULL && (*names) != NULL) {
192 /* take each name, construct a \DOMAIN\name string */
193 for (i = 0; i < (*num_names); i++) {
194 fstring name;
195 fstring dom_name;
196 fstring full_name;
197 uint32 dom_idx = t_names.name[i].domain_idx;
198 fstrcpy(dom_name, dos_unistr2(ref.ref_dom[dom_idx].uni_dom_name.buffer));
199 fstrcpy(name, dos_unistr2(t_names.uni_name[i].buffer));
201 slprintf(full_name, sizeof(full_name)-1, "\\%s\\%s",
202 dom_name, name);
204 (*names)[i] = strdup(full_name);
208 prs_mem_free(&rbuf);
210 return valid_response;
213 /****************************************************************************
214 do a LSA Query Info Policy
215 ****************************************************************************/
216 BOOL do_lsa_query_info_pol(struct cli_state *cli,
217 POLICY_HND *hnd, uint16 info_class,
218 fstring domain_name, DOM_SID *domain_sid)
220 prs_struct rbuf;
221 prs_struct buf;
222 LSA_Q_QUERY_INFO q_q;
223 LSA_R_QUERY_INFO r_q;
224 fstring sid_str;
226 ZERO_STRUCTP(domain_sid);
227 domain_name[0] = 0;
229 if (hnd == NULL || domain_name == NULL || domain_sid == NULL)
230 return False;
232 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
233 prs_init(&rbuf, 0, 4, UNMARSHALL );
235 /* create and send a MSRPC command with api LSA_QUERYINFOPOLICY */
237 DEBUG(4,("LSA Query Info Policy\n"));
239 /* store the parameters */
240 init_q_query(&q_q, hnd, info_class);
242 /* turn parameters into data stream */
243 if(!lsa_io_q_query("", &q_q, &buf, 0)) {
244 prs_mem_free(&buf);
245 prs_mem_free(&rbuf);
246 return False;
249 /* send the data on \PIPE\ */
250 if (!rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &buf, &rbuf)) {
251 prs_mem_free(&buf);
252 prs_mem_free(&rbuf);
253 return False;
256 prs_mem_free(&buf);
258 if(!lsa_io_r_query("", &r_q, &rbuf, 0)) {
259 prs_mem_free(&rbuf);
260 return False;
263 if (r_q.status != 0) {
264 /* report error code */
265 DEBUG(0,("LSA_QUERYINFOPOLICY: %s\n", get_nt_error_msg(r_q.status)));
266 prs_mem_free(&rbuf);
267 return False;
270 if (r_q.info_class != q_q.info_class) {
271 /* report different info classes */
272 DEBUG(0,("LSA_QUERYINFOPOLICY: error info_class (q,r) differ - (%x,%x)\n",
273 q_q.info_class, r_q.info_class));
274 prs_mem_free(&rbuf);
275 return False;
278 /* ok, at last: we're happy. */
279 switch (r_q.info_class) {
280 case 3:
281 if (r_q.dom.id3.buffer_dom_name != 0) {
282 char *dom_name = dos_unistrn2(r_q.dom.id3.uni_domain_name.buffer,
283 r_q.dom.id3.uni_domain_name.uni_str_len);
284 fstrcpy(domain_name, dom_name);
286 if (r_q.dom.id3.buffer_dom_sid != 0)
287 *domain_sid = r_q.dom.id3.dom_sid.sid;
288 break;
289 case 5:
290 if (r_q.dom.id5.buffer_dom_name != 0) {
291 char *dom_name = dos_unistrn2(r_q.dom.id5.uni_domain_name.buffer,
292 r_q.dom.id5.uni_domain_name.uni_str_len);
293 fstrcpy(domain_name, dom_name);
295 if (r_q.dom.id5.buffer_dom_sid != 0)
296 *domain_sid = r_q.dom.id5.dom_sid.sid;
297 break;
298 default:
299 DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
300 domain_name[0] = 0;
302 prs_mem_free(&rbuf);
303 return False;
306 sid_to_string(sid_str, domain_sid);
307 DEBUG(3,("LSA_QUERYINFOPOLICY (level %x): domain:%s domain sid:%s\n",
308 r_q.info_class, domain_name, sid_str));
310 prs_mem_free(&rbuf);
312 return True;
315 /****************************************************************************
316 do a LSA Close
317 ****************************************************************************/
319 BOOL do_lsa_close(struct cli_state *cli, POLICY_HND *hnd)
321 prs_struct rbuf;
322 prs_struct buf;
323 LSA_Q_CLOSE q_c;
324 LSA_R_CLOSE r_c;
325 int i;
327 if (hnd == NULL)
328 return False;
330 /* create and send a MSRPC command with api LSA_OPENPOLICY */
332 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
333 prs_init(&rbuf, 0, 4, UNMARSHALL );
335 DEBUG(4,("LSA Close\n"));
337 /* store the parameters */
338 init_lsa_q_close(&q_c, hnd);
340 /* turn parameters into data stream */
341 if(!lsa_io_q_close("", &q_c, &buf, 0)) {
342 prs_mem_free(&buf);
343 prs_mem_free(&rbuf);
344 return False;
347 /* send the data on \PIPE\ */
348 if (!rpc_api_pipe_req(cli, LSA_CLOSE, &buf, &rbuf)) {
349 prs_mem_free(&buf);
350 prs_mem_free(&rbuf);
351 return False;
354 prs_mem_free(&buf);
356 if(!lsa_io_r_close("", &r_c, &rbuf, 0)) {
357 prs_mem_free(&rbuf);
358 return False;
361 if (r_c.status != 0) {
362 /* report error code */
363 DEBUG(0,("LSA_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
364 prs_mem_free(&rbuf);
365 return False;
368 /* check that the returned policy handle is all zeros */
370 for (i = 0; i < sizeof(r_c.pol.data); i++) {
371 if (r_c.pol.data[i] != 0) {
372 DEBUG(0,("LSA_CLOSE: non-zero handle returned\n"));
373 prs_mem_free(&rbuf);
374 return False;
378 prs_mem_free(&rbuf);
380 return True;