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