2 Unix SMB/CIFS implementation.
3 Main SMB server routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Martin Pool 2002
6 Copyright (C) Jelmer Vernooij 2002-2003
7 Copyright (C) Volker Lendecke 1993-2007
8 Copyright (C) Jeremy Allison 1993-2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/filesys.h"
26 #include "lib/util/server_id.h"
27 #include "popt_common.h"
28 #include "locking/share_mode_lock.h"
29 #include "smbd/smbd.h"
30 #include "smbd/globals.h"
31 #include "registry/reg_init_full.h"
32 #include "libcli/auth/schannel.h"
34 #include "../lib/util/memcache.h"
35 #include "ctdbd_conn.h"
36 #include "util_cluster.h"
37 #include "printing/queue_process.h"
38 #include "rpc_server/rpc_service_setup.h"
39 #include "rpc_server/rpc_config.h"
43 #include "messages_ctdb.h"
44 #include "smbprofile.h"
45 #include "lib/id_cache.h"
46 #include "lib/param/param.h"
47 #include "lib/background.h"
48 #include "../lib/util/pidfile.h"
49 #include "lib/smbd_shim.h"
50 #include "scavenger.h"
51 #include "locking/leases_db.h"
52 #include "smbd/notifyd/notifyd.h"
53 #include "smbd/smbd_cleanupd.h"
54 #include "lib/util/sys_rw.h"
55 #include "cleanupdb.h"
57 #include "rpc_server/epmd.h"
58 #include "rpc_server/lsasd.h"
59 #include "rpc_server/fssd.h"
60 #include "rpc_server/mdssd.h"
61 #include "lib/global_contexts.h"
63 #ifdef CLUSTER_SUPPORT
64 #include "ctdb_protocol.h"
67 struct smbd_open_socket
;
68 struct smbd_child_pid
;
70 struct smbd_parent_context
{
73 struct tevent_context
*ev_ctx
;
74 struct messaging_context
*msg_ctx
;
75 struct dcesrv_context
*dce_ctx
;
77 /* the list of listening sockets */
78 struct smbd_open_socket
*sockets
;
80 /* the list of current child processes */
81 struct smbd_child_pid
*children
;
84 struct server_id cleanupd
;
85 struct server_id notifyd
;
87 struct tevent_timer
*cleanup_te
;
90 struct smbd_open_socket
{
91 struct smbd_open_socket
*prev
, *next
;
92 struct smbd_parent_context
*parent
;
94 struct tevent_fd
*fde
;
97 struct smbd_child_pid
{
98 struct smbd_child_pid
*prev
, *next
;
102 /*******************************************************************
103 What to do when smb.conf is updated.
104 ********************************************************************/
106 static NTSTATUS
messaging_send_to_children(struct messaging_context
*msg_ctx
,
107 uint32_t msg_type
, DATA_BLOB
* data
);
109 static void smbd_parent_conf_updated(struct messaging_context
*msg
,
112 struct server_id server_id
,
115 struct tevent_context
*ev_ctx
=
116 talloc_get_type_abort(private_data
, struct tevent_context
);
119 DEBUG(10,("smbd_parent_conf_updated: Got message saying smb.conf was "
120 "updated. Reloading.\n"));
121 change_to_root_user();
122 reload_services(NULL
, NULL
, false);
123 printing_subsystem_update(ev_ctx
, msg
, false);
125 ok
= reinit_guest_session_info(NULL
);
127 DBG_ERR("Failed to reinit guest info\n");
129 messaging_send_to_children(msg
, MSG_SMB_CONF_UPDATED
, NULL
);
132 /*******************************************************************
133 Delete a statcache entry.
134 ********************************************************************/
136 static void smb_stat_cache_delete(struct messaging_context
*msg
,
139 struct server_id server_id
,
142 const char *name
= (const char *)data
->data
;
143 DEBUG(10,("smb_stat_cache_delete: delete name %s\n", name
));
144 stat_cache_delete(name
);
147 /****************************************************************************
148 Send a SIGTERM to our process group.
149 *****************************************************************************/
151 static void killkids(void)
153 if(am_parent
) kill(0,SIGTERM
);
156 static void msg_exit_server(struct messaging_context
*msg
,
159 struct server_id server_id
,
162 DEBUG(3, ("got a SHUTDOWN message\n"));
163 exit_server_cleanly(NULL
);
167 static void msg_inject_fault(struct messaging_context
*msg
,
170 struct server_id src
,
174 struct server_id_buf tmp
;
176 if (data
->length
!= sizeof(sig
)) {
177 DEBUG(0, ("Process %s sent bogus signal injection request\n",
178 server_id_str_buf(src
, &tmp
)));
182 sig
= *(int *)data
->data
;
184 exit_server("internal error injected");
188 #ifdef HAVE_STRSIGNAL
189 DEBUG(0, ("Process %s requested injection of signal %d (%s)\n",
190 server_id_str_buf(src
, &tmp
), sig
, strsignal(sig
)));
192 DEBUG(0, ("Process %s requested injection of signal %d\n",
193 server_id_str_buf(src
, &tmp
), sig
));
198 #endif /* DEVELOPER */
200 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
202 * Sleep for the specified number of seconds.
204 static void msg_sleep(struct messaging_context
*msg
,
207 struct server_id src
,
210 unsigned int seconds
;
211 struct server_id_buf tmp
;
213 if (data
->length
!= sizeof(seconds
)) {
214 DBG_ERR("Process %s sent bogus sleep request\n",
215 server_id_str_buf(src
, &tmp
));
219 seconds
= *(unsigned int *)data
->data
;
220 DBG_ERR("Process %s request a sleep of %u seconds\n",
221 server_id_str_buf(src
, &tmp
),
224 DBG_ERR("Restarting after %u second sleep requested by process %s\n",
226 server_id_str_buf(src
, &tmp
));
228 #endif /* DEVELOPER */
230 static NTSTATUS
messaging_send_to_children(struct messaging_context
*msg_ctx
,
231 uint32_t msg_type
, DATA_BLOB
* data
)
234 struct smbd_parent_context
*parent
= am_parent
;
235 struct smbd_child_pid
*child
;
237 if (parent
== NULL
) {
238 return NT_STATUS_INTERNAL_ERROR
;
241 for (child
= parent
->children
; child
!= NULL
; child
= child
->next
) {
242 status
= messaging_send(parent
->msg_ctx
,
243 pid_to_procid(child
->pid
),
245 if (!NT_STATUS_IS_OK(status
)) {
246 DBG_DEBUG("messaging_send(%d) failed: %s\n",
247 (int)child
->pid
, nt_errstr(status
));
253 static void smb_parent_send_to_children(struct messaging_context
*ctx
,
256 struct server_id srv_id
,
259 messaging_send_to_children(ctx
, msg_type
, msg_data
);
263 * Parent smbd process sets its own debug level first and then
264 * sends a message to all the smbd children to adjust their debug
265 * level to that of the parent.
268 static void smbd_msg_debug(struct messaging_context
*msg_ctx
,
271 struct server_id server_id
,
274 debug_message(msg_ctx
, private_data
, MSG_DEBUG
, server_id
, data
);
276 messaging_send_to_children(msg_ctx
, MSG_DEBUG
, data
);
279 static void smbd_parent_id_cache_kill(struct messaging_context
*msg_ctx
,
282 struct server_id server_id
,
285 const char *msg
= (data
&& data
->data
)
286 ? (const char *)data
->data
: "<NULL>";
287 struct id_cache_ref id
;
289 if (!id_cache_ref_parse(msg
, &id
)) {
290 DEBUG(0, ("Invalid ?ID: %s\n", msg
));
294 id_cache_delete_from_cache(&id
);
296 messaging_send_to_children(msg_ctx
, msg_type
, data
);
299 static void smbd_parent_id_cache_delete(struct messaging_context
*ctx
,
302 struct server_id srv_id
,
305 id_cache_delete_message(ctx
, data
, msg_type
, srv_id
, msg_data
);
307 messaging_send_to_children(ctx
, msg_type
, msg_data
);
310 static void add_child_pid(struct smbd_parent_context
*parent
,
313 struct smbd_child_pid
*child
;
315 child
= talloc_zero(parent
, struct smbd_child_pid
);
317 DEBUG(0, ("Could not add child struct -- malloc failed\n"));
321 DLIST_ADD(parent
->children
, child
);
322 parent
->num_children
+= 1;
325 static void smb_tell_num_children(struct messaging_context
*ctx
, void *data
,
326 uint32_t msg_type
, struct server_id srv_id
,
329 uint8_t buf
[sizeof(uint32_t)];
332 SIVAL(buf
, 0, am_parent
->num_children
);
333 messaging_send_buf(ctx
, srv_id
, MSG_SMB_NUM_CHILDREN
,
338 static void notifyd_stopped(struct tevent_req
*req
);
340 static struct tevent_req
*notifyd_req(struct messaging_context
*msg_ctx
,
341 struct tevent_context
*ev
)
343 struct tevent_req
*req
;
344 sys_notify_watch_fn sys_notify_watch
= NULL
;
345 struct sys_notify_context
*sys_notify_ctx
= NULL
;
346 struct ctdbd_connection
*ctdbd_conn
= NULL
;
348 if (lp_kernel_change_notify()) {
351 if (lp_parm_bool(-1, "notify", "inotify", true)) {
352 sys_notify_watch
= inotify_watch
;
357 if (lp_parm_bool(-1, "notify", "fam",
358 (sys_notify_watch
== NULL
))) {
359 sys_notify_watch
= fam_watch
;
364 if (sys_notify_watch
!= NULL
) {
365 sys_notify_ctx
= sys_notify_context_create(msg_ctx
, ev
);
366 if (sys_notify_ctx
== NULL
) {
371 if (lp_clustering()) {
372 ctdbd_conn
= messaging_ctdb_connection();
375 req
= notifyd_send(msg_ctx
, ev
, msg_ctx
, ctdbd_conn
,
376 sys_notify_watch
, sys_notify_ctx
);
378 TALLOC_FREE(sys_notify_ctx
);
381 tevent_req_set_callback(req
, notifyd_stopped
, msg_ctx
);
386 static void notifyd_stopped(struct tevent_req
*req
)
390 ret
= notifyd_recv(req
);
392 DEBUG(1, ("notifyd stopped: %s\n", strerror(ret
)));
395 static void notifyd_sig_hup_handler(struct tevent_context
*ev
,
396 struct tevent_signal
*se
,
402 DBG_NOTICE("notifyd: Reloading services after SIGHUP\n");
403 reload_services(NULL
, NULL
, false);
406 static bool smbd_notifyd_init(struct messaging_context
*msg
, bool interactive
,
407 struct server_id
*ppid
)
409 struct tevent_context
*ev
= messaging_tevent_context(msg
);
410 struct tevent_req
*req
;
414 struct tevent_signal
*se
;
417 req
= notifyd_req(msg
, ev
);
418 return (req
!= NULL
);
423 DEBUG(1, ("%s: fork failed: %s\n", __func__
,
429 if (am_parent
!= NULL
) {
430 add_child_pid(am_parent
, pid
);
432 *ppid
= pid_to_procid(pid
);
436 status
= smbd_reinit_after_fork(msg
, ev
, true, "smbd-notifyd");
437 if (!NT_STATUS_IS_OK(status
)) {
438 DEBUG(1, ("%s: reinit_after_fork failed: %s\n",
439 __func__
, nt_errstr(status
)));
445 /* Set up sighup handler for notifyd */
446 se
= tevent_add_signal(ev
,
449 notifyd_sig_hup_handler
,
452 DEBUG(0, ("failed to setup notifyd SIGHUP handler\n"));
456 req
= notifyd_req(msg
, ev
);
460 tevent_req_set_callback(req
, notifyd_stopped
, msg
);
462 /* Block those signals that we are not handling */
463 BlockSignals(True
, SIGUSR1
);
465 messaging_send(msg
, pid_to_procid(getppid()), MSG_SMB_NOTIFY_STARTED
,
468 ok
= tevent_req_poll(req
, ev
);
470 DBG_WARNING("tevent_req_poll returned %s\n", strerror(errno
));
476 static void notifyd_init_trigger(struct tevent_req
*req
);
478 struct notifyd_init_state
{
480 struct tevent_context
*ev
;
481 struct messaging_context
*msg
;
482 struct server_id
*ppid
;
485 static struct tevent_req
*notifyd_init_send(struct tevent_context
*ev
,
487 struct messaging_context
*msg
,
488 struct server_id
*ppid
)
490 struct tevent_req
*req
= NULL
;
491 struct tevent_req
*subreq
= NULL
;
492 struct notifyd_init_state
*state
= NULL
;
494 req
= tevent_req_create(mem_ctx
, &state
, struct notifyd_init_state
);
499 *state
= (struct notifyd_init_state
) {
505 subreq
= tevent_wakeup_send(state
, ev
, tevent_timeval_current_ofs(1, 0));
506 if (tevent_req_nomem(subreq
, req
)) {
507 return tevent_req_post(req
, ev
);
510 tevent_req_set_callback(subreq
, notifyd_init_trigger
, req
);
514 static void notifyd_init_trigger(struct tevent_req
*subreq
)
516 struct tevent_req
*req
= tevent_req_callback_data(
517 subreq
, struct tevent_req
);
518 struct notifyd_init_state
*state
= tevent_req_data(
519 req
, struct notifyd_init_state
);
522 DBG_NOTICE("Triggering notifyd startup\n");
524 ok
= tevent_wakeup_recv(subreq
);
527 tevent_req_error(req
, ENOMEM
);
531 state
->ok
= smbd_notifyd_init(state
->msg
, false, state
->ppid
);
533 DBG_WARNING("notifyd restarted\n");
534 tevent_req_done(req
);
538 DBG_NOTICE("notifyd startup failed, rescheduling\n");
540 subreq
= tevent_wakeup_send(state
, state
->ev
,
541 tevent_timeval_current_ofs(1, 0));
542 if (tevent_req_nomem(subreq
, req
)) {
543 DBG_ERR("scheduling notifyd restart failed, giving up\n");
547 tevent_req_set_callback(subreq
, notifyd_init_trigger
, req
);
551 static bool notifyd_init_recv(struct tevent_req
*req
)
553 struct notifyd_init_state
*state
= tevent_req_data(
554 req
, struct notifyd_init_state
);
559 static void notifyd_started(struct tevent_req
*req
)
563 ok
= notifyd_init_recv(req
);
566 DBG_ERR("Failed to restart notifyd, giving up\n");
571 static void cleanupd_stopped(struct tevent_req
*req
);
573 static bool cleanupd_init(struct messaging_context
*msg
, bool interactive
,
574 struct server_id
*ppid
)
576 struct tevent_context
*ev
= messaging_tevent_context(msg
);
577 struct server_id parent_id
= messaging_server_id(msg
);
578 struct tevent_req
*req
;
588 req
= smbd_cleanupd_send(msg
, ev
, msg
, parent_id
.pid
);
589 *ppid
= messaging_server_id(msg
);
590 return (req
!= NULL
);
595 DBG_WARNING("pipe failed: %s\n", strerror(errno
));
601 DBG_WARNING("fork failed: %s\n", strerror(errno
));
610 rwret
= sys_read(up_pipe
[0], &c
, 1);
614 DBG_WARNING("sys_read failed: %s\n", strerror(errno
));
618 DBG_WARNING("cleanupd could not start\n");
622 DBG_WARNING("cleanupd returned %d\n", (int)c
);
626 DBG_DEBUG("Started cleanupd pid=%d\n", (int)pid
);
628 if (am_parent
!= NULL
) {
629 add_child_pid(am_parent
, pid
);
632 *ppid
= pid_to_procid(pid
);
638 status
= smbd_reinit_after_fork(msg
, ev
, true, "cleanupd");
639 if (!NT_STATUS_IS_OK(status
)) {
640 DBG_WARNING("reinit_after_fork failed: %s\n",
643 sys_write(up_pipe
[1], &c
, 1);
648 req
= smbd_cleanupd_send(msg
, ev
, msg
, parent_id
.pid
);
650 DBG_WARNING("smbd_cleanupd_send failed\n");
652 sys_write(up_pipe
[1], &c
, 1);
657 tevent_req_set_callback(req
, cleanupd_stopped
, msg
);
660 rwret
= sys_write(up_pipe
[1], &c
, 1);
664 DBG_WARNING("sys_write failed: %s\n", strerror(errno
));
668 DBG_WARNING("sys_write could not write result\n");
672 ok
= tevent_req_poll(req
, ev
);
674 DBG_WARNING("tevent_req_poll returned %s\n", strerror(errno
));
679 static void cleanupd_stopped(struct tevent_req
*req
)
683 status
= smbd_cleanupd_recv(req
);
684 DBG_WARNING("cleanupd stopped: %s\n", nt_errstr(status
));
687 static void cleanupd_init_trigger(struct tevent_req
*req
);
689 struct cleanup_init_state
{
691 struct tevent_context
*ev
;
692 struct messaging_context
*msg
;
693 struct server_id
*ppid
;
696 static struct tevent_req
*cleanupd_init_send(struct tevent_context
*ev
,
698 struct messaging_context
*msg
,
699 struct server_id
*ppid
)
701 struct tevent_req
*req
= NULL
;
702 struct tevent_req
*subreq
= NULL
;
703 struct cleanup_init_state
*state
= NULL
;
705 req
= tevent_req_create(mem_ctx
, &state
, struct cleanup_init_state
);
710 *state
= (struct cleanup_init_state
) {
716 subreq
= tevent_wakeup_send(state
, ev
, tevent_timeval_current_ofs(0, 0));
717 if (tevent_req_nomem(subreq
, req
)) {
718 return tevent_req_post(req
, ev
);
721 tevent_req_set_callback(subreq
, cleanupd_init_trigger
, req
);
725 static void cleanupd_init_trigger(struct tevent_req
*subreq
)
727 struct tevent_req
*req
= tevent_req_callback_data(
728 subreq
, struct tevent_req
);
729 struct cleanup_init_state
*state
= tevent_req_data(
730 req
, struct cleanup_init_state
);
733 DBG_NOTICE("Triggering cleanupd startup\n");
735 ok
= tevent_wakeup_recv(subreq
);
738 tevent_req_error(req
, ENOMEM
);
742 state
->ok
= cleanupd_init(state
->msg
, false, state
->ppid
);
744 DBG_WARNING("cleanupd restarted\n");
745 tevent_req_done(req
);
749 DBG_NOTICE("cleanupd startup failed, rescheduling\n");
751 subreq
= tevent_wakeup_send(state
, state
->ev
,
752 tevent_timeval_current_ofs(1, 0));
753 if (tevent_req_nomem(subreq
, req
)) {
754 DBG_ERR("scheduling cleanupd restart failed, giving up\n");
758 tevent_req_set_callback(subreq
, cleanupd_init_trigger
, req
);
762 static bool cleanupd_init_recv(struct tevent_req
*req
)
764 struct cleanup_init_state
*state
= tevent_req_data(
765 req
, struct cleanup_init_state
);
770 static void cleanupd_started(struct tevent_req
*req
)
774 struct smbd_parent_context
*parent
= tevent_req_callback_data(
775 req
, struct smbd_parent_context
);
777 ok
= cleanupd_init_recv(req
);
780 DBG_ERR("Failed to restart cleanupd, giving up\n");
784 status
= messaging_send(parent
->msg_ctx
,
786 MSG_SMB_NOTIFY_CLEANUP
,
788 if (!NT_STATUS_IS_OK(status
)) {
789 DBG_ERR("messaging_send returned %s\n",
794 static void remove_child_pid(struct smbd_parent_context
*parent
,
796 bool unclean_shutdown
)
798 struct smbd_child_pid
*child
;
802 for (child
= parent
->children
; child
!= NULL
; child
= child
->next
) {
803 if (child
->pid
== pid
) {
804 struct smbd_child_pid
*tmp
= child
;
805 DLIST_REMOVE(parent
->children
, child
);
807 parent
->num_children
-= 1;
813 /* not all forked child processes are added to the children list */
814 DEBUG(2, ("Could not find child %d -- ignoring\n", (int)pid
));
818 if (pid
== procid_to_pid(&parent
->cleanupd
)) {
819 struct tevent_req
*req
;
821 server_id_set_disconnected(&parent
->cleanupd
);
823 DBG_WARNING("Restarting cleanupd\n");
824 req
= cleanupd_init_send(messaging_tevent_context(parent
->msg_ctx
),
829 DBG_ERR("Failed to restart cleanupd\n");
832 tevent_req_set_callback(req
, cleanupd_started
, parent
);
836 if (pid
== procid_to_pid(&parent
->notifyd
)) {
837 struct tevent_req
*req
;
838 struct tevent_context
*ev
= messaging_tevent_context(
841 server_id_set_disconnected(&parent
->notifyd
);
843 DBG_WARNING("Restarting notifyd\n");
844 req
= notifyd_init_send(ev
,
849 DBG_ERR("Failed to restart notifyd\n");
852 tevent_req_set_callback(req
, notifyd_started
, parent
);
856 ok
= cleanupdb_store_child(pid
, unclean_shutdown
);
858 DBG_ERR("cleanupdb_store_child failed\n");
862 if (!server_id_is_disconnected(&parent
->cleanupd
)) {
863 status
= messaging_send(parent
->msg_ctx
,
865 MSG_SMB_NOTIFY_CLEANUP
,
867 if (!NT_STATUS_IS_OK(status
)) {
868 DBG_ERR("messaging_send returned %s\n",
874 /****************************************************************************
875 Have we reached the process limit ?
876 ****************************************************************************/
878 static bool allowable_number_of_smbd_processes(struct smbd_parent_context
*parent
)
880 int max_processes
= lp_max_smbd_processes();
885 return parent
->num_children
< max_processes
;
888 static void smbd_sig_chld_handler(struct tevent_context
*ev
,
889 struct tevent_signal
*se
,
897 struct smbd_parent_context
*parent
=
898 talloc_get_type_abort(private_data
,
899 struct smbd_parent_context
);
901 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0) {
902 bool unclean_shutdown
= False
;
904 /* If the child terminated normally, assume
905 it was an unclean shutdown unless the
908 if (WIFEXITED(status
)) {
909 unclean_shutdown
= WEXITSTATUS(status
);
911 /* If the child terminated due to a signal
912 we always assume it was unclean.
914 if (WIFSIGNALED(status
)) {
915 unclean_shutdown
= True
;
917 remove_child_pid(parent
, pid
, unclean_shutdown
);
921 static void smbd_setup_sig_chld_handler(struct smbd_parent_context
*parent
)
923 struct tevent_signal
*se
;
925 se
= tevent_add_signal(parent
->ev_ctx
,
926 parent
, /* mem_ctx */
928 smbd_sig_chld_handler
,
931 exit_server("failed to setup SIGCHLD handler");
935 static void smbd_open_socket_close_fn(struct tevent_context
*ev
,
936 struct tevent_fd
*fde
,
940 /* this might be the socket_wrapper swrap_close() */
944 static void smbd_accept_connection(struct tevent_context
*ev
,
945 struct tevent_fd
*fde
,
949 struct smbd_open_socket
*s
= talloc_get_type_abort(private_data
,
950 struct smbd_open_socket
);
951 struct messaging_context
*msg_ctx
= s
->parent
->msg_ctx
;
952 struct dcesrv_context
*dce_ctx
= s
->parent
->dce_ctx
;
953 struct sockaddr_storage addr
;
954 socklen_t in_addrlen
= sizeof(addr
);
958 fd
= accept(s
->fd
, (struct sockaddr
*)(void *)&addr
,&in_addrlen
);
959 if (fd
== -1 && errno
== EINTR
)
963 DEBUG(0,("accept: %s\n",
967 smb_set_close_on_exec(fd
);
969 if (s
->parent
->interactive
) {
970 reinit_after_fork(msg_ctx
, ev
, true, NULL
);
971 smbd_process(ev
, msg_ctx
, dce_ctx
, fd
, true);
972 exit_server_cleanly("end of interactive mode");
976 if (!allowable_number_of_smbd_processes(s
->parent
)) {
983 NTSTATUS status
= NT_STATUS_OK
;
986 * Can't use TALLOC_FREE here. Nulling out the argument to it
987 * would overwrite memory we've just freed.
989 talloc_free(s
->parent
);
992 /* Stop zombies, the parent explicitly handles
993 * them, counting worker smbds. */
996 status
= smbd_reinit_after_fork(msg_ctx
, ev
, true, NULL
);
997 if (!NT_STATUS_IS_OK(status
)) {
998 if (NT_STATUS_EQUAL(status
,
999 NT_STATUS_TOO_MANY_OPENED_FILES
)) {
1000 DEBUG(0,("child process cannot initialize "
1001 "because too many files are open\n"));
1004 if (lp_clustering() &&
1006 status
, NT_STATUS_INTERNAL_DB_ERROR
) ||
1008 status
, NT_STATUS_CONNECTION_REFUSED
))) {
1009 DEBUG(1, ("child process cannot initialize "
1010 "because connection to CTDB "
1012 nt_errstr(status
)));
1016 DEBUG(0,("reinit_after_fork() failed\n"));
1017 smb_panic("reinit_after_fork() failed");
1020 smbd_process(ev
, msg_ctx
, dce_ctx
, fd
, false);
1022 exit_server_cleanly("end of child");
1027 DEBUG(0,("smbd_accept_connection: fork() failed: %s\n",
1031 /* The parent doesn't need this socket */
1034 /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
1035 Clear the closed fd info out of server_fd --
1036 and more importantly, out of client_fd in
1037 util_sock.c, to avoid a possible
1038 getpeername failure if we reopen the logs
1039 and use %I in the filename.
1043 add_child_pid(s
->parent
, pid
);
1046 /* Force parent to check log size after
1047 * spawning child. Fix from
1048 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
1049 * parent smbd will log to logserver.smb. It
1050 * writes only two messages for each child
1051 * started/finished. But each child writes,
1052 * say, 50 messages also in logserver.smb,
1053 * beginning with the debug_count of the
1054 * parent, before the child opens its own log
1055 * file logserver.client. In a worst case
1056 * scenario the size of logserver.smb would be
1057 * checked after about 50*50=2500 messages
1060 force_check_log_size();
1063 static bool smbd_open_one_socket(struct smbd_parent_context
*parent
,
1064 struct tevent_context
*ev_ctx
,
1065 const struct sockaddr_storage
*ifss
,
1068 struct smbd_open_socket
*s
;
1070 s
= talloc(parent
, struct smbd_open_socket
);
1076 s
->fd
= open_socket_in(SOCK_STREAM
,
1078 parent
->sockets
== NULL
? 0 : 2,
1082 DEBUG(0,("smbd_open_one_socket: open_socket_in: "
1083 "%s\n", strerror(errno
)));
1086 * We ignore an error here, as we've done before
1091 /* ready to listen */
1092 set_socket_options(s
->fd
, "SO_KEEPALIVE");
1093 set_socket_options(s
->fd
, lp_socket_options());
1095 /* Set server socket to
1096 * non-blocking for the accept. */
1097 set_blocking(s
->fd
, False
);
1099 if (listen(s
->fd
, SMBD_LISTEN_BACKLOG
) == -1) {
1100 DEBUG(0,("smbd_open_one_socket: listen: "
1101 "%s\n", strerror(errno
)));
1107 s
->fde
= tevent_add_fd(ev_ctx
,
1109 s
->fd
, TEVENT_FD_READ
,
1110 smbd_accept_connection
,
1113 DEBUG(0,("smbd_open_one_socket: "
1114 "tevent_add_fd: %s\n",
1120 tevent_fd_set_close_fn(s
->fde
, smbd_open_socket_close_fn
);
1122 DLIST_ADD_END(parent
->sockets
, s
);
1127 /****************************************************************************
1128 Open the socket communication.
1129 ****************************************************************************/
1131 static bool open_sockets_smbd(struct smbd_parent_context
*parent
,
1132 struct tevent_context
*ev_ctx
,
1133 struct messaging_context
*msg_ctx
,
1134 const char *smb_ports
)
1136 int num_interfaces
= iface_count();
1139 unsigned dns_port
= 0;
1146 smbd_setup_sig_chld_handler(parent
);
1148 ports
= lp_smb_ports();
1150 /* use a reasonable default set of ports - listing on 445 and 139 */
1153 l
= str_list_make_v3(talloc_tos(), smb_ports
, NULL
);
1154 ports
= discard_const_p(const char *, l
);
1157 for (j
= 0; ports
&& ports
[j
]; j
++) {
1158 unsigned port
= atoi(ports
[j
]);
1160 if (port
== 0 || port
> 0xffff) {
1161 exit_server_cleanly("Invalid port in the config or on "
1162 "the commandline specified!");
1166 if (lp_interfaces() && lp_bind_interfaces_only()) {
1167 /* We have been given an interfaces line, and been
1168 told to only bind to those interfaces. Create a
1169 socket per interface and bind to only these.
1172 /* Now open a listen socket for each of the
1174 for(i
= 0; i
< num_interfaces
; i
++) {
1175 const struct sockaddr_storage
*ifss
=
1176 iface_n_sockaddr_storage(i
);
1178 DEBUG(0,("open_sockets_smbd: "
1179 "interface %d has NULL IP address !\n",
1184 for (j
= 0; ports
&& ports
[j
]; j
++) {
1185 unsigned port
= atoi(ports
[j
]);
1187 /* Keep the first port for mDNS service
1190 if (dns_port
== 0) {
1194 if (!smbd_open_one_socket(parent
,
1203 /* Just bind to 0.0.0.0 - accept connections
1206 const char *sock_addr
;
1208 const char *sock_ptr
;
1211 sock_addr
= "::,0.0.0.0";
1213 sock_addr
= "0.0.0.0";
1216 for (sock_ptr
=sock_addr
;
1217 next_token_talloc(talloc_tos(), &sock_ptr
, &sock_tok
, " \t,"); ) {
1218 for (j
= 0; ports
&& ports
[j
]; j
++) {
1219 struct sockaddr_storage ss
;
1220 unsigned port
= atoi(ports
[j
]);
1222 /* Keep the first port for mDNS service
1225 if (dns_port
== 0) {
1229 /* open an incoming socket */
1230 if (!interpret_string_addr(&ss
, sock_tok
,
1231 AI_NUMERICHOST
|AI_PASSIVE
)) {
1236 * If we fail to open any sockets
1237 * in this loop the parent-sockets == NULL
1238 * case below will prevent us from starting.
1241 (void)smbd_open_one_socket(parent
,
1249 if (parent
->sockets
== NULL
) {
1250 DEBUG(0,("open_sockets_smbd: No "
1251 "sockets available to bind to.\n"));
1255 /* Listen to messages */
1257 messaging_register(msg_ctx
, NULL
, MSG_SHUTDOWN
, msg_exit_server
);
1258 messaging_register(msg_ctx
, ev_ctx
, MSG_SMB_CONF_UPDATED
,
1259 smbd_parent_conf_updated
);
1260 messaging_register(msg_ctx
, NULL
, MSG_SMB_STAT_CACHE_DELETE
,
1261 smb_stat_cache_delete
);
1262 messaging_register(msg_ctx
, NULL
, MSG_DEBUG
, smbd_msg_debug
);
1263 messaging_register(msg_ctx
, NULL
, MSG_SMB_FORCE_TDIS
,
1264 smb_parent_send_to_children
);
1265 messaging_register(msg_ctx
, NULL
, MSG_SMB_FORCE_TDIS_DENIED
,
1266 smb_parent_send_to_children
);
1267 messaging_register(msg_ctx
, NULL
, MSG_SMB_KILL_CLIENT_IP
,
1268 smb_parent_send_to_children
);
1269 messaging_register(msg_ctx
, NULL
, MSG_SMB_TELL_NUM_CHILDREN
,
1270 smb_tell_num_children
);
1272 messaging_register(msg_ctx
, NULL
,
1273 ID_CACHE_DELETE
, smbd_parent_id_cache_delete
);
1274 messaging_register(msg_ctx
, NULL
,
1275 ID_CACHE_KILL
, smbd_parent_id_cache_kill
);
1276 messaging_register(msg_ctx
, NULL
, MSG_SMB_NOTIFY_STARTED
,
1277 smb_parent_send_to_children
);
1280 messaging_register(msg_ctx
, NULL
, MSG_SMB_INJECT_FAULT
,
1284 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
1285 messaging_register(msg_ctx
, NULL
, MSG_SMB_SLEEP
, msg_sleep
);
1288 if (lp_multicast_dns_register() && (dns_port
!= 0)) {
1289 #ifdef WITH_DNSSD_SUPPORT
1290 smbd_setup_mdns_registration(ev_ctx
,
1293 #ifdef WITH_AVAHI_SUPPORT
1296 avahi_conn
= avahi_start_register(ev_ctx
,
1299 if (avahi_conn
== NULL
) {
1300 DEBUG(10, ("avahi_start_register failed\n"));
1310 handle stdin becoming readable when we are in --foreground mode
1312 static void smbd_stdin_handler(struct tevent_context
*ev
,
1313 struct tevent_fd
*fde
,
1318 if (read(0, &c
, 1) != 1) {
1319 /* we have reached EOF on stdin, which means the
1320 parent has exited. Shutdown the server */
1321 exit_server_cleanly("EOF on stdin");
1325 struct smbd_parent_tevent_trace_state
{
1329 static void smbd_parent_tevent_trace_callback(enum tevent_trace_point point
,
1332 struct smbd_parent_tevent_trace_state
*state
=
1333 (struct smbd_parent_tevent_trace_state
*)private_data
;
1336 case TEVENT_TRACE_BEFORE_WAIT
:
1338 case TEVENT_TRACE_AFTER_WAIT
:
1340 case TEVENT_TRACE_BEFORE_LOOP_ONCE
:
1341 TALLOC_FREE(state
->frame
);
1342 state
->frame
= talloc_stackframe();
1344 case TEVENT_TRACE_AFTER_LOOP_ONCE
:
1345 TALLOC_FREE(state
->frame
);
1352 static void smbd_parent_loop(struct tevent_context
*ev_ctx
,
1353 struct smbd_parent_context
*parent
)
1355 struct smbd_parent_tevent_trace_state trace_state
= {
1360 tevent_set_trace_callback(ev_ctx
, smbd_parent_tevent_trace_callback
,
1363 /* now accept incoming connections - forking a new process
1364 for each incoming connection */
1365 DEBUG(2,("waiting for connections\n"));
1367 ret
= tevent_loop_wait(ev_ctx
);
1369 DEBUG(0, ("tevent_loop_wait failed: %d, %s, exiting\n",
1370 ret
, strerror(errno
)));
1373 TALLOC_FREE(trace_state
.frame
);
1375 /* NOTREACHED return True; */
1379 /****************************************************************************
1380 Initialise connect, service and file structs.
1381 ****************************************************************************/
1383 static bool init_structs(void )
1386 * Set the machine NETBIOS name if not already
1387 * set from the config file.
1393 if (!secrets_init())
1399 static void smbd_parent_sig_term_handler(struct tevent_context
*ev
,
1400 struct tevent_signal
*se
,
1406 exit_server_cleanly("termination signal");
1409 static void smbd_parent_sig_hup_handler(struct tevent_context
*ev
,
1410 struct tevent_signal
*se
,
1416 struct smbd_parent_context
*parent
=
1417 talloc_get_type_abort(private_data
,
1418 struct smbd_parent_context
);
1420 change_to_root_user();
1421 DEBUG(1,("parent: Reloading services after SIGHUP\n"));
1422 reload_services(NULL
, NULL
, false);
1424 printing_subsystem_update(parent
->ev_ctx
, parent
->msg_ctx
, true);
1427 struct smbd_claim_version_state
{
1428 TALLOC_CTX
*mem_ctx
;
1432 static void smbd_claim_version_parser(struct server_id exclusive
,
1434 struct server_id
*shared
,
1435 const uint8_t *data
,
1439 struct smbd_claim_version_state
*state
= private_data
;
1442 state
->version
= NULL
;
1445 if (data
[datalen
-1] != '\0') {
1446 DBG_WARNING("Invalid samba version\n");
1447 dump_data(DBGLVL_WARNING
, data
, datalen
);
1448 state
->version
= NULL
;
1451 state
->version
= talloc_strdup(state
->mem_ctx
, (const char *)data
);
1454 static NTSTATUS
smbd_claim_version(struct messaging_context
*msg
,
1455 const char *version
)
1457 const char *name
= "samba_version_string";
1458 const TDB_DATA key
= string_term_tdb_data(name
);
1459 struct smbd_claim_version_state state
;
1460 struct g_lock_ctx
*ctx
;
1463 ctx
= g_lock_ctx_init(msg
, msg
);
1465 DBG_WARNING("g_lock_ctx_init failed\n");
1466 return NT_STATUS_UNSUCCESSFUL
;
1469 status
= g_lock_lock(
1470 ctx
, key
, G_LOCK_READ
, (struct timeval
) { .tv_sec
= 60 });
1471 if (!NT_STATUS_IS_OK(status
)) {
1472 DBG_WARNING("g_lock_lock(G_LOCK_READ) failed: %s\n",
1478 state
= (struct smbd_claim_version_state
) { .mem_ctx
= ctx
};
1480 status
= g_lock_dump(ctx
, key
, smbd_claim_version_parser
, &state
);
1481 if (!NT_STATUS_IS_OK(status
) &&
1482 !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
1483 DBG_ERR("Could not read samba_version_string\n");
1484 g_lock_unlock(ctx
, key
);
1489 if ((state
.version
!= NULL
) && (strcmp(version
, state
.version
) == 0)) {
1491 * Leave the read lock for us around. Someone else already
1492 * set the version correctly
1495 return NT_STATUS_OK
;
1498 status
= g_lock_lock(
1499 ctx
, key
, G_LOCK_UPGRADE
, (struct timeval
) { .tv_sec
= 60 });
1500 if (!NT_STATUS_IS_OK(status
)) {
1501 DBG_WARNING("g_lock_lock(G_LOCK_WRITE) failed: %s\n",
1503 DBG_ERR("smbd %s already running, refusing to start "
1504 "version %s\n", state
.version
, version
);
1506 return NT_STATUS_SXS_VERSION_CONFLICT
;
1509 status
= g_lock_write_data(
1510 ctx
, key
, (const uint8_t *)version
, strlen(version
)+1);
1511 if (!NT_STATUS_IS_OK(status
)) {
1512 DBG_WARNING("g_lock_write_data failed: %s\n",
1518 status
= g_lock_lock(
1519 ctx
, key
, G_LOCK_DOWNGRADE
, (struct timeval
) { .tv_sec
= 60 });
1520 if (!NT_STATUS_IS_OK(status
)) {
1521 DBG_WARNING("g_lock_lock(G_LOCK_READ) failed: %s\n",
1528 * Leave "ctx" dangling so that g_lock.tdb keeps opened.
1530 return NT_STATUS_OK
;
1533 /****************************************************************************
1535 ****************************************************************************/
1537 /* Declare prototype for build_options() to avoid having to run it through
1538 mkproto.h. Mixing $(builddir) and $(srcdir) source files in the current
1539 prototype generation system is too complicated. */
1541 extern void build_options(bool screen
);
1543 int main(int argc
,const char *argv
[])
1545 /* shall I run as a daemon */
1546 bool is_daemon
= false;
1547 bool interactive
= false;
1549 bool no_process_group
= false;
1550 bool log_stdout
= false;
1552 char *profile_level
= NULL
;
1555 bool print_build_options
= False
;
1556 struct server_id main_server_id
= {0};
1561 OPT_NO_PROCESS_GROUP
,
1564 struct poptOption long_options
[] = {
1567 .longName
= "daemon",
1569 .argInfo
= POPT_ARG_NONE
,
1572 .descrip
= "Become a daemon (default)" ,
1575 .longName
= "interactive",
1577 .argInfo
= POPT_ARG_NONE
,
1579 .val
= OPT_INTERACTIVE
,
1580 .descrip
= "Run interactive (not a daemon) and log to stdout",
1583 .longName
= "foreground",
1585 .argInfo
= POPT_ARG_NONE
,
1588 .descrip
= "Run daemon in foreground (for daemontools, etc.)",
1591 .longName
= "no-process-group",
1593 .argInfo
= POPT_ARG_NONE
,
1595 .val
= OPT_NO_PROCESS_GROUP
,
1596 .descrip
= "Don't create a new process group" ,
1599 .longName
= "log-stdout",
1601 .argInfo
= POPT_ARG_NONE
,
1603 .val
= OPT_LOG_STDOUT
,
1604 .descrip
= "Log to stdout" ,
1607 .longName
= "build-options",
1609 .argInfo
= POPT_ARG_NONE
,
1612 .descrip
= "Print build options" ,
1617 .argInfo
= POPT_ARG_STRING
,
1620 .descrip
= "Listen on the specified ports",
1623 .longName
= "profiling-level",
1625 .argInfo
= POPT_ARG_STRING
,
1626 .arg
= &profile_level
,
1628 .descrip
= "Set profiling level","PROFILE_LEVEL",
1633 struct smbd_parent_context
*parent
= NULL
;
1636 struct tevent_context
*ev_ctx
;
1637 struct messaging_context
*msg_ctx
;
1638 struct dcesrv_context
*dce_ctx
= NULL
;
1639 struct server_id server_id
;
1640 struct tevent_signal
*se
;
1641 int profiling_level
;
1642 char *np_dir
= NULL
;
1643 const struct loadparm_substitution
*lp_sub
=
1644 loadparm_s3_global_substitution();
1645 static const struct smbd_shim smbd_shim_fns
=
1647 .send_stat_cache_delete_message
= smbd_send_stat_cache_delete_message
,
1648 .change_to_root_user
= smbd_change_to_root_user
,
1649 .become_authenticated_pipe_user
= smbd_become_authenticated_pipe_user
,
1650 .unbecome_authenticated_pipe_user
= smbd_unbecome_authenticated_pipe_user
,
1652 .contend_level2_oplocks_begin
= smbd_contend_level2_oplocks_begin
,
1653 .contend_level2_oplocks_end
= smbd_contend_level2_oplocks_end
,
1655 .become_root
= smbd_become_root
,
1656 .unbecome_root
= smbd_unbecome_root
,
1658 .exit_server
= smbd_exit_server
,
1659 .exit_server_cleanly
= smbd_exit_server_cleanly
,
1663 * Do this before any other talloc operation
1665 talloc_enable_null_tracking();
1666 frame
= talloc_stackframe();
1668 setup_logging(argv
[0], DEBUG_DEFAULT_STDOUT
);
1672 set_smbd_shim(&smbd_shim_fns
);
1674 smbd_init_globals();
1678 #ifdef HAVE_SET_AUTH_PARAMETERS
1679 set_auth_parameters(argc
,argv
);
1682 pc
= poptGetContext("smbd", argc
, argv
, long_options
, 0);
1683 while((opt
= poptGetNextOpt(pc
)) != -1) {
1688 case OPT_INTERACTIVE
:
1694 case OPT_NO_PROCESS_GROUP
:
1695 no_process_group
= true;
1697 case OPT_LOG_STDOUT
:
1701 print_build_options
= True
;
1704 d_fprintf(stderr
, "\nInvalid option %s: %s\n\n",
1705 poptBadOption(pc
, 0), poptStrerror(opt
));
1706 poptPrintUsage(pc
, stderr
, 0);
1710 poptFreeContext(pc
);
1718 setup_logging(argv
[0], DEBUG_STDOUT
);
1720 setup_logging(argv
[0], DEBUG_FILE
);
1723 if (print_build_options
) {
1724 build_options(True
); /* Display output to screen as well as debug */
1729 /* needed for SecureWare on SCO */
1733 set_remote_machine_name("smbd", False
);
1735 if (interactive
&& (DEBUGLEVEL
>= 9)) {
1736 talloc_enable_leak_report();
1739 if (log_stdout
&& Fork
) {
1740 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
1745 * We want to die early if we can't open /dev/urandom
1747 generate_random_buffer(NULL
, 0);
1749 /* get initial effective uid and gid */
1752 /* make absolutely sure we run as root - to handle cases where people
1753 are crazy enough to have it setuid */
1754 gain_root_privilege();
1755 gain_root_group_privilege();
1758 dump_core_setup("smbd", lp_logfile(talloc_tos(), lp_sub
));
1760 /* we are never interested in SIGPIPE */
1761 BlockSignals(True
,SIGPIPE
);
1764 /* we are never interested in SIGFPE */
1765 BlockSignals(True
,SIGFPE
);
1768 #if defined(SIGUSR2)
1769 /* We are no longer interested in USR2 */
1770 BlockSignals(True
,SIGUSR2
);
1774 * POSIX demands that signals are inherited. If the invoking
1775 * process has these signals masked, we will have problems, as
1776 * we won't receive them.
1778 BlockSignals(False
, SIGHUP
);
1779 BlockSignals(False
, SIGUSR1
);
1780 BlockSignals(False
, SIGTERM
);
1782 /* Ensure we leave no zombies until we
1783 * correctly set up child handling below. */
1787 /* we want total control over the permissions on created files,
1788 so set our umask to 0 */
1793 DEBUG(0,("smbd version %s started.\n", samba_version_string()));
1794 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE
));
1796 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
1797 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
1799 /* Output the build options to the debug log */
1800 build_options(False
);
1802 if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4) {
1803 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
1807 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1808 DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
1813 * This calls unshare(CLONE_FS); on linux
1814 * in order to check if the running kernel/container
1815 * environment supports it.
1817 per_thread_cwd_check();
1819 if (!cluster_probe_ok()) {
1823 /* Init the security context and global current_user */
1827 * Initialize the event context. The event context needs to be
1828 * initialized before the messaging context, cause the messaging
1829 * context holds an event context.
1831 ev_ctx
= global_event_context();
1832 if (ev_ctx
== NULL
) {
1837 * Init the messaging context
1838 * FIXME: This should only call messaging_init()
1840 msg_ctx
= global_messaging_context();
1841 if (msg_ctx
== NULL
) {
1845 dce_ctx
= global_dcesrv_context();
1846 if (dce_ctx
== NULL
) {
1851 * Reloading of the printers will not work here as we don't have a
1852 * server info and rpc services set up. It will be called later.
1854 if (!reload_services(NULL
, NULL
, false)) {
1858 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
) {
1859 if (!lp_parm_bool(-1, "server role check", "inhibit", false)) {
1860 DBG_ERR("server role = 'active directory domain controller' not compatible with running smbd standalone. \n");
1861 DEBUGADD(0, ("You should start 'samba' instead, and it will control starting smbd if required\n"));
1864 /* Main 'samba' daemon will notify */
1865 daemon_sd_notifications(false);
1868 /* ...NOTE... Log files are working from this point! */
1870 DEBUG(3,("loaded services\n"));
1874 if (!profile_setup(msg_ctx
, False
)) {
1875 DEBUG(0,("ERROR: failed to setup profiling\n"));
1879 if (profile_level
!= NULL
) {
1880 profiling_level
= atoi(profile_level
);
1882 profiling_level
= lp_smbd_profiling_level();
1884 main_server_id
= messaging_server_id(msg_ctx
);
1885 set_profile_level(profiling_level
, &main_server_id
);
1887 if (!is_daemon
&& !is_a_socket(0)) {
1889 DEBUG(3, ("Standard input is not a socket, "
1890 "assuming -D option\n"));
1894 * Setting is_daemon here prevents us from eventually calling
1895 * the open_sockets_inetd()
1901 if (is_daemon
&& !interactive
) {
1902 DEBUG(3, ("Becoming a daemon.\n"));
1903 become_daemon(Fork
, no_process_group
, log_stdout
);
1905 daemon_status("smbd", "Starting process ...");
1910 * If we're interactive we want to set our own process group for
1911 * signal management.
1913 if (interactive
&& !no_process_group
)
1914 setpgid( (pid_t
)0, (pid_t
)0);
1917 if (!directory_exist(lp_lock_directory()))
1918 mkdir(lp_lock_directory(), 0755);
1920 if (!directory_exist(lp_pid_directory()))
1921 mkdir(lp_pid_directory(), 0755);
1924 pidfile_create(lp_pid_directory(), "smbd");
1926 status
= reinit_after_fork(msg_ctx
, ev_ctx
, false, NULL
);
1927 if (!NT_STATUS_IS_OK(status
)) {
1928 exit_daemon("reinit_after_fork() failed", map_errno_from_nt_status(status
));
1933 * Do not initialize the parent-child-pipe before becoming a
1934 * daemon: this is used to detect a died parent in the child
1937 status
= init_before_fork();
1938 if (!NT_STATUS_IS_OK(status
)) {
1939 exit_daemon(nt_errstr(status
), map_errno_from_nt_status(status
));
1943 parent
= talloc_zero(ev_ctx
, struct smbd_parent_context
);
1945 exit_server("talloc(struct smbd_parent_context) failed");
1947 parent
->interactive
= interactive
;
1948 parent
->ev_ctx
= ev_ctx
;
1949 parent
->msg_ctx
= msg_ctx
;
1950 parent
->dce_ctx
= dce_ctx
;
1953 se
= tevent_add_signal(parent
->ev_ctx
,
1956 smbd_parent_sig_term_handler
,
1959 exit_server("failed to setup SIGTERM handler");
1961 se
= tevent_add_signal(parent
->ev_ctx
,
1964 smbd_parent_sig_hup_handler
,
1967 exit_server("failed to setup SIGHUP handler");
1970 /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
1972 if (smbd_memcache() == NULL
) {
1973 exit_daemon("no memcache available", EACCES
);
1976 memcache_set_global(smbd_memcache());
1978 /* Initialise the password backed before the global_sam_sid
1979 to ensure that we fetch from ldap before we make a domain sid up */
1981 if(!initialize_password_db(false, ev_ctx
))
1984 if (!secrets_init()) {
1985 exit_daemon("smbd can not open secrets.tdb", EACCES
);
1988 if (lp_server_role() == ROLE_DOMAIN_BDC
|| lp_server_role() == ROLE_DOMAIN_PDC
) {
1989 struct loadparm_context
*lp_ctx
= loadparm_init_s3(NULL
, loadparm_s3_helpers());
1990 if (!open_schannel_session_store(NULL
, lp_ctx
)) {
1991 exit_daemon("ERROR: Samba cannot open schannel store for secured NETLOGON operations.", EACCES
);
1993 TALLOC_FREE(lp_ctx
);
1996 if(!get_global_sam_sid()) {
1997 exit_daemon("Samba cannot create a SAM SID", EACCES
);
2000 server_id
= messaging_server_id(msg_ctx
);
2001 status
= smbXsrv_version_global_init(&server_id
);
2002 if (!NT_STATUS_IS_OK(status
)) {
2003 exit_daemon("Samba cannot init server context", EACCES
);
2006 status
= smbXsrv_client_global_init();
2007 if (!NT_STATUS_IS_OK(status
)) {
2008 exit_daemon("Samba cannot init clients context", EACCES
);
2011 status
= smbXsrv_session_global_init(msg_ctx
);
2012 if (!NT_STATUS_IS_OK(status
)) {
2013 exit_daemon("Samba cannot init session context", EACCES
);
2016 status
= smbXsrv_tcon_global_init();
2017 if (!NT_STATUS_IS_OK(status
)) {
2018 exit_daemon("Samba cannot init tcon context", EACCES
);
2021 if (!locking_init())
2022 exit_daemon("Samba cannot init locking", EACCES
);
2024 if (!leases_db_init(false)) {
2025 exit_daemon("Samba cannot init leases", EACCES
);
2028 if (!smbd_notifyd_init(msg_ctx
, interactive
, &parent
->notifyd
)) {
2029 exit_daemon("Samba cannot init notification", EACCES
);
2032 if (!cleanupd_init(msg_ctx
, interactive
, &parent
->cleanupd
)) {
2033 exit_daemon("Samba cannot init the cleanupd", EACCES
);
2036 if (!messaging_parent_dgm_cleanup_init(msg_ctx
)) {
2040 if (!smbd_scavenger_init(NULL
, msg_ctx
, ev_ctx
)) {
2041 exit_daemon("Samba cannot init scavenging", EACCES
);
2044 if (!W_ERROR_IS_OK(registry_init_full()))
2045 exit_daemon("Samba cannot init registry", EACCES
);
2047 /* Open the share_info.tdb here, so we don't have to open
2048 after the fork on every single connection. This is a small
2049 performance improvment and reduces the total number of system
2051 status
= share_info_db_init();
2052 if (!NT_STATUS_IS_OK(status
)) {
2053 exit_daemon("ERROR: failed to load share info db.", EACCES
);
2056 status
= init_system_session_info(NULL
);
2057 if (!NT_STATUS_IS_OK(status
)) {
2058 DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
2059 nt_errstr(status
)));
2063 if (!init_guest_session_info(NULL
)) {
2064 DEBUG(0,("ERROR: failed to setup guest info.\n"));
2068 if (!file_init_global()) {
2069 DEBUG(0, ("ERROR: file_init_global() failed\n"));
2072 status
= smbXsrv_open_global_init();
2073 if (!NT_STATUS_IS_OK(status
)) {
2074 exit_daemon("Samba cannot init global open", map_errno_from_nt_status(status
));
2077 if (lp_clustering() && !lp_allow_unsafe_cluster_upgrade()) {
2078 status
= smbd_claim_version(msg_ctx
, samba_version_string());
2079 if (!NT_STATUS_IS_OK(status
)) {
2080 DBG_ERR("Could not claim version: %s\n",
2086 /* This MUST be done before start_epmd() because otherwise
2087 * start_epmd() forks and races against dcesrv_ep_setup() to
2088 * call directory_create_or_exist() */
2089 if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
2090 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
2091 lp_ncalrpc_dir(), strerror(errno
)));
2095 np_dir
= talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
2097 DEBUG(0, ("%s: Out of memory\n", __location__
));
2101 if (!directory_create_or_exist_strict(np_dir
, geteuid(), 0700)) {
2102 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
2103 np_dir
, strerror(errno
)));
2107 status
= dcesrv_init(ev_ctx
, ev_ctx
, msg_ctx
, dce_ctx
);
2108 if (!NT_STATUS_IS_OK(status
)) {
2109 DBG_ERR("Failed to setup RPC server: %s\n", nt_errstr(status
));
2110 exit_daemon("Samba cannot setup ep pipe", EACCES
);
2114 daemon_ready("smbd");
2117 /* only start other daemons if we are running as a daemon
2118 * -- bad things will happen if smbd is launched via inetd
2119 * and we fork a copy of ourselves here */
2120 if (is_daemon
&& !interactive
) {
2122 if (rpc_epmapper_daemon() == RPC_DAEMON_FORK
) {
2123 start_epmd(ev_ctx
, msg_ctx
, dce_ctx
);
2126 if (rpc_lsasd_daemon() == RPC_DAEMON_FORK
) {
2127 start_lsasd(ev_ctx
, msg_ctx
, dce_ctx
);
2130 if (rpc_fss_daemon() == RPC_DAEMON_FORK
) {
2131 start_fssd(ev_ctx
, msg_ctx
, dce_ctx
);
2134 if (!lp__disable_spoolss() &&
2135 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED
)) {
2136 bool bgq
= lp_parm_bool(-1, "smbd", "backgroundqueue", true);
2137 bool ok
= printing_subsystem_init(ev_ctx
,
2143 exit_daemon("Samba failed to init printing subsystem", EACCES
);
2147 #ifdef WITH_SPOTLIGHT
2148 if ((rpc_mdssvc_mode() == RPC_SERVICE_MODE_EXTERNAL
) &&
2149 (rpc_mdssd_daemon() == RPC_DAEMON_FORK
)) {
2150 start_mdssd(ev_ctx
, msg_ctx
, dce_ctx
);
2153 } else if (!lp__disable_spoolss() &&
2154 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED
)) {
2155 bool ok
= printing_subsystem_init(ev_ctx
,
2171 /* Started from inetd. fd 0 is the socket. */
2172 /* We will abort gracefully when the client or remote system
2176 /* close stdin, stdout (if not logging to it), but not stderr */
2177 close_low_fds(true, !debug_get_output_is_stdout(), false);
2184 smbd_setup_sig_chld_handler(parent
);
2186 smbd_process(ev_ctx
, msg_ctx
, dce_ctx
, sock
, true);
2188 exit_server_cleanly(NULL
);
2192 if (!open_sockets_smbd(parent
, ev_ctx
, msg_ctx
, ports
))
2193 exit_server("open_sockets_smbd() failed");
2195 /* do a printer update now that all messaging has been set up,
2196 * before we allow clients to start connecting */
2197 if (!lp__disable_spoolss() &&
2198 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED
)) {
2199 printing_subsystem_update(ev_ctx
, msg_ctx
, false);
2203 /* make sure we always have a valid stackframe */
2204 frame
= talloc_stackframe();
2207 /* if we are running in the foreground then look for
2208 EOF on stdin, and exit if it happens. This allows
2209 us to die if the parent process dies
2210 Only do this on a pipe or socket, no other device.
2213 if (fstat(0, &st
) != 0) {
2216 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
)) {
2217 tevent_add_fd(ev_ctx
,
2226 smbd_parent_loop(ev_ctx
, parent
);
2228 exit_server_cleanly(NULL
);