removed an unused variable
[samba-svnmirror.git] / source / 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 event_context *event_ctx;
40         struct fd_event *fde;
41         struct socket_context *sock;
42         char *server_name;
43
44         struct sock_blob *pending_send;
45
46         struct {
47                 size_t received;
48                 DATA_BLOB data;
49                 uint_t pending_count;
50         } recv;
51 };
52
53
54 /*
55   mark the socket dead
56 */
57 static void sock_dead(struct dcerpc_connection *p, NTSTATUS status)
58 {
59         struct sock_private *sock = p->transport.private;
60
61         if (sock && sock->sock != NULL) {
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         talloc_free(sock->fde);
78 }
79
80 /*
81   process send requests
82 */
83 static void sock_process_send(struct dcerpc_connection *p)
84 {
85         struct sock_private *sock = p->transport.private;
86
87         while (sock->pending_send) {
88                 struct sock_blob *blob = sock->pending_send;
89                 NTSTATUS status;
90                 size_t sent;
91                 status = socket_send(sock->sock, &blob->data, &sent, 0);
92                 if (NT_STATUS_IS_ERR(status)) {
93                         sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
94                         break;
95                 }
96                 if (sent == 0) {
97                         break;
98                 }
99
100                 blob->data.data += sent;
101                 blob->data.length -= sent;
102
103                 if (blob->data.length != 0) {
104                         break;
105                 }
106
107                 DLIST_REMOVE(sock->pending_send, blob);
108                 talloc_free(blob);
109         }
110
111         if (sock->pending_send == NULL) {
112                 EVENT_FD_NOT_WRITEABLE(sock->fde);
113         }
114 }
115
116
117 /*
118   process recv requests
119 */
120 static void sock_process_recv(struct dcerpc_connection *p)
121 {
122         struct sock_private *sock = p->transport.private;
123         NTSTATUS status;
124         size_t nread;
125
126         if (sock->recv.data.data == NULL) {
127                 sock->recv.data = data_blob_talloc(sock, NULL, MIN_HDR_SIZE);
128         }
129
130         /* read in the base header to get the fragment length */
131         if (sock->recv.received < MIN_HDR_SIZE) {
132                 uint32_t frag_length;
133
134                 status = socket_recv(sock->sock, 
135                                      sock->recv.data.data + sock->recv.received, 
136                                      MIN_HDR_SIZE - sock->recv.received, 
137                                      &nread, 0);
138                 if (NT_STATUS_IS_ERR(status)) {
139                         sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
140                         return;
141                 }
142                 if (nread == 0) {
143                         return;
144                 }
145                 
146                 sock->recv.received += nread;
147
148                 if (sock->recv.received != MIN_HDR_SIZE) {
149                         return;
150                 }
151                 frag_length = dcerpc_get_frag_length(&sock->recv.data);
152
153                 sock->recv.data.data = talloc_realloc(sock, sock->recv.data.data,
154                                                       uint8_t, frag_length);
155                 if (sock->recv.data.data == NULL) {
156                         sock_dead(p, NT_STATUS_NO_MEMORY);
157                         return;
158                 }
159                 sock->recv.data.length = frag_length;
160         }
161
162         /* read in the rest of the packet */
163         status = socket_recv(sock->sock, 
164                              sock->recv.data.data + sock->recv.received, 
165                              sock->recv.data.length - sock->recv.received, 
166                              &nread, 0);
167         if (NT_STATUS_IS_ERR(status)) {
168                 sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
169                 return;
170         }
171         if (nread == 0) {
172                 return;
173         }
174         sock->recv.received += nread;
175
176         if (sock->recv.received != sock->recv.data.length) {
177                 return;
178         }
179
180         /* we have a full packet */
181         p->transport.recv_data(p, &sock->recv.data, NT_STATUS_OK);
182         talloc_free(sock->recv.data.data);
183         sock->recv.data = data_blob(NULL, 0);
184         sock->recv.received = 0;
185         sock->recv.pending_count--;
186         if (sock->recv.pending_count == 0) {
187                 EVENT_FD_NOT_READABLE(sock->fde);
188         }
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    return the event context so the caller can process asynchronously
264 */
265 static struct event_context *sock_event_context(struct dcerpc_connection *p)
266 {
267         struct sock_private *sock = p->transport.private;
268
269         return sock->event_ctx;
270 }
271
272 /* 
273    shutdown sock pipe connection
274 */
275 static NTSTATUS sock_shutdown_pipe(struct dcerpc_connection *p)
276 {
277         struct sock_private *sock = p->transport.private;
278
279         if (sock && sock->sock) {
280                 sock_dead(p, NT_STATUS_OK);
281         }
282
283         return NT_STATUS_OK;
284 }
285
286 /*
287   return sock server name
288 */
289 static const char *sock_peer_name(struct dcerpc_connection *p)
290 {
291         struct sock_private *sock = p->transport.private;
292         return sock->server_name;
293 }
294
295 /* 
296    open a rpc connection using the generic socket library
297 */
298 static NTSTATUS dcerpc_pipe_open_socket(struct dcerpc_connection *c, 
299                                         const char *server,
300                                         uint32_t port, 
301                                         const char *type,
302                                         enum dcerpc_transport_t transport)
303 {
304         struct sock_private *sock;
305         struct socket_context *socket_ctx;
306         NTSTATUS status;
307
308         sock = talloc(c, struct sock_private);
309         if (!sock) {
310                 return NT_STATUS_NO_MEMORY;
311         }
312
313         status = socket_create(type, SOCKET_TYPE_STREAM, &socket_ctx, 0);
314         if (!NT_STATUS_IS_OK(status)) {
315                 talloc_free(sock);
316                 return status;
317         }
318         talloc_steal(sock, socket_ctx);
319
320         status = socket_connect(socket_ctx, NULL, 0, server, port, 0);
321         if (!NT_STATUS_IS_OK(status)) {
322                 talloc_free(sock);
323                 return status;
324         }
325
326         /*
327           fill in the transport methods
328         */
329         c->transport.transport = transport;
330         c->transport.private = NULL;
331
332         c->transport.send_request = sock_send_request;
333         c->transport.send_read = sock_send_read;
334         c->transport.event_context = sock_event_context;
335         c->transport.recv_data = NULL;
336
337         c->transport.shutdown_pipe = sock_shutdown_pipe;
338         c->transport.peer_name = sock_peer_name;
339         
340         sock->sock = socket_ctx;
341         sock->server_name = strupper_talloc(sock, server);
342         sock->event_ctx = event_context_init(sock);
343         sock->pending_send = NULL;
344         sock->recv.received = 0;
345         sock->recv.data = data_blob(NULL, 0);
346         sock->recv.pending_count = 0;
347
348         sock->fde = event_add_fd(sock->event_ctx, sock, socket_get_fd(sock->sock), 
349                                  0, sock_io_handler, c);
350
351         c->transport.private = sock;
352
353         /* ensure we don't get SIGPIPE */
354         BlockSignals(True,SIGPIPE);
355
356         return NT_STATUS_OK;
357 }
358
359 /* 
360    open a rpc connection using tcp
361 */
362 NTSTATUS dcerpc_pipe_open_tcp(struct dcerpc_connection *c, const char *server, uint32_t port)
363 {
364         NTSTATUS status;
365         
366         /* Try IPv6 first */
367         status = dcerpc_pipe_open_socket(c, server, port, "ipv6", NCACN_IP_TCP);
368         if (NT_STATUS_IS_OK(status)) {
369                 return status;
370         }
371         
372         return dcerpc_pipe_open_socket(c, server, port, "ipv4", NCACN_IP_TCP);
373 }
374
375 /* 
376    open a rpc connection to a unix socket 
377 */
378 NTSTATUS dcerpc_pipe_open_unix_stream(struct dcerpc_connection *c, const char *path)
379 {
380         return dcerpc_pipe_open_socket(c, path, 0, "unix", NCACN_UNIX_STREAM);
381 }
382
383 /* 
384    open a rpc connection to a named pipe 
385 */
386 NTSTATUS dcerpc_pipe_open_pipe(struct dcerpc_connection *c, const char *identifier)
387 {
388         NTSTATUS status;
389         char *canon, *full_path;
390
391         canon = talloc_strdup(NULL, identifier);
392
393         string_replace(canon, '/', '\\');
394         full_path = talloc_asprintf(canon, "%s/%s", lp_ncalrpc_dir(), canon);
395
396         status = dcerpc_pipe_open_socket(c, full_path, 0, "unix", NCALRPC);
397         talloc_free(canon);
398
399         return status;
400 }