s4:librpc/rpc: optionally return the local address from dcerpc_pipe_open_socket_recv()
[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 };
217
218
219 static void continue_ip_open_socket(struct composite_context *ctx);
220 static void continue_ip_resolve_name(struct composite_context *ctx);
221
222 static void continue_ip_resolve_name(struct composite_context *ctx)
223 {
224         struct composite_context *c = talloc_get_type_abort(
225                 ctx->async.private_data, struct composite_context);
226         struct pipe_tcp_state *s = talloc_get_type_abort(
227                 c->private_data, struct pipe_tcp_state);
228         struct composite_context *sock_ip_req;
229
230         c->status = resolve_name_multiple_recv(ctx, s, &s->addresses);
231         if (!composite_is_ok(c)) return;
232
233         /* prepare server address using host ip:port and transport name */
234         s->srvaddr = socket_address_from_strings(s->conn, "ip", s->addresses[s->index], s->port);
235         s->index++;
236         if (composite_nomem(s->srvaddr, c)) return;
237
238         sock_ip_req = dcerpc_pipe_open_socket_send(c, s->conn, s->localaddr,
239                                                      s->srvaddr, s->target_hostname,
240                                                      NULL,
241                                                      NCACN_IP_TCP);
242         composite_continue(c, sock_ip_req, continue_ip_open_socket, c);
243 }
244
245
246 /*
247   Stage 2 of dcerpc_pipe_open_tcp_send: receive result of pipe open request
248   on IP transport.
249 */
250 static void continue_ip_open_socket(struct composite_context *ctx)
251 {
252         struct composite_context *c = talloc_get_type_abort(
253                 ctx->async.private_data, struct composite_context);
254         struct pipe_tcp_state *s = talloc_get_type_abort(
255                 c->private_data, struct pipe_tcp_state);
256         
257         /* receive result socket open request */
258         c->status = dcerpc_pipe_open_socket_recv(ctx, NULL, NULL);
259         if (!NT_STATUS_IS_OK(c->status)) {
260                 /* something went wrong... */
261                 DEBUG(0, ("Failed to connect host %s (%s) on port %d - %s.\n",
262                           s->addresses[s->index - 1], s->target_hostname,
263                           s->port, nt_errstr(c->status)));
264                 if (s->addresses[s->index]) {
265                         struct composite_context *sock_ip_req;
266                         talloc_free(s->srvaddr);
267                         /* prepare server address using host ip:port and transport name */
268                         s->srvaddr = socket_address_from_strings(s->conn, "ip", s->addresses[s->index], s->port);
269                         s->index++;
270                         if (composite_nomem(s->srvaddr, c)) return;
271
272                         sock_ip_req = dcerpc_pipe_open_socket_send(c, s->conn, s->localaddr,
273                                                                 s->srvaddr, s->target_hostname,
274                                                                 NULL,
275                                                                 NCACN_IP_TCP);
276                         composite_continue(c, sock_ip_req, continue_ip_open_socket, c);
277
278                         return;
279                 } else {
280                         composite_error(c, c->status);
281                         return;
282                 }
283         }
284
285         composite_done(c);
286 }
287
288 /*
289   Send rpc pipe open request to given host:port using
290   tcp/ip transport
291 */
292 struct composite_context* dcerpc_pipe_open_tcp_send(struct dcecli_connection *conn,
293                                                     const char *localaddr,
294                                                     const char *server,
295                                                     const char *target_hostname,
296                                                     uint32_t port,
297                                                     struct resolve_context *resolve_ctx)
298 {
299         struct composite_context *c;
300         struct pipe_tcp_state *s;
301         struct composite_context *resolve_req;
302         struct nbt_name name;
303
304         /* composite context allocation and setup */
305         c = composite_create(conn, conn->event_ctx);
306         if (c == NULL) return NULL;
307
308         s = talloc_zero(c, struct pipe_tcp_state);
309         if (composite_nomem(s, c)) return c;
310         c->private_data = s;
311
312         /* store input parameters in state structure */
313         s->server          = talloc_strdup(c, server);
314         if (composite_nomem(s->server, c)) return c;
315         if (target_hostname) {
316                 s->target_hostname = talloc_strdup(c, target_hostname);
317                 if (composite_nomem(s->target_hostname, c)) return c;
318         }
319         s->port            = port;
320         s->conn            = conn;
321         s->resolve_ctx     = resolve_ctx;
322         if (localaddr) {
323                 s->localaddr = socket_address_from_strings(s, "ip", localaddr, 0);
324                 /* if there is no localaddr, we pass NULL for
325                    s->localaddr, which is handled by the socket libraries as
326                    meaning no local binding address specified */
327         }
328
329         make_nbt_name_server(&name, server);
330         resolve_req = resolve_name_send(resolve_ctx, s, &name, c->event_ctx);
331         composite_continue(c, resolve_req, continue_ip_resolve_name, c);
332         return c;
333 }
334
335 /*
336   Receive result of pipe open request on tcp/ip
337 */
338 NTSTATUS dcerpc_pipe_open_tcp_recv(struct composite_context *c)
339 {
340         NTSTATUS status;
341         status = composite_wait(c);
342
343         talloc_free(c);
344         return status;
345 }
346
347
348 struct pipe_unix_state {
349         const char *path;
350         struct socket_address *srvaddr;
351         struct dcecli_connection *conn;
352 };
353
354
355 /*
356   Stage 2 of dcerpc_pipe_open_unix_stream_send: receive result of pipe open
357   request on unix socket.
358 */
359 static void continue_unix_open_socket(struct composite_context *ctx)
360 {
361         struct composite_context *c = talloc_get_type_abort(
362                 ctx->async.private_data, struct composite_context);
363
364         c->status = dcerpc_pipe_open_socket_recv(ctx, NULL, NULL);
365         if (NT_STATUS_IS_OK(c->status)) {
366                 composite_done(c);
367                 return;
368         }
369
370         composite_error(c, c->status);
371 }
372
373
374 /*
375   Send pipe open request on unix socket
376 */
377 struct composite_context *dcerpc_pipe_open_unix_stream_send(struct dcecli_connection *conn,
378                                                             const char *path)
379 {
380         struct composite_context *c;
381         struct composite_context *sock_unix_req;
382         struct pipe_unix_state *s;
383
384         /* composite context allocation and setup */
385         c = composite_create(conn, conn->event_ctx);
386         if (c == NULL) return NULL;
387
388         s = talloc_zero(c, struct pipe_unix_state);
389         if (composite_nomem(s, c)) return c;
390         c->private_data = s;
391
392         /* store parameters in state structure */
393         s->path = talloc_strdup(c, path);
394         if (composite_nomem(s->path, c)) return c;
395         s->conn = conn;
396
397         /* prepare server address using socket path and transport name */
398         s->srvaddr = socket_address_from_strings(conn, "unix", s->path, 0);
399         if (composite_nomem(s->srvaddr, c)) return c;
400
401         /* send socket open request */
402         sock_unix_req = dcerpc_pipe_open_socket_send(c, s->conn, NULL,
403                                                      s->srvaddr, NULL,
404                                                      s->path,
405                                                      NCALRPC);
406         composite_continue(c, sock_unix_req, continue_unix_open_socket, c);
407         return c;
408 }
409
410
411 /*
412   Receive result of pipe open request on unix socket
413 */
414 NTSTATUS dcerpc_pipe_open_unix_stream_recv(struct composite_context *c)
415 {
416         NTSTATUS status = composite_wait(c);
417
418         talloc_free(c);
419         return status;
420 }
421
422
423 /*
424   Stage 2 of dcerpc_pipe_open_pipe_send: receive socket open request
425 */
426 static void continue_np_open_socket(struct composite_context *ctx)
427 {
428         struct composite_context *c = talloc_get_type_abort(
429                 ctx->async.private_data, struct composite_context);
430
431         c->status = dcerpc_pipe_open_socket_recv(ctx, NULL, NULL);
432         if (!composite_is_ok(c)) return;
433
434         composite_done(c);
435 }
436
437
438 /*
439   Send pipe open request on ncalrpc
440 */
441 struct composite_context* dcerpc_pipe_open_pipe_send(struct dcecli_connection *conn,
442                                                      const char *ncalrpc_dir,
443                                                      const char *identifier)
444 {
445         char *canon = NULL;
446
447         struct composite_context *c;
448         struct composite_context *sock_np_req;
449         struct pipe_unix_state *s;
450
451         /* composite context allocation and setup */
452         c = composite_create(conn, conn->event_ctx);
453         if (c == NULL) return NULL;
454
455         s = talloc_zero(c, struct pipe_unix_state);
456         if (composite_nomem(s, c)) return c;
457         c->private_data = s;
458
459         /* store parameters in state structure */
460         canon = talloc_strdup(s, identifier);
461         if (composite_nomem(canon, c)) return c;
462         s->conn = conn;
463
464         string_replace(canon, '/', '\\');
465         s->path = talloc_asprintf(canon, "%s/%s", ncalrpc_dir, canon);
466         if (composite_nomem(s->path, c)) return c;
467
468         /* prepare server address using path and transport name */
469         s->srvaddr = socket_address_from_strings(conn, "unix", s->path, 0);
470         if (composite_nomem(s->srvaddr, c)) return c;
471
472         /* send socket open request */
473         sock_np_req = dcerpc_pipe_open_socket_send(c, s->conn, NULL, s->srvaddr, NULL, s->path, NCALRPC);
474         composite_continue(c, sock_np_req, continue_np_open_socket, c);
475         return c;
476 }
477
478
479 /*
480   Receive result of pipe open request on ncalrpc
481 */
482 NTSTATUS dcerpc_pipe_open_pipe_recv(struct composite_context *c)
483 {
484         NTSTATUS status = composite_wait(c);
485         
486         talloc_free(c);
487         return status;
488 }
489
490
491 /*
492   Open a rpc pipe on a named pipe - sync version
493 */
494 NTSTATUS dcerpc_pipe_open_pipe(struct dcecli_connection *conn, const char *ncalrpc_dir, const char *identifier)
495 {
496         struct composite_context *c = dcerpc_pipe_open_pipe_send(conn, ncalrpc_dir, identifier);
497         return dcerpc_pipe_open_pipe_recv(c);
498 }
499
500 const char *dcerpc_unix_socket_path(struct dcecli_connection *p)
501 {
502         struct sock_private *sock = talloc_get_type_abort(
503                 p->transport.private_data, struct sock_private);
504         return sock->path;
505 }
506
507 struct socket_address *dcerpc_socket_peer_addr(struct dcecli_connection *p, TALLOC_CTX *mem_ctx)
508 {
509         struct sock_private *sock = talloc_get_type_abort(
510                 p->transport.private_data, struct sock_private);
511         return socket_address_copy(mem_ctx, sock->peer_addr);
512 }
513