1 | /*
|
---|
2 | Unix SMB/Netbios implementation.
|
---|
3 | Generic infrstructure for RPC Daemons
|
---|
4 | Copyright (C) Simo Sorce 2010
|
---|
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 "ntdomain.h"
|
---|
22 | #include "rpc_server/rpc_server.h"
|
---|
23 | #include "rpc_dce.h"
|
---|
24 | #include "librpc/gen_ndr/netlogon.h"
|
---|
25 | #include "librpc/gen_ndr/auth.h"
|
---|
26 | #include "lib/tsocket/tsocket.h"
|
---|
27 | #include "libcli/named_pipe_auth/npa_tstream.h"
|
---|
28 | #include "../auth/auth_sam_reply.h"
|
---|
29 | #include "auth.h"
|
---|
30 | #include "rpc_server/rpc_ncacn_np.h"
|
---|
31 | #include "rpc_server/srv_pipe_hnd.h"
|
---|
32 | #include "rpc_server/srv_pipe.h"
|
---|
33 |
|
---|
34 | #define SERVER_TCP_LOW_PORT 1024
|
---|
35 | #define SERVER_TCP_HIGH_PORT 1300
|
---|
36 |
|
---|
37 | static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
|
---|
38 | struct auth_session_info_transport **session_info)
|
---|
39 | {
|
---|
40 | struct auth_session_info_transport *i;
|
---|
41 | struct auth_serversupplied_info *s;
|
---|
42 | struct auth_user_info_dc *u;
|
---|
43 | union netr_Validation val;
|
---|
44 | NTSTATUS status;
|
---|
45 |
|
---|
46 | i = talloc_zero(mem_ctx, struct auth_session_info_transport);
|
---|
47 | if (i == NULL) {
|
---|
48 | return NT_STATUS_NO_MEMORY;
|
---|
49 | }
|
---|
50 |
|
---|
51 | status = make_server_info_guest(i, &s);
|
---|
52 | if (!NT_STATUS_IS_OK(status)) {
|
---|
53 | return status;
|
---|
54 | }
|
---|
55 |
|
---|
56 | i->security_token = s->security_token;
|
---|
57 | i->session_key = s->user_session_key;
|
---|
58 |
|
---|
59 | val.sam3 = s->info3;
|
---|
60 |
|
---|
61 | status = make_user_info_dc_netlogon_validation(mem_ctx,
|
---|
62 | "",
|
---|
63 | 3,
|
---|
64 | &val,
|
---|
65 | &u);
|
---|
66 | if (!NT_STATUS_IS_OK(status)) {
|
---|
67 | DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
|
---|
68 | return status;
|
---|
69 | }
|
---|
70 | i->info = talloc_move(i, &u->info);
|
---|
71 | talloc_free(u);
|
---|
72 |
|
---|
73 | *session_info = i;
|
---|
74 |
|
---|
75 | return NT_STATUS_OK;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* Creates a pipes_struct and initializes it with the information
|
---|
79 | * sent from the client */
|
---|
80 | static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
|
---|
81 | const char *pipe_name,
|
---|
82 | const struct ndr_syntax_id id,
|
---|
83 | enum dcerpc_transport_t transport,
|
---|
84 | bool ncalrpc_as_system,
|
---|
85 | const char *client_address,
|
---|
86 | const char *server_address,
|
---|
87 | struct auth_session_info_transport *session_info,
|
---|
88 | struct pipes_struct **_p,
|
---|
89 | int *perrno)
|
---|
90 | {
|
---|
91 | struct netr_SamInfo3 *info3;
|
---|
92 | struct auth_user_info_dc *auth_user_info_dc;
|
---|
93 | struct pipes_struct *p;
|
---|
94 | NTSTATUS status;
|
---|
95 | bool ok;
|
---|
96 |
|
---|
97 | p = talloc_zero(mem_ctx, struct pipes_struct);
|
---|
98 | if (!p) {
|
---|
99 | *perrno = ENOMEM;
|
---|
100 | return -1;
|
---|
101 | }
|
---|
102 | p->syntax = id;
|
---|
103 | p->transport = transport;
|
---|
104 | p->ncalrpc_as_system = ncalrpc_as_system;
|
---|
105 | p->allow_bind = true;
|
---|
106 |
|
---|
107 | p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
|
---|
108 | if (!p->mem_ctx) {
|
---|
109 | TALLOC_FREE(p);
|
---|
110 | *perrno = ENOMEM;
|
---|
111 | return -1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | ok = init_pipe_handles(p, &id);
|
---|
115 | if (!ok) {
|
---|
116 | DEBUG(1, ("Failed to init handles\n"));
|
---|
117 | TALLOC_FREE(p);
|
---|
118 | *perrno = EINVAL;
|
---|
119 | return -1;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | data_blob_free(&p->in_data.data);
|
---|
124 | data_blob_free(&p->in_data.pdu);
|
---|
125 |
|
---|
126 | p->endian = RPC_LITTLE_ENDIAN;
|
---|
127 |
|
---|
128 | /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
|
---|
129 | auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
|
---|
130 | if (!auth_user_info_dc) {
|
---|
131 | TALLOC_FREE(p);
|
---|
132 | *perrno = ENOMEM;
|
---|
133 | return -1;
|
---|
134 | }
|
---|
135 |
|
---|
136 | auth_user_info_dc->num_sids = session_info->security_token->num_sids;
|
---|
137 | auth_user_info_dc->sids = session_info->security_token->sids;
|
---|
138 | auth_user_info_dc->info = session_info->info;
|
---|
139 | auth_user_info_dc->user_session_key = session_info->session_key;
|
---|
140 |
|
---|
141 | /* This creates the input structure that make_server_info_info3 is looking for */
|
---|
142 | status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
|
---|
143 | &info3);
|
---|
144 |
|
---|
145 | if (!NT_STATUS_IS_OK(status)) {
|
---|
146 | DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
|
---|
147 | TALLOC_FREE(p);
|
---|
148 | *perrno = EINVAL;
|
---|
149 | return -1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | status = make_server_info_info3(p,
|
---|
153 | info3->base.account_name.string,
|
---|
154 | info3->base.domain.string,
|
---|
155 | &p->session_info, info3);
|
---|
156 | if (!NT_STATUS_IS_OK(status)) {
|
---|
157 | DEBUG(1, ("Failed to init server info\n"));
|
---|
158 | TALLOC_FREE(p);
|
---|
159 | *perrno = EINVAL;
|
---|
160 | return -1;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Some internal functions need a local token to determine access to
|
---|
165 | * resoutrces.
|
---|
166 | */
|
---|
167 | status = create_local_token(p->session_info);
|
---|
168 | if (!NT_STATUS_IS_OK(status)) {
|
---|
169 | DEBUG(1, ("Failed to init local auth token\n"));
|
---|
170 | TALLOC_FREE(p);
|
---|
171 | *perrno = EINVAL;
|
---|
172 | return -1;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /* Now override the session_info->security_token with the exact
|
---|
176 | * security_token we were given from the other side,
|
---|
177 | * regardless of what we just calculated */
|
---|
178 | p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
|
---|
179 |
|
---|
180 | /* Also set the session key to the correct value */
|
---|
181 | p->session_info->user_session_key = session_info->session_key;
|
---|
182 | p->session_info->user_session_key.data = talloc_move(p->session_info, &session_info->session_key.data);
|
---|
183 |
|
---|
184 | p->client_id = talloc_zero(p, struct client_address);
|
---|
185 | if (!p->client_id) {
|
---|
186 | TALLOC_FREE(p);
|
---|
187 | *perrno = ENOMEM;
|
---|
188 | return -1;
|
---|
189 | }
|
---|
190 | strlcpy(p->client_id->addr,
|
---|
191 | client_address, sizeof(p->client_id->addr));
|
---|
192 | p->client_id->name = talloc_strdup(p->client_id, client_address);
|
---|
193 | if (p->client_id->name == NULL) {
|
---|
194 | TALLOC_FREE(p);
|
---|
195 | *perrno = ENOMEM;
|
---|
196 | return -1;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (server_address != NULL) {
|
---|
200 | p->server_id = talloc_zero(p, struct client_address);
|
---|
201 | if (p->client_id == NULL) {
|
---|
202 | TALLOC_FREE(p);
|
---|
203 | *perrno = ENOMEM;
|
---|
204 | return -1;
|
---|
205 | }
|
---|
206 |
|
---|
207 | strlcpy(p->server_id->addr,
|
---|
208 | server_address,
|
---|
209 | sizeof(p->server_id->addr));
|
---|
210 |
|
---|
211 | p->server_id->name = talloc_strdup(p->server_id,
|
---|
212 | server_address);
|
---|
213 | if (p->server_id->name == NULL) {
|
---|
214 | TALLOC_FREE(p);
|
---|
215 | *perrno = ENOMEM;
|
---|
216 | return -1;
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
|
---|
221 |
|
---|
222 | *_p = p;
|
---|
223 | return 0;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /* Start listening on the appropriate unix socket and setup all is needed to
|
---|
227 | * dispatch requests to the pipes rpc implementation */
|
---|
228 |
|
---|
229 | struct dcerpc_ncacn_listen_state {
|
---|
230 | struct ndr_syntax_id syntax_id;
|
---|
231 |
|
---|
232 | int fd;
|
---|
233 | union {
|
---|
234 | char *name;
|
---|
235 | uint16_t port;
|
---|
236 | } ep;
|
---|
237 |
|
---|
238 | struct tevent_context *ev_ctx;
|
---|
239 | struct messaging_context *msg_ctx;
|
---|
240 | dcerpc_ncacn_disconnect_fn disconnect_fn;
|
---|
241 | };
|
---|
242 |
|
---|
243 | static void named_pipe_listener(struct tevent_context *ev,
|
---|
244 | struct tevent_fd *fde,
|
---|
245 | uint16_t flags,
|
---|
246 | void *private_data);
|
---|
247 |
|
---|
248 | bool setup_named_pipe_socket(const char *pipe_name,
|
---|
249 | struct tevent_context *ev_ctx)
|
---|
250 | {
|
---|
251 | struct dcerpc_ncacn_listen_state *state;
|
---|
252 | struct tevent_fd *fde;
|
---|
253 | char *np_dir;
|
---|
254 |
|
---|
255 | state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
|
---|
256 | if (!state) {
|
---|
257 | DEBUG(0, ("Out of memory\n"));
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 | state->ep.name = talloc_strdup(state, pipe_name);
|
---|
261 | if (state->ep.name == NULL) {
|
---|
262 | DEBUG(0, ("Out of memory\n"));
|
---|
263 | goto out;
|
---|
264 | }
|
---|
265 | state->fd = -1;
|
---|
266 |
|
---|
267 | /*
|
---|
268 | * As lp_ncalrpc_dir() should have 0755, but
|
---|
269 | * lp_ncalrpc_dir()/np should have 0700, we need to
|
---|
270 | * create lp_ncalrpc_dir() first.
|
---|
271 | */
|
---|
272 | if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
|
---|
273 | DEBUG(0, ("Failed to create pipe directory %s - %s\n",
|
---|
274 | lp_ncalrpc_dir(), strerror(errno)));
|
---|
275 | goto out;
|
---|
276 | }
|
---|
277 |
|
---|
278 | np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
|
---|
279 | if (!np_dir) {
|
---|
280 | DEBUG(0, ("Out of memory\n"));
|
---|
281 | goto out;
|
---|
282 | }
|
---|
283 |
|
---|
284 | if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
|
---|
285 | DEBUG(0, ("Failed to create pipe directory %s - %s\n",
|
---|
286 | np_dir, strerror(errno)));
|
---|
287 | goto out;
|
---|
288 | }
|
---|
289 |
|
---|
290 | state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
|
---|
291 | if (state->fd == -1) {
|
---|
292 | DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
|
---|
293 | np_dir, pipe_name));
|
---|
294 | goto out;
|
---|
295 | }
|
---|
296 |
|
---|
297 | DEBUG(10, ("Openened pipe socket fd %d for %s\n",
|
---|
298 | state->fd, pipe_name));
|
---|
299 |
|
---|
300 | fde = tevent_add_fd(ev_ctx,
|
---|
301 | state, state->fd, TEVENT_FD_READ,
|
---|
302 | named_pipe_listener, state);
|
---|
303 | if (!fde) {
|
---|
304 | DEBUG(0, ("Failed to add event handler!\n"));
|
---|
305 | goto out;
|
---|
306 | }
|
---|
307 |
|
---|
308 | tevent_fd_set_auto_close(fde);
|
---|
309 | return true;
|
---|
310 |
|
---|
311 | out:
|
---|
312 | if (state->fd != -1) {
|
---|
313 | close(state->fd);
|
---|
314 | }
|
---|
315 | TALLOC_FREE(state);
|
---|
316 | return false;
|
---|
317 | }
|
---|
318 |
|
---|
319 | static void named_pipe_accept_function(const char *pipe_name, int fd);
|
---|
320 |
|
---|
321 | static void named_pipe_listener(struct tevent_context *ev,
|
---|
322 | struct tevent_fd *fde,
|
---|
323 | uint16_t flags,
|
---|
324 | void *private_data)
|
---|
325 | {
|
---|
326 | struct dcerpc_ncacn_listen_state *state =
|
---|
327 | talloc_get_type_abort(private_data,
|
---|
328 | struct dcerpc_ncacn_listen_state);
|
---|
329 | struct sockaddr_un sunaddr;
|
---|
330 | socklen_t len;
|
---|
331 | int sd = -1;
|
---|
332 |
|
---|
333 | /* TODO: should we have a limit to the number of clients ? */
|
---|
334 |
|
---|
335 | len = sizeof(sunaddr);
|
---|
336 |
|
---|
337 | sd = accept(state->fd,
|
---|
338 | (struct sockaddr *)(void *)&sunaddr, &len);
|
---|
339 |
|
---|
340 | if (sd == -1) {
|
---|
341 | if (errno != EINTR) {
|
---|
342 | DEBUG(6, ("Failed to get a valid socket [%s]\n",
|
---|
343 | strerror(errno)));
|
---|
344 | }
|
---|
345 | return;
|
---|
346 | }
|
---|
347 |
|
---|
348 | DEBUG(6, ("Accepted socket %d\n", sd));
|
---|
349 |
|
---|
350 | named_pipe_accept_function(state->ep.name, sd);
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | /* This is the core of the rpc server.
|
---|
355 | * Accepts connections from clients and process requests using the appropriate
|
---|
356 | * dispatcher table. */
|
---|
357 |
|
---|
358 | struct named_pipe_client {
|
---|
359 | const char *pipe_name;
|
---|
360 | struct ndr_syntax_id pipe_id;
|
---|
361 |
|
---|
362 | struct tevent_context *ev;
|
---|
363 | struct messaging_context *msg_ctx;
|
---|
364 |
|
---|
365 | uint16_t file_type;
|
---|
366 | uint16_t device_state;
|
---|
367 | uint64_t allocation_size;
|
---|
368 |
|
---|
369 | struct tstream_context *tstream;
|
---|
370 |
|
---|
371 | struct tsocket_address *client;
|
---|
372 | char *client_name;
|
---|
373 | struct tsocket_address *server;
|
---|
374 | char *server_name;
|
---|
375 | struct auth_session_info_transport *session_info;
|
---|
376 |
|
---|
377 | struct pipes_struct *p;
|
---|
378 |
|
---|
379 | struct tevent_queue *write_queue;
|
---|
380 |
|
---|
381 | struct iovec *iov;
|
---|
382 | size_t count;
|
---|
383 | };
|
---|
384 |
|
---|
385 | static void named_pipe_accept_done(struct tevent_req *subreq);
|
---|
386 |
|
---|
387 | static void named_pipe_accept_function(const char *pipe_name, int fd)
|
---|
388 | {
|
---|
389 | struct ndr_syntax_id syntax;
|
---|
390 | struct named_pipe_client *npc;
|
---|
391 | struct tstream_context *plain;
|
---|
392 | struct tevent_req *subreq;
|
---|
393 | bool ok;
|
---|
394 | int ret;
|
---|
395 |
|
---|
396 | ok = is_known_pipename(pipe_name, &syntax);
|
---|
397 | if (!ok) {
|
---|
398 | DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
|
---|
399 | close(fd);
|
---|
400 | return;
|
---|
401 | }
|
---|
402 |
|
---|
403 | npc = talloc_zero(NULL, struct named_pipe_client);
|
---|
404 | if (!npc) {
|
---|
405 | DEBUG(0, ("Out of memory!\n"));
|
---|
406 | close(fd);
|
---|
407 | return;
|
---|
408 | }
|
---|
409 | npc->pipe_name = pipe_name;
|
---|
410 | npc->pipe_id = syntax;
|
---|
411 | npc->ev = server_event_context();
|
---|
412 | npc->msg_ctx = server_messaging_context();
|
---|
413 |
|
---|
414 | /* make sure socket is in NON blocking state */
|
---|
415 | ret = set_blocking(fd, false);
|
---|
416 | if (ret != 0) {
|
---|
417 | DEBUG(2, ("Failed to make socket non-blocking\n"));
|
---|
418 | TALLOC_FREE(npc);
|
---|
419 | close(fd);
|
---|
420 | return;
|
---|
421 | }
|
---|
422 |
|
---|
423 | ret = tstream_bsd_existing_socket(npc, fd, &plain);
|
---|
424 | if (ret != 0) {
|
---|
425 | DEBUG(2, ("Failed to create tstream socket\n"));
|
---|
426 | TALLOC_FREE(npc);
|
---|
427 | close(fd);
|
---|
428 | return;
|
---|
429 | }
|
---|
430 |
|
---|
431 | npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
|
---|
432 | npc->device_state = 0xff | 0x0400 | 0x0100;
|
---|
433 | npc->allocation_size = 4096;
|
---|
434 |
|
---|
435 | subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
|
---|
436 | npc->file_type,
|
---|
437 | npc->device_state,
|
---|
438 | npc->allocation_size);
|
---|
439 | if (!subreq) {
|
---|
440 | DEBUG(2, ("Failed to start async accept procedure\n"));
|
---|
441 | TALLOC_FREE(npc);
|
---|
442 | close(fd);
|
---|
443 | return;
|
---|
444 | }
|
---|
445 | tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
|
---|
446 | }
|
---|
447 |
|
---|
448 | static void named_pipe_packet_process(struct tevent_req *subreq);
|
---|
449 | static void named_pipe_packet_done(struct tevent_req *subreq);
|
---|
450 |
|
---|
451 | static void named_pipe_accept_done(struct tevent_req *subreq)
|
---|
452 | {
|
---|
453 | struct named_pipe_client *npc =
|
---|
454 | tevent_req_callback_data(subreq, struct named_pipe_client);
|
---|
455 | const char *cli_addr;
|
---|
456 | int error;
|
---|
457 | int ret;
|
---|
458 |
|
---|
459 | ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
|
---|
460 | &npc->tstream,
|
---|
461 | &npc->client,
|
---|
462 | &npc->client_name,
|
---|
463 | &npc->server,
|
---|
464 | &npc->server_name,
|
---|
465 | &npc->session_info);
|
---|
466 | TALLOC_FREE(subreq);
|
---|
467 | if (ret != 0) {
|
---|
468 | DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
|
---|
469 | strerror(error)));
|
---|
470 | TALLOC_FREE(npc);
|
---|
471 | return;
|
---|
472 | }
|
---|
473 |
|
---|
474 | if (tsocket_address_is_inet(npc->client, "ip")) {
|
---|
475 | cli_addr = tsocket_address_inet_addr_string(npc->client,
|
---|
476 | subreq);
|
---|
477 | if (cli_addr == NULL) {
|
---|
478 | TALLOC_FREE(npc);
|
---|
479 | return;
|
---|
480 | }
|
---|
481 | } else {
|
---|
482 | cli_addr = "";
|
---|
483 | }
|
---|
484 |
|
---|
485 | ret = make_server_pipes_struct(npc,
|
---|
486 | npc->pipe_name, npc->pipe_id, NCACN_NP,
|
---|
487 | false, cli_addr, NULL, npc->session_info,
|
---|
488 | &npc->p, &error);
|
---|
489 | if (ret != 0) {
|
---|
490 | DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
|
---|
491 | strerror(error)));
|
---|
492 | goto fail;
|
---|
493 | }
|
---|
494 | npc->p->msg_ctx = npc->msg_ctx;
|
---|
495 |
|
---|
496 | npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
|
---|
497 | if (!npc->write_queue) {
|
---|
498 | DEBUG(2, ("Failed to set up write queue!\n"));
|
---|
499 | goto fail;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /* And now start receaving and processing packets */
|
---|
503 | subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
|
---|
504 | if (!subreq) {
|
---|
505 | DEBUG(2, ("Failed to start receving packets\n"));
|
---|
506 | goto fail;
|
---|
507 | }
|
---|
508 | tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
|
---|
509 | return;
|
---|
510 |
|
---|
511 | fail:
|
---|
512 | DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
|
---|
513 | npc->client_name));
|
---|
514 | /* terminate client connection */
|
---|
515 | talloc_free(npc);
|
---|
516 | return;
|
---|
517 | }
|
---|
518 |
|
---|
519 | static void named_pipe_packet_process(struct tevent_req *subreq)
|
---|
520 | {
|
---|
521 | struct named_pipe_client *npc =
|
---|
522 | tevent_req_callback_data(subreq, struct named_pipe_client);
|
---|
523 | struct _output_data *out = &npc->p->out_data;
|
---|
524 | DATA_BLOB recv_buffer = data_blob_null;
|
---|
525 | struct ncacn_packet *pkt;
|
---|
526 | NTSTATUS status;
|
---|
527 | ssize_t data_left;
|
---|
528 | ssize_t data_used;
|
---|
529 | char *data;
|
---|
530 | uint32_t to_send;
|
---|
531 | bool ok;
|
---|
532 |
|
---|
533 | status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
|
---|
534 | TALLOC_FREE(subreq);
|
---|
535 | if (!NT_STATUS_IS_OK(status)) {
|
---|
536 | goto fail;
|
---|
537 | }
|
---|
538 |
|
---|
539 | data_left = recv_buffer.length;
|
---|
540 | data = (char *)recv_buffer.data;
|
---|
541 |
|
---|
542 | while (data_left) {
|
---|
543 |
|
---|
544 | data_used = process_incoming_data(npc->p, data, data_left);
|
---|
545 | if (data_used < 0) {
|
---|
546 | DEBUG(3, ("Failed to process dceprc request!\n"));
|
---|
547 | status = NT_STATUS_UNEXPECTED_IO_ERROR;
|
---|
548 | goto fail;
|
---|
549 | }
|
---|
550 |
|
---|
551 | data_left -= data_used;
|
---|
552 | data += data_used;
|
---|
553 | }
|
---|
554 |
|
---|
555 | /* Do not leak this buffer, npc is a long lived context */
|
---|
556 | talloc_free(recv_buffer.data);
|
---|
557 | talloc_free(pkt);
|
---|
558 |
|
---|
559 | /* this is needed because of the way DCERPC Binds work in
|
---|
560 | * the RPC marshalling code */
|
---|
561 | to_send = out->frag.length - out->current_pdu_sent;
|
---|
562 | if (to_send > 0) {
|
---|
563 |
|
---|
564 | DEBUG(10, ("Current_pdu_len = %u, "
|
---|
565 | "current_pdu_sent = %u "
|
---|
566 | "Returning %u bytes\n",
|
---|
567 | (unsigned int)out->frag.length,
|
---|
568 | (unsigned int)out->current_pdu_sent,
|
---|
569 | (unsigned int)to_send));
|
---|
570 |
|
---|
571 | npc->iov = talloc_zero(npc, struct iovec);
|
---|
572 | if (!npc->iov) {
|
---|
573 | status = NT_STATUS_NO_MEMORY;
|
---|
574 | goto fail;
|
---|
575 | }
|
---|
576 | npc->count = 1;
|
---|
577 |
|
---|
578 | npc->iov[0].iov_base = out->frag.data
|
---|
579 | + out->current_pdu_sent;
|
---|
580 | npc->iov[0].iov_len = to_send;
|
---|
581 |
|
---|
582 | out->current_pdu_sent += to_send;
|
---|
583 | }
|
---|
584 |
|
---|
585 | /* this condition is false for bind packets, or when we haven't
|
---|
586 | * yet got a full request, and need to wait for more data from
|
---|
587 | * the client */
|
---|
588 | while (out->data_sent_length < out->rdata.length) {
|
---|
589 |
|
---|
590 | ok = create_next_pdu(npc->p);
|
---|
591 | if (!ok) {
|
---|
592 | DEBUG(3, ("Failed to create next PDU!\n"));
|
---|
593 | status = NT_STATUS_UNEXPECTED_IO_ERROR;
|
---|
594 | goto fail;
|
---|
595 | }
|
---|
596 |
|
---|
597 | npc->iov = talloc_realloc(npc, npc->iov,
|
---|
598 | struct iovec, npc->count + 1);
|
---|
599 | if (!npc->iov) {
|
---|
600 | status = NT_STATUS_NO_MEMORY;
|
---|
601 | goto fail;
|
---|
602 | }
|
---|
603 |
|
---|
604 | npc->iov[npc->count].iov_base = out->frag.data;
|
---|
605 | npc->iov[npc->count].iov_len = out->frag.length;
|
---|
606 |
|
---|
607 | DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
|
---|
608 | (unsigned int)npc->count,
|
---|
609 | (unsigned int)npc->iov[npc->count].iov_len));
|
---|
610 | dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
|
---|
611 | npc->iov[npc->count].iov_len);
|
---|
612 | npc->count++;
|
---|
613 | }
|
---|
614 |
|
---|
615 | /* we still don't have a complete request, go back and wait for more
|
---|
616 | * data */
|
---|
617 | if (npc->count == 0) {
|
---|
618 | /* Wait for the next packet */
|
---|
619 | subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
|
---|
620 | if (!subreq) {
|
---|
621 | DEBUG(2, ("Failed to start receving packets\n"));
|
---|
622 | status = NT_STATUS_NO_MEMORY;
|
---|
623 | goto fail;
|
---|
624 | }
|
---|
625 | tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
|
---|
626 | return;
|
---|
627 | }
|
---|
628 |
|
---|
629 | DEBUG(10, ("Sending a total of %u bytes\n",
|
---|
630 | (unsigned int)npc->p->out_data.data_sent_length));
|
---|
631 |
|
---|
632 | subreq = tstream_writev_queue_send(npc, npc->ev,
|
---|
633 | npc->tstream,
|
---|
634 | npc->write_queue,
|
---|
635 | npc->iov, npc->count);
|
---|
636 | if (!subreq) {
|
---|
637 | DEBUG(2, ("Failed to send packet\n"));
|
---|
638 | status = NT_STATUS_NO_MEMORY;
|
---|
639 | goto fail;
|
---|
640 | }
|
---|
641 | tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
|
---|
642 | return;
|
---|
643 |
|
---|
644 | fail:
|
---|
645 | DEBUG(2, ("Fatal error(%s). "
|
---|
646 | "Terminating client(%s) connection!\n",
|
---|
647 | nt_errstr(status), npc->client_name));
|
---|
648 | /* terminate client connection */
|
---|
649 | talloc_free(npc);
|
---|
650 | return;
|
---|
651 | }
|
---|
652 |
|
---|
653 | static void named_pipe_packet_done(struct tevent_req *subreq)
|
---|
654 | {
|
---|
655 | struct named_pipe_client *npc =
|
---|
656 | tevent_req_callback_data(subreq, struct named_pipe_client);
|
---|
657 | int sys_errno;
|
---|
658 | int ret;
|
---|
659 |
|
---|
660 | ret = tstream_writev_queue_recv(subreq, &sys_errno);
|
---|
661 | TALLOC_FREE(subreq);
|
---|
662 | if (ret == -1) {
|
---|
663 | DEBUG(2, ("Writev failed!\n"));
|
---|
664 | goto fail;
|
---|
665 | }
|
---|
666 |
|
---|
667 | if (npc->p->fault_state != 0) {
|
---|
668 | DEBUG(2, ("Disconnect after fault\n"));
|
---|
669 | sys_errno = EINVAL;
|
---|
670 | goto fail;
|
---|
671 | }
|
---|
672 |
|
---|
673 | /* clear out any data that may have been left around */
|
---|
674 | npc->count = 0;
|
---|
675 | TALLOC_FREE(npc->iov);
|
---|
676 | data_blob_free(&npc->p->in_data.data);
|
---|
677 | data_blob_free(&npc->p->out_data.frag);
|
---|
678 | data_blob_free(&npc->p->out_data.rdata);
|
---|
679 |
|
---|
680 | /* Wait for the next packet */
|
---|
681 | subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
|
---|
682 | if (!subreq) {
|
---|
683 | DEBUG(2, ("Failed to start receving packets\n"));
|
---|
684 | sys_errno = ENOMEM;
|
---|
685 | goto fail;
|
---|
686 | }
|
---|
687 | tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
|
---|
688 | return;
|
---|
689 |
|
---|
690 | fail:
|
---|
691 | DEBUG(2, ("Fatal error(%s). "
|
---|
692 | "Terminating client(%s) connection!\n",
|
---|
693 | strerror(sys_errno), npc->client_name));
|
---|
694 | /* terminate client connection */
|
---|
695 | talloc_free(npc);
|
---|
696 | return;
|
---|
697 | }
|
---|
698 |
|
---|
699 | static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
|
---|
700 | struct messaging_context *msg_ctx,
|
---|
701 | struct ndr_syntax_id syntax_id,
|
---|
702 | enum dcerpc_transport_t transport,
|
---|
703 | const char *name,
|
---|
704 | uint16_t port,
|
---|
705 | struct tsocket_address *cli_addr,
|
---|
706 | struct tsocket_address *srv_addr,
|
---|
707 | int s,
|
---|
708 | dcerpc_ncacn_disconnect_fn fn);
|
---|
709 |
|
---|
710 | /********************************************************************
|
---|
711 | * Start listening on the tcp/ip socket
|
---|
712 | ********************************************************************/
|
---|
713 |
|
---|
714 | static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
|
---|
715 | struct tevent_fd *fde,
|
---|
716 | uint16_t flags,
|
---|
717 | void *private_data);
|
---|
718 |
|
---|
719 | uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
|
---|
720 | struct messaging_context *msg_ctx,
|
---|
721 | struct ndr_syntax_id syntax_id,
|
---|
722 | const struct sockaddr_storage *ifss,
|
---|
723 | uint16_t port)
|
---|
724 | {
|
---|
725 | struct dcerpc_ncacn_listen_state *state;
|
---|
726 | struct tevent_fd *fde;
|
---|
727 | int rc;
|
---|
728 |
|
---|
729 | state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
|
---|
730 | if (state == NULL) {
|
---|
731 | DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
|
---|
732 | return 0;
|
---|
733 | }
|
---|
734 |
|
---|
735 | state->syntax_id = syntax_id;
|
---|
736 | state->fd = -1;
|
---|
737 | state->ep.port = port;
|
---|
738 | state->disconnect_fn = NULL;
|
---|
739 |
|
---|
740 | if (state->ep.port == 0) {
|
---|
741 | uint16_t i;
|
---|
742 |
|
---|
743 | for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
|
---|
744 | state->fd = open_socket_in(SOCK_STREAM,
|
---|
745 | i,
|
---|
746 | 0,
|
---|
747 | ifss,
|
---|
748 | false);
|
---|
749 | if (state->fd > 0) {
|
---|
750 | state->ep.port = i;
|
---|
751 | break;
|
---|
752 | }
|
---|
753 | }
|
---|
754 | } else {
|
---|
755 | state->fd = open_socket_in(SOCK_STREAM,
|
---|
756 | state->ep.port,
|
---|
757 | 0,
|
---|
758 | ifss,
|
---|
759 | true);
|
---|
760 | }
|
---|
761 | if (state->fd == -1) {
|
---|
762 | DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
|
---|
763 | "socket on port %u!\n", state->ep.port));
|
---|
764 | goto out;
|
---|
765 | }
|
---|
766 |
|
---|
767 | state->ev_ctx = ev_ctx;
|
---|
768 | state->msg_ctx = msg_ctx;
|
---|
769 |
|
---|
770 | /* ready to listen */
|
---|
771 | set_socket_options(state->fd, "SO_KEEPALIVE");
|
---|
772 | set_socket_options(state->fd, lp_socket_options());
|
---|
773 |
|
---|
774 | /* Set server socket to non-blocking for the accept. */
|
---|
775 | set_blocking(state->fd, false);
|
---|
776 |
|
---|
777 | rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
|
---|
778 | if (rc == -1) {
|
---|
779 | DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
|
---|
780 | goto out;
|
---|
781 | }
|
---|
782 |
|
---|
783 | DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
|
---|
784 | state->fd, state->ep.port));
|
---|
785 |
|
---|
786 | fde = tevent_add_fd(state->ev_ctx,
|
---|
787 | state,
|
---|
788 | state->fd,
|
---|
789 | TEVENT_FD_READ,
|
---|
790 | dcerpc_ncacn_tcpip_listener,
|
---|
791 | state);
|
---|
792 | if (fde == NULL) {
|
---|
793 | DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
|
---|
794 | goto out;
|
---|
795 | }
|
---|
796 |
|
---|
797 | tevent_fd_set_auto_close(fde);
|
---|
798 |
|
---|
799 | return state->ep.port;
|
---|
800 | out:
|
---|
801 | if (state->fd != -1) {
|
---|
802 | close(state->fd);
|
---|
803 | }
|
---|
804 | TALLOC_FREE(state);
|
---|
805 |
|
---|
806 | return 0;
|
---|
807 | }
|
---|
808 |
|
---|
809 | static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
|
---|
810 | struct tevent_fd *fde,
|
---|
811 | uint16_t flags,
|
---|
812 | void *private_data)
|
---|
813 | {
|
---|
814 | struct dcerpc_ncacn_listen_state *state =
|
---|
815 | talloc_get_type_abort(private_data,
|
---|
816 | struct dcerpc_ncacn_listen_state);
|
---|
817 | struct tsocket_address *cli_addr = NULL;
|
---|
818 | struct tsocket_address *srv_addr = NULL;
|
---|
819 | struct sockaddr_storage addr;
|
---|
820 | socklen_t in_addrlen = sizeof(addr);
|
---|
821 | int s = -1;
|
---|
822 | int rc;
|
---|
823 |
|
---|
824 | s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
|
---|
825 | if (s == -1) {
|
---|
826 | if (errno != EINTR) {
|
---|
827 | DEBUG(0,("tcpip_listener accept: %s\n",
|
---|
828 | strerror(errno)));
|
---|
829 | }
|
---|
830 | return;
|
---|
831 | }
|
---|
832 |
|
---|
833 | rc = tsocket_address_bsd_from_sockaddr(state,
|
---|
834 | (struct sockaddr *)(void *) &addr,
|
---|
835 | in_addrlen,
|
---|
836 | &cli_addr);
|
---|
837 | if (rc < 0) {
|
---|
838 | close(s);
|
---|
839 | return;
|
---|
840 | }
|
---|
841 |
|
---|
842 | rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
|
---|
843 | if (rc < 0) {
|
---|
844 | close(s);
|
---|
845 | return;
|
---|
846 | }
|
---|
847 |
|
---|
848 | rc = tsocket_address_bsd_from_sockaddr(state,
|
---|
849 | (struct sockaddr *)(void *) &addr,
|
---|
850 | in_addrlen,
|
---|
851 | &srv_addr);
|
---|
852 | if (rc < 0) {
|
---|
853 | close(s);
|
---|
854 | return;
|
---|
855 | }
|
---|
856 |
|
---|
857 | DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
|
---|
858 |
|
---|
859 | dcerpc_ncacn_accept(state->ev_ctx,
|
---|
860 | state->msg_ctx,
|
---|
861 | state->syntax_id,
|
---|
862 | NCACN_IP_TCP,
|
---|
863 | NULL,
|
---|
864 | state->ep.port,
|
---|
865 | cli_addr,
|
---|
866 | srv_addr,
|
---|
867 | s,
|
---|
868 | NULL);
|
---|
869 | }
|
---|
870 |
|
---|
871 | /********************************************************************
|
---|
872 | * Start listening on the ncalrpc socket
|
---|
873 | ********************************************************************/
|
---|
874 |
|
---|
875 | static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
|
---|
876 | struct tevent_fd *fde,
|
---|
877 | uint16_t flags,
|
---|
878 | void *private_data);
|
---|
879 |
|
---|
880 | bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
|
---|
881 | struct messaging_context *msg_ctx,
|
---|
882 | struct ndr_syntax_id syntax_id,
|
---|
883 | const char *name,
|
---|
884 | dcerpc_ncacn_disconnect_fn fn)
|
---|
885 | {
|
---|
886 | struct dcerpc_ncacn_listen_state *state;
|
---|
887 | struct tevent_fd *fde;
|
---|
888 |
|
---|
889 | state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
|
---|
890 | if (state == NULL) {
|
---|
891 | DEBUG(0, ("Out of memory\n"));
|
---|
892 | return false;
|
---|
893 | }
|
---|
894 |
|
---|
895 | state->syntax_id = syntax_id;
|
---|
896 | state->fd = -1;
|
---|
897 | state->disconnect_fn = fn;
|
---|
898 |
|
---|
899 | if (name == NULL) {
|
---|
900 | name = "DEFAULT";
|
---|
901 | }
|
---|
902 | state->ep.name = talloc_strdup(state, name);
|
---|
903 |
|
---|
904 | if (state->ep.name == NULL) {
|
---|
905 | DEBUG(0, ("Out of memory\n"));
|
---|
906 | talloc_free(state);
|
---|
907 | return false;
|
---|
908 | }
|
---|
909 |
|
---|
910 | if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
|
---|
911 | DEBUG(0, ("Failed to create pipe directory %s - %s\n",
|
---|
912 | lp_ncalrpc_dir(), strerror(errno)));
|
---|
913 | goto out;
|
---|
914 | }
|
---|
915 |
|
---|
916 | state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
|
---|
917 | if (state->fd == -1) {
|
---|
918 | DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
|
---|
919 | lp_ncalrpc_dir(), name));
|
---|
920 | goto out;
|
---|
921 | }
|
---|
922 |
|
---|
923 | DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
|
---|
924 |
|
---|
925 | state->ev_ctx = ev_ctx;
|
---|
926 | state->msg_ctx = msg_ctx;
|
---|
927 |
|
---|
928 | /* Set server socket to non-blocking for the accept. */
|
---|
929 | set_blocking(state->fd, false);
|
---|
930 |
|
---|
931 | fde = tevent_add_fd(state->ev_ctx,
|
---|
932 | state,
|
---|
933 | state->fd,
|
---|
934 | TEVENT_FD_READ,
|
---|
935 | dcerpc_ncalrpc_listener,
|
---|
936 | state);
|
---|
937 | if (fde == NULL) {
|
---|
938 | DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
|
---|
939 | goto out;
|
---|
940 | }
|
---|
941 |
|
---|
942 | tevent_fd_set_auto_close(fde);
|
---|
943 |
|
---|
944 | return true;
|
---|
945 | out:
|
---|
946 | if (state->fd != -1) {
|
---|
947 | close(state->fd);
|
---|
948 | }
|
---|
949 | TALLOC_FREE(state);
|
---|
950 |
|
---|
951 | return 0;
|
---|
952 | }
|
---|
953 |
|
---|
954 | static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
|
---|
955 | struct tevent_fd *fde,
|
---|
956 | uint16_t flags,
|
---|
957 | void *private_data)
|
---|
958 | {
|
---|
959 | struct dcerpc_ncacn_listen_state *state =
|
---|
960 | talloc_get_type_abort(private_data,
|
---|
961 | struct dcerpc_ncacn_listen_state);
|
---|
962 | struct tsocket_address *cli_addr = NULL;
|
---|
963 | struct sockaddr_un sunaddr;
|
---|
964 | struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
|
---|
965 | socklen_t len = sizeof(sunaddr);
|
---|
966 | int sd = -1;
|
---|
967 | int rc;
|
---|
968 |
|
---|
969 | ZERO_STRUCT(sunaddr);
|
---|
970 |
|
---|
971 | sd = accept(state->fd, addr, &len);
|
---|
972 | if (sd == -1) {
|
---|
973 | if (errno != EINTR) {
|
---|
974 | DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
|
---|
975 | }
|
---|
976 | return;
|
---|
977 | }
|
---|
978 |
|
---|
979 | rc = tsocket_address_bsd_from_sockaddr(state,
|
---|
980 | addr, len,
|
---|
981 | &cli_addr);
|
---|
982 | if (rc < 0) {
|
---|
983 | close(sd);
|
---|
984 | return;
|
---|
985 | }
|
---|
986 |
|
---|
987 | DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
|
---|
988 |
|
---|
989 | dcerpc_ncacn_accept(state->ev_ctx,
|
---|
990 | state->msg_ctx,
|
---|
991 | state->syntax_id, NCALRPC,
|
---|
992 | state->ep.name, 0,
|
---|
993 | cli_addr, NULL, sd,
|
---|
994 | state->disconnect_fn);
|
---|
995 | }
|
---|
996 |
|
---|
997 | struct dcerpc_ncacn_conn {
|
---|
998 | struct ndr_syntax_id syntax_id;
|
---|
999 |
|
---|
1000 | enum dcerpc_transport_t transport;
|
---|
1001 |
|
---|
1002 | union {
|
---|
1003 | const char *name;
|
---|
1004 | uint16_t port;
|
---|
1005 | } ep;
|
---|
1006 |
|
---|
1007 | int sock;
|
---|
1008 |
|
---|
1009 | struct pipes_struct *p;
|
---|
1010 | dcerpc_ncacn_disconnect_fn disconnect_fn;
|
---|
1011 |
|
---|
1012 | struct tevent_context *ev_ctx;
|
---|
1013 | struct messaging_context *msg_ctx;
|
---|
1014 |
|
---|
1015 | struct tstream_context *tstream;
|
---|
1016 | struct tevent_queue *send_queue;
|
---|
1017 |
|
---|
1018 | struct tsocket_address *client;
|
---|
1019 | char *client_name;
|
---|
1020 | struct tsocket_address *server;
|
---|
1021 | char *server_name;
|
---|
1022 | struct auth_session_info_transport *session_info;
|
---|
1023 |
|
---|
1024 | struct iovec *iov;
|
---|
1025 | size_t count;
|
---|
1026 | };
|
---|
1027 |
|
---|
1028 | static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
|
---|
1029 | static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
|
---|
1030 |
|
---|
1031 | static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
|
---|
1032 | struct messaging_context *msg_ctx,
|
---|
1033 | struct ndr_syntax_id syntax_id,
|
---|
1034 | enum dcerpc_transport_t transport,
|
---|
1035 | const char *name,
|
---|
1036 | uint16_t port,
|
---|
1037 | struct tsocket_address *cli_addr,
|
---|
1038 | struct tsocket_address *srv_addr,
|
---|
1039 | int s,
|
---|
1040 | dcerpc_ncacn_disconnect_fn fn) {
|
---|
1041 | struct dcerpc_ncacn_conn *ncacn_conn;
|
---|
1042 | struct tevent_req *subreq;
|
---|
1043 | const char *cli_str;
|
---|
1044 | const char *srv_str = NULL;
|
---|
1045 | bool system_user = false;
|
---|
1046 | char *pipe_name;
|
---|
1047 | NTSTATUS status;
|
---|
1048 | int sys_errno;
|
---|
1049 | uid_t uid;
|
---|
1050 | int rc;
|
---|
1051 |
|
---|
1052 | DEBUG(10, ("dcerpc_ncacn_accept\n"));
|
---|
1053 |
|
---|
1054 | ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
|
---|
1055 | if (ncacn_conn == NULL) {
|
---|
1056 | DEBUG(0, ("Out of memory!\n"));
|
---|
1057 | close(s);
|
---|
1058 | return;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | ncacn_conn->transport = transport;
|
---|
1062 | ncacn_conn->syntax_id = syntax_id;
|
---|
1063 | ncacn_conn->ev_ctx = ev_ctx;
|
---|
1064 | ncacn_conn->msg_ctx = msg_ctx;
|
---|
1065 | ncacn_conn->sock = s;
|
---|
1066 | ncacn_conn->disconnect_fn = fn;
|
---|
1067 |
|
---|
1068 | ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
|
---|
1069 | if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
|
---|
1070 | ncacn_conn->client_name =
|
---|
1071 | tsocket_address_inet_addr_string(ncacn_conn->client,
|
---|
1072 | ncacn_conn);
|
---|
1073 | } else {
|
---|
1074 | ncacn_conn->client_name =
|
---|
1075 | tsocket_address_unix_path(ncacn_conn->client,
|
---|
1076 | ncacn_conn);
|
---|
1077 | }
|
---|
1078 | if (ncacn_conn->client_name == NULL) {
|
---|
1079 | DEBUG(0, ("Out of memory!\n"));
|
---|
1080 | talloc_free(ncacn_conn);
|
---|
1081 | close(s);
|
---|
1082 | return;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | if (srv_addr != NULL) {
|
---|
1086 | ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
|
---|
1087 |
|
---|
1088 | ncacn_conn->server_name =
|
---|
1089 | tsocket_address_inet_addr_string(ncacn_conn->server,
|
---|
1090 | ncacn_conn);
|
---|
1091 | if (ncacn_conn->server_name == NULL) {
|
---|
1092 | DEBUG(0, ("Out of memory!\n"));
|
---|
1093 | talloc_free(ncacn_conn);
|
---|
1094 | close(s);
|
---|
1095 | return;
|
---|
1096 | }
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | switch (transport) {
|
---|
1100 | case NCACN_IP_TCP:
|
---|
1101 | ncacn_conn->ep.port = port;
|
---|
1102 |
|
---|
1103 | pipe_name = tsocket_address_string(ncacn_conn->client,
|
---|
1104 | ncacn_conn);
|
---|
1105 | if (pipe_name == NULL) {
|
---|
1106 | close(s);
|
---|
1107 | talloc_free(ncacn_conn);
|
---|
1108 | return;
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | break;
|
---|
1112 | case NCALRPC:
|
---|
1113 | rc = sys_getpeereid(s, &uid);
|
---|
1114 | if (rc < 0) {
|
---|
1115 | DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
|
---|
1116 | } else {
|
---|
1117 | if (uid == sec_initial_uid()) {
|
---|
1118 | system_user = true;
|
---|
1119 | }
|
---|
1120 | }
|
---|
1121 | case NCACN_NP:
|
---|
1122 | ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
|
---|
1123 | if (ncacn_conn->ep.name == NULL) {
|
---|
1124 | close(s);
|
---|
1125 | talloc_free(ncacn_conn);
|
---|
1126 | return;
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | pipe_name = talloc_strdup(ncacn_conn,
|
---|
1130 | name);
|
---|
1131 | if (pipe_name == NULL) {
|
---|
1132 | close(s);
|
---|
1133 | talloc_free(ncacn_conn);
|
---|
1134 | return;
|
---|
1135 | }
|
---|
1136 | break;
|
---|
1137 | default:
|
---|
1138 | DEBUG(0, ("unknown dcerpc transport: %u!\n",
|
---|
1139 | transport));
|
---|
1140 | talloc_free(ncacn_conn);
|
---|
1141 | close(s);
|
---|
1142 | return;
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | rc = set_blocking(s, false);
|
---|
1146 | if (rc < 0) {
|
---|
1147 | DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
|
---|
1148 | talloc_free(ncacn_conn);
|
---|
1149 | close(s);
|
---|
1150 | return;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | /*
|
---|
1154 | * As soon as we have tstream_bsd_existing_socket set up it will
|
---|
1155 | * take care of closing the socket.
|
---|
1156 | */
|
---|
1157 | rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
|
---|
1158 | if (rc < 0) {
|
---|
1159 | DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
|
---|
1160 | talloc_free(ncacn_conn);
|
---|
1161 | close(s);
|
---|
1162 | return;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
|
---|
1166 | cli_str = ncacn_conn->client_name;
|
---|
1167 | } else {
|
---|
1168 | cli_str = "";
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | if (ncacn_conn->server != NULL) {
|
---|
1172 | if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
|
---|
1173 | srv_str = ncacn_conn->server_name;
|
---|
1174 | } else {
|
---|
1175 | srv_str = NULL;
|
---|
1176 | }
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | if (ncacn_conn->session_info == NULL) {
|
---|
1180 | status = auth_anonymous_session_info(ncacn_conn,
|
---|
1181 | &ncacn_conn->session_info);
|
---|
1182 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1183 | DEBUG(2, ("Failed to create "
|
---|
1184 | "auth_anonymous_session_info - %s\n",
|
---|
1185 | nt_errstr(status)));
|
---|
1186 | talloc_free(ncacn_conn);
|
---|
1187 | return;
|
---|
1188 | }
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | rc = make_server_pipes_struct(ncacn_conn,
|
---|
1192 | pipe_name,
|
---|
1193 | ncacn_conn->syntax_id,
|
---|
1194 | ncacn_conn->transport,
|
---|
1195 | system_user,
|
---|
1196 | cli_str,
|
---|
1197 | srv_str,
|
---|
1198 | ncacn_conn->session_info,
|
---|
1199 | &ncacn_conn->p,
|
---|
1200 | &sys_errno);
|
---|
1201 | if (rc < 0) {
|
---|
1202 | DEBUG(2, ("Failed to create pipe struct - %s",
|
---|
1203 | strerror(sys_errno)));
|
---|
1204 | talloc_free(ncacn_conn);
|
---|
1205 | return;
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
|
---|
1209 | "dcerpc send queue");
|
---|
1210 | if (ncacn_conn->send_queue == NULL) {
|
---|
1211 | DEBUG(0, ("Out of memory!\n"));
|
---|
1212 | talloc_free(ncacn_conn);
|
---|
1213 | return;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
|
---|
1217 | ncacn_conn->ev_ctx,
|
---|
1218 | ncacn_conn->tstream);
|
---|
1219 | if (subreq == NULL) {
|
---|
1220 | DEBUG(2, ("Failed to send ncacn packet\n"));
|
---|
1221 | talloc_free(ncacn_conn);
|
---|
1222 | return;
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
|
---|
1226 |
|
---|
1227 | DEBUG(10, ("dcerpc_ncacn_accept done\n"));
|
---|
1228 |
|
---|
1229 | return;
|
---|
1230 | }
|
---|
1231 |
|
---|
1232 | static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
|
---|
1233 | {
|
---|
1234 | struct dcerpc_ncacn_conn *ncacn_conn =
|
---|
1235 | tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
|
---|
1236 |
|
---|
1237 | struct _output_data *out = &ncacn_conn->p->out_data;
|
---|
1238 | DATA_BLOB recv_buffer = data_blob_null;
|
---|
1239 | struct ncacn_packet *pkt;
|
---|
1240 | ssize_t data_left;
|
---|
1241 | ssize_t data_used;
|
---|
1242 | uint32_t to_send;
|
---|
1243 | char *data;
|
---|
1244 | NTSTATUS status;
|
---|
1245 | bool ok;
|
---|
1246 |
|
---|
1247 | status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
|
---|
1248 | TALLOC_FREE(subreq);
|
---|
1249 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1250 | if (ncacn_conn->disconnect_fn != NULL) {
|
---|
1251 | ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
|
---|
1252 | if (!ok) {
|
---|
1253 | DEBUG(3, ("Failed to call disconnect function\n"));
|
---|
1254 | }
|
---|
1255 | }
|
---|
1256 | goto fail;
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | data_left = recv_buffer.length;
|
---|
1260 | data = (char *) recv_buffer.data;
|
---|
1261 |
|
---|
1262 | while (data_left) {
|
---|
1263 | data_used = process_incoming_data(ncacn_conn->p, data, data_left);
|
---|
1264 | if (data_used < 0) {
|
---|
1265 | DEBUG(3, ("Failed to process dcerpc request!\n"));
|
---|
1266 | status = NT_STATUS_UNEXPECTED_IO_ERROR;
|
---|
1267 | goto fail;
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | data_left -= data_used;
|
---|
1271 | data += data_used;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /* Do not leak this buffer */
|
---|
1275 | talloc_free(recv_buffer.data);
|
---|
1276 | talloc_free(pkt);
|
---|
1277 |
|
---|
1278 | /*
|
---|
1279 | * This is needed because of the way DCERPC binds work in the RPC
|
---|
1280 | * marshalling code
|
---|
1281 | */
|
---|
1282 | to_send = out->frag.length - out->current_pdu_sent;
|
---|
1283 | if (to_send > 0) {
|
---|
1284 |
|
---|
1285 | DEBUG(10, ("Current_pdu_len = %u, "
|
---|
1286 | "current_pdu_sent = %u "
|
---|
1287 | "Returning %u bytes\n",
|
---|
1288 | (unsigned int)out->frag.length,
|
---|
1289 | (unsigned int)out->current_pdu_sent,
|
---|
1290 | (unsigned int)to_send));
|
---|
1291 |
|
---|
1292 | ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
|
---|
1293 | if (ncacn_conn->iov == NULL) {
|
---|
1294 | status = NT_STATUS_NO_MEMORY;
|
---|
1295 | DEBUG(3, ("Out of memory!\n"));
|
---|
1296 | goto fail;
|
---|
1297 | }
|
---|
1298 | ncacn_conn->count = 1;
|
---|
1299 |
|
---|
1300 | ncacn_conn->iov[0].iov_base = out->frag.data
|
---|
1301 | + out->current_pdu_sent;
|
---|
1302 | ncacn_conn->iov[0].iov_len = to_send;
|
---|
1303 |
|
---|
1304 | out->current_pdu_sent += to_send;
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | /*
|
---|
1308 | * This condition is false for bind packets, or when we haven't yet got
|
---|
1309 | * a full request, and need to wait for more data from the client
|
---|
1310 | */
|
---|
1311 | while (out->data_sent_length < out->rdata.length) {
|
---|
1312 | ok = create_next_pdu(ncacn_conn->p);
|
---|
1313 | if (!ok) {
|
---|
1314 | DEBUG(3, ("Failed to create next PDU!\n"));
|
---|
1315 | status = NT_STATUS_UNEXPECTED_IO_ERROR;
|
---|
1316 | goto fail;
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | ncacn_conn->iov = talloc_realloc(ncacn_conn,
|
---|
1320 | ncacn_conn->iov,
|
---|
1321 | struct iovec,
|
---|
1322 | ncacn_conn->count + 1);
|
---|
1323 | if (ncacn_conn->iov == NULL) {
|
---|
1324 | DEBUG(3, ("Out of memory!\n"));
|
---|
1325 | status = NT_STATUS_NO_MEMORY;
|
---|
1326 | goto fail;
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
|
---|
1330 | ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
|
---|
1331 |
|
---|
1332 | DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
|
---|
1333 | (unsigned int) ncacn_conn->count,
|
---|
1334 | (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
|
---|
1335 | dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
|
---|
1336 | ncacn_conn->iov[ncacn_conn->count].iov_len);
|
---|
1337 | ncacn_conn->count++;
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | /*
|
---|
1341 | * We still don't have a complete request, go back and wait for more
|
---|
1342 | * data.
|
---|
1343 | */
|
---|
1344 | if (ncacn_conn->count == 0) {
|
---|
1345 | /* Wait for the next packet */
|
---|
1346 | subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
|
---|
1347 | ncacn_conn->ev_ctx,
|
---|
1348 | ncacn_conn->tstream);
|
---|
1349 | if (subreq == NULL) {
|
---|
1350 | DEBUG(2, ("Failed to start receving packets\n"));
|
---|
1351 | status = NT_STATUS_NO_MEMORY;
|
---|
1352 | goto fail;
|
---|
1353 | }
|
---|
1354 | tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
|
---|
1355 | return;
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | DEBUG(10, ("Sending a total of %u bytes\n",
|
---|
1359 | (unsigned int)ncacn_conn->p->out_data.data_sent_length));
|
---|
1360 |
|
---|
1361 | subreq = tstream_writev_queue_send(ncacn_conn,
|
---|
1362 | ncacn_conn->ev_ctx,
|
---|
1363 | ncacn_conn->tstream,
|
---|
1364 | ncacn_conn->send_queue,
|
---|
1365 | ncacn_conn->iov,
|
---|
1366 | ncacn_conn->count);
|
---|
1367 | if (subreq == NULL) {
|
---|
1368 | DEBUG(2, ("Failed to send packet\n"));
|
---|
1369 | status = NT_STATUS_NO_MEMORY;
|
---|
1370 | goto fail;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
|
---|
1374 | return;
|
---|
1375 |
|
---|
1376 | fail:
|
---|
1377 | DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
|
---|
1378 | ncacn_conn->client_name, nt_errstr(status)));
|
---|
1379 |
|
---|
1380 | /* Terminate client connection */
|
---|
1381 | talloc_free(ncacn_conn);
|
---|
1382 | return;
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
|
---|
1386 | {
|
---|
1387 | struct dcerpc_ncacn_conn *ncacn_conn =
|
---|
1388 | tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
|
---|
1389 | NTSTATUS status = NT_STATUS_OK;
|
---|
1390 | int sys_errno;
|
---|
1391 | int rc;
|
---|
1392 |
|
---|
1393 | rc = tstream_writev_queue_recv(subreq, &sys_errno);
|
---|
1394 | TALLOC_FREE(subreq);
|
---|
1395 | if (rc < 0) {
|
---|
1396 | DEBUG(2, ("Writev failed!\n"));
|
---|
1397 | status = map_nt_error_from_unix(sys_errno);
|
---|
1398 | goto fail;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | if (ncacn_conn->p->fault_state != 0) {
|
---|
1402 | DEBUG(2, ("Disconnect after fault\n"));
|
---|
1403 | sys_errno = EINVAL;
|
---|
1404 | goto fail;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | /* clear out any data that may have been left around */
|
---|
1408 | ncacn_conn->count = 0;
|
---|
1409 | TALLOC_FREE(ncacn_conn->iov);
|
---|
1410 | data_blob_free(&ncacn_conn->p->in_data.data);
|
---|
1411 | data_blob_free(&ncacn_conn->p->out_data.frag);
|
---|
1412 | data_blob_free(&ncacn_conn->p->out_data.rdata);
|
---|
1413 |
|
---|
1414 | /* Wait for the next packet */
|
---|
1415 | subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
|
---|
1416 | ncacn_conn->ev_ctx,
|
---|
1417 | ncacn_conn->tstream);
|
---|
1418 | if (subreq == NULL) {
|
---|
1419 | DEBUG(2, ("Failed to start receving packets\n"));
|
---|
1420 | status = NT_STATUS_NO_MEMORY;
|
---|
1421 | goto fail;
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 | tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
|
---|
1425 | return;
|
---|
1426 |
|
---|
1427 | fail:
|
---|
1428 | DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
|
---|
1429 | ncacn_conn->client_name, nt_errstr(status)));
|
---|
1430 |
|
---|
1431 | /* Terminate client connection */
|
---|
1432 | talloc_free(ncacn_conn);
|
---|
1433 | return;
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */
|
---|