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"
32 #include "lib/messaging/irpc.h"
34 struct dcesrv_socket_context {
35 const struct dcesrv_endpoint *endpoint;
36 struct dcesrv_context *dcesrv_ctx;
40 write_fn callback for dcesrv_output()
42 static NTSTATUS dcerpc_write_fn(void *private_data, DATA_BLOB *out, size_t *nwritten)
45 struct socket_context *sock = talloc_get_type(private_data, struct socket_context);
48 status = socket_send(sock, out, &sendlen, 0);
49 NT_STATUS_IS_ERR_RETURN(status);
55 static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
57 stream_terminate_connection(dce_conn->srv_conn, reason);
61 static void dcesrv_sock_accept(struct stream_connection *srv_conn)
64 struct dcesrv_socket_context *dcesrv_sock =
65 talloc_get_type(srv_conn->private, struct dcesrv_socket_context);
66 struct dcesrv_connection *dcesrv_conn = NULL;
68 status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
70 dcesrv_sock->endpoint,
73 if (!NT_STATUS_IS_OK(status)) {
74 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n",
76 stream_terminate_connection(srv_conn, nt_errstr(status));
80 srv_conn->private = dcesrv_conn;
82 irpc_add_name(srv_conn->msg_ctx, "rpc_server");
87 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
90 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
94 if (dce_conn->processing) {
95 EVENT_FD_NOT_READABLE(conn->event.fde);
99 tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
100 if (tmp_blob.data == NULL) {
101 dcesrv_terminate_connection(dce_conn, "out of memory");
105 status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread, 0);
106 if (NT_STATUS_IS_ERR(status)) {
107 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
111 talloc_free(tmp_blob.data);
115 tmp_blob.length = nread;
117 dce_conn->processing = True;
118 status = dcesrv_input(dce_conn, &tmp_blob);
119 dce_conn->processing = False;
120 talloc_free(tmp_blob.data);
122 EVENT_FD_READABLE(conn->event.fde);
124 if (!NT_STATUS_IS_OK(status)) {
125 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
129 if (dce_conn->call_list && dce_conn->call_list->replies) {
130 EVENT_FD_WRITEABLE(conn->event.fde);
134 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
136 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
139 status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
140 if (NT_STATUS_IS_ERR(status)) {
141 dcesrv_terminate_connection(dce_conn, "eof on socket");
145 if (!dce_conn->call_list || !dce_conn->call_list->replies) {
146 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
151 static const struct stream_server_ops dcesrv_stream_ops = {
153 .accept_connection = dcesrv_sock_accept,
154 .recv_handler = dcesrv_sock_recv,
155 .send_handler = dcesrv_sock_send,
160 NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
161 struct event_context *event_ctx, const struct model_ops *model_ops)
163 struct dcesrv_socket_context *dcesrv_sock;
167 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
168 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
170 /* remember the endpoint of this socket */
171 dcesrv_sock->endpoint = e;
172 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
174 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
175 "unix", e->ep_description->endpoint, &port,
177 if (!NT_STATUS_IS_OK(status)) {
178 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
179 e->ep_description->endpoint, nt_errstr(status)));
185 NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
186 struct event_context *event_ctx, const struct model_ops *model_ops)
188 struct dcesrv_socket_context *dcesrv_sock;
193 if (!e->ep_description->endpoint) {
194 /* No identifier specified: use DEFAULT.
195 * DO NOT hardcode this value anywhere else. Rather, specify
196 * no endpoint and let the epmapper worry about it. */
197 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
200 full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(), e->ep_description->endpoint);
202 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
203 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
205 /* remember the endpoint of this socket */
206 dcesrv_sock->endpoint = e;
207 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
209 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
210 "unix", full_path, &port, dcesrv_sock);
211 if (!NT_STATUS_IS_OK(status)) {
212 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
213 e->ep_description->endpoint, full_path, nt_errstr(status)));
219 add a socket address to the list of events, one event per dcerpc endpoint
221 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
222 struct event_context *event_ctx, const struct model_ops *model_ops,
225 struct dcesrv_socket_context *dcesrv_sock;
229 if (e->ep_description->endpoint) {
230 port = atoi(e->ep_description->endpoint);
233 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
234 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
236 /* remember the endpoint of this socket */
237 dcesrv_sock->endpoint = e;
238 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
240 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
241 "ipv4", address, &port, dcesrv_sock);
242 if (!NT_STATUS_IS_OK(status)) {
243 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n",
244 address, port, nt_errstr(status)));
247 if (e->ep_description->endpoint == NULL) {
248 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
254 NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
255 struct event_context *event_ctx, const struct model_ops *model_ops)
259 /* Add TCP/IP sockets */
260 if (lp_interfaces() && lp_bind_interfaces_only()) {
261 int num_interfaces = iface_count();
263 for(i = 0; i < num_interfaces; i++) {
264 const char *address = iface_n_ip(i);
265 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
266 NT_STATUS_NOT_OK_RETURN(status);
269 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, lp_socket_address());
270 NT_STATUS_NOT_OK_RETURN(status);