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