LCOV - code coverage report
Current view: top level - source3/printing - queue_process.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 98 194 50.5 %
Date: 2024-04-21 15:09:00 Functions: 10 13 76.9 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/Netbios implementation.
       3             :    Version 3.0
       4             :    printing backend routines
       5             :    Copyright (C) Andrew Tridgell 1992-2000
       6             :    Copyright (C) Jeremy Allison 2002
       7             :    Copyright (C) Simo Sorce 2011
       8             : 
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include "includes.h"
      24             : #include <spawn.h>
      25             : #include "smbd/globals.h"
      26             : #include "include/messages.h"
      27             : #include "lib/util/util_process.h"
      28             : #include "lib/util/sys_rw.h"
      29             : #include "printing.h"
      30             : #include "printing/pcap.h"
      31             : #include "printing/printer_list.h"
      32             : #include "printing/queue_process.h"
      33             : #include "locking/proto.h"
      34             : #include "locking/share_mode_lock.h"
      35             : #include "smbd/smbd.h"
      36             : #include "rpc_server/rpc_config.h"
      37             : #include "printing/load.h"
      38             : #include "rpc_server/spoolss/srv_spoolss_nt.h"
      39             : #include "auth.h"
      40             : #include "nt_printing.h"
      41             : #include "util_event.h"
      42             : #include "lib/global_contexts.h"
      43             : #include "lib/util/pidfile.h"
      44             : 
      45             : /**
      46             :  * @brief Purge stale printers and reload from pre-populated pcap cache.
      47             :  *
      48             :  * This function should normally only be called as a callback on a successful
      49             :  * pcap_cache_reload().
      50             :  *
      51             :  * This function can cause DELETION of printers and drivers from our registry,
      52             :  * so calling it on a failed pcap reload may REMOVE permanently all printers
      53             :  * and drivers.
      54             :  *
      55             :  * @param[in] ev        The event context.
      56             :  *
      57             :  * @param[in] msg_ctx   The messaging context.
      58             :  */
      59         120 : static void delete_and_reload_printers_full(struct tevent_context *ev,
      60             :                                             struct messaging_context *msg_ctx)
      61             : {
      62         120 :         struct auth_session_info *session_info = NULL;
      63         120 :         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
      64           0 :         const struct loadparm_substitution *lp_sub =
      65         120 :                 loadparm_s3_global_substitution();
      66           0 :         int n_services;
      67           0 :         int pnum;
      68           0 :         int snum;
      69           0 :         const char *pname;
      70           0 :         const char *sname;
      71           0 :         NTSTATUS status;
      72             : 
      73         120 :         n_services = lp_numservices();
      74         120 :         pnum = lp_servicenumber(PRINTERS_NAME);
      75             : 
      76         120 :         status = make_session_info_system(talloc_tos(), &session_info);
      77         120 :         if (!NT_STATUS_IS_OK(status)) {
      78           0 :                 DEBUG(3, ("reload_printers: "
      79             :                           "Could not create system session_info\n"));
      80             :                 /* can't remove stale printers before we
      81             :                  * are fully initialized */
      82           0 :                 return;
      83             :         }
      84             : 
      85             :         /*
      86             :          * Add default config for printers added to smb.conf file and remove
      87             :          * stale printers
      88             :          */
      89         120 :         for (snum = 0; snum < n_services; snum++) {
      90             :                 /* avoid removing PRINTERS_NAME */
      91           0 :                 if (snum == pnum) {
      92           0 :                         continue;
      93             :                 }
      94             : 
      95             :                 /* skip no-printer services */
      96           0 :                 if (!snum_is_shared_printer(snum)) {
      97           0 :                         continue;
      98             :                 }
      99             : 
     100           0 :                 sname = lp_const_servicename(snum);
     101           0 :                 pname = lp_printername(session_info, lp_sub, snum);
     102             : 
     103             :                 /* check printer, but avoid removing non-autoloaded printers */
     104           0 :                 if (lp_autoloaded(snum) &&
     105           0 :                     !printer_list_printername_exists(pname)) {
     106           0 :                         DEBUG(3, ("removing stale printer %s\n", pname));
     107             : 
     108           0 :                         if (is_printer_published(session_info, session_info,
     109             :                                                  msg_ctx,
     110             :                                                  NULL,
     111           0 :                                                  lp_servicename(session_info,
     112             :                                                                 lp_sub,
     113             :                                                                 snum),
     114             :                                                  &pinfo2)) {
     115           0 :                                 nt_printer_publish(session_info,
     116             :                                                    session_info,
     117             :                                                    msg_ctx,
     118             :                                                    pinfo2,
     119             :                                                    DSPRINT_UNPUBLISH);
     120           0 :                                 TALLOC_FREE(pinfo2);
     121             :                         }
     122           0 :                         nt_printer_remove(session_info, session_info, msg_ctx,
     123             :                                           pname);
     124             :                 } else {
     125           0 :                         DEBUG(8, ("Adding default registry entry for printer "
     126             :                                   "[%s], if it doesn't exist.\n", sname));
     127           0 :                         nt_printer_add(session_info, session_info, msg_ctx,
     128             :                                        sname);
     129             :                 }
     130             :         }
     131             : 
     132             :         /* finally, purge old snums */
     133         120 :         delete_and_reload_printers();
     134             : 
     135         120 :         TALLOC_FREE(session_info);
     136             : }
     137             : 
     138             : 
     139             : /****************************************************************************
     140             :  Notify smbds of new printcap data
     141             : **************************************************************************/
     142         120 : static void reload_pcap_change_notify(struct tevent_context *ev,
     143             :                                struct messaging_context *msg_ctx)
     144             : {
     145             :         /*
     146             :          * Reload the printers first in the background process so that
     147             :          * newly added printers get default values created in the registry.
     148             :          *
     149             :          * This will block the process for some time (~1 sec per printer), but
     150             :          * it doesn't block smbd's serving clients.
     151             :          */
     152         120 :         delete_and_reload_printers_full(ev, msg_ctx);
     153             : 
     154         120 :         messaging_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0);
     155         120 : }
     156             : 
     157             : struct bq_state {
     158             :         struct tevent_context *ev;
     159             :         struct messaging_context *msg;
     160             :         struct idle_event *housekeep;
     161             :         struct tevent_signal *sighup_handler;
     162             :         struct tevent_signal *sigchld_handler;
     163             : };
     164             : 
     165           2 : static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
     166             : {
     167           0 :         struct bq_state *state;
     168             : 
     169           2 :         state = talloc_get_type_abort(pvt, struct bq_state);
     170             : 
     171           2 :         DEBUG(5, ("print queue housekeeping\n"));
     172           2 :         pcap_cache_reload(state->ev, state->msg, reload_pcap_change_notify);
     173             : 
     174           2 :         return true;
     175             : }
     176             : 
     177         118 : static bool printing_subsystem_queue_tasks(struct bq_state *state)
     178             : {
     179         118 :         uint32_t housekeeping_period = lp_printcap_cache_time();
     180             : 
     181             :         /* cancel any existing housekeeping event */
     182         118 :         TALLOC_FREE(state->housekeep);
     183             : 
     184         118 :         if ((housekeeping_period == 0) || !lp_load_printers()) {
     185           0 :                 DEBUG(4, ("background print queue housekeeping disabled\n"));
     186           0 :                 return true;
     187             :         }
     188             : 
     189         118 :         state->housekeep = event_add_idle(
     190             :                 state->ev,
     191             :                 NULL,
     192             :                 tevent_timeval_set(housekeeping_period, 0),
     193             :                 "print_queue_housekeeping",
     194             :                 print_queue_housekeeping,
     195             :                 state);
     196         118 :         if (state->housekeep == NULL) {
     197           0 :                 DEBUG(0,("Could not add print_queue_housekeeping event\n"));
     198           0 :                 return false;
     199             :         }
     200             : 
     201         118 :         return true;
     202             : }
     203             : 
     204           0 : static void bq_reopen_logs(char *logfile)
     205             : {
     206           0 :         if (logfile) {
     207           0 :                 lp_set_logfile(logfile);
     208             :         }
     209           0 :         reopen_logs();
     210           0 : }
     211             : 
     212           0 : static void bq_sig_hup_handler(struct tevent_context *ev,
     213             :                                 struct tevent_signal *se,
     214             :                                 int signum,
     215             :                                 int count,
     216             :                                 void *siginfo,
     217             :                                 void *pvt)
     218             : {
     219           0 :         struct bq_state *state;
     220             : 
     221           0 :         state = talloc_get_type_abort(pvt, struct bq_state);
     222           0 :         change_to_root_user();
     223             : 
     224           0 :         DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
     225           0 :         pcap_cache_reload(state->ev, state->msg,
     226             :                           reload_pcap_change_notify);
     227           0 :         printing_subsystem_queue_tasks(state);
     228           0 :         bq_reopen_logs(NULL);
     229           0 : }
     230             : 
     231           0 : static void bq_sig_chld_handler(struct tevent_context *ev_ctx,
     232             :                                 struct tevent_signal *se,
     233             :                                 int signum, int count,
     234             :                                 void *siginfo, void *pvt)
     235             : {
     236           0 :         int status;
     237           0 :         pid_t pid;
     238             : 
     239           0 :         do {
     240           0 :                 do {
     241           0 :                         pid = waitpid(-1, &status, WNOHANG);
     242           0 :                 } while ((pid == -1) && (errno == EINTR));
     243             : 
     244           0 :                 if (WIFEXITED(status)) {
     245           0 :                         DBG_INFO("Bq child process %d terminated with %d\n",
     246             :                                  (int)pid,
     247             :                                  WEXITSTATUS(status));
     248             :                 } else {
     249           0 :                         DBG_NOTICE("Bq child process %d terminated abnormally\n",
     250             :                                    (int)pid);
     251             :                 }
     252           0 :         } while (pid > 0);
     253           0 : }
     254             : 
     255         106 : static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
     256             :                                 void *private_data,
     257             :                                 uint32_t msg_type,
     258             :                                 struct server_id server_id,
     259             :                                 DATA_BLOB *data)
     260             : {
     261           0 :         struct bq_state *state;
     262             : 
     263         106 :         state = talloc_get_type_abort(private_data, struct bq_state);
     264             : 
     265         106 :         DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
     266             :                   "updated. Reloading.\n"));
     267         106 :         change_to_root_user();
     268         106 :         pcap_cache_reload(state->ev, msg_ctx, reload_pcap_change_notify);
     269         106 :         printing_subsystem_queue_tasks(state);
     270         106 : }
     271             : 
     272          12 : static int bq_state_destructor(struct bq_state *s)
     273             : {
     274          12 :         struct messaging_context *msg_ctx = s->msg;
     275          12 :         TALLOC_FREE(s->sighup_handler);
     276          12 :         TALLOC_FREE(s->sigchld_handler);
     277          12 :         messaging_deregister(msg_ctx, MSG_PRINTER_DRVUPGRADE, NULL);
     278          12 :         messaging_deregister(msg_ctx, MSG_PRINTER_UPDATE, NULL);
     279          12 :         messaging_deregister(msg_ctx, MSG_SMB_CONF_UPDATED, s);
     280          12 :         return 0;
     281             : }
     282             : 
     283          12 : struct bq_state *register_printing_bq_handlers(
     284             :         TALLOC_CTX *mem_ctx,
     285             :         struct messaging_context *msg_ctx)
     286             : {
     287          12 :         struct bq_state *state = NULL;
     288           0 :         NTSTATUS status;
     289           0 :         bool ok;
     290             : 
     291          12 :         state = talloc_zero(mem_ctx, struct bq_state);
     292          12 :         if (state == NULL) {
     293           0 :                 return NULL;
     294             :         }
     295          12 :         state->ev = messaging_tevent_context(msg_ctx);
     296          12 :         state->msg = msg_ctx;
     297             : 
     298          12 :         status = messaging_register(
     299             :                 msg_ctx, state, MSG_SMB_CONF_UPDATED, bq_smb_conf_updated);
     300          12 :         if (!NT_STATUS_IS_OK(status)) {
     301           0 :                 goto fail;
     302             :         }
     303          12 :         status = messaging_register(
     304             :                 msg_ctx, NULL, MSG_PRINTER_UPDATE, print_queue_receive);
     305          12 :         if (!NT_STATUS_IS_OK(status)) {
     306           0 :                 goto fail_dereg_smb_conf_updated;
     307             :         }
     308          12 :         status = messaging_register(
     309             :                 msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE, do_drv_upgrade_printer);
     310          12 :         if (!NT_STATUS_IS_OK(status)) {
     311           0 :                 goto fail_dereg_printer_update;
     312             :         }
     313             : 
     314          12 :         state->sighup_handler = tevent_add_signal(
     315             :                 state->ev, state, SIGHUP, 0, bq_sig_hup_handler, state);
     316          12 :         if (state->sighup_handler == NULL) {
     317           0 :                 goto fail_dereg_printer_drvupgrade;
     318             :         }
     319          12 :         state->sigchld_handler = tevent_add_signal(
     320             :                 state->ev, state, SIGCHLD, 0, bq_sig_chld_handler, NULL);
     321          12 :         if (state->sigchld_handler == NULL) {
     322           0 :                 goto fail_free_handlers;
     323             :         }
     324             : 
     325             :         /* Initialize the printcap cache as soon as the daemon starts. */
     326          12 :         pcap_cache_reload(state->ev, state->msg, reload_pcap_change_notify);
     327             : 
     328          12 :         ok = printing_subsystem_queue_tasks(state);
     329          12 :         if (!ok) {
     330           0 :                 goto fail_free_handlers;
     331             :         }
     332             : 
     333          12 :         talloc_set_destructor(state, bq_state_destructor);
     334             : 
     335          12 :         return state;
     336             : 
     337           0 : fail_free_handlers:
     338           0 :         TALLOC_FREE(state->sighup_handler);
     339           0 :         TALLOC_FREE(state->sigchld_handler);
     340           0 : fail_dereg_printer_drvupgrade:
     341           0 :         messaging_deregister(msg_ctx, MSG_PRINTER_DRVUPGRADE, NULL);
     342           0 : fail_dereg_printer_update:
     343           0 :         messaging_deregister(msg_ctx, MSG_PRINTER_UPDATE, NULL);
     344           0 : fail_dereg_smb_conf_updated:
     345           0 :         messaging_deregister(msg_ctx, MSG_SMB_CONF_UPDATED, state);
     346           0 : fail:
     347           0 :         TALLOC_FREE(state);
     348           0 :         return NULL;
     349             : }
     350             : 
     351             : /****************************************************************************
     352             : main thread of the background lpq updater
     353             : ****************************************************************************/
     354          28 : pid_t start_background_queue(struct tevent_context *ev,
     355             :                              struct messaging_context *msg_ctx,
     356             :                              char *logfile)
     357             : {
     358           0 :         pid_t pid;
     359           0 :         int ret;
     360           0 :         ssize_t nread;
     361          28 :         char **argv = NULL;
     362           0 :         int ready_fds[2];
     363             : 
     364          28 :         DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
     365             : 
     366          28 :         ret = pipe(ready_fds);
     367          28 :         if (ret == -1) {
     368           0 :                 return -1;
     369             :         }
     370             : 
     371          28 :         argv = str_list_make_empty(talloc_tos());
     372          28 :         str_list_add_printf(
     373             :                 &argv, "%s/samba-bgqd", get_dyn_SAMBA_LIBEXECDIR());
     374          28 :         str_list_add_printf(
     375             :                 &argv, "--ready-signal-fd=%d", ready_fds[1]);
     376          28 :         str_list_add_printf(
     377             :                 &argv, "--parent-watch-fd=%d", 0);
     378          28 :         str_list_add_printf(
     379             :                 &argv, "--debuglevel=%d", debuglevel_get_class(DBGC_RPC_SRV));
     380          28 :         if (!is_default_dyn_CONFIGFILE()) {
     381          28 :                 str_list_add_printf(
     382             :                         &argv, "--configfile=%s", get_dyn_CONFIGFILE());
     383             :         }
     384          28 :         if (!is_default_dyn_LOGFILEBASE()) {
     385          16 :                 str_list_add_printf(
     386             :                         &argv, "--log-basename=%s", get_dyn_LOGFILEBASE());
     387             :         }
     388          28 :         str_list_add_printf(&argv, "-F");
     389          28 :         if (argv == NULL) {
     390           0 :                 goto nomem;
     391             :         }
     392             : 
     393          28 :         ret = posix_spawn(&pid, argv[0], NULL, NULL, argv, environ);
     394          28 :         if (ret == -1) {
     395           0 :                 goto fail;
     396             :         }
     397          28 :         TALLOC_FREE(argv);
     398             : 
     399          28 :         close(ready_fds[1]);
     400             : 
     401          28 :         nread = sys_read(ready_fds[0], &pid, sizeof(pid));
     402          28 :         close(ready_fds[0]);
     403          28 :         if (nread != sizeof(pid)) {
     404           0 :                 goto fail;
     405             :         }
     406             : 
     407          28 :         return pid;
     408             : 
     409           0 : nomem:
     410           0 :         errno = ENOMEM;
     411           0 : fail:
     412             :         {
     413           0 :                 int err = errno;
     414           0 :                 TALLOC_FREE(argv);
     415           0 :                 errno = err;
     416             :         }
     417             : 
     418           0 :         return -1;
     419             : }
     420             : 
     421             : 
     422             : /* Run before the parent forks */
     423          28 : bool printing_subsystem_init(struct tevent_context *ev_ctx,
     424             :                              struct messaging_context *msg_ctx,
     425             :                              struct dcesrv_context *dce_ctx)
     426             : {
     427          28 :         pid_t pid = -1;
     428             : 
     429          28 :         pid = start_background_queue(NULL, NULL, NULL);
     430          28 :         if (pid == -1) {
     431           0 :                 return false;
     432             :         }
     433          28 :         background_lpq_updater_pid = pid;
     434             : 
     435          28 :         if (!print_backend_init(msg_ctx)) {
     436           0 :                 return false;
     437             :         }
     438             : 
     439          28 :         return true;
     440             : }
     441             : 
     442        1329 : void send_to_bgqd(struct messaging_context *msg_ctx,
     443             :                   uint32_t msg_type,
     444             :                   const uint8_t *buf,
     445             :                   size_t buflen)
     446             : {
     447        1329 :         pid_t bgqd = pidfile_pid(lp_pid_directory(), "samba-bgqd");
     448             : 
     449        1329 :         if (bgqd == -1) {
     450           0 :                 return;
     451             :         }
     452        1329 :         messaging_send_buf(
     453             :                 msg_ctx, pid_to_procid(bgqd), msg_type, buf, buflen);
     454             : }

Generated by: LCOV version 1.14