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