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