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
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.
27 struct dcesrv_socket_context {
28 const struct dcesrv_endpoint *endpoint;
29 struct dcesrv_context *dcesrv_ctx;
33 write_fn callback for dcesrv_output()
35 static ssize_t dcerpc_write_fn(void *private, DATA_BLOB *out)
38 struct socket_context *sock = private;
41 status = socket_send(sock, out, &sendlen, 0);
42 if (NT_STATUS_IS_ERR(status)) {
49 void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
51 server_terminate_connection(dce_conn->srv_conn, reason);
54 static void add_socket_rpc_unix(struct server_service *service,
55 const struct model_ops *model_ops,
56 struct dcesrv_context *dce_ctx,
57 struct dcesrv_endpoint *e)
59 struct server_socket *sock;
60 struct dcesrv_socket_context *dcesrv_sock;
63 sock = service_setup_socket(service,model_ops, "unix", e->ep_description.endpoint, &port);
65 DEBUG(0,("service_setup_socket(path=%s) failed\n",e->ep_description.endpoint));
69 dcesrv_sock = talloc_p(sock, struct dcesrv_socket_context);
71 DEBUG(0,("talloc_p(sock->mem_ctx, struct dcesrv_socket_context) failed\n"));
75 /* remember the endpoint of this socket */
76 dcesrv_sock->endpoint = e;
77 dcesrv_sock->dcesrv_ctx = dce_ctx;
79 sock->private_data = dcesrv_sock;
82 static void add_socket_rpc_ncalrpc(struct server_service *service,
83 const struct model_ops *model_ops,
84 struct dcesrv_context *dce_ctx,
85 struct dcesrv_endpoint *e)
87 struct server_socket *sock;
88 struct dcesrv_socket_context *dcesrv_sock;
92 if (!e->ep_description.endpoint) {
93 /* No identifier specified: use DEFAULT.
94 * DO NOT hardcode this value anywhere else. Rather, specify
95 * no endpoint and let the epmapper worry about it. */
96 e->ep_description.endpoint = talloc_strdup(dce_ctx, "DEFAULT");
99 full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(), e->ep_description.endpoint);
101 sock = service_setup_socket(service,model_ops, "unix", full_path, &port);
103 DEBUG(0,("service_setup_socket(identifier=%s,path=%s) failed\n",e->ep_description.endpoint, full_path));
107 dcesrv_sock = talloc_p(sock, struct dcesrv_socket_context);
109 DEBUG(0,("talloc_p(sock->mem_ctx, struct dcesrv_socket_context) failed\n"));
113 /* remember the endpoint of this socket */
114 dcesrv_sock->endpoint = e;
115 dcesrv_sock->dcesrv_ctx = dce_ctx;
117 sock->private_data = dcesrv_sock;
123 add a socket address to the list of events, one event per dcerpc endpoint
125 static void add_socket_rpc_tcp_iface(struct server_service *service,
126 const struct model_ops *model_ops,
127 struct dcesrv_context *dce_ctx,
128 struct dcesrv_endpoint *e,
129 struct ipv4_addr *ifip)
131 struct server_socket *sock;
132 struct dcesrv_socket_context *dcesrv_sock;
134 char *ip_str = talloc_strdup(service, sys_inet_ntoa(*ifip));
136 if (e->ep_description.endpoint)
137 port = atoi(e->ep_description.endpoint);
139 sock = service_setup_socket(service,model_ops, "ipv4", ip_str, &port);
141 DEBUG(0,("service_setup_socket(port=%u) failed\n",port));
145 if (e->ep_description.endpoint == NULL) {
146 e->ep_description.endpoint = talloc_asprintf(dce_ctx, "%d", port);
149 dcesrv_sock = talloc_p(sock, struct dcesrv_socket_context);
151 DEBUG(0,("talloc_p(sock->mem_ctx, struct dcesrv_socket_context) failed\n"));
155 /* remember the endpoint of this socket */
156 dcesrv_sock->endpoint = e;
157 dcesrv_sock->dcesrv_ctx = dce_ctx;
159 sock->private_data = dcesrv_sock;
166 static void add_socket_rpc_tcp(struct server_service *service,
167 const struct model_ops *model_ops,
168 struct dcesrv_context *dce_ctx,
169 struct dcesrv_endpoint *e)
171 /* Add TCP/IP sockets */
172 if (lp_interfaces() && lp_bind_interfaces_only()) {
173 int num_interfaces = iface_count();
175 for(i = 0; i < num_interfaces; i++) {
176 struct ipv4_addr *ifip = iface_n_ip(i);
180 add_socket_rpc_tcp_iface(service, model_ops, dce_ctx, e, ifip);
183 struct ipv4_addr ifip;
184 ifip = interpret_addr2(lp_socket_address());
185 add_socket_rpc_tcp_iface(service, model_ops, dce_ctx, e, &ifip);
191 /****************************************************************************
192 Open the listening sockets for RPC over NCACN_IP_TCP/NCALRPC/NCACN_UNIX_STREAM
193 ****************************************************************************/
194 void dcesrv_sock_init(struct server_service *service, const struct model_ops *model_ops, struct dcesrv_context *dce_ctx)
196 struct dcesrv_endpoint *e;
198 DEBUG(1,("dcesrv_sock_init\n"));
200 /* Make sure the directory for NCALRPC exists */
201 if (!directory_exist(lp_ncalrpc_dir(), NULL)) {
202 mkdir(lp_ncalrpc_dir(), S_IWUSR | S_IRUSR | S_IXUSR);
205 for (e=dce_ctx->endpoint_list;e;e=e->next) {
206 switch (e->ep_description.transport) {
207 case NCACN_UNIX_STREAM:
208 add_socket_rpc_unix(service, model_ops, dce_ctx, e);
212 add_socket_rpc_ncalrpc(service, model_ops, dce_ctx, e);
216 add_socket_rpc_tcp(service, model_ops, dce_ctx, e);
227 void dcesrv_sock_accept(struct server_connection *conn)
230 struct dcesrv_socket_context *dcesrv_sock = conn->server_socket->private_data;
231 struct dcesrv_connection *dcesrv_conn = NULL;
233 DEBUG(5,("dcesrv_sock_accept\n"));
235 status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx, dcesrv_sock->endpoint, &dcesrv_conn);
236 if (!NT_STATUS_IS_OK(status)) {
237 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n",
242 dcesrv_conn->srv_conn = conn;
244 conn->private_data = dcesrv_conn;
249 void dcesrv_sock_recv(struct server_connection *conn, time_t t, uint16_t flags)
252 struct dcesrv_connection *dce_conn = conn->private_data;
256 tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
257 if (tmp_blob.data == NULL) {
258 dcesrv_terminate_connection(dce_conn, "out of memory");
262 status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread, 0);
263 if (NT_STATUS_IS_ERR(status)) {
264 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
268 talloc_free(tmp_blob.data);
272 tmp_blob.length = nread;
274 status = dcesrv_input(dce_conn, &tmp_blob);
275 talloc_free(tmp_blob.data);
277 if (!NT_STATUS_IS_OK(status)) {
278 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
282 if (dce_conn->call_list && dce_conn->call_list->replies) {
283 conn->event.fde->flags |= EVENT_FD_WRITE;
289 void dcesrv_sock_send(struct server_connection *conn, time_t t, uint16_t flags)
291 struct dcesrv_connection *dce_conn = conn->private_data;
294 DEBUG(10,("dcesrv_sock_send\n"));
296 status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
297 if (!NT_STATUS_IS_OK(status)) {
298 dcesrv_terminate_connection(dce_conn, "eof on socket");
302 if (!dce_conn->call_list || !dce_conn->call_list->replies) {
303 conn->event.fde->flags &= ~EVENT_FD_WRITE;
309 void dcesrv_sock_idle(struct server_connection *conn, time_t t)
311 DEBUG(10,("dcesrv_sock_idle\n"));
312 conn->event.idle->next_event = t + 5;
317 void dcesrv_sock_close(struct server_connection *conn, const char *reason)
319 struct dcesrv_connection *dce_conn = conn->private_data;
321 DEBUG(5,("dcesrv_sock_close: %s\n",reason));
323 talloc_free(dce_conn);
328 void dcesrv_sock_exit(struct server_service *service, const char *reason)
330 DEBUG(1,("dcesrv_sock_exit: %s\n",reason));