2 Unix SMB/Netbios implementation.
4 Copyright (C) Simo Sorce <idra@samba.org> 2010-2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "smbd/smbd.h"
23 #include "include/printing.h"
24 #include "printing/nt_printing_migrate_internal.h"
25 #include "printing/queue_process.h"
26 #include "printing/pcap.h"
27 #include "printing/load.h"
29 #include "librpc/gen_ndr/srv_winreg.h"
30 #include "librpc/gen_ndr/srv_spoolss.h"
31 #include "rpc_server/rpc_server.h"
32 #include "rpc_server/rpc_ep_register.h"
33 #include "rpc_server/rpc_config.h"
34 #include "rpc_server/spoolss/srv_spoolss_nt.h"
35 #include "librpc/rpc/dcerpc_ep.h"
36 #include "lib/server_prefork.h"
37 #include "lib/server_prefork_util.h"
39 #define SPOOLSS_PIPE_NAME "spoolss"
40 #define DAEMON_NAME "spoolssd"
42 static struct server_id parent_id;
43 static struct prefork_pool *spoolss_pool = NULL;
44 static int spoolss_child_id = 0;
46 static struct pf_daemon_config default_pf_spoolss_cfg = {
47 .prefork_status = PFH_INIT,
51 .max_allowed_clients = 100,
52 .child_min_life = 60 /* 1 minute minimum life time */
54 static struct pf_daemon_config pf_spoolss_cfg = { 0 };
56 pid_t start_spoolssd(struct tevent_context *ev_ctx,
57 struct messaging_context *msg_ctx);
59 static void spoolss_reopen_logs(int child_id)
61 char *lfile = lp_logfile(talloc_tos());
66 rc = asprintf(&ext, "%s.%d", DAEMON_NAME, child_id);
68 rc = asprintf(&ext, "%s", DAEMON_NAME);
76 if (lfile == NULL || lfile[0] == '\0') {
77 rc = asprintf(&lfile, "%s/log.%s",
78 get_dyn_LOGFILEBASE(), ext);
80 if (strstr(lfile, ext) == NULL) {
82 rc = asprintf(&lfile, "%s.%d",
83 lp_logfile(talloc_tos()),
86 rc = asprintf(&lfile, "%s.%s",
87 lp_logfile(talloc_tos()),
94 lp_set_logfile(lfile);
103 static void update_conf(struct tevent_context *ev,
104 struct messaging_context *msg)
106 change_to_root_user();
107 lp_load_global(get_dyn_CONFIGFILE());
110 spoolss_reopen_logs(spoolss_child_id);
111 if (spoolss_child_id == 0) {
112 pfh_daemon_config(DAEMON_NAME,
114 &default_pf_spoolss_cfg);
115 pfh_manage_pool(ev, msg, &pf_spoolss_cfg, spoolss_pool);
119 static void smb_conf_updated(struct messaging_context *msg,
122 struct server_id server_id,
125 struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
126 struct tevent_context);
128 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
129 update_conf(ev_ctx, msg);
132 static void spoolss_sig_term_handler(struct tevent_context *ev,
133 struct tevent_signal *se,
139 exit_server_cleanly("termination signal");
142 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
144 struct tevent_signal *se;
146 se = tevent_add_signal(ev_ctx,
149 spoolss_sig_term_handler,
152 exit_server("failed to setup SIGTERM handler");
156 static void spoolss_sig_hup_handler(struct tevent_context *ev,
157 struct tevent_signal *se,
163 struct messaging_context *msg_ctx;
165 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
167 DEBUG(1,("Reloading printers after SIGHUP\n"));
168 update_conf(ev, msg_ctx);
170 /* relay to all children */
172 prefork_send_signal_to_all(spoolss_pool, SIGHUP);
176 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
177 struct messaging_context *msg_ctx)
179 struct tevent_signal *se;
181 se = tevent_add_signal(ev_ctx,
184 spoolss_sig_hup_handler,
187 exit_server("failed to setup SIGHUP handler");
191 static bool spoolss_init_cb(void *ptr)
193 struct messaging_context *msg_ctx = talloc_get_type_abort(
194 ptr, struct messaging_context);
196 return nt_printing_tdb_migrate(msg_ctx);
199 static bool spoolss_shutdown_cb(void *ptr)
201 srv_spoolss_cleanup();
208 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
209 struct tevent_signal *se,
215 change_to_root_user();
216 DEBUG(1,("Reloading printers after SIGHUP\n"));
218 spoolss_reopen_logs(spoolss_child_id);
221 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
222 struct messaging_context *msg_ctx,
223 struct pf_worker_data *pf)
225 struct tevent_signal *se;
227 se = tevent_add_signal(ev_ctx,
230 spoolss_chld_sig_hup_handler,
233 DEBUG(1, ("failed to setup SIGHUP handler"));
240 static void parent_ping(struct messaging_context *msg_ctx,
243 struct server_id server_id,
247 /* The fact we received this message is enough to let make the event
248 * loop if it was idle. spoolss_children_main will cycle through
249 * spoolss_next_client at least once. That function will take whatever
250 * action is necessary */
252 DEBUG(10, ("Got message that the parent changed status.\n"));
256 static bool spoolss_child_init(struct tevent_context *ev_ctx,
257 int child_id, struct pf_worker_data *pf)
260 struct rpc_srv_callbacks spoolss_cb;
261 struct messaging_context *msg_ctx = global_messaging_context();
264 status = reinit_after_fork(msg_ctx, ev_ctx, true, "spoolssd-child");
265 if (!NT_STATUS_IS_OK(status)) {
266 DEBUG(0,("reinit_after_fork() failed\n"));
267 smb_panic("reinit_after_fork() failed");
270 spoolss_child_id = child_id;
271 spoolss_reopen_logs(child_id);
273 ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
278 if (!locking_init()) {
282 messaging_register(msg_ctx, ev_ctx,
283 MSG_SMB_CONF_UPDATED, smb_conf_updated);
284 messaging_register(msg_ctx, ev_ctx,
285 MSG_PREFORK_PARENT_EVENT, parent_ping);
287 /* As soon as messaging is up check if pcap has been loaded already.
288 * If so then we probably missed a message and should load_printers()
289 * ourselves. If pcap has not been loaded yet, then ignore, we will get
290 * a message as soon as the bq process completes the reload. */
293 /* try to reinit rpc queues */
294 spoolss_cb.init = spoolss_init_cb;
295 spoolss_cb.shutdown = spoolss_shutdown_cb;
296 spoolss_cb.private_data = msg_ctx;
298 status = rpc_winreg_init(NULL);
299 if (!NT_STATUS_IS_OK(status)) {
300 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
305 status = rpc_spoolss_init(&spoolss_cb);
306 if (!NT_STATUS_IS_OK(status)) {
307 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
315 struct spoolss_children_data {
316 struct tevent_context *ev_ctx;
317 struct messaging_context *msg_ctx;
318 struct pf_worker_data *pf;
323 static void spoolss_next_client(void *pvt);
325 static int spoolss_children_main(struct tevent_context *ev_ctx,
326 struct messaging_context *msg_ctx,
327 struct pf_worker_data *pf,
333 struct spoolss_children_data *data;
337 ok = spoolss_child_init(ev_ctx, child_id, pf);
342 data = talloc(ev_ctx, struct spoolss_children_data);
347 data->ev_ctx = ev_ctx;
348 data->msg_ctx = msg_ctx;
349 data->listen_fd_size = listen_fd_size;
350 data->listen_fds = listen_fds;
352 /* loop until it is time to exit */
353 while (pf->status != PF_WORKER_EXITING) {
354 /* try to see if it is time to schedule the next client */
355 spoolss_next_client(data);
357 ret = tevent_loop_once(ev_ctx);
359 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
360 ret, strerror(errno)));
361 pf->status = PF_WORKER_EXITING;
368 static void spoolss_client_terminated(void *pvt)
370 struct spoolss_children_data *data;
372 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
374 pfh_client_terminated(data->pf);
376 spoolss_next_client(pvt);
379 struct spoolss_new_client {
380 struct spoolss_children_data *data;
381 struct tsocket_address *srv_addr;
382 struct tsocket_address *cli_addr;
385 static void spoolss_handle_client(struct tevent_req *req);
387 static void spoolss_next_client(void *pvt)
389 struct tevent_req *req;
390 struct spoolss_children_data *data;
391 struct spoolss_new_client *next;
393 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
395 if (!pfh_child_allowed_to_accept(data->pf)) {
396 /* nothing to do for now we are already listening
397 * or we are not allowed to listen further */
401 next = talloc_zero(data, struct spoolss_new_client);
403 DEBUG(1, ("Out of memory!?\n"));
408 req = prefork_listen_send(next, data->ev_ctx, data->pf,
409 data->listen_fd_size,
412 DEBUG(1, ("Failed to make listening request!?\n"));
416 tevent_req_set_callback(req, spoolss_handle_client, next);
419 static void spoolss_handle_client(struct tevent_req *req)
421 struct spoolss_children_data *data;
422 struct spoolss_new_client *client;
423 const DATA_BLOB ping = data_blob_null;
427 client = tevent_req_callback_data(req, struct spoolss_new_client);
430 ret = prefork_listen_recv(req, client, &sd,
431 &client->srv_addr, &client->cli_addr);
433 /* this will free the request too */
437 DEBUG(6, ("No client connection was available after all!\n"));
441 /* Warn parent that our status changed */
442 messaging_send(data->msg_ctx, parent_id,
443 MSG_PREFORK_CHILD_EVENT, &ping);
445 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
446 (int)(data->pf->pid)));
448 dcerpc_ncacn_accept(data->ev_ctx,
452 NULL, /* remote client address */
453 NULL, /* local server address */
455 NULL, /* disconnect function */
456 spoolss_client_terminated,
460 /* ==== Main Process Functions ==== */
462 extern pid_t background_lpq_updater_pid;
463 static char *bq_logfile;
465 static void check_updater_child(struct tevent_context *ev_ctx,
466 struct messaging_context *msg_ctx)
471 if (background_lpq_updater_pid == -1) {
475 pid = waitpid(background_lpq_updater_pid, &status, WNOHANG);
477 DEBUG(2, ("The background queue child died... Restarting!\n"));
478 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
479 background_lpq_updater_pid = pid;
483 static void child_ping(struct messaging_context *msg_ctx,
486 struct server_id server_id,
489 struct tevent_context *ev_ctx;
491 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
493 DEBUG(10, ("Got message that a child changed status.\n"));
494 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
497 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
498 struct messaging_context *msg_ctx,
499 struct timeval current_time);
500 static void spoolssd_check_children(struct tevent_context *ev_ctx,
501 struct tevent_timer *te,
502 struct timeval current_time,
505 static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
506 struct prefork_pool *pfp,
509 struct messaging_context *msg_ctx;
511 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
513 /* run pool management so we can fork/retire or increase
514 * the allowed connections per child based on load */
515 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
517 /* also check if the updater child is alive and well */
518 check_updater_child(ev_ctx, msg_ctx);
521 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
522 struct messaging_context *msg_ctx)
526 /* add our oun sigchld callback */
527 prefork_set_sigchld_callback(spoolss_pool,
528 spoolssd_sigchld_handler, msg_ctx);
530 ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
531 tevent_timeval_current());
535 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
536 struct messaging_context *msg_ctx,
537 struct timeval current_time)
539 struct tevent_timer *te;
540 struct timeval next_event;
542 /* check situation again in 10 seconds */
543 next_event = tevent_timeval_current_ofs(10, 0);
545 /* TODO: check when the socket becomes readable, so that children
546 * are checked only when there is some activity ? */
547 te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
548 spoolssd_check_children, msg_ctx);
550 DEBUG(2, ("Failed to set up children monitoring!\n"));
557 static void spoolssd_check_children(struct tevent_context *ev_ctx,
558 struct tevent_timer *te,
559 struct timeval current_time,
562 struct messaging_context *msg_ctx;
564 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
566 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
568 spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
571 static void print_queue_forward(struct messaging_context *msg,
574 struct server_id server_id,
577 messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
578 MSG_PRINTER_UPDATE, data->data, data->length);
581 static char *get_bq_logfile(void)
583 char *lfile = lp_logfile(talloc_tos());
586 if (lfile == NULL || lfile[0] == '\0') {
587 rc = asprintf(&lfile, "%s/log.%s.bq",
588 get_dyn_LOGFILEBASE(), DAEMON_NAME);
590 rc = asprintf(&lfile, "%s.bq", lp_logfile(talloc_tos()));
598 pid_t start_spoolssd(struct tevent_context *ev_ctx,
599 struct messaging_context *msg_ctx)
601 enum rpc_service_mode_e epm_mode = rpc_epmapper_mode();
602 struct rpc_srv_callbacks spoolss_cb;
603 struct dcerpc_binding_vector *v;
611 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
614 * Block signals before forking child as it will have to
615 * set its own handlers. Child will re-enable SIGHUP as
616 * soon as the handlers are set up.
618 BlockSignals(true, SIGTERM);
619 BlockSignals(true, SIGHUP);
624 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
629 /* parent or error */
632 /* Re-enable SIGHUP before returnig */
633 BlockSignals(false, SIGTERM);
634 BlockSignals(false, SIGHUP);
638 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true,
640 if (!NT_STATUS_IS_OK(status)) {
641 DEBUG(0,("reinit_after_fork() failed\n"));
642 smb_panic("reinit_after_fork() failed");
645 /* save the parent process id so the children can use it later */
646 parent_id = messaging_server_id(msg_ctx);
648 spoolss_reopen_logs(0);
649 pfh_daemon_config(DAEMON_NAME,
651 &default_pf_spoolss_cfg);
653 spoolss_setup_sig_term_handler(ev_ctx);
654 spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
656 BlockSignals(false, SIGTERM);
657 BlockSignals(false, SIGHUP);
659 /* always start the backgroundqueue listner in spoolssd */
660 bq_logfile = get_bq_logfile();
661 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
663 background_lpq_updater_pid = pid;
666 /* the listening fd must be created before the children are actually
668 status = dcesrv_create_ncacn_np_socket(SPOOLSS_PIPE_NAME, &listen_fd);
669 if (!NT_STATUS_IS_OK(status)) {
673 ret = listen(listen_fd, pf_spoolss_cfg.max_allowed_clients);
675 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
680 /* start children before any more initialization is done */
681 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
684 pf_spoolss_cfg.min_children,
685 pf_spoolss_cfg.max_children,
686 &spoolss_children_main, NULL,
692 if (!locking_init()) {
696 messaging_register(msg_ctx, ev_ctx,
697 MSG_SMB_CONF_UPDATED, smb_conf_updated);
698 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
699 print_queue_forward);
700 messaging_register(msg_ctx, ev_ctx,
701 MSG_PREFORK_CHILD_EVENT, child_ping);
704 * As soon as messaging is up check if pcap has been loaded already.
705 * If pcap has not been loaded yet, then ignore, as we will reload on
706 * client enumeration anyway.
710 mem_ctx = talloc_new(NULL);
711 if (mem_ctx == NULL) {
716 * Initialize spoolss with an init function to convert printers first.
717 * static_init_rpc will try to initialize the spoolss server too but you
718 * can't register it twice.
720 spoolss_cb.init = spoolss_init_cb;
721 spoolss_cb.shutdown = spoolss_shutdown_cb;
722 spoolss_cb.private_data = msg_ctx;
724 status = rpc_winreg_init(NULL);
725 if (!NT_STATUS_IS_OK(status)) {
726 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
731 status = rpc_spoolss_init(&spoolss_cb);
732 if (!NT_STATUS_IS_OK(status)) {
733 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
738 if (epm_mode != RPC_SERVICE_MODE_DISABLED &&
739 (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
740 status = dcerpc_binding_vector_new(mem_ctx, &v);
741 if (!NT_STATUS_IS_OK(status)) {
742 DEBUG(0, ("Failed to create binding vector (%s)\n",
747 status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
748 if (!NT_STATUS_IS_OK(status)) {
749 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
754 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
755 if (!NT_STATUS_IS_OK(status)) {
756 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
762 talloc_free(mem_ctx);
764 ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
766 DEBUG(0, ("Failed to setup children monitoring!\n"));
770 DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
772 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
775 ret = tevent_loop_wait(ev_ctx);
777 /* should not be reached */
778 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
779 ret, (ret == 0) ? "out of events" : strerror(errno)));