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 | #include "popt_common.h"
|
---|
22 |
|
---|
23 | #include "lib/netapi/netapi.h"
|
---|
24 | #include "lib/netapi/netapi_private.h"
|
---|
25 | #include "libsmb/libsmb.h"
|
---|
26 | #include "rpc_client/cli_pipe.h"
|
---|
27 |
|
---|
28 | /********************************************************************
|
---|
29 | ********************************************************************/
|
---|
30 |
|
---|
31 | struct client_ipc_connection {
|
---|
32 | struct client_ipc_connection *prev, *next;
|
---|
33 | struct cli_state *cli;
|
---|
34 | struct client_pipe_connection *pipe_connections;
|
---|
35 | };
|
---|
36 |
|
---|
37 | struct client_pipe_connection {
|
---|
38 | struct client_pipe_connection *prev, *next;
|
---|
39 | struct rpc_pipe_client *pipe;
|
---|
40 | };
|
---|
41 |
|
---|
42 | /********************************************************************
|
---|
43 | ********************************************************************/
|
---|
44 |
|
---|
45 | static struct client_ipc_connection *ipc_cm_find(
|
---|
46 | struct libnetapi_private_ctx *priv_ctx, const char *server_name)
|
---|
47 | {
|
---|
48 | struct client_ipc_connection *p;
|
---|
49 |
|
---|
50 | for (p = priv_ctx->ipc_connections; p; p = p->next) {
|
---|
51 | if (strequal(p->cli->desthost, server_name)) {
|
---|
52 | return p;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | return NULL;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /********************************************************************
|
---|
60 | ********************************************************************/
|
---|
61 |
|
---|
62 | static WERROR libnetapi_open_ipc_connection(struct libnetapi_ctx *ctx,
|
---|
63 | const char *server_name,
|
---|
64 | struct client_ipc_connection **pp)
|
---|
65 | {
|
---|
66 | struct libnetapi_private_ctx *priv_ctx =
|
---|
67 | (struct libnetapi_private_ctx *)ctx->private_data;
|
---|
68 | struct user_auth_info *auth_info = NULL;
|
---|
69 | struct cli_state *cli_ipc = NULL;
|
---|
70 | struct client_ipc_connection *p;
|
---|
71 |
|
---|
72 | if (!ctx || !pp || !server_name) {
|
---|
73 | return WERR_INVALID_PARAM;
|
---|
74 | }
|
---|
75 |
|
---|
76 | p = ipc_cm_find(priv_ctx, server_name);
|
---|
77 | if (p) {
|
---|
78 | *pp = p;
|
---|
79 | return WERR_OK;
|
---|
80 | }
|
---|
81 |
|
---|
82 | auth_info = user_auth_info_init(ctx);
|
---|
83 | if (!auth_info) {
|
---|
84 | return WERR_NOMEM;
|
---|
85 | }
|
---|
86 | auth_info->signing_state = Undefined;
|
---|
87 | set_cmdline_auth_info_use_kerberos(auth_info, ctx->use_kerberos);
|
---|
88 | set_cmdline_auth_info_username(auth_info, ctx->username);
|
---|
89 | if (ctx->password) {
|
---|
90 | set_cmdline_auth_info_password(auth_info, ctx->password);
|
---|
91 | } else {
|
---|
92 | set_cmdline_auth_info_getpass(auth_info);
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (ctx->username && ctx->username[0] &&
|
---|
96 | ctx->password && ctx->password[0] &&
|
---|
97 | ctx->use_kerberos) {
|
---|
98 | set_cmdline_auth_info_fallback_after_kerberos(auth_info, true);
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (ctx->use_ccache) {
|
---|
102 | set_cmdline_auth_info_use_ccache(auth_info, true);
|
---|
103 | }
|
---|
104 |
|
---|
105 | cli_ipc = cli_cm_open(ctx, NULL,
|
---|
106 | server_name, "IPC$",
|
---|
107 | auth_info,
|
---|
108 | false, false,
|
---|
109 | PROTOCOL_NT1,
|
---|
110 | 0, 0x20);
|
---|
111 | if (cli_ipc) {
|
---|
112 | cli_set_username(cli_ipc, ctx->username);
|
---|
113 | cli_set_password(cli_ipc, ctx->password);
|
---|
114 | cli_set_domain(cli_ipc, ctx->workgroup);
|
---|
115 | }
|
---|
116 | TALLOC_FREE(auth_info);
|
---|
117 |
|
---|
118 | if (!cli_ipc) {
|
---|
119 | libnetapi_set_error_string(ctx,
|
---|
120 | "Failed to connect to IPC$ share on %s", server_name);
|
---|
121 | return WERR_CAN_NOT_COMPLETE;
|
---|
122 | }
|
---|
123 |
|
---|
124 | p = TALLOC_ZERO_P(ctx, struct client_ipc_connection);
|
---|
125 | if (p == NULL) {
|
---|
126 | return WERR_NOMEM;
|
---|
127 | }
|
---|
128 |
|
---|
129 | p->cli = cli_ipc;
|
---|
130 | DLIST_ADD(priv_ctx->ipc_connections, p);
|
---|
131 |
|
---|
132 | *pp = p;
|
---|
133 |
|
---|
134 | return WERR_OK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /********************************************************************
|
---|
138 | ********************************************************************/
|
---|
139 |
|
---|
140 | WERROR libnetapi_shutdown_cm(struct libnetapi_ctx *ctx)
|
---|
141 | {
|
---|
142 | struct libnetapi_private_ctx *priv_ctx =
|
---|
143 | (struct libnetapi_private_ctx *)ctx->private_data;
|
---|
144 | struct client_ipc_connection *p;
|
---|
145 |
|
---|
146 | for (p = priv_ctx->ipc_connections; p; p = p->next) {
|
---|
147 | cli_shutdown(p->cli);
|
---|
148 | }
|
---|
149 |
|
---|
150 | return WERR_OK;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /********************************************************************
|
---|
154 | ********************************************************************/
|
---|
155 |
|
---|
156 | static NTSTATUS pipe_cm_find(struct client_ipc_connection *ipc,
|
---|
157 | const struct ndr_syntax_id *interface,
|
---|
158 | struct rpc_pipe_client **presult)
|
---|
159 | {
|
---|
160 | struct client_pipe_connection *p;
|
---|
161 |
|
---|
162 | for (p = ipc->pipe_connections; p; p = p->next) {
|
---|
163 |
|
---|
164 | if (!rpc_pipe_np_smb_conn(p->pipe)) {
|
---|
165 | return NT_STATUS_PIPE_EMPTY;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (strequal(ipc->cli->desthost, p->pipe->desthost)
|
---|
169 | && ndr_syntax_id_equal(&p->pipe->abstract_syntax,
|
---|
170 | interface)) {
|
---|
171 | *presult = p->pipe;
|
---|
172 | return NT_STATUS_OK;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | return NT_STATUS_PIPE_NOT_AVAILABLE;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /********************************************************************
|
---|
180 | ********************************************************************/
|
---|
181 |
|
---|
182 | static NTSTATUS pipe_cm_connect(TALLOC_CTX *mem_ctx,
|
---|
183 | struct client_ipc_connection *ipc,
|
---|
184 | const struct ndr_syntax_id *interface,
|
---|
185 | struct rpc_pipe_client **presult)
|
---|
186 | {
|
---|
187 | struct client_pipe_connection *p;
|
---|
188 | NTSTATUS status;
|
---|
189 |
|
---|
190 | p = TALLOC_ZERO_ARRAY(mem_ctx, struct client_pipe_connection, 1);
|
---|
191 | if (!p) {
|
---|
192 | return NT_STATUS_NO_MEMORY;
|
---|
193 | }
|
---|
194 |
|
---|
195 | status = cli_rpc_pipe_open_noauth(ipc->cli, interface, &p->pipe);
|
---|
196 | if (!NT_STATUS_IS_OK(status)) {
|
---|
197 | TALLOC_FREE(p);
|
---|
198 | return status;
|
---|
199 | }
|
---|
200 |
|
---|
201 | DLIST_ADD(ipc->pipe_connections, p);
|
---|
202 |
|
---|
203 | *presult = p->pipe;
|
---|
204 | return NT_STATUS_OK;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /********************************************************************
|
---|
208 | ********************************************************************/
|
---|
209 |
|
---|
210 | static NTSTATUS pipe_cm_open(TALLOC_CTX *ctx,
|
---|
211 | struct client_ipc_connection *ipc,
|
---|
212 | const struct ndr_syntax_id *interface,
|
---|
213 | struct rpc_pipe_client **presult)
|
---|
214 | {
|
---|
215 | if (NT_STATUS_IS_OK(pipe_cm_find(ipc, interface, presult))) {
|
---|
216 | return NT_STATUS_OK;
|
---|
217 | }
|
---|
218 |
|
---|
219 | return pipe_cm_connect(ctx, ipc, interface, presult);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /********************************************************************
|
---|
223 | ********************************************************************/
|
---|
224 |
|
---|
225 | WERROR libnetapi_open_pipe(struct libnetapi_ctx *ctx,
|
---|
226 | const char *server_name,
|
---|
227 | const struct ndr_syntax_id *interface,
|
---|
228 | struct rpc_pipe_client **presult)
|
---|
229 | {
|
---|
230 | struct rpc_pipe_client *result = NULL;
|
---|
231 | NTSTATUS status;
|
---|
232 | WERROR werr;
|
---|
233 | struct client_ipc_connection *ipc = NULL;
|
---|
234 |
|
---|
235 | if (!presult) {
|
---|
236 | return WERR_INVALID_PARAM;
|
---|
237 | }
|
---|
238 |
|
---|
239 | werr = libnetapi_open_ipc_connection(ctx, server_name, &ipc);
|
---|
240 | if (!W_ERROR_IS_OK(werr)) {
|
---|
241 | return werr;
|
---|
242 | }
|
---|
243 |
|
---|
244 | status = pipe_cm_open(ctx, ipc, interface, &result);
|
---|
245 | if (!NT_STATUS_IS_OK(status)) {
|
---|
246 | libnetapi_set_error_string(ctx, "failed to open PIPE %s: %s",
|
---|
247 | get_pipe_name_from_syntax(talloc_tos(), interface),
|
---|
248 | get_friendly_nt_error_msg(status));
|
---|
249 | return WERR_DEST_NOT_FOUND;
|
---|
250 | }
|
---|
251 |
|
---|
252 | *presult = result;
|
---|
253 |
|
---|
254 | return WERR_OK;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /********************************************************************
|
---|
258 | ********************************************************************/
|
---|
259 |
|
---|
260 | WERROR libnetapi_get_binding_handle(struct libnetapi_ctx *ctx,
|
---|
261 | const char *server_name,
|
---|
262 | const struct ndr_syntax_id *interface,
|
---|
263 | struct dcerpc_binding_handle **binding_handle)
|
---|
264 | {
|
---|
265 | struct rpc_pipe_client *pipe_cli;
|
---|
266 | WERROR result;
|
---|
267 |
|
---|
268 | *binding_handle = NULL;
|
---|
269 |
|
---|
270 | result = libnetapi_open_pipe(ctx, server_name, interface, &pipe_cli);
|
---|
271 | if (!W_ERROR_IS_OK(result)) {
|
---|
272 | return result;
|
---|
273 | }
|
---|
274 |
|
---|
275 | *binding_handle = pipe_cli->binding_handle;
|
---|
276 |
|
---|
277 | return WERR_OK;
|
---|
278 | }
|
---|