Remove the global "struct cm_cred_struct" and associated calls, make
[Samba.git] / source3 / lib / netapi / cm.c
blobb676ae63dde08c6296b8b8349142be5886563dd9
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 user_auth_info *auth_info = NULL;
33 struct cli_state *cli_ipc = NULL;
35 if (!ctx || !cli || !server_name) {
36 return WERR_INVALID_PARAM;
39 auth_info = user_auth_info_init(NULL);
40 if (!auth_info) {
41 return WERR_NOMEM;
43 auth_info->signing_state = Undefined;
44 set_cmdline_auth_info_use_kerberos(auth_info, ctx->use_kerberos);
45 set_cmdline_auth_info_password(auth_info, ctx->password);
46 set_cmdline_auth_info_username(auth_info, ctx->username);
48 if (ctx->username && ctx->username[0] &&
49 ctx->password && ctx->password[0] &&
50 ctx->use_kerberos) {
51 set_cmdline_auth_info_fallback_after_kerberos(auth_info, true);
54 cli_ipc = cli_cm_open(ctx, NULL,
55 server_name, "IPC$",
56 auth_info,
57 false, false,
58 PROTOCOL_NT1,
59 0, 0x20);
60 TALLOC_FREE(auth_info);
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 struct client_pipe_connection {
77 struct client_pipe_connection *prev, *next;
78 struct rpc_pipe_client *pipe;
79 struct cli_state *cli;
82 static struct client_pipe_connection *pipe_connections;
84 /********************************************************************
85 ********************************************************************/
87 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
89 struct client_pipe_connection *p;
91 for (p = pipe_connections; p; p = p->next) {
92 cli_shutdown(p->cli);
95 return WERR_OK;
98 /********************************************************************
99 ********************************************************************/
101 static NTSTATUS pipe_cm_find(struct cli_state *cli,
102 const struct ndr_syntax_id *interface,
103 struct rpc_pipe_client **presult)
105 struct client_pipe_connection *p;
107 for (p = pipe_connections; p; p = p->next) {
109 if (!rpc_pipe_np_smb_conn(p->pipe)) {
110 return NT_STATUS_PIPE_EMPTY;
113 if (strequal(cli->desthost, p->pipe->desthost)
114 && ndr_syntax_id_equal(&p->pipe->abstract_syntax,
115 interface)) {
116 *presult = p->pipe;
117 return NT_STATUS_OK;
121 return NT_STATUS_PIPE_NOT_AVAILABLE;
124 /********************************************************************
125 ********************************************************************/
127 static NTSTATUS pipe_cm_connect(TALLOC_CTX *mem_ctx,
128 struct cli_state *cli,
129 const struct ndr_syntax_id *interface,
130 struct rpc_pipe_client **presult)
132 struct client_pipe_connection *p;
133 NTSTATUS status;
135 p = TALLOC_ZERO_ARRAY(mem_ctx, struct client_pipe_connection, 1);
136 if (!p) {
137 return NT_STATUS_NO_MEMORY;
140 status = cli_rpc_pipe_open_noauth(cli, interface, &p->pipe);
141 if (!NT_STATUS_IS_OK(status)) {
142 TALLOC_FREE(p);
143 return status;
146 p->cli = cli;
147 DLIST_ADD(pipe_connections, p);
149 *presult = p->pipe;
150 return NT_STATUS_OK;
153 /********************************************************************
154 ********************************************************************/
156 static NTSTATUS pipe_cm_open(TALLOC_CTX *ctx,
157 struct cli_state *cli,
158 const struct ndr_syntax_id *interface,
159 struct rpc_pipe_client **presult)
161 if (NT_STATUS_IS_OK(pipe_cm_find(cli, interface, presult))) {
162 return NT_STATUS_OK;
165 return pipe_cm_connect(ctx, cli, interface, presult);
168 /********************************************************************
169 ********************************************************************/
171 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
172 const char *server_name,
173 const struct ndr_syntax_id *interface,
174 struct rpc_pipe_client **presult)
176 struct rpc_pipe_client *result = NULL;
177 NTSTATUS status;
178 WERROR werr;
179 struct cli_state *cli = NULL;
181 if (!presult) {
182 return WERR_INVALID_PARAM;
185 werr = libnetapi_open_ipc_connection(ctx, server_name, &cli);
186 if (!W_ERROR_IS_OK(werr)) {
187 return werr;
190 status = pipe_cm_open(ctx, cli, interface, &result);
191 if (!NT_STATUS_IS_OK(status)) {
192 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
193 get_pipe_name_from_iface(interface),
194 get_friendly_nt_error_msg(status));
195 return WERR_DEST_NOT_FOUND;
198 *presult = result;
200 return WERR_OK;