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"
29 #include "lib/stream/packet.h"
31 /* transport private information used by general socket pipe transports */
34 struct socket_context *sock;
37 struct packet_context *packet;
38 uint32_t pending_reads;
45 static void sock_dead(struct dcerpc_connection *p, NTSTATUS status)
47 struct sock_private *sock = p->transport.private;
49 if (sock && sock->sock != NULL) {
50 talloc_free(sock->fde);
51 talloc_free(sock->sock);
55 if (!NT_STATUS_IS_OK(status)) {
56 p->transport.recv_data(p, NULL, status);
62 handle socket recv errors
64 static void sock_error_handler(void *private, NTSTATUS status)
66 struct dcerpc_connection *p = talloc_get_type(private,
67 struct dcerpc_connection);
72 check if a blob is a complete packet
74 static NTSTATUS sock_complete_packet(void *private, DATA_BLOB blob, size_t *size)
76 if (blob.length < DCERPC_FRAG_LEN_OFFSET+2) {
77 return STATUS_MORE_ENTRIES;
79 *size = dcerpc_get_frag_length(&blob);
80 if (*size > blob.length) {
81 return STATUS_MORE_ENTRIES;
89 static NTSTATUS sock_process_recv(void *private, DATA_BLOB blob)
91 struct dcerpc_connection *p = talloc_get_type(private,
92 struct dcerpc_connection);
93 struct sock_private *sock = p->transport.private;
94 sock->pending_reads--;
95 if (sock->pending_reads == 0) {
96 packet_recv_disable(sock->packet);
98 p->transport.recv_data(p, &blob, NT_STATUS_OK);
103 called when a IO is triggered by the events system
105 static void sock_io_handler(struct event_context *ev, struct fd_event *fde,
106 uint16_t flags, void *private)
108 struct dcerpc_connection *p = talloc_get_type(private,
109 struct dcerpc_connection);
110 struct sock_private *sock = p->transport.private;
112 if (flags & EVENT_FD_WRITE) {
113 packet_queue_run(sock->packet);
117 if (sock->sock == NULL) {
121 if (flags & EVENT_FD_READ) {
122 packet_recv(sock->packet);
127 initiate a read request - not needed for dcerpc sockets
129 static NTSTATUS sock_send_read(struct dcerpc_connection *p)
131 struct sock_private *sock = p->transport.private;
132 sock->pending_reads++;
133 if (sock->pending_reads == 1) {
134 packet_recv_enable(sock->packet);
140 send an initial pdu in a multi-pdu sequence
142 static NTSTATUS sock_send_request(struct dcerpc_connection *p, DATA_BLOB *data,
145 struct sock_private *sock = p->transport.private;
149 if (sock->sock == NULL) {
150 return NT_STATUS_CONNECTION_DISCONNECTED;
153 blob = data_blob_talloc(sock->packet, data->data, data->length);
154 if (blob.data == NULL) {
155 return NT_STATUS_NO_MEMORY;
158 status = packet_send(sock->packet, blob);
159 if (!NT_STATUS_IS_OK(status)) {
171 shutdown sock pipe connection
173 static NTSTATUS sock_shutdown_pipe(struct dcerpc_connection *p)
175 struct sock_private *sock = p->transport.private;
177 if (sock && sock->sock) {
178 sock_dead(p, NT_STATUS_OK);
185 return sock server name
187 static const char *sock_peer_name(struct dcerpc_connection *p)
189 struct sock_private *sock = p->transport.private;
190 return sock->server_name;
194 open a rpc connection using the generic socket library
196 static NTSTATUS dcerpc_pipe_open_socket(struct dcerpc_connection *c,
200 enum dcerpc_transport_t transport)
202 struct sock_private *sock;
203 struct socket_context *socket_ctx;
206 sock = talloc(c, struct sock_private);
208 return NT_STATUS_NO_MEMORY;
211 status = socket_create(type, SOCKET_TYPE_STREAM, &socket_ctx, 0);
212 if (!NT_STATUS_IS_OK(status)) {
216 talloc_steal(sock, socket_ctx);
218 status = socket_connect_ev(socket_ctx, NULL, 0, server, port, 0, c->event_ctx);
219 if (!NT_STATUS_IS_OK(status)) {
225 fill in the transport methods
227 c->transport.transport = transport;
228 c->transport.private = NULL;
230 c->transport.send_request = sock_send_request;
231 c->transport.send_read = sock_send_read;
232 c->transport.recv_data = NULL;
234 c->transport.shutdown_pipe = sock_shutdown_pipe;
235 c->transport.peer_name = sock_peer_name;
237 sock->sock = socket_ctx;
238 sock->pending_reads = 0;
239 sock->server_name = strupper_talloc(sock, server);
241 sock->fde = event_add_fd(c->event_ctx, sock->sock, socket_get_fd(sock->sock),
242 0, sock_io_handler, c);
244 c->transport.private = sock;
246 sock->packet = packet_init(sock);
247 if (sock->packet == NULL) {
249 return NT_STATUS_NO_MEMORY;
251 packet_set_private(sock->packet, c);
252 packet_set_socket(sock->packet, sock->sock);
253 packet_set_callback(sock->packet, sock_process_recv);
254 packet_set_full_request(sock->packet, sock_complete_packet);
255 packet_set_error_handler(sock->packet, sock_error_handler);
256 packet_set_event_context(sock->packet, c->event_ctx);
257 packet_set_fde(sock->packet, sock->fde);
258 packet_set_serialise(sock->packet);
259 packet_recv_disable(sock->packet);
260 packet_set_initial_read(sock->packet, 16);
262 /* ensure we don't get SIGPIPE */
263 BlockSignals(True,SIGPIPE);
269 open a rpc connection using tcp
271 NTSTATUS dcerpc_pipe_open_tcp(struct dcerpc_connection *c, const char *server, uint32_t port)
276 status = dcerpc_pipe_open_socket(c, server, port, "ipv6", NCACN_IP_TCP);
277 if (NT_STATUS_IS_OK(status)) {
281 return dcerpc_pipe_open_socket(c, server, port, "ipv4", NCACN_IP_TCP);
285 open a rpc connection to a unix socket
287 NTSTATUS dcerpc_pipe_open_unix_stream(struct dcerpc_connection *c, const char *path)
289 return dcerpc_pipe_open_socket(c, path, 0, "unix", NCACN_UNIX_STREAM);
293 open a rpc connection to a named pipe
295 NTSTATUS dcerpc_pipe_open_pipe(struct dcerpc_connection *c, const char *identifier)
298 char *canon, *full_path;
300 canon = talloc_strdup(NULL, identifier);
302 string_replace(canon, '/', '\\');
303 full_path = talloc_asprintf(canon, "%s/%s", lp_ncalrpc_dir(), canon);
305 status = dcerpc_pipe_open_socket(c, full_path, 0, "unix", NCALRPC);