r5928: Use cli_credentials in:
[gd/samba-autobuild/.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         uint32_t port;
44
45         struct sock_blob *pending_send;
46
47         struct {
48                 size_t received;
49                 DATA_BLOB data;
50                 uint_t pending_count;
51         } recv;
52 };
53
54
55 /*
56   mark the socket dead
57 */
58 static void sock_dead(struct dcerpc_connection *p, NTSTATUS status)
59 {
60         struct sock_private *sock = p->transport.private;
61
62         if (sock && sock->sock != NULL) {
63                 talloc_free(sock->sock);
64                 sock->sock = NULL;
65         }
66
67         /* wipe any pending sends */
68         while (sock->pending_send) {
69                 struct sock_blob *blob = sock->pending_send;
70                 DLIST_REMOVE(sock->pending_send, blob);
71                 talloc_free(blob);
72         }
73
74         if (!NT_STATUS_IS_OK(status)) {
75                 p->transport.recv_data(p, NULL, status);
76         }
77
78         talloc_free(sock->fde);
79 }
80
81 /*
82   process send requests
83 */
84 static void sock_process_send(struct dcerpc_connection *p)
85 {
86         struct sock_private *sock = p->transport.private;
87
88         while (sock->pending_send) {
89                 struct sock_blob *blob = sock->pending_send;
90                 NTSTATUS status;
91                 size_t sent;
92                 status = socket_send(sock->sock, &blob->data, &sent, 0);
93                 if (NT_STATUS_IS_ERR(status)) {
94                         sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
95                         break;
96                 }
97                 if (sent == 0) {
98                         break;
99                 }
100
101                 blob->data.data += sent;
102                 blob->data.length -= sent;
103
104                 if (blob->data.length != 0) {
105                         break;
106                 }
107
108                 DLIST_REMOVE(sock->pending_send, blob);
109                 talloc_free(blob);
110         }
111
112         if (sock->pending_send == NULL) {
113                 EVENT_FD_NOT_WRITEABLE(sock->fde);
114         }
115 }
116
117
118 /*
119   process recv requests
120 */
121 static void sock_process_recv(struct dcerpc_connection *p)
122 {
123         struct sock_private *sock = p->transport.private;
124         NTSTATUS status;
125         size_t nread;
126
127         if (sock->recv.data.data == NULL) {
128                 sock->recv.data = data_blob_talloc(sock, NULL, MIN_HDR_SIZE);
129         }
130
131         /* read in the base header to get the fragment length */
132         if (sock->recv.received < MIN_HDR_SIZE) {
133                 uint32_t frag_length;
134
135                 status = socket_recv(sock->sock, 
136                                      sock->recv.data.data + sock->recv.received, 
137                                      MIN_HDR_SIZE - sock->recv.received, 
138                                      &nread, 0);
139                 if (NT_STATUS_IS_ERR(status)) {
140                         sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
141                         return;
142                 }
143                 if (nread == 0) {
144                         return;
145                 }
146                 
147                 sock->recv.received += nread;
148
149                 if (sock->recv.received != MIN_HDR_SIZE) {
150                         return;
151                 }
152                 frag_length = dcerpc_get_frag_length(&sock->recv.data);
153
154                 sock->recv.data.data = talloc_realloc(sock, sock->recv.data.data,
155                                                       uint8_t, frag_length);
156                 if (sock->recv.data.data == NULL) {
157                         sock_dead(p, NT_STATUS_NO_MEMORY);
158                         return;
159                 }
160                 sock->recv.data.length = frag_length;
161         }
162
163         /* read in the rest of the packet */
164         status = socket_recv(sock->sock, 
165                              sock->recv.data.data + sock->recv.received, 
166                              sock->recv.data.length - sock->recv.received, 
167                              &nread, 0);
168         if (NT_STATUS_IS_ERR(status)) {
169                 sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
170                 return;
171         }
172         if (nread == 0) {
173                 return;
174         }
175         sock->recv.received += nread;
176
177         if (sock->recv.received != sock->recv.data.length) {
178                 return;
179         }
180
181         /* we have a full packet */
182         p->transport.recv_data(p, &sock->recv.data, NT_STATUS_OK);
183         talloc_free(sock->recv.data.data);
184         sock->recv.data = data_blob(NULL, 0);
185         sock->recv.received = 0;
186         sock->recv.pending_count--;
187         if (sock->recv.pending_count == 0) {
188                 EVENT_FD_NOT_READABLE(sock->fde);
189         }
190 }
191
192 /*
193   called when a IO is triggered by the events system
194 */
195 static void sock_io_handler(struct event_context *ev, struct fd_event *fde, 
196                             uint16_t flags, void *private)
197 {
198         struct dcerpc_connection *p = talloc_get_type(private, struct dcerpc_connection);
199         struct sock_private *sock = p->transport.private;
200
201         if (flags & EVENT_FD_WRITE) {
202                 sock_process_send(p);
203                 return;
204         }
205
206         if (sock->sock == NULL) {
207                 return;
208         }
209
210         if (flags & EVENT_FD_READ) {
211                 sock_process_recv(p);
212         }
213 }
214
215 /* 
216    initiate a read request 
217 */
218 static NTSTATUS sock_send_read(struct dcerpc_connection *p)
219 {
220         struct sock_private *sock = p->transport.private;
221
222         sock->recv.pending_count++;
223         if (sock->recv.pending_count == 1) {
224                 EVENT_FD_READABLE(sock->fde);
225         }
226         return NT_STATUS_OK;
227 }
228
229 /* 
230    send an initial pdu in a multi-pdu sequence
231 */
232 static NTSTATUS sock_send_request(struct dcerpc_connection *p, DATA_BLOB *data, BOOL trigger_read)
233 {
234         struct sock_private *sock = p->transport.private;
235         struct sock_blob *blob;
236
237         if (sock->sock == NULL) {
238                 return NT_STATUS_CONNECTION_DISCONNECTED;
239         }
240
241         blob = talloc(sock, struct sock_blob);
242         if (blob == NULL) {
243                 return NT_STATUS_NO_MEMORY;
244         }
245
246         blob->data = data_blob_talloc(blob, data->data, data->length);
247         if (blob->data.data == NULL) {
248                 talloc_free(blob);
249                 return NT_STATUS_NO_MEMORY;
250         }
251
252         DLIST_ADD_END(sock->pending_send, blob, struct sock_blob *);
253
254         EVENT_FD_WRITEABLE(sock->fde);
255
256         if (trigger_read) {
257                 sock_send_read(p);
258         }
259
260         return NT_STATUS_OK;
261 }
262
263 /* 
264    return the event context so the caller can process asynchronously
265 */
266 static struct event_context *sock_event_context(struct dcerpc_connection *p)
267 {
268         struct sock_private *sock = p->transport.private;
269
270         return sock->event_ctx;
271 }
272
273 /* 
274    shutdown sock pipe connection
275 */
276 static NTSTATUS sock_shutdown_pipe(struct dcerpc_connection *p)
277 {
278         sock_dead(p, NT_STATUS_OK);
279
280         return NT_STATUS_OK;
281 }
282
283 /*
284   return sock server name
285 */
286 static const char *sock_peer_name(struct dcerpc_connection *p)
287 {
288         struct sock_private *sock = p->transport.private;
289         return sock->server_name;
290 }
291
292 /* 
293    open a rpc connection using the generic socket library
294 */
295 static NTSTATUS dcerpc_pipe_open_socket(struct dcerpc_connection *c, 
296                                         const char *server,
297                                         uint32_t port, 
298                                         const char *type,
299                                         enum dcerpc_transport_t transport)
300 {
301         struct sock_private *sock;
302         struct socket_context *socket_ctx;
303         NTSTATUS status;
304
305         sock = talloc(c, struct sock_private);
306         if (!sock) {
307                 return NT_STATUS_NO_MEMORY;
308         }
309
310         status = socket_create(type, SOCKET_TYPE_STREAM, &socket_ctx, 0);
311         if (!NT_STATUS_IS_OK(status)) {
312                 talloc_free(sock);
313                 return status;
314         }
315         talloc_steal(sock, socket_ctx);
316
317         status = socket_connect(socket_ctx, NULL, 0, server, port, 0);
318         if (!NT_STATUS_IS_OK(status)) {
319                 talloc_free(sock);
320                 return status;
321         }
322
323         /*
324           fill in the transport methods
325         */
326         c->transport.transport = transport;
327         c->transport.private = NULL;
328
329         c->transport.send_request = sock_send_request;
330         c->transport.send_read = sock_send_read;
331         c->transport.event_context = sock_event_context;
332         c->transport.recv_data = NULL;
333
334         c->transport.shutdown_pipe = sock_shutdown_pipe;
335         c->transport.peer_name = sock_peer_name;
336         
337         sock->sock = socket_ctx;
338         sock->server_name = strupper_talloc(sock, server);
339         sock->event_ctx = event_context_init(sock);
340         sock->pending_send = NULL;
341         sock->recv.received = 0;
342         sock->recv.data = data_blob(NULL, 0);
343         sock->recv.pending_count = 0;
344
345         sock->fde = event_add_fd(sock->event_ctx, sock, socket_get_fd(sock->sock), 
346                                  0, sock_io_handler, c);
347
348         c->transport.private = sock;
349
350         /* ensure we don't get SIGPIPE */
351         BlockSignals(True,SIGPIPE);
352
353         return NT_STATUS_OK;
354 }
355
356 /* 
357    open a rpc connection using tcp
358 */
359 NTSTATUS dcerpc_pipe_open_tcp(struct dcerpc_connection *c, const char *server, uint32_t port)
360 {
361         NTSTATUS status;
362         
363         /* Try IPv6 first */
364         status = dcerpc_pipe_open_socket(c, server, port, "ipv6", NCACN_IP_TCP);
365         if (NT_STATUS_IS_OK(status)) {
366                 return status;
367         }
368         
369         return dcerpc_pipe_open_socket(c, server, port, "ipv4", NCACN_IP_TCP);
370 }
371
372 /* 
373    open a rpc connection to a unix socket 
374 */
375 NTSTATUS dcerpc_pipe_open_unix_stream(struct dcerpc_connection *c, const char *path)
376 {
377         return dcerpc_pipe_open_socket(c, path, 0, "unix", NCACN_UNIX_STREAM);
378 }
379
380 /* 
381    open a rpc connection to a named pipe 
382 */
383 NTSTATUS dcerpc_pipe_open_pipe(struct dcerpc_connection *c, const char *identifier)
384 {
385         NTSTATUS status;
386         char *canon, *full_path;
387
388         canon = talloc_strdup(NULL, identifier);
389
390         string_replace(canon, '/', '\\');
391         full_path = talloc_asprintf(canon, "%s/%s", lp_ncalrpc_dir(), canon);
392
393         status = dcerpc_pipe_open_socket(c, full_path, 0, "unix", NCALRPC);
394         talloc_free(canon);
395
396         return status;
397 }