r10699: fixed the dcerpc code so that you can shutdown the pipe safely from
[abartlet/samba.git/.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    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "dlinklist.h"
26 #include "lib/events/events.h"
27 #include "librpc/gen_ndr/ndr_epmapper.h"
28 #include "lib/socket/socket.h"
29
30 #define MIN_HDR_SIZE 16
31
32 struct sock_blob {
33         struct sock_blob *next, *prev;
34         DATA_BLOB data;
35 };
36
37 /* transport private information used by general socket pipe transports */
38 struct sock_private {
39         struct fd_event *fde;
40         struct socket_context *sock;
41         char *server_name;
42
43         struct sock_blob *pending_send;
44
45         struct {
46                 size_t received;
47                 DATA_BLOB data;
48                 uint_t pending_count;
49         } recv;
50 };
51
52
53 /*
54   mark the socket dead
55 */
56 static void sock_dead(struct dcerpc_connection *p, NTSTATUS status)
57 {
58         struct sock_private *sock = p->transport.private;
59
60         if (sock && sock->sock != NULL) {
61                 talloc_free(sock->fde);
62                 talloc_free(sock->sock);
63                 sock->sock = NULL;
64         }
65
66         /* wipe any pending sends */
67         while (sock->pending_send) {
68                 struct sock_blob *blob = sock->pending_send;
69                 DLIST_REMOVE(sock->pending_send, blob);
70                 talloc_free(blob);
71         }
72
73         if (!NT_STATUS_IS_OK(status)) {
74                 p->transport.recv_data(p, NULL, status);
75         }
76 }
77
78 /*
79   process send requests
80 */
81 static void sock_process_send(struct dcerpc_connection *p)
82 {
83         struct sock_private *sock = p->transport.private;
84
85         while (sock->pending_send) {
86                 struct sock_blob *blob = sock->pending_send;
87                 NTSTATUS status;
88                 size_t sent;
89                 status = socket_send(sock->sock, &blob->data, &sent, 0);
90                 if (NT_STATUS_IS_ERR(status)) {
91                         sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
92                         break;
93                 }
94                 if (sent == 0) {
95                         break;
96                 }
97
98                 blob->data.data += sent;
99                 blob->data.length -= sent;
100
101                 if (blob->data.length != 0) {
102                         break;
103                 }
104
105                 DLIST_REMOVE(sock->pending_send, blob);
106                 talloc_free(blob);
107         }
108
109         if (sock->pending_send == NULL && sock->sock) {
110                 EVENT_FD_NOT_WRITEABLE(sock->fde);
111         }
112 }
113
114
115 /*
116   process recv requests
117 */
118 static void sock_process_recv(struct dcerpc_connection *p)
119 {
120         struct sock_private *sock = p->transport.private;
121         NTSTATUS status;
122         size_t nread;
123         DATA_BLOB data;
124
125         if (sock->recv.data.data == NULL) {
126                 sock->recv.data = data_blob_talloc(sock, NULL, MIN_HDR_SIZE);
127         }
128
129         /* read in the base header to get the fragment length */
130         if (sock->recv.received < MIN_HDR_SIZE) {
131                 uint32_t frag_length;
132
133                 status = socket_recv(sock->sock, 
134                                      sock->recv.data.data + sock->recv.received, 
135                                      MIN_HDR_SIZE - sock->recv.received, 
136                                      &nread, 0);
137                 if (NT_STATUS_IS_ERR(status)) {
138                         sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
139                         return;
140                 }
141                 if (nread == 0) {
142                         return;
143                 }
144                 
145                 sock->recv.received += nread;
146
147                 if (sock->recv.received != MIN_HDR_SIZE) {
148                         return;
149                 }
150                 frag_length = dcerpc_get_frag_length(&sock->recv.data);
151
152                 sock->recv.data.data = talloc_realloc(sock, sock->recv.data.data,
153                                                       uint8_t, frag_length);
154                 if (sock->recv.data.data == NULL) {
155                         sock_dead(p, NT_STATUS_NO_MEMORY);
156                         return;
157                 }
158                 sock->recv.data.length = frag_length;
159         }
160
161         /* read in the rest of the packet */
162         status = socket_recv(sock->sock, 
163                              sock->recv.data.data + sock->recv.received, 
164                              sock->recv.data.length - sock->recv.received, 
165                              &nread, 0);
166         if (NT_STATUS_IS_ERR(status)) {
167                 sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
168                 return;
169         }
170         if (nread == 0) {
171                 return;
172         }
173         sock->recv.received += nread;
174
175         if (sock->recv.received != sock->recv.data.length) {
176                 return;
177         }
178
179         /* we have a full packet */
180         data = sock->recv.data;
181         sock->recv.data = data_blob(NULL, 0);
182         sock->recv.received = 0;
183         sock->recv.pending_count--;
184         if (sock->recv.pending_count == 0) {
185                 EVENT_FD_NOT_READABLE(sock->fde);
186         }
187
188         p->transport.recv_data(p, &data, NT_STATUS_OK);
189 }
190
191 /*
192   called when a IO is triggered by the events system
193 */
194 static void sock_io_handler(struct event_context *ev, struct fd_event *fde, 
195                             uint16_t flags, void *private)
196 {
197         struct dcerpc_connection *p = talloc_get_type(private, struct dcerpc_connection);
198         struct sock_private *sock = p->transport.private;
199
200         if (flags & EVENT_FD_WRITE) {
201                 sock_process_send(p);
202                 return;
203         }
204
205         if (sock->sock == NULL) {
206                 return;
207         }
208
209         if (flags & EVENT_FD_READ) {
210                 sock_process_recv(p);
211         }
212 }
213
214 /* 
215    initiate a read request 
216 */
217 static NTSTATUS sock_send_read(struct dcerpc_connection *p)
218 {
219         struct sock_private *sock = p->transport.private;
220
221         sock->recv.pending_count++;
222         if (sock->recv.pending_count == 1) {
223                 EVENT_FD_READABLE(sock->fde);
224         }
225         return NT_STATUS_OK;
226 }
227
228 /* 
229    send an initial pdu in a multi-pdu sequence
230 */
231 static NTSTATUS sock_send_request(struct dcerpc_connection *p, DATA_BLOB *data, BOOL trigger_read)
232 {
233         struct sock_private *sock = p->transport.private;
234         struct sock_blob *blob;
235
236         if (sock->sock == NULL) {
237                 return NT_STATUS_CONNECTION_DISCONNECTED;
238         }
239
240         blob = talloc(sock, struct sock_blob);
241         if (blob == NULL) {
242                 return NT_STATUS_NO_MEMORY;
243         }
244
245         blob->data = data_blob_talloc(blob, data->data, data->length);
246         if (blob->data.data == NULL) {
247                 talloc_free(blob);
248                 return NT_STATUS_NO_MEMORY;
249         }
250
251         DLIST_ADD_END(sock->pending_send, blob, struct sock_blob *);
252
253         EVENT_FD_WRITEABLE(sock->fde);
254
255         if (trigger_read) {
256                 sock_send_read(p);
257         }
258
259         return NT_STATUS_OK;
260 }
261
262 /* 
263    shutdown sock pipe connection
264 */
265 static NTSTATUS sock_shutdown_pipe(struct dcerpc_connection *p)
266 {
267         struct sock_private *sock = p->transport.private;
268
269         if (sock && sock->sock) {
270                 sock_dead(p, NT_STATUS_OK);
271         }
272
273         return NT_STATUS_OK;
274 }
275
276 /*
277   return sock server name
278 */
279 static const char *sock_peer_name(struct dcerpc_connection *p)
280 {
281         struct sock_private *sock = p->transport.private;
282         return sock->server_name;
283 }
284
285 /* 
286    open a rpc connection using the generic socket library
287 */
288 static NTSTATUS dcerpc_pipe_open_socket(struct dcerpc_connection *c, 
289                                         const char *server,
290                                         uint32_t port, 
291                                         const char *type,
292                                         enum dcerpc_transport_t transport)
293 {
294         struct sock_private *sock;
295         struct socket_context *socket_ctx;
296         NTSTATUS status;
297
298         sock = talloc(c, struct sock_private);
299         if (!sock) {
300                 return NT_STATUS_NO_MEMORY;
301         }
302
303         status = socket_create(type, SOCKET_TYPE_STREAM, &socket_ctx, 0);
304         if (!NT_STATUS_IS_OK(status)) {
305                 talloc_free(sock);
306                 return status;
307         }
308         talloc_steal(sock, socket_ctx);
309
310         status = socket_connect_ev(socket_ctx, NULL, 0, server, port, 0, c->event_ctx);
311         if (!NT_STATUS_IS_OK(status)) {
312                 talloc_free(sock);
313                 return status;
314         }
315
316         /*
317           fill in the transport methods
318         */
319         c->transport.transport = transport;
320         c->transport.private = NULL;
321
322         c->transport.send_request = sock_send_request;
323         c->transport.send_read = sock_send_read;
324         c->transport.recv_data = NULL;
325
326         c->transport.shutdown_pipe = sock_shutdown_pipe;
327         c->transport.peer_name = sock_peer_name;
328         
329         sock->sock = socket_ctx;
330         sock->server_name = strupper_talloc(sock, server);
331         sock->pending_send = NULL;
332         sock->recv.received = 0;
333         sock->recv.data = data_blob(NULL, 0);
334         sock->recv.pending_count = 0;
335
336         sock->fde = event_add_fd(c->event_ctx, sock->sock, socket_get_fd(sock->sock), 
337                                  0, sock_io_handler, c);
338
339         c->transport.private = sock;
340
341         /* ensure we don't get SIGPIPE */
342         BlockSignals(True,SIGPIPE);
343
344         return NT_STATUS_OK;
345 }
346
347 /* 
348    open a rpc connection using tcp
349 */
350 NTSTATUS dcerpc_pipe_open_tcp(struct dcerpc_connection *c, const char *server, uint32_t port)
351 {
352         NTSTATUS status;
353         
354         /* Try IPv6 first */
355         status = dcerpc_pipe_open_socket(c, server, port, "ipv6", NCACN_IP_TCP);
356         if (NT_STATUS_IS_OK(status)) {
357                 return status;
358         }
359         
360         return dcerpc_pipe_open_socket(c, server, port, "ipv4", NCACN_IP_TCP);
361 }
362
363 /* 
364    open a rpc connection to a unix socket 
365 */
366 NTSTATUS dcerpc_pipe_open_unix_stream(struct dcerpc_connection *c, const char *path)
367 {
368         return dcerpc_pipe_open_socket(c, path, 0, "unix", NCACN_UNIX_STREAM);
369 }
370
371 /* 
372    open a rpc connection to a named pipe 
373 */
374 NTSTATUS dcerpc_pipe_open_pipe(struct dcerpc_connection *c, const char *identifier)
375 {
376         NTSTATUS status;
377         char *canon, *full_path;
378
379         canon = talloc_strdup(NULL, identifier);
380
381         string_replace(canon, '/', '\\');
382         full_path = talloc_asprintf(canon, "%s/%s", lp_ncalrpc_dir(), canon);
383
384         status = dcerpc_pipe_open_socket(c, full_path, 0, "unix", NCALRPC);
385         talloc_free(canon);
386
387         return status;
388 }