2 * Unix SMB/CIFS implementation.
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/>.
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
,
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();
70 rc
= asprintf(&extension
, "%s.%d", DAEMON_NAME
, child_id
);
72 rc
= asprintf(&extension
, "%s", DAEMON_NAME
);
79 if (lfile
== NULL
|| lfile
[0] == '\0') {
80 rc
= asprintf(&lfile
, "%s/log.%s",
81 get_dyn_LOGFILEBASE(), extension
);
83 if (strstr(lfile
, extension
) == NULL
) {
85 rc
= asprintf(&lfile
, "%s.%d",
89 rc
= asprintf(&lfile
, "%s.%s",
97 lp_set_logfile(lfile
);
101 SAFE_FREE(extension
);
106 static void lsasd_smb_conf_updated(struct messaging_context
*msg
,
109 struct server_id server_id
,
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
,
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
,
136 rpc_netlogon_shutdown();
138 rpc_lsarpc_shutdown();
140 DEBUG(0, ("termination signal\n"));
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
,
151 lsasd_sig_term_handler
,
154 DEBUG(0, ("failed to setup SIGTERM handler\n"));
159 static void lsasd_sig_hup_handler(struct tevent_context
*ev
,
160 struct tevent_signal
*se
,
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
,
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
,
186 lsasd_sig_hup_handler
,
189 DEBUG(0, ("failed to setup SIGHUP handler\n"));
194 /**********************************************************
196 **********************************************************/
198 static void lsasd_chld_sig_hup_handler(struct tevent_context
*ev
,
199 struct tevent_signal
*se
,
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
,
216 lsasd_chld_sig_hup_handler
,
219 DEBUG(1, ("failed to setup SIGHUP handler"));
226 static void parent_ping(struct messaging_context
*msg_ctx
,
229 struct server_id server_id
,
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"));
242 static bool lsasd_child_init(struct tevent_context
*ev_ctx
,
244 struct pf_worker_data
*pf
)
247 struct messaging_context
*msg_ctx
= server_messaging_context();
250 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
251 procid_self(), 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
);
265 if (!serverid_register(procid_self(), FLAG_MSG_GENERAL
)) {
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",
282 status
= rpc_samr_init(NULL
);
283 if (!NT_STATUS_IS_OK(status
)) {
284 DEBUG(0, ("Failed to register samr rpc inteface! (%s)\n",
289 status
= rpc_netlogon_init(NULL
);
290 if (!NT_STATUS_IS_OK(status
)) {
291 DEBUG(0, ("Failed to register netlogon rpc inteface! (%s)\n",
299 struct lsasd_children_data
{
300 struct tevent_context
*ev_ctx
;
301 struct messaging_context
*msg_ctx
;
302 struct pf_worker_data
*pf
;
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
,
317 struct lsasd_children_data
*data
;
321 ok
= lsasd_child_init(ev_ctx
, child_id
, pf
);
326 data
= talloc(ev_ctx
, struct lsasd_children_data
);
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
);
343 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
344 ret
, strerror(errno
)));
345 pf
->status
= PF_WORKER_EXITING
;
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 */
383 next
= talloc_zero(data
, struct lsasd_new_client
);
385 DEBUG(1, ("Out of memory!?\n"));
390 req
= prefork_listen_send(next
,
393 data
->listen_fd_size
,
396 DEBUG(1, ("Failed to make listening request!?\n"));
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
;
411 struct tsocket_address
*srv_addr
;
412 struct tsocket_address
*cli_addr
;
414 client
= tevent_req_callback_data(req
, struct lsasd_new_client
);
417 tmp_ctx
= talloc_stackframe();
418 if (tmp_ctx
== NULL
) {
419 DEBUG(1, ("Failed to allocate stackframe!\n"));
423 rc
= prefork_listen_recv(req
,
429 /* this will free the request too */
433 DEBUG(6, ("No client connection was available after all!\n"));
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
,
457 } else if (tsocket_address_is_unix(srv_addr
)) {
460 p
= tsocket_address_unix_path(srv_addr
, tmp_ctx
);
462 talloc_free(tmp_ctx
);
466 if (strstr(p
, "/np/")) {
469 named_pipe_accept_function(data
->ev_ctx
,
473 lsasd_client_terminated
,
478 dcerpc_ncacn_accept(data
->ev_ctx
,
488 DEBUG(0, ("ERROR: Unsupported socket!\n"));
492 talloc_free(tmp_ctx
);
499 static void child_ping(struct messaging_context
*msg_ctx
,
502 struct server_id server_id
,
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
,
522 static void lsasd_sigchld_handler(struct tevent_context
*ev_ctx
,
523 struct prefork_pool
*pfp
,
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
)
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());
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
);
563 DEBUG(2, ("Failed to set up children monitoring!\n"));
570 static void lsasd_check_children(struct tevent_context
*ev_ctx
,
571 struct tevent_timer
*te
,
572 struct timeval current_time
,
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
);
588 static bool lsasd_create_sockets(struct tevent_context
*ev_ctx
,
589 struct messaging_context
*msg_ctx
,
593 struct dcerpc_binding_vector
*v
, *v_orig
;
601 tmp_ctx
= talloc_stackframe();
602 if (tmp_ctx
== NULL
) {
606 status
= dcerpc_binding_vector_new(tmp_ctx
, &v_orig
);
607 if (!NT_STATUS_IS_OK(status
)) {
612 /* Create only one tcpip listener for all services */
613 status
= rpc_create_tcpip_sockets(&ndr_table_lsarpc
,
618 if (!NT_STATUS_IS_OK(status
)) {
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
);
627 DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
635 fd
= create_named_pipe_socket("lsarpc");
640 listen_fd
[*listen_fd_size
] = fd
;
643 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
645 DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
651 fd
= create_named_pipe_socket("lsass");
656 listen_fd
[*listen_fd_size
] = fd
;
659 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
661 DEBUG(0, ("Failed to listen on lsass pipe - %s\n",
667 fd
= create_dcerpc_ncalrpc_socket("lsarpc");
672 listen_fd
[*listen_fd_size
] = fd
;
675 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
677 DEBUG(0, ("Failed to listen on lsarpc ncalrpc - %s\n",
683 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
689 status
= dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc
, v
);
690 if (!NT_STATUS_IS_OK(status
)) {
694 status
= dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc
, v
);
695 if (!NT_STATUS_IS_OK(status
)) {
700 status
= dcerpc_binding_vector_add_unix(&ndr_table_lsarpc
, v
, "lsarpc");
701 if (!NT_STATUS_IS_OK(status
)) {
706 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_lsarpc
, v
);
707 if (!NT_STATUS_IS_OK(status
)) {
713 fd
= create_named_pipe_socket("samr");
719 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
721 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
726 listen_fd
[*listen_fd_size
] = fd
;
729 fd
= create_dcerpc_ncalrpc_socket("samr");
734 listen_fd
[*listen_fd_size
] = fd
;
737 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
739 DEBUG(0, ("Failed to listen on samr ncalrpc - %s\n",
745 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
751 status
= dcerpc_binding_vector_replace_iface(&ndr_table_samr
, v
);
752 if (!NT_STATUS_IS_OK(status
)) {
756 status
= dcerpc_binding_vector_add_np_default(&ndr_table_samr
, v
);
757 if (!NT_STATUS_IS_OK(status
)) {
762 status
= dcerpc_binding_vector_add_unix(&ndr_table_lsarpc
, v
, "samr");
763 if (!NT_STATUS_IS_OK(status
)) {
768 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_samr
, v
);
769 if (!NT_STATUS_IS_OK(status
)) {
775 fd
= create_named_pipe_socket("netlogon");
781 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
783 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
788 listen_fd
[*listen_fd_size
] = fd
;
791 fd
= create_dcerpc_ncalrpc_socket("netlogon");
796 listen_fd
[*listen_fd_size
] = fd
;
799 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
801 DEBUG(0, ("Failed to listen on netlogon ncalrpc - %s\n",
807 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
813 status
= dcerpc_binding_vector_replace_iface(&ndr_table_netlogon
, v
);
814 if (!NT_STATUS_IS_OK(status
)) {
818 status
= dcerpc_binding_vector_add_np_default(&ndr_table_netlogon
, v
);
819 if (!NT_STATUS_IS_OK(status
)) {
824 status
= dcerpc_binding_vector_add_unix(&ndr_table_lsarpc
, v
, "netlogon");
825 if (!NT_STATUS_IS_OK(status
)) {
830 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_netlogon
, v
);
831 if (!NT_STATUS_IS_OK(status
)) {
837 talloc_free(tmp_ctx
);
841 void start_lsasd(struct tevent_context
*ev_ctx
,
842 struct messaging_context
*msg_ctx
)
845 int listen_fd
[LSASD_MAX_SOCKETS
];
846 int listen_fd_size
= 0;
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
);
863 DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
868 /* parent or error */
871 /* Re-enable SIGHUP before returnig */
872 BlockSignals(false, SIGTERM
);
873 BlockSignals(false, SIGHUP
);
879 close_low_fds(false);
881 /* save the parent process id so the children can use it later */
882 parent_id
= procid_self();
884 status
= reinit_after_fork(msg_ctx
,
886 procid_self(), true);
887 if (!NT_STATUS_IS_OK(status
)) {
888 DEBUG(0,("reinit_after_fork() failed\n"));
889 smb_panic("reinit_after_fork() failed");
892 lsasd_reopen_logs(0);
893 pfh_daemon_config(DAEMON_NAME
,
895 &default_pf_lsasd_cfg
);
897 lsasd_setup_sig_term_handler(ev_ctx
);
898 lsasd_setup_sig_hup_handler(ev_ctx
);
900 BlockSignals(false, SIGTERM
);
901 BlockSignals(false, SIGHUP
);
903 ok
= lsasd_create_sockets(ev_ctx
, msg_ctx
, listen_fd
, &listen_fd_size
);
908 /* start children before any more initialization is done */
909 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
914 pf_lsasd_cfg
.min_children
,
915 pf_lsasd_cfg
.max_children
,
916 &lsasd_children_main
,
923 if (!serverid_register(procid_self(), FLAG_MSG_GENERAL
)) {
927 messaging_register(msg_ctx
,
929 MSG_SMB_CONF_UPDATED
,
930 lsasd_smb_conf_updated
);
931 messaging_register(msg_ctx
, ev_ctx
,
932 MSG_PREFORK_CHILD_EVENT
, child_ping
);
934 status
= rpc_lsarpc_init(NULL
);
935 if (!NT_STATUS_IS_OK(status
)) {
936 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
941 status
= rpc_samr_init(NULL
);
942 if (!NT_STATUS_IS_OK(status
)) {
943 DEBUG(0, ("Failed to register lsasd rpc inteface! (%s)\n",
948 status
= rpc_netlogon_init(NULL
);
949 if (!NT_STATUS_IS_OK(status
)) {
950 DEBUG(0, ("Failed to register lsasd rpc inteface! (%s)\n",
955 ok
= lsasd_setup_children_monitor(ev_ctx
, msg_ctx
);
957 DEBUG(0, ("Failed to setup children monitoring!\n"));
961 DEBUG(1, ("LSASD Daemon Started (%d)\n", getpid()));
964 rc
= tevent_loop_wait(ev_ctx
);
966 /* should not be reached */
967 DEBUG(0,("lsasd: tevent_loop_wait() exited with %d - %s\n",
968 rc
, (rc
== 0) ? "out of events" : strerror(errno
)));