s3:rpc_server: Unify RPC client disconnect and termination functions
[metze/samba/wip.git] / source3 / rpc_server / lsasd.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  LSA service daemon
5  *
6  *  Copyright (c) 2011      Andreas Schneider <asn@samba.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "messages.h"
24 #include "ntdomain.h"
25 #include "passdb.h"
26
27 #include "lib/id_cache.h"
28
29 #include "../lib/tsocket/tsocket.h"
30 #include "lib/server_prefork.h"
31 #include "lib/server_prefork_util.h"
32 #include "librpc/rpc/dcerpc_ep.h"
33
34 #include "rpc_server/rpc_server.h"
35 #include "rpc_server/rpc_ep_register.h"
36 #include "rpc_server/rpc_sock_helper.h"
37
38 #include "librpc/gen_ndr/srv_lsa.h"
39 #include "librpc/gen_ndr/srv_samr.h"
40 #include "librpc/gen_ndr/srv_netlogon.h"
41 #include "rpc_server/lsasd.h"
42
43 #undef DBGC_CLASS
44 #define DBGC_CLASS DBGC_RPC_SRV
45
46 #define DAEMON_NAME "lsasd"
47 #define LSASD_MAX_SOCKETS 64
48
49 static struct server_id parent_id;
50 static struct prefork_pool *lsasd_pool = NULL;
51 static int lsasd_child_id = 0;
52
53 static struct pf_daemon_config default_pf_lsasd_cfg = {
54         .prefork_status = PFH_INIT,
55         .min_children = 5,
56         .max_children = 25,
57         .spawn_rate = 5,
58         .max_allowed_clients = 100,
59         .child_min_life = 60 /* 1 minute minimum life time */
60 };
61 static struct pf_daemon_config pf_lsasd_cfg = { 0 };
62
63 static void lsasd_reopen_logs(int child_id)
64 {
65         char *lfile = lp_logfile(talloc_tos());
66         char *extension;
67         int rc;
68
69         if (child_id) {
70                 rc = asprintf(&extension, "%s.%d", DAEMON_NAME, child_id);
71         } else {
72                 rc = asprintf(&extension, "%s", DAEMON_NAME);
73         }
74         if (rc == -1) {
75                 return;
76         }
77
78         rc = 0;
79         if (lfile == NULL || lfile[0] == '\0') {
80                 rc = asprintf(&lfile, "%s/log.%s",
81                               get_dyn_LOGFILEBASE(), extension);
82         } else {
83                 if (strstr(lfile, extension) == NULL) {
84                         if (child_id) {
85                                 rc = asprintf(&lfile, "%s.%d",
86                                                 lp_logfile(talloc_tos()),
87                                                 child_id);
88                         } else {
89                                 rc = asprintf(&lfile, "%s.%s",
90                                                 lp_logfile(talloc_tos()),
91                                                 extension);
92                         }
93                 }
94         }
95
96         if (rc > 0) {
97                 lp_set_logfile(lfile);
98                 SAFE_FREE(lfile);
99         }
100
101         SAFE_FREE(extension);
102
103         reopen_logs();
104 }
105
106 static void lsasd_smb_conf_updated(struct messaging_context *msg,
107                                   void *private_data,
108                                   uint32_t msg_type,
109                                   struct server_id server_id,
110                                   DATA_BLOB *data)
111 {
112         struct tevent_context *ev_ctx;
113
114         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
115         ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
116
117         change_to_root_user();
118         lp_load_global(get_dyn_CONFIGFILE());
119
120         lsasd_reopen_logs(lsasd_child_id);
121         if (lsasd_child_id == 0) {
122                 pfh_daemon_config(DAEMON_NAME,
123                                   &pf_lsasd_cfg,
124                                   &default_pf_lsasd_cfg);
125                 pfh_manage_pool(ev_ctx, msg, &pf_lsasd_cfg, lsasd_pool);
126         }
127 }
128
129 static void lsasd_sig_term_handler(struct tevent_context *ev,
130                                   struct tevent_signal *se,
131                                   int signum,
132                                   int count,
133                                   void *siginfo,
134                                   void *private_data)
135 {
136         rpc_netlogon_shutdown();
137         rpc_samr_shutdown();
138         rpc_lsarpc_shutdown();
139
140         DEBUG(0, ("termination signal\n"));
141         exit(0);
142 }
143
144 static void lsasd_setup_sig_term_handler(struct tevent_context *ev_ctx)
145 {
146         struct tevent_signal *se;
147
148         se = tevent_add_signal(ev_ctx,
149                                ev_ctx,
150                                SIGTERM, 0,
151                                lsasd_sig_term_handler,
152                                NULL);
153         if (!se) {
154                 DEBUG(0, ("failed to setup SIGTERM handler\n"));
155                 exit(1);
156         }
157 }
158
159 static void lsasd_sig_hup_handler(struct tevent_context *ev,
160                                     struct tevent_signal *se,
161                                     int signum,
162                                     int count,
163                                     void *siginfo,
164                                     void *pvt)
165 {
166
167         change_to_root_user();
168         lp_load_global(get_dyn_CONFIGFILE());
169
170         lsasd_reopen_logs(lsasd_child_id);
171         pfh_daemon_config(DAEMON_NAME,
172                           &pf_lsasd_cfg,
173                           &default_pf_lsasd_cfg);
174
175         /* relay to all children */
176         prefork_send_signal_to_all(lsasd_pool, SIGHUP);
177 }
178
179 static void lsasd_setup_sig_hup_handler(struct tevent_context *ev_ctx)
180 {
181         struct tevent_signal *se;
182
183         se = tevent_add_signal(ev_ctx,
184                                ev_ctx,
185                                SIGHUP, 0,
186                                lsasd_sig_hup_handler,
187                                NULL);
188         if (!se) {
189                 DEBUG(0, ("failed to setup SIGHUP handler\n"));
190                 exit(1);
191         }
192 }
193
194 /**********************************************************
195  * Children
196  **********************************************************/
197
198 static void lsasd_chld_sig_hup_handler(struct tevent_context *ev,
199                                          struct tevent_signal *se,
200                                          int signum,
201                                          int count,
202                                          void *siginfo,
203                                          void *pvt)
204 {
205         change_to_root_user();
206         lsasd_reopen_logs(lsasd_child_id);
207 }
208
209 static bool lsasd_setup_chld_hup_handler(struct tevent_context *ev_ctx)
210 {
211         struct tevent_signal *se;
212
213         se = tevent_add_signal(ev_ctx,
214                                ev_ctx,
215                                SIGHUP, 0,
216                                lsasd_chld_sig_hup_handler,
217                                NULL);
218         if (!se) {
219                 DEBUG(1, ("failed to setup SIGHUP handler"));
220                 return false;
221         }
222
223         return true;
224 }
225
226 static void parent_ping(struct messaging_context *msg_ctx,
227                         void *private_data,
228                         uint32_t msg_type,
229                         struct server_id server_id,
230                         DATA_BLOB *data)
231 {
232
233         /* The fact we received this message is enough to let make the event
234          * loop if it was idle. lsasd_children_main will cycle through
235          * lsasd_next_client at least once. That function will take whatever
236          * action is necessary */
237
238         DEBUG(10, ("Got message that the parent changed status.\n"));
239         return;
240 }
241
242 static bool lsasd_child_init(struct tevent_context *ev_ctx,
243                              int child_id,
244                              struct pf_worker_data *pf)
245 {
246         NTSTATUS status;
247         struct messaging_context *msg_ctx = global_messaging_context();
248         bool ok;
249
250         status = reinit_after_fork(msg_ctx, ev_ctx,
251                                    true, "lsasd-child");
252         if (!NT_STATUS_IS_OK(status)) {
253                 DEBUG(0,("reinit_after_fork() failed\n"));
254                 smb_panic("reinit_after_fork() failed");
255         }
256         initialize_password_db(true, ev_ctx);
257
258         lsasd_child_id = child_id;
259         lsasd_reopen_logs(child_id);
260
261         ok = lsasd_setup_chld_hup_handler(ev_ctx);
262         if (!ok) {
263                 return false;
264         }
265
266         messaging_register(msg_ctx, ev_ctx,
267                            MSG_SMB_CONF_UPDATED, lsasd_smb_conf_updated);
268         messaging_register(msg_ctx, ev_ctx,
269                            MSG_PREFORK_PARENT_EVENT, parent_ping);
270         id_cache_register_msgs(msg_ctx);
271
272         status = rpc_lsarpc_init(NULL);
273         if (!NT_STATUS_IS_OK(status)) {
274                 DEBUG(0, ("Failed to register lsarpc rpc interface! (%s)\n",
275                           nt_errstr(status)));
276                 return false;
277         }
278
279         status = rpc_samr_init(NULL);
280         if (!NT_STATUS_IS_OK(status)) {
281                 DEBUG(0, ("Failed to register samr rpc interface! (%s)\n",
282                           nt_errstr(status)));
283                 return false;
284         }
285
286         status = rpc_netlogon_init(NULL);
287         if (!NT_STATUS_IS_OK(status)) {
288                 DEBUG(0, ("Failed to register netlogon rpc interface! (%s)\n",
289                           nt_errstr(status)));
290                 return false;
291         }
292
293         return true;
294 }
295
296 struct lsasd_children_data {
297         struct tevent_context *ev_ctx;
298         struct messaging_context *msg_ctx;
299         struct pf_worker_data *pf;
300         int listen_fd_size;
301         struct pf_listen_fd *listen_fds;
302 };
303
304 static void lsasd_next_client(void *pvt);
305
306 static int lsasd_children_main(struct tevent_context *ev_ctx,
307                                struct messaging_context *msg_ctx,
308                                struct pf_worker_data *pf,
309                                int child_id,
310                                int listen_fd_size,
311                                struct pf_listen_fd *listen_fds,
312                                void *private_data)
313 {
314         struct lsasd_children_data *data;
315         bool ok;
316         int ret = 0;
317
318         ok = lsasd_child_init(ev_ctx, child_id, pf);
319         if (!ok) {
320                 return 1;
321         }
322
323         data = talloc(ev_ctx, struct lsasd_children_data);
324         if (!data) {
325                 return 1;
326         }
327         data->pf = pf;
328         data->ev_ctx = ev_ctx;
329         data->msg_ctx = msg_ctx;
330         data->listen_fd_size = listen_fd_size;
331         data->listen_fds = listen_fds;
332
333         /* loop until it is time to exit */
334         while (pf->status != PF_WORKER_EXITING) {
335                 /* try to see if it is time to schedule the next client */
336                 lsasd_next_client(data);
337
338                 ret = tevent_loop_once(ev_ctx);
339                 if (ret != 0) {
340                         DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
341                                   ret, strerror(errno)));
342                         pf->status = PF_WORKER_EXITING;
343                 }
344         }
345
346         return ret;
347 }
348
349 static void lsasd_client_terminated(struct pipes_struct *p, void *pvt)
350 {
351         struct lsasd_children_data *data;
352
353         data = talloc_get_type_abort(pvt, struct lsasd_children_data);
354
355         pfh_client_terminated(data->pf);
356
357         lsasd_next_client(pvt);
358 }
359
360 struct lsasd_new_client {
361         struct lsasd_children_data *data;
362 };
363
364 static void lsasd_handle_client(struct tevent_req *req);
365
366 static void lsasd_next_client(void *pvt)
367 {
368         struct tevent_req *req;
369         struct lsasd_children_data *data;
370         struct lsasd_new_client *next;
371
372         data = talloc_get_type_abort(pvt, struct lsasd_children_data);
373
374         if (!pfh_child_allowed_to_accept(data->pf)) {
375                 /* nothing to do for now we are already listening
376                  * or we are not allowed to listen further */
377                 return;
378         }
379
380         next = talloc_zero(data, struct lsasd_new_client);
381         if (!next) {
382                 DEBUG(1, ("Out of memory!?\n"));
383                 return;
384         }
385         next->data = data;
386
387         req = prefork_listen_send(next,
388                                   data->ev_ctx,
389                                   data->pf,
390                                   data->listen_fd_size,
391                                   data->listen_fds);
392         if (!req) {
393                 DEBUG(1, ("Failed to make listening request!?\n"));
394                 talloc_free(next);
395                 return;
396         }
397         tevent_req_set_callback(req, lsasd_handle_client, next);
398 }
399
400 static void lsasd_handle_client(struct tevent_req *req)
401 {
402         struct lsasd_children_data *data;
403         struct lsasd_new_client *client;
404         const DATA_BLOB ping = data_blob_null;
405         int rc;
406         int sd;
407         TALLOC_CTX *tmp_ctx;
408         struct tsocket_address *srv_addr;
409         struct tsocket_address *cli_addr;
410
411         client = tevent_req_callback_data(req, struct lsasd_new_client);
412         data = client->data;
413
414         tmp_ctx = talloc_stackframe();
415         if (tmp_ctx == NULL) {
416                 DEBUG(1, ("Failed to allocate stackframe!\n"));
417                 return;
418         }
419
420         rc = prefork_listen_recv(req,
421                                  tmp_ctx,
422                                  &sd,
423                                  NULL,
424                                  &srv_addr,
425                                  &cli_addr);
426
427         /* this will free the request too */
428         talloc_free(client);
429
430         if (rc != 0) {
431                 DEBUG(6, ("No client connection was available after all!\n"));
432                 goto done;
433         }
434
435         /* Warn parent that our status changed */
436         messaging_send(data->msg_ctx, parent_id,
437                         MSG_PREFORK_CHILD_EVENT, &ping);
438
439         DEBUG(2, ("LSASD preforked child %d got client connection!\n",
440                   (int)(data->pf->pid)));
441
442         if (tsocket_address_is_inet(srv_addr, "ip")) {
443                 DEBUG(3, ("Got a tcpip client connection from %s on interface %s\n",
444                            tsocket_address_string(cli_addr, tmp_ctx),
445                            tsocket_address_string(srv_addr, tmp_ctx)));
446
447                 dcerpc_ncacn_accept(data->ev_ctx,
448                                     data->msg_ctx,
449                                     NCACN_IP_TCP,
450                                     "IP",
451                                     cli_addr,
452                                     srv_addr,
453                                     sd,
454                                     lsasd_client_terminated,
455                                     data);
456         } else if (tsocket_address_is_unix(srv_addr)) {
457                 const char *p;
458                 const char *b;
459
460                 p = tsocket_address_unix_path(srv_addr, tmp_ctx);
461                 if (p == NULL) {
462                         talloc_free(tmp_ctx);
463                         return;
464                 }
465
466                 b = strrchr(p, '/');
467                 if (b != NULL) {
468                         b++;
469                 } else {
470                         b = p;
471                 }
472
473                 if (strstr(p, "/np/")) {
474                         dcerpc_ncacn_accept(data->ev_ctx,
475                                             data->msg_ctx,
476                                             NCACN_NP,
477                                             b,
478                                             NULL,  /* remote client address */
479                                             NULL,  /* local server address */
480                                             sd,
481                                             lsasd_client_terminated,
482                                             data);
483
484                 } else {
485                         dcerpc_ncacn_accept(data->ev_ctx,
486                                             data->msg_ctx,
487                                             NCALRPC,
488                                             b,
489                                             cli_addr,
490                                             srv_addr,
491                                             sd,
492                                             lsasd_client_terminated,
493                                             data);
494                 }
495         } else {
496                 DEBUG(0, ("ERROR: Unsupported socket!\n"));
497         }
498
499 done:
500         talloc_free(tmp_ctx);
501 }
502
503 /*
504  * MAIN
505  */
506
507 static void child_ping(struct messaging_context *msg_ctx,
508                         void *private_data,
509                         uint32_t msg_type,
510                         struct server_id server_id,
511                         DATA_BLOB *data)
512 {
513         struct tevent_context *ev_ctx;
514
515         ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
516
517         DEBUG(10, ("Got message that a child changed status.\n"));
518         pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
519 }
520
521 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
522                                  struct messaging_context *msg_ctx,
523                                  struct timeval current_time);
524
525 static void lsasd_check_children(struct tevent_context *ev_ctx,
526                                     struct tevent_timer *te,
527                                     struct timeval current_time,
528                                     void *pvt);
529
530 static void lsasd_sigchld_handler(struct tevent_context *ev_ctx,
531                                   struct prefork_pool *pfp,
532                                   void *pvt)
533 {
534         struct messaging_context *msg_ctx;
535
536         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
537
538         /* run pool management so we can fork/retire or increase
539          * the allowed connections per child based on load */
540         pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
541 }
542
543 static bool lsasd_setup_children_monitor(struct tevent_context *ev_ctx,
544                                          struct messaging_context *msg_ctx)
545 {
546         bool ok;
547
548         /* add our oun sigchld callback */
549         prefork_set_sigchld_callback(lsasd_pool, lsasd_sigchld_handler, msg_ctx);
550
551         ok = lsasd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());
552
553         return ok;
554 }
555
556 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
557                                  struct messaging_context *msg_ctx,
558                                  struct timeval current_time)
559 {
560         struct tevent_timer *te;
561         struct timeval next_event;
562
563         /* check situation again in 10 seconds */
564         next_event = tevent_timeval_current_ofs(10, 0);
565
566         /* TODO: check when the socket becomes readable, so that children
567          * are checked only when there is some activity ? */
568         te = tevent_add_timer(ev_ctx, lsasd_pool, next_event,
569                               lsasd_check_children, msg_ctx);
570         if (!te) {
571                 DEBUG(2, ("Failed to set up children monitoring!\n"));
572                 return false;
573         }
574
575         return true;
576 }
577
578 static void lsasd_check_children(struct tevent_context *ev_ctx,
579                                  struct tevent_timer *te,
580                                  struct timeval current_time,
581                                  void *pvt)
582 {
583         struct messaging_context *msg_ctx;
584
585         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
586
587         pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
588
589         lsasd_schedule_check(ev_ctx, msg_ctx, current_time);
590 }
591
592 /*
593  * start it up
594  */
595
596 static bool lsasd_create_sockets(struct tevent_context *ev_ctx,
597                                  struct messaging_context *msg_ctx,
598                                  struct pf_listen_fd *listen_fd,
599                                  int *listen_fd_size)
600 {
601         struct dcerpc_binding_vector *v, *v_orig;
602         TALLOC_CTX *tmp_ctx;
603         NTSTATUS status;
604         uint32_t i;
605         int fd = -1;
606         int rc;
607         bool ok = false;
608
609         tmp_ctx = talloc_stackframe();
610         if (tmp_ctx == NULL) {
611                 return false;
612         }
613
614         status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
615         if (!NT_STATUS_IS_OK(status)) {
616                 goto done;
617         }
618
619         /* Create only one tcpip listener for all services */
620         status = dcesrv_create_ncacn_ip_tcp_sockets(&ndr_table_lsarpc,
621                                                     v_orig,
622                                                     0,
623                                                     listen_fd,
624                                                     listen_fd_size);
625         if (!NT_STATUS_IS_OK(status)) {
626                 goto done;
627         }
628
629         /* Start to listen on tcpip sockets */
630         for (i = 0; i < *listen_fd_size; i++) {
631                 rc = listen(listen_fd[i].fd, pf_lsasd_cfg.max_allowed_clients);
632                 if (rc == -1) {
633                         DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
634                                   strerror(errno)));
635                         goto done;
636                 }
637         }
638
639         /* LSARPC */
640         status = dcesrv_create_ncacn_np_socket("lsarpc", &fd);
641         if (!NT_STATUS_IS_OK(status)) {
642                 goto done;
643         }
644
645         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
646         if (rc == -1) {
647                 DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
648                           strerror(errno)));
649                 goto done;
650         }
651         listen_fd[*listen_fd_size].fd = fd;
652         listen_fd[*listen_fd_size].fd_data = NULL;
653         (*listen_fd_size)++;
654         fd = -1;
655
656         status = dcesrv_create_ncacn_np_socket("lsass", &fd);
657         if (!NT_STATUS_IS_OK(status)) {
658                 goto done;
659         }
660
661         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
662         if (rc == -1) {
663                 DEBUG(0, ("Failed to listen on lsass pipe - %s\n",
664                           strerror(errno)));
665                 goto done;
666         }
667         listen_fd[*listen_fd_size].fd = fd;
668         listen_fd[*listen_fd_size].fd_data = NULL;
669         (*listen_fd_size)++;
670         fd = -1;
671
672         status = dcesrv_create_ncalrpc_socket("lsarpc", &fd);
673         if (!NT_STATUS_IS_OK(status)) {
674                 goto done;
675         }
676
677         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
678         if (rc == -1) {
679                 DEBUG(0, ("Failed to listen on lsarpc ncalrpc - %s\n",
680                           strerror(errno)));
681                 goto done;
682         }
683         listen_fd[*listen_fd_size].fd = fd;
684         listen_fd[*listen_fd_size].fd_data = NULL;
685         (*listen_fd_size)++;
686         fd = -1;
687
688         v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
689         if (v == NULL) {
690                 goto done;
691         }
692
693         status = dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc, v);
694         if (!NT_STATUS_IS_OK(status)) {
695                 goto done;
696         }
697
698         status = dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc, v);
699         if (!NT_STATUS_IS_OK(status)) {
700                 goto done;
701         }
702
703         status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "lsarpc");
704         if (!NT_STATUS_IS_OK(status)) {
705                 goto done;
706         }
707
708         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_lsarpc, v);
709         if (!NT_STATUS_IS_OK(status)) {
710                 goto done;
711         }
712
713         /* SAMR */
714         status = dcesrv_create_ncacn_np_socket("samr", &fd);
715         if (!NT_STATUS_IS_OK(status)) {
716                 goto done;
717         }
718
719         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
720         if (rc == -1) {
721                 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
722                           strerror(errno)));
723                 goto done;
724         }
725         listen_fd[*listen_fd_size].fd = fd;
726         listen_fd[*listen_fd_size].fd_data = NULL;
727         (*listen_fd_size)++;
728         fd = -1;
729
730         status = dcesrv_create_ncalrpc_socket("samr", &fd);
731         if (!NT_STATUS_IS_OK(status)) {
732                 goto done;
733         }
734
735         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
736         if (rc == -1) {
737                 DEBUG(0, ("Failed to listen on samr ncalrpc - %s\n",
738                           strerror(errno)));
739                 goto done;
740         }
741         listen_fd[*listen_fd_size].fd = fd;
742         listen_fd[*listen_fd_size].fd_data = NULL;
743         (*listen_fd_size)++;
744         fd = -1;
745
746         v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
747         if (v == NULL) {
748                 goto done;
749         }
750
751         status = dcerpc_binding_vector_replace_iface(&ndr_table_samr, v);
752         if (!NT_STATUS_IS_OK(status)) {
753                 goto done;
754         }
755
756         status = dcerpc_binding_vector_add_np_default(&ndr_table_samr, v);
757         if (!NT_STATUS_IS_OK(status)) {
758                 goto done;
759         }
760
761         status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "samr");
762         if (!NT_STATUS_IS_OK(status)) {
763                 goto done;
764         }
765
766         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_samr, v);
767         if (!NT_STATUS_IS_OK(status)) {
768                 goto done;
769         }
770
771         /* NETLOGON */
772         status = dcesrv_create_ncacn_np_socket("netlogon", &fd);
773         if (!NT_STATUS_IS_OK(status)) {
774                 goto done;
775         }
776
777         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
778         if (rc == -1) {
779                 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
780                           strerror(errno)));
781                 goto done;
782         }
783         listen_fd[*listen_fd_size].fd = fd;
784         listen_fd[*listen_fd_size].fd_data = NULL;
785         (*listen_fd_size)++;
786         fd = -1;
787
788         status = dcesrv_create_ncalrpc_socket("netlogon", &fd);
789         if (!NT_STATUS_IS_OK(status)) {
790                 goto done;
791         }
792
793         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
794         if (rc == -1) {
795                 DEBUG(0, ("Failed to listen on netlogon ncalrpc - %s\n",
796                           strerror(errno)));
797                 goto done;
798         }
799         listen_fd[*listen_fd_size].fd = fd;
800         listen_fd[*listen_fd_size].fd_data = NULL;
801         (*listen_fd_size)++;
802         fd = -1;
803
804         v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
805         if (v == NULL) {
806                 goto done;
807         }
808
809         status = dcerpc_binding_vector_replace_iface(&ndr_table_netlogon, v);
810         if (!NT_STATUS_IS_OK(status)) {
811                 goto done;
812         }
813
814         status = dcerpc_binding_vector_add_np_default(&ndr_table_netlogon, v);
815         if (!NT_STATUS_IS_OK(status)) {
816                 goto done;
817         }
818
819         status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "netlogon");
820         if (!NT_STATUS_IS_OK(status)) {
821                 goto done;
822         }
823
824         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_netlogon, v);
825         if (!NT_STATUS_IS_OK(status)) {
826                 goto done;
827         }
828
829         ok = true;
830 done:
831         if (fd != -1) {
832                 close(fd);
833         }
834         talloc_free(tmp_ctx);
835         return ok;
836 }
837
838 void start_lsasd(struct tevent_context *ev_ctx,
839                  struct messaging_context *msg_ctx)
840 {
841         NTSTATUS status;
842         struct pf_listen_fd listen_fd[LSASD_MAX_SOCKETS];
843         int listen_fd_size = 0;
844         pid_t pid;
845         int rc;
846         bool ok;
847
848         DEBUG(1, ("Forking LSA Service Daemon\n"));
849
850         /*
851          * Block signals before forking child as it will have to
852          * set its own handlers. Child will re-enable SIGHUP as
853          * soon as the handlers are set up.
854          */
855         BlockSignals(true, SIGTERM);
856         BlockSignals(true, SIGHUP);
857
858         pid = fork();
859         if (pid == -1) {
860                 DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
861                            strerror(errno)));
862                 exit(1);
863         }
864
865         /* parent or error */
866         if (pid != 0) {
867
868                 /* Re-enable SIGHUP before returnig */
869                 BlockSignals(false, SIGTERM);
870                 BlockSignals(false, SIGHUP);
871
872                 return;
873         }
874
875         status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, "lsasd-master");
876         if (!NT_STATUS_IS_OK(status)) {
877                 DEBUG(0,("reinit_after_fork() failed\n"));
878                 smb_panic("reinit_after_fork() failed");
879         }
880         initialize_password_db(true, ev_ctx);
881
882         /* save the parent process id so the children can use it later */
883         parent_id = messaging_server_id(msg_ctx);
884
885         lsasd_reopen_logs(0);
886         pfh_daemon_config(DAEMON_NAME,
887                           &pf_lsasd_cfg,
888                           &default_pf_lsasd_cfg);
889
890         lsasd_setup_sig_term_handler(ev_ctx);
891         lsasd_setup_sig_hup_handler(ev_ctx);
892
893         BlockSignals(false, SIGTERM);
894         BlockSignals(false, SIGHUP);
895
896         ok = lsasd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
897         if (!ok) {
898                 exit(1);
899         }
900
901         /* start children before any more initialization is done */
902         ok = prefork_create_pool(ev_ctx, /* mem_ctx */
903                                  ev_ctx,
904                                  msg_ctx,
905                                  listen_fd_size,
906                                  listen_fd,
907                                  pf_lsasd_cfg.min_children,
908                                  pf_lsasd_cfg.max_children,
909                                  &lsasd_children_main,
910                                  NULL,
911                                  &lsasd_pool);
912         if (!ok) {
913                 exit(1);
914         }
915
916         messaging_register(msg_ctx,
917                            ev_ctx,
918                            MSG_SMB_CONF_UPDATED,
919                            lsasd_smb_conf_updated);
920         messaging_register(msg_ctx, ev_ctx,
921                            MSG_PREFORK_CHILD_EVENT, child_ping);
922
923         status = rpc_lsarpc_init(NULL);
924         if (!NT_STATUS_IS_OK(status)) {
925                 DEBUG(0, ("Failed to register lsarpc rpc interface in lsasd! (%s)\n",
926                           nt_errstr(status)));
927                 exit(1);
928         }
929
930         status = rpc_samr_init(NULL);
931         if (!NT_STATUS_IS_OK(status)) {
932                 DEBUG(0, ("Failed to register samr rpc interface in lsasd! (%s)\n",
933                           nt_errstr(status)));
934                 exit(1);
935         }
936
937         status = rpc_netlogon_init(NULL);
938         if (!NT_STATUS_IS_OK(status)) {
939                 DEBUG(0, ("Failed to register netlogon rpc interface in lsasd! (%s)\n",
940                           nt_errstr(status)));
941                 exit(1);
942         }
943
944         ok = lsasd_setup_children_monitor(ev_ctx, msg_ctx);
945         if (!ok) {
946                 DEBUG(0, ("Failed to setup children monitoring!\n"));
947                 exit(1);
948         }
949
950         DEBUG(1, ("LSASD Daemon Started (%u)\n", (unsigned int)getpid()));
951
952         /* loop forever */
953         rc = tevent_loop_wait(ev_ctx);
954
955         /* should not be reached */
956         DEBUG(0,("lsasd: tevent_loop_wait() exited with %d - %s\n",
957                  rc, (rc == 0) ? "out of events" : strerror(errno)));
958         exit(1);
959 }