Remove the pipe_idx variable from rpc_pipe_client
[Samba.git] / source / lib / netapi / cm.c
blob2e16b98ffba1e1bac3f32c27fc853d2f80b2bcea
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"
25 /********************************************************************
26 ********************************************************************/
28 WERROR libnetapi_open_ipc_connection(struct libnetapi_ctx *ctx,
29 const char *server_name,
30 struct cli_state **cli)
32 struct cli_state *cli_ipc = NULL;
34 if (!ctx || !cli || !server_name) {
35 return WERR_INVALID_PARAM;
38 cli_cm_set_signing_state(Undefined);
40 if (ctx->use_kerberos) {
41 cli_cm_set_use_kerberos();
44 if (ctx->password) {
45 cli_cm_set_password(ctx->password);
47 if (ctx->username) {
48 cli_cm_set_username(ctx->username);
51 if (ctx->username && ctx->username[0] &&
52 ctx->password && ctx->password[0] &&
53 ctx->use_kerberos) {
54 cli_cm_set_fallback_after_kerberos();
57 cli_ipc = cli_cm_open(ctx, NULL,
58 server_name, "IPC$",
59 false, false);
60 if (!cli_ipc) {
61 libnetapi_set_error_string(ctx,
62 "Failed to connect to IPC$ share on %s", server_name);
63 return WERR_CAN_NOT_COMPLETE;
66 *cli = cli_ipc;
68 return WERR_OK;
71 /********************************************************************
72 ********************************************************************/
74 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
76 cli_cm_shutdown();
78 return WERR_OK;
81 /********************************************************************
82 ********************************************************************/
84 struct client_pipe_connection {
85 struct client_pipe_connection *prev, *next;
86 struct rpc_pipe_client *pipe;
89 static struct client_pipe_connection *pipe_connections;
91 /********************************************************************
92 ********************************************************************/
94 static struct rpc_pipe_client *pipe_cm_find(struct cli_state *cli,
95 int pipe_idx,
96 NTSTATUS *status)
98 struct client_pipe_connection *p;
100 for (p = pipe_connections; p; p = p->next) {
102 if (!p->pipe->cli) {
103 *status = NT_STATUS_PIPE_EMPTY;
104 return NULL;
107 if (strequal(cli->desthost, p->pipe->desthost) &&
108 rpccli_is_pipe_idx(p->pipe, pipe_idx)) {
109 *status = NT_STATUS_OK;
110 return p->pipe;
114 *status = NT_STATUS_PIPE_NOT_AVAILABLE;
116 return NULL;
119 /********************************************************************
120 ********************************************************************/
122 static struct rpc_pipe_client *pipe_cm_connect(TALLOC_CTX *mem_ctx,
123 struct cli_state *cli,
124 int pipe_idx,
125 NTSTATUS *status)
127 struct client_pipe_connection *p;
129 p = TALLOC_ZERO_ARRAY(mem_ctx, struct client_pipe_connection, 1);
130 if (!p) {
131 *status = NT_STATUS_NO_MEMORY;
132 return NULL;
135 p->pipe = cli_rpc_pipe_open_noauth(cli, pipe_idx, status);
136 if (!p->pipe) {
137 TALLOC_FREE(p);
138 return NULL;
141 DLIST_ADD(pipe_connections, p);
143 return p->pipe;
146 /********************************************************************
147 ********************************************************************/
149 static struct rpc_pipe_client *pipe_cm_open(TALLOC_CTX *ctx,
150 struct cli_state *cli,
151 int pipe_idx,
152 NTSTATUS *status)
154 struct rpc_pipe_client *p;
156 p = pipe_cm_find(cli, pipe_idx, status);
157 if (!p) {
158 p = pipe_cm_connect(ctx, cli, pipe_idx, status);
161 return p;
164 /********************************************************************
165 ********************************************************************/
167 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
168 struct cli_state *cli,
169 int pipe_idx,
170 struct rpc_pipe_client **pipe_cli)
172 NTSTATUS status;
174 if (!cli || !pipe_cli) {
175 return WERR_INVALID_PARAM;
178 *pipe_cli = pipe_cm_open(ctx, cli, pipe_idx, &status);
179 if (!*pipe_cli) {
180 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
181 cli_get_pipe_name(pipe_idx),
182 get_friendly_nt_error_msg(status));
183 return WERR_DEST_NOT_FOUND;
186 return WERR_OK;