s3-lib: Fix util_cmdline which doesn't use popt.
[metze/samba/wip.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 "auth_info.h"
22
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"
27
28 /********************************************************************
29 ********************************************************************/
30
31 struct client_ipc_connection {
32         struct client_ipc_connection *prev, *next;
33         struct cli_state *cli;
34         struct client_pipe_connection *pipe_connections;
35 };
36
37 struct client_pipe_connection {
38         struct client_pipe_connection *prev, *next;
39         struct rpc_pipe_client *pipe;
40 };
41
42 /********************************************************************
43 ********************************************************************/
44
45 static struct client_ipc_connection *ipc_cm_find(
46         struct libnetapi_private_ctx *priv_ctx, const char *server_name)
47 {
48         struct client_ipc_connection *p;
49
50         for (p = priv_ctx->ipc_connections; p; p = p->next) {
51                 const char *remote_name = cli_state_remote_name(p->cli);
52
53                 if (strequal(remote_name, server_name)) {
54                         return p;
55                 }
56         }
57
58         return NULL;
59 }
60
61 /********************************************************************
62 ********************************************************************/
63
64 static WERROR libnetapi_open_ipc_connection(struct libnetapi_ctx *ctx,
65                                             const char *server_name,
66                                             struct client_ipc_connection **pp)
67 {
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;
73
74         if (!ctx || !pp || !server_name) {
75                 return WERR_INVALID_PARAM;
76         }
77
78         priv_ctx = (struct libnetapi_private_ctx *)ctx->private_data;
79
80         p = ipc_cm_find(priv_ctx, server_name);
81         if (p) {
82                 *pp = p;
83                 return WERR_OK;
84         }
85
86         auth_info = user_auth_info_init(ctx);
87         if (!auth_info) {
88                 return WERR_NOMEM;
89         }
90         auth_info->signing_state = SMB_SIGNING_DEFAULT;
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);
97         }
98
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);
103         }
104
105         if (ctx->use_ccache) {
106                 set_cmdline_auth_info_use_ccache(auth_info, true);
107         }
108
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;
121         }
122         TALLOC_FREE(auth_info);
123
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;
128         }
129
130         p = talloc_zero(ctx, struct client_ipc_connection);
131         if (p == NULL) {
132                 return WERR_NOMEM;
133         }
134
135         p->cli = cli_ipc;
136         DLIST_ADD(priv_ctx->ipc_connections, p);
137
138         *pp = p;
139
140         return WERR_OK;
141 }
142
143 /********************************************************************
144 ********************************************************************/
145
146 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
147 {
148         struct libnetapi_private_ctx *priv_ctx =
149                 (struct libnetapi_private_ctx *)ctx->private_data;
150         struct client_ipc_connection *p;
151
152         for (p = priv_ctx->ipc_connections; p; p = p->next) {
153                 cli_shutdown(p->cli);
154         }
155
156         return WERR_OK;
157 }
158
159 /********************************************************************
160 ********************************************************************/
161
162 static NTSTATUS pipe_cm_find(struct client_ipc_connection *ipc,
163                              const struct ndr_syntax_id *interface,
164                              struct rpc_pipe_client **presult)
165 {
166         struct client_pipe_connection *p;
167
168         for (p = ipc->pipe_connections; p; p = p->next) {
169                 const char *ipc_remote_name;
170
171                 if (!rpc_pipe_np_smb_conn(p->pipe)) {
172                         return NT_STATUS_PIPE_EMPTY;
173                 }
174
175                 ipc_remote_name = cli_state_remote_name(ipc->cli);
176
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;
182                 }
183         }
184
185         return NT_STATUS_PIPE_NOT_AVAILABLE;
186 }
187
188 /********************************************************************
189 ********************************************************************/
190
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)
195 {
196         struct client_pipe_connection *p;
197         NTSTATUS status;
198
199         p = talloc_zero_array(mem_ctx, struct client_pipe_connection, 1);
200         if (!p) {
201                 return NT_STATUS_NO_MEMORY;
202         }
203
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;
208         }
209
210         DLIST_ADD(ipc->pipe_connections, p);
211
212         *presult = p->pipe;
213         return NT_STATUS_OK;
214 }
215
216 /********************************************************************
217 ********************************************************************/
218
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)
223 {
224         if (NT_STATUS_IS_OK(pipe_cm_find(ipc, interface, presult))) {
225                 return NT_STATUS_OK;
226         }
227
228         return pipe_cm_connect(ctx, ipc, interface, presult);
229 }
230
231 /********************************************************************
232 ********************************************************************/
233
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)
238 {
239         struct rpc_pipe_client *result = NULL;
240         NTSTATUS status;
241         WERROR werr;
242         struct client_ipc_connection *ipc = NULL;
243
244         if (!presult) {
245                 return WERR_INVALID_PARAM;
246         }
247
248         werr = libnetapi_open_ipc_connection(ctx, server_name, &ipc);
249         if (!W_ERROR_IS_OK(werr)) {
250                 return werr;
251         }
252
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;
259         }
260
261         *presult = result;
262
263         return WERR_OK;
264 }
265
266 /********************************************************************
267 ********************************************************************/
268
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)
273 {
274         struct rpc_pipe_client *pipe_cli;
275         WERROR result;
276
277         *binding_handle = NULL;
278
279         result = libnetapi_open_pipe(ctx, server_name, interface, &pipe_cli);
280         if (!W_ERROR_IS_OK(result)) {
281                 return result;
282         }
283
284         *binding_handle = pipe_cli->binding_handle;
285
286         return WERR_OK;
287 }