Add libnetapi_open_pipe, inspired by the cli_cm_ interface.
[Samba.git] / source / lib / netapi / cm.c
blob96087247d2798b720dba96d4c21adfee8f5d09a1
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);
39 cli_cm_set_use_kerberos();
41 if (ctx->password) {
42 cli_cm_set_password(ctx->password);
44 if (ctx->username) {
45 cli_cm_set_username(ctx->username);
48 if (ctx->username && ctx->username[0] &&
49 ctx->password && ctx->password[0]) {
50 cli_cm_set_fallback_after_kerberos();
53 cli_ipc = cli_cm_open(ctx, NULL,
54 server_name, "IPC$",
55 false, false);
56 if (!cli_ipc) {
57 libnetapi_set_error_string(ctx,
58 "Failed to connect to IPC$ share on %s", server_name);
59 return WERR_CAN_NOT_COMPLETE;
62 *cli = cli_ipc;
64 return WERR_OK;
67 /********************************************************************
68 ********************************************************************/
70 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
72 cli_cm_shutdown();
74 return WERR_OK;
77 /********************************************************************
78 ********************************************************************/
80 struct client_pipe_connection {
81 struct client_pipe_connection *prev, *next;
82 struct rpc_pipe_client *pipe;
85 static struct client_pipe_connection *pipe_connections;
87 /********************************************************************
88 ********************************************************************/
90 static struct rpc_pipe_client *pipe_cm_find(struct cli_state *cli,
91 int pipe_idx,
92 NTSTATUS *status)
94 struct client_pipe_connection *p;
96 for (p = pipe_connections; p; p = p->next) {
98 if (!p->pipe->cli) {
99 *status = NT_STATUS_PIPE_EMPTY;
100 return NULL;
103 if (strequal(cli->desthost, p->pipe->cli->desthost) &&
104 pipe_idx == p->pipe->pipe_idx) {
105 *status = NT_STATUS_OK;
106 return p->pipe;
110 *status = NT_STATUS_PIPE_NOT_AVAILABLE;
112 return NULL;
115 /********************************************************************
116 ********************************************************************/
118 static struct rpc_pipe_client *pipe_cm_connect(TALLOC_CTX *mem_ctx,
119 struct cli_state *cli,
120 int pipe_idx,
121 NTSTATUS *status)
123 struct client_pipe_connection *p;
125 p = TALLOC_ZERO_ARRAY(mem_ctx, struct client_pipe_connection, 1);
126 if (!p) {
127 *status = NT_STATUS_NO_MEMORY;
128 return NULL;
131 p->pipe = cli_rpc_pipe_open_noauth(cli, pipe_idx, status);
132 if (!p->pipe) {
133 TALLOC_FREE(p);
134 return NULL;
137 DLIST_ADD(pipe_connections, p);
139 return p->pipe;
142 /********************************************************************
143 ********************************************************************/
145 static struct rpc_pipe_client *pipe_cm_open(TALLOC_CTX *ctx,
146 struct cli_state *cli,
147 int pipe_idx,
148 NTSTATUS *status)
150 struct rpc_pipe_client *p;
152 p = pipe_cm_find(cli, pipe_idx, status);
153 if (!p) {
154 p = pipe_cm_connect(ctx, cli, pipe_idx, status);
157 return p;
160 /********************************************************************
161 ********************************************************************/
163 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
164 struct cli_state *cli,
165 int pipe_idx,
166 struct rpc_pipe_client **pipe_cli)
168 NTSTATUS status;
170 if (!cli || !pipe_cli) {
171 return WERR_INVALID_PARAM;
174 *pipe_cli = pipe_cm_open(ctx, cli, pipe_idx, &status);
175 if (!*pipe_cli) {
176 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
177 cli_get_pipe_name(pipe_idx),
178 get_friendly_nt_error_msg(status));
179 return WERR_DEST_NOT_FOUND;
182 return WERR_OK;