2 Unix SMB/CIFS implementation.
4 smbd-specific dcerpc server code
6 Copyright (C) Andrew Tridgell 2003-2005
7 Copyright (C) Stefan (metze) Metzmacher 2004-2005
8 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2004,2007
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 3 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, see <http://www.gnu.org/licenses/>.
25 #include "librpc/gen_ndr/ndr_dcerpc.h"
26 #include "auth/auth.h"
27 #include "auth/gensec/gensec.h"
28 #include "lib/util/dlinklist.h"
29 #include "rpc_server/dcerpc_server.h"
30 #include "lib/events/events.h"
31 #include "smbd/service_task.h"
32 #include "smbd/service_stream.h"
33 #include "smbd/service.h"
34 #include "system/filesys.h"
35 #include "libcli/security/security.h"
36 #include "lib/socket/socket.h"
37 #include "lib/messaging/irpc.h"
38 #include "system/network.h"
39 #include "lib/socket/netif.h"
41 #include "param/param.h"
43 struct dcesrv_socket_context {
44 const struct dcesrv_endpoint *endpoint;
45 struct dcesrv_context *dcesrv_ctx;
49 write_fn callback for dcesrv_output()
51 static NTSTATUS dcerpc_write_fn(void *private_data, DATA_BLOB *out, size_t *nwritten)
54 struct socket_context *sock = talloc_get_type(private_data, struct socket_context);
57 status = socket_send(sock, out, &sendlen);
58 NT_STATUS_IS_ERR_RETURN(status);
64 static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
66 struct stream_connection *srv_conn;
67 srv_conn = talloc_get_type(dce_conn->transport.private_data,
68 struct stream_connection);
70 stream_terminate_connection(srv_conn, reason);
73 static void dcesrv_sock_report_output_data(struct dcesrv_connection *dcesrv_conn)
75 struct stream_connection *srv_conn;
76 srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
77 struct stream_connection);
79 if (srv_conn && srv_conn->event.fde) {
80 EVENT_FD_WRITEABLE(srv_conn->event.fde);
84 static struct socket_address *dcesrv_sock_get_my_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
86 struct stream_connection *srv_conn;
87 srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
88 struct stream_connection);
90 return socket_get_my_addr(srv_conn->socket, mem_ctx);
93 static struct socket_address *dcesrv_sock_get_peer_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
95 struct stream_connection *srv_conn;
96 srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
97 struct stream_connection);
99 return socket_get_peer_addr(srv_conn->socket, mem_ctx);
102 static void dcesrv_sock_accept(struct stream_connection *srv_conn)
105 struct dcesrv_socket_context *dcesrv_sock =
106 talloc_get_type(srv_conn->private, struct dcesrv_socket_context);
107 struct dcesrv_connection *dcesrv_conn = NULL;
108 struct auth_session_info *session_info = NULL;
110 status = auth_anonymous_session_info(srv_conn, &session_info);
111 if (!NT_STATUS_IS_OK(status)) {
112 DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n",
114 stream_terminate_connection(srv_conn, nt_errstr(status));
118 status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
120 dcesrv_sock->endpoint,
125 DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
127 if (!NT_STATUS_IS_OK(status)) {
128 DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n",
130 stream_terminate_connection(srv_conn, nt_errstr(status));
134 dcesrv_conn->transport.private_data = srv_conn;
135 dcesrv_conn->transport.report_output_data = dcesrv_sock_report_output_data;
136 dcesrv_conn->transport.get_my_addr = dcesrv_sock_get_my_addr;
137 dcesrv_conn->transport.get_peer_addr = dcesrv_sock_get_peer_addr;
139 srv_conn->private = dcesrv_conn;
141 irpc_add_name(srv_conn->msg_ctx, "rpc_server");
146 static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
149 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
153 if (dce_conn->processing) {
154 EVENT_FD_NOT_READABLE(conn->event.fde);
158 tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
159 if (tmp_blob.data == NULL) {
160 dcesrv_terminate_connection(dce_conn, "out of memory");
164 status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread);
165 if (NT_STATUS_IS_ERR(status)) {
166 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
170 talloc_free(tmp_blob.data);
174 tmp_blob.length = nread;
176 dce_conn->processing = true;
177 status = dcesrv_input(dce_conn, &tmp_blob);
178 dce_conn->processing = false;
179 talloc_free(tmp_blob.data);
181 EVENT_FD_READABLE(conn->event.fde);
183 if (!NT_STATUS_IS_OK(status)) {
184 dcesrv_terminate_connection(dce_conn, nt_errstr(status));
188 if (dce_conn->call_list && dce_conn->call_list->replies) {
189 EVENT_FD_WRITEABLE(conn->event.fde);
193 static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
195 struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
198 status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
199 if (NT_STATUS_IS_ERR(status)) {
200 dcesrv_terminate_connection(dce_conn, "eof on socket");
204 if (!dce_conn->call_list || !dce_conn->call_list->replies) {
205 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
210 static const struct stream_server_ops dcesrv_stream_ops = {
212 .accept_connection = dcesrv_sock_accept,
213 .recv_handler = dcesrv_sock_recv,
214 .send_handler = dcesrv_sock_send,
219 static NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
220 struct event_context *event_ctx, const struct model_ops *model_ops)
222 struct dcesrv_socket_context *dcesrv_sock;
226 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
227 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
229 /* remember the endpoint of this socket */
230 dcesrv_sock->endpoint = e;
231 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
233 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
234 "unix", e->ep_description->endpoint, &port,
236 if (!NT_STATUS_IS_OK(status)) {
237 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
238 e->ep_description->endpoint, nt_errstr(status)));
244 static NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
245 struct event_context *event_ctx, const struct model_ops *model_ops)
247 struct dcesrv_socket_context *dcesrv_sock;
252 if (!e->ep_description->endpoint) {
253 /* No identifier specified: use DEFAULT.
254 * DO NOT hardcode this value anywhere else. Rather, specify
255 * no endpoint and let the epmapper worry about it. */
256 e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
259 full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(global_loadparm),
260 e->ep_description->endpoint);
262 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
263 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
265 /* remember the endpoint of this socket */
266 dcesrv_sock->endpoint = e;
267 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
269 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
270 "unix", full_path, &port, dcesrv_sock);
271 if (!NT_STATUS_IS_OK(status)) {
272 DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
273 e->ep_description->endpoint, full_path, nt_errstr(status)));
280 add a socket address to the list of events, one event per dcerpc endpoint
282 static NTSTATUS add_socket_rpc_pipe_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
283 struct event_context *event_ctx, const struct model_ops *model_ops)
285 struct dcesrv_socket_context *dcesrv_sock;
288 if (e->ep_description->endpoint == NULL) {
289 DEBUG(0, ("Endpoint mandatory for named pipes\n"));
290 return NT_STATUS_INVALID_PARAMETER;
293 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
294 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
296 /* remember the endpoint of this socket */
297 dcesrv_sock->endpoint = e;
298 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
300 status = NT_STATUS_OK;
303 status = stream_setup_smb_pipe(event_ctx, model_ops, &dcesrv_stream_ops,
304 e->ep_description->endpoint, dcesrv_sock);
305 if (!NT_STATUS_IS_OK(status)) {
306 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
307 e->ep_description->endpoint, nt_errstr(status)));
313 static NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
314 struct event_context *event_ctx, const struct model_ops *model_ops)
318 status = add_socket_rpc_pipe_iface(dce_ctx, e, event_ctx, model_ops);
319 NT_STATUS_NOT_OK_RETURN(status);
325 add a socket address to the list of events, one event per dcerpc endpoint
327 static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
328 struct event_context *event_ctx, const struct model_ops *model_ops,
331 struct dcesrv_socket_context *dcesrv_sock;
335 if (e->ep_description->endpoint) {
336 port = atoi(e->ep_description->endpoint);
339 dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
340 NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
342 /* remember the endpoint of this socket */
343 dcesrv_sock->endpoint = e;
344 dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx);
346 status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops,
347 "ipv4", address, &port, dcesrv_sock);
348 if (!NT_STATUS_IS_OK(status)) {
349 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n",
350 address, port, nt_errstr(status)));
353 if (e->ep_description->endpoint == NULL) {
354 e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
360 static NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
361 struct event_context *event_ctx, const struct model_ops *model_ops)
365 /* Add TCP/IP sockets */
366 if (lp_interfaces(global_loadparm) && lp_bind_interfaces_only(global_loadparm)) {
367 int num_interfaces = iface_count();
369 for(i = 0; i < num_interfaces; i++) {
370 const char *address = iface_n_ip(i);
371 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
372 NT_STATUS_NOT_OK_RETURN(status);
375 status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops,
376 lp_socket_address(global_loadparm));
377 NT_STATUS_NOT_OK_RETURN(status);
384 static NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
385 struct event_context *event_ctx, const struct model_ops *model_ops)
387 switch (e->ep_description->transport) {
388 case NCACN_UNIX_STREAM:
389 return dcesrv_add_ep_unix(dce_ctx, e, event_ctx, model_ops);
392 return dcesrv_add_ep_ncalrpc(dce_ctx, e, event_ctx, model_ops);
395 return dcesrv_add_ep_tcp(dce_ctx, e, event_ctx, model_ops);
398 return dcesrv_add_ep_np(dce_ctx, e, event_ctx, model_ops);
401 return NT_STATUS_NOT_SUPPORTED;
406 open the dcerpc server sockets
408 static void dcesrv_task_init(struct task_server *task)
411 struct dcesrv_context *dce_ctx;
412 struct dcesrv_endpoint *e;
414 task_server_set_title(task, "task[dcesrv]");
416 status = dcesrv_init_context(task->event_ctx,
417 lp_dcerpc_endpoint_servers(global_loadparm),
419 if (!NT_STATUS_IS_OK(status)) goto failed;
421 /* Make sure the directory for NCALRPC exists */
422 if (!directory_exist(lp_ncalrpc_dir(global_loadparm))) {
423 mkdir(lp_ncalrpc_dir(global_loadparm), 0755);
426 for (e=dce_ctx->endpoint_list;e;e=e->next) {
427 status = dcesrv_add_ep(dce_ctx, e, task->event_ctx, task->model_ops);
428 if (!NT_STATUS_IS_OK(status)) goto failed;
433 task_server_terminate(task, "Failed to startup dcerpc server task");
437 called on startup of the smb server service It's job is to start
438 listening on all configured sockets
440 static NTSTATUS dcesrv_init(struct event_context *event_context,
441 struct loadparm_context *lp_ctx,
442 const struct model_ops *model_ops)
444 return task_server_startup(event_context, model_ops, dcesrv_task_init);
447 NTSTATUS server_service_rpc_init(void)
449 init_module_fn static_init[] = STATIC_dcerpc_server_MODULES;
450 init_module_fn *shared_init = load_samba_modules(NULL, global_loadparm, "dcerpc_server");
452 run_init_functions(static_init);
453 run_init_functions(shared_init);
455 talloc_free(shared_init);
457 return register_server_service("rpc", dcesrv_init);