2 Unix SMB/CIFS implementation.
4 dcerpc over standard sockets transport
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Jelmer Vernooij 2004
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.
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.
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.
25 #include "dlinklist.h"
26 #include "lib/events/events.h"
27 #include "librpc/gen_ndr/ndr_epmapper.h"
28 #include "lib/socket/socket.h"
30 #define MIN_HDR_SIZE 16
33 struct sock_blob *next, *prev;
37 /* transport private information used by general socket pipe transports */
40 struct socket_context *sock;
43 struct sock_blob *pending_send;
56 static void sock_dead(struct dcerpc_connection *p, NTSTATUS status)
58 struct sock_private *sock = p->transport.private;
60 if (sock && sock->sock != NULL) {
61 talloc_free(sock->fde);
62 talloc_free(sock->sock);
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);
73 if (!NT_STATUS_IS_OK(status)) {
74 p->transport.recv_data(p, NULL, status);
81 static void sock_process_send(struct dcerpc_connection *p)
83 struct sock_private *sock = p->transport.private;
85 while (sock->pending_send) {
86 struct sock_blob *blob = sock->pending_send;
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);
98 blob->data.data += sent;
99 blob->data.length -= sent;
101 if (blob->data.length != 0) {
105 DLIST_REMOVE(sock->pending_send, blob);
109 if (sock->pending_send == NULL && sock->sock) {
110 EVENT_FD_NOT_WRITEABLE(sock->fde);
116 process recv requests
118 static void sock_process_recv(struct dcerpc_connection *p)
120 struct sock_private *sock = p->transport.private;
125 if (sock->recv.data.data == NULL) {
126 sock->recv.data = data_blob_talloc(sock, NULL, MIN_HDR_SIZE);
129 /* read in the base header to get the fragment length */
130 if (sock->recv.received < MIN_HDR_SIZE) {
131 uint32_t frag_length;
133 status = socket_recv(sock->sock,
134 sock->recv.data.data + sock->recv.received,
135 MIN_HDR_SIZE - sock->recv.received,
137 if (NT_STATUS_IS_ERR(status)) {
138 sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
145 sock->recv.received += nread;
147 if (sock->recv.received != MIN_HDR_SIZE) {
150 frag_length = dcerpc_get_frag_length(&sock->recv.data);
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);
158 sock->recv.data.length = frag_length;
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,
166 if (NT_STATUS_IS_ERR(status)) {
167 sock_dead(p, NT_STATUS_NET_WRITE_FAULT);
173 sock->recv.received += nread;
175 if (sock->recv.received != sock->recv.data.length) {
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);
188 p->transport.recv_data(p, &data, NT_STATUS_OK);
192 called when a IO is triggered by the events system
194 static void sock_io_handler(struct event_context *ev, struct fd_event *fde,
195 uint16_t flags, void *private)
197 struct dcerpc_connection *p = talloc_get_type(private, struct dcerpc_connection);
198 struct sock_private *sock = p->transport.private;
200 if (flags & EVENT_FD_WRITE) {
201 sock_process_send(p);
205 if (sock->sock == NULL) {
209 if (flags & EVENT_FD_READ) {
210 sock_process_recv(p);
215 initiate a read request
217 static NTSTATUS sock_send_read(struct dcerpc_connection *p)
219 struct sock_private *sock = p->transport.private;
221 sock->recv.pending_count++;
222 if (sock->recv.pending_count == 1) {
223 EVENT_FD_READABLE(sock->fde);
229 send an initial pdu in a multi-pdu sequence
231 static NTSTATUS sock_send_request(struct dcerpc_connection *p, DATA_BLOB *data, BOOL trigger_read)
233 struct sock_private *sock = p->transport.private;
234 struct sock_blob *blob;
236 if (sock->sock == NULL) {
237 return NT_STATUS_CONNECTION_DISCONNECTED;
240 blob = talloc(sock, struct sock_blob);
242 return NT_STATUS_NO_MEMORY;
245 blob->data = data_blob_talloc(blob, data->data, data->length);
246 if (blob->data.data == NULL) {
248 return NT_STATUS_NO_MEMORY;
251 DLIST_ADD_END(sock->pending_send, blob, struct sock_blob *);
253 EVENT_FD_WRITEABLE(sock->fde);
263 shutdown sock pipe connection
265 static NTSTATUS sock_shutdown_pipe(struct dcerpc_connection *p)
267 struct sock_private *sock = p->transport.private;
269 if (sock && sock->sock) {
270 sock_dead(p, NT_STATUS_OK);
277 return sock server name
279 static const char *sock_peer_name(struct dcerpc_connection *p)
281 struct sock_private *sock = p->transport.private;
282 return sock->server_name;
286 open a rpc connection using the generic socket library
288 static NTSTATUS dcerpc_pipe_open_socket(struct dcerpc_connection *c,
292 enum dcerpc_transport_t transport)
294 struct sock_private *sock;
295 struct socket_context *socket_ctx;
298 sock = talloc(c, struct sock_private);
300 return NT_STATUS_NO_MEMORY;
303 status = socket_create(type, SOCKET_TYPE_STREAM, &socket_ctx, 0);
304 if (!NT_STATUS_IS_OK(status)) {
308 talloc_steal(sock, socket_ctx);
310 status = socket_connect_ev(socket_ctx, NULL, 0, server, port, 0, c->event_ctx);
311 if (!NT_STATUS_IS_OK(status)) {
317 fill in the transport methods
319 c->transport.transport = transport;
320 c->transport.private = NULL;
322 c->transport.send_request = sock_send_request;
323 c->transport.send_read = sock_send_read;
324 c->transport.recv_data = NULL;
326 c->transport.shutdown_pipe = sock_shutdown_pipe;
327 c->transport.peer_name = sock_peer_name;
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;
336 sock->fde = event_add_fd(c->event_ctx, sock->sock, socket_get_fd(sock->sock),
337 0, sock_io_handler, c);
339 c->transport.private = sock;
341 /* ensure we don't get SIGPIPE */
342 BlockSignals(True,SIGPIPE);
348 open a rpc connection using tcp
350 NTSTATUS dcerpc_pipe_open_tcp(struct dcerpc_connection *c, const char *server, uint32_t port)
355 status = dcerpc_pipe_open_socket(c, server, port, "ipv6", NCACN_IP_TCP);
356 if (NT_STATUS_IS_OK(status)) {
360 return dcerpc_pipe_open_socket(c, server, port, "ipv4", NCACN_IP_TCP);
364 open a rpc connection to a unix socket
366 NTSTATUS dcerpc_pipe_open_unix_stream(struct dcerpc_connection *c, const char *path)
368 return dcerpc_pipe_open_socket(c, path, 0, "unix", NCACN_UNIX_STREAM);
372 open a rpc connection to a named pipe
374 NTSTATUS dcerpc_pipe_open_pipe(struct dcerpc_connection *c, const char *identifier)
377 char *canon, *full_path;
379 canon = talloc_strdup(NULL, identifier);
381 string_replace(canon, '/', '\\');
382 full_path = talloc_asprintf(canon, "%s/%s", lp_ncalrpc_dir(), canon);
384 status = dcerpc_pipe_open_socket(c, full_path, 0, "unix", NCALRPC);