de86e9802c4629f16313a1c4b8021e4610bab657
[samba.git] / source4 / librpc / rpc / dcerpc_sock.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc over standard sockets transport
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    Copyright (C) Rafal Szczesniak 2006
9    
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.
14    
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.
19    
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/>.
22 */
23
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "lib/events/events.h"
27 #include "lib/socket/socket.h"
28 #include "lib/tsocket/tsocket.h"
29 #include "libcli/composite/composite.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "librpc/rpc/dcerpc_proto.h"
32 #include "libcli/resolve/resolve.h"
33 #include "librpc/rpc/rpc_common.h"
34
35 /* transport private information used by general socket pipe transports */
36 struct sock_private {
37         const char *path; /* For ncacn_unix_sock and ncalrpc */
38
39         struct socket_address *peer_addr;
40 };
41
42 struct pipe_open_socket_state {
43         struct dcecli_connection *conn;
44         struct socket_context *socket_ctx;
45         struct sock_private *sock;
46         struct socket_address *localaddr;
47         struct socket_address *server;
48         const char *target_hostname;
49         enum dcerpc_transport_t transport;
50         struct socket_address *client;
51 };
52
53
54 static void continue_socket_connect(struct composite_context *ctx)
55 {
56         struct dcecli_connection *conn;
57         struct sock_private *sock;
58         struct composite_context *c = talloc_get_type_abort(
59                 ctx->async.private_data, struct composite_context);
60         struct pipe_open_socket_state *s = talloc_get_type_abort(
61                 c->private_data, struct pipe_open_socket_state);
62         int rc;
63         int sock_fd;
64
65         /* make it easier to write a function calls */
66         conn = s->conn;
67         sock = s->sock;
68
69         c->status = socket_connect_recv(ctx);
70         if (!NT_STATUS_IS_OK(c->status)) {
71                 DEBUG(0, ("Failed to connect host %s on port %d - %s\n", 
72                           s->server->addr, s->server->port,
73                           nt_errstr(c->status)));
74                 composite_error(c, c->status);
75                 return;
76         }
77
78         s->client = socket_get_my_addr(s->socket_ctx, s);
79         if (s->client == NULL) {
80                 talloc_free(sock);
81                 composite_error(c, NT_STATUS_NO_MEMORY);
82                 return;
83         }
84         sock_fd = socket_get_fd(s->socket_ctx);
85         sock->peer_addr = socket_get_peer_addr(s->socket_ctx, sock);
86         if (sock->peer_addr == NULL) {
87                 talloc_free(sock);
88                 composite_error(c, NT_STATUS_NO_MEMORY);
89                 return;
90         }
91         socket_set_flags(s->socket_ctx, SOCKET_FLAG_NOCLOSE);
92         TALLOC_FREE(s->socket_ctx);
93
94         /*
95           fill in the transport methods
96         */
97         conn->transport.transport       = s->transport;
98         conn->transport.private_data    = NULL;
99
100         /*
101          * Windows uses 5840 for ncacn_ip_tcp,
102          * so we also use it (for every transport which uses bsd sockets)
103          */
104         conn->srv_max_xmit_frag = 5840;
105         conn->srv_max_recv_frag = 5840;
106
107         conn->transport.pending_reads = 0;
108         conn->server_name   = strupper_talloc(conn, s->target_hostname);
109
110         conn->transport.private_data = sock;
111
112         rc = tstream_bsd_existing_socket(sock, sock_fd,
113                                          &conn->transport.stream);
114         if (rc < 0) {
115                 talloc_free(sock);
116                 composite_error(c, NT_STATUS_NO_MEMORY);
117                 return;
118         }
119
120         conn->transport.write_queue =
121                 tevent_queue_create(conn, "dcerpc sock write queue");
122         if (conn->transport.write_queue == NULL) {
123                 talloc_free(sock);
124                 composite_error(c, NT_STATUS_NO_MEMORY);
125                 return;
126         }
127
128         /* ensure we don't get SIGPIPE */
129         BlockSignals(true, SIGPIPE);
130
131         composite_done(c);
132 }
133
134
135 static struct composite_context *dcerpc_pipe_open_socket_send(TALLOC_CTX *mem_ctx,
136                                                        struct dcecli_connection *cn,
137                                                        struct socket_address *localaddr,
138                                                        struct socket_address *server,
139                                                        const char *target_hostname,
140                                                        const char *full_path,
141                                                        enum dcerpc_transport_t transport)
142 {
143         struct composite_context *c;
144         struct pipe_open_socket_state *s;
145         struct composite_context *conn_req;
146
147         c = composite_create(mem_ctx, cn->event_ctx);
148         if (c == NULL) return NULL;
149
150         s = talloc_zero(c, struct pipe_open_socket_state);
151         if (composite_nomem(s, c)) return c;
152         c->private_data = s;
153
154         s->conn      = cn;
155         s->transport = transport;
156         if (localaddr) {
157                 s->localaddr = socket_address_copy(s, localaddr);
158                 if (composite_nomem(s->localaddr, c)) return c;
159         }
160         s->server = socket_address_copy(s, server);
161         if (composite_nomem(s->server, c)) return c;
162         if (target_hostname) {
163                 s->target_hostname = talloc_strdup(s, target_hostname);
164                 if (composite_nomem(s->target_hostname, c)) return c;
165         }
166
167         s->sock = talloc_zero(cn, struct sock_private);
168         if (composite_nomem(s->sock, c)) return c;
169
170         c->status = socket_create(server->family, SOCKET_TYPE_STREAM, &s->socket_ctx, 0);
171         if (!composite_is_ok(c)) return c;
172
173         talloc_steal(s->sock, s->socket_ctx);
174
175         if (full_path != NULL) {
176                 s->sock->path = talloc_strdup(s->sock, full_path);
177                 if (composite_nomem(s->sock->path, c)) return c;
178         }
179
180         conn_req = socket_connect_send(s->socket_ctx, s->localaddr, s->server, 0,
181                                        c->event_ctx);
182         composite_continue(c, conn_req, continue_socket_connect, c);
183         return c;
184 }
185
186 static NTSTATUS dcerpc_pipe_open_socket_recv(struct composite_context *c,
187                                              TALLOC_CTX *mem_ctx,
188                                              struct socket_address **localaddr)
189 {
190         NTSTATUS status = composite_wait(c);
191
192         if (NT_STATUS_IS_OK(status)) {
193                 struct pipe_open_socket_state *s =
194                         talloc_get_type_abort(c->private_data,
195                         struct pipe_open_socket_state);
196
197                 if (localaddr != NULL) {
198                         *localaddr = talloc_move(mem_ctx, &s->client);
199                 }
200         }
201
202         talloc_free(c);
203         return status;
204 }
205
206 struct pipe_tcp_state {
207         const char *server;
208         const char *target_hostname;
209         const char **addresses;
210         uint32_t index;
211         uint32_t port;
212         struct socket_address *localaddr;
213         struct socket_address *srvaddr;
214         struct resolve_context *resolve_ctx;
215         struct dcecli_connection *conn;
216         char *local_address;
217         char *remote_address;
218 };
219
220
221 static void continue_ip_open_socket(struct composite_context *ctx);
222 static void continue_ip_resolve_name(struct composite_context *ctx);
223
224 static void continue_ip_resolve_name(struct composite_context *ctx)
225 {
226         struct composite_context *c = talloc_get_type_abort(
227                 ctx->async.private_data, struct composite_context);
228         struct pipe_tcp_state *s = talloc_get_type_abort(
229                 c->private_data, struct pipe_tcp_state);
230         struct composite_context *sock_ip_req;
231
232         c->status = resolve_name_multiple_recv(ctx, s, &s->addresses);
233         if (!composite_is_ok(c)) return;
234
235         /* prepare server address using host ip:port and transport name */
236         s->srvaddr = socket_address_from_strings(s->conn, "ip", s->addresses[s->index], s->port);
237         s->index++;
238         if (composite_nomem(s->srvaddr, c)) return;
239
240         sock_ip_req = dcerpc_pipe_open_socket_send(c, s->conn, s->localaddr,
241                                                      s->srvaddr, s->target_hostname,
242                                                      NULL,
243                                                      NCACN_IP_TCP);
244         composite_continue(c, sock_ip_req, continue_ip_open_socket, c);
245 }
246
247
248 /*
249   Stage 2 of dcerpc_pipe_open_tcp_send: receive result of pipe open request
250   on IP transport.
251 */
252 static void continue_ip_open_socket(struct composite_context *ctx)
253 {
254         struct composite_context *c = talloc_get_type_abort(
255                 ctx->async.private_data, struct composite_context);
256         struct pipe_tcp_state *s = talloc_get_type_abort(
257                 c->private_data, struct pipe_tcp_state);
258         struct socket_address *localaddr = NULL;
259
260         /* receive result socket open request */
261         c->status = dcerpc_pipe_open_socket_recv(ctx, s, &localaddr);
262         if (!NT_STATUS_IS_OK(c->status)) {
263                 /* something went wrong... */
264                 DEBUG(0, ("Failed to connect host %s (%s) on port %d - %s.\n",
265                           s->addresses[s->index - 1], s->target_hostname,
266                           s->port, nt_errstr(c->status)));
267                 if (s->addresses[s->index]) {
268                         struct composite_context *sock_ip_req;
269                         talloc_free(s->srvaddr);
270                         /* prepare server address using host ip:port and transport name */
271                         s->srvaddr = socket_address_from_strings(s->conn, "ip", s->addresses[s->index], s->port);
272                         s->index++;
273                         if (composite_nomem(s->srvaddr, c)) return;
274
275                         sock_ip_req = dcerpc_pipe_open_socket_send(c, s->conn, s->localaddr,
276                                                                 s->srvaddr, s->target_hostname,
277                                                                 NULL,
278                                                                 NCACN_IP_TCP);
279                         composite_continue(c, sock_ip_req, continue_ip_open_socket, c);
280
281                         return;
282                 } else {
283                         composite_error(c, c->status);
284                         return;
285                 }
286         }
287
288         s->local_address = talloc_strdup(s, localaddr->addr);
289         if (composite_nomem(s->local_address, c)) return;
290         s->remote_address = talloc_strdup(s, s->addresses[s->index - 1]);
291         if (composite_nomem(s->remote_address, c)) return;
292
293         composite_done(c);
294 }
295
296 /*
297   Send rpc pipe open request to given host:port using
298   tcp/ip transport
299 */
300 struct composite_context* dcerpc_pipe_open_tcp_send(struct dcecli_connection *conn,
301                                                     const char *localaddr,
302                                                     const char *server,
303                                                     const char *target_hostname,
304                                                     uint32_t port,
305                                                     struct resolve_context *resolve_ctx)
306 {
307         struct composite_context *c;
308         struct pipe_tcp_state *s;
309         struct composite_context *resolve_req;
310         struct nbt_name name;
311
312         /* composite context allocation and setup */
313         c = composite_create(conn, conn->event_ctx);
314         if (c == NULL) return NULL;
315
316         s = talloc_zero(c, struct pipe_tcp_state);
317         if (composite_nomem(s, c)) return c;
318         c->private_data = s;
319
320         /* store input parameters in state structure */
321         s->server          = talloc_strdup(c, server);
322         if (composite_nomem(s->server, c)) return c;
323         if (target_hostname) {
324                 s->target_hostname = talloc_strdup(c, target_hostname);
325                 if (composite_nomem(s->target_hostname, c)) return c;
326         }
327         s->port            = port;
328         s->conn            = conn;
329         s->resolve_ctx     = resolve_ctx;
330         if (localaddr) {
331                 s->localaddr = socket_address_from_strings(s, "ip", localaddr, 0);
332                 /* if there is no localaddr, we pass NULL for
333                    s->localaddr, which is handled by the socket libraries as
334                    meaning no local binding address specified */
335         }
336
337         make_nbt_name_server(&name, server);
338         resolve_req = resolve_name_send(resolve_ctx, s, &name, c->event_ctx);
339         composite_continue(c, resolve_req, continue_ip_resolve_name, c);
340         return c;
341 }
342
343 /*
344   Receive result of pipe open request on tcp/ip
345 */
346 NTSTATUS dcerpc_pipe_open_tcp_recv(struct composite_context *c,
347                                    TALLOC_CTX *mem_ctx,
348                                    char **localaddr,
349                                    char **remoteaddr)
350 {
351         NTSTATUS status;
352         status = composite_wait(c);
353
354         if (NT_STATUS_IS_OK(status)) {
355                 struct pipe_tcp_state *s = talloc_get_type_abort(
356                         c->private_data, struct pipe_tcp_state);
357
358                 if (localaddr != NULL) {
359                         *localaddr = talloc_move(mem_ctx, &s->local_address);
360                 }
361                 if (remoteaddr != NULL) {
362                         *remoteaddr = talloc_move(mem_ctx, &s->remote_address);
363                 }
364         }
365
366         talloc_free(c);
367         return status;
368 }
369
370
371 struct pipe_unix_state {
372         const char *path;
373         struct socket_address *srvaddr;
374         struct dcecli_connection *conn;
375 };
376
377
378 /*
379   Stage 2 of dcerpc_pipe_open_unix_stream_send: receive result of pipe open
380   request on unix socket.
381 */
382 static void continue_unix_open_socket(struct composite_context *ctx)
383 {
384         struct composite_context *c = talloc_get_type_abort(
385                 ctx->async.private_data, struct composite_context);
386
387         c->status = dcerpc_pipe_open_socket_recv(ctx, NULL, NULL);
388         if (NT_STATUS_IS_OK(c->status)) {
389                 composite_done(c);
390                 return;
391         }
392
393         composite_error(c, c->status);
394 }
395
396
397 /*
398   Send pipe open request on unix socket
399 */
400 struct composite_context *dcerpc_pipe_open_unix_stream_send(struct dcecli_connection *conn,
401                                                             const char *path)
402 {
403         struct composite_context *c;
404         struct composite_context *sock_unix_req;
405         struct pipe_unix_state *s;
406
407         /* composite context allocation and setup */
408         c = composite_create(conn, conn->event_ctx);
409         if (c == NULL) return NULL;
410
411         s = talloc_zero(c, struct pipe_unix_state);
412         if (composite_nomem(s, c)) return c;
413         c->private_data = s;
414
415         /* store parameters in state structure */
416         s->path = talloc_strdup(c, path);
417         if (composite_nomem(s->path, c)) return c;
418         s->conn = conn;
419
420         /* prepare server address using socket path and transport name */
421         s->srvaddr = socket_address_from_strings(conn, "unix", s->path, 0);
422         if (composite_nomem(s->srvaddr, c)) return c;
423
424         /* send socket open request */
425         sock_unix_req = dcerpc_pipe_open_socket_send(c, s->conn, NULL,
426                                                      s->srvaddr, NULL,
427                                                      s->path,
428                                                      NCALRPC);
429         composite_continue(c, sock_unix_req, continue_unix_open_socket, c);
430         return c;
431 }
432
433
434 /*
435   Receive result of pipe open request on unix socket
436 */
437 NTSTATUS dcerpc_pipe_open_unix_stream_recv(struct composite_context *c)
438 {
439         NTSTATUS status = composite_wait(c);
440
441         talloc_free(c);
442         return status;
443 }
444
445
446 /*
447   Stage 2 of dcerpc_pipe_open_pipe_send: receive socket open request
448 */
449 static void continue_np_open_socket(struct composite_context *ctx)
450 {
451         struct composite_context *c = talloc_get_type_abort(
452                 ctx->async.private_data, struct composite_context);
453
454         c->status = dcerpc_pipe_open_socket_recv(ctx, NULL, NULL);
455         if (!composite_is_ok(c)) return;
456
457         composite_done(c);
458 }
459
460
461 /*
462   Send pipe open request on ncalrpc
463 */
464 struct composite_context* dcerpc_pipe_open_pipe_send(struct dcecli_connection *conn,
465                                                      const char *ncalrpc_dir,
466                                                      const char *identifier)
467 {
468         char *canon = NULL;
469
470         struct composite_context *c;
471         struct composite_context *sock_np_req;
472         struct pipe_unix_state *s;
473
474         /* composite context allocation and setup */
475         c = composite_create(conn, conn->event_ctx);
476         if (c == NULL) return NULL;
477
478         s = talloc_zero(c, struct pipe_unix_state);
479         if (composite_nomem(s, c)) return c;
480         c->private_data = s;
481
482         /* store parameters in state structure */
483         canon = talloc_strdup(s, identifier);
484         if (composite_nomem(canon, c)) return c;
485         s->conn = conn;
486
487         string_replace(canon, '/', '\\');
488         s->path = talloc_asprintf(canon, "%s/%s", ncalrpc_dir, canon);
489         if (composite_nomem(s->path, c)) return c;
490
491         /* prepare server address using path and transport name */
492         s->srvaddr = socket_address_from_strings(conn, "unix", s->path, 0);
493         if (composite_nomem(s->srvaddr, c)) return c;
494
495         /* send socket open request */
496         sock_np_req = dcerpc_pipe_open_socket_send(c, s->conn, NULL, s->srvaddr, NULL, s->path, NCALRPC);
497         composite_continue(c, sock_np_req, continue_np_open_socket, c);
498         return c;
499 }
500
501
502 /*
503   Receive result of pipe open request on ncalrpc
504 */
505 NTSTATUS dcerpc_pipe_open_pipe_recv(struct composite_context *c)
506 {
507         NTSTATUS status = composite_wait(c);
508         
509         talloc_free(c);
510         return status;
511 }
512
513
514 /*
515   Open a rpc pipe on a named pipe - sync version
516 */
517 NTSTATUS dcerpc_pipe_open_pipe(struct dcecli_connection *conn, const char *ncalrpc_dir, const char *identifier)
518 {
519         struct composite_context *c = dcerpc_pipe_open_pipe_send(conn, ncalrpc_dir, identifier);
520         return dcerpc_pipe_open_pipe_recv(c);
521 }
522
523 const char *dcerpc_unix_socket_path(struct dcecli_connection *p)
524 {
525         struct sock_private *sock = talloc_get_type_abort(
526                 p->transport.private_data, struct sock_private);
527         return sock->path;
528 }
529
530 struct socket_address *dcerpc_socket_peer_addr(struct dcecli_connection *p, TALLOC_CTX *mem_ctx)
531 {
532         struct sock_private *sock = talloc_get_type_abort(
533                 p->transport.private_data, struct sock_private);
534         return socket_address_copy(mem_ctx, sock->peer_addr);
535 }
536