sync with SAMBA_2_2
[Samba/gbeck.git] / source / libsmb / cli_lsarpc.c
blob00c5ac9a1614c639e7a7640022f1390c91f589b6
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.2
4 RPC pipe client
5 Copyright (C) Tim Potter 2000-2001,
6 Copyright (C) Andrew Tridgell 1992-1997,2000,
7 Copyright (C) Luke Kenneth Casson Leighton 1996-1997,2000,
8 Copyright (C) Paul Ashton 1997,2000,
9 Copyright (C) Elrond 2000.
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.
26 #include "includes.h"
28 /* Opens a SMB connection to the lsa pipe */
30 struct cli_state *cli_lsa_initialise(struct cli_state *cli, char *system_name,
31 struct ntuser_creds *creds)
33 struct in_addr dest_ip;
34 struct nmb_name calling, called;
35 fstring dest_host;
36 extern pstring global_myname;
37 struct ntuser_creds anon;
39 /* Initialise cli_state information */
41 if (!cli_initialise(cli)) {
42 return NULL;
45 if (!creds) {
46 ZERO_STRUCT(anon);
47 anon.pwd.null_pwd = 1;
48 creds = &anon;
51 cli_init_creds(cli, creds);
53 /* Establish a SMB connection */
55 if (!resolve_srv_name(system_name, dest_host, &dest_ip)) {
56 return NULL;
59 make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
60 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
62 if (!cli_establish_connection(cli, dest_host, &dest_ip, &calling,
63 &called, "IPC$", "IPC", False, True)) {
64 return NULL;
67 /* Open a NT session thingy */
69 if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
70 cli_shutdown(cli);
71 return NULL;
74 return cli;
77 /* Shut down a SMB connection to the LSA pipe */
79 void cli_lsa_shutdown(struct cli_state *cli)
81 if (cli->fd != -1) cli_ulogoff(cli);
82 cli_shutdown(cli);
85 /* Open a LSA policy handle */
87 uint32 cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
88 BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
90 prs_struct qbuf, rbuf;
91 LSA_Q_OPEN_POL q;
92 LSA_R_OPEN_POL r;
93 LSA_SEC_QOS qos;
94 uint32 result;
96 ZERO_STRUCT(q);
97 ZERO_STRUCT(r);
99 /* Initialise parse structures */
101 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
102 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
104 /* Initialise input parameters */
106 if (sec_qos) {
107 init_lsa_sec_qos(&qos, 2, 1, 0, des_access);
108 init_q_open_pol(&q, '\\', 0, des_access, &qos);
109 } else {
110 init_q_open_pol(&q, '\\', 0, des_access, NULL);
113 /* Marshall data and send request */
115 if (!lsa_io_q_open_pol("", &q, &qbuf, 0) ||
116 !rpc_api_pipe_req(cli, LSA_OPENPOLICY, &qbuf, &rbuf)) {
117 result = NT_STATUS_UNSUCCESSFUL;
118 goto done;
121 /* Unmarshall response */
123 if (!lsa_io_r_open_pol("", &r, &rbuf, 0)) {
124 result = NT_STATUS_UNSUCCESSFUL;
125 goto done;
128 /* Return output parameters */
130 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
131 *pol = r.pol;
134 done:
135 prs_mem_free(&qbuf);
136 prs_mem_free(&rbuf);
138 return result;
141 /* Close a LSA policy handle */
143 uint32 cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
144 POLICY_HND *pol)
146 prs_struct qbuf, rbuf;
147 LSA_Q_CLOSE q;
148 LSA_R_CLOSE r;
149 uint32 result;
151 ZERO_STRUCT(q);
152 ZERO_STRUCT(r);
154 /* Initialise parse structures */
156 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
157 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
159 /* Marshall data and send request */
161 init_lsa_q_close(&q, pol);
163 if (!lsa_io_q_close("", &q, &qbuf, 0) ||
164 !rpc_api_pipe_req(cli, LSA_CLOSE, &qbuf, &rbuf)) {
165 result = NT_STATUS_UNSUCCESSFUL;
166 goto done;
169 /* Unmarshall response */
171 if (!lsa_io_r_close("", &r, &rbuf, 0)) {
172 result = NT_STATUS_UNSUCCESSFUL;
173 goto done;
176 /* Return output parameters */
178 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
179 *pol = r.pol;
182 done:
183 prs_mem_free(&qbuf);
184 prs_mem_free(&rbuf);
186 return result;
189 /* Lookup a list of sids */
191 uint32 cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
192 POLICY_HND *pol, int num_sids, DOM_SID *sids,
193 char ***names, uint32 **types, int *num_names)
195 prs_struct qbuf, rbuf;
196 LSA_Q_LOOKUP_SIDS q;
197 LSA_R_LOOKUP_SIDS r;
198 DOM_R_REF ref;
199 LSA_TRANS_NAME_ENUM t_names;
200 uint32 result;
201 int i;
203 ZERO_STRUCT(q);
204 ZERO_STRUCT(r);
206 /* Initialise parse structures */
208 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
209 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
211 /* Marshall data and send request */
213 init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
215 if (!lsa_io_q_lookup_sids("", &q, &qbuf, 0) ||
216 !rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &qbuf, &rbuf)) {
217 result = NT_STATUS_UNSUCCESSFUL;
218 goto done;
221 /* Unmarshall response */
223 ZERO_STRUCT(ref);
224 ZERO_STRUCT(t_names);
226 r.dom_ref = &ref;
227 r.names = &t_names;
229 if (!lsa_io_r_lookup_sids("", &r, &rbuf, 0)) {
230 result = NT_STATUS_UNSUCCESSFUL;
231 goto done;
234 result = r.status;
236 if (result != NT_STATUS_NOPROBLEMO && result != 0x00000107 &&
237 result != (0xC0000000 | NT_STATUS_NONE_MAPPED)) {
239 /* An actual error occured */
241 goto done;
244 result = NT_STATUS_NOPROBLEMO;
246 /* Return output parameters */
248 (*num_names) = r.names->num_entries;
250 if (!((*names) = (char **)talloc(mem_ctx, sizeof(char *) *
251 r.names->num_entries))) {
252 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
253 result = NT_STATUS_UNSUCCESSFUL;
254 goto done;
257 if (!((*types) = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
258 r.names->num_entries))) {
259 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
260 result = NT_STATUS_UNSUCCESSFUL;
261 goto done;
264 for (i = 0; i < r.names->num_entries; i++) {
265 fstring name, dom_name, full_name;
266 uint32 dom_idx = t_names.name[i].domain_idx;
268 /* Translate optimised name through domain index array */
270 if (dom_idx != 0xffffffff) {
271 unistr2_to_ascii(dom_name,
272 &ref.ref_dom[dom_idx].uni_dom_name,
273 sizeof(dom_name)- 1);
274 unistr2_to_ascii(name, &t_names.uni_name[i],
275 sizeof(name) - 1);
277 slprintf(full_name, sizeof(full_name) - 1,
278 "%s%s%s", dom_name, dom_name[0] ?
279 "\\" : "", name);
281 (*names)[i] = talloc_strdup(mem_ctx, full_name);
282 (*types)[i] = t_names.name[i].sid_name_use;
283 } else {
284 (*names)[i] = NULL;
285 (*types)[i] = SID_NAME_UNKNOWN;
289 done:
290 prs_mem_free(&qbuf);
291 prs_mem_free(&rbuf);
293 return result;
296 /* Lookup a list of names */
298 uint32 cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
299 POLICY_HND *pol, int num_names, char **names,
300 DOM_SID **sids, uint32 **types, int *num_sids)
302 prs_struct qbuf, rbuf;
303 LSA_Q_LOOKUP_NAMES q;
304 LSA_R_LOOKUP_NAMES r;
305 DOM_R_REF ref;
306 uint32 result;
307 int i;
309 ZERO_STRUCT(q);
310 ZERO_STRUCT(r);
312 /* Initialise parse structures */
314 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
315 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
317 /* Marshall data and send request */
319 init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
321 if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
322 !rpc_api_pipe_req(cli, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
323 result = NT_STATUS_UNSUCCESSFUL;
324 goto done;
327 /* Unmarshall response */
329 ZERO_STRUCT(ref);
330 r.dom_ref = &ref;
332 if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
333 result = NT_STATUS_UNSUCCESSFUL;
334 goto done;
337 result = r.status;
339 if (result != NT_STATUS_NOPROBLEMO &&
340 result != (0xC0000000 | NT_STATUS_NONE_MAPPED)) {
342 /* An actual error occured */
344 goto done;
347 result = NT_STATUS_NOPROBLEMO;
349 /* Return output parameters */
351 (*num_sids) = r.num_entries;
353 if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
354 r.num_entries)))) {
355 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
356 result = NT_STATUS_UNSUCCESSFUL;
357 goto done;
360 if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
361 r.num_entries)))) {
362 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
363 result = NT_STATUS_UNSUCCESSFUL;
364 goto done;
367 for (i = 0; i < r.num_entries; i++) {
368 DOM_RID2 *t_rids = r.dom_rid;
369 uint32 dom_idx = t_rids[i].rid_idx;
370 uint32 dom_rid = t_rids[i].rid;
371 DOM_SID *sid = &(*sids)[i];
373 /* Translate optimised sid through domain index array */
375 if (dom_idx != 0xffffffff) {
377 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
379 if (dom_rid != 0xffffffff) {
380 sid_append_rid(sid, dom_rid);
383 (*types)[i] = t_rids[i].type;
384 } else {
385 ZERO_STRUCTP(sid);
386 (*types)[i] = SID_NAME_UNKNOWN;
390 done:
391 prs_mem_free(&qbuf);
392 prs_mem_free(&rbuf);
394 return result;
397 /* Query info policy */
399 uint32 cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
400 POLICY_HND *pol, uint16 info_class,
401 fstring domain_name, DOM_SID *domain_sid)
403 prs_struct qbuf, rbuf;
404 LSA_Q_QUERY_INFO q;
405 LSA_R_QUERY_INFO r;
406 uint32 result;
408 ZERO_STRUCT(q);
409 ZERO_STRUCT(r);
411 /* Initialise parse structures */
413 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
414 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
416 /* Marshall data and send request */
418 init_q_query(&q, pol, info_class);
420 if (!lsa_io_q_query("", &q, &qbuf, 0) ||
421 !rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
422 result = NT_STATUS_UNSUCCESSFUL;
423 goto done;
426 /* Unmarshall response */
428 if (!lsa_io_r_query("", &r, &rbuf, 0)) {
429 result = NT_STATUS_UNSUCCESSFUL;
430 goto done;
433 if ((result = r.status) != NT_STATUS_NOPROBLEMO) {
434 goto done;
437 /* Return output parameters */
439 ZERO_STRUCTP(domain_sid);
440 domain_name[0] = '\0';
442 switch (info_class) {
444 case 3:
445 if (r.dom.id3.buffer_dom_name != 0) {
446 unistr2_to_ascii(domain_name,
447 &r.dom.id3.
448 uni_domain_name,
449 sizeof (fstring) - 1);
452 if (r.dom.id3.buffer_dom_sid != 0) {
453 *domain_sid = r.dom.id3.dom_sid.sid;
456 break;
458 case 5:
460 if (r.dom.id5.buffer_dom_name != 0) {
461 unistr2_to_ascii(domain_name, &r.dom.id5.
462 uni_domain_name,
463 sizeof (fstring) - 1);
466 if (r.dom.id5.buffer_dom_sid != 0) {
467 *domain_sid = r.dom.id5.dom_sid.sid;
470 break;
472 default:
473 DEBUG(3, ("unknown info class %d\n", info_class));
474 break;
477 done:
478 prs_mem_free(&qbuf);
479 prs_mem_free(&rbuf);
481 return result;
484 /* Enumerate list of trusted domains */
486 uint32 cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
487 POLICY_HND *pol, uint32 *enum_ctx,
488 uint32 *num_domains, char ***domain_names,
489 DOM_SID **domain_sids)
491 prs_struct qbuf, rbuf;
492 LSA_Q_ENUM_TRUST_DOM q;
493 LSA_R_ENUM_TRUST_DOM r;
494 uint32 result;
495 int i;
497 ZERO_STRUCT(q);
498 ZERO_STRUCT(r);
500 /* Initialise parse structures */
502 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
503 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
505 /* Marshall data and send request */
507 init_q_enum_trust_dom(&q, pol, *enum_ctx, 0xffffffff);
509 if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
510 !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
511 result = NT_STATUS_UNSUCCESSFUL;
512 goto done;
515 /* Unmarshall response */
517 if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
518 result = NT_STATUS_UNSUCCESSFUL;
519 goto done;
522 result = r.status;
524 if (result != NT_STATUS_NOPROBLEMO && result != 0x8000001a) {
526 /* An actual error ocured */
528 goto done;
531 result = NT_STATUS_NOPROBLEMO;
533 /* Return output parameters */
535 if (!((*domain_names) = (char **)talloc(mem_ctx, sizeof(char *) *
536 r.num_domains))) {
537 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
538 result = NT_STATUS_UNSUCCESSFUL;
539 goto done;
542 if (!((*domain_sids) = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
543 r.num_domains))) {
544 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
545 result = NT_STATUS_UNSUCCESSFUL;
546 goto done;
549 for (i = 0; i < r.num_domains; i++) {
550 fstring tmp;
552 unistr2_to_ascii(tmp, &r.uni_domain_name[i], sizeof(tmp) - 1);
553 (*domain_names)[i] = strdup(tmp);
554 sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
557 *num_domains = r.num_domains;
558 *enum_ctx = r.enum_context;
560 lsa_free_r_enum_trust_dom(&r);
562 done:
563 prs_mem_free(&qbuf);
564 prs_mem_free(&rbuf);
566 return result;