[862] | 1 | /*
|
---|
| 2 | Unix SMB/Netbios implementation.
|
---|
| 3 | SPOOLSS Daemon
|
---|
| 4 | Copyright (C) Simo Sorce 2010
|
---|
| 5 |
|
---|
| 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.
|
---|
| 10 |
|
---|
| 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.
|
---|
| 15 |
|
---|
| 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/>.
|
---|
| 18 | */
|
---|
| 19 | #include "includes.h"
|
---|
| 20 | #include "serverid.h"
|
---|
| 21 | #include "smbd/smbd.h"
|
---|
| 22 |
|
---|
| 23 | #include "messages.h"
|
---|
| 24 | #include "include/printing.h"
|
---|
| 25 | #include "printing/nt_printing_migrate_internal.h"
|
---|
| 26 | #include "ntdomain.h"
|
---|
| 27 | #include "librpc/gen_ndr/srv_winreg.h"
|
---|
| 28 | #include "librpc/gen_ndr/srv_spoolss.h"
|
---|
| 29 | #include "rpc_server/rpc_server.h"
|
---|
| 30 | #include "rpc_server/rpc_ep_setup.h"
|
---|
| 31 | #include "rpc_server/spoolss/srv_spoolss_nt.h"
|
---|
| 32 |
|
---|
| 33 | #define SPOOLSS_PIPE_NAME "spoolss"
|
---|
| 34 | #define DAEMON_NAME "spoolssd"
|
---|
| 35 |
|
---|
| 36 | void start_spoolssd(struct tevent_context *ev_ctx,
|
---|
| 37 | struct messaging_context *msg_ctx);
|
---|
| 38 |
|
---|
| 39 | static void spoolss_reopen_logs(void)
|
---|
| 40 | {
|
---|
| 41 | char *lfile = lp_logfile();
|
---|
| 42 | int rc;
|
---|
| 43 |
|
---|
| 44 | if (lfile == NULL || lfile[0] == '\0') {
|
---|
| 45 | rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
|
---|
| 46 | if (rc > 0) {
|
---|
| 47 | lp_set_logfile(lfile);
|
---|
| 48 | SAFE_FREE(lfile);
|
---|
| 49 | }
|
---|
| 50 | } else {
|
---|
| 51 | if (strstr(lfile, DAEMON_NAME) == NULL) {
|
---|
| 52 | rc = asprintf(&lfile, "%s.%s", lp_logfile(), DAEMON_NAME);
|
---|
| 53 | if (rc > 0) {
|
---|
| 54 | lp_set_logfile(lfile);
|
---|
| 55 | SAFE_FREE(lfile);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | reopen_logs();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | static void smb_conf_updated(struct messaging_context *msg,
|
---|
| 64 | void *private_data,
|
---|
| 65 | uint32_t msg_type,
|
---|
| 66 | struct server_id server_id,
|
---|
| 67 | DATA_BLOB *data)
|
---|
| 68 | {
|
---|
| 69 | struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
|
---|
| 70 | struct tevent_context);
|
---|
| 71 |
|
---|
| 72 | DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
|
---|
| 73 | change_to_root_user();
|
---|
| 74 | spoolss_reopen_logs();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | static void spoolss_pcap_updated(struct messaging_context *msg,
|
---|
| 78 | void *private_data,
|
---|
| 79 | uint32_t msg_type,
|
---|
| 80 | struct server_id server_id,
|
---|
| 81 | DATA_BLOB *data)
|
---|
| 82 | {
|
---|
| 83 | struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
|
---|
| 84 | struct tevent_context);
|
---|
| 85 |
|
---|
| 86 | DEBUG(10, ("Got message saying pcap was updated. Reloading.\n"));
|
---|
| 87 | change_to_root_user();
|
---|
| 88 | reload_printers(ev_ctx, msg);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | static void spoolss_sig_term_handler(struct tevent_context *ev,
|
---|
| 92 | struct tevent_signal *se,
|
---|
| 93 | int signum,
|
---|
| 94 | int count,
|
---|
| 95 | void *siginfo,
|
---|
| 96 | void *private_data)
|
---|
| 97 | {
|
---|
| 98 | exit_server_cleanly("termination signal");
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
|
---|
| 102 | {
|
---|
| 103 | struct tevent_signal *se;
|
---|
| 104 |
|
---|
| 105 | se = tevent_add_signal(ev_ctx,
|
---|
| 106 | ev_ctx,
|
---|
| 107 | SIGTERM, 0,
|
---|
| 108 | spoolss_sig_term_handler,
|
---|
| 109 | NULL);
|
---|
| 110 | if (!se) {
|
---|
| 111 | exit_server("failed to setup SIGTERM handler");
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | static void spoolss_sig_hup_handler(struct tevent_context *ev,
|
---|
| 116 | struct tevent_signal *se,
|
---|
| 117 | int signum,
|
---|
| 118 | int count,
|
---|
| 119 | void *siginfo,
|
---|
| 120 | void *private_data)
|
---|
| 121 | {
|
---|
| 122 | struct messaging_context *msg_ctx = talloc_get_type_abort(private_data,
|
---|
| 123 | struct messaging_context);
|
---|
| 124 |
|
---|
| 125 | change_to_root_user();
|
---|
| 126 | DEBUG(1,("Reloading printers after SIGHUP\n"));
|
---|
| 127 | spoolss_reopen_logs();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
|
---|
| 131 | struct messaging_context *msg_ctx)
|
---|
| 132 | {
|
---|
| 133 | struct tevent_signal *se;
|
---|
| 134 |
|
---|
| 135 | se = tevent_add_signal(ev_ctx,
|
---|
| 136 | ev_ctx,
|
---|
| 137 | SIGHUP, 0,
|
---|
| 138 | spoolss_sig_hup_handler,
|
---|
| 139 | msg_ctx);
|
---|
| 140 | if (!se) {
|
---|
| 141 | exit_server("failed to setup SIGHUP handler");
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | static bool spoolss_init_cb(void *ptr)
|
---|
| 146 | {
|
---|
| 147 | struct messaging_context *msg_ctx = talloc_get_type_abort(
|
---|
| 148 | ptr, struct messaging_context);
|
---|
| 149 |
|
---|
| 150 | return nt_printing_tdb_migrate(msg_ctx);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | static bool spoolss_shutdown_cb(void *ptr)
|
---|
| 154 | {
|
---|
| 155 | srv_spoolss_cleanup();
|
---|
| 156 |
|
---|
| 157 | return true;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | void start_spoolssd(struct tevent_context *ev_ctx,
|
---|
| 161 | struct messaging_context *msg_ctx)
|
---|
| 162 | {
|
---|
| 163 | struct rpc_srv_callbacks spoolss_cb;
|
---|
| 164 | pid_t pid;
|
---|
| 165 | NTSTATUS status;
|
---|
| 166 | int ret;
|
---|
| 167 |
|
---|
| 168 | DEBUG(1, ("Forking SPOOLSS Daemon\n"));
|
---|
| 169 |
|
---|
| 170 | pid = sys_fork();
|
---|
| 171 |
|
---|
| 172 | if (pid == -1) {
|
---|
| 173 | DEBUG(0, ("Failed to fork SPOOLSS [%s], aborting ...\n",
|
---|
| 174 | strerror(errno)));
|
---|
| 175 | exit(1);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | if (pid) {
|
---|
| 179 | /* parent */
|
---|
| 180 | return;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /* child */
|
---|
| 184 | close_low_fds(false);
|
---|
| 185 |
|
---|
| 186 | status = reinit_after_fork(msg_ctx,
|
---|
| 187 | ev_ctx,
|
---|
| 188 | procid_self(), true);
|
---|
| 189 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 190 | DEBUG(0,("reinit_after_fork() failed\n"));
|
---|
| 191 | smb_panic("reinit_after_fork() failed");
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | spoolss_reopen_logs();
|
---|
| 195 |
|
---|
| 196 | spoolss_setup_sig_term_handler(ev_ctx);
|
---|
| 197 | spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
|
---|
| 198 |
|
---|
| 199 | if (!serverid_register(procid_self(),
|
---|
| 200 | FLAG_MSG_GENERAL|FLAG_MSG_SMBD
|
---|
| 201 | |FLAG_MSG_PRINT_GENERAL)) {
|
---|
| 202 | exit(1);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | if (!locking_init()) {
|
---|
| 206 | exit(1);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | messaging_register(msg_ctx, NULL,
|
---|
| 210 | MSG_PRINTER_UPDATE, print_queue_receive);
|
---|
| 211 | messaging_register(msg_ctx, ev_ctx,
|
---|
| 212 | MSG_SMB_CONF_UPDATED, smb_conf_updated);
|
---|
| 213 | messaging_register(msg_ctx, ev_ctx,
|
---|
| 214 | MSG_PRINTER_PCAP, spoolss_pcap_updated);
|
---|
| 215 |
|
---|
| 216 | /*
|
---|
| 217 | * Initialize spoolss with an init function to convert printers first.
|
---|
| 218 | * static_init_rpc will try to initialize the spoolss server too but you
|
---|
| 219 | * can't register it twice.
|
---|
| 220 | */
|
---|
| 221 | spoolss_cb.init = spoolss_init_cb;
|
---|
| 222 | spoolss_cb.shutdown = spoolss_shutdown_cb;
|
---|
| 223 | spoolss_cb.private_data = msg_ctx;
|
---|
| 224 |
|
---|
| 225 | status = rpc_winreg_init(NULL);
|
---|
| 226 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 227 | DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
|
---|
| 228 | nt_errstr(status)));
|
---|
| 229 | exit(1);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | status = rpc_spoolss_init(&spoolss_cb);
|
---|
| 233 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 234 | DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
|
---|
| 235 | nt_errstr(status)));
|
---|
| 236 | exit(1);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | if (!setup_named_pipe_socket(SPOOLSS_PIPE_NAME, ev_ctx)) {
|
---|
| 240 | exit(1);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | status = rpc_ep_setup_register(ev_ctx, msg_ctx, &ndr_table_spoolss, NULL, 0);
|
---|
| 244 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 245 | DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
|
---|
| 246 | nt_errstr(status)));
|
---|
| 247 | exit(1);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
|
---|
| 251 |
|
---|
| 252 | /* loop forever */
|
---|
| 253 | ret = tevent_loop_wait(ev_ctx);
|
---|
| 254 |
|
---|
| 255 | /* should not be reached */
|
---|
| 256 | DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
|
---|
| 257 | ret, (ret == 0) ? "out of events" : strerror(errno)));
|
---|
| 258 | exit(1);
|
---|
| 259 | }
|
---|