source3: initilize_password_db after a fork.
[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
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(talloc_tos());
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(talloc_tos()),
86                                                 child_id);
87                         } else {
88                                 rc = asprintf(&lfile, "%s.%s",
89                                                 lp_logfile(talloc_tos()),
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_global(get_dyn_CONFIGFILE());
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_global(get_dyn_CONFIGFILE());
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                                    true, "lsasd-child");
251         if (!NT_STATUS_IS_OK(status)) {
252                 DEBUG(0,("reinit_after_fork() failed\n"));
253                 smb_panic("reinit_after_fork() failed");
254         }
255         initialize_password_db(true, ev_ctx);
256
257         lsasd_child_id = child_id;
258         lsasd_reopen_logs(child_id);
259
260         ok = lsasd_setup_chld_hup_handler(ev_ctx);
261         if (!ok) {
262                 return false;
263         }
264
265         messaging_register(msg_ctx, ev_ctx,
266                            MSG_SMB_CONF_UPDATED, lsasd_smb_conf_updated);
267         messaging_register(msg_ctx, ev_ctx,
268                            MSG_PREFORK_PARENT_EVENT, parent_ping);
269         id_cache_register_msgs(msg_ctx);
270
271         status = rpc_lsarpc_init(NULL);
272         if (!NT_STATUS_IS_OK(status)) {
273                 DEBUG(0, ("Failed to register lsarpc rpc interface! (%s)\n",
274                           nt_errstr(status)));
275                 return false;
276         }
277
278         status = rpc_samr_init(NULL);
279         if (!NT_STATUS_IS_OK(status)) {
280                 DEBUG(0, ("Failed to register samr rpc interface! (%s)\n",
281                           nt_errstr(status)));
282                 return false;
283         }
284
285         status = rpc_netlogon_init(NULL);
286         if (!NT_STATUS_IS_OK(status)) {
287                 DEBUG(0, ("Failed to register netlogon rpc interface! (%s)\n",
288                           nt_errstr(status)));
289                 return false;
290         }
291
292         return true;
293 }
294
295 struct lsasd_children_data {
296         struct tevent_context *ev_ctx;
297         struct messaging_context *msg_ctx;
298         struct pf_worker_data *pf;
299         int listen_fd_size;
300         int *listen_fds;
301 };
302
303 static void lsasd_next_client(void *pvt);
304
305 static int lsasd_children_main(struct tevent_context *ev_ctx,
306                                struct messaging_context *msg_ctx,
307                                struct pf_worker_data *pf,
308                                int child_id,
309                                int listen_fd_size,
310                                int *listen_fds,
311                                void *private_data)
312 {
313         struct lsasd_children_data *data;
314         bool ok;
315         int ret = 0;
316
317         ok = lsasd_child_init(ev_ctx, child_id, pf);
318         if (!ok) {
319                 return 1;
320         }
321
322         data = talloc(ev_ctx, struct lsasd_children_data);
323         if (!data) {
324                 return 1;
325         }
326         data->pf = pf;
327         data->ev_ctx = ev_ctx;
328         data->msg_ctx = msg_ctx;
329         data->listen_fd_size = listen_fd_size;
330         data->listen_fds = listen_fds;
331
332         /* loop until it is time to exit */
333         while (pf->status != PF_WORKER_EXITING) {
334                 /* try to see if it is time to schedule the next client */
335                 lsasd_next_client(data);
336
337                 ret = tevent_loop_once(ev_ctx);
338                 if (ret != 0) {
339                         DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
340                                   ret, strerror(errno)));
341                         pf->status = PF_WORKER_EXITING;
342                 }
343         }
344
345         return ret;
346 }
347
348 static void lsasd_client_terminated(void *pvt)
349 {
350         struct lsasd_children_data *data;
351
352         data = talloc_get_type_abort(pvt, struct lsasd_children_data);
353
354         pfh_client_terminated(data->pf);
355
356         lsasd_next_client(pvt);
357 }
358
359 struct lsasd_new_client {
360         struct lsasd_children_data *data;
361 };
362
363 static void lsasd_handle_client(struct tevent_req *req);
364
365 static void lsasd_next_client(void *pvt)
366 {
367         struct tevent_req *req;
368         struct lsasd_children_data *data;
369         struct lsasd_new_client *next;
370
371         data = talloc_get_type_abort(pvt, struct lsasd_children_data);
372
373         if (!pfh_child_allowed_to_accept(data->pf)) {
374                 /* nothing to do for now we are already listening
375                  * or we are not allowed to listen further */
376                 return;
377         }
378
379         next = talloc_zero(data, struct lsasd_new_client);
380         if (!next) {
381                 DEBUG(1, ("Out of memory!?\n"));
382                 return;
383         }
384         next->data = data;
385
386         req = prefork_listen_send(next,
387                                   data->ev_ctx,
388                                   data->pf,
389                                   data->listen_fd_size,
390                                   data->listen_fds);
391         if (!req) {
392                 DEBUG(1, ("Failed to make listening request!?\n"));
393                 talloc_free(next);
394                 return;
395         }
396         tevent_req_set_callback(req, lsasd_handle_client, next);
397 }
398
399 static void lsasd_handle_client(struct tevent_req *req)
400 {
401         struct lsasd_children_data *data;
402         struct lsasd_new_client *client;
403         const DATA_BLOB ping = data_blob_null;
404         int rc;
405         int sd;
406         TALLOC_CTX *tmp_ctx;
407         struct tsocket_address *srv_addr;
408         struct tsocket_address *cli_addr;
409
410         client = tevent_req_callback_data(req, struct lsasd_new_client);
411         data = client->data;
412
413         tmp_ctx = talloc_stackframe();
414         if (tmp_ctx == NULL) {
415                 DEBUG(1, ("Failed to allocate stackframe!\n"));
416                 return;
417         }
418
419         rc = prefork_listen_recv(req,
420                                  tmp_ctx,
421                                  &sd,
422                                  &srv_addr,
423                                  &cli_addr);
424
425         /* this will free the request too */
426         talloc_free(client);
427
428         if (rc != 0) {
429                 DEBUG(6, ("No client connection was available after all!\n"));
430                 goto done;
431         }
432
433         /* Warn parent that our status changed */
434         messaging_send(data->msg_ctx, parent_id,
435                         MSG_PREFORK_CHILD_EVENT, &ping);
436
437         DEBUG(2, ("LSASD preforked child %d got client connection!\n",
438                   (int)(data->pf->pid)));
439
440         if (tsocket_address_is_inet(srv_addr, "ip")) {
441                 DEBUG(3, ("Got a tcpip client connection from %s on interface %s\n",
442                            tsocket_address_string(cli_addr, tmp_ctx),
443                            tsocket_address_string(srv_addr, tmp_ctx)));
444
445                 dcerpc_ncacn_accept(data->ev_ctx,
446                                     data->msg_ctx,
447                                     NCACN_IP_TCP,
448                                     "IP",
449                                     cli_addr,
450                                     srv_addr,
451                                     sd,
452                                     NULL);
453         } else if (tsocket_address_is_unix(srv_addr)) {
454                 const char *p;
455                 const char *b;
456
457                 p = tsocket_address_unix_path(srv_addr, tmp_ctx);
458                 if (p == NULL) {
459                         talloc_free(tmp_ctx);
460                         return;
461                 }
462
463                 b = strrchr(p, '/');
464                 if (b != NULL) {
465                         b++;
466                 } else {
467                         b = p;
468                 }
469
470                 if (strstr(p, "/np/")) {
471                         named_pipe_accept_function(data->ev_ctx,
472                                                    data->msg_ctx,
473                                                    b,
474                                                    sd,
475                                                    lsasd_client_terminated,
476                                                    data);
477                 } else {
478                         dcerpc_ncacn_accept(data->ev_ctx,
479                                             data->msg_ctx,
480                                             NCALRPC,
481                                             b,
482                                             cli_addr,
483                                             srv_addr,
484                                             sd,
485                                             NULL);
486                 }
487         } else {
488                 DEBUG(0, ("ERROR: Unsupported socket!\n"));
489         }
490
491 done:
492         talloc_free(tmp_ctx);
493 }
494
495 /*
496  * MAIN
497  */
498
499 static void child_ping(struct messaging_context *msg_ctx,
500                         void *private_data,
501                         uint32_t msg_type,
502                         struct server_id server_id,
503                         DATA_BLOB *data)
504 {
505         struct tevent_context *ev_ctx;
506
507         ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
508
509         DEBUG(10, ("Got message that a child changed status.\n"));
510         pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
511 }
512
513 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
514                                  struct messaging_context *msg_ctx,
515                                  struct timeval current_time);
516
517 static void lsasd_check_children(struct tevent_context *ev_ctx,
518                                     struct tevent_timer *te,
519                                     struct timeval current_time,
520                                     void *pvt);
521
522 static void lsasd_sigchld_handler(struct tevent_context *ev_ctx,
523                                   struct prefork_pool *pfp,
524                                   void *pvt)
525 {
526         struct messaging_context *msg_ctx;
527
528         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
529
530         /* run pool management so we can fork/retire or increase
531          * the allowed connections per child based on load */
532         pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
533 }
534
535 static bool lsasd_setup_children_monitor(struct tevent_context *ev_ctx,
536                                          struct messaging_context *msg_ctx)
537 {
538         bool ok;
539
540         /* add our oun sigchld callback */
541         prefork_set_sigchld_callback(lsasd_pool, lsasd_sigchld_handler, msg_ctx);
542
543         ok = lsasd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());
544
545         return ok;
546 }
547
548 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
549                                  struct messaging_context *msg_ctx,
550                                  struct timeval current_time)
551 {
552         struct tevent_timer *te;
553         struct timeval next_event;
554
555         /* check situation again in 10 seconds */
556         next_event = tevent_timeval_current_ofs(10, 0);
557
558         /* TODO: check when the socket becomes readable, so that children
559          * are checked only when there is some activity ? */
560         te = tevent_add_timer(ev_ctx, lsasd_pool, next_event,
561                               lsasd_check_children, msg_ctx);
562         if (!te) {
563                 DEBUG(2, ("Failed to set up children monitoring!\n"));
564                 return false;
565         }
566
567         return true;
568 }
569
570 static void lsasd_check_children(struct tevent_context *ev_ctx,
571                                  struct tevent_timer *te,
572                                  struct timeval current_time,
573                                  void *pvt)
574 {
575         struct messaging_context *msg_ctx;
576
577         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
578
579         pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
580
581         lsasd_schedule_check(ev_ctx, msg_ctx, current_time);
582 }
583
584 /*
585  * start it up
586  */
587
588 static bool lsasd_create_sockets(struct tevent_context *ev_ctx,
589                                  struct messaging_context *msg_ctx,
590                                  int *listen_fd,
591                                  int *listen_fd_size)
592 {
593         struct dcerpc_binding_vector *v, *v_orig;
594         TALLOC_CTX *tmp_ctx;
595         NTSTATUS status;
596         uint32_t i;
597         int fd = -1;
598         int rc;
599         bool ok = false;
600
601         tmp_ctx = talloc_stackframe();
602         if (tmp_ctx == NULL) {
603                 return false;
604         }
605
606         status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
607         if (!NT_STATUS_IS_OK(status)) {
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                 goto done;
619         }
620
621         /* Start to listen on tcpip sockets */
622         for (i = 0; i < *listen_fd_size; i++) {
623                 rc = listen(listen_fd[i], pf_lsasd_cfg.max_allowed_clients);
624                 if (rc == -1) {
625                         DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
626                                   strerror(errno)));
627                         goto done;
628                 }
629         }
630
631         /* LSARPC */
632         fd = create_named_pipe_socket("lsarpc");
633         if (fd < 0) {
634                 goto done;
635         }
636
637         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
638         if (rc == -1) {
639                 DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
640                           strerror(errno)));
641                 goto done;
642         }
643         listen_fd[*listen_fd_size] = fd;
644         (*listen_fd_size)++;
645
646         fd = create_named_pipe_socket("lsass");
647         if (fd < 0) {
648                 goto done;
649         }
650
651         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
652         if (rc == -1) {
653                 DEBUG(0, ("Failed to listen on lsass pipe - %s\n",
654                           strerror(errno)));
655                 goto done;
656         }
657         listen_fd[*listen_fd_size] = fd;
658         (*listen_fd_size)++;
659
660         fd = create_dcerpc_ncalrpc_socket("lsarpc");
661         if (fd < 0) {
662                 goto done;
663         }
664
665         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
666         if (rc == -1) {
667                 DEBUG(0, ("Failed to listen on lsarpc ncalrpc - %s\n",
668                           strerror(errno)));
669                 goto done;
670         }
671         listen_fd[*listen_fd_size] = fd;
672         (*listen_fd_size)++;
673         fd = -1;
674
675         v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
676         if (v == NULL) {
677                 goto done;
678         }
679
680         status = dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc, v);
681         if (!NT_STATUS_IS_OK(status)) {
682                 goto done;
683         }
684
685         status = dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc, v);
686         if (!NT_STATUS_IS_OK(status)) {
687                 goto done;
688         }
689
690         status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "lsarpc");
691         if (!NT_STATUS_IS_OK(status)) {
692                 goto done;
693         }
694
695         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_lsarpc, v);
696         if (!NT_STATUS_IS_OK(status)) {
697                 goto done;
698         }
699
700         /* SAMR */
701         fd = create_named_pipe_socket("samr");
702         if (fd < 0) {
703                 goto done;
704         }
705
706         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
707         if (rc == -1) {
708                 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
709                           strerror(errno)));
710                 goto done;
711         }
712         listen_fd[*listen_fd_size] = fd;
713         (*listen_fd_size)++;
714
715         fd = create_dcerpc_ncalrpc_socket("samr");
716         if (fd < 0) {
717                 goto done;
718         }
719
720         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
721         if (rc == -1) {
722                 DEBUG(0, ("Failed to listen on samr ncalrpc - %s\n",
723                           strerror(errno)));
724                 goto done;
725         }
726         listen_fd[*listen_fd_size] = fd;
727         (*listen_fd_size)++;
728         fd = -1;
729
730         v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
731         if (v == NULL) {
732                 goto done;
733         }
734
735         status = dcerpc_binding_vector_replace_iface(&ndr_table_samr, v);
736         if (!NT_STATUS_IS_OK(status)) {
737                 goto done;
738         }
739
740         status = dcerpc_binding_vector_add_np_default(&ndr_table_samr, v);
741         if (!NT_STATUS_IS_OK(status)) {
742                 goto done;
743         }
744
745         status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "samr");
746         if (!NT_STATUS_IS_OK(status)) {
747                 goto done;
748         }
749
750         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_samr, v);
751         if (!NT_STATUS_IS_OK(status)) {
752                 goto done;
753         }
754
755         /* NETLOGON */
756         fd = create_named_pipe_socket("netlogon");
757         if (fd < 0) {
758                 goto done;
759         }
760
761         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
762         if (rc == -1) {
763                 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
764                           strerror(errno)));
765                 goto done;
766         }
767         listen_fd[*listen_fd_size] = fd;
768         (*listen_fd_size)++;
769
770         fd = create_dcerpc_ncalrpc_socket("netlogon");
771         if (fd < 0) {
772                 goto done;
773         }
774
775         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
776         if (rc == -1) {
777                 DEBUG(0, ("Failed to listen on netlogon ncalrpc - %s\n",
778                           strerror(errno)));
779                 goto done;
780         }
781         listen_fd[*listen_fd_size] = fd;
782         (*listen_fd_size)++;
783         fd = -1;
784
785         v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
786         if (v == NULL) {
787                 goto done;
788         }
789
790         status = dcerpc_binding_vector_replace_iface(&ndr_table_netlogon, v);
791         if (!NT_STATUS_IS_OK(status)) {
792                 goto done;
793         }
794
795         status = dcerpc_binding_vector_add_np_default(&ndr_table_netlogon, v);
796         if (!NT_STATUS_IS_OK(status)) {
797                 goto done;
798         }
799
800         status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "netlogon");
801         if (!NT_STATUS_IS_OK(status)) {
802                 goto done;
803         }
804
805         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_netlogon, v);
806         if (!NT_STATUS_IS_OK(status)) {
807                 goto done;
808         }
809
810         ok = true;
811 done:
812         if (fd != -1) {
813                 close(fd);
814         }
815         talloc_free(tmp_ctx);
816         return ok;
817 }
818
819 void start_lsasd(struct tevent_context *ev_ctx,
820                  struct messaging_context *msg_ctx)
821 {
822         NTSTATUS status;
823         int listen_fd[LSASD_MAX_SOCKETS];
824         int listen_fd_size = 0;
825         pid_t pid;
826         int rc;
827         bool ok;
828
829         DEBUG(1, ("Forking LSA Service Daemon\n"));
830
831         /*
832          * Block signals before forking child as it will have to
833          * set its own handlers. Child will re-enable SIGHUP as
834          * soon as the handlers are set up.
835          */
836         BlockSignals(true, SIGTERM);
837         BlockSignals(true, SIGHUP);
838
839         pid = fork();
840         if (pid == -1) {
841                 DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
842                            strerror(errno)));
843                 exit(1);
844         }
845
846         /* parent or error */
847         if (pid != 0) {
848
849                 /* Re-enable SIGHUP before returnig */
850                 BlockSignals(false, SIGTERM);
851                 BlockSignals(false, SIGHUP);
852
853                 return;
854         }
855
856         status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, "lsasd-master");
857         if (!NT_STATUS_IS_OK(status)) {
858                 DEBUG(0,("reinit_after_fork() failed\n"));
859                 smb_panic("reinit_after_fork() failed");
860         }
861         initialize_password_db(true, ev_ctx);
862
863         /* save the parent process id so the children can use it later */
864         parent_id = messaging_server_id(msg_ctx);
865
866         lsasd_reopen_logs(0);
867         pfh_daemon_config(DAEMON_NAME,
868                           &pf_lsasd_cfg,
869                           &default_pf_lsasd_cfg);
870
871         lsasd_setup_sig_term_handler(ev_ctx);
872         lsasd_setup_sig_hup_handler(ev_ctx);
873
874         BlockSignals(false, SIGTERM);
875         BlockSignals(false, SIGHUP);
876
877         ok = lsasd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
878         if (!ok) {
879                 exit(1);
880         }
881
882         /* start children before any more initialization is done */
883         ok = prefork_create_pool(ev_ctx, /* mem_ctx */
884                                  ev_ctx,
885                                  msg_ctx,
886                                  listen_fd_size,
887                                  listen_fd,
888                                  pf_lsasd_cfg.min_children,
889                                  pf_lsasd_cfg.max_children,
890                                  &lsasd_children_main,
891                                  NULL,
892                                  &lsasd_pool);
893         if (!ok) {
894                 exit(1);
895         }
896
897         messaging_register(msg_ctx,
898                            ev_ctx,
899                            MSG_SMB_CONF_UPDATED,
900                            lsasd_smb_conf_updated);
901         messaging_register(msg_ctx, ev_ctx,
902                            MSG_PREFORK_CHILD_EVENT, child_ping);
903
904         status = rpc_lsarpc_init(NULL);
905         if (!NT_STATUS_IS_OK(status)) {
906                 DEBUG(0, ("Failed to register lsarpc rpc interface in lsasd! (%s)\n",
907                           nt_errstr(status)));
908                 exit(1);
909         }
910
911         status = rpc_samr_init(NULL);
912         if (!NT_STATUS_IS_OK(status)) {
913                 DEBUG(0, ("Failed to register samr rpc interface in lsasd! (%s)\n",
914                           nt_errstr(status)));
915                 exit(1);
916         }
917
918         status = rpc_netlogon_init(NULL);
919         if (!NT_STATUS_IS_OK(status)) {
920                 DEBUG(0, ("Failed to register netlogon rpc interface in lsasd! (%s)\n",
921                           nt_errstr(status)));
922                 exit(1);
923         }
924
925         ok = lsasd_setup_children_monitor(ev_ctx, msg_ctx);
926         if (!ok) {
927                 DEBUG(0, ("Failed to setup children monitoring!\n"));
928                 exit(1);
929         }
930
931         DEBUG(1, ("LSASD Daemon Started (%u)\n", (unsigned int)getpid()));
932
933         /* loop forever */
934         rc = tevent_loop_wait(ev_ctx);
935
936         /* should not be reached */
937         DEBUG(0,("lsasd: tevent_loop_wait() exited with %d - %s\n",
938                  rc, (rc == 0) ? "out of events" : strerror(errno)));
939         exit(1);
940 }