s3:libsmb: fix the talloc parent of clistr_pull_talloc() in cli_notify_done()
[Samba/gebeck_regimport.git] / source3 / rpc_server / lsasd.c
blob6608d6ba33279fabc4804b51e052beb7ff33533f
1 /*
2 * Unix SMB/CIFS implementation.
4 * LSA service daemon
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
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.
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.
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/>.
22 #include "includes.h"
23 #include "serverid.h"
24 #include "messages.h"
25 #include "ntdomain.h"
26 #include <libgen.h>
28 #include "lib/id_cache.h"
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"
35 #include "rpc_server/rpc_server.h"
36 #include "rpc_server/rpc_ep_register.h"
37 #include "rpc_server/rpc_sock_helper.h"
39 #include "librpc/gen_ndr/srv_lsa.h"
40 #include "librpc/gen_ndr/srv_samr.h"
41 #include "librpc/gen_ndr/srv_netlogon.h"
43 #define DAEMON_NAME "lsasd"
44 #define LSASD_MAX_SOCKETS 64
46 static struct server_id parent_id;
47 static struct prefork_pool *lsasd_pool = NULL;
48 static int lsasd_child_id = 0;
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 */
58 static struct pf_daemon_config pf_lsasd_cfg = { 0 };
60 void start_lsasd(struct tevent_context *ev_ctx,
61 struct messaging_context *msg_ctx);
63 static void lsasd_reopen_logs(int child_id)
65 char *lfile = lp_logfile();
66 char *extension;
67 int rc;
69 if (child_id) {
70 rc = asprintf(&extension, "%s.%d", DAEMON_NAME, child_id);
71 } else {
72 rc = asprintf(&extension, "%s", DAEMON_NAME);
74 if (rc == -1) {
75 return;
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);
96 if (rc > 0) {
97 lp_set_logfile(lfile);
98 SAFE_FREE(lfile);
101 SAFE_FREE(extension);
103 reopen_logs();
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)
112 struct tevent_context *ev_ctx;
114 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
115 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
117 change_to_root_user();
118 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
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);
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)
136 rpc_netlogon_shutdown();
137 rpc_samr_shutdown();
138 rpc_lsarpc_shutdown();
140 DEBUG(0, ("termination signal\n"));
141 exit(0);
144 static void lsasd_setup_sig_term_handler(struct tevent_context *ev_ctx)
146 struct tevent_signal *se;
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);
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)
167 change_to_root_user();
168 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
170 lsasd_reopen_logs(lsasd_child_id);
171 pfh_daemon_config(DAEMON_NAME,
172 &pf_lsasd_cfg,
173 &default_pf_lsasd_cfg);
175 /* relay to all children */
176 prefork_send_signal_to_all(lsasd_pool, SIGHUP);
179 static void lsasd_setup_sig_hup_handler(struct tevent_context *ev_ctx)
181 struct tevent_signal *se;
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);
194 /**********************************************************
195 * Children
196 **********************************************************/
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)
205 change_to_root_user();
206 lsasd_reopen_logs(lsasd_child_id);
209 static bool lsasd_setup_chld_hup_handler(struct tevent_context *ev_ctx)
211 struct tevent_signal *se;
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;
223 return true;
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)
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 */
238 DEBUG(10, ("Got message that the parent changed status.\n"));
239 return;
242 static bool lsasd_child_init(struct tevent_context *ev_ctx,
243 int child_id,
244 struct pf_worker_data *pf)
246 NTSTATUS status;
247 struct messaging_context *msg_ctx = server_messaging_context();
248 bool ok;
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");
257 lsasd_child_id = child_id;
258 lsasd_reopen_logs(child_id);
260 ok = lsasd_setup_chld_hup_handler(ev_ctx);
261 if (!ok) {
262 return false;
265 if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
266 return false;
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);
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;
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;
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;
296 return true;
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;
307 static void lsasd_next_client(void *pvt);
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)
317 struct lsasd_children_data *data;
318 bool ok;
319 int ret = 0;
321 ok = lsasd_child_init(ev_ctx, child_id, pf);
322 if (!ok) {
323 return 1;
326 data = talloc(ev_ctx, struct lsasd_children_data);
327 if (!data) {
328 return 1;
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;
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);
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;
349 return ret;
352 static void lsasd_client_terminated(void *pvt)
354 struct lsasd_children_data *data;
356 data = talloc_get_type_abort(pvt, struct lsasd_children_data);
358 pfh_client_terminated(data->pf);
360 lsasd_next_client(pvt);
363 struct lsasd_new_client {
364 struct lsasd_children_data *data;
367 static void lsasd_handle_client(struct tevent_req *req);
369 static void lsasd_next_client(void *pvt)
371 struct tevent_req *req;
372 struct lsasd_children_data *data;
373 struct lsasd_new_client *next;
375 data = talloc_get_type_abort(pvt, struct lsasd_children_data);
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;
383 next = talloc_zero(data, struct lsasd_new_client);
384 if (!next) {
385 DEBUG(1, ("Out of memory!?\n"));
386 return;
388 next->data = data;
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;
400 tevent_req_set_callback(req, lsasd_handle_client, next);
403 static void lsasd_handle_client(struct tevent_req *req)
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;
414 client = tevent_req_callback_data(req, struct lsasd_new_client);
415 data = client->data;
417 tmp_ctx = talloc_stackframe();
418 if (tmp_ctx == NULL) {
419 DEBUG(1, ("Failed to allocate stackframe!\n"));
420 return;
423 rc = prefork_listen_recv(req,
424 tmp_ctx,
425 &sd,
426 &srv_addr,
427 &cli_addr);
429 /* this will free the request too */
430 talloc_free(client);
432 if (rc != 0) {
433 DEBUG(6, ("No client connection was available after all!\n"));
434 goto done;
437 /* Warn parent that our status changed */
438 messaging_send(data->msg_ctx, parent_id,
439 MSG_PREFORK_CHILD_EVENT, &ping);
441 DEBUG(2, ("LSASD preforked child %d got client connection!\n",
442 (int)(data->pf->pid)));
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)));
449 dcerpc_ncacn_accept(data->ev_ctx,
450 data->msg_ctx,
451 NCACN_IP_TCP,
452 "IP",
453 cli_addr,
454 srv_addr,
456 NULL);
457 } else if (tsocket_address_is_unix(srv_addr)) {
458 char *p;
460 p = tsocket_address_unix_path(srv_addr, tmp_ctx);
461 if (p == NULL) {
462 talloc_free(tmp_ctx);
463 return;
466 if (strstr(p, "/np/")) {
467 p = basename(p);
469 named_pipe_accept_function(data->ev_ctx,
470 data->msg_ctx,
473 lsasd_client_terminated,
474 data);
475 } else {
476 p = basename(p);
478 dcerpc_ncacn_accept(data->ev_ctx,
479 data->msg_ctx,
480 NCALRPC,
482 cli_addr,
483 srv_addr,
485 NULL);
487 } else {
488 DEBUG(0, ("ERROR: Unsupported socket!\n"));
491 done:
492 talloc_free(tmp_ctx);
496 * MAIN
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)
505 struct tevent_context *ev_ctx;
507 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
509 DEBUG(10, ("Got message that a child changed status.\n"));
510 pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
513 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
514 struct messaging_context *msg_ctx,
515 struct timeval current_time);
517 static void lsasd_check_children(struct tevent_context *ev_ctx,
518 struct tevent_timer *te,
519 struct timeval current_time,
520 void *pvt);
522 static void lsasd_sigchld_handler(struct tevent_context *ev_ctx,
523 struct prefork_pool *pfp,
524 void *pvt)
526 struct messaging_context *msg_ctx;
528 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
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);
535 static bool lsasd_setup_children_monitor(struct tevent_context *ev_ctx,
536 struct messaging_context *msg_ctx)
538 bool ok;
540 /* add our oun sigchld callback */
541 prefork_set_sigchld_callback(lsasd_pool, lsasd_sigchld_handler, msg_ctx);
543 ok = lsasd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());
545 return ok;
548 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
549 struct messaging_context *msg_ctx,
550 struct timeval current_time)
552 struct tevent_timer *te;
553 struct timeval next_event;
555 /* check situation again in 10 seconds */
556 next_event = tevent_timeval_current_ofs(10, 0);
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;
567 return true;
570 static void lsasd_check_children(struct tevent_context *ev_ctx,
571 struct tevent_timer *te,
572 struct timeval current_time,
573 void *pvt)
575 struct messaging_context *msg_ctx;
577 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
579 pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
581 lsasd_schedule_check(ev_ctx, msg_ctx, current_time);
585 * start it up
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)
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;
601 tmp_ctx = talloc_stackframe();
602 if (tmp_ctx == NULL) {
603 return false;
606 status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
607 if (!NT_STATUS_IS_OK(status)) {
608 ok = false;
609 goto done;
612 /* Create only one tcpip listener for all services */
613 status = rpc_create_tcpip_sockets(&ndr_table_lsarpc,
614 v_orig,
616 listen_fd,
617 listen_fd_size);
618 if (!NT_STATUS_IS_OK(status)) {
619 ok = false;
620 goto done;
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;
634 /* LSARPC */
635 fd = create_named_pipe_socket("lsarpc");
636 if (fd < 0) {
637 ok = false;
638 goto done;
640 listen_fd[*listen_fd_size] = fd;
641 (*listen_fd_size)++;
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;
651 fd = create_named_pipe_socket("lsass");
652 if (fd < 0) {
653 ok = false;
654 goto done;
656 listen_fd[*listen_fd_size] = fd;
657 (*listen_fd_size)++;
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;
667 fd = create_dcerpc_ncalrpc_socket("lsarpc");
668 if (fd < 0) {
669 ok = false;
670 goto done;
672 listen_fd[*listen_fd_size] = fd;
673 (*listen_fd_size)++;
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;
683 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
684 if (v == NULL) {
685 ok = false;
686 goto done;
689 status = dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc, v);
690 if (!NT_STATUS_IS_OK(status)) {
691 return false;
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;
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;
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;
712 /* SAMR */
713 fd = create_named_pipe_socket("samr");
714 if (fd < 0) {
715 ok = false;
716 goto done;
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;
726 listen_fd[*listen_fd_size] = fd;
727 (*listen_fd_size)++;
729 fd = create_dcerpc_ncalrpc_socket("samr");
730 if (fd < 0) {
731 ok = false;
732 goto done;
734 listen_fd[*listen_fd_size] = fd;
735 (*listen_fd_size)++;
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;
745 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
746 if (v == NULL) {
747 ok = false;
748 goto done;
751 status = dcerpc_binding_vector_replace_iface(&ndr_table_samr, v);
752 if (!NT_STATUS_IS_OK(status)) {
753 return false;
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;
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;
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;
774 /* NETLOGON */
775 fd = create_named_pipe_socket("netlogon");
776 if (fd < 0) {
777 ok = false;
778 goto done;
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;
788 listen_fd[*listen_fd_size] = fd;
789 (*listen_fd_size)++;
791 fd = create_dcerpc_ncalrpc_socket("netlogon");
792 if (fd < 0) {
793 ok = false;
794 goto done;
796 listen_fd[*listen_fd_size] = fd;
797 (*listen_fd_size)++;
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;
807 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
808 if (v == NULL) {
809 ok = false;
810 goto done;
813 status = dcerpc_binding_vector_replace_iface(&ndr_table_netlogon, v);
814 if (!NT_STATUS_IS_OK(status)) {
815 return false;
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;
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;
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;
836 done:
837 talloc_free(tmp_ctx);
838 return ok;
841 void start_lsasd(struct tevent_context *ev_ctx,
842 struct messaging_context *msg_ctx)
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;
851 DEBUG(1, ("Forking LSA Service Daemon\n"));
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.
858 BlockSignals(true, SIGTERM);
859 BlockSignals(true, SIGHUP);
861 pid = fork();
862 if (pid == -1) {
863 DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
864 strerror(errno)));
865 exit(1);
868 /* parent or error */
869 if (pid != 0) {
871 /* Re-enable SIGHUP before returnig */
872 BlockSignals(false, SIGTERM);
873 BlockSignals(false, SIGHUP);
875 return;
878 /* save the parent process id so the children can use it later */
879 parent_id = procid_self();
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");
889 lsasd_reopen_logs(0);
890 pfh_daemon_config(DAEMON_NAME,
891 &pf_lsasd_cfg,
892 &default_pf_lsasd_cfg);
894 lsasd_setup_sig_term_handler(ev_ctx);
895 lsasd_setup_sig_hup_handler(ev_ctx);
897 BlockSignals(false, SIGTERM);
898 BlockSignals(false, SIGHUP);
900 ok = lsasd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
901 if (!ok) {
902 exit(1);
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);
920 if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
921 exit(1);
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);
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);
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);
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);
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);
958 DEBUG(1, ("LSASD Daemon Started (%d)\n", getpid()));
960 /* loop forever */
961 rc = tevent_loop_wait(ev_ctx);
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);