s3: RPC: Don't crash on trying to talloc_free(-1) if smb_iconv_open_ex() fails.
[Samba.git] / source3 / rpc_server / mdssd.c
blob6f2635c65196eebf4bd6c37bf7d38a8e1367df71
1 /*
2 * Unix SMB/CIFS implementation.
4 * mds service daemon
6 * Copyright (c) 2014 Ralph Boehme <rb@sernet.de>
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"
23 #include "messages.h"
24 #include "ntdomain.h"
26 #include "lib/id_cache.h"
28 #include "../lib/tsocket/tsocket.h"
29 #include "lib/server_prefork.h"
30 #include "lib/server_prefork_util.h"
31 #include "librpc/rpc/dcerpc_ep.h"
33 #include "rpc_server/rpc_server.h"
34 #include "rpc_server/rpc_ep_register.h"
35 #include "rpc_server/rpc_sock_helper.h"
36 #include "rpc_server/rpc_modules.h"
38 #include "librpc/gen_ndr/srv_mdssvc.h"
39 #include "rpc_server/mdssvc/srv_mdssvc_nt.h"
40 #include "rpc_server/mdssd.h"
42 #undef DBGC_CLASS
43 #define DBGC_CLASS DBGC_RPC_SRV
45 #define DAEMON_NAME "mdssd"
46 #define MDSSD_MAX_SOCKETS 64
48 static struct server_id parent_id;
49 static struct prefork_pool *mdssd_pool = NULL;
50 static int mdssd_child_id = 0;
52 static struct pf_daemon_config default_pf_mdssd_cfg = {
53 .prefork_status = PFH_INIT,
54 .min_children = 5,
55 .max_children = 25,
56 .spawn_rate = 5,
57 .max_allowed_clients = 1000,
58 .child_min_life = 60 /* 1 minute minimum life time */
60 static struct pf_daemon_config pf_mdssd_cfg = { 0 };
62 void start_mdssd(struct tevent_context *ev_ctx,
63 struct messaging_context *msg_ctx);
65 static void mdssd_smb_conf_updated(struct messaging_context *msg,
66 void *private_data,
67 uint32_t msg_type,
68 struct server_id server_id,
69 DATA_BLOB *data)
71 struct tevent_context *ev_ctx;
73 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
74 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
76 change_to_root_user();
77 lp_load_global(get_dyn_CONFIGFILE());
79 reopen_logs();
80 if (mdssd_child_id == 0) {
81 pfh_daemon_config(DAEMON_NAME,
82 &pf_mdssd_cfg,
83 &default_pf_mdssd_cfg);
84 pfh_manage_pool(ev_ctx, msg, &pf_mdssd_cfg, mdssd_pool);
88 static void mdssd_sig_term_handler(struct tevent_context *ev,
89 struct tevent_signal *se,
90 int signum,
91 int count,
92 void *siginfo,
93 void *private_data)
95 shutdown_rpc_module("mdssvc");
97 exit_server_cleanly("termination signal");
100 static void mdssd_setup_sig_term_handler(struct tevent_context *ev_ctx)
102 struct tevent_signal *se;
104 se = tevent_add_signal(ev_ctx,
105 ev_ctx,
106 SIGTERM, 0,
107 mdssd_sig_term_handler,
108 NULL);
109 if (!se) {
110 exit_server("failed to setup SIGTERM handler");
114 static void mdssd_sig_hup_handler(struct tevent_context *ev,
115 struct tevent_signal *se,
116 int signum,
117 int count,
118 void *siginfo,
119 void *pvt)
122 change_to_root_user();
123 lp_load_global(get_dyn_CONFIGFILE());
125 reopen_logs();
126 pfh_daemon_config(DAEMON_NAME,
127 &pf_mdssd_cfg,
128 &default_pf_mdssd_cfg);
130 /* relay to all children */
131 prefork_send_signal_to_all(mdssd_pool, SIGHUP);
134 static void mdssd_setup_sig_hup_handler(struct tevent_context *ev_ctx)
136 struct tevent_signal *se;
138 se = tevent_add_signal(ev_ctx,
139 ev_ctx,
140 SIGHUP, 0,
141 mdssd_sig_hup_handler,
142 NULL);
143 if (!se) {
144 DEBUG(0, ("failed to setup SIGHUP handler\n"));
145 exit(1);
149 /**********************************************************
150 * Children
151 **********************************************************/
153 static void mdssd_chld_sig_hup_handler(struct tevent_context *ev,
154 struct tevent_signal *se,
155 int signum,
156 int count,
157 void *siginfo,
158 void *pvt)
160 change_to_root_user();
161 reopen_logs();
164 static bool mdssd_setup_chld_hup_handler(struct tevent_context *ev_ctx)
166 struct tevent_signal *se;
168 se = tevent_add_signal(ev_ctx,
169 ev_ctx,
170 SIGHUP, 0,
171 mdssd_chld_sig_hup_handler,
172 NULL);
173 if (!se) {
174 DEBUG(1, ("failed to setup SIGHUP handler"));
175 return false;
178 return true;
181 static void parent_ping(struct messaging_context *msg_ctx,
182 void *private_data,
183 uint32_t msg_type,
184 struct server_id server_id,
185 DATA_BLOB *data)
188 * The fact we received this message is enough to let make the
189 * event loop if it was idle. mdssd_children_main will cycle
190 * through mdssd_next_client at least once. That function will
191 * take whatever action is necessary
193 DEBUG(10, ("Got message that the parent changed status.\n"));
194 return;
197 static bool mdssd_child_init(struct tevent_context *ev_ctx,
198 int child_id,
199 struct pf_worker_data *pf)
201 NTSTATUS status;
202 struct messaging_context *msg_ctx = global_messaging_context();
203 bool ok;
205 status = reinit_after_fork(msg_ctx, ev_ctx,
206 true, "mdssd-child");
207 if (!NT_STATUS_IS_OK(status)) {
208 DEBUG(0,("reinit_after_fork() failed\n"));
209 smb_panic("reinit_after_fork() failed");
212 mdssd_child_id = child_id;
213 reopen_logs();
215 ok = mdssd_setup_chld_hup_handler(ev_ctx);
216 if (!ok) {
217 return false;
220 messaging_register(msg_ctx, ev_ctx,
221 MSG_SMB_CONF_UPDATED, mdssd_smb_conf_updated);
222 messaging_register(msg_ctx, ev_ctx,
223 MSG_PREFORK_PARENT_EVENT, parent_ping);
225 ok = setup_rpc_module(ev_ctx, msg_ctx, "mdssvc");
226 if (!ok) {
227 DBG_ERR("Failed to initialize mdssvc module\n");
228 return false;
231 return true;
234 struct mdssd_children_data {
235 struct tevent_context *ev_ctx;
236 struct messaging_context *msg_ctx;
237 struct pf_worker_data *pf;
238 int listen_fd_size;
239 struct pf_listen_fd *listen_fds;
242 static void mdssd_next_client(void *pvt);
244 static int mdssd_children_main(struct tevent_context *ev_ctx,
245 struct messaging_context *msg_ctx,
246 struct pf_worker_data *pf,
247 int child_id,
248 int listen_fd_size,
249 struct pf_listen_fd *listen_fds,
250 void *private_data)
252 struct mdssd_children_data *data;
253 bool ok;
254 int ret = 0;
256 ok = mdssd_child_init(ev_ctx, child_id, pf);
257 if (!ok) {
258 return 1;
261 data = talloc(ev_ctx, struct mdssd_children_data);
262 if (!data) {
263 return 1;
265 data->pf = pf;
266 data->ev_ctx = ev_ctx;
267 data->msg_ctx = msg_ctx;
268 data->listen_fd_size = listen_fd_size;
269 data->listen_fds = listen_fds;
271 /* loop until it is time to exit */
272 while (pf->status != PF_WORKER_EXITING) {
273 /* try to see if it is time to schedule the next client */
274 mdssd_next_client(data);
276 ret = tevent_loop_once(ev_ctx);
277 if (ret != 0) {
278 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
279 ret, strerror(errno)));
280 pf->status = PF_WORKER_EXITING;
284 return ret;
287 static void mdssd_client_terminated(struct pipes_struct *p, void *pvt)
289 struct mdssd_children_data *data;
291 data = talloc_get_type_abort(pvt, struct mdssd_children_data);
293 pfh_client_terminated(data->pf);
295 mdssd_next_client(pvt);
298 struct mdssd_new_client {
299 struct mdssd_children_data *data;
302 static void mdssd_handle_client(struct tevent_req *req);
304 static void mdssd_next_client(void *pvt)
306 struct tevent_req *req;
307 struct mdssd_children_data *data;
308 struct mdssd_new_client *next;
310 data = talloc_get_type_abort(pvt, struct mdssd_children_data);
312 if (!pfh_child_allowed_to_accept(data->pf)) {
313 /* nothing to do for now we are already listening
314 * or we are not allowed to listen further */
315 return;
318 next = talloc_zero(data, struct mdssd_new_client);
319 if (!next) {
320 DEBUG(1, ("Out of memory!?\n"));
321 return;
323 next->data = data;
325 req = prefork_listen_send(next,
326 data->ev_ctx,
327 data->pf,
328 data->listen_fd_size,
329 data->listen_fds);
330 if (!req) {
331 DEBUG(1, ("Failed to make listening request!?\n"));
332 talloc_free(next);
333 return;
335 tevent_req_set_callback(req, mdssd_handle_client, next);
338 static void mdssd_handle_client(struct tevent_req *req)
340 struct mdssd_children_data *data;
341 struct mdssd_new_client *client;
342 const DATA_BLOB ping = data_blob_null;
343 int rc;
344 int sd;
345 TALLOC_CTX *tmp_ctx;
346 struct tsocket_address *srv_addr;
347 struct tsocket_address *cli_addr;
349 client = tevent_req_callback_data(req, struct mdssd_new_client);
350 data = client->data;
352 tmp_ctx = talloc_stackframe();
353 if (tmp_ctx == NULL) {
354 DEBUG(1, ("Failed to allocate stackframe!\n"));
355 return;
358 rc = prefork_listen_recv(req,
359 tmp_ctx,
360 &sd,
361 NULL,
362 &srv_addr,
363 &cli_addr);
365 /* this will free the request too */
366 talloc_free(client);
368 if (rc != 0) {
369 DEBUG(6, ("No client connection was available after all!\n"));
370 goto done;
373 /* Warn parent that our status changed */
374 messaging_send(data->msg_ctx, parent_id,
375 MSG_PREFORK_CHILD_EVENT, &ping);
377 DEBUG(2, ("mdssd preforked child %d got client connection!\n",
378 (int)(data->pf->pid)));
380 if (tsocket_address_is_inet(srv_addr, "ip")) {
381 DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
382 tsocket_address_string(cli_addr, tmp_ctx),
383 tsocket_address_string(srv_addr, tmp_ctx)));
385 dcerpc_ncacn_accept(data->ev_ctx,
386 data->msg_ctx,
387 NCACN_IP_TCP,
388 "IP",
389 cli_addr,
390 srv_addr,
392 mdssd_client_terminated,
393 data);
394 } else if (tsocket_address_is_unix(srv_addr)) {
395 const char *p;
396 const char *b;
398 p = tsocket_address_unix_path(srv_addr, tmp_ctx);
399 if (p == NULL) {
400 talloc_free(tmp_ctx);
401 return;
404 b = strrchr(p, '/');
405 if (b != NULL) {
406 b++;
407 } else {
408 b = p;
411 if (strstr(p, "/np/")) {
412 dcerpc_ncacn_accept(data->ev_ctx,
413 data->msg_ctx,
414 NCACN_NP,
416 NULL, /* remote client address */
417 NULL, /* local server address */
419 mdssd_client_terminated,
420 data);
421 } else {
422 dcerpc_ncacn_accept(data->ev_ctx,
423 data->msg_ctx,
424 NCALRPC,
426 cli_addr,
427 srv_addr,
429 mdssd_client_terminated,
430 data);
432 } else {
433 DEBUG(0, ("ERROR: Unsupported socket!\n"));
436 done:
437 talloc_free(tmp_ctx);
441 * MAIN
444 static void child_ping(struct messaging_context *msg_ctx,
445 void *private_data,
446 uint32_t msg_type,
447 struct server_id server_id,
448 DATA_BLOB *data)
450 struct tevent_context *ev_ctx;
452 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
454 DEBUG(10, ("Got message that a child changed status.\n"));
455 pfh_manage_pool(ev_ctx, msg_ctx, &pf_mdssd_cfg, mdssd_pool);
458 static bool mdssd_schedule_check(struct tevent_context *ev_ctx,
459 struct messaging_context *msg_ctx,
460 struct timeval current_time);
462 static void mdssd_check_children(struct tevent_context *ev_ctx,
463 struct tevent_timer *te,
464 struct timeval current_time,
465 void *pvt);
467 static void mdssd_sigchld_handler(struct tevent_context *ev_ctx,
468 struct prefork_pool *pfp,
469 void *pvt)
471 struct messaging_context *msg_ctx;
473 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
475 /* run pool management so we can fork/retire or increase
476 * the allowed connections per child based on load */
477 pfh_manage_pool(ev_ctx, msg_ctx, &pf_mdssd_cfg, mdssd_pool);
480 static bool mdssd_setup_children_monitor(struct tevent_context *ev_ctx,
481 struct messaging_context *msg_ctx)
483 bool ok;
485 /* add our oun sigchld callback */
486 prefork_set_sigchld_callback(mdssd_pool, mdssd_sigchld_handler, msg_ctx);
488 ok = mdssd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());
490 return ok;
493 static bool mdssd_schedule_check(struct tevent_context *ev_ctx,
494 struct messaging_context *msg_ctx,
495 struct timeval current_time)
497 struct tevent_timer *te;
498 struct timeval next_event;
500 /* check situation again in 10 seconds */
501 next_event = tevent_timeval_current_ofs(10, 0);
503 /* TODO: check when the socket becomes readable, so that children
504 * are checked only when there is some activity ? */
505 te = tevent_add_timer(ev_ctx, mdssd_pool, next_event,
506 mdssd_check_children, msg_ctx);
507 if (!te) {
508 DEBUG(2, ("Failed to set up children monitoring!\n"));
509 return false;
512 return true;
515 static void mdssd_check_children(struct tevent_context *ev_ctx,
516 struct tevent_timer *te,
517 struct timeval current_time,
518 void *pvt)
520 struct messaging_context *msg_ctx;
522 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
524 pfh_manage_pool(ev_ctx, msg_ctx, &pf_mdssd_cfg, mdssd_pool);
526 mdssd_schedule_check(ev_ctx, msg_ctx, current_time);
530 * start it up
533 static bool mdssd_create_sockets(struct tevent_context *ev_ctx,
534 struct messaging_context *msg_ctx,
535 struct pf_listen_fd *listen_fd,
536 int *listen_fd_size)
538 struct dcerpc_binding_vector *v, *v_orig;
539 TALLOC_CTX *tmp_ctx;
540 NTSTATUS status;
541 int fd = -1;
542 int rc;
543 bool ok = false;
545 tmp_ctx = talloc_stackframe();
546 if (tmp_ctx == NULL) {
547 return false;
550 status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
551 if (!NT_STATUS_IS_OK(status)) {
552 goto done;
555 /* mdssvc */
556 status = dcesrv_create_ncacn_np_socket("mdssvc", &fd);
557 if (!NT_STATUS_IS_OK(status)) {
558 goto done;
561 rc = listen(fd, pf_mdssd_cfg.max_allowed_clients);
562 if (rc == -1) {
563 goto done;
565 listen_fd[*listen_fd_size].fd = fd;
566 listen_fd[*listen_fd_size].fd_data = NULL;
567 (*listen_fd_size)++;
568 fd = -1;
570 status = dcesrv_create_ncalrpc_socket("mdssvc", &fd);
571 if (!NT_STATUS_IS_OK(status)) {
572 goto done;
575 rc = listen(fd, pf_mdssd_cfg.max_allowed_clients);
576 if (rc == -1) {
577 goto done;
579 listen_fd[*listen_fd_size].fd = fd;
580 listen_fd[*listen_fd_size].fd_data = NULL;
581 (*listen_fd_size)++;
582 fd = -1;
584 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
585 if (v == NULL) {
586 goto done;
589 status = dcerpc_binding_vector_replace_iface(&ndr_table_mdssvc, v);
590 if (!NT_STATUS_IS_OK(status)) {
591 goto done;
594 status = dcerpc_binding_vector_add_np_default(&ndr_table_mdssvc, v);
595 if (!NT_STATUS_IS_OK(status)) {
596 goto done;
599 status = dcerpc_binding_vector_add_unix(&ndr_table_mdssvc, v, "mdssvc");
600 if (!NT_STATUS_IS_OK(status)) {
601 goto done;
604 ok = true;
605 done:
606 if (fd != -1) {
607 close(fd);
609 talloc_free(tmp_ctx);
610 return ok;
613 void start_mdssd(struct tevent_context *ev_ctx,
614 struct messaging_context *msg_ctx)
616 NTSTATUS status;
617 struct pf_listen_fd listen_fd[MDSSD_MAX_SOCKETS];
618 int listen_fd_size = 0;
619 pid_t pid;
620 int rc;
621 bool ok;
623 DEBUG(1, ("Forking Metadata Service Daemon\n"));
626 * Block signals before forking child as it will have to
627 * set its own handlers. Child will re-enable SIGHUP as
628 * soon as the handlers are set up.
630 BlockSignals(true, SIGTERM);
631 BlockSignals(true, SIGHUP);
633 pid = fork();
634 if (pid == -1) {
635 DEBUG(0, ("Failed to fork mdssd [%s], aborting ...\n",
636 strerror(errno)));
637 exit(1);
640 /* parent or error */
641 if (pid != 0) {
643 /* Re-enable SIGHUP before returnig */
644 BlockSignals(false, SIGTERM);
645 BlockSignals(false, SIGHUP);
647 return;
650 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, "mdssd-master");
651 if (!NT_STATUS_IS_OK(status)) {
652 DEBUG(0,("reinit_after_fork() failed\n"));
653 smb_panic("reinit_after_fork() failed");
656 reopen_logs();
658 /* save the parent process id so the children can use it later */
659 parent_id = messaging_server_id(msg_ctx);
661 pfh_daemon_config(DAEMON_NAME,
662 &pf_mdssd_cfg,
663 &default_pf_mdssd_cfg);
665 mdssd_setup_sig_term_handler(ev_ctx);
666 mdssd_setup_sig_hup_handler(ev_ctx);
668 BlockSignals(false, SIGTERM);
669 BlockSignals(false, SIGHUP);
671 ok = mdssd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
672 if (!ok) {
673 exit(1);
676 /* start children before any more initialization is done */
677 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
678 ev_ctx,
679 msg_ctx,
680 listen_fd_size,
681 listen_fd,
682 pf_mdssd_cfg.min_children,
683 pf_mdssd_cfg.max_children,
684 &mdssd_children_main,
685 NULL,
686 &mdssd_pool);
687 if (!ok) {
688 exit(1);
691 messaging_register(msg_ctx,
692 ev_ctx,
693 MSG_SMB_CONF_UPDATED,
694 mdssd_smb_conf_updated);
695 messaging_register(msg_ctx, ev_ctx,
696 MSG_PREFORK_CHILD_EVENT, child_ping);
698 ok = mdssd_setup_children_monitor(ev_ctx, msg_ctx);
699 if (!ok) {
700 exit(1);
703 DEBUG(1, ("mdssd Daemon Started (%u)\n", (unsigned int)getpid()));
705 /* loop forever */
706 rc = tevent_loop_wait(ev_ctx);
708 /* should not be reached */
709 DEBUG(0,("mdssd: tevent_loop_wait() exited with %d - %s\n",
710 rc, (rc == 0) ? "out of events" : strerror(errno)));
711 exit(1);