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/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
,
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());
71 rc
= asprintf(&extension
, "%s.%d", DAEMON_NAME
, child_id
);
73 rc
= asprintf(&extension
, "%s", DAEMON_NAME
);
80 if (lfile
== NULL
|| lfile
[0] == '\0') {
81 rc
= asprintf(&lfile
, "%s/log.%s",
82 get_dyn_LOGFILEBASE(), extension
);
84 if (strstr(lfile
, extension
) == NULL
) {
86 rc
= asprintf(&lfile
, "%s.%d",
87 lp_logfile(talloc_tos()),
90 rc
= asprintf(&lfile
, "%s.%s",
91 lp_logfile(talloc_tos()),
98 lp_set_logfile(lfile
);
102 SAFE_FREE(extension
);
107 static void lsasd_smb_conf_updated(struct messaging_context
*msg
,
110 struct server_id server_id
,
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
,
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
,
137 rpc_netlogon_shutdown();
139 rpc_lsarpc_shutdown();
141 DEBUG(0, ("termination signal\n"));
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
,
152 lsasd_sig_term_handler
,
155 DEBUG(0, ("failed to setup SIGTERM handler\n"));
160 static void lsasd_sig_hup_handler(struct tevent_context
*ev
,
161 struct tevent_signal
*se
,
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
,
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
,
187 lsasd_sig_hup_handler
,
190 DEBUG(0, ("failed to setup SIGHUP handler\n"));
195 /**********************************************************
197 **********************************************************/
199 static void lsasd_chld_sig_hup_handler(struct tevent_context
*ev
,
200 struct tevent_signal
*se
,
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
,
217 lsasd_chld_sig_hup_handler
,
220 DEBUG(1, ("failed to setup SIGHUP handler"));
227 static void parent_ping(struct messaging_context
*msg_ctx
,
230 struct server_id server_id
,
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"));
243 static bool lsasd_child_init(struct tevent_context
*ev_ctx
,
245 struct pf_worker_data
*pf
)
248 struct messaging_context
*msg_ctx
= server_messaging_context();
251 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
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
);
268 if (!serverid_register(messaging_server_id(msg_ctx
),
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",
286 status
= rpc_samr_init(NULL
);
287 if (!NT_STATUS_IS_OK(status
)) {
288 DEBUG(0, ("Failed to register samr rpc inteface! (%s)\n",
293 status
= rpc_netlogon_init(NULL
);
294 if (!NT_STATUS_IS_OK(status
)) {
295 DEBUG(0, ("Failed to register netlogon rpc inteface! (%s)\n",
303 struct lsasd_children_data
{
304 struct tevent_context
*ev_ctx
;
305 struct messaging_context
*msg_ctx
;
306 struct pf_worker_data
*pf
;
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
,
321 struct lsasd_children_data
*data
;
325 ok
= lsasd_child_init(ev_ctx
, child_id
, pf
);
330 data
= talloc(ev_ctx
, struct lsasd_children_data
);
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
);
347 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
348 ret
, strerror(errno
)));
349 pf
->status
= PF_WORKER_EXITING
;
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 */
387 next
= talloc_zero(data
, struct lsasd_new_client
);
389 DEBUG(1, ("Out of memory!?\n"));
394 req
= prefork_listen_send(next
,
397 data
->listen_fd_size
,
400 DEBUG(1, ("Failed to make listening request!?\n"));
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
;
415 struct tsocket_address
*srv_addr
;
416 struct tsocket_address
*cli_addr
;
418 client
= tevent_req_callback_data(req
, struct lsasd_new_client
);
421 tmp_ctx
= talloc_stackframe();
422 if (tmp_ctx
== NULL
) {
423 DEBUG(1, ("Failed to allocate stackframe!\n"));
427 rc
= prefork_listen_recv(req
,
433 /* this will free the request too */
437 DEBUG(6, ("No client connection was available after all!\n"));
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
,
461 } else if (tsocket_address_is_unix(srv_addr
)) {
465 p
= tsocket_address_unix_path(srv_addr
, tmp_ctx
);
467 talloc_free(tmp_ctx
);
478 if (strstr(p
, "/np/")) {
479 named_pipe_accept_function(data
->ev_ctx
,
483 lsasd_client_terminated
,
486 dcerpc_ncacn_accept(data
->ev_ctx
,
496 DEBUG(0, ("ERROR: Unsupported socket!\n"));
500 talloc_free(tmp_ctx
);
507 static void child_ping(struct messaging_context
*msg_ctx
,
510 struct server_id server_id
,
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
,
530 static void lsasd_sigchld_handler(struct tevent_context
*ev_ctx
,
531 struct prefork_pool
*pfp
,
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
)
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());
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
);
571 DEBUG(2, ("Failed to set up children monitoring!\n"));
578 static void lsasd_check_children(struct tevent_context
*ev_ctx
,
579 struct tevent_timer
*te
,
580 struct timeval current_time
,
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
);
596 static bool lsasd_create_sockets(struct tevent_context
*ev_ctx
,
597 struct messaging_context
*msg_ctx
,
601 struct dcerpc_binding_vector
*v
, *v_orig
;
609 tmp_ctx
= talloc_stackframe();
610 if (tmp_ctx
== NULL
) {
614 status
= dcerpc_binding_vector_new(tmp_ctx
, &v_orig
);
615 if (!NT_STATUS_IS_OK(status
)) {
620 /* Create only one tcpip listener for all services */
621 status
= rpc_create_tcpip_sockets(&ndr_table_lsarpc
,
626 if (!NT_STATUS_IS_OK(status
)) {
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
);
635 DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
643 fd
= create_named_pipe_socket("lsarpc");
649 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
651 DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
656 listen_fd
[*listen_fd_size
] = fd
;
659 fd
= create_named_pipe_socket("lsass");
665 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
667 DEBUG(0, ("Failed to listen on lsass pipe - %s\n",
672 listen_fd
[*listen_fd_size
] = fd
;
675 fd
= create_dcerpc_ncalrpc_socket("lsarpc");
681 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
683 DEBUG(0, ("Failed to listen on lsarpc ncalrpc - %s\n",
688 listen_fd
[*listen_fd_size
] = fd
;
692 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
698 status
= dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc
, v
);
699 if (!NT_STATUS_IS_OK(status
)) {
703 status
= dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc
, v
);
704 if (!NT_STATUS_IS_OK(status
)) {
709 status
= dcerpc_binding_vector_add_unix(&ndr_table_lsarpc
, v
, "lsarpc");
710 if (!NT_STATUS_IS_OK(status
)) {
715 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_lsarpc
, v
);
716 if (!NT_STATUS_IS_OK(status
)) {
722 fd
= create_named_pipe_socket("samr");
728 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
730 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
735 listen_fd
[*listen_fd_size
] = fd
;
738 fd
= create_dcerpc_ncalrpc_socket("samr");
744 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
746 DEBUG(0, ("Failed to listen on samr ncalrpc - %s\n",
751 listen_fd
[*listen_fd_size
] = fd
;
755 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
761 status
= dcerpc_binding_vector_replace_iface(&ndr_table_samr
, v
);
762 if (!NT_STATUS_IS_OK(status
)) {
766 status
= dcerpc_binding_vector_add_np_default(&ndr_table_samr
, v
);
767 if (!NT_STATUS_IS_OK(status
)) {
772 status
= dcerpc_binding_vector_add_unix(&ndr_table_lsarpc
, v
, "samr");
773 if (!NT_STATUS_IS_OK(status
)) {
778 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_samr
, v
);
779 if (!NT_STATUS_IS_OK(status
)) {
785 fd
= create_named_pipe_socket("netlogon");
791 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
793 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
798 listen_fd
[*listen_fd_size
] = fd
;
801 fd
= create_dcerpc_ncalrpc_socket("netlogon");
807 rc
= listen(fd
, pf_lsasd_cfg
.max_allowed_clients
);
809 DEBUG(0, ("Failed to listen on netlogon ncalrpc - %s\n",
815 listen_fd
[*listen_fd_size
] = fd
;
819 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
825 status
= dcerpc_binding_vector_replace_iface(&ndr_table_netlogon
, v
);
826 if (!NT_STATUS_IS_OK(status
)) {
830 status
= dcerpc_binding_vector_add_np_default(&ndr_table_netlogon
, v
);
831 if (!NT_STATUS_IS_OK(status
)) {
836 status
= dcerpc_binding_vector_add_unix(&ndr_table_lsarpc
, v
, "netlogon");
837 if (!NT_STATUS_IS_OK(status
)) {
842 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_netlogon
, v
);
843 if (!NT_STATUS_IS_OK(status
)) {
852 talloc_free(tmp_ctx
);
856 void start_lsasd(struct tevent_context
*ev_ctx
,
857 struct messaging_context
*msg_ctx
)
860 int listen_fd
[LSASD_MAX_SOCKETS
];
861 int listen_fd_size
= 0;
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
);
878 DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
883 /* parent or error */
886 /* Re-enable SIGHUP before returnig */
887 BlockSignals(false, SIGTERM
);
888 BlockSignals(false, SIGHUP
);
893 status
= reinit_after_fork(msg_ctx
,
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
,
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
);
922 /* start children before any more initialization is done */
923 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
928 pf_lsasd_cfg
.min_children
,
929 pf_lsasd_cfg
.max_children
,
930 &lsasd_children_main
,
937 if (!serverid_register(messaging_server_id(msg_ctx
),
942 messaging_register(msg_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",
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",
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",
970 ok
= lsasd_setup_children_monitor(ev_ctx
, msg_ctx
);
972 DEBUG(0, ("Failed to setup children monitoring!\n"));
976 DEBUG(1, ("LSASD Daemon Started (%u)\n", (unsigned int)getpid()));
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
)));