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 "locking/share_mode_lock.h"
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"
29 #include "printing/spoolssd.h"
31 #include "librpc/gen_ndr/ndr_winreg_scompat.h"
32 #include "librpc/gen_ndr/ndr_spoolss_scompat.h"
33 #include "rpc_server/rpc_server.h"
34 #include "rpc_server/rpc_service_setup.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 "librpc/rpc/dcesrv_core.h"
40 #include "lib/server_prefork.h"
41 #include "lib/server_prefork_util.h"
42 #include "lib/global_contexts.h"
45 #define DBGC_CLASS DBGC_RPC_SRV
47 #define DAEMON_NAME "spoolssd"
49 static struct server_id parent_id
;
50 static struct prefork_pool
*spoolss_pool
= NULL
;
51 static int spoolss_child_id
= 0;
53 static const struct pf_daemon_config default_pf_spoolss_cfg
= {
54 .prefork_status
= PFH_INIT
,
58 .max_allowed_clients
= 100,
59 .child_min_life
= 60 /* 1 minute minimum life time */
61 static struct pf_daemon_config pf_spoolss_cfg
= { 0 };
63 static void spoolss_reopen_logs(int child_id
)
65 const struct loadparm_substitution
*lp_sub
=
66 loadparm_s3_global_substitution();
67 char *lfile
= lp_logfile(talloc_tos(), lp_sub
);
72 rc
= asprintf(&ext
, "%s.%d", DAEMON_NAME
, child_id
);
74 rc
= asprintf(&ext
, "%s", DAEMON_NAME
);
82 if (lfile
== NULL
|| lfile
[0] == '\0') {
83 rc
= asprintf(&lfile
, "%s/log.%s",
84 get_dyn_LOGFILEBASE(), ext
);
86 if (strstr(lfile
, ext
) == NULL
) {
88 rc
= asprintf(&lfile
, "%s.%d",
89 lp_logfile(talloc_tos(), lp_sub
),
92 rc
= asprintf(&lfile
, "%s.%s",
93 lp_logfile(talloc_tos(), lp_sub
),
100 lp_set_logfile(lfile
);
109 static void update_conf(struct tevent_context
*ev
,
110 struct messaging_context
*msg
)
112 change_to_root_user();
113 lp_load_global(get_dyn_CONFIGFILE());
116 spoolss_reopen_logs(spoolss_child_id
);
117 if (spoolss_child_id
== 0) {
118 pfh_daemon_config(DAEMON_NAME
,
120 &default_pf_spoolss_cfg
);
121 pfh_manage_pool(ev
, msg
, &pf_spoolss_cfg
, spoolss_pool
);
125 static void smb_conf_updated(struct messaging_context
*msg
,
128 struct server_id server_id
,
131 struct tevent_context
*ev_ctx
= talloc_get_type_abort(private_data
,
132 struct tevent_context
);
134 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
135 update_conf(ev_ctx
, msg
);
138 static void spoolss_sig_term_handler(struct tevent_context
*ev
,
139 struct tevent_signal
*se
,
145 exit_server_cleanly("termination signal");
148 static void spoolss_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
150 struct tevent_signal
*se
;
152 se
= tevent_add_signal(ev_ctx
,
155 spoolss_sig_term_handler
,
158 exit_server("failed to setup SIGTERM handler");
162 static void spoolss_sig_hup_handler(struct tevent_context
*ev
,
163 struct tevent_signal
*se
,
169 struct messaging_context
*msg_ctx
;
171 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
173 DEBUG(1,("Reloading printers after SIGHUP\n"));
174 update_conf(ev
, msg_ctx
);
176 /* relay to all children */
178 prefork_send_signal_to_all(spoolss_pool
, SIGHUP
);
182 static void spoolss_setup_sig_hup_handler(struct tevent_context
*ev_ctx
,
183 struct messaging_context
*msg_ctx
)
185 struct tevent_signal
*se
;
187 se
= tevent_add_signal(ev_ctx
,
190 spoolss_sig_hup_handler
,
193 exit_server("failed to setup SIGHUP handler");
199 static void spoolss_chld_sig_hup_handler(struct tevent_context
*ev
,
200 struct tevent_signal
*se
,
206 change_to_root_user();
207 DEBUG(1,("Reloading printers after SIGHUP\n"));
209 spoolss_reopen_logs(spoolss_child_id
);
212 static bool spoolss_setup_chld_hup_handler(struct tevent_context
*ev_ctx
,
213 struct messaging_context
*msg_ctx
,
214 struct pf_worker_data
*pf
)
216 struct tevent_signal
*se
;
218 se
= tevent_add_signal(ev_ctx
,
221 spoolss_chld_sig_hup_handler
,
224 DEBUG(1, ("failed to setup SIGHUP handler"));
231 static void parent_ping(struct messaging_context
*msg_ctx
,
234 struct server_id server_id
,
238 /* The fact we received this message is enough to let make the event
239 * loop if it was idle. spoolss_children_main will cycle through
240 * spoolss_next_client at least once. That function will take whatever
241 * action is necessary */
243 DEBUG(10, ("Got message that the parent changed status.\n"));
247 static bool spoolss_child_init(struct tevent_context
*ev_ctx
,
248 int child_id
, struct pf_worker_data
*pf
)
251 struct messaging_context
*msg_ctx
= global_messaging_context();
254 status
= reinit_after_fork(msg_ctx
, ev_ctx
, true, "spoolssd-child");
255 if (!NT_STATUS_IS_OK(status
)) {
256 DEBUG(0,("reinit_after_fork() failed\n"));
257 smb_panic("reinit_after_fork() failed");
260 spoolss_child_id
= child_id
;
261 spoolss_reopen_logs(child_id
);
263 ok
= spoolss_setup_chld_hup_handler(ev_ctx
, msg_ctx
, pf
);
268 if (!locking_init()) {
272 messaging_register(msg_ctx
, ev_ctx
,
273 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
274 messaging_register(msg_ctx
, ev_ctx
,
275 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
277 /* As soon as messaging is up check if pcap has been loaded already.
278 * If so then we probably missed a message and should load_printers()
279 * ourselves. If pcap has not been loaded yet, then ignore, we will get
280 * a message as soon as the bq process completes the reload. */
286 struct spoolss_children_data
{
287 struct tevent_context
*ev_ctx
;
288 struct messaging_context
*msg_ctx
;
289 struct dcesrv_context
*dce_ctx
;
290 struct pf_worker_data
*pf
;
292 struct pf_listen_fd
*listen_fds
;
295 static void spoolss_next_client(void *pvt
);
297 static int spoolss_children_main(struct tevent_context
*ev_ctx
,
298 struct messaging_context
*msg_ctx
,
299 struct pf_worker_data
*pf
,
302 struct pf_listen_fd
*listen_fds
,
305 struct spoolss_children_data
*data
;
308 struct dcesrv_context
*dce_ctx
= NULL
;
310 dce_ctx
= talloc_get_type_abort(private_data
, struct dcesrv_context
);
312 ok
= spoolss_child_init(ev_ctx
, child_id
, pf
);
317 data
= talloc(ev_ctx
, struct spoolss_children_data
);
322 data
->ev_ctx
= ev_ctx
;
323 data
->msg_ctx
= msg_ctx
;
324 data
->dce_ctx
= dce_ctx
;
325 data
->listen_fd_size
= listen_fd_size
;
326 data
->listen_fds
= listen_fds
;
328 /* loop until it is time to exit */
329 while (pf
->status
!= PF_WORKER_EXITING
) {
330 /* try to see if it is time to schedule the next client */
331 spoolss_next_client(data
);
333 ret
= tevent_loop_once(ev_ctx
);
335 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
336 ret
, strerror(errno
)));
337 pf
->status
= PF_WORKER_EXITING
;
344 static void spoolss_client_terminated(struct dcesrv_connection
*conn
,
347 struct spoolss_children_data
*data
;
349 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
351 pfh_client_terminated(data
->pf
);
353 spoolss_next_client(pvt
);
356 struct spoolss_new_client
{
357 struct spoolss_children_data
*data
;
360 static void spoolss_handle_client(struct tevent_req
*req
);
362 static void spoolss_next_client(void *pvt
)
364 struct tevent_req
*req
;
365 struct spoolss_children_data
*data
;
366 struct spoolss_new_client
*next
;
368 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
370 if (!pfh_child_allowed_to_accept(data
->pf
)) {
371 /* nothing to do for now we are already listening
372 * or we are not allowed to listen further */
376 next
= talloc_zero(data
, struct spoolss_new_client
);
378 DEBUG(1, ("Out of memory!?\n"));
383 req
= prefork_listen_send(next
, data
->ev_ctx
, data
->pf
,
384 data
->listen_fd_size
,
387 DEBUG(1, ("Failed to make listening request!?\n"));
391 tevent_req_set_callback(req
, spoolss_handle_client
, next
);
394 static void spoolss_handle_client(struct tevent_req
*req
)
396 struct spoolss_children_data
*data
;
397 struct spoolss_new_client
*client
;
398 const DATA_BLOB ping
= data_blob_null
;
401 struct tsocket_address
*srv_addr
= NULL
;
402 struct tsocket_address
*cli_addr
= NULL
;
403 void *listen_fd_data
= NULL
;
404 struct dcesrv_endpoint
*ep
= NULL
;
406 client
= tevent_req_callback_data(req
, struct spoolss_new_client
);
409 ret
= prefork_listen_recv(req
, data
, &sd
, &listen_fd_data
,
410 &srv_addr
, &cli_addr
);
412 /* this will free the request too */
416 DEBUG(6, ("No client connection was available after all!\n"));
420 ep
= talloc_get_type_abort(listen_fd_data
, struct dcesrv_endpoint
);
422 /* Warn parent that our status changed */
423 messaging_send(data
->msg_ctx
, parent_id
,
424 MSG_PREFORK_CHILD_EVENT
, &ping
);
426 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
427 (int)(data
->pf
->pid
)));
429 dcerpc_ncacn_accept(data
->ev_ctx
,
436 spoolss_client_terminated
,
440 /* ==== Main Process Functions ==== */
442 extern pid_t background_lpq_updater_pid
;
443 static char *bq_logfile
;
445 static void check_updater_child(struct tevent_context
*ev_ctx
,
446 struct messaging_context
*msg_ctx
)
451 if (background_lpq_updater_pid
== -1) {
455 pid
= waitpid(background_lpq_updater_pid
, &status
, WNOHANG
);
457 DEBUG(2, ("The background queue child died... Restarting!\n"));
458 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
459 background_lpq_updater_pid
= pid
;
463 static void child_ping(struct messaging_context
*msg_ctx
,
466 struct server_id server_id
,
469 struct tevent_context
*ev_ctx
;
471 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
473 DEBUG(10, ("Got message that a child changed status.\n"));
474 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
477 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
478 struct messaging_context
*msg_ctx
,
479 struct timeval current_time
);
480 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
481 struct tevent_timer
*te
,
482 struct timeval current_time
,
485 static void spoolssd_sigchld_handler(struct tevent_context
*ev_ctx
,
486 struct prefork_pool
*pfp
,
489 struct messaging_context
*msg_ctx
;
491 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
493 /* run pool management so we can fork/retire or increase
494 * the allowed connections per child based on load */
495 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
497 /* also check if the updater child is alive and well */
498 check_updater_child(ev_ctx
, msg_ctx
);
501 static bool spoolssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
502 struct messaging_context
*msg_ctx
)
506 /* add our oun sigchld callback */
507 prefork_set_sigchld_callback(spoolss_pool
,
508 spoolssd_sigchld_handler
, msg_ctx
);
510 ok
= spoolssd_schedule_check(ev_ctx
, msg_ctx
,
511 tevent_timeval_current());
515 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
516 struct messaging_context
*msg_ctx
,
517 struct timeval current_time
)
519 struct tevent_timer
*te
;
520 struct timeval next_event
;
522 /* check situation again in 10 seconds */
523 next_event
= tevent_timeval_current_ofs(10, 0);
525 /* TODO: check when the socket becomes readable, so that children
526 * are checked only when there is some activity ? */
527 te
= tevent_add_timer(ev_ctx
, spoolss_pool
, next_event
,
528 spoolssd_check_children
, msg_ctx
);
530 DEBUG(2, ("Failed to set up children monitoring!\n"));
537 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
538 struct tevent_timer
*te
,
539 struct timeval current_time
,
542 struct messaging_context
*msg_ctx
;
544 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
546 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
548 spoolssd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
551 static void print_queue_forward(struct messaging_context
*msg
,
554 struct server_id server_id
,
557 send_to_bgqd(msg
, msg_type
, data
->data
, data
->length
);
560 static char *get_bq_logfile(void)
562 const struct loadparm_substitution
*lp_sub
=
563 loadparm_s3_global_substitution();
564 char *lfile
= lp_logfile(talloc_tos(), lp_sub
);
567 if (lfile
== NULL
|| lfile
[0] == '\0') {
568 rc
= asprintf(&lfile
, "%s/log.%s.bq",
569 get_dyn_LOGFILEBASE(), DAEMON_NAME
);
571 rc
= asprintf(&lfile
, "%s.bq", lp_logfile(talloc_tos(), lp_sub
));
579 static NTSTATUS
spoolssd_create_sockets(struct tevent_context
*ev_ctx
,
580 struct messaging_context
*msg_ctx
,
581 struct dcesrv_context
*dce_ctx
,
583 struct pf_listen_fd
**plisten_fds
,
584 size_t *pnum_listen_fds
)
588 enum rpc_service_mode_e epm_mode
= rpc_epmapper_mode();
590 struct pf_listen_fd
*fds
= NULL
;
591 struct dcesrv_endpoint
*e
= dce_ctx
->endpoint_list
;
593 DBG_INFO("Initializing DCE/RPC connection endpoints\n");
595 status
= dcesrv_create_endpoint_list_pf_listen_fds(
596 ev_ctx
, msg_ctx
, dce_ctx
, e
, mem_ctx
, &num_fds
, &fds
);
597 if (!NT_STATUS_IS_OK(status
)) {
601 for (i
= 0; i
< num_fds
; i
++) {
602 rc
= listen(fds
[i
].fd
, pf_spoolss_cfg
.max_allowed_clients
);
604 char *ep_string
= NULL
;
607 ep_string
= dcerpc_binding_string(dce_ctx
,
609 DBG_ERR("Failed to listen on endpoint '%s': %s\n",
610 ep_string
, strerror(errno
));
611 status
= map_nt_error_from_unix(errno
);
612 TALLOC_FREE(ep_string
);
617 if (epm_mode
!= RPC_SERVICE_MODE_DISABLED
&&
618 (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
619 for (e
= dce_ctx
->endpoint_list
; e
; e
= e
->next
) {
620 struct dcesrv_if_list
*ifl
= NULL
;
621 for (ifl
= e
->interface_list
; ifl
; ifl
= ifl
->next
) {
622 status
= rpc_ep_register(ev_ctx
,
626 if (!NT_STATUS_IS_OK(status
)) {
627 DBG_ERR("Failed to register interface"
628 " in endpoint mapper: %s\n",
637 *pnum_listen_fds
= num_fds
;
639 status
= NT_STATUS_OK
;
644 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
645 struct messaging_context
*msg_ctx
,
646 struct dcesrv_context
*dce_ctx
)
650 struct pf_listen_fd
*listen_fds
= NULL
;
651 size_t listen_fds_size
= 0;
654 const struct dcesrv_endpoint_server
*ep_server
= NULL
;
655 const char *ep_servers
[] = { "winreg", "spoolss", NULL
};
657 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
660 * Block signals before forking child as it will have to
661 * set its own handlers. Child will re-enable SIGHUP as
662 * soon as the handlers are set up.
664 BlockSignals(true, SIGTERM
);
665 BlockSignals(true, SIGHUP
);
670 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
675 /* parent or error */
678 /* Re-enable SIGHUP before returnig */
679 BlockSignals(false, SIGTERM
);
680 BlockSignals(false, SIGHUP
);
684 status
= smbd_reinit_after_fork(msg_ctx
, ev_ctx
, true,
686 if (!NT_STATUS_IS_OK(status
)) {
687 DEBUG(0,("reinit_after_fork() failed\n"));
688 smb_panic("reinit_after_fork() failed");
691 /* save the parent process id so the children can use it later */
692 parent_id
= messaging_server_id(msg_ctx
);
694 spoolss_reopen_logs(0);
695 pfh_daemon_config(DAEMON_NAME
,
697 &default_pf_spoolss_cfg
);
699 spoolss_setup_sig_term_handler(ev_ctx
);
700 spoolss_setup_sig_hup_handler(ev_ctx
, msg_ctx
);
702 BlockSignals(false, SIGTERM
);
703 BlockSignals(false, SIGHUP
);
705 /* always start the backgroundqueue listner in spoolssd */
706 bq_logfile
= get_bq_logfile();
707 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
709 background_lpq_updater_pid
= pid
;
712 DBG_INFO("Registering DCE/RPC endpoint servers\n");
714 ep_server
= winreg_get_ep_server();
715 if (ep_server
== NULL
) {
716 DBG_ERR("Failed to get 'winreg' endpoint server\n");
720 status
= dcerpc_register_ep_server(ep_server
);
721 if (!NT_STATUS_IS_OK(status
) &&
722 !NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_COLLISION
)) {
723 DBG_ERR("Failed to register 'winreg' endpoint server: %s\n",
728 ep_server
= spoolss_get_ep_server();
729 if (ep_server
== NULL
) {
730 DBG_ERR("Failed to get 'spoolss' endpoint server\n");
734 status
= dcerpc_register_ep_server(ep_server
);
735 if (!NT_STATUS_IS_OK(status
)) {
736 DBG_ERR("Failed to register 'spoolss' endpoint server: %s\n",
741 DBG_INFO("Reinitializing DCE/RPC server context\n");
743 status
= dcesrv_reinit_context(dce_ctx
);
744 if (!NT_STATUS_IS_OK(status
)) {
745 DBG_ERR("Failed to reinit DCE/RPC context: %s\n",
750 DBG_INFO("Initializing DCE/RPC registered endpoint servers\n");
752 /* Init ep servers */
753 status
= dcesrv_init_ep_servers(dce_ctx
, ep_servers
);
754 if (!NT_STATUS_IS_OK(status
)) {
755 DBG_ERR("Failed to init DCE/RPC endpoint server: %s\n",
760 /* the listening fd must be created before the children are actually
762 status
= spoolssd_create_sockets(ev_ctx
,
768 if (!NT_STATUS_IS_OK(status
)) {
769 DBG_ERR("Failed to create sockets: %s\n",
774 /* start children before any more initialization is done */
775 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
777 listen_fds_size
, listen_fds
,
778 pf_spoolss_cfg
.min_children
,
779 pf_spoolss_cfg
.max_children
,
780 &spoolss_children_main
, dce_ctx
,
782 TALLOC_FREE(listen_fds
);
787 if (!locking_init()) {
791 messaging_register(msg_ctx
, ev_ctx
,
792 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
793 messaging_register(msg_ctx
, NULL
, MSG_PRINTER_UPDATE
,
794 print_queue_forward
);
795 messaging_register(msg_ctx
, ev_ctx
,
796 MSG_PREFORK_CHILD_EVENT
, child_ping
);
799 * As soon as messaging is up check if pcap has been loaded already.
800 * If pcap has not been loaded yet, then ignore, as we will reload on
801 * client enumeration anyway.
805 ok
= spoolssd_setup_children_monitor(ev_ctx
, msg_ctx
);
807 DEBUG(0, ("Failed to setup children monitoring!\n"));
811 DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
813 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
816 ret
= tevent_loop_wait(ev_ctx
);
818 /* should not be reached */
819 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
820 ret
, (ret
== 0) ? "out of events" : strerror(errno
)));