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