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/filesys.h"
28 #include "lib/events/events.h"
29 #include "rpc_server/dcerpc_server.h"
30 #include "smbd/service_stream.h"
32 struct dcesrv_socket_context {
33 const struct dcesrv_endpoint *endpoint;
34 struct dcesrv_context *dcesrv_ctx;
38 write_fn callback for dcesrv_output()
40 static ssize_t dcerpc_write_fn(void *private, DATA_BLOB *out)
43 struct socket_context *sock = private;
46 status = socket_send(sock, out, &sendlen, 0);
47 if (NT_STATUS_IS_ERR(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 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 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 tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
91 if (tmp_blob.data == NULL) {
92 dcesrv_terminate_connection(dce_conn, "out of memory");
96 status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread, 0);
97 if (NT_STATUS_IS_ERR(status)) {
98 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
102 talloc_free(tmp_blob.data);
106 tmp_blob.length = nread;
108 status = dcesrv_input(dce_conn, &tmp_blob);
109 talloc_free(tmp_blob.data);
111 if (!NT_STATUS_IS_OK(status)) {
112 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
116 if (dce_conn->call_list && dce_conn->call_list->replies) {
117 EVENT_FD_WRITEABLE(conn->event.fde);
121 void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
123 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
126 status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
127 if (!NT_STATUS_IS_OK(status)) {
128 dcesrv_terminate_connection(dce_conn, "eof on socket");
132 if (!dce_conn->call_list || !dce_conn->call_list->replies) {
133 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
138 static const struct stream_server_ops dcesrv_stream_ops = {
140 .accept_connection = dcesrv_sock_accept,
141 .recv_handler = dcesrv_sock_recv,
142 .send_handler = dcesrv_sock_send,
147 static NTSTATUS add_socket_rpc_unix(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
148 struct event_context *event_ctx, const struct model_ops *model_ops)
150 struct dcesrv_socket_context *dcesrv_sock;
154 dcesrv_sock = talloc(dce_ctx, struct dcesrv_socket_context);
155 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
157 /* remember the endpoint of this socket */
158 dcesrv_sock->endpoint = e;
159 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
161 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
162 "unix", e->ep_description->endpoint, &port,
164 if (!NT_STATUS_IS_OK(status)) {
165 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
166 e->ep_description->endpoint, nt_errstr(status)));
172 static NTSTATUS add_socket_rpc_ncalrpc(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
173 struct event_context *event_ctx, const struct model_ops *model_ops)
175 struct dcesrv_socket_context *dcesrv_sock;
180 if (!e->ep_description->endpoint) {
181 /* No identifier specified: use DEFAULT.
182 * DO NOT hardcode this value anywhere else. Rather, specify
183 * no endpoint and let the epmapper worry about it. */
184 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
187 full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(), e->ep_description->endpoint);
189 dcesrv_sock = talloc(dce_ctx, struct dcesrv_socket_context);
190 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
192 /* remember the endpoint of this socket */
193 dcesrv_sock->endpoint = e;
194 dcesrv_sock->dcesrv_ctx = dce_ctx;
196 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
197 "unix", full_path, &port, dcesrv_sock);
198 if (!NT_STATUS_IS_OK(status)) {
199 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
200 e->ep_description->endpoint, full_path, nt_errstr(status)));
206 add a socket address to the list of events, one event per dcerpc endpoint
208 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
209 struct event_context *event_ctx, const struct model_ops *model_ops,
212 struct dcesrv_socket_context *dcesrv_sock;
216 if (e->ep_description->endpoint) {
217 port = atoi(e->ep_description->endpoint);
220 dcesrv_sock = talloc(dce_ctx, struct dcesrv_socket_context);
221 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
223 /* remember the endpoint of this socket */
224 dcesrv_sock->endpoint = e;
225 dcesrv_sock->dcesrv_ctx = dce_ctx;
227 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
228 "ipv4", address, &port, dcesrv_sock);
229 if (!NT_STATUS_IS_OK(status)) {
230 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n",
231 address, port, nt_errstr(status)));
234 if (e->ep_description->endpoint == NULL) {
235 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
241 static NTSTATUS add_socket_rpc_tcp(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
242 struct event_context *event_ctx, const struct model_ops *model_ops)
246 /* Add TCP/IP sockets */
247 if (lp_interfaces() && lp_bind_interfaces_only()) {
248 int num_interfaces = iface_count();
250 for(i = 0; i < num_interfaces; i++) {
251 const char *address = iface_n_ip(i);
252 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
253 NT_STATUS_NOT_OK_RETURN(status);
256 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, lp_socket_address());
257 NT_STATUS_NOT_OK_RETURN(status);
263 /****************************************************************************
264 Open the listening sockets for RPC over NCACN_IP_TCP/NCALRPC/NCACN_UNIX_STREAM
265 ****************************************************************************/
266 NTSTATUS dcesrv_sock_init(struct dcesrv_context *dce_ctx,
267 struct event_context *event_ctx, const struct model_ops *model_ops)
269 struct dcesrv_endpoint *e;
272 /* Make sure the directory for NCALRPC exists */
273 if (!directory_exist(lp_ncalrpc_dir())) {
274 mkdir(lp_ncalrpc_dir(), 0755);
277 for (e=dce_ctx->endpoint_list;e;e=e->next) {
278 switch (e->ep_description->transport) {
279 case NCACN_UNIX_STREAM:
280 status = add_socket_rpc_unix(dce_ctx, e, event_ctx, model_ops);
281 NT_STATUS_NOT_OK_RETURN(status);
285 status = add_socket_rpc_ncalrpc(dce_ctx, e, event_ctx, model_ops);
286 NT_STATUS_NOT_OK_RETURN(status);
290 status = add_socket_rpc_tcp(dce_ctx, e, event_ctx, model_ops);
291 NT_STATUS_NOT_OK_RETURN(status);