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"
24 #include "include/printing.h"
25 #include "printing/nt_printing_migrate_internal.h"
26 #include "printing/queue_process.h"
27 #include "printing/pcap.h"
28 #include "printing/load.h"
30 #include "librpc/gen_ndr/srv_winreg.h"
31 #include "librpc/gen_ndr/srv_spoolss.h"
32 #include "rpc_server/rpc_server.h"
33 #include "rpc_server/rpc_ep_register.h"
34 #include "rpc_server/rpc_config.h"
35 #include "rpc_server/spoolss/srv_spoolss_nt.h"
36 #include "librpc/rpc/dcerpc_ep.h"
37 #include "lib/server_prefork.h"
38 #include "lib/server_prefork_util.h"
40 #define SPOOLSS_PIPE_NAME "spoolss"
41 #define DAEMON_NAME "spoolssd"
43 static struct server_id parent_id
;
44 static struct prefork_pool
*spoolss_pool
= NULL
;
45 static int spoolss_child_id
= 0;
47 static struct pf_daemon_config default_pf_spoolss_cfg
= {
48 .prefork_status
= PFH_INIT
,
52 .max_allowed_clients
= 100,
53 .child_min_life
= 60 /* 1 minute minimum life time */
55 static struct pf_daemon_config pf_spoolss_cfg
= { 0 };
57 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
58 struct messaging_context
*msg_ctx
);
60 static void spoolss_reopen_logs(int child_id
)
62 char *lfile
= lp_logfile(talloc_tos());
67 rc
= asprintf(&ext
, "%s.%d", DAEMON_NAME
, child_id
);
69 rc
= asprintf(&ext
, "%s", DAEMON_NAME
);
77 if (lfile
== NULL
|| lfile
[0] == '\0') {
78 rc
= asprintf(&lfile
, "%s/log.%s",
79 get_dyn_LOGFILEBASE(), ext
);
81 if (strstr(lfile
, ext
) == NULL
) {
83 rc
= asprintf(&lfile
, "%s.%d",
84 lp_logfile(talloc_tos()),
87 rc
= asprintf(&lfile
, "%s.%s",
88 lp_logfile(talloc_tos()),
95 lp_set_logfile(lfile
);
104 static void update_conf(struct tevent_context
*ev
,
105 struct messaging_context
*msg
)
107 change_to_root_user();
108 lp_load_global(get_dyn_CONFIGFILE());
109 load_printers(ev
, msg
);
111 spoolss_reopen_logs(spoolss_child_id
);
112 if (spoolss_child_id
== 0) {
113 pfh_daemon_config(DAEMON_NAME
,
115 &default_pf_spoolss_cfg
);
116 pfh_manage_pool(ev
, msg
, &pf_spoolss_cfg
, spoolss_pool
);
120 static void smb_conf_updated(struct messaging_context
*msg
,
123 struct server_id server_id
,
126 struct tevent_context
*ev_ctx
= talloc_get_type_abort(private_data
,
127 struct tevent_context
);
129 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
130 update_conf(ev_ctx
, msg
);
133 static void spoolss_sig_term_handler(struct tevent_context
*ev
,
134 struct tevent_signal
*se
,
140 exit_server_cleanly("termination signal");
143 static void spoolss_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
145 struct tevent_signal
*se
;
147 se
= tevent_add_signal(ev_ctx
,
150 spoolss_sig_term_handler
,
153 exit_server("failed to setup SIGTERM handler");
157 static void spoolss_sig_hup_handler(struct tevent_context
*ev
,
158 struct tevent_signal
*se
,
164 struct messaging_context
*msg_ctx
;
166 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
168 DEBUG(1,("Reloading printers after SIGHUP\n"));
169 update_conf(ev
, msg_ctx
);
171 /* relay to all children */
173 prefork_send_signal_to_all(spoolss_pool
, SIGHUP
);
177 static void spoolss_setup_sig_hup_handler(struct tevent_context
*ev_ctx
,
178 struct messaging_context
*msg_ctx
)
180 struct tevent_signal
*se
;
182 se
= tevent_add_signal(ev_ctx
,
185 spoolss_sig_hup_handler
,
188 exit_server("failed to setup SIGHUP handler");
192 static bool spoolss_init_cb(void *ptr
)
194 struct messaging_context
*msg_ctx
= talloc_get_type_abort(
195 ptr
, struct messaging_context
);
197 return nt_printing_tdb_migrate(msg_ctx
);
200 static bool spoolss_shutdown_cb(void *ptr
)
202 srv_spoolss_cleanup();
209 static void spoolss_chld_sig_hup_handler(struct tevent_context
*ev
,
210 struct tevent_signal
*se
,
216 struct messaging_context
*msg_ctx
;
218 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
220 change_to_root_user();
221 DEBUG(1,("Reloading printers after SIGHUP\n"));
222 load_printers(ev
, msg_ctx
);
223 spoolss_reopen_logs(spoolss_child_id
);
226 static bool spoolss_setup_chld_hup_handler(struct tevent_context
*ev_ctx
,
227 struct messaging_context
*msg_ctx
,
228 struct pf_worker_data
*pf
)
230 struct tevent_signal
*se
;
232 se
= tevent_add_signal(ev_ctx
,
235 spoolss_chld_sig_hup_handler
,
238 DEBUG(1, ("failed to setup SIGHUP handler"));
245 static void parent_ping(struct messaging_context
*msg_ctx
,
248 struct server_id server_id
,
252 /* The fact we received this message is enough to let make the event
253 * loop if it was idle. spoolss_children_main will cycle through
254 * spoolss_next_client at least once. That function will take whatever
255 * action is necessary */
257 DEBUG(10, ("Got message that the parent changed status.\n"));
261 static bool spoolss_child_init(struct tevent_context
*ev_ctx
,
262 int child_id
, struct pf_worker_data
*pf
)
265 struct rpc_srv_callbacks spoolss_cb
;
266 struct messaging_context
*msg_ctx
= server_messaging_context();
269 status
= reinit_after_fork(msg_ctx
, ev_ctx
, true, "spoolssd-child");
270 if (!NT_STATUS_IS_OK(status
)) {
271 DEBUG(0,("reinit_after_fork() failed\n"));
272 smb_panic("reinit_after_fork() failed");
275 spoolss_child_id
= child_id
;
276 spoolss_reopen_logs(child_id
);
278 ok
= spoolss_setup_chld_hup_handler(ev_ctx
, msg_ctx
, pf
);
283 if (!serverid_register(messaging_server_id(msg_ctx
),
285 FLAG_MSG_PRINT_GENERAL
)) {
289 if (!locking_init()) {
293 messaging_register(msg_ctx
, ev_ctx
,
294 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
295 messaging_register(msg_ctx
, ev_ctx
,
296 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
298 /* As soon as messaging is up check if pcap has been loaded already.
299 * If so then we probably missed a message and should load_printers()
300 * ourselves. If pcap has not been loaded yet, then ignore, we will get
301 * a message as soon as the bq process completes the reload. */
302 if (pcap_cache_loaded(NULL
)) {
303 load_printers(ev_ctx
, msg_ctx
);
306 /* try to reinit rpc queues */
307 spoolss_cb
.init
= spoolss_init_cb
;
308 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
309 spoolss_cb
.private_data
= msg_ctx
;
311 status
= rpc_winreg_init(NULL
);
312 if (!NT_STATUS_IS_OK(status
)) {
313 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
318 status
= rpc_spoolss_init(&spoolss_cb
);
319 if (!NT_STATUS_IS_OK(status
)) {
320 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
328 struct spoolss_children_data
{
329 struct tevent_context
*ev_ctx
;
330 struct messaging_context
*msg_ctx
;
331 struct pf_worker_data
*pf
;
336 static void spoolss_next_client(void *pvt
);
338 static int spoolss_children_main(struct tevent_context
*ev_ctx
,
339 struct messaging_context
*msg_ctx
,
340 struct pf_worker_data
*pf
,
346 struct spoolss_children_data
*data
;
350 ok
= spoolss_child_init(ev_ctx
, child_id
, pf
);
355 data
= talloc(ev_ctx
, struct spoolss_children_data
);
360 data
->ev_ctx
= ev_ctx
;
361 data
->msg_ctx
= msg_ctx
;
362 data
->listen_fd_size
= listen_fd_size
;
363 data
->listen_fds
= listen_fds
;
365 /* loop until it is time to exit */
366 while (pf
->status
!= PF_WORKER_EXITING
) {
367 /* try to see if it is time to schedule the next client */
368 spoolss_next_client(data
);
370 ret
= tevent_loop_once(ev_ctx
);
372 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
373 ret
, strerror(errno
)));
374 pf
->status
= PF_WORKER_EXITING
;
381 static void spoolss_client_terminated(void *pvt
)
383 struct spoolss_children_data
*data
;
385 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
387 pfh_client_terminated(data
->pf
);
389 spoolss_next_client(pvt
);
392 struct spoolss_new_client
{
393 struct spoolss_children_data
*data
;
394 struct tsocket_address
*srv_addr
;
395 struct tsocket_address
*cli_addr
;
398 static void spoolss_handle_client(struct tevent_req
*req
);
400 static void spoolss_next_client(void *pvt
)
402 struct tevent_req
*req
;
403 struct spoolss_children_data
*data
;
404 struct spoolss_new_client
*next
;
406 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
408 if (!pfh_child_allowed_to_accept(data
->pf
)) {
409 /* nothing to do for now we are already listening
410 * or we are not allowed to listen further */
414 next
= talloc_zero(data
, struct spoolss_new_client
);
416 DEBUG(1, ("Out of memory!?\n"));
421 req
= prefork_listen_send(next
, data
->ev_ctx
, data
->pf
,
422 data
->listen_fd_size
,
425 DEBUG(1, ("Failed to make listening request!?\n"));
429 tevent_req_set_callback(req
, spoolss_handle_client
, next
);
432 static void spoolss_handle_client(struct tevent_req
*req
)
434 struct spoolss_children_data
*data
;
435 struct spoolss_new_client
*client
;
436 const DATA_BLOB ping
= data_blob_null
;
440 client
= tevent_req_callback_data(req
, struct spoolss_new_client
);
443 ret
= prefork_listen_recv(req
, client
, &sd
,
444 &client
->srv_addr
, &client
->cli_addr
);
446 /* this will free the request too */
450 DEBUG(6, ("No client connection was available after all!\n"));
454 /* Warn parent that our status changed */
455 messaging_send(data
->msg_ctx
, parent_id
,
456 MSG_PREFORK_CHILD_EVENT
, &ping
);
458 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
459 (int)(data
->pf
->pid
)));
461 named_pipe_accept_function(data
->ev_ctx
, data
->msg_ctx
,
462 SPOOLSS_PIPE_NAME
, sd
,
463 spoolss_client_terminated
, data
);
466 /* ==== Main Process Functions ==== */
468 extern pid_t background_lpq_updater_pid
;
469 static char *bq_logfile
;
471 static void check_updater_child(struct tevent_context
*ev_ctx
,
472 struct messaging_context
*msg_ctx
)
477 if (background_lpq_updater_pid
== -1) {
481 pid
= waitpid(background_lpq_updater_pid
, &status
, WNOHANG
);
483 DEBUG(2, ("The background queue child died... Restarting!\n"));
484 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
485 background_lpq_updater_pid
= pid
;
489 static void child_ping(struct messaging_context
*msg_ctx
,
492 struct server_id server_id
,
495 struct tevent_context
*ev_ctx
;
497 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
499 DEBUG(10, ("Got message that a child changed status.\n"));
500 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
503 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
504 struct messaging_context
*msg_ctx
,
505 struct timeval current_time
);
506 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
507 struct tevent_timer
*te
,
508 struct timeval current_time
,
511 static void spoolssd_sigchld_handler(struct tevent_context
*ev_ctx
,
512 struct prefork_pool
*pfp
,
515 struct messaging_context
*msg_ctx
;
517 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
519 /* run pool management so we can fork/retire or increase
520 * the allowed connections per child based on load */
521 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
523 /* also check if the updater child is alive and well */
524 check_updater_child(ev_ctx
, msg_ctx
);
527 static bool spoolssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
528 struct messaging_context
*msg_ctx
)
532 /* add our oun sigchld callback */
533 prefork_set_sigchld_callback(spoolss_pool
,
534 spoolssd_sigchld_handler
, msg_ctx
);
536 ok
= spoolssd_schedule_check(ev_ctx
, msg_ctx
,
537 tevent_timeval_current());
541 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
542 struct messaging_context
*msg_ctx
,
543 struct timeval current_time
)
545 struct tevent_timer
*te
;
546 struct timeval next_event
;
548 /* check situation again in 10 seconds */
549 next_event
= tevent_timeval_current_ofs(10, 0);
551 /* TODO: check when the socket becomes readable, so that children
552 * are checked only when there is some activity ? */
553 te
= tevent_add_timer(ev_ctx
, spoolss_pool
, next_event
,
554 spoolssd_check_children
, msg_ctx
);
556 DEBUG(2, ("Failed to set up children monitoring!\n"));
563 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
564 struct tevent_timer
*te
,
565 struct timeval current_time
,
568 struct messaging_context
*msg_ctx
;
570 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
572 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
574 spoolssd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
577 static void print_queue_forward(struct messaging_context
*msg
,
580 struct server_id server_id
,
583 messaging_send_buf(msg
, pid_to_procid(background_lpq_updater_pid
),
584 MSG_PRINTER_UPDATE
, data
->data
, data
->length
);
587 static char *get_bq_logfile(void)
589 char *lfile
= lp_logfile(talloc_tos());
592 if (lfile
== NULL
|| lfile
[0] == '\0') {
593 rc
= asprintf(&lfile
, "%s/log.%s.bq",
594 get_dyn_LOGFILEBASE(), DAEMON_NAME
);
596 rc
= asprintf(&lfile
, "%s.bq", lp_logfile(talloc_tos()));
604 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
605 struct messaging_context
*msg_ctx
)
607 enum rpc_service_mode_e epm_mode
= rpc_epmapper_mode();
608 struct rpc_srv_callbacks spoolss_cb
;
609 struct dcerpc_binding_vector
*v
;
617 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
620 * Block signals before forking child as it will have to
621 * set its own handlers. Child will re-enable SIGHUP as
622 * soon as the handlers are set up.
624 BlockSignals(true, SIGTERM
);
625 BlockSignals(true, SIGHUP
);
630 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
634 /* parent or error */
637 /* Re-enable SIGHUP before returnig */
638 BlockSignals(false, SIGTERM
);
639 BlockSignals(false, SIGHUP
);
643 status
= smbd_reinit_after_fork(msg_ctx
, ev_ctx
, true,
645 if (!NT_STATUS_IS_OK(status
)) {
646 DEBUG(0,("reinit_after_fork() failed\n"));
647 smb_panic("reinit_after_fork() failed");
650 /* save the parent process id so the children can use it later */
651 parent_id
= messaging_server_id(msg_ctx
);
653 spoolss_reopen_logs(0);
654 pfh_daemon_config(DAEMON_NAME
,
656 &default_pf_spoolss_cfg
);
658 spoolss_setup_sig_term_handler(ev_ctx
);
659 spoolss_setup_sig_hup_handler(ev_ctx
, msg_ctx
);
661 BlockSignals(false, SIGTERM
);
662 BlockSignals(false, SIGHUP
);
664 /* always start the backgroundqueue listner in spoolssd */
665 bq_logfile
= get_bq_logfile();
666 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
668 background_lpq_updater_pid
= pid
;
671 /* the listening fd must be created before the children are actually
673 listen_fd
= create_named_pipe_socket(SPOOLSS_PIPE_NAME
);
674 if (listen_fd
== -1) {
678 ret
= listen(listen_fd
, pf_spoolss_cfg
.max_allowed_clients
);
680 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
685 /* start children before any more initialization is done */
686 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
689 pf_spoolss_cfg
.min_children
,
690 pf_spoolss_cfg
.max_children
,
691 &spoolss_children_main
, NULL
,
697 if (!serverid_register(messaging_server_id(msg_ctx
),
699 FLAG_MSG_PRINT_GENERAL
)) {
703 if (!locking_init()) {
707 messaging_register(msg_ctx
, ev_ctx
,
708 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
709 messaging_register(msg_ctx
, NULL
, MSG_PRINTER_UPDATE
,
710 print_queue_forward
);
711 messaging_register(msg_ctx
, ev_ctx
,
712 MSG_PREFORK_CHILD_EVENT
, child_ping
);
715 * As soon as messaging is up check if pcap has been loaded already.
716 * If pcap has not been loaded yet, then ignore, as we will reload on
717 * client enumeration anyway.
719 if (pcap_cache_loaded(NULL
)) {
720 load_printers(ev_ctx
, msg_ctx
);
723 mem_ctx
= talloc_new(NULL
);
724 if (mem_ctx
== NULL
) {
729 * Initialize spoolss with an init function to convert printers first.
730 * static_init_rpc will try to initialize the spoolss server too but you
731 * can't register it twice.
733 spoolss_cb
.init
= spoolss_init_cb
;
734 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
735 spoolss_cb
.private_data
= msg_ctx
;
737 status
= rpc_winreg_init(NULL
);
738 if (!NT_STATUS_IS_OK(status
)) {
739 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
744 status
= rpc_spoolss_init(&spoolss_cb
);
745 if (!NT_STATUS_IS_OK(status
)) {
746 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
751 if (epm_mode
!= RPC_SERVICE_MODE_DISABLED
&&
752 (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
753 status
= dcerpc_binding_vector_new(mem_ctx
, &v
);
754 if (!NT_STATUS_IS_OK(status
)) {
755 DEBUG(0, ("Failed to create binding vector (%s)\n",
760 status
= dcerpc_binding_vector_add_np_default(&ndr_table_spoolss
, v
);
761 if (!NT_STATUS_IS_OK(status
)) {
762 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
767 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_spoolss
, v
);
768 if (!NT_STATUS_IS_OK(status
)) {
769 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
775 talloc_free(mem_ctx
);
777 ok
= spoolssd_setup_children_monitor(ev_ctx
, msg_ctx
);
779 DEBUG(0, ("Failed to setup children monitoring!\n"));
783 DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
785 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
788 ret
= tevent_loop_wait(ev_ctx
);
790 /* should not be reached */
791 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
792 ret
, (ret
== 0) ? "out of events" : strerror(errno
)));