libnetapi_open_pipe does not need to return cli_state
[Samba/gebeck_regimport.git] / source3 / lib / netapi / cm.c
blobe616a2509f223b44d8470bba6873f0fc9fe91561
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 static 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 PROTOCOL_NT1,
61 0, 0x20);
62 if (!cli_ipc) {
63 libnetapi_set_error_string(ctx,
64 "Failed to connect to IPC$ share on %s", server_name);
65 return WERR_CAN_NOT_COMPLETE;
68 *cli = cli_ipc;
70 return WERR_OK;
73 /********************************************************************
74 ********************************************************************/
76 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
78 cli_cm_shutdown();
80 return WERR_OK;
83 /********************************************************************
84 ********************************************************************/
86 struct client_pipe_connection {
87 struct client_pipe_connection *prev, *next;
88 struct rpc_pipe_client *pipe;
91 static struct client_pipe_connection *pipe_connections;
93 /********************************************************************
94 ********************************************************************/
96 static NTSTATUS pipe_cm_find(struct cli_state *cli,
97 const struct ndr_syntax_id *interface,
98 struct rpc_pipe_client **presult)
100 struct client_pipe_connection *p;
102 for (p = pipe_connections; p; p = p->next) {
104 if (!rpc_pipe_np_smb_conn(p->pipe)) {
105 return NT_STATUS_PIPE_EMPTY;
108 if (strequal(cli->desthost, p->pipe->desthost)
109 && ndr_syntax_id_equal(&p->pipe->abstract_syntax,
110 interface)) {
111 *presult = p->pipe;
112 return NT_STATUS_OK;
116 return NT_STATUS_PIPE_NOT_AVAILABLE;
119 /********************************************************************
120 ********************************************************************/
122 static NTSTATUS pipe_cm_connect(TALLOC_CTX *mem_ctx,
123 struct cli_state *cli,
124 const struct ndr_syntax_id *interface,
125 struct rpc_pipe_client **presult)
127 struct client_pipe_connection *p;
128 NTSTATUS status;
130 p = TALLOC_ZERO_ARRAY(mem_ctx, struct client_pipe_connection, 1);
131 if (!p) {
132 return NT_STATUS_NO_MEMORY;
135 status = cli_rpc_pipe_open_noauth(cli, interface, &p->pipe);
136 if (!NT_STATUS_IS_OK(status)) {
137 TALLOC_FREE(p);
138 return status;
141 DLIST_ADD(pipe_connections, p);
143 *presult = p->pipe;
144 return NT_STATUS_OK;
147 /********************************************************************
148 ********************************************************************/
150 static NTSTATUS pipe_cm_open(TALLOC_CTX *ctx,
151 struct cli_state *cli,
152 const struct ndr_syntax_id *interface,
153 struct rpc_pipe_client **presult)
155 if (NT_STATUS_IS_OK(pipe_cm_find(cli, interface, presult))) {
156 return NT_STATUS_OK;
159 return pipe_cm_connect(ctx, cli, interface, presult);
162 /********************************************************************
163 ********************************************************************/
165 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
166 const char *server_name,
167 const struct ndr_syntax_id *interface,
168 struct rpc_pipe_client **presult)
170 struct rpc_pipe_client *result = NULL;
171 NTSTATUS status;
172 WERROR werr;
173 struct cli_state *cli = NULL;
175 if (!presult) {
176 return WERR_INVALID_PARAM;
179 werr = libnetapi_open_ipc_connection(ctx, server_name, &cli);
180 if (!W_ERROR_IS_OK(werr)) {
181 return werr;
184 status = pipe_cm_open(ctx, cli, interface, &result);
185 if (!NT_STATUS_IS_OK(status)) {
186 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
187 cli_get_pipe_name_from_iface(debug_ctx(), cli,
188 interface),
189 get_friendly_nt_error_msg(status));
190 return WERR_DEST_NOT_FOUND;
193 *presult = result;
195 return WERR_OK;