s3:lib/netapi: make use of cli_state_remote_name()
[Samba/gebeck_regimport.git] / source3 / lib / netapi / cm.c
blob251e98c2c251acd308a9af8db31207d09e55b8cc
1 /*
2 * Unix SMB/CIFS implementation.
3 * NetApi Support
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/>.
20 #include "includes.h"
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 const char *remote_name = cli_state_remote_name(p->cli);
53 if (strequal(remote_name, server_name)) {
54 return p;
58 return NULL;
61 /********************************************************************
62 ********************************************************************/
64 static WERROR libnetapi_open_ipc_connection(struct libnetapi_ctx *ctx,
65 const char *server_name,
66 struct client_ipc_connection **pp)
68 struct libnetapi_private_ctx *priv_ctx;
69 struct user_auth_info *auth_info = NULL;
70 struct cli_state *cli_ipc = NULL;
71 struct client_ipc_connection *p;
72 NTSTATUS status;
74 if (!ctx || !pp || !server_name) {
75 return WERR_INVALID_PARAM;
78 priv_ctx = (struct libnetapi_private_ctx *)ctx->private_data;
80 p = ipc_cm_find(priv_ctx, server_name);
81 if (p) {
82 *pp = p;
83 return WERR_OK;
86 auth_info = user_auth_info_init(ctx);
87 if (!auth_info) {
88 return WERR_NOMEM;
90 auth_info->signing_state = Undefined;
91 set_cmdline_auth_info_use_kerberos(auth_info, ctx->use_kerberos);
92 set_cmdline_auth_info_username(auth_info, ctx->username);
93 if (ctx->password) {
94 set_cmdline_auth_info_password(auth_info, ctx->password);
95 } else {
96 set_cmdline_auth_info_getpass(auth_info);
99 if (ctx->username && ctx->username[0] &&
100 ctx->password && ctx->password[0] &&
101 ctx->use_kerberos) {
102 set_cmdline_auth_info_fallback_after_kerberos(auth_info, true);
105 if (ctx->use_ccache) {
106 set_cmdline_auth_info_use_ccache(auth_info, true);
109 status = cli_cm_open(ctx, NULL,
110 server_name, "IPC$",
111 auth_info,
112 false, false,
113 PROTOCOL_NT1,
114 0, 0x20, &cli_ipc);
115 if (NT_STATUS_IS_OK(status)) {
116 cli_set_username(cli_ipc, ctx->username);
117 cli_set_password(cli_ipc, ctx->password);
118 cli_set_domain(cli_ipc, ctx->workgroup);
119 } else {
120 cli_ipc = NULL;
122 TALLOC_FREE(auth_info);
124 if (!cli_ipc) {
125 libnetapi_set_error_string(ctx,
126 "Failed to connect to IPC$ share on %s", server_name);
127 return WERR_CAN_NOT_COMPLETE;
130 p = talloc_zero(ctx, struct client_ipc_connection);
131 if (p == NULL) {
132 return WERR_NOMEM;
135 p->cli = cli_ipc;
136 DLIST_ADD(priv_ctx->ipc_connections, p);
138 *pp = p;
140 return WERR_OK;
143 /********************************************************************
144 ********************************************************************/
146 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
148 struct libnetapi_private_ctx *priv_ctx =
149 (struct libnetapi_private_ctx *)ctx->private_data;
150 struct client_ipc_connection *p;
152 for (p = priv_ctx->ipc_connections; p; p = p->next) {
153 cli_shutdown(p->cli);
156 return WERR_OK;
159 /********************************************************************
160 ********************************************************************/
162 static NTSTATUS pipe_cm_find(struct client_ipc_connection *ipc,
163 const struct ndr_syntax_id *interface,
164 struct rpc_pipe_client **presult)
166 struct client_pipe_connection *p;
168 for (p = ipc->pipe_connections; p; p = p->next) {
169 const char *ipc_remote_name;
171 if (!rpc_pipe_np_smb_conn(p->pipe)) {
172 return NT_STATUS_PIPE_EMPTY;
175 ipc_remote_name = cli_state_remote_name(ipc->cli);
177 if (strequal(ipc_remote_name, p->pipe->desthost)
178 && ndr_syntax_id_equal(&p->pipe->abstract_syntax,
179 interface)) {
180 *presult = p->pipe;
181 return NT_STATUS_OK;
185 return NT_STATUS_PIPE_NOT_AVAILABLE;
188 /********************************************************************
189 ********************************************************************/
191 static NTSTATUS pipe_cm_connect(TALLOC_CTX *mem_ctx,
192 struct client_ipc_connection *ipc,
193 const struct ndr_syntax_id *interface,
194 struct rpc_pipe_client **presult)
196 struct client_pipe_connection *p;
197 NTSTATUS status;
199 p = talloc_zero_array(mem_ctx, struct client_pipe_connection, 1);
200 if (!p) {
201 return NT_STATUS_NO_MEMORY;
204 status = cli_rpc_pipe_open_noauth(ipc->cli, interface, &p->pipe);
205 if (!NT_STATUS_IS_OK(status)) {
206 TALLOC_FREE(p);
207 return status;
210 DLIST_ADD(ipc->pipe_connections, p);
212 *presult = p->pipe;
213 return NT_STATUS_OK;
216 /********************************************************************
217 ********************************************************************/
219 static NTSTATUS pipe_cm_open(TALLOC_CTX *ctx,
220 struct client_ipc_connection *ipc,
221 const struct ndr_syntax_id *interface,
222 struct rpc_pipe_client **presult)
224 if (NT_STATUS_IS_OK(pipe_cm_find(ipc, interface, presult))) {
225 return NT_STATUS_OK;
228 return pipe_cm_connect(ctx, ipc, interface, presult);
231 /********************************************************************
232 ********************************************************************/
234 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
235 const char *server_name,
236 const struct ndr_syntax_id *interface,
237 struct rpc_pipe_client **presult)
239 struct rpc_pipe_client *result = NULL;
240 NTSTATUS status;
241 WERROR werr;
242 struct client_ipc_connection *ipc = NULL;
244 if (!presult) {
245 return WERR_INVALID_PARAM;
248 werr = libnetapi_open_ipc_connection(ctx, server_name, &ipc);
249 if (!W_ERROR_IS_OK(werr)) {
250 return werr;
253 status = pipe_cm_open(ctx, ipc, interface, &result);
254 if (!NT_STATUS_IS_OK(status)) {
255 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
256 get_pipe_name_from_syntax(talloc_tos(), interface),
257 get_friendly_nt_error_msg(status));
258 return WERR_DEST_NOT_FOUND;
261 *presult = result;
263 return WERR_OK;
266 /********************************************************************
267 ********************************************************************/
269 WERROR libnetapi_get_binding_handle(struct libnetapi_ctx *ctx,
270 const char *server_name,
271 const struct ndr_syntax_id *interface,
272 struct dcerpc_binding_handle **binding_handle)
274 struct rpc_pipe_client *pipe_cli;
275 WERROR result;
277 *binding_handle = NULL;
279 result = libnetapi_open_pipe(ctx, server_name, interface, &pipe_cli);
280 if (!W_ERROR_IS_OK(result)) {
281 return result;
284 *binding_handle = pipe_cli->binding_handle;
286 return WERR_OK;