smbspool: Improve URI handling code
[sfrench/samba-autobuild/.git] / source3 / printing / spoolssd.c
1 /*
2    Unix SMB/Netbios implementation.
3    SPOOLSS Daemon
4    Copyright (C) Simo Sorce <idra@samba.org> 2010-2011
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 "smbd/smbd.h"
21
22 #include "messages.h"
23 #include "include/printing.h"
24 #include "printing/nt_printing_migrate_internal.h"
25 #include "printing/queue_process.h"
26 #include "printing/pcap.h"
27 #include "printing/load.h"
28 #include "ntdomain.h"
29 #include "librpc/gen_ndr/srv_winreg.h"
30 #include "librpc/gen_ndr/srv_spoolss.h"
31 #include "rpc_server/rpc_server.h"
32 #include "rpc_server/rpc_ep_register.h"
33 #include "rpc_server/rpc_config.h"
34 #include "rpc_server/spoolss/srv_spoolss_nt.h"
35 #include "librpc/rpc/dcerpc_ep.h"
36 #include "lib/server_prefork.h"
37 #include "lib/server_prefork_util.h"
38
39 #define SPOOLSS_PIPE_NAME "spoolss"
40 #define DAEMON_NAME "spoolssd"
41
42 static struct server_id parent_id;
43 static struct prefork_pool *spoolss_pool = NULL;
44 static int spoolss_child_id = 0;
45
46 static struct pf_daemon_config default_pf_spoolss_cfg = {
47         .prefork_status = PFH_INIT,
48         .min_children = 5,
49         .max_children = 25,
50         .spawn_rate = 5,
51         .max_allowed_clients = 100,
52         .child_min_life = 60 /* 1 minute minimum life time */
53 };
54 static struct pf_daemon_config pf_spoolss_cfg = { 0 };
55
56 pid_t start_spoolssd(struct tevent_context *ev_ctx,
57                      struct messaging_context *msg_ctx);
58
59 static void spoolss_reopen_logs(int child_id)
60 {
61         char *lfile = lp_logfile(talloc_tos());
62         char *ext;
63         int rc;
64
65         if (child_id) {
66                 rc = asprintf(&ext, "%s.%d", DAEMON_NAME, child_id);
67         } else {
68                 rc = asprintf(&ext, "%s", DAEMON_NAME);
69         }
70
71         if (rc == -1) {
72                 return;
73         }
74
75         rc = 0;
76         if (lfile == NULL || lfile[0] == '\0') {
77                 rc = asprintf(&lfile, "%s/log.%s",
78                               get_dyn_LOGFILEBASE(), ext);
79         } else {
80                 if (strstr(lfile, ext) == NULL) {
81                         if (child_id) {
82                                 rc = asprintf(&lfile, "%s.%d",
83                                               lp_logfile(talloc_tos()),
84                                               child_id);
85                         } else {
86                                 rc = asprintf(&lfile, "%s.%s",
87                                               lp_logfile(talloc_tos()),
88                                               ext);
89                         }
90                 }
91         }
92
93         if (rc > 0) {
94                 lp_set_logfile(lfile);
95                 SAFE_FREE(lfile);
96         }
97
98         SAFE_FREE(ext);
99
100         reopen_logs();
101 }
102
103 static void update_conf(struct tevent_context *ev,
104                         struct messaging_context *msg)
105 {
106         change_to_root_user();
107         lp_load_global(get_dyn_CONFIGFILE());
108         load_printers(ev, msg);
109
110         spoolss_reopen_logs(spoolss_child_id);
111         if (spoolss_child_id == 0) {
112                 pfh_daemon_config(DAEMON_NAME,
113                                   &pf_spoolss_cfg,
114                                   &default_pf_spoolss_cfg);
115                 pfh_manage_pool(ev, msg, &pf_spoolss_cfg, spoolss_pool);
116         }
117 }
118
119 static void smb_conf_updated(struct messaging_context *msg,
120                              void *private_data,
121                              uint32_t msg_type,
122                              struct server_id server_id,
123                              DATA_BLOB *data)
124 {
125         struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
126                                                              struct tevent_context);
127
128         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
129         update_conf(ev_ctx, msg);
130 }
131
132 static void spoolss_sig_term_handler(struct tevent_context *ev,
133                                      struct tevent_signal *se,
134                                      int signum,
135                                      int count,
136                                      void *siginfo,
137                                      void *private_data)
138 {
139         exit_server_cleanly("termination signal");
140 }
141
142 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
143 {
144         struct tevent_signal *se;
145
146         se = tevent_add_signal(ev_ctx,
147                                ev_ctx,
148                                SIGTERM, 0,
149                                spoolss_sig_term_handler,
150                                NULL);
151         if (!se) {
152                 exit_server("failed to setup SIGTERM handler");
153         }
154 }
155
156 static void spoolss_sig_hup_handler(struct tevent_context *ev,
157                                     struct tevent_signal *se,
158                                     int signum,
159                                     int count,
160                                     void *siginfo,
161                                     void *pvt)
162 {
163         struct messaging_context *msg_ctx;
164
165         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
166
167         DEBUG(1,("Reloading printers after SIGHUP\n"));
168         update_conf(ev, msg_ctx);
169
170         /* relay to all children */
171         if (spoolss_pool) {
172                 prefork_send_signal_to_all(spoolss_pool, SIGHUP);
173         }
174 }
175
176 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
177                                           struct messaging_context *msg_ctx)
178 {
179         struct tevent_signal *se;
180
181         se = tevent_add_signal(ev_ctx,
182                                ev_ctx,
183                                SIGHUP, 0,
184                                spoolss_sig_hup_handler,
185                                msg_ctx);
186         if (!se) {
187                 exit_server("failed to setup SIGHUP handler");
188         }
189 }
190
191 static bool spoolss_init_cb(void *ptr)
192 {
193         struct messaging_context *msg_ctx = talloc_get_type_abort(
194                 ptr, struct messaging_context);
195
196         return nt_printing_tdb_migrate(msg_ctx);
197 }
198
199 static bool spoolss_shutdown_cb(void *ptr)
200 {
201         srv_spoolss_cleanup();
202
203         return true;
204 }
205
206 /* Children */
207
208 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
209                                          struct tevent_signal *se,
210                                          int signum,
211                                          int count,
212                                          void *siginfo,
213                                          void *pvt)
214 {
215         struct messaging_context *msg_ctx;
216
217         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
218
219         change_to_root_user();
220         DEBUG(1,("Reloading printers after SIGHUP\n"));
221         load_printers(ev, msg_ctx);
222         spoolss_reopen_logs(spoolss_child_id);
223 }
224
225 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
226                                            struct messaging_context *msg_ctx,
227                                            struct pf_worker_data *pf)
228 {
229         struct tevent_signal *se;
230
231         se = tevent_add_signal(ev_ctx,
232                                ev_ctx,
233                                SIGHUP, 0,
234                                spoolss_chld_sig_hup_handler,
235                                msg_ctx);
236         if (!se) {
237                 DEBUG(1, ("failed to setup SIGHUP handler"));
238                 return false;
239         }
240
241         return true;
242 }
243
244 static void parent_ping(struct messaging_context *msg_ctx,
245                         void *private_data,
246                         uint32_t msg_type,
247                         struct server_id server_id,
248                         DATA_BLOB *data)
249 {
250
251         /* The fact we received this message is enough to let make the event
252          * loop if it was idle. spoolss_children_main will cycle through
253          * spoolss_next_client at least once. That function will take whatever
254          * action is necessary */
255
256         DEBUG(10, ("Got message that the parent changed status.\n"));
257         return;
258 }
259
260 static bool spoolss_child_init(struct tevent_context *ev_ctx,
261                                int child_id, struct pf_worker_data *pf)
262 {
263         NTSTATUS status;
264         struct rpc_srv_callbacks spoolss_cb;
265         struct messaging_context *msg_ctx = server_messaging_context();
266         bool ok;
267
268         status = reinit_after_fork(msg_ctx, ev_ctx, true, "spoolssd-child");
269         if (!NT_STATUS_IS_OK(status)) {
270                 DEBUG(0,("reinit_after_fork() failed\n"));
271                 smb_panic("reinit_after_fork() failed");
272         }
273
274         spoolss_child_id = child_id;
275         spoolss_reopen_logs(child_id);
276
277         ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
278         if (!ok) {
279                 return false;
280         }
281
282         if (!locking_init()) {
283                 return false;
284         }
285
286         messaging_register(msg_ctx, ev_ctx,
287                            MSG_SMB_CONF_UPDATED, smb_conf_updated);
288         messaging_register(msg_ctx, ev_ctx,
289                            MSG_PREFORK_PARENT_EVENT, parent_ping);
290
291         /* As soon as messaging is up check if pcap has been loaded already.
292          * If so then we probably missed a message and should load_printers()
293          * ourselves. If pcap has not been loaded yet, then ignore, we will get
294          * a message as soon as the bq process completes the reload. */
295         if (pcap_cache_loaded(NULL)) {
296                 load_printers(ev_ctx, msg_ctx);
297         }
298
299         /* try to reinit rpc queues */
300         spoolss_cb.init = spoolss_init_cb;
301         spoolss_cb.shutdown = spoolss_shutdown_cb;
302         spoolss_cb.private_data = msg_ctx;
303
304         status = rpc_winreg_init(NULL);
305         if (!NT_STATUS_IS_OK(status)) {
306                 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
307                           nt_errstr(status)));
308                 return false;
309         }
310
311         status = rpc_spoolss_init(&spoolss_cb);
312         if (!NT_STATUS_IS_OK(status)) {
313                 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
314                           nt_errstr(status)));
315                 return false;
316         }
317
318         return true;
319 }
320
321 struct spoolss_children_data {
322         struct tevent_context *ev_ctx;
323         struct messaging_context *msg_ctx;
324         struct pf_worker_data *pf;
325         int listen_fd_size;
326         int *listen_fds;
327 };
328
329 static void spoolss_next_client(void *pvt);
330
331 static int spoolss_children_main(struct tevent_context *ev_ctx,
332                                  struct messaging_context *msg_ctx,
333                                  struct pf_worker_data *pf,
334                                  int child_id,
335                                  int listen_fd_size,
336                                  int *listen_fds,
337                                  void *private_data)
338 {
339         struct spoolss_children_data *data;
340         bool ok;
341         int ret = 0;
342
343         ok = spoolss_child_init(ev_ctx, child_id, pf);
344         if (!ok) {
345                 return 1;
346         }
347
348         data = talloc(ev_ctx, struct spoolss_children_data);
349         if (!data) {
350                 return 1;
351         }
352         data->pf = pf;
353         data->ev_ctx = ev_ctx;
354         data->msg_ctx = msg_ctx;
355         data->listen_fd_size = listen_fd_size;
356         data->listen_fds = listen_fds;
357
358         /* loop until it is time to exit */
359         while (pf->status != PF_WORKER_EXITING) {
360                 /* try to see if it is time to schedule the next client */
361                 spoolss_next_client(data);
362
363                 ret = tevent_loop_once(ev_ctx);
364                 if (ret != 0) {
365                         DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
366                                   ret, strerror(errno)));
367                         pf->status = PF_WORKER_EXITING;
368                 }
369         }
370
371         return ret;
372 }
373
374 static void spoolss_client_terminated(void *pvt)
375 {
376         struct spoolss_children_data *data;
377
378         data = talloc_get_type_abort(pvt, struct spoolss_children_data);
379
380         pfh_client_terminated(data->pf);
381
382         spoolss_next_client(pvt);
383 }
384
385 struct spoolss_new_client {
386         struct spoolss_children_data *data;
387         struct tsocket_address *srv_addr;
388         struct tsocket_address *cli_addr;
389 };
390
391 static void spoolss_handle_client(struct tevent_req *req);
392
393 static void spoolss_next_client(void *pvt)
394 {
395         struct tevent_req *req;
396         struct spoolss_children_data *data;
397         struct spoolss_new_client *next;
398
399         data = talloc_get_type_abort(pvt, struct spoolss_children_data);
400
401         if (!pfh_child_allowed_to_accept(data->pf)) {
402                 /* nothing to do for now we are already listening
403                  * or we are not allowed to listen further */
404                 return;
405         }
406
407         next = talloc_zero(data, struct spoolss_new_client);
408         if (!next) {
409                 DEBUG(1, ("Out of memory!?\n"));
410                 return;
411         }
412         next->data = data;
413
414         req = prefork_listen_send(next, data->ev_ctx, data->pf,
415                                   data->listen_fd_size,
416                                   data->listen_fds);
417         if (!req) {
418                 DEBUG(1, ("Failed to make listening request!?\n"));
419                 talloc_free(next);
420                 return;
421         }
422         tevent_req_set_callback(req, spoolss_handle_client, next);
423 }
424
425 static void spoolss_handle_client(struct tevent_req *req)
426 {
427         struct spoolss_children_data *data;
428         struct spoolss_new_client *client;
429         const DATA_BLOB ping = data_blob_null;
430         int ret;
431         int sd;
432
433         client = tevent_req_callback_data(req, struct spoolss_new_client);
434         data = client->data;
435
436         ret = prefork_listen_recv(req, client, &sd,
437                                   &client->srv_addr, &client->cli_addr);
438
439         /* this will free the request too */
440         talloc_free(client);
441
442         if (ret != 0) {
443                 DEBUG(6, ("No client connection was available after all!\n"));
444                 return;
445         }
446
447         /* Warn parent that our status changed */
448         messaging_send(data->msg_ctx, parent_id,
449                         MSG_PREFORK_CHILD_EVENT, &ping);
450
451         DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
452                   (int)(data->pf->pid)));
453
454         named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
455                                    SPOOLSS_PIPE_NAME, sd,
456                                    spoolss_client_terminated, data);
457 }
458
459 /* ==== Main Process Functions ==== */
460
461 extern pid_t background_lpq_updater_pid;
462 static char *bq_logfile;
463
464 static void check_updater_child(struct tevent_context *ev_ctx,
465                                 struct messaging_context *msg_ctx)
466 {
467         int status;
468         pid_t pid;
469
470         if (background_lpq_updater_pid == -1) {
471                 return;
472         }
473
474         pid = waitpid(background_lpq_updater_pid, &status, WNOHANG);
475         if (pid > 0) {
476                 DEBUG(2, ("The background queue child died... Restarting!\n"));
477                 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
478                 background_lpq_updater_pid = pid;
479         }
480 }
481
482 static void child_ping(struct messaging_context *msg_ctx,
483                         void *private_data,
484                         uint32_t msg_type,
485                         struct server_id server_id,
486                         DATA_BLOB *data)
487 {
488         struct tevent_context *ev_ctx;
489
490         ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
491
492         DEBUG(10, ("Got message that a child changed status.\n"));
493         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
494 }
495
496 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
497                                     struct messaging_context *msg_ctx,
498                                     struct timeval current_time);
499 static void spoolssd_check_children(struct tevent_context *ev_ctx,
500                                     struct tevent_timer *te,
501                                     struct timeval current_time,
502                                     void *pvt);
503
504 static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
505                                      struct prefork_pool *pfp,
506                                      void *pvt)
507 {
508         struct messaging_context *msg_ctx;
509
510         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
511
512         /* run pool management so we can fork/retire or increase
513          * the allowed connections per child based on load */
514         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
515
516         /* also check if the updater child is alive and well */
517         check_updater_child(ev_ctx, msg_ctx);
518 }
519
520 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
521                                             struct messaging_context *msg_ctx)
522 {
523         bool ok;
524
525         /* add our oun sigchld callback */
526         prefork_set_sigchld_callback(spoolss_pool,
527                                      spoolssd_sigchld_handler, msg_ctx);
528
529         ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
530                                      tevent_timeval_current());
531         return ok;
532 }
533
534 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
535                                     struct messaging_context *msg_ctx,
536                                     struct timeval current_time)
537 {
538         struct tevent_timer *te;
539         struct timeval next_event;
540
541         /* check situation again in 10 seconds */
542         next_event = tevent_timeval_current_ofs(10, 0);
543
544         /* TODO: check when the socket becomes readable, so that children
545          * are checked only when there is some activity ? */
546         te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
547                                 spoolssd_check_children, msg_ctx);
548         if (!te) {
549                 DEBUG(2, ("Failed to set up children monitoring!\n"));
550                 return false;
551         }
552
553         return true;
554 }
555
556 static void spoolssd_check_children(struct tevent_context *ev_ctx,
557                                     struct tevent_timer *te,
558                                     struct timeval current_time,
559                                     void *pvt)
560 {
561         struct messaging_context *msg_ctx;
562
563         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
564
565         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
566
567         spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
568 }
569
570 static void print_queue_forward(struct messaging_context *msg,
571                                 void *private_data,
572                                 uint32_t msg_type,
573                                 struct server_id server_id,
574                                 DATA_BLOB *data)
575 {
576         messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
577                            MSG_PRINTER_UPDATE, data->data, data->length);
578 }
579
580 static char *get_bq_logfile(void)
581 {
582         char *lfile = lp_logfile(talloc_tos());
583         int rc;
584
585         if (lfile == NULL || lfile[0] == '\0') {
586                 rc = asprintf(&lfile, "%s/log.%s.bq",
587                                         get_dyn_LOGFILEBASE(), DAEMON_NAME);
588         } else {
589                 rc = asprintf(&lfile, "%s.bq", lp_logfile(talloc_tos()));
590         }
591         if (rc == -1) {
592                 lfile = NULL;
593         }
594         return lfile;
595 }
596
597 pid_t start_spoolssd(struct tevent_context *ev_ctx,
598                     struct messaging_context *msg_ctx)
599 {
600         enum rpc_service_mode_e epm_mode = rpc_epmapper_mode();
601         struct rpc_srv_callbacks spoolss_cb;
602         struct dcerpc_binding_vector *v;
603         TALLOC_CTX *mem_ctx;
604         pid_t pid;
605         NTSTATUS status;
606         int listen_fd;
607         int ret;
608         bool ok;
609
610         DEBUG(1, ("Forking SPOOLSS Daemon\n"));
611
612         /*
613          * Block signals before forking child as it will have to
614          * set its own handlers. Child will re-enable SIGHUP as
615          * soon as the handlers are set up.
616          */
617         BlockSignals(true, SIGTERM);
618         BlockSignals(true, SIGHUP);
619
620         pid = fork();
621
622         if (pid == -1) {
623                 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
624                            strerror(errno)));
625         }
626
627         /* parent or error */
628         if (pid != 0) {
629
630                 /* Re-enable SIGHUP before returnig */
631                 BlockSignals(false, SIGTERM);
632                 BlockSignals(false, SIGHUP);
633                 return pid;
634         }
635
636         status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true,
637                                         "spoolssd-master");
638         if (!NT_STATUS_IS_OK(status)) {
639                 DEBUG(0,("reinit_after_fork() failed\n"));
640                 smb_panic("reinit_after_fork() failed");
641         }
642
643         /* save the parent process id so the children can use it later */
644         parent_id = messaging_server_id(msg_ctx);
645
646         spoolss_reopen_logs(0);
647         pfh_daemon_config(DAEMON_NAME,
648                           &pf_spoolss_cfg,
649                           &default_pf_spoolss_cfg);
650
651         spoolss_setup_sig_term_handler(ev_ctx);
652         spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
653
654         BlockSignals(false, SIGTERM);
655         BlockSignals(false, SIGHUP);
656
657         /* always start the backgroundqueue listner in spoolssd */
658         bq_logfile = get_bq_logfile();
659         pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
660         if (pid > 0) {
661                 background_lpq_updater_pid = pid;
662         }
663
664         /* the listening fd must be created before the children are actually
665          * forked out. */
666         listen_fd = create_named_pipe_socket(SPOOLSS_PIPE_NAME);
667         if (listen_fd == -1) {
668                 exit(1);
669         }
670
671         ret = listen(listen_fd, pf_spoolss_cfg.max_allowed_clients);
672         if (ret == -1) {
673                 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
674                           strerror(errno)));
675                 exit(1);
676         }
677
678         /* start children before any more initialization is done */
679         ok = prefork_create_pool(ev_ctx, /* mem_ctx */
680                                  ev_ctx, msg_ctx,
681                                  1, &listen_fd,
682                                  pf_spoolss_cfg.min_children,
683                                  pf_spoolss_cfg.max_children,
684                                  &spoolss_children_main, NULL,
685                                  &spoolss_pool);
686         if (!ok) {
687                 exit(1);
688         }
689
690         if (!locking_init()) {
691                 exit(1);
692         }
693
694         messaging_register(msg_ctx, ev_ctx,
695                            MSG_SMB_CONF_UPDATED, smb_conf_updated);
696         messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
697                            print_queue_forward);
698         messaging_register(msg_ctx, ev_ctx,
699                            MSG_PREFORK_CHILD_EVENT, child_ping);
700
701         /*
702          * As soon as messaging is up check if pcap has been loaded already.
703          * If pcap has not been loaded yet, then ignore, as we will reload on
704          * client enumeration anyway.
705          */
706         if (pcap_cache_loaded(NULL)) {
707                 load_printers(ev_ctx, msg_ctx);
708         }
709
710         mem_ctx = talloc_new(NULL);
711         if (mem_ctx == NULL) {
712                 exit(1);
713         }
714
715         /*
716          * Initialize spoolss with an init function to convert printers first.
717          * static_init_rpc will try to initialize the spoolss server too but you
718          * can't register it twice.
719          */
720         spoolss_cb.init = spoolss_init_cb;
721         spoolss_cb.shutdown = spoolss_shutdown_cb;
722         spoolss_cb.private_data = msg_ctx;
723
724         status = rpc_winreg_init(NULL);
725         if (!NT_STATUS_IS_OK(status)) {
726                 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
727                           nt_errstr(status)));
728                 exit(1);
729         }
730
731         status = rpc_spoolss_init(&spoolss_cb);
732         if (!NT_STATUS_IS_OK(status)) {
733                 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
734                           nt_errstr(status)));
735                 exit(1);
736         }
737
738         if (epm_mode != RPC_SERVICE_MODE_DISABLED &&
739             (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
740                 status = dcerpc_binding_vector_new(mem_ctx, &v);
741                 if (!NT_STATUS_IS_OK(status)) {
742                         DEBUG(0, ("Failed to create binding vector (%s)\n",
743                                   nt_errstr(status)));
744                         exit(1);
745                 }
746
747                 status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
748                 if (!NT_STATUS_IS_OK(status)) {
749                         DEBUG(0, ("Failed to add np to binding vector (%s)\n",
750                                   nt_errstr(status)));
751                         exit(1);
752                 }
753
754                 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
755                 if (!NT_STATUS_IS_OK(status)) {
756                         DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
757                                   nt_errstr(status)));
758                         exit(1);
759                 }
760         }
761
762         talloc_free(mem_ctx);
763
764         ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
765         if (!ok) {
766                 DEBUG(0, ("Failed to setup children monitoring!\n"));
767                 exit(1);
768         }
769
770         DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
771
772         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
773
774         /* loop forever */
775         ret = tevent_loop_wait(ev_ctx);
776
777         /* should not be reached */
778         DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
779                  ret, (ret == 0) ? "out of events" : strerror(errno)));
780         exit(1);
781 }