ctdb-server: Clean up connection tracking functions
[samba4-gss.git] / source3 / lib / netapi / cm.c
blobda63fd921e92042a660636b93356b642ddc2b0d9
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"
22 #include "lib/netapi/netapi.h"
23 #include "lib/netapi/netapi_private.h"
24 #include "libsmb/libsmb.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/smb/smbXcli_base.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 = smbXcli_conn_remote_name(p->cli->conn);
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 cli_state *cli_ipc = NULL;
70 struct client_ipc_connection *p;
71 NTSTATUS status;
72 const char *username = NULL;
73 const char *password = NULL;
74 NET_API_STATUS rc;
75 enum credentials_use_kerberos krb5_state;
77 if (!ctx || !pp || !server_name) {
78 return WERR_INVALID_PARAMETER;
81 priv_ctx = (struct libnetapi_private_ctx *)ctx->private_data;
83 p = ipc_cm_find(priv_ctx, server_name);
84 if (p) {
85 *pp = p;
86 return WERR_OK;
89 rc = libnetapi_get_username(ctx, &username);
90 if (rc != 0) {
91 return WERR_INTERNAL_ERROR;
94 rc = libnetapi_get_password(ctx, &password);
95 if (rc != 0) {
96 return WERR_INTERNAL_ERROR;
99 if (password == NULL) {
100 cli_credentials_set_cmdline_callbacks(ctx->creds);
103 krb5_state = cli_credentials_get_kerberos_state(ctx->creds);
105 if (username != NULL && username[0] != '\0' &&
106 password != NULL && password[0] != '\0' &&
107 krb5_state == CRED_USE_KERBEROS_REQUIRED) {
108 cli_credentials_set_kerberos_state(ctx->creds,
109 CRED_USE_KERBEROS_DESIRED,
110 CRED_SPECIFIED);
113 status = cli_cm_open(ctx, NULL,
114 server_name, "IPC$",
115 ctx->creds,
116 NULL, 0, 0x20, &cli_ipc);
117 if (!NT_STATUS_IS_OK(status)) {
118 cli_ipc = NULL;
121 if (!cli_ipc) {
122 libnetapi_set_error_string(ctx,
123 "Failed to connect to IPC$ share on %s", server_name);
124 return WERR_CAN_NOT_COMPLETE;
127 p = talloc_zero(ctx, struct client_ipc_connection);
128 if (p == NULL) {
129 return WERR_NOT_ENOUGH_MEMORY;
132 p->cli = cli_ipc;
133 DLIST_ADD(priv_ctx->ipc_connections, p);
135 *pp = p;
137 return WERR_OK;
140 /********************************************************************
141 ********************************************************************/
143 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
145 struct libnetapi_private_ctx *priv_ctx =
146 (struct libnetapi_private_ctx *)ctx->private_data;
147 struct client_ipc_connection *p;
149 for (p = priv_ctx->ipc_connections; p; p = p->next) {
150 cli_shutdown(p->cli);
153 return WERR_OK;
156 /********************************************************************
157 ********************************************************************/
159 static NTSTATUS pipe_cm_find(struct client_ipc_connection *ipc,
160 const struct ndr_interface_table *table,
161 struct rpc_pipe_client **presult)
163 struct client_pipe_connection *p;
165 for (p = ipc->pipe_connections; p; p = p->next) {
166 struct dcerpc_binding_handle *bh = NULL;
167 const struct dcerpc_binding *bd = NULL;
168 const char *ipc_remote_name;
169 struct ndr_syntax_id syntax;
171 if (!rpccli_is_connected(p->pipe)) {
172 return NT_STATUS_PIPE_EMPTY;
175 ipc_remote_name = smbXcli_conn_remote_name(ipc->cli->conn);
177 if (!strequal(ipc_remote_name, p->pipe->desthost)) {
178 continue;
181 bh = p->pipe->binding_handle;
182 bd = dcerpc_binding_handle_get_binding(bh);
183 syntax = dcerpc_binding_get_abstract_syntax(bd);
185 if (ndr_syntax_id_equal(&syntax, &table->syntax_id)) {
186 *presult = p->pipe;
187 return NT_STATUS_OK;
191 return NT_STATUS_PIPE_NOT_AVAILABLE;
194 /********************************************************************
195 ********************************************************************/
197 static NTSTATUS pipe_cm_connect(TALLOC_CTX *mem_ctx,
198 struct client_ipc_connection *ipc,
199 const struct ndr_interface_table *table,
200 struct rpc_pipe_client **presult)
202 struct client_pipe_connection *p;
203 NTSTATUS status;
205 p = talloc_zero_array(mem_ctx, struct client_pipe_connection, 1);
206 if (!p) {
207 return NT_STATUS_NO_MEMORY;
210 status = cli_rpc_pipe_open_noauth(ipc->cli, table, &p->pipe);
211 if (!NT_STATUS_IS_OK(status)) {
212 TALLOC_FREE(p);
213 return status;
216 DLIST_ADD(ipc->pipe_connections, p);
218 *presult = p->pipe;
219 return NT_STATUS_OK;
222 /********************************************************************
223 ********************************************************************/
225 static NTSTATUS pipe_cm_open(TALLOC_CTX *ctx,
226 struct client_ipc_connection *ipc,
227 const struct ndr_interface_table *table,
228 struct rpc_pipe_client **presult)
230 if (NT_STATUS_IS_OK(pipe_cm_find(ipc, table, presult))) {
231 return NT_STATUS_OK;
234 return pipe_cm_connect(ctx, ipc, table, presult);
237 /********************************************************************
238 ********************************************************************/
240 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
241 const char *server_name,
242 const struct ndr_interface_table *table,
243 struct rpc_pipe_client **presult)
245 struct rpc_pipe_client *result = NULL;
246 NTSTATUS status;
247 WERROR werr;
248 struct client_ipc_connection *ipc = NULL;
250 if (!presult) {
251 return WERR_INVALID_PARAMETER;
254 werr = libnetapi_open_ipc_connection(ctx, server_name, &ipc);
255 if (!W_ERROR_IS_OK(werr)) {
256 return werr;
259 status = pipe_cm_open(ctx, ipc, table, &result);
260 if (!NT_STATUS_IS_OK(status)) {
261 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
262 table->name,
263 get_friendly_nt_error_msg(status));
264 return WERR_NERR_DESTNOTFOUND;
267 *presult = result;
269 return WERR_OK;
272 /********************************************************************
273 ********************************************************************/
275 WERROR libnetapi_get_binding_handle(struct libnetapi_ctx *ctx,
276 const char *server_name,
277 const struct ndr_interface_table *table,
278 struct dcerpc_binding_handle **binding_handle)
280 struct rpc_pipe_client *pipe_cli;
281 WERROR result;
283 *binding_handle = NULL;
285 result = libnetapi_open_pipe(ctx, server_name, table, &pipe_cli);
286 if (!W_ERROR_IS_OK(result)) {
287 return result;
290 *binding_handle = pipe_cli->binding_handle;
292 return WERR_OK;