2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "lib/netapi/netapi.h"
23 #include "lib/netapi/netapi_private.h"
25 /********************************************************************
26 ********************************************************************/
28 static WERROR libnetapi_open_ipc_connection(struct libnetapi_ctx *ctx,
29 const char *server_name,
30 struct cli_state **cli)
32 struct user_auth_info *auth_info = NULL;
33 struct cli_state *cli_ipc = NULL;
35 if (!ctx || !cli || !server_name) {
36 return WERR_INVALID_PARAM;
39 auth_info = user_auth_info_init(NULL);
43 auth_info->signing_state = Undefined;
44 set_cmdline_auth_info_use_kerberos(auth_info, ctx->use_kerberos);
45 set_cmdline_auth_info_username(auth_info, ctx->username);
47 set_cmdline_auth_info_password(auth_info, ctx->password);
49 set_cmdline_auth_info_getpass(auth_info);
52 if (ctx->username && ctx->username[0] &&
53 ctx->password && ctx->password[0] &&
55 set_cmdline_auth_info_fallback_after_kerberos(auth_info, true);
58 if (ctx->use_ccache) {
59 set_cmdline_auth_info_use_ccache(auth_info, true);
62 cli_ipc = cli_cm_open(ctx, NULL,
69 cli_set_username(cli_ipc, ctx->username);
70 cli_set_password(cli_ipc, ctx->password);
71 cli_set_domain(cli_ipc, ctx->workgroup);
73 TALLOC_FREE(auth_info);
76 libnetapi_set_error_string(ctx,
77 "Failed to connect to IPC$ share on %s", server_name);
78 return WERR_CAN_NOT_COMPLETE;
86 /********************************************************************
87 ********************************************************************/
89 struct client_pipe_connection {
90 struct client_pipe_connection *prev, *next;
91 struct rpc_pipe_client *pipe;
92 struct cli_state *cli;
95 static struct client_pipe_connection *pipe_connections;
97 /********************************************************************
98 ********************************************************************/
100 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
102 struct client_pipe_connection *p;
104 for (p = pipe_connections; p; p = p->next) {
105 cli_shutdown(p->cli);
111 /********************************************************************
112 ********************************************************************/
114 static NTSTATUS pipe_cm_find(struct cli_state *cli,
115 const struct ndr_syntax_id *interface,
116 struct rpc_pipe_client **presult)
118 struct client_pipe_connection *p;
120 for (p = pipe_connections; p; p = p->next) {
122 if (!rpc_pipe_np_smb_conn(p->pipe)) {
123 return NT_STATUS_PIPE_EMPTY;
126 if (strequal(cli->desthost, p->pipe->desthost)
127 && ndr_syntax_id_equal(&p->pipe->abstract_syntax,
134 return NT_STATUS_PIPE_NOT_AVAILABLE;
137 /********************************************************************
138 ********************************************************************/
140 static NTSTATUS pipe_cm_connect(TALLOC_CTX *mem_ctx,
141 struct cli_state *cli,
142 const struct ndr_syntax_id *interface,
143 struct rpc_pipe_client **presult)
145 struct client_pipe_connection *p;
148 p = TALLOC_ZERO_ARRAY(mem_ctx, struct client_pipe_connection, 1);
150 return NT_STATUS_NO_MEMORY;
153 status = cli_rpc_pipe_open_noauth(cli, interface, &p->pipe);
154 if (!NT_STATUS_IS_OK(status)) {
160 DLIST_ADD(pipe_connections, p);
166 /********************************************************************
167 ********************************************************************/
169 static NTSTATUS pipe_cm_open(TALLOC_CTX *ctx,
170 struct cli_state *cli,
171 const struct ndr_syntax_id *interface,
172 struct rpc_pipe_client **presult)
174 if (NT_STATUS_IS_OK(pipe_cm_find(cli, interface, presult))) {
178 return pipe_cm_connect(ctx, cli, interface, presult);
181 /********************************************************************
182 ********************************************************************/
184 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
185 const char *server_name,
186 const struct ndr_syntax_id *interface,
187 struct rpc_pipe_client **presult)
189 struct rpc_pipe_client *result = NULL;
192 struct cli_state *cli = NULL;
195 return WERR_INVALID_PARAM;
198 werr = libnetapi_open_ipc_connection(ctx, server_name, &cli);
199 if (!W_ERROR_IS_OK(werr)) {
203 status = pipe_cm_open(ctx, cli, interface, &result);
204 if (!NT_STATUS_IS_OK(status)) {
205 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
206 get_pipe_name_from_syntax(talloc_tos(), interface),
207 get_friendly_nt_error_msg(status));
208 return WERR_DEST_NOT_FOUND;