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