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/>.
21 #include "smbd/smbd.h"
23 #include "lib/util/util_process.h"
26 #include "include/printing.h"
27 #include "printing/nt_printing_migrate_internal.h"
28 #include "printing/queue_process.h"
29 #include "printing/pcap.h"
30 #include "printing/load.h"
32 #include "librpc/gen_ndr/srv_winreg.h"
33 #include "librpc/gen_ndr/srv_spoolss.h"
34 #include "rpc_server/rpc_server.h"
35 #include "rpc_server/rpc_ep_register.h"
36 #include "rpc_server/rpc_config.h"
37 #include "rpc_server/spoolss/srv_spoolss_nt.h"
38 #include "librpc/rpc/dcerpc_ep.h"
39 #include "lib/server_prefork.h"
40 #include "lib/server_prefork_util.h"
42 #define SPOOLSS_PIPE_NAME "spoolss"
43 #define DAEMON_NAME "spoolssd"
45 static struct server_id parent_id
;
46 static struct prefork_pool
*spoolss_pool
= NULL
;
47 static int spoolss_child_id
= 0;
49 static struct pf_daemon_config default_pf_spoolss_cfg
= {
50 .prefork_status
= PFH_INIT
,
54 .max_allowed_clients
= 100,
55 .child_min_life
= 60 /* 1 minute minimum life time */
57 static struct pf_daemon_config pf_spoolss_cfg
= { 0 };
59 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
60 struct messaging_context
*msg_ctx
);
62 static void spoolss_reopen_logs(int child_id
)
64 char *lfile
= lp_logfile(talloc_tos());
69 rc
= asprintf(&ext
, "%s.%d", DAEMON_NAME
, child_id
);
71 rc
= asprintf(&ext
, "%s", DAEMON_NAME
);
79 if (lfile
== NULL
|| lfile
[0] == '\0') {
80 rc
= asprintf(&lfile
, "%s/log.%s",
81 get_dyn_LOGFILEBASE(), ext
);
83 if (strstr(lfile
, ext
) == NULL
) {
85 rc
= asprintf(&lfile
, "%s.%d",
86 lp_logfile(talloc_tos()),
89 rc
= asprintf(&lfile
, "%s.%s",
90 lp_logfile(talloc_tos()),
97 lp_set_logfile(lfile
);
106 static void update_conf(struct tevent_context
*ev
,
107 struct messaging_context
*msg
)
109 change_to_root_user();
110 lp_load_global(get_dyn_CONFIGFILE());
111 load_printers(ev
, msg
);
113 spoolss_reopen_logs(spoolss_child_id
);
114 if (spoolss_child_id
== 0) {
115 pfh_daemon_config(DAEMON_NAME
,
117 &default_pf_spoolss_cfg
);
118 pfh_manage_pool(ev
, msg
, &pf_spoolss_cfg
, spoolss_pool
);
122 static void smb_conf_updated(struct messaging_context
*msg
,
125 struct server_id server_id
,
128 struct tevent_context
*ev_ctx
= talloc_get_type_abort(private_data
,
129 struct tevent_context
);
131 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
132 update_conf(ev_ctx
, msg
);
135 static void spoolss_sig_term_handler(struct tevent_context
*ev
,
136 struct tevent_signal
*se
,
142 exit_server_cleanly("termination signal");
145 static void spoolss_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
147 struct tevent_signal
*se
;
149 se
= tevent_add_signal(ev_ctx
,
152 spoolss_sig_term_handler
,
155 exit_server("failed to setup SIGTERM handler");
159 static void spoolss_sig_hup_handler(struct tevent_context
*ev
,
160 struct tevent_signal
*se
,
166 struct messaging_context
*msg_ctx
;
168 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
170 DEBUG(1,("Reloading printers after SIGHUP\n"));
171 update_conf(ev
, msg_ctx
);
173 /* relay to all children */
175 prefork_send_signal_to_all(spoolss_pool
, SIGHUP
);
179 static void spoolss_setup_sig_hup_handler(struct tevent_context
*ev_ctx
,
180 struct messaging_context
*msg_ctx
)
182 struct tevent_signal
*se
;
184 se
= tevent_add_signal(ev_ctx
,
187 spoolss_sig_hup_handler
,
190 exit_server("failed to setup SIGHUP handler");
194 static bool spoolss_init_cb(void *ptr
)
196 struct messaging_context
*msg_ctx
= talloc_get_type_abort(
197 ptr
, struct messaging_context
);
199 return nt_printing_tdb_migrate(msg_ctx
);
202 static bool spoolss_shutdown_cb(void *ptr
)
204 srv_spoolss_cleanup();
211 static void spoolss_chld_sig_hup_handler(struct tevent_context
*ev
,
212 struct tevent_signal
*se
,
218 struct messaging_context
*msg_ctx
;
220 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
222 change_to_root_user();
223 DEBUG(1,("Reloading printers after SIGHUP\n"));
224 load_printers(ev
, msg_ctx
);
225 spoolss_reopen_logs(spoolss_child_id
);
228 static bool spoolss_setup_chld_hup_handler(struct tevent_context
*ev_ctx
,
229 struct messaging_context
*msg_ctx
,
230 struct pf_worker_data
*pf
)
232 struct tevent_signal
*se
;
234 se
= tevent_add_signal(ev_ctx
,
237 spoolss_chld_sig_hup_handler
,
240 DEBUG(1, ("failed to setup SIGHUP handler"));
247 static void parent_ping(struct messaging_context
*msg_ctx
,
250 struct server_id server_id
,
254 /* The fact we received this message is enough to let make the event
255 * loop if it was idle. spoolss_children_main will cycle through
256 * spoolss_next_client at least once. That function will take whatever
257 * action is necessary */
259 DEBUG(10, ("Got message that the parent changed status.\n"));
263 static bool spoolss_child_init(struct tevent_context
*ev_ctx
,
264 int child_id
, struct pf_worker_data
*pf
)
267 struct rpc_srv_callbacks spoolss_cb
;
268 struct messaging_context
*msg_ctx
= server_messaging_context();
271 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
273 if (!NT_STATUS_IS_OK(status
)) {
274 DEBUG(0,("reinit_after_fork() failed\n"));
275 smb_panic("reinit_after_fork() failed");
278 prctl_set_comment("spoolssd-child");
280 spoolss_child_id
= child_id
;
281 spoolss_reopen_logs(child_id
);
283 ok
= spoolss_setup_chld_hup_handler(ev_ctx
, msg_ctx
, pf
);
288 if (!serverid_register(messaging_server_id(msg_ctx
),
290 FLAG_MSG_PRINT_GENERAL
)) {
294 if (!locking_init()) {
298 messaging_register(msg_ctx
, ev_ctx
,
299 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
300 messaging_register(msg_ctx
, ev_ctx
,
301 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
303 /* As soon as messaging is up check if pcap has been loaded already.
304 * If so then we probably missed a message and should load_printers()
305 * ourselves. If pcap has not been loaded yet, then ignore, we will get
306 * a message as soon as the bq process completes the reload. */
307 if (pcap_cache_loaded(NULL
)) {
308 load_printers(ev_ctx
, msg_ctx
);
311 /* try to reinit rpc queues */
312 spoolss_cb
.init
= spoolss_init_cb
;
313 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
314 spoolss_cb
.private_data
= msg_ctx
;
316 status
= rpc_winreg_init(NULL
);
317 if (!NT_STATUS_IS_OK(status
)) {
318 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
323 status
= rpc_spoolss_init(&spoolss_cb
);
324 if (!NT_STATUS_IS_OK(status
)) {
325 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
333 struct spoolss_children_data
{
334 struct tevent_context
*ev_ctx
;
335 struct messaging_context
*msg_ctx
;
336 struct pf_worker_data
*pf
;
341 static void spoolss_next_client(void *pvt
);
343 static int spoolss_children_main(struct tevent_context
*ev_ctx
,
344 struct messaging_context
*msg_ctx
,
345 struct pf_worker_data
*pf
,
351 struct spoolss_children_data
*data
;
355 ok
= spoolss_child_init(ev_ctx
, child_id
, pf
);
360 data
= talloc(ev_ctx
, struct spoolss_children_data
);
365 data
->ev_ctx
= ev_ctx
;
366 data
->msg_ctx
= msg_ctx
;
367 data
->listen_fd_size
= listen_fd_size
;
368 data
->listen_fds
= listen_fds
;
370 /* loop until it is time to exit */
371 while (pf
->status
!= PF_WORKER_EXITING
) {
372 /* try to see if it is time to schedule the next client */
373 spoolss_next_client(data
);
375 ret
= tevent_loop_once(ev_ctx
);
377 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
378 ret
, strerror(errno
)));
379 pf
->status
= PF_WORKER_EXITING
;
386 static void spoolss_client_terminated(void *pvt
)
388 struct spoolss_children_data
*data
;
390 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
392 pfh_client_terminated(data
->pf
);
394 spoolss_next_client(pvt
);
397 struct spoolss_new_client
{
398 struct spoolss_children_data
*data
;
399 struct tsocket_address
*srv_addr
;
400 struct tsocket_address
*cli_addr
;
403 static void spoolss_handle_client(struct tevent_req
*req
);
405 static void spoolss_next_client(void *pvt
)
407 struct tevent_req
*req
;
408 struct spoolss_children_data
*data
;
409 struct spoolss_new_client
*next
;
411 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
413 if (!pfh_child_allowed_to_accept(data
->pf
)) {
414 /* nothing to do for now we are already listening
415 * or we are not allowed to listen further */
419 next
= talloc_zero(data
, struct spoolss_new_client
);
421 DEBUG(1, ("Out of memory!?\n"));
426 req
= prefork_listen_send(next
, data
->ev_ctx
, data
->pf
,
427 data
->listen_fd_size
,
430 DEBUG(1, ("Failed to make listening request!?\n"));
434 tevent_req_set_callback(req
, spoolss_handle_client
, next
);
437 static void spoolss_handle_client(struct tevent_req
*req
)
439 struct spoolss_children_data
*data
;
440 struct spoolss_new_client
*client
;
441 const DATA_BLOB ping
= data_blob_null
;
445 client
= tevent_req_callback_data(req
, struct spoolss_new_client
);
448 ret
= prefork_listen_recv(req
, client
, &sd
,
449 &client
->srv_addr
, &client
->cli_addr
);
451 /* this will free the request too */
455 DEBUG(6, ("No client connection was available after all!\n"));
459 /* Warn parent that our status changed */
460 messaging_send(data
->msg_ctx
, parent_id
,
461 MSG_PREFORK_CHILD_EVENT
, &ping
);
463 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
464 (int)(data
->pf
->pid
)));
466 named_pipe_accept_function(data
->ev_ctx
, data
->msg_ctx
,
467 SPOOLSS_PIPE_NAME
, sd
,
468 spoolss_client_terminated
, data
);
471 /* ==== Main Process Functions ==== */
473 extern pid_t background_lpq_updater_pid
;
474 static char *bq_logfile
;
476 static void check_updater_child(struct tevent_context
*ev_ctx
,
477 struct messaging_context
*msg_ctx
)
482 if (background_lpq_updater_pid
== -1) {
486 pid
= sys_waitpid(background_lpq_updater_pid
, &status
, WNOHANG
);
488 DEBUG(2, ("The background queue child died... Restarting!\n"));
489 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
490 background_lpq_updater_pid
= pid
;
494 static void child_ping(struct messaging_context
*msg_ctx
,
497 struct server_id server_id
,
500 struct tevent_context
*ev_ctx
;
502 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
504 DEBUG(10, ("Got message that a child changed status.\n"));
505 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
508 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
509 struct messaging_context
*msg_ctx
,
510 struct timeval current_time
);
511 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
512 struct tevent_timer
*te
,
513 struct timeval current_time
,
516 static void spoolssd_sigchld_handler(struct tevent_context
*ev_ctx
,
517 struct prefork_pool
*pfp
,
520 struct messaging_context
*msg_ctx
;
522 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
524 /* run pool management so we can fork/retire or increase
525 * the allowed connections per child based on load */
526 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
528 /* also check if the updater child is alive and well */
529 check_updater_child(ev_ctx
, msg_ctx
);
532 static bool spoolssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
533 struct messaging_context
*msg_ctx
)
537 /* add our oun sigchld callback */
538 prefork_set_sigchld_callback(spoolss_pool
,
539 spoolssd_sigchld_handler
, msg_ctx
);
541 ok
= spoolssd_schedule_check(ev_ctx
, msg_ctx
,
542 tevent_timeval_current());
546 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
547 struct messaging_context
*msg_ctx
,
548 struct timeval current_time
)
550 struct tevent_timer
*te
;
551 struct timeval next_event
;
553 /* check situation again in 10 seconds */
554 next_event
= tevent_timeval_current_ofs(10, 0);
556 /* TODO: check when the socket becomes readable, so that children
557 * are checked only when there is some activity ? */
558 te
= tevent_add_timer(ev_ctx
, spoolss_pool
, next_event
,
559 spoolssd_check_children
, msg_ctx
);
561 DEBUG(2, ("Failed to set up children monitoring!\n"));
568 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
569 struct tevent_timer
*te
,
570 struct timeval current_time
,
573 struct messaging_context
*msg_ctx
;
575 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
577 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
579 spoolssd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
582 static void print_queue_forward(struct messaging_context
*msg
,
585 struct server_id server_id
,
588 messaging_send_buf(msg
, pid_to_procid(background_lpq_updater_pid
),
589 MSG_PRINTER_UPDATE
, data
->data
, data
->length
);
592 static char *get_bq_logfile(void)
594 char *lfile
= lp_logfile(talloc_tos());
597 if (lfile
== NULL
|| lfile
[0] == '\0') {
598 rc
= asprintf(&lfile
, "%s/log.%s.bq",
599 get_dyn_LOGFILEBASE(), DAEMON_NAME
);
601 rc
= asprintf(&lfile
, "%s.bq", lp_logfile(talloc_tos()));
609 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
610 struct messaging_context
*msg_ctx
)
612 enum rpc_service_mode_e epm_mode
= rpc_epmapper_mode();
613 struct rpc_srv_callbacks spoolss_cb
;
614 struct dcerpc_binding_vector
*v
;
622 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
625 * Block signals before forking child as it will have to
626 * set its own handlers. Child will re-enable SIGHUP as
627 * soon as the handlers are set up.
629 BlockSignals(true, SIGTERM
);
630 BlockSignals(true, SIGHUP
);
635 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
639 /* parent or error */
642 /* Re-enable SIGHUP before returnig */
643 BlockSignals(false, SIGTERM
);
644 BlockSignals(false, SIGHUP
);
648 status
= smbd_reinit_after_fork(msg_ctx
, ev_ctx
, true);
649 if (!NT_STATUS_IS_OK(status
)) {
650 DEBUG(0,("reinit_after_fork() failed\n"));
651 smb_panic("reinit_after_fork() failed");
654 prctl_set_comment("spoolssd-master");
656 /* save the parent process id so the children can use it later */
657 parent_id
= messaging_server_id(msg_ctx
);
659 spoolss_reopen_logs(0);
660 pfh_daemon_config(DAEMON_NAME
,
662 &default_pf_spoolss_cfg
);
664 spoolss_setup_sig_term_handler(ev_ctx
);
665 spoolss_setup_sig_hup_handler(ev_ctx
, msg_ctx
);
667 BlockSignals(false, SIGTERM
);
668 BlockSignals(false, SIGHUP
);
670 /* always start the backgroundqueue listner in spoolssd */
671 bq_logfile
= get_bq_logfile();
672 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
674 background_lpq_updater_pid
= pid
;
677 /* the listening fd must be created before the children are actually
679 listen_fd
= create_named_pipe_socket(SPOOLSS_PIPE_NAME
);
680 if (listen_fd
== -1) {
684 ret
= listen(listen_fd
, pf_spoolss_cfg
.max_allowed_clients
);
686 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
691 /* start children before any more initialization is done */
692 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
695 pf_spoolss_cfg
.min_children
,
696 pf_spoolss_cfg
.max_children
,
697 &spoolss_children_main
, NULL
,
703 if (!serverid_register(messaging_server_id(msg_ctx
),
705 FLAG_MSG_PRINT_GENERAL
)) {
709 if (!locking_init()) {
713 messaging_register(msg_ctx
, ev_ctx
,
714 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
715 messaging_register(msg_ctx
, NULL
, MSG_PRINTER_UPDATE
,
716 print_queue_forward
);
717 messaging_register(msg_ctx
, ev_ctx
,
718 MSG_PREFORK_CHILD_EVENT
, child_ping
);
721 * As soon as messaging is up check if pcap has been loaded already.
722 * If pcap has not been loaded yet, then ignore, as we will reload on
723 * client enumeration anyway.
725 if (pcap_cache_loaded(NULL
)) {
726 load_printers(ev_ctx
, msg_ctx
);
729 mem_ctx
= talloc_new(NULL
);
730 if (mem_ctx
== NULL
) {
735 * Initialize spoolss with an init function to convert printers first.
736 * static_init_rpc will try to initialize the spoolss server too but you
737 * can't register it twice.
739 spoolss_cb
.init
= spoolss_init_cb
;
740 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
741 spoolss_cb
.private_data
= msg_ctx
;
743 status
= rpc_winreg_init(NULL
);
744 if (!NT_STATUS_IS_OK(status
)) {
745 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
750 status
= rpc_spoolss_init(&spoolss_cb
);
751 if (!NT_STATUS_IS_OK(status
)) {
752 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
757 if (epm_mode
!= RPC_SERVICE_MODE_DISABLED
&&
758 (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
759 status
= dcerpc_binding_vector_new(mem_ctx
, &v
);
760 if (!NT_STATUS_IS_OK(status
)) {
761 DEBUG(0, ("Failed to create binding vector (%s)\n",
766 status
= dcerpc_binding_vector_add_np_default(&ndr_table_spoolss
, v
);
767 if (!NT_STATUS_IS_OK(status
)) {
768 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
773 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_spoolss
, v
);
774 if (!NT_STATUS_IS_OK(status
)) {
775 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
781 talloc_free(mem_ctx
);
783 ok
= spoolssd_setup_children_monitor(ev_ctx
, msg_ctx
);
785 DEBUG(0, ("Failed to setup children monitoring!\n"));
789 DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
791 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
794 ret
= tevent_loop_wait(ev_ctx
);
796 /* should not be reached */
797 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
798 ret
, (ret
== 0) ? "out of events" : strerror(errno
)));