s3-rpc_client: Move client pipe functions to own header.
[kamenim/samba.git] / source3 / lib / netapi / cm.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi Support
4  *  Copyright (C) Guenther Deschner 2008
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21 #include "popt_common.h"
22
23 #include "lib/netapi/netapi.h"
24 #include "lib/netapi/netapi_private.h"
25 #include "rpc_client/cli_pipe.h"
26
27 /********************************************************************
28 ********************************************************************/
29
30 struct client_ipc_connection {
31         struct client_ipc_connection *prev, *next;
32         struct cli_state *cli;
33         struct client_pipe_connection *pipe_connections;
34 };
35
36 struct client_pipe_connection {
37         struct client_pipe_connection *prev, *next;
38         struct rpc_pipe_client *pipe;
39 };
40
41 /********************************************************************
42 ********************************************************************/
43
44 static struct client_ipc_connection *ipc_cm_find(
45         struct libnetapi_private_ctx *priv_ctx, const char *server_name)
46 {
47         struct client_ipc_connection *p;
48
49         for (p = priv_ctx->ipc_connections; p; p = p->next) {
50                 if (strequal(p->cli->desthost, server_name)) {
51                         return p;
52                 }
53         }
54
55         return NULL;
56 }
57
58 /********************************************************************
59 ********************************************************************/
60
61 static WERROR libnetapi_open_ipc_connection(struct libnetapi_ctx *ctx,
62                                             const char *server_name,
63                                             struct client_ipc_connection **pp)
64 {
65         struct libnetapi_private_ctx *priv_ctx =
66                 (struct libnetapi_private_ctx *)ctx->private_data;
67         struct user_auth_info *auth_info = NULL;
68         struct cli_state *cli_ipc = NULL;
69         struct client_ipc_connection *p;
70
71         if (!ctx || !pp || !server_name) {
72                 return WERR_INVALID_PARAM;
73         }
74
75         p = ipc_cm_find(priv_ctx, server_name);
76         if (p) {
77                 *pp = p;
78                 return WERR_OK;
79         }
80
81         auth_info = user_auth_info_init(ctx);
82         if (!auth_info) {
83                 return WERR_NOMEM;
84         }
85         auth_info->signing_state = Undefined;
86         set_cmdline_auth_info_use_kerberos(auth_info, ctx->use_kerberos);
87         set_cmdline_auth_info_username(auth_info, ctx->username);
88         if (ctx->password) {
89                 set_cmdline_auth_info_password(auth_info, ctx->password);
90         } else {
91                 set_cmdline_auth_info_getpass(auth_info);
92         }
93
94         if (ctx->username && ctx->username[0] &&
95             ctx->password && ctx->password[0] &&
96             ctx->use_kerberos) {
97                 set_cmdline_auth_info_fallback_after_kerberos(auth_info, true);
98         }
99
100         if (ctx->use_ccache) {
101                 set_cmdline_auth_info_use_ccache(auth_info, true);
102         }
103
104         cli_ipc = cli_cm_open(ctx, NULL,
105                                 server_name, "IPC$",
106                                 auth_info,
107                                 false, false,
108                                 PROTOCOL_NT1,
109                                 0, 0x20);
110         if (cli_ipc) {
111                 cli_set_username(cli_ipc, ctx->username);
112                 cli_set_password(cli_ipc, ctx->password);
113                 cli_set_domain(cli_ipc, ctx->workgroup);
114         }
115         TALLOC_FREE(auth_info);
116
117         if (!cli_ipc) {
118                 libnetapi_set_error_string(ctx,
119                         "Failed to connect to IPC$ share on %s", server_name);
120                 return WERR_CAN_NOT_COMPLETE;
121         }
122
123         p = TALLOC_ZERO_P(ctx, struct client_ipc_connection);
124         if (p == NULL) {
125                 return WERR_NOMEM;
126         }
127
128         p->cli = cli_ipc;
129         DLIST_ADD(priv_ctx->ipc_connections, p);
130
131         *pp = p;
132
133         return WERR_OK;
134 }
135
136 /********************************************************************
137 ********************************************************************/
138
139 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
140 {
141         struct libnetapi_private_ctx *priv_ctx =
142                 (struct libnetapi_private_ctx *)ctx->private_data;
143         struct client_ipc_connection *p;
144
145         for (p = priv_ctx->ipc_connections; p; p = p->next) {
146                 cli_shutdown(p->cli);
147         }
148
149         return WERR_OK;
150 }
151
152 /********************************************************************
153 ********************************************************************/
154
155 static NTSTATUS pipe_cm_find(struct client_ipc_connection *ipc,
156                              const struct ndr_syntax_id *interface,
157                              struct rpc_pipe_client **presult)
158 {
159         struct client_pipe_connection *p;
160
161         for (p = ipc->pipe_connections; p; p = p->next) {
162
163                 if (!rpc_pipe_np_smb_conn(p->pipe)) {
164                         return NT_STATUS_PIPE_EMPTY;
165                 }
166
167                 if (strequal(ipc->cli->desthost, p->pipe->desthost)
168                     && ndr_syntax_id_equal(&p->pipe->abstract_syntax,
169                                            interface)) {
170                         *presult = p->pipe;
171                         return NT_STATUS_OK;
172                 }
173         }
174
175         return NT_STATUS_PIPE_NOT_AVAILABLE;
176 }
177
178 /********************************************************************
179 ********************************************************************/
180
181 static NTSTATUS pipe_cm_connect(TALLOC_CTX *mem_ctx,
182                                 struct client_ipc_connection *ipc,
183                                 const struct ndr_syntax_id *interface,
184                                 struct rpc_pipe_client **presult)
185 {
186         struct client_pipe_connection *p;
187         NTSTATUS status;
188
189         p = TALLOC_ZERO_ARRAY(mem_ctx, struct client_pipe_connection, 1);
190         if (!p) {
191                 return NT_STATUS_NO_MEMORY;
192         }
193
194         status = cli_rpc_pipe_open_noauth(ipc->cli, interface, &p->pipe);
195         if (!NT_STATUS_IS_OK(status)) {
196                 TALLOC_FREE(p);
197                 return status;
198         }
199
200         DLIST_ADD(ipc->pipe_connections, p);
201
202         *presult = p->pipe;
203         return NT_STATUS_OK;
204 }
205
206 /********************************************************************
207 ********************************************************************/
208
209 static NTSTATUS pipe_cm_open(TALLOC_CTX *ctx,
210                              struct client_ipc_connection *ipc,
211                              const struct ndr_syntax_id *interface,
212                              struct rpc_pipe_client **presult)
213 {
214         if (NT_STATUS_IS_OK(pipe_cm_find(ipc, interface, presult))) {
215                 return NT_STATUS_OK;
216         }
217
218         return pipe_cm_connect(ctx, ipc, interface, presult);
219 }
220
221 /********************************************************************
222 ********************************************************************/
223
224 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
225                            const char *server_name,
226                            const struct ndr_syntax_id *interface,
227                            struct rpc_pipe_client **presult)
228 {
229         struct rpc_pipe_client *result = NULL;
230         NTSTATUS status;
231         WERROR werr;
232         struct client_ipc_connection *ipc = NULL;
233
234         if (!presult) {
235                 return WERR_INVALID_PARAM;
236         }
237
238         werr = libnetapi_open_ipc_connection(ctx, server_name, &ipc);
239         if (!W_ERROR_IS_OK(werr)) {
240                 return werr;
241         }
242
243         status = pipe_cm_open(ctx, ipc, interface, &result);
244         if (!NT_STATUS_IS_OK(status)) {
245                 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
246                         get_pipe_name_from_syntax(talloc_tos(), interface),
247                         get_friendly_nt_error_msg(status));
248                 return WERR_DEST_NOT_FOUND;
249         }
250
251         *presult = result;
252
253         return WERR_OK;
254 }