netapi: make libnetapi_open_ipc_connection static.
[metze/samba/wip.git] / source3 / lib / netapi / cm.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi Support
4  *  Copyright (C) Guenther Deschner 2008
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21
22 #include "lib/netapi/netapi.h"
23 #include "lib/netapi/netapi_private.h"
24
25 /********************************************************************
26 ********************************************************************/
27
28 static WERROR libnetapi_open_ipc_connection(struct libnetapi_ctx *ctx,
29                                             const char *server_name,
30                                             struct cli_state **cli)
31 {
32         struct cli_state *cli_ipc = NULL;
33
34         if (!ctx || !cli || !server_name) {
35                 return WERR_INVALID_PARAM;
36         }
37
38         cli_cm_set_signing_state(Undefined);
39
40         if (ctx->use_kerberos) {
41                 cli_cm_set_use_kerberos();
42         }
43
44         if (ctx->password) {
45                 cli_cm_set_password(ctx->password);
46         }
47         if (ctx->username) {
48                 cli_cm_set_username(ctx->username);
49         }
50
51         if (ctx->username && ctx->username[0] &&
52             ctx->password && ctx->password[0] &&
53             ctx->use_kerberos) {
54                 cli_cm_set_fallback_after_kerberos();
55         }
56
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;
64         }
65
66         *cli = cli_ipc;
67
68         return WERR_OK;
69 }
70
71 /********************************************************************
72 ********************************************************************/
73
74 WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
75 {
76         cli_cm_shutdown();
77
78         return WERR_OK;
79 }
80
81 /********************************************************************
82 ********************************************************************/
83
84 struct client_pipe_connection {
85         struct client_pipe_connection *prev, *next;
86         struct rpc_pipe_client *pipe;
87 };
88
89 static struct client_pipe_connection *pipe_connections;
90
91 /********************************************************************
92 ********************************************************************/
93
94 static NTSTATUS pipe_cm_find(struct cli_state *cli,
95                              const struct ndr_syntax_id *interface,
96                              struct rpc_pipe_client **presult)
97 {
98         struct client_pipe_connection *p;
99
100         for (p = pipe_connections; p; p = p->next) {
101
102                 if (!rpc_pipe_np_smb_conn(p->pipe)) {
103                         return NT_STATUS_PIPE_EMPTY;
104                 }
105
106                 if (strequal(cli->desthost, p->pipe->desthost)
107                     && ndr_syntax_id_equal(&p->pipe->abstract_syntax,
108                                            interface)) {
109                         *presult = p->pipe;
110                         return NT_STATUS_OK;
111                 }
112         }
113
114         return NT_STATUS_PIPE_NOT_AVAILABLE;
115 }
116
117 /********************************************************************
118 ********************************************************************/
119
120 static NTSTATUS pipe_cm_connect(TALLOC_CTX *mem_ctx,
121                                 struct cli_state *cli,
122                                 const struct ndr_syntax_id *interface,
123                                 struct rpc_pipe_client **presult)
124 {
125         struct client_pipe_connection *p;
126         NTSTATUS status;
127
128         p = TALLOC_ZERO_ARRAY(mem_ctx, struct client_pipe_connection, 1);
129         if (!p) {
130                 return NT_STATUS_NO_MEMORY;
131         }
132
133         status = cli_rpc_pipe_open_noauth(cli, interface, &p->pipe);
134         if (!NT_STATUS_IS_OK(status)) {
135                 TALLOC_FREE(p);
136                 return status;
137         }
138
139         DLIST_ADD(pipe_connections, p);
140
141         *presult = p->pipe;
142         return NT_STATUS_OK;
143 }
144
145 /********************************************************************
146 ********************************************************************/
147
148 static NTSTATUS pipe_cm_open(TALLOC_CTX *ctx,
149                              struct cli_state *cli,
150                              const struct ndr_syntax_id *interface,
151                              struct rpc_pipe_client **presult)
152 {
153         if (NT_STATUS_IS_OK(pipe_cm_find(cli, interface, presult))) {
154                 return NT_STATUS_OK;
155         }
156
157         return pipe_cm_connect(ctx, cli, interface, presult);
158 }
159
160 /********************************************************************
161 ********************************************************************/
162
163 WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
164                            const char *server_name,
165                            const struct ndr_syntax_id *interface,
166                            struct cli_state **pcli,
167                            struct rpc_pipe_client **presult)
168 {
169         struct rpc_pipe_client *result = NULL;
170         NTSTATUS status;
171         WERROR werr;
172         struct cli_state *cli = NULL;
173
174         if (!presult) {
175                 return WERR_INVALID_PARAM;
176         }
177
178         werr = libnetapi_open_ipc_connection(ctx, server_name, &cli);
179         if (!W_ERROR_IS_OK(werr)) {
180                 return werr;
181         }
182
183         status = pipe_cm_open(ctx, cli, interface, &result);
184         if (!NT_STATUS_IS_OK(status)) {
185                 libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
186                         cli_get_pipe_name_from_iface(debug_ctx(), cli,
187                                                      interface),
188                         get_friendly_nt_error_msg(status));
189                 return WERR_DEST_NOT_FOUND;
190         }
191
192         *presult = result;
193         *pcli = cli;
194
195         return WERR_OK;
196 }
197
198