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/>.
27 #include "lib/id_cache.h"
29 #include "../lib/tsocket/tsocket.h"
30 #include "lib/server_prefork.h"
31 #include "lib/server_prefork_util.h"
32 #include "librpc/rpc/dcerpc_ep.h"
34 #include "rpc_server/rpc_server.h"
35 #include "rpc_server/rpc_ep_register.h"
36 #include "rpc_server/rpc_sock_helper.h"
38 #include "librpc/gen_ndr/srv_lsa.h"
39 #include "librpc/gen_ndr/srv_samr.h"
40 #include "librpc/gen_ndr/srv_netlogon.h"
42 #define DAEMON_NAME "lsasd"
43 #define LSASD_MAX_SOCKETS 64
45 static struct server_id parent_id
;
46 static struct prefork_pool
*lsasd_pool
= NULL
;
47 static int lsasd_child_id
= 0;
49 static struct pf_daemon_config default_pf_lsasd_cfg
= {
50 .prefork_status
= PFH_INIT
,
54 .max_allowed_clients
= 100,
55 .child_min_life
= 60 /* 1 minute minimum life time */
57 static struct pf_daemon_config pf_lsasd_cfg
= { 0 };
59 void start_lsasd(struct tevent_context
*ev_ctx
,
60 struct messaging_context
*msg_ctx
);
62 static void lsasd_reopen_logs(int child_id
)
64 char *lfile
= lp_logfile(talloc_tos());
69 rc
= asprintf(&extension
, "%s.%d", DAEMON_NAME
, child_id
);
71 rc
= asprintf(&extension
, "%s", DAEMON_NAME
);
78 if (lfile
== NULL
|| lfile
[0] == '\0') {
79 rc
= asprintf(&lfile
, "%s/log.%s",
80 get_dyn_LOGFILEBASE(), extension
);
82 if (strstr(lfile
, extension
) == NULL
) {
84 rc
= asprintf(&lfile
, "%s.%d",
85 lp_logfile(talloc_tos()),
88 rc
= asprintf(&lfile
, "%s.%s",
89 lp_logfile(talloc_tos()),
96 lp_set_logfile(lfile
);
100 SAFE_FREE(extension
);
105 static void lsasd_smb_conf_updated(struct messaging_context
*msg
,
108 struct server_id server_id
,
111 struct tevent_context
*ev_ctx
;
113 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
114 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
116 change_to_root_user();
117 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
119 lsasd_reopen_logs(lsasd_child_id
);
120 if (lsasd_child_id
== 0) {
121 pfh_daemon_config(DAEMON_NAME
,
123 &default_pf_lsasd_cfg
);
124 pfh_manage_pool(ev_ctx
, msg
, &pf_lsasd_cfg
, lsasd_pool
);
128 static void lsasd_sig_term_handler(struct tevent_context
*ev
,
129 struct tevent_signal
*se
,
135 rpc_netlogon_shutdown();
137 rpc_lsarpc_shutdown();
139 DEBUG(0, ("termination signal\n"));
143 static void lsasd_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
145 struct tevent_signal
*se
;
147 se
= tevent_add_signal(ev_ctx
,
150 lsasd_sig_term_handler
,
153 DEBUG(0, ("failed to setup SIGTERM handler\n"));
158 static void lsasd_sig_hup_handler(struct tevent_context
*ev
,
159 struct tevent_signal
*se
,
166 change_to_root_user();
167 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
169 lsasd_reopen_logs(lsasd_child_id
);
170 pfh_daemon_config(DAEMON_NAME
,
172 &default_pf_lsasd_cfg
);
174 /* relay to all children */
175 prefork_send_signal_to_all(lsasd_pool
, SIGHUP
);
178 static void lsasd_setup_sig_hup_handler(struct tevent_context
*ev_ctx
)
180 struct tevent_signal
*se
;
182 se
= tevent_add_signal(ev_ctx
,
185 lsasd_sig_hup_handler
,
188 DEBUG(0, ("failed to setup SIGHUP handler\n"));
193 /**********************************************************
195 **********************************************************/
197 static void lsasd_chld_sig_hup_handler(struct tevent_context
*ev
,
198 struct tevent_signal
*se
,
204 change_to_root_user();
205 lsasd_reopen_logs(lsasd_child_id
);
208 static bool lsasd_setup_chld_hup_handler(struct tevent_context
*ev_ctx
)
210 struct tevent_signal
*se
;
212 se
= tevent_add_signal(ev_ctx
,
215 lsasd_chld_sig_hup_handler
,
218 DEBUG(1, ("failed to setup SIGHUP handler"));
225 static void parent_ping(struct messaging_context
*msg_ctx
,
228 struct server_id server_id
,
232 /* The fact we received this message is enough to let make the event
233 * loop if it was idle. lsasd_children_main will cycle through
234 * lsasd_next_client at least once. That function will take whatever
235 * action is necessary */
237 DEBUG(10, ("Got message that the parent changed status.\n"));
241 static bool lsasd_child_init(struct tevent_context
*ev_ctx
,
243 struct pf_worker_data
*pf
)
246 struct messaging_context
*msg_ctx
= server_messaging_context();
249 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
251 if (!NT_STATUS_IS_OK(status
)) {
252 DEBUG(0,("reinit_after_fork() failed\n"));
253 smb_panic("reinit_after_fork() failed");
256 lsasd_child_id
= child_id
;
257 lsasd_reopen_logs(child_id
);
259 ok
= lsasd_setup_chld_hup_handler(ev_ctx
);
264 if (!serverid_register(procid_self(), FLAG_MSG_GENERAL
)) {
268 messaging_register(msg_ctx
, ev_ctx
,
269 MSG_SMB_CONF_UPDATED
, lsasd_smb_conf_updated
);
270 messaging_register(msg_ctx
, ev_ctx
,
271 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
272 id_cache_register_msgs(msg_ctx
);
274 status
= rpc_lsarpc_init(NULL
);
275 if (!NT_STATUS_IS_OK(status
)) {
276 DEBUG(0, ("Failed to register lsarpc rpc inteface! (%s)\n",
281 status
= rpc_samr_init(NULL
);
282 if (!NT_STATUS_IS_OK(status
)) {
283 DEBUG(0, ("Failed to register samr rpc inteface! (%s)\n",
288 status
= rpc_netlogon_init(NULL
);
289 if (!NT_STATUS_IS_OK(status
)) {
290 DEBUG(0, ("Failed to register netlogon rpc inteface! (%s)\n",
298 struct lsasd_children_data
{
299 struct tevent_context
*ev_ctx
;
300 struct messaging_context
*msg_ctx
;
301 struct pf_worker_data
*pf
;
306 static void lsasd_next_client(void *pvt
);
308 static int lsasd_children_main(struct tevent_context
*ev_ctx
,
309 struct messaging_context
*msg_ctx
,
310 struct pf_worker_data
*pf
,
316 struct lsasd_children_data
*data
;
320 ok
= lsasd_child_init(ev_ctx
, child_id
, pf
);
325 data
= talloc(ev_ctx
, struct lsasd_children_data
);
330 data
->ev_ctx
= ev_ctx
;
331 data
->msg_ctx
= msg_ctx
;
332 data
->listen_fd_size
= listen_fd_size
;
333 data
->listen_fds
= listen_fds
;
335 /* loop until it is time to exit */
336 while (pf
->status
!= PF_WORKER_EXITING
) {
337 /* try to see if it is time to schedule the next client */
338 lsasd_next_client(data
);
340 ret
= tevent_loop_once(ev_ctx
);
342 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
343 ret
, strerror(errno
)));
344 pf
->status
= PF_WORKER_EXITING
;
351 static void lsasd_client_terminated(void *pvt
)
353 struct lsasd_children_data
*data
;
355 data
= talloc_get_type_abort(pvt
, struct lsasd_children_data
);
357 pfh_client_terminated(data
->pf
);
359 lsasd_next_client(pvt
);
362 struct lsasd_new_client
{
363 struct lsasd_children_data
*data
;
366 static void lsasd_handle_client(struct tevent_req
*req
);
368 static void lsasd_next_client(void *pvt
)
370 struct tevent_req
*req
;
371 struct lsasd_children_data
*data
;
372 struct lsasd_new_client
*next
;
374 data
= talloc_get_type_abort(pvt
, struct lsasd_children_data
);
376 if (!pfh_child_allowed_to_accept(data
->pf
)) {
377 /* nothing to do for now we are already listening
378 * or we are not allowed to listen further */
382 next
= talloc_zero(data
, struct lsasd_new_client
);
384 DEBUG(1, ("Out of memory!?\n"));
389 req
= prefork_listen_send(next
,
392 data
->listen_fd_size
,
395 DEBUG(1, ("Failed to make listening request!?\n"));
399 tevent_req_set_callback(req
, lsasd_handle_client
, next
);
402 static void lsasd_handle_client(struct tevent_req
*req
)
404 struct lsasd_children_data
*data
;
405 struct lsasd_new_client
*client
;
406 const DATA_BLOB ping
= data_blob_null
;
410 struct tsocket_address
*srv_addr
;
411 struct tsocket_address
*cli_addr
;
413 client
= tevent_req_callback_data(req
, struct lsasd_new_client
);
416 tmp_ctx
= talloc_stackframe();
417 if (tmp_ctx
== NULL
) {
418 DEBUG(1, ("Failed to allocate stackframe!\n"));
422 rc
= prefork_listen_recv(req
,
428 /* this will free the request too */
432 DEBUG(6, ("No client connection was available after all!\n"));
436 /* Warn parent that our status changed */
437 messaging_send(data
->msg_ctx
, parent_id
,
438 MSG_PREFORK_CHILD_EVENT
, &ping
);
440 DEBUG(2, ("LSASD preforked child %d got client connection!\n",
441 (int)(data
->pf
->pid
)));
443 if (tsocket_address_is_inet(srv_addr
, "ip")) {
444 DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
445 tsocket_address_string(cli_addr
, tmp_ctx
),
446 tsocket_address_string(srv_addr
, tmp_ctx
)));
448 dcerpc_ncacn_accept(data
->ev_ctx
,
456 } else if (tsocket_address_is_unix(srv_addr
)) {
460 p
= tsocket_address_unix_path(srv_addr
, tmp_ctx
);
462 talloc_free(tmp_ctx
);
473 if (strstr(p
, "/np/")) {
474 named_pipe_accept_function(data
->ev_ctx
,
478 lsasd_client_terminated
,
481 dcerpc_ncacn_accept(data
->ev_ctx
,
491 DEBUG(0, ("ERROR: Unsupported socket!\n"));
495 talloc_free(tmp_ctx
);
502 static void child_ping(struct messaging_context
*msg_ctx
,
505 struct server_id server_id
,
508 struct tevent_context
*ev_ctx
;
510 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
512 DEBUG(10, ("Got message that a child changed status.\n"));
513 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_lsasd_cfg
, lsasd_pool
);
516 static bool lsasd_schedule_check(struct tevent_context
*ev_ctx
,
517 struct messaging_context
*msg_ctx
,
518 struct timeval current_time
);
520 static void lsasd_check_children(struct tevent_context
*ev_ctx
,
521 struct tevent_timer
*te
,
522 struct timeval current_time
,
525 static void lsasd_sigchld_handler(struct tevent_context
*ev_ctx
,
526 struct prefork_pool
*pfp
,
529 struct messaging_context
*msg_ctx
;
531 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
533 /* run pool management so we can fork/retire or increase
534 * the allowed connections per child based on load */
535 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_lsasd_cfg
, lsasd_pool
);
538 static bool lsasd_setup_children_monitor(struct tevent_context
*ev_ctx
,
539 struct messaging_context
*msg_ctx
)
543 /* add our oun sigchld callback */
544 prefork_set_sigchld_callback(lsasd_pool
, lsasd_sigchld_handler
, msg_ctx
);
546 ok
= lsasd_schedule_check(ev_ctx
, msg_ctx
, tevent_timeval_current());
551 static bool lsasd_schedule_check(struct tevent_context
*ev_ctx
,
552 struct messaging_context
*msg_ctx
,
553 struct timeval current_time
)
555 struct tevent_timer
*te
;
556 struct timeval next_event
;
558 /* check situation again in 10 seconds */
559 next_event
= tevent_timeval_current_ofs(10, 0);
561 /* TODO: check when the socket becomes readable, so that children
562 * are checked only when there is some activity ? */
563 te
= tevent_add_timer(ev_ctx
, lsasd_pool
, next_event
,
564 lsasd_check_children
, msg_ctx
);
566 DEBUG(2, ("Failed to set up children monitoring!\n"));
573 static void lsasd_check_children(struct tevent_context
*ev_ctx
,
574 struct tevent_timer
*te
,
575 struct timeval current_time
,
578 struct messaging_context
*msg_ctx
;
580 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
582 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_lsasd_cfg
, lsasd_pool
);
584 lsasd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
591 static bool lsasd_create_sockets(struct tevent_context
*ev_ctx
,
592 struct messaging_context
*msg_ctx
,
596 struct dcerpc_binding_vector
*v
, *v_orig
;
604 tmp_ctx
= talloc_stackframe();
605 if (tmp_ctx
== NULL
) {
609 status
= dcerpc_binding_vector_new(tmp_ctx
, &v_orig
);
610 if (!NT_STATUS_IS_OK(status
)) {
615 /* Create only one tcpip listener for all services */
616 status
= rpc_create_tcpip_sockets(&ndr_table_lsarpc
,
621 if (!NT_STATUS_IS_OK(status
)) {
626 /* Start to listen on tcpip sockets */
627 for (i
= 0; i
< *listen_fd_size
; i
++) {
628 rc
= listen(listen_fd
[i
], pf_lsasd_cfg
.max_allowed_clients
);
630 DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
638 fd
= create_named_pipe_socket("lsarpc");
644 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
646 DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
651 listen_fd
[*listen_fd_size
] = fd
;
654 fd
= create_named_pipe_socket("lsass");
660 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
662 DEBUG(0, ("Failed to listen on lsass pipe - %s\n",
667 listen_fd
[*listen_fd_size
] = fd
;
670 fd
= create_dcerpc_ncalrpc_socket("lsarpc");
676 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
678 DEBUG(0, ("Failed to listen on lsarpc ncalrpc - %s\n",
683 listen_fd
[*listen_fd_size
] = fd
;
687 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
693 status
= dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc
, v
);
694 if (!NT_STATUS_IS_OK(status
)) {
698 status
= dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc
, v
);
699 if (!NT_STATUS_IS_OK(status
)) {
704 status
= dcerpc_binding_vector_add_unix(&ndr_table_lsarpc
, v
, "lsarpc");
705 if (!NT_STATUS_IS_OK(status
)) {
710 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_lsarpc
, v
);
711 if (!NT_STATUS_IS_OK(status
)) {
717 fd
= create_named_pipe_socket("samr");
723 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
725 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
730 listen_fd
[*listen_fd_size
] = fd
;
733 fd
= create_dcerpc_ncalrpc_socket("samr");
739 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
741 DEBUG(0, ("Failed to listen on samr ncalrpc - %s\n",
746 listen_fd
[*listen_fd_size
] = fd
;
750 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
756 status
= dcerpc_binding_vector_replace_iface(&ndr_table_samr
, v
);
757 if (!NT_STATUS_IS_OK(status
)) {
761 status
= dcerpc_binding_vector_add_np_default(&ndr_table_samr
, v
);
762 if (!NT_STATUS_IS_OK(status
)) {
767 status
= dcerpc_binding_vector_add_unix(&ndr_table_lsarpc
, v
, "samr");
768 if (!NT_STATUS_IS_OK(status
)) {
773 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_samr
, v
);
774 if (!NT_STATUS_IS_OK(status
)) {
780 fd
= create_named_pipe_socket("netlogon");
786 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
788 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
793 listen_fd
[*listen_fd_size
] = fd
;
796 fd
= create_dcerpc_ncalrpc_socket("netlogon");
802 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
804 DEBUG(0, ("Failed to listen on netlogon ncalrpc - %s\n",
810 listen_fd
[*listen_fd_size
] = fd
;
814 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
820 status
= dcerpc_binding_vector_replace_iface(&ndr_table_netlogon
, v
);
821 if (!NT_STATUS_IS_OK(status
)) {
825 status
= dcerpc_binding_vector_add_np_default(&ndr_table_netlogon
, v
);
826 if (!NT_STATUS_IS_OK(status
)) {
831 status
= dcerpc_binding_vector_add_unix(&ndr_table_lsarpc
, v
, "netlogon");
832 if (!NT_STATUS_IS_OK(status
)) {
837 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_netlogon
, v
);
838 if (!NT_STATUS_IS_OK(status
)) {
847 talloc_free(tmp_ctx
);
851 void start_lsasd(struct tevent_context
*ev_ctx
,
852 struct messaging_context
*msg_ctx
)
855 int listen_fd
[LSASD_MAX_SOCKETS
];
856 int listen_fd_size
= 0;
861 DEBUG(1, ("Forking LSA Service Daemon\n"));
864 * Block signals before forking child as it will have to
865 * set its own handlers. Child will re-enable SIGHUP as
866 * soon as the handlers are set up.
868 BlockSignals(true, SIGTERM
);
869 BlockSignals(true, SIGHUP
);
873 DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
878 /* parent or error */
881 /* Re-enable SIGHUP before returnig */
882 BlockSignals(false, SIGTERM
);
883 BlockSignals(false, SIGHUP
);
888 /* save the parent process id so the children can use it later */
889 parent_id
= procid_self();
891 status
= reinit_after_fork(msg_ctx
,
894 if (!NT_STATUS_IS_OK(status
)) {
895 DEBUG(0,("reinit_after_fork() failed\n"));
896 smb_panic("reinit_after_fork() failed");
899 lsasd_reopen_logs(0);
900 pfh_daemon_config(DAEMON_NAME
,
902 &default_pf_lsasd_cfg
);
904 lsasd_setup_sig_term_handler(ev_ctx
);
905 lsasd_setup_sig_hup_handler(ev_ctx
);
907 BlockSignals(false, SIGTERM
);
908 BlockSignals(false, SIGHUP
);
910 ok
= lsasd_create_sockets(ev_ctx
, msg_ctx
, listen_fd
, &listen_fd_size
);
915 /* start children before any more initialization is done */
916 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
921 pf_lsasd_cfg
.min_children
,
922 pf_lsasd_cfg
.max_children
,
923 &lsasd_children_main
,
930 if (!serverid_register(procid_self(), FLAG_MSG_GENERAL
)) {
934 messaging_register(msg_ctx
,
936 MSG_SMB_CONF_UPDATED
,
937 lsasd_smb_conf_updated
);
938 messaging_register(msg_ctx
, ev_ctx
,
939 MSG_PREFORK_CHILD_EVENT
, child_ping
);
941 status
= rpc_lsarpc_init(NULL
);
942 if (!NT_STATUS_IS_OK(status
)) {
943 DEBUG(0, ("Failed to register lsarpc rpc inteface in lsasd! (%s)\n",
948 status
= rpc_samr_init(NULL
);
949 if (!NT_STATUS_IS_OK(status
)) {
950 DEBUG(0, ("Failed to register samr rpc inteface in lsasd! (%s)\n",
955 status
= rpc_netlogon_init(NULL
);
956 if (!NT_STATUS_IS_OK(status
)) {
957 DEBUG(0, ("Failed to register netlogon rpc inteface in lsasd! (%s)\n",
962 ok
= lsasd_setup_children_monitor(ev_ctx
, msg_ctx
);
964 DEBUG(0, ("Failed to setup children monitoring!\n"));
968 DEBUG(1, ("LSASD Daemon Started (%d)\n", getpid()));
971 rc
= tevent_loop_wait(ev_ctx
);
973 /* should not be reached */
974 DEBUG(0,("lsasd: tevent_loop_wait() exited with %d - %s\n",
975 rc
, (rc
== 0) ? "out of events" : strerror(errno
)));