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) {
272 rpcstr_pull_unistr2_fstring(
273 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
274 rpcstr_pull_unistr2_fstring(
275 name, &t_names.uni_name[i]);
277 slprintf(full_name, sizeof(full_name) - 1,
279 (dom_name[0] && name[0]) ?
280 lp_winbind_separator() : "", name);
282 (*names)[i] = talloc_strdup(mem_ctx, full_name);
283 (*types)[i] = t_names.name[i].sid_name_use;
287 (*types)[i] = SID_NAME_UNKNOWN;
298 /* Lookup a list of names */
300 uint32 cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
301 POLICY_HND *pol, int num_names, char **names,
302 DOM_SID **sids, uint32 **types, int *num_sids)
304 prs_struct qbuf, rbuf;
305 LSA_Q_LOOKUP_NAMES q;
306 LSA_R_LOOKUP_NAMES r;
314 /* Initialise parse structures */
316 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
317 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
319 /* Marshall data and send request */
321 init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
323 if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
324 !rpc_api_pipe_req(cli, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
325 result = NT_STATUS_UNSUCCESSFUL;
329 /* Unmarshall response */
334 if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
335 result = NT_STATUS_UNSUCCESSFUL;
341 if (result != NT_STATUS_NOPROBLEMO &&
342 result != (0xC0000000 | NT_STATUS_NONE_MAPPED)) {
344 /* An actual error occured */
349 result = NT_STATUS_NOPROBLEMO;
351 /* Return output parameters */
353 (*num_sids) = r.num_entries;
355 if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
357 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
358 result = NT_STATUS_UNSUCCESSFUL;
362 if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
364 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
365 result = NT_STATUS_UNSUCCESSFUL;
369 for (i = 0; i < r.num_entries; i++) {
370 DOM_RID2 *t_rids = r.dom_rid;
371 uint32 dom_idx = t_rids[i].rid_idx;
372 uint32 dom_rid = t_rids[i].rid;
373 DOM_SID *sid = &(*sids)[i];
375 /* Translate optimised sid through domain index array */
377 if (dom_idx != 0xffffffff) {
379 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
381 if (dom_rid != 0xffffffff) {
382 sid_append_rid(sid, dom_rid);
385 (*types)[i] = t_rids[i].type;
388 (*types)[i] = SID_NAME_UNKNOWN;
399 /* Query info policy */
401 uint32 cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
402 POLICY_HND *pol, uint16 info_class,
403 fstring domain_name, DOM_SID *domain_sid)
405 prs_struct qbuf, rbuf;
413 /* Initialise parse structures */
415 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
416 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
418 /* Marshall data and send request */
420 init_q_query(&q, pol, info_class);
422 if (!lsa_io_q_query("", &q, &qbuf, 0) ||
423 !rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
424 result = NT_STATUS_UNSUCCESSFUL;
428 /* Unmarshall response */
430 if (!lsa_io_r_query("", &r, &rbuf, 0)) {
431 result = NT_STATUS_UNSUCCESSFUL;
435 if ((result = r.status) != NT_STATUS_NOPROBLEMO) {
439 /* Return output parameters */
441 ZERO_STRUCTP(domain_sid);
442 domain_name[0] = '\0';
444 switch (info_class) {
447 if (r.dom.id3.buffer_dom_name != 0) {
448 unistr2_to_ascii(domain_name,
451 sizeof (fstring) - 1);
454 if (r.dom.id3.buffer_dom_sid != 0) {
455 *domain_sid = r.dom.id3.dom_sid.sid;
462 if (r.dom.id5.buffer_dom_name != 0) {
463 unistr2_to_ascii(domain_name, &r.dom.id5.
465 sizeof (fstring) - 1);
468 if (r.dom.id5.buffer_dom_sid != 0) {
469 *domain_sid = r.dom.id5.dom_sid.sid;
475 DEBUG(3, ("unknown info class %d\n", info_class));
486 /* Enumerate list of trusted domains */
488 uint32 cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
489 POLICY_HND *pol, uint32 *enum_ctx,
490 uint32 *num_domains, char ***domain_names,
491 DOM_SID **domain_sids)
493 prs_struct qbuf, rbuf;
494 LSA_Q_ENUM_TRUST_DOM q;
495 LSA_R_ENUM_TRUST_DOM r;
502 /* Initialise parse structures */
504 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
505 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
507 /* Marshall data and send request */
509 init_q_enum_trust_dom(&q, pol, *enum_ctx, 0xffffffff);
511 if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
512 !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
513 result = NT_STATUS_UNSUCCESSFUL;
517 /* Unmarshall response */
519 if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
520 result = NT_STATUS_UNSUCCESSFUL;
526 /* For some undocumented reason this function sometimes returns
527 0x8000001a (NT_STATUS_UNABLE_TO_FREE_VM) so we ignore it and
528 pretend everything is OK. */
530 if (result != NT_STATUS_NOPROBLEMO &&
531 result != NT_STATUS_UNABLE_TO_FREE_VM) {
533 /* An actual error ocured */
538 result = NT_STATUS_NOPROBLEMO;
540 /* Return output parameters */
544 /* Allocate memory for trusted domain names and sids */
546 *domain_names = (char **)talloc(mem_ctx, sizeof(char *) *
549 if (!*domain_names) {
550 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
551 result = NT_STATUS_UNSUCCESSFUL;
555 *domain_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
558 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
559 result = NT_STATUS_UNSUCCESSFUL;
563 /* Copy across names and sids */
565 for (i = 0; i < r.num_domains; i++) {
568 unistr2_to_ascii(tmp, &r.uni_domain_name[i],
570 (*domain_names)[i] = strdup(tmp);
571 sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
575 *num_domains = r.num_domains;
576 *enum_ctx = r.enum_context;