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/>.
21 #include "popt_common.h"
23 #include "lib/netapi/netapi.h"
24 #include "lib/netapi/netapi_private.h"
25 #include "libsmb/libsmb.h"
26 #include "rpc_client/cli_pipe.h"
28 /********************************************************************
29 ********************************************************************/
31 struct client_ipc_connection {
32 struct client_ipc_connection *prev, *next;
33 struct cli_state *cli;
34 struct client_pipe_connection *pipe_connections;
37 struct client_pipe_connection {
38 struct client_pipe_connection *prev, *next;
39 struct rpc_pipe_client *pipe;
42 /********************************************************************
43 ********************************************************************/
45 static struct client_ipc_connection *ipc_cm_find(
46 struct libnetapi_private_ctx *priv_ctx, const char *server_name)
48 struct client_ipc_connection *p;
50 for (p = priv_ctx->ipc_connections; p; p = p->next) {
51 if (strequal(p->cli->desthost, server_name)) {
59 /********************************************************************
60 ********************************************************************/
62 static WERROR libnetapi_open_ipc_connection(struct libnetapi_ctx *ctx,
63 const char *server_name,
64 struct client_ipc_connection **pp)
66 struct libnetapi_private_ctx *priv_ctx;
67 struct user_auth_info *auth_info = NULL;
68 struct cli_state *cli_ipc = NULL;
69 struct client_ipc_connection *p;
71 if (!ctx || !pp || !server_name) {
72 return WERR_INVALID_PARAM;
75 priv_ctx = (struct libnetapi_private_ctx *)ctx->private_data;
77 p = ipc_cm_find(priv_ctx, server_name);
83 auth_info = user_auth_info_init(ctx);
87 auth_info->signing_state = Undefined;
88 set_cmdline_auth_info_use_kerberos(auth_info, ctx->use_kerberos);
89 set_cmdline_auth_info_username(auth_info, ctx->username);
91 set_cmdline_auth_info_password(auth_info, ctx->password);
93 set_cmdline_auth_info_getpass(auth_info);
96 if (ctx->username && ctx->username[0] &&
97 ctx->password && ctx->password[0] &&
99 set_cmdline_auth_info_fallback_after_kerberos(auth_info, true);
102 if (ctx->use_ccache) {
103 set_cmdline_auth_info_use_ccache(auth_info, true);
106 cli_ipc = cli_cm_open(ctx, NULL,
113 cli_set_username(cli_ipc, ctx->username);
114 cli_set_password(cli_ipc, ctx->password);
115 cli_set_domain(cli_ipc, ctx->workgroup);
117 TALLOC_FREE(auth_info);
120 libnetapi_set_error_string(ctx,
121 "Failed to connect to IPC$ share on %s", server_name);
122 return WERR_CAN_NOT_COMPLETE;
125 p = talloc_zero(ctx, struct client_ipc_connection);
131 DLIST_ADD(priv_ctx->ipc_connections, p);
138 /********************************************************************
139 ********************************************************************/
141 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
143 struct libnetapi_private_ctx *priv_ctx =
144 (struct libnetapi_private_ctx *)ctx->private_data;
145 struct client_ipc_connection *p;
147 for (p = priv_ctx->ipc_connections; p; p = p->next) {
148 cli_shutdown(p->cli);
154 /********************************************************************
155 ********************************************************************/
157 static NTSTATUS pipe_cm_find(struct client_ipc_connection *ipc,
158 const struct ndr_syntax_id *interface,
159 struct rpc_pipe_client **presult)
161 struct client_pipe_connection *p;
163 for (p = ipc->pipe_connections; p; p = p->next) {
165 if (!rpc_pipe_np_smb_conn(p->pipe)) {
166 return NT_STATUS_PIPE_EMPTY;
169 if (strequal(ipc->cli->desthost, p->pipe->desthost)
170 && ndr_syntax_id_equal(&p->pipe->abstract_syntax,
177 return NT_STATUS_PIPE_NOT_AVAILABLE;
180 /********************************************************************
181 ********************************************************************/
183 static NTSTATUS pipe_cm_connect(TALLOC_CTX *mem_ctx,
184 struct client_ipc_connection *ipc,
185 const struct ndr_syntax_id *interface,
186 struct rpc_pipe_client **presult)
188 struct client_pipe_connection *p;
191 p = talloc_zero_array(mem_ctx, struct client_pipe_connection, 1);
193 return NT_STATUS_NO_MEMORY;
196 status = cli_rpc_pipe_open_noauth(ipc->cli, interface, &p->pipe);
197 if (!NT_STATUS_IS_OK(status)) {
202 DLIST_ADD(ipc->pipe_connections, p);
208 /********************************************************************
209 ********************************************************************/
211 static NTSTATUS pipe_cm_open(TALLOC_CTX *ctx,
212 struct client_ipc_connection *ipc,
213 const struct ndr_syntax_id *interface,
214 struct rpc_pipe_client **presult)
216 if (NT_STATUS_IS_OK(pipe_cm_find(ipc, interface, presult))) {
220 return pipe_cm_connect(ctx, ipc, interface, presult);
223 /********************************************************************
224 ********************************************************************/
226 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
227 const char *server_name,
228 const struct ndr_syntax_id *interface,
229 struct rpc_pipe_client **presult)
231 struct rpc_pipe_client *result = NULL;
234 struct client_ipc_connection *ipc = NULL;
237 return WERR_INVALID_PARAM;
240 werr = libnetapi_open_ipc_connection(ctx, server_name, &ipc);
241 if (!W_ERROR_IS_OK(werr)) {
245 status = pipe_cm_open(ctx, ipc, interface, &result);
246 if (!NT_STATUS_IS_OK(status)) {
247 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
248 get_pipe_name_from_syntax(talloc_tos(), interface),
249 get_friendly_nt_error_msg(status));
250 return WERR_DEST_NOT_FOUND;
258 /********************************************************************
259 ********************************************************************/
261 WERROR libnetapi_get_binding_handle(struct libnetapi_ctx *ctx,
262 const char *server_name,
263 const struct ndr_syntax_id *interface,
264 struct dcerpc_binding_handle **binding_handle)
266 struct rpc_pipe_client *pipe_cli;
269 *binding_handle = NULL;
271 result = libnetapi_open_pipe(ctx, server_name, interface, &pipe_cli);
272 if (!W_ERROR_IS_OK(result)) {
276 *binding_handle = pipe_cli->binding_handle;