2 Unix SMB/CIFS implementation.
4 server side dcerpc using various kinds of sockets (tcp, unix domain)
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Stefan (metze) Metzmacher 2004-2005
8 Copyright (C) Jelmer Vernooij 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "lib/socket/socket.h"
27 #include "system/dir.h"
28 #include "system/filesys.h"
29 #include "lib/events/events.h"
30 #include "rpc_server/dcerpc_server.h"
31 #include "smbd/service_stream.h"
33 struct dcesrv_socket_context {
34 const struct dcesrv_endpoint *endpoint;
35 struct dcesrv_context *dcesrv_ctx;
39 write_fn callback for dcesrv_output()
41 static NTSTATUS dcerpc_write_fn(void *private_data, DATA_BLOB *out, size_t *nwritten)
44 struct socket_context *sock = talloc_get_type(private_data, struct socket_context);
47 status = socket_send(sock, out, &sendlen, 0);
48 NT_STATUS_IS_ERR_RETURN(status);
54 static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
56 stream_terminate_connection(dce_conn->srv_conn, reason);
60 static void dcesrv_sock_accept(struct stream_connection *srv_conn)
63 struct dcesrv_socket_context *dcesrv_sock =
64 talloc_get_type(srv_conn->private, struct dcesrv_socket_context);
65 struct dcesrv_connection *dcesrv_conn = NULL;
67 status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
69 dcesrv_sock->endpoint,
72 if (!NT_STATUS_IS_OK(status)) {
73 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n",
78 srv_conn->private = dcesrv_conn;
83 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
86 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
90 if (dce_conn->processing) {
91 EVENT_FD_NOT_READABLE(conn->event.fde);
95 tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
96 if (tmp_blob.data == NULL) {
97 dcesrv_terminate_connection(dce_conn, "out of memory");
101 status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread, 0);
102 if (NT_STATUS_IS_ERR(status)) {
103 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
107 talloc_free(tmp_blob.data);
111 tmp_blob.length = nread;
113 dce_conn->processing = True;
114 status = dcesrv_input(dce_conn, &tmp_blob);
115 dce_conn->processing = False;
116 talloc_free(tmp_blob.data);
118 EVENT_FD_READABLE(conn->event.fde);
120 if (!NT_STATUS_IS_OK(status)) {
121 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
125 if (dce_conn->call_list && dce_conn->call_list->replies) {
126 EVENT_FD_WRITEABLE(conn->event.fde);
130 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
132 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
135 status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
136 if (NT_STATUS_IS_ERR(status)) {
137 dcesrv_terminate_connection(dce_conn, "eof on socket");
141 if (!dce_conn->call_list || !dce_conn->call_list->replies) {
142 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
147 static const struct stream_server_ops dcesrv_stream_ops = {
149 .accept_connection = dcesrv_sock_accept,
150 .recv_handler = dcesrv_sock_recv,
151 .send_handler = dcesrv_sock_send,
156 static NTSTATUS add_socket_rpc_unix(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
157 struct event_context *event_ctx, const struct model_ops *model_ops)
159 struct dcesrv_socket_context *dcesrv_sock;
163 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
164 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
166 /* remember the endpoint of this socket */
167 dcesrv_sock->endpoint = e;
168 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
170 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
171 "unix", e->ep_description->endpoint, &port,
173 if (!NT_STATUS_IS_OK(status)) {
174 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
175 e->ep_description->endpoint, nt_errstr(status)));
181 static NTSTATUS add_socket_rpc_ncalrpc(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
182 struct event_context *event_ctx, const struct model_ops *model_ops)
184 struct dcesrv_socket_context *dcesrv_sock;
189 if (!e->ep_description->endpoint) {
190 /* No identifier specified: use DEFAULT.
191 * DO NOT hardcode this value anywhere else. Rather, specify
192 * no endpoint and let the epmapper worry about it. */
193 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
196 full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(), e->ep_description->endpoint);
198 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
199 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
201 /* remember the endpoint of this socket */
202 dcesrv_sock->endpoint = e;
203 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
205 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
206 "unix", full_path, &port, dcesrv_sock);
207 if (!NT_STATUS_IS_OK(status)) {
208 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
209 e->ep_description->endpoint, full_path, nt_errstr(status)));
215 add a socket address to the list of events, one event per dcerpc endpoint
217 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
218 struct event_context *event_ctx, const struct model_ops *model_ops,
221 struct dcesrv_socket_context *dcesrv_sock;
225 if (e->ep_description->endpoint) {
226 port = atoi(e->ep_description->endpoint);
229 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
230 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
232 /* remember the endpoint of this socket */
233 dcesrv_sock->endpoint = e;
234 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
236 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
237 "ipv4", address, &port, dcesrv_sock);
238 if (!NT_STATUS_IS_OK(status)) {
239 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n",
240 address, port, nt_errstr(status)));
243 if (e->ep_description->endpoint == NULL) {
244 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
250 static NTSTATUS add_socket_rpc_tcp(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
251 struct event_context *event_ctx, const struct model_ops *model_ops)
255 /* Add TCP/IP sockets */
256 if (lp_interfaces() && lp_bind_interfaces_only()) {
257 int num_interfaces = iface_count();
259 for(i = 0; i < num_interfaces; i++) {
260 const char *address = iface_n_ip(i);
261 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
262 NT_STATUS_NOT_OK_RETURN(status);
265 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, lp_socket_address());
266 NT_STATUS_NOT_OK_RETURN(status);
272 /****************************************************************************
273 Open the listening sockets for RPC over NCACN_IP_TCP/NCALRPC/NCACN_UNIX_STREAM
274 ****************************************************************************/
275 NTSTATUS dcesrv_sock_init(struct dcesrv_context *dce_ctx,
276 struct event_context *event_ctx, const struct model_ops *model_ops)
278 struct dcesrv_endpoint *e;
281 /* Make sure the directory for NCALRPC exists */
282 if (!directory_exist(lp_ncalrpc_dir())) {
283 mkdir(lp_ncalrpc_dir(), 0755);
286 for (e=dce_ctx->endpoint_list;e;e=e->next) {
287 switch (e->ep_description->transport) {
288 case NCACN_UNIX_STREAM:
289 status = add_socket_rpc_unix(dce_ctx, e, event_ctx, model_ops);
290 NT_STATUS_NOT_OK_RETURN(status);
294 status = add_socket_rpc_ncalrpc(dce_ctx, e, event_ctx, model_ops);
295 NT_STATUS_NOT_OK_RETURN(status);
299 status = add_socket_rpc_tcp(dce_ctx, e, event_ctx, model_ops);
300 NT_STATUS_NOT_OK_RETURN(status);