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