2 Unix SMB/Netbios implementation.
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.
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;
36 extern pstring global_myname;
37 struct ntuser_creds anon;
39 /* Initialise cli_state information */
41 if (!cli_initialise(cli)) {
47 anon.pwd.null_pwd = 1;
51 cli_init_creds(cli, creds);
53 /* Establish a SMB connection */
55 if (!resolve_srv_name(system_name, dest_host, &dest_ip)) {
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)) {
67 /* Open a NT session thingy */
69 if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
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);
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;
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 */
107 init_lsa_sec_qos(&qos, 2, 1, 0, des_access);
108 init_q_open_pol(&q, '\\', 0, des_access, &qos);
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;
121 /* Unmarshall response */
123 if (!lsa_io_r_open_pol("", &r, &rbuf, 0)) {
124 result = NT_STATUS_UNSUCCESSFUL;
128 /* Return output parameters */
130 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
141 /* Close a LSA policy handle */
143 uint32 cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
146 prs_struct qbuf, rbuf;
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;
169 /* Unmarshall response */
171 if (!lsa_io_r_close("", &r, &rbuf, 0)) {
172 result = NT_STATUS_UNSUCCESSFUL;
176 /* Return output parameters */
178 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
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;
199 LSA_TRANS_NAME_ENUM t_names;
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;
221 /* Unmarshall response */
224 ZERO_STRUCT(t_names);
229 if (!lsa_io_r_lookup_sids("", &r, &rbuf, 0)) {
230 result = NT_STATUS_UNSUCCESSFUL;
236 if (result != NT_STATUS_NOPROBLEMO && result != 0x00000107 &&
237 result != (0xC0000000 | NT_STATUS_NONE_MAPPED)) {
239 /* An actual error occured */
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;
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;
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],
277 slprintf(full_name, sizeof(full_name) - 1,
278 "%s%s%s", dom_name, dom_name[0] ?
281 (*names)[i] = talloc_strdup(mem_ctx, full_name);
282 (*types)[i] = t_names.name[i].sid_name_use;
285 (*types)[i] = SID_NAME_UNKNOWN;
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;
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;
327 /* Unmarshall response */
332 if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
333 result = NT_STATUS_UNSUCCESSFUL;
339 if (result != NT_STATUS_NOPROBLEMO &&
340 result != (0xC0000000 | NT_STATUS_NONE_MAPPED)) {
342 /* An actual error occured */
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) *
355 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
356 result = NT_STATUS_UNSUCCESSFUL;
360 if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
362 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
363 result = NT_STATUS_UNSUCCESSFUL;
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;
386 (*types)[i] = SID_NAME_UNKNOWN;
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;
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;
426 /* Unmarshall response */
428 if (!lsa_io_r_query("", &r, &rbuf, 0)) {
429 result = NT_STATUS_UNSUCCESSFUL;
433 if ((result = r.status) != NT_STATUS_NOPROBLEMO) {
437 /* Return output parameters */
439 ZERO_STRUCTP(domain_sid);
440 domain_name[0] = '\0';
442 switch (info_class) {
445 if (r.dom.id3.buffer_dom_name != 0) {
446 unistr2_to_ascii(domain_name,
449 sizeof (fstring) - 1);
452 if (r.dom.id3.buffer_dom_sid != 0) {
453 *domain_sid = r.dom.id3.dom_sid.sid;
460 if (r.dom.id5.buffer_dom_name != 0) {
461 unistr2_to_ascii(domain_name, &r.dom.id5.
463 sizeof (fstring) - 1);
466 if (r.dom.id5.buffer_dom_sid != 0) {
467 *domain_sid = r.dom.id5.dom_sid.sid;
473 DEBUG(3, ("unknown info class %d\n", info_class));
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;
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;
515 /* Unmarshall response */
517 if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
518 result = NT_STATUS_UNSUCCESSFUL;
524 /* For some undocumented reason this function sometimes returns
525 0x8000001a (NT_STATUS_UNABLE_TO_FREE_VM) so we ignore it and
526 pretend everything is OK. */
528 if (result != NT_STATUS_NOPROBLEMO &&
529 result != NT_STATUS_UNABLE_TO_FREE_VM) {
531 /* An actual error ocured */
536 result = NT_STATUS_NOPROBLEMO;
538 /* Return output parameters */
542 /* Allocate memory for trusted domain names and sids */
544 *domain_names = (char **)talloc(mem_ctx, sizeof(char *) *
547 if (!*domain_names) {
548 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
549 result = NT_STATUS_UNSUCCESSFUL;
553 *domain_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
556 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
557 result = NT_STATUS_UNSUCCESSFUL;
561 /* Copy across names and sids */
563 for (i = 0; i < r.num_domains; i++) {
566 unistr2_to_ascii(tmp, &r.uni_domain_name[i],
568 (*domain_names)[i] = strdup(tmp);
569 sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
573 *num_domains = r.num_domains;
574 *enum_ctx = r.enum_context;