tests/krb5: Fix method for creating invalid length zeroed checksum
[Samba.git] / source3 / rpc_server / epmd.c
blobf1178f67aa33fe0095e4366b437cdb28a1d41858
1 /*
2 * Unix SMB/CIFS implementation.
4 * SMBD RPC service callbacks
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
24 #include "ntdomain.h"
25 #include "messages.h"
27 #include "librpc/rpc/dcerpc_ep.h"
29 #include "librpc/rpc/dcesrv_core.h"
30 #include "librpc/gen_ndr/ndr_epmapper_scompat.h"
32 #include "rpc_server/rpc_server.h"
33 #include "rpc_server/rpc_service_setup.h"
34 #include "rpc_server/rpc_sock_helper.h"
35 #include "rpc_server/epmapper/srv_epmapper.h"
36 #include "rpc_server/epmd.h"
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_RPC_SRV
41 #define DAEMON_NAME "epmd"
43 static void epmd_reopen_logs(void)
45 const struct loadparm_substitution *lp_sub =
46 loadparm_s3_global_substitution();
47 char *lfile = lp_logfile(talloc_tos(), lp_sub);
48 int rc;
50 if (lfile == NULL || lfile[0] == '\0') {
51 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
52 if (rc > 0) {
53 lp_set_logfile(lfile);
54 SAFE_FREE(lfile);
56 } else {
57 if (strstr(lfile, DAEMON_NAME) == NULL) {
58 rc = asprintf(&lfile, "%s.%s",
59 lp_logfile(talloc_tos(), lp_sub), DAEMON_NAME);
60 if (rc > 0) {
61 lp_set_logfile(lfile);
62 SAFE_FREE(lfile);
67 reopen_logs();
70 static void epmd_smb_conf_updated(struct messaging_context *msg,
71 void *private_data,
72 uint32_t msg_type,
73 struct server_id server_id,
74 DATA_BLOB *data)
76 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
77 change_to_root_user();
78 epmd_reopen_logs();
81 static void epmd_sig_term_handler(struct tevent_context *ev,
82 struct tevent_signal *se,
83 int signum,
84 int count,
85 void *siginfo,
86 void *private_data)
88 exit_server_cleanly("termination signal");
91 static void epmd_setup_sig_term_handler(struct tevent_context *ev_ctx)
93 struct tevent_signal *se;
95 se = tevent_add_signal(ev_ctx,
96 ev_ctx,
97 SIGTERM, 0,
98 epmd_sig_term_handler,
99 NULL);
100 if (se == NULL) {
101 exit_server("failed to setup SIGTERM handler");
105 static void epmd_sig_hup_handler(struct tevent_context *ev,
106 struct tevent_signal *se,
107 int signum,
108 int count,
109 void *siginfo,
110 void *private_data)
112 change_to_root_user();
114 DEBUG(1,("Reloading printers after SIGHUP\n"));
115 epmd_reopen_logs();
118 static void epmd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
119 struct messaging_context *msg_ctx)
121 struct tevent_signal *se;
123 se = tevent_add_signal(ev_ctx,
124 ev_ctx,
125 SIGHUP, 0,
126 epmd_sig_hup_handler,
127 msg_ctx);
128 if (se == NULL) {
129 exit_server("failed to setup SIGHUP handler");
133 void start_epmd(struct tevent_context *ev_ctx,
134 struct messaging_context *msg_ctx,
135 struct dcesrv_context *dce_ctx)
137 NTSTATUS status;
138 pid_t pid;
139 int rc;
140 const struct dcesrv_endpoint_server *ep_server = NULL;
141 struct dcesrv_endpoint *e = NULL;
143 DEBUG(1, ("Forking Endpoint Mapper Daemon\n"));
145 pid = fork();
147 if (pid == -1) {
148 DEBUG(0, ("Failed to fork Endpoint Mapper [%s], aborting ...\n",
149 strerror(errno)));
150 exit(1);
153 if (pid) {
154 /* parent */
155 return;
158 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, "epmd");
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(0,("reinit_after_fork() failed\n"));
161 smb_panic("reinit_after_fork() failed");
164 epmd_reopen_logs();
166 epmd_setup_sig_term_handler(ev_ctx);
167 epmd_setup_sig_hup_handler(ev_ctx, msg_ctx);
169 messaging_register(msg_ctx,
170 ev_ctx,
171 MSG_SMB_CONF_UPDATED,
172 epmd_smb_conf_updated);
174 DBG_INFO("Registering DCE/RPC endpoint servers\n");
176 /* Register the endpoint server in DCERPC core */
177 ep_server = epmapper_get_ep_server();
178 if (ep_server == NULL) {
179 DBG_ERR("Failed to get 'epmapper' endpoint server\n");
180 exit(1);
183 status = dcerpc_register_ep_server(ep_server);
184 if (!NT_STATUS_IS_OK(status)) {
185 DBG_ERR("Failed to register 'epmapper' endpoint server: %s\n",
186 nt_errstr(status));
187 exit(1);
190 DBG_INFO("Reinitializing DCE/RPC server context\n");
192 status = dcesrv_reinit_context(dce_ctx);
193 if (!NT_STATUS_IS_OK(status)) {
194 DBG_ERR("Failed to reinit DCE/RPC context: %s\n",
195 nt_errstr(status));
196 exit(1);
199 DBG_INFO("Initializing DCE/RPC registered endpoint servers\n");
201 status = dcesrv_init_ep_server(dce_ctx, "epmapper");
202 if (!NT_STATUS_IS_OK(status)) {
203 DBG_ERR("Failed to init DCE/RPC endpoint server: %s\n",
204 nt_errstr(status));
205 exit(1);
208 DBG_INFO("Initializing DCE/RPC connection endpoints\n");
210 for (e = dce_ctx->endpoint_list; e; e = e->next) {
211 enum dcerpc_transport_t transport =
212 dcerpc_binding_get_transport(e->ep_description);
213 dcerpc_ncacn_termination_fn term_fn = NULL;
215 if (transport == NCACN_HTTP) {
216 continue;
219 if (transport == NCALRPC) {
220 term_fn = srv_epmapper_delete_endpoints;
223 status = dcesrv_setup_endpoint_sockets(ev_ctx,
224 msg_ctx,
225 dce_ctx,
227 term_fn,
228 NULL); /* termination_data */
229 if (!NT_STATUS_IS_OK(status)) {
230 char *ep_string = dcerpc_binding_string(
231 dce_ctx, e->ep_description);
232 DBG_ERR("Failed to setup endpoint '%s': %s\n",
233 ep_string, nt_errstr(status));
234 TALLOC_FREE(ep_string);
235 exit(1);
239 DEBUG(1, ("Endpoint Mapper Daemon Started (%u)\n", (unsigned int)getpid()));
241 /* loop forever */
242 rc = tevent_loop_wait(ev_ctx);
244 /* should not be reached */
245 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
246 rc, (rc == 0) ? "out of events" : strerror(errno)));
248 exit(1);
251 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */