2 * Unix SMB/CIFS implementation.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "source3/include/includes.h"
19 #include "lib/cmdline/cmdline.h"
20 #include "rpc_worker.h"
21 #include "rpc_config.h"
22 #include "librpc/rpc/dcesrv_core.h"
23 #include "librpc/rpc/dcerpc_util.h"
24 #include "source3/librpc/gen_ndr/ndr_rpc_host.h"
25 #include "lib/util/debug.h"
26 #include "lib/util/fault.h"
27 #include "rpc_server.h"
28 #include "rpc_pipes.h"
29 #include "source3/smbd/proto.h"
30 #include "source3/lib/smbd_shim.h"
31 #include "source3/lib/global_contexts.h"
32 #include "source3/lib/util_procid.h"
33 #include "lib/tsocket/tsocket.h"
34 #include "libcli/named_pipe_auth/npa_tstream.h"
35 #include "libcli/smb/smb_constants.h"
36 #include "lib/param/param.h"
37 #include "lib/util/idtree_random.h"
38 #include "lib/util/tevent_unix.h"
39 #include "lib/async_req/async_sock.h"
40 #include "lib/util/dlinklist.h"
41 #include "source3/include/auth.h"
42 #include "nsswitch/winbind_client.h"
43 #include "source3/include/messages.h"
44 #include "libcli/security/security_token.h"
47 * This is the generic code that becomes the
48 * template that all rpcd_* instances that
49 * serve DCERPC can use to provide services to samba-dcerpcd.
51 * The external entry point is:
52 * rpc_worker_main() which takes an argc/argv list
55 * get_interfaces() - List all interfaces that this server provides
56 * get_servers() - Provide the RPC server implementations
58 * Each rpcd_* service needs only to provide
59 * the implementations of get_interfaces() and get_servers()
60 * and call rpc_worker_main() from their main() function
61 * to provide services that can be connected to from samba-dcerpcd.
65 struct dcerpc_ncacn_conn *conns;
66 struct server_id rpc_host_pid;
67 struct messaging_context *msg_ctx;
68 struct dcesrv_context *dce_ctx;
70 struct dcesrv_context_callbacks cb;
72 struct rpc_worker_status status;
77 static void rpc_worker_print_interface(
78 FILE *f, const struct ndr_interface_table *t)
80 const struct ndr_interface_string_array *endpoints = t->endpoints;
82 struct ndr_syntax_id_buf id_buf;
86 ndr_syntax_id_buf_string(&t->syntax_id, &id_buf),
89 for (i=0; i<endpoints->count; i++) {
90 fprintf(f, " %s\n", endpoints->names[i]);
94 static NTSTATUS rpc_worker_report_status(struct rpc_worker *worker)
97 DATA_BLOB blob = { .data = buf, .length = sizeof(buf), };
98 enum ndr_err_code ndr_err;
101 if (DEBUGLEVEL >= 10) {
102 NDR_PRINT_DEBUG(rpc_worker_status, &worker->status);
105 ndr_err = ndr_push_struct_into_fixed_blob(
108 (ndr_push_flags_fn_t)ndr_push_rpc_worker_status);
109 SMB_ASSERT(NDR_ERR_CODE_IS_SUCCESS(ndr_err));
111 status = messaging_send(
113 worker->rpc_host_pid,
114 MSG_RPC_WORKER_STATUS,
119 static void rpc_worker_connection_terminated(
120 struct dcesrv_connection *conn, void *private_data)
122 struct rpc_worker *worker = talloc_get_type_abort(
123 private_data, struct rpc_worker);
124 struct dcerpc_ncacn_conn *ncacn_conn = talloc_get_type_abort(
125 conn->transport.private_data, struct dcerpc_ncacn_conn);
126 struct dcerpc_ncacn_conn *w = NULL;
130 SMB_ASSERT(worker->status.num_clients > 0);
132 for (w = worker->conns; w != NULL; w = w->next) {
133 if (w == ncacn_conn) {
140 DLIST_REMOVE(worker->conns, ncacn_conn);
142 worker->status.num_clients -= 1;
144 status = rpc_worker_report_status(worker);
145 if (!NT_STATUS_IS_OK(status)) {
146 DBG_DEBUG("rpc_worker_report_status returned %s\n",
151 static int dcesrv_connection_destructor(struct dcesrv_connection *conn)
153 struct dcerpc_ncacn_conn *ncacn_conn = talloc_get_type_abort(
154 conn->transport.private_data,
155 struct dcerpc_ncacn_conn);
157 if (ncacn_conn->termination_fn != NULL) {
158 ncacn_conn->termination_fn(conn, ncacn_conn->termination_data);
165 * A new client has been passed to us from samba-dcerpcd.
167 static void rpc_worker_new_client(
168 struct rpc_worker *worker,
169 struct rpc_host_client *client,
172 struct dcesrv_context *dce_ctx = worker->dce_ctx;
173 struct named_pipe_auth_req_info5 *info5 = client->npa_info5;
174 struct dcerpc_binding *b = NULL;
175 enum dcerpc_transport_t transport;
176 struct dcesrv_endpoint *ep = NULL;
177 struct dcerpc_ncacn_conn *ncacn_conn = NULL;
178 struct dcesrv_connection *dcesrv_conn = NULL;
179 DATA_BLOB buffer = { .data = NULL };
180 struct ncacn_packet *pkt = NULL;
184 DBG_DEBUG("Got new conn sock %d for binding %s\n",
188 status = dcerpc_parse_binding(client, client->binding, &b);
189 if (!NT_STATUS_IS_OK(status)) {
190 DBG_DEBUG("dcerpc_parse_binding(%s) failed: %s\n",
195 transport = dcerpc_binding_get_transport(b);
197 status = dcesrv_find_endpoint(dce_ctx, b, &ep);
199 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) &&
200 ((transport == NCACN_IP_TCP) || (transport == NCALRPC)) &&
201 (dcerpc_binding_get_string_option(b, "endpoint") != NULL)) {
203 * We have two kinds of servers: Those who explicitly
204 * bind to a port (e.g. 135 for epmapper) and those
205 * who just specify a transport. The client specified
206 * a port (or socket name), but we did not find this
207 * in the list of servers having specified a
208 * port. Retry just matching for the transport,
209 * catching the servers that did not explicitly
212 * This is not fully correct, what we should do is
213 * that once the port the server listens on has been
214 * finalized we should mark this in the server list,
215 * but for now it works. We don't have the same RPC
216 * interface listening twice on different ports.
218 struct dcerpc_binding *b_without_port = dcerpc_binding_dup(
220 if (b_without_port == NULL) {
221 status = NT_STATUS_NO_MEMORY;
225 status = dcerpc_binding_set_string_option(
226 b_without_port, "endpoint", NULL);
227 if (!NT_STATUS_IS_OK(status)) {
228 DBG_DEBUG("Could not delete endpoint: %s\n",
230 TALLOC_FREE(b_without_port);
234 status = dcesrv_find_endpoint(dce_ctx, b_without_port, &ep);
236 TALLOC_FREE(b_without_port);
239 if (!NT_STATUS_IS_OK(status)) {
240 DBG_DEBUG("Could not find endpoint for %s: %s\n",
246 ncacn_conn = talloc(dce_ctx, struct dcerpc_ncacn_conn);
247 if (ncacn_conn == NULL) {
248 DBG_DEBUG("talloc failed\n");
251 *ncacn_conn = (struct dcerpc_ncacn_conn) {
254 .termination_fn = rpc_worker_connection_terminated,
255 .termination_data = worker,
258 if (transport == NCALRPC) {
259 ret = tsocket_address_unix_from_path(
261 info5->remote_client_addr,
262 &ncacn_conn->remote_client_addr);
264 DBG_DEBUG("tsocket_address_unix_from_path"
266 info5->remote_client_addr,
271 ncacn_conn->remote_client_name = talloc_strdup(
272 ncacn_conn, info5->remote_client_name);
273 if (ncacn_conn->remote_client_name == NULL) {
274 DBG_DEBUG("talloc_strdup(%s) failed\n",
275 info5->remote_client_name);
279 ret = tsocket_address_unix_from_path(
281 info5->local_server_addr,
282 &ncacn_conn->local_server_addr);
284 DBG_DEBUG("tsocket_address_unix_from_path"
286 info5->local_server_addr,
291 ncacn_conn->local_server_name = talloc_strdup(
292 ncacn_conn, info5->local_server_name);
293 if (ncacn_conn->local_server_name == NULL) {
294 DBG_DEBUG("talloc_strdup(%s) failed\n",
295 info5->local_server_name);
299 ret = tsocket_address_inet_from_strings(
302 info5->remote_client_addr,
303 info5->remote_client_port,
304 &ncacn_conn->remote_client_addr);
306 DBG_DEBUG("tsocket_address_inet_from_strings"
307 "(%s, %"PRIu16") failed: %s\n",
308 info5->remote_client_addr,
309 info5->remote_client_port,
313 ncacn_conn->remote_client_name = talloc_strdup(
314 ncacn_conn, info5->remote_client_name);
315 if (ncacn_conn->remote_client_name == NULL) {
316 DBG_DEBUG("talloc_strdup(%s) failed\n",
317 info5->remote_client_name);
321 ret = tsocket_address_inet_from_strings(
324 info5->local_server_addr,
325 info5->local_server_port,
326 &ncacn_conn->local_server_addr);
328 DBG_DEBUG("tsocket_address_inet_from_strings"
329 "(%s, %"PRIu16") failed: %s\n",
330 info5->local_server_addr,
331 info5->local_server_port,
335 ncacn_conn->local_server_name = talloc_strdup(
336 ncacn_conn, info5->local_server_name);
337 if (ncacn_conn->local_server_name == NULL) {
338 DBG_DEBUG("talloc_strdup(%s) failed\n",
339 info5->local_server_name);
344 if (transport == NCACN_NP) {
345 ret = tstream_npa_existing_socket(
348 FILE_TYPE_MESSAGE_MODE_PIPE,
349 &ncacn_conn->tstream);
351 DBG_DEBUG("tstream_npa_existing_socket failed: %s\n",
357 * "transport" so far is implicitly assigned by the
358 * socket that the client connected to, passed in from
359 * samba-dcerpcd via the binding. For NCACN_NP (root
360 * only by unix permissions) we got a
361 * named_pipe_auth_req_info5 where the transport can
364 transport = info5->transport;
366 ret = tstream_bsd_existing_socket(
367 ncacn_conn, sock, &ncacn_conn->tstream);
369 DBG_DEBUG("tstream_bsd_existing_socket failed: %s\n",
376 ncacn_conn->session_info = talloc_move(
377 ncacn_conn, &info5->session_info->session_info);
379 if (security_token_is_system(
380 ncacn_conn->session_info->security_token) &&
381 (transport != NCALRPC)) {
382 DBG_DEBUG("System token only allowed on NCALRPC\n");
386 ncacn_conn->p = talloc_zero(ncacn_conn, struct pipes_struct);
387 if (ncacn_conn->p == NULL) {
388 DBG_DEBUG("talloc failed\n");
391 ncacn_conn->p->msg_ctx = global_messaging_context();
392 ncacn_conn->p->transport = transport;
394 status = dcesrv_endpoint_connect(
398 ncacn_conn->session_info,
399 global_event_context(),
400 DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
402 if (!NT_STATUS_IS_OK(status)) {
403 DBG_DEBUG("Failed to connect to endpoint: %s\n",
408 talloc_set_destructor(dcesrv_conn, dcesrv_connection_destructor);
410 dcesrv_conn->transport.private_data = ncacn_conn;
411 dcesrv_conn->transport.report_output_data =
412 dcesrv_sock_report_output_data;
413 dcesrv_conn->transport.terminate_connection =
414 dcesrv_transport_terminate_connection;
416 dcesrv_conn->send_queue = tevent_queue_create(
417 dcesrv_conn, "dcesrv send queue");
418 if (dcesrv_conn->send_queue == NULL) {
419 DBG_DEBUG("tevent_queue_create failed\n");
423 dcesrv_conn->stream = talloc_move(dcesrv_conn, &ncacn_conn->tstream);
424 dcesrv_conn->local_address = ncacn_conn->local_server_addr;
425 dcesrv_conn->remote_address = ncacn_conn->remote_client_addr;
427 if (client->bind_packet.length == 0) {
428 DBG_DEBUG("Expected bind packet\n");
432 buffer = (DATA_BLOB) {
433 .data = talloc_move(dcesrv_conn, &client->bind_packet.data),
434 .length = client->bind_packet.length,
437 pkt = talloc(dcesrv_conn, struct ncacn_packet);
439 DBG_DEBUG("talloc failed\n");
443 status = dcerpc_pull_ncacn_packet(pkt, &buffer, pkt);
444 if (!NT_STATUS_IS_OK(status)) {
445 DBG_DEBUG("dcerpc_pull_ncacn_packet failed: %s\n",
452 DLIST_ADD(worker->conns, ncacn_conn);
453 worker->status.num_clients += 1;
455 dcesrv_loop_next_packet(dcesrv_conn, pkt, buffer);
459 TALLOC_FREE(ncacn_conn);
460 TALLOC_FREE(dcesrv_conn);
467 * Parent thinks it successfully sent us a client. Tell it
470 status = rpc_worker_report_status(worker);
471 if (!NT_STATUS_IS_OK(status)) {
472 DBG_DEBUG("rpc_worker_report_status returned %s\n",
478 * New client message processing.
480 static bool rpc_worker_new_client_filter(
481 struct messaging_rec *rec, void *private_data)
483 struct rpc_worker *worker = talloc_get_type_abort(
484 private_data, struct rpc_worker);
485 struct dcesrv_context *dce_ctx = worker->dce_ctx;
486 struct rpc_host_client *client = NULL;
487 enum ndr_err_code ndr_err;
490 if (rec->msg_type != MSG_RPC_HOST_NEW_CLIENT) {
494 if (rec->num_fds != 1) {
495 DBG_DEBUG("Got %"PRIu8" fds\n", rec->num_fds);
499 client = talloc(dce_ctx, struct rpc_host_client);
500 if (client == NULL) {
501 DBG_DEBUG("talloc failed\n");
505 ndr_err = ndr_pull_struct_blob_all(
509 (ndr_pull_flags_fn_t)ndr_pull_rpc_host_client);
510 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
511 DBG_DEBUG("ndr_pull_rpc_host_client failed: %s\n",
512 ndr_errstr(ndr_err));
517 if (DEBUGLEVEL >= 10) {
518 NDR_PRINT_DEBUG(rpc_host_client, client);
524 rpc_worker_new_client(worker, client, sock);
530 * Return your status message processing.
532 static bool rpc_worker_status_filter(
533 struct messaging_rec *rec, void *private_data)
535 struct rpc_worker *worker = talloc_get_type_abort(
536 private_data, struct rpc_worker);
537 struct dcerpc_ncacn_conn *conn = NULL;
541 if (rec->msg_type != MSG_RPC_DUMP_STATUS) {
545 if (rec->num_fds != 1) {
546 DBG_DEBUG("Got %"PRIu8" fds\n", rec->num_fds);
550 fd = dup(rec->fds[0]);
552 DBG_DEBUG("dup(%"PRIi64") failed: %s\n",
560 DBG_DEBUG("fdopen failed: %s\n", strerror(errno));
565 for (conn = worker->conns; conn != NULL; conn = conn->next) {
566 char *endpoint = NULL;
568 endpoint = dcerpc_binding_string(
569 conn, conn->endpoint->ep_description);
572 "endpoint=%s client=%s server=%s\n",
573 endpoint ? endpoint : "UNKNOWN",
574 conn->remote_client_name,
575 conn->local_server_name);
576 TALLOC_FREE(endpoint);
585 take a reference to an existing association group
587 static struct dcesrv_assoc_group *rpc_worker_assoc_group_reference(
588 struct dcesrv_connection *conn,
591 const struct dcesrv_endpoint *endpoint = conn->endpoint;
592 enum dcerpc_transport_t transport = dcerpc_binding_get_transport(
593 endpoint->ep_description);
594 struct dcesrv_assoc_group *assoc_group = NULL;
597 /* find an association group given a assoc_group_id */
598 id_ptr = idr_find(conn->dce_ctx->assoc_groups_idr, id & 0xffffff);
599 if (id_ptr == NULL) {
600 DBG_NOTICE("Failed to find assoc_group 0x%08x\n", id);
603 assoc_group = talloc_get_type_abort(id_ptr, struct dcesrv_assoc_group);
605 if (assoc_group->transport != transport) {
606 const char *at = derpc_transport_string_by_transport(
607 assoc_group->transport);
608 const char *ct = derpc_transport_string_by_transport(
611 DBG_NOTICE("assoc_group 0x%08x (transport %s) "
612 "is not available on transport %s",
618 * Yes, this is a talloc_reference: The assoc group must be
619 * removed when all connections go. This should be replaced by
620 * adding a linked list of dcesrv_connection structs to the
623 return talloc_reference(conn, assoc_group);
626 static int rpc_worker_assoc_group_destructor(
627 struct dcesrv_assoc_group *assoc_group)
632 assoc_group->dce_ctx->assoc_groups_idr,
633 assoc_group->id & 0xffffff);
635 DBG_WARNING("Failed to remove assoc_group 0x%08x\n",
642 allocate a new association group
644 static struct dcesrv_assoc_group *rpc_worker_assoc_group_new(
645 struct dcesrv_connection *conn, uint8_t worker_index)
647 struct dcesrv_context *dce_ctx = conn->dce_ctx;
648 const struct dcesrv_endpoint *endpoint = conn->endpoint;
649 enum dcerpc_transport_t transport = dcerpc_binding_get_transport(
650 endpoint->ep_description);
651 struct dcesrv_assoc_group *assoc_group = NULL;
654 assoc_group = talloc_zero(conn, struct dcesrv_assoc_group);
655 if (assoc_group == NULL) {
659 id = idr_get_new_random(
660 dce_ctx->assoc_groups_idr, assoc_group, UINT16_MAX);
662 talloc_free(assoc_group);
663 DBG_WARNING("Out of association groups!\n");
666 assoc_group->id = (worker_index << 24) + id;
667 assoc_group->transport = transport;
668 assoc_group->dce_ctx = dce_ctx;
670 talloc_set_destructor(assoc_group, rpc_worker_assoc_group_destructor);
675 static NTSTATUS rpc_worker_assoc_group_find(
676 struct dcesrv_call_state *call,
679 struct rpc_worker *w = talloc_get_type_abort(
680 private_data, struct rpc_worker);
681 uint32_t assoc_group_id = call->pkt.u.bind.assoc_group_id;
683 if (assoc_group_id != 0) {
684 uint8_t worker_index = (assoc_group_id & 0xff000000) >> 24;
685 if (worker_index != w->status.worker_index) {
686 DBG_DEBUG("Wrong worker id %"PRIu8", "
687 "expected %"PRIu8"\n",
689 w->status.worker_index);
690 return NT_STATUS_NOT_FOUND;
692 call->conn->assoc_group = rpc_worker_assoc_group_reference(
693 call->conn, assoc_group_id);
695 call->conn->assoc_group = rpc_worker_assoc_group_new(
696 call->conn, w->status.worker_index);
699 if (call->conn->assoc_group == NULL) {
700 /* TODO Return correct status */
701 return NT_STATUS_UNSUCCESSFUL;
707 static struct rpc_worker *rpc_worker_new(
709 struct messaging_context *msg_ctx)
711 struct rpc_worker *worker = NULL;
713 worker = talloc_zero(mem_ctx, struct rpc_worker);
714 if (worker == NULL) {
718 worker->rpc_host_pid = (struct server_id) { .pid = 0 };
719 worker->msg_ctx = msg_ctx;
721 worker->cb = (struct dcesrv_context_callbacks) {
722 .log.successful_authz = dcesrv_log_successful_authz,
723 .auth.gensec_prepare = dcesrv_auth_gensec_prepare,
724 .assoc_group.find = rpc_worker_assoc_group_find,
725 .assoc_group.private_data = worker,
728 worker->dce_ctx = global_dcesrv_context();
729 if (worker->dce_ctx == NULL) {
732 dcesrv_context_set_callbacks(worker->dce_ctx, &worker->cb);
740 static struct dcesrv_context *rpc_worker_dce_ctx(struct rpc_worker *w)
745 struct rpc_worker_state {
746 struct tevent_context *ev;
747 struct rpc_worker *w;
748 struct tevent_req *new_client_req;
749 struct tevent_req *status_req;
750 struct tevent_req *finish_req;
753 static void rpc_worker_done(struct tevent_req *subreq);
754 static void rpc_worker_shutdown(
755 struct messaging_context *msg,
758 struct server_id server_id,
761 static struct tevent_req *rpc_worker_send(
763 struct tevent_context *ev,
764 struct rpc_worker *w,
769 struct tevent_req *req = NULL;
770 struct rpc_worker_state *state = NULL;
773 req = tevent_req_create(mem_ctx, &state, struct rpc_worker_state);
780 if ((server_index < 0) || ((unsigned)server_index > UINT32_MAX)) {
781 DBG_ERR("Invalid server index %d\n", server_index);
782 tevent_req_error(req, EINVAL);
783 return tevent_req_post(req, ev);
785 if ((worker_index < 0) || ((unsigned)worker_index > UINT32_MAX)) {
786 DBG_ERR("Invalid worker index %d\n", worker_index);
787 tevent_req_error(req, EINVAL);
788 return tevent_req_post(req, ev);
790 w->rpc_host_pid = pid_to_procid(rpc_host_pid);
792 w->status = (struct rpc_worker_status) {
793 .server_index = server_index,
794 .worker_index = worker_index,
797 /* Wait for new client messages. */
798 state->new_client_req = messaging_filtered_read_send(
800 messaging_tevent_context(w->msg_ctx),
802 rpc_worker_new_client_filter,
804 if (tevent_req_nomem(state->new_client_req, req)) {
805 return tevent_req_post(req, ev);
808 /* Wait for report your status messages. */
809 state->status_req = messaging_filtered_read_send(
811 messaging_tevent_context(w->msg_ctx),
813 rpc_worker_status_filter,
815 if (tevent_req_nomem(state->status_req, req)) {
816 return tevent_req_post(req, ev);
819 /* Wait for shutdown messages. */
820 status = messaging_register(
821 w->msg_ctx, req, MSG_SHUTDOWN, rpc_worker_shutdown);
822 if (!NT_STATUS_IS_OK(status)) {
823 DBG_DEBUG("messaging_register failed: %s\n",
825 tevent_req_error(req, map_errno_from_nt_status(status));
826 return tevent_req_post(req, ev);
829 state->finish_req = wait_for_read_send(state, ev, 0, false);
830 if (tevent_req_nomem(state->finish_req, req)) {
831 return tevent_req_post(req, ev);
833 tevent_req_set_callback(state->finish_req, rpc_worker_done, req);
835 rpc_worker_report_status(w);
840 static void rpc_worker_done(struct tevent_req *subreq)
842 struct tevent_req *req = tevent_req_callback_data(
843 subreq, struct tevent_req);
847 ok = wait_for_read_recv(subreq, &err);
850 tevent_req_error(req, err);
853 tevent_req_done(req);
856 static void rpc_worker_shutdown(
857 struct messaging_context *msg,
860 struct server_id server_id,
863 struct tevent_req *req = talloc_get_type_abort(
864 private_data, struct tevent_req);
865 tevent_req_done(req);
868 static int rpc_worker_recv(struct tevent_req *req)
870 return tevent_req_simple_recv_unix(req);
873 static void sig_term_handler(
874 struct tevent_context *ev,
875 struct tevent_signal *se,
884 static void sig_hup_handler(
885 struct tevent_context *ev,
886 struct tevent_signal *se,
892 change_to_root_user();
893 lp_load_with_shares(get_dyn_CONFIGFILE());
896 static NTSTATUS register_ep_server(
897 struct dcesrv_context *dce_ctx,
898 const struct dcesrv_endpoint_server *ep_server)
902 DBG_DEBUG("Registering server %s\n", ep_server->name);
904 status = dcerpc_register_ep_server(ep_server);
905 if (!NT_STATUS_IS_OK(status) &&
906 !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
907 DBG_ERR("Failed to register '%s' endpoint server: %s\n",
913 status = dcesrv_init_ep_server(dce_ctx, ep_server->name);
914 if (!NT_STATUS_IS_OK(status)) {
915 DBG_ERR("dcesrv_init_ep_server(%s) failed: %s\n",
925 * @brief Main function for RPC server implementations
927 * This function provides all that is necessary to run a RPC server
928 * inside the samba-dcerpcd framework. Just pass argv and argc on to
931 * The get_interfaces() callback provides the information that is
932 * passed to samba-dcerpcd via --list-interfaces, it should not do any
933 * real RPC server initialization work. Quickly after this function is
934 * called by rpc_worker_main, the process exits again. It should
935 * return the number of interfaces provided.
937 * get_servers() is called when the process is about to do the real
938 * work. So more heavy-weight initialization should happen here. It
939 * should return the number of server implementations provided.
941 * @param[in] argc argc from main()
942 * @param[in] argv argv from main()
943 * @param[in] get_interfaces List all interfaces that this server provides
944 * @param[in] get_servers Provide the RPC server implementations
945 * @param[in] private_data Passed to the callback functions
946 * @return 0 It should never return except on successful process exit
952 const char *daemon_config_name,
955 size_t (*get_interfaces)(
956 const struct ndr_interface_table ***ifaces,
958 size_t (*get_servers)(
959 struct dcesrv_context *dce_ctx,
960 const struct dcesrv_endpoint_server ***ep_servers,
964 const struct loadparm_substitution *lp_sub =
965 loadparm_s3_global_substitution();
966 const char *progname = getprogname();
967 TALLOC_CTX *frame = NULL;
968 struct tevent_context *ev_ctx = NULL;
969 struct tevent_req *req = NULL;
970 struct messaging_context *msg_ctx = NULL;
971 struct dcesrv_context *dce_ctx = NULL;
972 struct tevent_signal *se = NULL;
977 int worker_group = -1;
978 int worker_index = -1;
980 int list_interfaces = 0;
981 struct rpc_worker *worker = NULL;
982 const struct dcesrv_endpoint_server **ep_servers;
983 size_t i, num_servers;
986 struct poptOption long_options[] = {
989 .longName = "list-interfaces",
990 .argInfo = POPT_ARG_NONE,
991 .arg = &list_interfaces,
992 .descrip = "List the interfaces provided",
995 .longName = "worker-group",
996 .argInfo = POPT_ARG_INT,
997 .arg = &worker_group,
998 .descrip = "Group index in status message",
1001 .longName = "worker-index",
1002 .argInfo = POPT_ARG_INT,
1003 .arg = &worker_index,
1004 .descrip = "Worker index in status message",
1009 static const struct smbd_shim smbd_shim_fns = {
1010 .become_authenticated_pipe_user =
1011 smbd_become_authenticated_pipe_user,
1012 .unbecome_authenticated_pipe_user =
1013 smbd_unbecome_authenticated_pipe_user,
1014 .become_root = smbd_become_root,
1015 .unbecome_root = smbd_unbecome_root,
1019 talloc_enable_null_tracking();
1020 frame = talloc_stackframe();
1024 ok = samba_cmdline_init(frame,
1025 SAMBA_CMDLINE_CONFIG_SERVER,
1026 true /* require_smbconf */);
1028 DBG_ERR("Failed to init cmdline parser!\n");
1033 pc = samba_popt_get_context(progname, argc, argv, long_options, 0);
1035 DBG_ERR("Failed to setup popt context!\n");
1040 while ((opt = poptGetNextOpt(pc)) != -1) {
1042 "\nInvalid option %s: %s\n\n",
1043 poptBadOption(pc, 0),
1045 poptPrintUsage(pc, stderr, 0);
1049 poptFreeContext(pc);
1051 if (list_interfaces != 0) {
1052 const struct ndr_interface_table **ifaces = NULL;
1055 num_workers = lp_parm_int(
1056 -1, daemon_config_name, "num_workers", num_workers);
1057 idle_seconds = lp_parm_int(
1058 -1, daemon_config_name, "idle_seconds", idle_seconds);
1060 DBG_DEBUG("daemon=%s, num_workers=%d, idle_seconds=%d\n",
1065 fprintf(stdout, "%d\n%d\n", num_workers, idle_seconds);
1067 num_ifaces = get_interfaces(&ifaces, private_data);
1069 for (i=0; i<num_ifaces; i++) {
1070 rpc_worker_print_interface(stdout, ifaces[i]);
1077 log_stdout = (debug_get_log_type() == DEBUG_STDOUT);
1078 if (log_stdout != 0) {
1079 setup_logging(argv[0], DEBUG_STDOUT);
1081 setup_logging(argv[0], DEBUG_FILE);
1084 set_smbd_shim(&smbd_shim_fns);
1086 dump_core_setup(progname, lp_logfile(talloc_tos(), lp_sub));
1088 /* POSIX demands that signals are inherited. If the invoking
1089 * process has these signals masked, we will have problems, as
1090 * we won't receive them. */
1091 BlockSignals(False, SIGHUP);
1092 BlockSignals(False, SIGUSR1);
1093 BlockSignals(False, SIGTERM);
1096 /* we are never interested in SIGFPE */
1097 BlockSignals(True,SIGFPE);
1099 /* We no longer use USR2... */
1100 #if defined(SIGUSR2)
1101 BlockSignals(True, SIGUSR2);
1103 /* Ignore children - no zombies. */
1106 DEBUG(0, ("%s version %s started.\n",
1108 samba_version_string()));
1109 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1111 msg_ctx = global_messaging_context();
1112 if (msg_ctx == NULL) {
1113 DBG_ERR("global_messaging_context() failed\n");
1117 ev_ctx = messaging_tevent_context(msg_ctx);
1119 worker = rpc_worker_new(ev_ctx, msg_ctx);
1120 if (worker == NULL) {
1121 DBG_ERR("rpc_worker_new failed\n");
1122 global_messaging_context_free();
1126 dce_ctx = rpc_worker_dce_ctx(worker);
1128 se = tevent_add_signal(
1129 ev_ctx, ev_ctx, SIGTERM, 0, sig_term_handler, NULL);
1131 DBG_ERR("tevent_add_signal failed\n");
1132 global_messaging_context_free();
1136 BlockSignals(false, SIGTERM);
1138 se = tevent_add_signal(
1139 ev_ctx, ev_ctx, SIGHUP, 0, sig_hup_handler, NULL);
1141 DBG_ERR("tevent_add_signal failed\n");
1142 global_messaging_context_free();
1146 BlockSignals(false, SIGHUP);
1148 (void)winbind_off();
1149 ok = init_guest_session_info(NULL);
1152 DBG_WARNING("init_guest_session_info failed\n");
1153 global_messaging_context_free();
1158 status = init_system_session_info(NULL);
1159 if (!NT_STATUS_IS_OK(status)) {
1160 DBG_WARNING("init_system_session_info failed: %s\n",
1162 global_messaging_context_free();
1167 DBG_INFO("Initializing DCE/RPC registered endpoint servers\n");
1169 num_servers = get_servers(dce_ctx, &ep_servers, private_data);
1171 DBG_DEBUG("get_servers() returned %zu servers\n", num_servers);
1173 for (i=0; i<num_servers; i++) {
1174 status = register_ep_server(dce_ctx, ep_servers[i]);
1175 if (!NT_STATUS_IS_OK(status)) {
1176 DBG_DEBUG("register_ep_server failed: %s\n",
1178 global_messaging_context_free();
1184 req = rpc_worker_send(
1185 ev_ctx, ev_ctx, worker, getppid(), worker_group, worker_index);
1187 DBG_ERR("rpc_worker_send failed\n");
1188 global_messaging_context_free();
1193 DBG_DEBUG("%s worker running\n", progname);
1195 while (tevent_req_is_in_progress(req)) {
1196 TALLOC_CTX *loop_frame = NULL;
1198 loop_frame = talloc_stackframe();
1200 ret = tevent_loop_once(ev_ctx);
1202 TALLOC_FREE(loop_frame);
1205 DBG_WARNING("tevent_req_once() failed: %s\n",
1207 global_messaging_context_free();
1213 status = dcesrv_shutdown_registered_ep_servers(dce_ctx);
1214 if (!NT_STATUS_IS_OK(status)) {
1215 DBG_DEBUG("Shutdown failed with: %s\n",
1219 ret = rpc_worker_recv(req);
1221 DBG_DEBUG("rpc_worker_recv returned %s\n", strerror(ret));
1222 global_messaging_context_free();