librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/gebeck_regimport.git] / source4 / librpc / rpc / dcerpc_sock.c
blob58fca4ce37236669b5fdc131ac46c0cf7fe5c010
1 /*
2 Unix SMB/CIFS implementation.
4 dcerpc over standard sockets transport
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Jelmer Vernooij 2004
8 Copyright (C) Rafal Szczesniak 2006
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "lib/stream/packet.h"
28 #include "libcli/composite/composite.h"
29 #include "librpc/rpc/dcerpc.h"
30 #include "librpc/rpc/dcerpc_proto.h"
31 #include "libcli/resolve/resolve.h"
32 #include "librpc/rpc/rpc_common.h"
34 /* transport private information used by general socket pipe transports */
35 struct sock_private {
36 struct tevent_fd *fde;
37 struct socket_context *sock;
38 char *server_name;
40 struct packet_context *packet;
41 uint32_t pending_reads;
43 const char *path; /* For ncacn_unix_sock and ncalrpc */
48 mark the socket dead
50 static void sock_dead(struct dcecli_connection *p, NTSTATUS status)
52 struct sock_private *sock = (struct sock_private *)p->transport.private_data;
54 if (!sock) return;
56 if (sock->packet) {
57 packet_recv_disable(sock->packet);
58 packet_set_fde(sock->packet, NULL);
59 packet_set_socket(sock->packet, NULL);
62 if (sock->fde) {
63 talloc_free(sock->fde);
64 sock->fde = NULL;
67 if (sock->sock) {
68 talloc_free(sock->sock);
69 sock->sock = NULL;
72 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
73 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
76 if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
77 status = NT_STATUS_END_OF_FILE;
80 if (p->transport.recv_data) {
81 p->transport.recv_data(p, NULL, status);
87 handle socket recv errors
89 static void sock_error_handler(void *private_data, NTSTATUS status)
91 struct dcecli_connection *p = talloc_get_type(private_data,
92 struct dcecli_connection);
93 sock_dead(p, status);
97 check if a blob is a complete packet
99 static NTSTATUS sock_complete_packet(void *private_data, DATA_BLOB blob, size_t *size)
101 if (blob.length < DCERPC_FRAG_LEN_OFFSET+2) {
102 return STATUS_MORE_ENTRIES;
104 *size = dcerpc_get_frag_length(&blob);
105 if (*size > blob.length) {
106 return STATUS_MORE_ENTRIES;
108 return NT_STATUS_OK;
112 process recv requests
114 static NTSTATUS sock_process_recv(void *private_data, DATA_BLOB blob)
116 struct dcecli_connection *p = talloc_get_type(private_data,
117 struct dcecli_connection);
118 struct sock_private *sock = (struct sock_private *)p->transport.private_data;
119 sock->pending_reads--;
120 if (sock->pending_reads == 0) {
121 packet_recv_disable(sock->packet);
123 p->transport.recv_data(p, &blob, NT_STATUS_OK);
124 return NT_STATUS_OK;
128 called when a IO is triggered by the events system
130 static void sock_io_handler(struct tevent_context *ev, struct tevent_fd *fde,
131 uint16_t flags, void *private_data)
133 struct dcecli_connection *p = talloc_get_type(private_data,
134 struct dcecli_connection);
135 struct sock_private *sock = (struct sock_private *)p->transport.private_data;
137 if (flags & TEVENT_FD_WRITE) {
138 packet_queue_run(sock->packet);
139 return;
142 if (sock->sock == NULL) {
143 return;
146 if (flags & TEVENT_FD_READ) {
147 packet_recv(sock->packet);
152 initiate a read request - not needed for dcerpc sockets
154 static NTSTATUS sock_send_read(struct dcecli_connection *p)
156 struct sock_private *sock = (struct sock_private *)p->transport.private_data;
157 sock->pending_reads++;
158 if (sock->pending_reads == 1) {
159 packet_recv_enable(sock->packet);
161 return NT_STATUS_OK;
165 send an initial pdu in a multi-pdu sequence
167 static NTSTATUS sock_send_request(struct dcecli_connection *p, DATA_BLOB *data,
168 bool trigger_read)
170 struct sock_private *sock = (struct sock_private *)p->transport.private_data;
171 DATA_BLOB blob;
172 NTSTATUS status;
174 if (sock->sock == NULL) {
175 return NT_STATUS_CONNECTION_DISCONNECTED;
178 blob = data_blob_talloc(sock->packet, data->data, data->length);
179 if (blob.data == NULL) {
180 return NT_STATUS_NO_MEMORY;
183 status = packet_send(sock->packet, blob);
184 if (!NT_STATUS_IS_OK(status)) {
185 return status;
188 if (trigger_read) {
189 sock_send_read(p);
192 return NT_STATUS_OK;
196 shutdown sock pipe connection
198 static NTSTATUS sock_shutdown_pipe(struct dcecli_connection *p, NTSTATUS status)
200 struct sock_private *sock = (struct sock_private *)p->transport.private_data;
202 if (sock && sock->sock) {
203 sock_dead(p, status);
206 return status;
210 return sock server name
212 static const char *sock_peer_name(struct dcecli_connection *p)
214 struct sock_private *sock = talloc_get_type(p->transport.private_data, struct sock_private);
215 return sock->server_name;
219 return remote name we make the actual connection (good for kerberos)
221 static const char *sock_target_hostname(struct dcecli_connection *p)
223 struct sock_private *sock = talloc_get_type(p->transport.private_data, struct sock_private);
224 return sock->server_name;
228 struct pipe_open_socket_state {
229 struct dcecli_connection *conn;
230 struct socket_context *socket_ctx;
231 struct sock_private *sock;
232 struct socket_address *localaddr;
233 struct socket_address *server;
234 const char *target_hostname;
235 enum dcerpc_transport_t transport;
239 static void continue_socket_connect(struct composite_context *ctx)
241 struct dcecli_connection *conn;
242 struct sock_private *sock;
243 struct composite_context *c = talloc_get_type(ctx->async.private_data,
244 struct composite_context);
245 struct pipe_open_socket_state *s = talloc_get_type(c->private_data,
246 struct pipe_open_socket_state);
248 /* make it easier to write a function calls */
249 conn = s->conn;
250 sock = s->sock;
252 c->status = socket_connect_recv(ctx);
253 if (!NT_STATUS_IS_OK(c->status)) {
254 DEBUG(0, ("Failed to connect host %s on port %d - %s\n",
255 s->server->addr, s->server->port,
256 nt_errstr(c->status)));
257 composite_error(c, c->status);
258 return;
262 fill in the transport methods
264 conn->transport.transport = s->transport;
265 conn->transport.private_data = NULL;
267 conn->transport.send_request = sock_send_request;
268 conn->transport.send_read = sock_send_read;
269 conn->transport.recv_data = NULL;
271 conn->transport.shutdown_pipe = sock_shutdown_pipe;
272 conn->transport.peer_name = sock_peer_name;
273 conn->transport.target_hostname = sock_target_hostname;
275 sock->sock = s->socket_ctx;
276 sock->pending_reads = 0;
277 sock->server_name = strupper_talloc(sock, s->target_hostname);
279 sock->fde = tevent_add_fd(conn->event_ctx, sock->sock, socket_get_fd(sock->sock),
280 TEVENT_FD_READ, sock_io_handler, conn);
282 conn->transport.private_data = sock;
284 sock->packet = packet_init(sock);
285 if (sock->packet == NULL) {
286 talloc_free(sock);
287 composite_error(c, NT_STATUS_NO_MEMORY);
288 return;
291 packet_set_private(sock->packet, conn);
292 packet_set_socket(sock->packet, sock->sock);
293 packet_set_callback(sock->packet, sock_process_recv);
294 packet_set_full_request(sock->packet, sock_complete_packet);
295 packet_set_error_handler(sock->packet, sock_error_handler);
296 packet_set_event_context(sock->packet, conn->event_ctx);
297 packet_set_fde(sock->packet, sock->fde);
298 packet_set_serialise(sock->packet);
299 packet_set_initial_read(sock->packet, 16);
301 /* ensure we don't get SIGPIPE */
302 BlockSignals(true, SIGPIPE);
304 composite_done(c);
308 static struct composite_context *dcerpc_pipe_open_socket_send(TALLOC_CTX *mem_ctx,
309 struct dcecli_connection *cn,
310 struct socket_address *localaddr,
311 struct socket_address *server,
312 const char *target_hostname,
313 const char *full_path,
314 enum dcerpc_transport_t transport)
316 struct composite_context *c;
317 struct pipe_open_socket_state *s;
318 struct composite_context *conn_req;
320 c = composite_create(mem_ctx, cn->event_ctx);
321 if (c == NULL) return NULL;
323 s = talloc_zero(c, struct pipe_open_socket_state);
324 if (composite_nomem(s, c)) return c;
325 c->private_data = s;
327 s->conn = cn;
328 s->transport = transport;
329 if (localaddr) {
330 s->localaddr = talloc_reference(c, localaddr);
331 if (composite_nomem(s->localaddr, c)) return c;
333 s->server = talloc_reference(c, server);
334 if (composite_nomem(s->server, c)) return c;
335 s->target_hostname = talloc_reference(s, target_hostname);
337 s->sock = talloc(cn, struct sock_private);
338 if (composite_nomem(s->sock, c)) return c;
340 c->status = socket_create(server->family, SOCKET_TYPE_STREAM, &s->socket_ctx, 0);
341 if (!composite_is_ok(c)) return c;
343 talloc_steal(s->sock, s->socket_ctx);
345 s->sock->path = talloc_reference(s->sock, full_path);
347 conn_req = socket_connect_send(s->socket_ctx, s->localaddr, s->server, 0,
348 c->event_ctx);
349 composite_continue(c, conn_req, continue_socket_connect, c);
350 return c;
354 static NTSTATUS dcerpc_pipe_open_socket_recv(struct composite_context *c)
356 NTSTATUS status = composite_wait(c);
358 talloc_free(c);
359 return status;
362 struct pipe_tcp_state {
363 const char *server;
364 const char *target_hostname;
365 const char **addresses;
366 uint32_t index;
367 uint32_t port;
368 struct socket_address *localaddr;
369 struct socket_address *srvaddr;
370 struct resolve_context *resolve_ctx;
371 struct dcecli_connection *conn;
375 static void continue_ip_open_socket(struct composite_context *ctx);
376 static void continue_ip_resolve_name(struct composite_context *ctx);
378 static void continue_ip_resolve_name(struct composite_context *ctx)
380 struct composite_context *c = talloc_get_type(ctx->async.private_data,
381 struct composite_context);
382 struct pipe_tcp_state *s = talloc_get_type(c->private_data,
383 struct pipe_tcp_state);
384 struct composite_context *sock_ip_req;
386 c->status = resolve_name_multiple_recv(ctx, s, &s->addresses);
387 if (!composite_is_ok(c)) return;
389 /* prepare server address using host ip:port and transport name */
390 s->srvaddr = socket_address_from_strings(s->conn, "ip", s->addresses[s->index], s->port);
391 s->index++;
392 if (composite_nomem(s->srvaddr, c)) return;
394 sock_ip_req = dcerpc_pipe_open_socket_send(c, s->conn, s->localaddr,
395 s->srvaddr, s->target_hostname,
396 NULL,
397 NCACN_IP_TCP);
398 composite_continue(c, sock_ip_req, continue_ip_open_socket, c);
403 Stage 2 of dcerpc_pipe_open_tcp_send: receive result of pipe open request
404 on IP transport.
406 static void continue_ip_open_socket(struct composite_context *ctx)
408 struct composite_context *c = talloc_get_type(ctx->async.private_data,
409 struct composite_context);
410 struct pipe_tcp_state *s = talloc_get_type(c->private_data,
411 struct pipe_tcp_state);
413 /* receive result socket open request */
414 c->status = dcerpc_pipe_open_socket_recv(ctx);
415 if (!NT_STATUS_IS_OK(c->status)) {
416 /* something went wrong... */
417 DEBUG(0, ("Failed to connect host %s (%s) on port %d - %s.\n",
418 s->addresses[s->index - 1], s->target_hostname,
419 s->port, nt_errstr(c->status)));
420 if (s->addresses[s->index]) {
421 struct composite_context *sock_ip_req;
422 talloc_free(s->srvaddr);
423 /* prepare server address using host ip:port and transport name */
424 s->srvaddr = socket_address_from_strings(s->conn, "ip", s->addresses[s->index], s->port);
425 s->index++;
426 if (composite_nomem(s->srvaddr, c)) return;
428 sock_ip_req = dcerpc_pipe_open_socket_send(c, s->conn, s->localaddr,
429 s->srvaddr, s->target_hostname,
430 NULL,
431 NCACN_IP_TCP);
432 composite_continue(c, sock_ip_req, continue_ip_open_socket, c);
434 return;
435 } else {
436 composite_error(c, c->status);
437 return;
441 composite_done(c);
445 Send rpc pipe open request to given host:port using
446 tcp/ip transport
448 struct composite_context* dcerpc_pipe_open_tcp_send(struct dcecli_connection *conn,
449 const char *localaddr,
450 const char *server,
451 const char *target_hostname,
452 uint32_t port,
453 struct resolve_context *resolve_ctx)
455 struct composite_context *c;
456 struct pipe_tcp_state *s;
457 struct composite_context *resolve_req;
458 struct nbt_name name;
460 /* composite context allocation and setup */
461 c = composite_create(conn, conn->event_ctx);
462 if (c == NULL) return NULL;
464 s = talloc_zero(c, struct pipe_tcp_state);
465 if (composite_nomem(s, c)) return c;
466 c->private_data = s;
468 /* store input parameters in state structure */
469 s->server = talloc_strdup(c, server);
470 if (composite_nomem(s->server, c)) return c;
471 if (target_hostname) {
472 s->target_hostname = talloc_strdup(c, target_hostname);
473 if (composite_nomem(s->target_hostname, c)) return c;
475 s->port = port;
476 s->conn = conn;
477 s->resolve_ctx = resolve_ctx;
478 if (localaddr) {
479 s->localaddr = socket_address_from_strings(s, "ip", localaddr, 0);
480 /* if there is no localaddr, we pass NULL for
481 s->localaddr, which is handled by the socket libraries as
482 meaning no local binding address specified */
485 make_nbt_name_server(&name, server);
486 resolve_req = resolve_name_send(resolve_ctx, s, &name, c->event_ctx);
487 composite_continue(c, resolve_req, continue_ip_resolve_name, c);
488 return c;
492 Receive result of pipe open request on tcp/ip
494 NTSTATUS dcerpc_pipe_open_tcp_recv(struct composite_context *c)
496 NTSTATUS status;
497 status = composite_wait(c);
499 talloc_free(c);
500 return status;
504 struct pipe_unix_state {
505 const char *path;
506 struct socket_address *srvaddr;
507 struct dcecli_connection *conn;
512 Stage 2 of dcerpc_pipe_open_unix_stream_send: receive result of pipe open
513 request on unix socket.
515 static void continue_unix_open_socket(struct composite_context *ctx)
517 struct composite_context *c = talloc_get_type(ctx->async.private_data,
518 struct composite_context);
520 c->status = dcerpc_pipe_open_socket_recv(ctx);
521 if (NT_STATUS_IS_OK(c->status)) {
522 composite_done(c);
523 return;
526 composite_error(c, c->status);
531 Send pipe open request on unix socket
533 struct composite_context *dcerpc_pipe_open_unix_stream_send(struct dcecli_connection *conn,
534 const char *path)
536 struct composite_context *c;
537 struct composite_context *sock_unix_req;
538 struct pipe_unix_state *s;
540 /* composite context allocation and setup */
541 c = composite_create(conn, conn->event_ctx);
542 if (c == NULL) return NULL;
544 s = talloc_zero(c, struct pipe_unix_state);
545 if (composite_nomem(s, c)) return c;
546 c->private_data = s;
548 /* store parameters in state structure */
549 s->path = talloc_strdup(c, path);
550 if (composite_nomem(s->path, c)) return c;
551 s->conn = conn;
553 /* prepare server address using socket path and transport name */
554 s->srvaddr = socket_address_from_strings(conn, "unix", s->path, 0);
555 if (composite_nomem(s->srvaddr, c)) return c;
557 /* send socket open request */
558 sock_unix_req = dcerpc_pipe_open_socket_send(c, s->conn, NULL,
559 s->srvaddr, NULL,
560 s->path,
561 NCALRPC);
562 composite_continue(c, sock_unix_req, continue_unix_open_socket, c);
563 return c;
568 Receive result of pipe open request on unix socket
570 NTSTATUS dcerpc_pipe_open_unix_stream_recv(struct composite_context *c)
572 NTSTATUS status = composite_wait(c);
574 talloc_free(c);
575 return status;
580 Stage 2 of dcerpc_pipe_open_pipe_send: receive socket open request
582 static void continue_np_open_socket(struct composite_context *ctx)
584 struct composite_context *c = talloc_get_type(ctx->async.private_data,
585 struct composite_context);
587 c->status = dcerpc_pipe_open_socket_recv(ctx);
588 if (!composite_is_ok(c)) return;
590 composite_done(c);
595 Send pipe open request on ncalrpc
597 struct composite_context* dcerpc_pipe_open_pipe_send(struct dcecli_connection *conn,
598 const char *ncalrpc_dir,
599 const char *identifier)
601 char *canon = NULL;
603 struct composite_context *c;
604 struct composite_context *sock_np_req;
605 struct pipe_unix_state *s;
607 /* composite context allocation and setup */
608 c = composite_create(conn, conn->event_ctx);
609 if (c == NULL) return NULL;
611 s = talloc_zero(c, struct pipe_unix_state);
612 if (composite_nomem(s, c)) return c;
613 c->private_data = s;
615 /* store parameters in state structure */
616 canon = talloc_strdup(s, identifier);
617 if (composite_nomem(canon, c)) return c;
618 s->conn = conn;
620 string_replace(canon, '/', '\\');
621 s->path = talloc_asprintf(canon, "%s/%s", ncalrpc_dir, canon);
622 if (composite_nomem(s->path, c)) return c;
624 /* prepare server address using path and transport name */
625 s->srvaddr = socket_address_from_strings(conn, "unix", s->path, 0);
626 if (composite_nomem(s->srvaddr, c)) return c;
628 /* send socket open request */
629 sock_np_req = dcerpc_pipe_open_socket_send(c, s->conn, NULL, s->srvaddr, NULL, s->path, NCALRPC);
630 composite_continue(c, sock_np_req, continue_np_open_socket, c);
631 return c;
636 Receive result of pipe open request on ncalrpc
638 NTSTATUS dcerpc_pipe_open_pipe_recv(struct composite_context *c)
640 NTSTATUS status = composite_wait(c);
642 talloc_free(c);
643 return status;
648 Open a rpc pipe on a named pipe - sync version
650 NTSTATUS dcerpc_pipe_open_pipe(struct dcecli_connection *conn, const char *ncalrpc_dir, const char *identifier)
652 struct composite_context *c = dcerpc_pipe_open_pipe_send(conn, ncalrpc_dir, identifier);
653 return dcerpc_pipe_open_pipe_recv(c);
656 const char *dcerpc_unix_socket_path(struct dcecli_connection *p)
658 struct sock_private *sock = (struct sock_private *)p->transport.private_data;
659 return sock->path;
662 struct socket_address *dcerpc_socket_peer_addr(struct dcecli_connection *p, TALLOC_CTX *mem_ctx)
664 struct sock_private *sock = (struct sock_private *)p->transport.private_data;
665 return socket_get_peer_addr(sock->sock, mem_ctx);