s3:libsmb: let cli_read_andx_create() accept any length
[Samba/gebeck_regimport.git] / source3 / rpc_server / lsasd.c
blob8d2245abb5fb91c1525dd2de81e53c2018737de3
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"
27 #include "lib/util/util_process.h"
29 #include "lib/id_cache.h"
31 #include "../lib/tsocket/tsocket.h"
32 #include "lib/server_prefork.h"
33 #include "lib/server_prefork_util.h"
34 #include "librpc/rpc/dcerpc_ep.h"
36 #include "rpc_server/rpc_server.h"
37 #include "rpc_server/rpc_ep_register.h"
38 #include "rpc_server/rpc_sock_helper.h"
40 #include "librpc/gen_ndr/srv_lsa.h"
41 #include "librpc/gen_ndr/srv_samr.h"
42 #include "librpc/gen_ndr/srv_netlogon.h"
44 #define DAEMON_NAME "lsasd"
45 #define LSASD_MAX_SOCKETS 64
47 static struct server_id parent_id;
48 static struct prefork_pool *lsasd_pool = NULL;
49 static int lsasd_child_id = 0;
51 static struct pf_daemon_config default_pf_lsasd_cfg = {
52 .prefork_status = PFH_INIT,
53 .min_children = 5,
54 .max_children = 25,
55 .spawn_rate = 5,
56 .max_allowed_clients = 100,
57 .child_min_life = 60 /* 1 minute minimum life time */
59 static struct pf_daemon_config pf_lsasd_cfg = { 0 };
61 void start_lsasd(struct tevent_context *ev_ctx,
62 struct messaging_context *msg_ctx);
64 static void lsasd_reopen_logs(int child_id)
66 char *lfile = lp_logfile(talloc_tos());
67 char *extension;
68 int rc;
70 if (child_id) {
71 rc = asprintf(&extension, "%s.%d", DAEMON_NAME, child_id);
72 } else {
73 rc = asprintf(&extension, "%s", DAEMON_NAME);
75 if (rc == -1) {
76 return;
79 rc = 0;
80 if (lfile == NULL || lfile[0] == '\0') {
81 rc = asprintf(&lfile, "%s/log.%s",
82 get_dyn_LOGFILEBASE(), extension);
83 } else {
84 if (strstr(lfile, extension) == NULL) {
85 if (child_id) {
86 rc = asprintf(&lfile, "%s.%d",
87 lp_logfile(talloc_tos()),
88 child_id);
89 } else {
90 rc = asprintf(&lfile, "%s.%s",
91 lp_logfile(talloc_tos()),
92 extension);
97 if (rc > 0) {
98 lp_set_logfile(lfile);
99 SAFE_FREE(lfile);
102 SAFE_FREE(extension);
104 reopen_logs();
107 static void lsasd_smb_conf_updated(struct messaging_context *msg,
108 void *private_data,
109 uint32_t msg_type,
110 struct server_id server_id,
111 DATA_BLOB *data)
113 struct tevent_context *ev_ctx;
115 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
116 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
118 change_to_root_user();
119 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
121 lsasd_reopen_logs(lsasd_child_id);
122 if (lsasd_child_id == 0) {
123 pfh_daemon_config(DAEMON_NAME,
124 &pf_lsasd_cfg,
125 &default_pf_lsasd_cfg);
126 pfh_manage_pool(ev_ctx, msg, &pf_lsasd_cfg, lsasd_pool);
130 static void lsasd_sig_term_handler(struct tevent_context *ev,
131 struct tevent_signal *se,
132 int signum,
133 int count,
134 void *siginfo,
135 void *private_data)
137 rpc_netlogon_shutdown();
138 rpc_samr_shutdown();
139 rpc_lsarpc_shutdown();
141 DEBUG(0, ("termination signal\n"));
142 exit(0);
145 static void lsasd_setup_sig_term_handler(struct tevent_context *ev_ctx)
147 struct tevent_signal *se;
149 se = tevent_add_signal(ev_ctx,
150 ev_ctx,
151 SIGTERM, 0,
152 lsasd_sig_term_handler,
153 NULL);
154 if (!se) {
155 DEBUG(0, ("failed to setup SIGTERM handler\n"));
156 exit(1);
160 static void lsasd_sig_hup_handler(struct tevent_context *ev,
161 struct tevent_signal *se,
162 int signum,
163 int count,
164 void *siginfo,
165 void *pvt)
168 change_to_root_user();
169 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
171 lsasd_reopen_logs(lsasd_child_id);
172 pfh_daemon_config(DAEMON_NAME,
173 &pf_lsasd_cfg,
174 &default_pf_lsasd_cfg);
176 /* relay to all children */
177 prefork_send_signal_to_all(lsasd_pool, SIGHUP);
180 static void lsasd_setup_sig_hup_handler(struct tevent_context *ev_ctx)
182 struct tevent_signal *se;
184 se = tevent_add_signal(ev_ctx,
185 ev_ctx,
186 SIGHUP, 0,
187 lsasd_sig_hup_handler,
188 NULL);
189 if (!se) {
190 DEBUG(0, ("failed to setup SIGHUP handler\n"));
191 exit(1);
195 /**********************************************************
196 * Children
197 **********************************************************/
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)
206 change_to_root_user();
207 lsasd_reopen_logs(lsasd_child_id);
210 static bool lsasd_setup_chld_hup_handler(struct tevent_context *ev_ctx)
212 struct tevent_signal *se;
214 se = tevent_add_signal(ev_ctx,
215 ev_ctx,
216 SIGHUP, 0,
217 lsasd_chld_sig_hup_handler,
218 NULL);
219 if (!se) {
220 DEBUG(1, ("failed to setup SIGHUP handler"));
221 return false;
224 return true;
227 static void parent_ping(struct messaging_context *msg_ctx,
228 void *private_data,
229 uint32_t msg_type,
230 struct server_id server_id,
231 DATA_BLOB *data)
234 /* The fact we received this message is enough to let make the event
235 * loop if it was idle. lsasd_children_main will cycle through
236 * lsasd_next_client at least once. That function will take whatever
237 * action is necessary */
239 DEBUG(10, ("Got message that the parent changed status.\n"));
240 return;
243 static bool lsasd_child_init(struct tevent_context *ev_ctx,
244 int child_id,
245 struct pf_worker_data *pf)
247 NTSTATUS status;
248 struct messaging_context *msg_ctx = server_messaging_context();
249 bool ok;
251 status = reinit_after_fork(msg_ctx, ev_ctx,
252 true);
253 if (!NT_STATUS_IS_OK(status)) {
254 DEBUG(0,("reinit_after_fork() failed\n"));
255 smb_panic("reinit_after_fork() failed");
258 prctl_set_comment("lsasd-child");
260 lsasd_child_id = child_id;
261 lsasd_reopen_logs(child_id);
263 ok = lsasd_setup_chld_hup_handler(ev_ctx);
264 if (!ok) {
265 return false;
268 if (!serverid_register(messaging_server_id(msg_ctx),
269 FLAG_MSG_GENERAL)) {
270 return false;
273 messaging_register(msg_ctx, ev_ctx,
274 MSG_SMB_CONF_UPDATED, lsasd_smb_conf_updated);
275 messaging_register(msg_ctx, ev_ctx,
276 MSG_PREFORK_PARENT_EVENT, parent_ping);
277 id_cache_register_msgs(msg_ctx);
279 status = rpc_lsarpc_init(NULL);
280 if (!NT_STATUS_IS_OK(status)) {
281 DEBUG(0, ("Failed to register lsarpc rpc inteface! (%s)\n",
282 nt_errstr(status)));
283 return false;
286 status = rpc_samr_init(NULL);
287 if (!NT_STATUS_IS_OK(status)) {
288 DEBUG(0, ("Failed to register samr rpc inteface! (%s)\n",
289 nt_errstr(status)));
290 return false;
293 status = rpc_netlogon_init(NULL);
294 if (!NT_STATUS_IS_OK(status)) {
295 DEBUG(0, ("Failed to register netlogon rpc inteface! (%s)\n",
296 nt_errstr(status)));
297 return false;
300 return true;
303 struct lsasd_children_data {
304 struct tevent_context *ev_ctx;
305 struct messaging_context *msg_ctx;
306 struct pf_worker_data *pf;
307 int listen_fd_size;
308 int *listen_fds;
311 static void lsasd_next_client(void *pvt);
313 static int lsasd_children_main(struct tevent_context *ev_ctx,
314 struct messaging_context *msg_ctx,
315 struct pf_worker_data *pf,
316 int child_id,
317 int listen_fd_size,
318 int *listen_fds,
319 void *private_data)
321 struct lsasd_children_data *data;
322 bool ok;
323 int ret = 0;
325 ok = lsasd_child_init(ev_ctx, child_id, pf);
326 if (!ok) {
327 return 1;
330 data = talloc(ev_ctx, struct lsasd_children_data);
331 if (!data) {
332 return 1;
334 data->pf = pf;
335 data->ev_ctx = ev_ctx;
336 data->msg_ctx = msg_ctx;
337 data->listen_fd_size = listen_fd_size;
338 data->listen_fds = listen_fds;
340 /* loop until it is time to exit */
341 while (pf->status != PF_WORKER_EXITING) {
342 /* try to see if it is time to schedule the next client */
343 lsasd_next_client(data);
345 ret = tevent_loop_once(ev_ctx);
346 if (ret != 0) {
347 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
348 ret, strerror(errno)));
349 pf->status = PF_WORKER_EXITING;
353 return ret;
356 static void lsasd_client_terminated(void *pvt)
358 struct lsasd_children_data *data;
360 data = talloc_get_type_abort(pvt, struct lsasd_children_data);
362 pfh_client_terminated(data->pf);
364 lsasd_next_client(pvt);
367 struct lsasd_new_client {
368 struct lsasd_children_data *data;
371 static void lsasd_handle_client(struct tevent_req *req);
373 static void lsasd_next_client(void *pvt)
375 struct tevent_req *req;
376 struct lsasd_children_data *data;
377 struct lsasd_new_client *next;
379 data = talloc_get_type_abort(pvt, struct lsasd_children_data);
381 if (!pfh_child_allowed_to_accept(data->pf)) {
382 /* nothing to do for now we are already listening
383 * or we are not allowed to listen further */
384 return;
387 next = talloc_zero(data, struct lsasd_new_client);
388 if (!next) {
389 DEBUG(1, ("Out of memory!?\n"));
390 return;
392 next->data = data;
394 req = prefork_listen_send(next,
395 data->ev_ctx,
396 data->pf,
397 data->listen_fd_size,
398 data->listen_fds);
399 if (!req) {
400 DEBUG(1, ("Failed to make listening request!?\n"));
401 talloc_free(next);
402 return;
404 tevent_req_set_callback(req, lsasd_handle_client, next);
407 static void lsasd_handle_client(struct tevent_req *req)
409 struct lsasd_children_data *data;
410 struct lsasd_new_client *client;
411 const DATA_BLOB ping = data_blob_null;
412 int rc;
413 int sd;
414 TALLOC_CTX *tmp_ctx;
415 struct tsocket_address *srv_addr;
416 struct tsocket_address *cli_addr;
418 client = tevent_req_callback_data(req, struct lsasd_new_client);
419 data = client->data;
421 tmp_ctx = talloc_stackframe();
422 if (tmp_ctx == NULL) {
423 DEBUG(1, ("Failed to allocate stackframe!\n"));
424 return;
427 rc = prefork_listen_recv(req,
428 tmp_ctx,
429 &sd,
430 &srv_addr,
431 &cli_addr);
433 /* this will free the request too */
434 talloc_free(client);
436 if (rc != 0) {
437 DEBUG(6, ("No client connection was available after all!\n"));
438 goto done;
441 /* Warn parent that our status changed */
442 messaging_send(data->msg_ctx, parent_id,
443 MSG_PREFORK_CHILD_EVENT, &ping);
445 DEBUG(2, ("LSASD preforked child %d got client connection!\n",
446 (int)(data->pf->pid)));
448 if (tsocket_address_is_inet(srv_addr, "ip")) {
449 DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
450 tsocket_address_string(cli_addr, tmp_ctx),
451 tsocket_address_string(srv_addr, tmp_ctx)));
453 dcerpc_ncacn_accept(data->ev_ctx,
454 data->msg_ctx,
455 NCACN_IP_TCP,
456 "IP",
457 cli_addr,
458 srv_addr,
460 NULL);
461 } else if (tsocket_address_is_unix(srv_addr)) {
462 const char *p;
463 const char *b;
465 p = tsocket_address_unix_path(srv_addr, tmp_ctx);
466 if (p == NULL) {
467 talloc_free(tmp_ctx);
468 return;
471 b = strrchr(p, '/');
472 if (b != NULL) {
473 b++;
474 } else {
475 b = p;
478 if (strstr(p, "/np/")) {
479 named_pipe_accept_function(data->ev_ctx,
480 data->msg_ctx,
483 lsasd_client_terminated,
484 data);
485 } else {
486 dcerpc_ncacn_accept(data->ev_ctx,
487 data->msg_ctx,
488 NCALRPC,
490 cli_addr,
491 srv_addr,
493 NULL);
495 } else {
496 DEBUG(0, ("ERROR: Unsupported socket!\n"));
499 done:
500 talloc_free(tmp_ctx);
504 * MAIN
507 static void child_ping(struct messaging_context *msg_ctx,
508 void *private_data,
509 uint32_t msg_type,
510 struct server_id server_id,
511 DATA_BLOB *data)
513 struct tevent_context *ev_ctx;
515 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
517 DEBUG(10, ("Got message that a child changed status.\n"));
518 pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
521 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
522 struct messaging_context *msg_ctx,
523 struct timeval current_time);
525 static void lsasd_check_children(struct tevent_context *ev_ctx,
526 struct tevent_timer *te,
527 struct timeval current_time,
528 void *pvt);
530 static void lsasd_sigchld_handler(struct tevent_context *ev_ctx,
531 struct prefork_pool *pfp,
532 void *pvt)
534 struct messaging_context *msg_ctx;
536 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
538 /* run pool management so we can fork/retire or increase
539 * the allowed connections per child based on load */
540 pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
543 static bool lsasd_setup_children_monitor(struct tevent_context *ev_ctx,
544 struct messaging_context *msg_ctx)
546 bool ok;
548 /* add our oun sigchld callback */
549 prefork_set_sigchld_callback(lsasd_pool, lsasd_sigchld_handler, msg_ctx);
551 ok = lsasd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());
553 return ok;
556 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
557 struct messaging_context *msg_ctx,
558 struct timeval current_time)
560 struct tevent_timer *te;
561 struct timeval next_event;
563 /* check situation again in 10 seconds */
564 next_event = tevent_timeval_current_ofs(10, 0);
566 /* TODO: check when the socket becomes readable, so that children
567 * are checked only when there is some activity ? */
568 te = tevent_add_timer(ev_ctx, lsasd_pool, next_event,
569 lsasd_check_children, msg_ctx);
570 if (!te) {
571 DEBUG(2, ("Failed to set up children monitoring!\n"));
572 return false;
575 return true;
578 static void lsasd_check_children(struct tevent_context *ev_ctx,
579 struct tevent_timer *te,
580 struct timeval current_time,
581 void *pvt)
583 struct messaging_context *msg_ctx;
585 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
587 pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
589 lsasd_schedule_check(ev_ctx, msg_ctx, current_time);
593 * start it up
596 static bool lsasd_create_sockets(struct tevent_context *ev_ctx,
597 struct messaging_context *msg_ctx,
598 int *listen_fd,
599 int *listen_fd_size)
601 struct dcerpc_binding_vector *v, *v_orig;
602 TALLOC_CTX *tmp_ctx;
603 NTSTATUS status;
604 uint32_t i;
605 int fd = -1;
606 int rc;
607 bool ok = true;
609 tmp_ctx = talloc_stackframe();
610 if (tmp_ctx == NULL) {
611 return false;
614 status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
615 if (!NT_STATUS_IS_OK(status)) {
616 ok = false;
617 goto done;
620 /* Create only one tcpip listener for all services */
621 status = rpc_create_tcpip_sockets(&ndr_table_lsarpc,
622 v_orig,
624 listen_fd,
625 listen_fd_size);
626 if (!NT_STATUS_IS_OK(status)) {
627 ok = false;
628 goto done;
631 /* Start to listen on tcpip sockets */
632 for (i = 0; i < *listen_fd_size; i++) {
633 rc = listen(listen_fd[i], pf_lsasd_cfg.max_allowed_clients);
634 if (rc == -1) {
635 DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
636 strerror(errno)));
637 ok = false;
638 goto done;
642 /* LSARPC */
643 fd = create_named_pipe_socket("lsarpc");
644 if (fd < 0) {
645 ok = false;
646 goto done;
649 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
650 if (rc == -1) {
651 DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
652 strerror(errno)));
653 ok = false;
654 goto done;
656 listen_fd[*listen_fd_size] = fd;
657 (*listen_fd_size)++;
659 fd = create_named_pipe_socket("lsass");
660 if (fd < 0) {
661 ok = false;
662 goto done;
665 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
666 if (rc == -1) {
667 DEBUG(0, ("Failed to listen on lsass pipe - %s\n",
668 strerror(errno)));
669 ok = false;
670 goto done;
672 listen_fd[*listen_fd_size] = fd;
673 (*listen_fd_size)++;
675 fd = create_dcerpc_ncalrpc_socket("lsarpc");
676 if (fd < 0) {
677 ok = false;
678 goto done;
681 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
682 if (rc == -1) {
683 DEBUG(0, ("Failed to listen on lsarpc ncalrpc - %s\n",
684 strerror(errno)));
685 ok = false;
686 goto done;
688 listen_fd[*listen_fd_size] = fd;
689 (*listen_fd_size)++;
690 fd = -1;
692 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
693 if (v == NULL) {
694 ok = false;
695 goto done;
698 status = dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc, v);
699 if (!NT_STATUS_IS_OK(status)) {
700 return false;
703 status = dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc, v);
704 if (!NT_STATUS_IS_OK(status)) {
705 ok = false;
706 goto done;
709 status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "lsarpc");
710 if (!NT_STATUS_IS_OK(status)) {
711 ok = false;
712 goto done;
715 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_lsarpc, v);
716 if (!NT_STATUS_IS_OK(status)) {
717 ok = false;
718 goto done;
721 /* SAMR */
722 fd = create_named_pipe_socket("samr");
723 if (fd < 0) {
724 ok = false;
725 goto done;
728 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
729 if (rc == -1) {
730 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
731 strerror(errno)));
732 ok = false;
733 goto done;
735 listen_fd[*listen_fd_size] = fd;
736 (*listen_fd_size)++;
738 fd = create_dcerpc_ncalrpc_socket("samr");
739 if (fd < 0) {
740 ok = false;
741 goto done;
744 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
745 if (rc == -1) {
746 DEBUG(0, ("Failed to listen on samr ncalrpc - %s\n",
747 strerror(errno)));
748 ok = false;
749 goto done;
751 listen_fd[*listen_fd_size] = fd;
752 (*listen_fd_size)++;
753 fd = -1;
755 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
756 if (v == NULL) {
757 ok = false;
758 goto done;
761 status = dcerpc_binding_vector_replace_iface(&ndr_table_samr, v);
762 if (!NT_STATUS_IS_OK(status)) {
763 return false;
766 status = dcerpc_binding_vector_add_np_default(&ndr_table_samr, v);
767 if (!NT_STATUS_IS_OK(status)) {
768 ok = false;
769 goto done;
772 status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "samr");
773 if (!NT_STATUS_IS_OK(status)) {
774 ok = false;
775 goto done;
778 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_samr, v);
779 if (!NT_STATUS_IS_OK(status)) {
780 ok = false;
781 goto done;
784 /* NETLOGON */
785 fd = create_named_pipe_socket("netlogon");
786 if (fd < 0) {
787 ok = false;
788 goto done;
791 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
792 if (rc == -1) {
793 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
794 strerror(errno)));
795 ok = false;
796 goto done;
798 listen_fd[*listen_fd_size] = fd;
799 (*listen_fd_size)++;
801 fd = create_dcerpc_ncalrpc_socket("netlogon");
802 if (fd < 0) {
803 ok = false;
804 goto done;
807 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
808 if (rc == -1) {
809 DEBUG(0, ("Failed to listen on netlogon ncalrpc - %s\n",
810 strerror(errno)));
811 close(fd);
812 ok = false;
813 goto done;
815 listen_fd[*listen_fd_size] = fd;
816 (*listen_fd_size)++;
817 fd = -1;
819 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
820 if (v == NULL) {
821 ok = false;
822 goto done;
825 status = dcerpc_binding_vector_replace_iface(&ndr_table_netlogon, v);
826 if (!NT_STATUS_IS_OK(status)) {
827 return false;
830 status = dcerpc_binding_vector_add_np_default(&ndr_table_netlogon, v);
831 if (!NT_STATUS_IS_OK(status)) {
832 ok = false;
833 goto done;
836 status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "netlogon");
837 if (!NT_STATUS_IS_OK(status)) {
838 ok = false;
839 goto done;
842 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_netlogon, v);
843 if (!NT_STATUS_IS_OK(status)) {
844 ok = false;
845 goto done;
848 done:
849 if (fd != -1) {
850 close(fd);
852 talloc_free(tmp_ctx);
853 return ok;
856 void start_lsasd(struct tevent_context *ev_ctx,
857 struct messaging_context *msg_ctx)
859 NTSTATUS status;
860 int listen_fd[LSASD_MAX_SOCKETS];
861 int listen_fd_size = 0;
862 pid_t pid;
863 int rc;
864 bool ok;
866 DEBUG(1, ("Forking LSA Service Daemon\n"));
869 * Block signals before forking child as it will have to
870 * set its own handlers. Child will re-enable SIGHUP as
871 * soon as the handlers are set up.
873 BlockSignals(true, SIGTERM);
874 BlockSignals(true, SIGHUP);
876 pid = fork();
877 if (pid == -1) {
878 DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
879 strerror(errno)));
880 exit(1);
883 /* parent or error */
884 if (pid != 0) {
886 /* Re-enable SIGHUP before returnig */
887 BlockSignals(false, SIGTERM);
888 BlockSignals(false, SIGHUP);
890 return;
893 status = reinit_after_fork(msg_ctx,
894 ev_ctx,
895 true);
896 if (!NT_STATUS_IS_OK(status)) {
897 DEBUG(0,("reinit_after_fork() failed\n"));
898 smb_panic("reinit_after_fork() failed");
901 prctl_set_comment("lsasd-master");
903 /* save the parent process id so the children can use it later */
904 parent_id = messaging_server_id(msg_ctx);
906 lsasd_reopen_logs(0);
907 pfh_daemon_config(DAEMON_NAME,
908 &pf_lsasd_cfg,
909 &default_pf_lsasd_cfg);
911 lsasd_setup_sig_term_handler(ev_ctx);
912 lsasd_setup_sig_hup_handler(ev_ctx);
914 BlockSignals(false, SIGTERM);
915 BlockSignals(false, SIGHUP);
917 ok = lsasd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
918 if (!ok) {
919 exit(1);
922 /* start children before any more initialization is done */
923 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
924 ev_ctx,
925 msg_ctx,
926 listen_fd_size,
927 listen_fd,
928 pf_lsasd_cfg.min_children,
929 pf_lsasd_cfg.max_children,
930 &lsasd_children_main,
931 NULL,
932 &lsasd_pool);
933 if (!ok) {
934 exit(1);
937 if (!serverid_register(messaging_server_id(msg_ctx),
938 FLAG_MSG_GENERAL)) {
939 exit(1);
942 messaging_register(msg_ctx,
943 ev_ctx,
944 MSG_SMB_CONF_UPDATED,
945 lsasd_smb_conf_updated);
946 messaging_register(msg_ctx, ev_ctx,
947 MSG_PREFORK_CHILD_EVENT, child_ping);
949 status = rpc_lsarpc_init(NULL);
950 if (!NT_STATUS_IS_OK(status)) {
951 DEBUG(0, ("Failed to register lsarpc rpc inteface in lsasd! (%s)\n",
952 nt_errstr(status)));
953 exit(1);
956 status = rpc_samr_init(NULL);
957 if (!NT_STATUS_IS_OK(status)) {
958 DEBUG(0, ("Failed to register samr rpc inteface in lsasd! (%s)\n",
959 nt_errstr(status)));
960 exit(1);
963 status = rpc_netlogon_init(NULL);
964 if (!NT_STATUS_IS_OK(status)) {
965 DEBUG(0, ("Failed to register netlogon rpc inteface in lsasd! (%s)\n",
966 nt_errstr(status)));
967 exit(1);
970 ok = lsasd_setup_children_monitor(ev_ctx, msg_ctx);
971 if (!ok) {
972 DEBUG(0, ("Failed to setup children monitoring!\n"));
973 exit(1);
976 DEBUG(1, ("LSASD Daemon Started (%u)\n", (unsigned int)getpid()));
978 /* loop forever */
979 rc = tevent_loop_wait(ev_ctx);
981 /* should not be reached */
982 DEBUG(0,("lsasd: tevent_loop_wait() exited with %d - %s\n",
983 rc, (rc == 0) ? "out of events" : strerror(errno)));
984 exit(1);