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