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 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.
33 extern int DEBUGLEVEL;
36 /****************************************************************************
38 ****************************************************************************/
40 BOOL do_lsa_open_policy(struct cli_state *cli,
41 char *system_name, POLICY_HND *hnd,
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 */
62 init_lsa_sec_qos(&qos, 2, 1, 0, 0x20000000);
63 init_q_open_pol(&q_o, 0x5c, 0, 0, &qos);
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)) {
75 /* send the data on \PIPE\ */
76 if (!rpc_api_pipe_req(cli, LSA_OPENPOLICY, &buf, &rbuf)) {
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"));
90 if (r_o.status != 0) {
91 /* report error code */
92 DEBUG(0,("LSA_OPENPOLICY: %s\n", get_nt_error_msg(r_o.status)));
96 /* ok, at last: we're happy. return the policy handle */
97 memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
105 /****************************************************************************
107 ****************************************************************************/
109 BOOL do_lsa_lookup_sids(struct cli_state *cli,
118 LSA_Q_LOOKUP_SIDS q_l;
119 LSA_R_LOOKUP_SIDS r_l;
121 LSA_TRANS_NAME_ENUM t_names;
123 BOOL valid_response = False;
125 if (hnd == NULL || num_sids == 0 || sids == NULL)
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)) {
145 /* send the data on \PIPE\ */
146 if (!rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &buf, &rbuf)) {
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"));
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)));
168 if (t_names.ptr_trans_names != 0)
169 valid_response = True;
172 if(!valid_response) {
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"));
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++) {
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",
204 (*names)[i] = strdup(full_name);
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)
222 LSA_Q_QUERY_INFO q_q;
223 LSA_R_QUERY_INFO r_q;
226 ZERO_STRUCTP(domain_sid);
229 if (hnd == NULL || domain_name == NULL || domain_sid == NULL)
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)) {
249 /* send the data on \PIPE\ */
250 if (!rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &buf, &rbuf)) {
258 if(!lsa_io_r_query("", &r_q, &rbuf, 0)) {
263 if (r_q.status != 0) {
264 /* report error code */
265 DEBUG(0,("LSA_QUERYINFOPOLICY: %s\n", get_nt_error_msg(r_q.status)));
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));
278 /* ok, at last: we're happy. */
279 switch (r_q.info_class) {
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;
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;
299 DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
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));
315 /****************************************************************************
317 ****************************************************************************/
319 BOOL do_lsa_close(struct cli_state *cli, POLICY_HND *hnd)
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)) {
347 /* send the data on \PIPE\ */
348 if (!rpc_api_pipe_req(cli, LSA_CLOSE, &buf, &rbuf)) {
356 if(!lsa_io_r_close("", &r_c, &rbuf, 0)) {
361 if (r_c.status != 0) {
362 /* report error code */
363 DEBUG(0,("LSA_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
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"));
383 /****************************************************************************
384 obtain a server's SAM SID and save it in the secrets database
385 ****************************************************************************/
387 BOOL cli_lsa_get_domain_sid(struct cli_state *cli, char *server)
392 BOOL res, res2, res3;
394 res = cli_nt_session_open(cli, PIPE_LSARPC);
395 res2 = res ? do_lsa_open_policy(cli, server, &pol, 0) : False;
396 res3 = res2 ? do_lsa_query_info_pol(cli, &pol, 5, domain, &sid) : False;
398 res3 = res3 ? secrets_store_domain_sid(domain, &sid) : False;
400 res2 = res2 ? do_lsa_close(cli, &pol) : False;
401 cli_nt_session_close(cli);