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 "smbd/smbd.h"
29 #include "smbd/globals.h"
30 #include "registry/reg_init_full.h"
31 #include "libcli/auth/schannel.h"
33 #include "../lib/util/memcache.h"
34 #include "ctdbd_conn.h"
35 #include "util_cluster.h"
36 #include "printing/queue_process.h"
37 #include "rpc_server/rpc_service_setup.h"
38 #include "rpc_server/rpc_config.h"
42 #include "messages_ctdb.h"
43 #include "smbprofile.h"
44 #include "lib/id_cache.h"
45 #include "lib/param/param.h"
46 #include "lib/background.h"
47 #include "../lib/util/pidfile.h"
48 #include "lib/smbd_shim.h"
49 #include "scavenger.h"
50 #include "locking/leases_db.h"
51 #include "smbd/notifyd/notifyd.h"
52 #include "smbd/smbd_cleanupd.h"
53 #include "lib/util/sys_rw.h"
54 #include "cleanupdb.h"
56 #include "rpc_server/epmd.h"
57 #include "rpc_server/lsasd.h"
58 #include "rpc_server/fssd.h"
59 #include "rpc_server/mdssd.h"
61 #ifdef CLUSTER_SUPPORT
62 #include "ctdb_protocol.h"
65 struct smbd_open_socket;
66 struct smbd_child_pid;
68 struct smbd_parent_context {
71 struct tevent_context *ev_ctx;
72 struct messaging_context *msg_ctx;
74 /* the list of listening sockets */
75 struct smbd_open_socket *sockets;
77 /* the list of current child processes */
78 struct smbd_child_pid *children;
81 struct server_id cleanupd;
82 struct server_id notifyd;
84 struct tevent_timer *cleanup_te;
87 struct smbd_open_socket {
88 struct smbd_open_socket *prev, *next;
89 struct smbd_parent_context *parent;
91 struct tevent_fd *fde;
94 struct smbd_child_pid {
95 struct smbd_child_pid *prev, *next;
99 /*******************************************************************
100 What to do when smb.conf is updated.
101 ********************************************************************/
103 static void smbd_parent_conf_updated(struct messaging_context *msg,
106 struct server_id server_id,
109 struct tevent_context *ev_ctx =
110 talloc_get_type_abort(private_data, struct tevent_context);
112 DEBUG(10,("smbd_parent_conf_updated: Got message saying smb.conf was "
113 "updated. Reloading.\n"));
114 change_to_root_user();
115 reload_services(NULL, NULL, false);
116 printing_subsystem_update(ev_ctx, msg, false);
119 /*******************************************************************
120 Delete a statcache entry.
121 ********************************************************************/
123 static void smb_stat_cache_delete(struct messaging_context *msg,
126 struct server_id server_id,
129 const char *name = (const char *)data->data;
130 DEBUG(10,("smb_stat_cache_delete: delete name %s\n", name));
131 stat_cache_delete(name);
134 /****************************************************************************
135 Send a SIGTERM to our process group.
136 *****************************************************************************/
138 static void killkids(void)
140 if(am_parent) kill(0,SIGTERM);
143 static void msg_exit_server(struct messaging_context *msg,
146 struct server_id server_id,
149 DEBUG(3, ("got a SHUTDOWN message\n"));
150 exit_server_cleanly(NULL);
154 static void msg_inject_fault(struct messaging_context *msg,
157 struct server_id src,
161 struct server_id_buf tmp;
163 if (data->length != sizeof(sig)) {
164 DEBUG(0, ("Process %s sent bogus signal injection request\n",
165 server_id_str_buf(src, &tmp)));
169 sig = *(int *)data->data;
171 exit_server("internal error injected");
175 #ifdef HAVE_STRSIGNAL
176 DEBUG(0, ("Process %s requested injection of signal %d (%s)\n",
177 server_id_str_buf(src, &tmp), sig, strsignal(sig)));
179 DEBUG(0, ("Process %s requested injection of signal %d\n",
180 server_id_str_buf(src, &tmp), sig));
185 #endif /* DEVELOPER */
187 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
189 * Sleep for the specified number of seconds.
191 static void msg_sleep(struct messaging_context *msg,
194 struct server_id src,
197 unsigned int seconds;
198 struct server_id_buf tmp;
200 if (data->length != sizeof(seconds)) {
201 DBG_ERR("Process %s sent bogus sleep request\n",
202 server_id_str_buf(src, &tmp));
206 seconds = *(unsigned int *)data->data;
207 DBG_ERR("Process %s request a sleep of %u seconds\n",
208 server_id_str_buf(src, &tmp),
211 DBG_ERR("Restarting after %u second sleep requested by process %s\n",
213 server_id_str_buf(src, &tmp));
215 #endif /* DEVELOPER */
217 static NTSTATUS messaging_send_to_children(struct messaging_context *msg_ctx,
218 uint32_t msg_type, DATA_BLOB* data)
221 struct smbd_parent_context *parent = am_parent;
222 struct smbd_child_pid *child;
224 if (parent == NULL) {
225 return NT_STATUS_INTERNAL_ERROR;
228 for (child = parent->children; child != NULL; child = child->next) {
229 status = messaging_send(parent->msg_ctx,
230 pid_to_procid(child->pid),
232 if (!NT_STATUS_IS_OK(status)) {
233 DBG_DEBUG("messaging_send(%d) failed: %s\n",
234 (int)child->pid, nt_errstr(status));
240 static void smb_parent_send_to_children(struct messaging_context *ctx,
243 struct server_id srv_id,
246 messaging_send_to_children(ctx, msg_type, msg_data);
250 * Parent smbd process sets its own debug level first and then
251 * sends a message to all the smbd children to adjust their debug
252 * level to that of the parent.
255 static void smbd_msg_debug(struct messaging_context *msg_ctx,
258 struct server_id server_id,
261 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
263 messaging_send_to_children(msg_ctx, MSG_DEBUG, data);
266 static void smbd_parent_id_cache_kill(struct messaging_context *msg_ctx,
269 struct server_id server_id,
272 const char *msg = (data && data->data)
273 ? (const char *)data->data : "<NULL>";
274 struct id_cache_ref id;
276 if (!id_cache_ref_parse(msg, &id)) {
277 DEBUG(0, ("Invalid ?ID: %s\n", msg));
281 id_cache_delete_from_cache(&id);
283 messaging_send_to_children(msg_ctx, msg_type, data);
286 static void smbd_parent_id_cache_delete(struct messaging_context *ctx,
289 struct server_id srv_id,
292 id_cache_delete_message(ctx, data, msg_type, srv_id, msg_data);
294 messaging_send_to_children(ctx, msg_type, msg_data);
297 #ifdef CLUSTER_SUPPORT
298 static int smbd_parent_ctdb_reconfigured(
299 struct tevent_context *ev,
300 uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid,
301 const uint8_t *msg, size_t msglen, void *private_data)
303 struct messaging_context *msg_ctx = talloc_get_type_abort(
304 private_data, struct messaging_context);
306 DEBUG(10, ("Got %s message\n", (dst_srvid == CTDB_SRVID_RECONFIGURE)
307 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
310 * Someone from the family died, validate our locks
314 messaging_send_buf(msg_ctx, am_parent->cleanupd,
315 MSG_SMB_BRL_VALIDATE, NULL, 0);
322 static void add_child_pid(struct smbd_parent_context *parent,
325 struct smbd_child_pid *child;
327 child = talloc_zero(parent, struct smbd_child_pid);
329 DEBUG(0, ("Could not add child struct -- malloc failed\n"));
333 DLIST_ADD(parent->children, child);
334 parent->num_children += 1;
337 static void smb_tell_num_children(struct messaging_context *ctx, void *data,
338 uint32_t msg_type, struct server_id srv_id,
341 uint8_t buf[sizeof(uint32_t)];
344 SIVAL(buf, 0, am_parent->num_children);
345 messaging_send_buf(ctx, srv_id, MSG_SMB_NUM_CHILDREN,
350 static void notifyd_stopped(struct tevent_req *req);
352 static struct tevent_req *notifyd_req(struct messaging_context *msg_ctx,
353 struct tevent_context *ev)
355 struct tevent_req *req;
356 sys_notify_watch_fn sys_notify_watch = NULL;
357 struct sys_notify_context *sys_notify_ctx = NULL;
358 struct ctdbd_connection *ctdbd_conn = NULL;
360 if (lp_kernel_change_notify()) {
363 if (lp_parm_bool(-1, "notify", "inotify", true)) {
364 sys_notify_watch = inotify_watch;
369 if (lp_parm_bool(-1, "notify", "fam",
370 (sys_notify_watch == NULL))) {
371 sys_notify_watch = fam_watch;
376 if (sys_notify_watch != NULL) {
377 sys_notify_ctx = sys_notify_context_create(msg_ctx, ev);
378 if (sys_notify_ctx == NULL) {
383 if (lp_clustering()) {
384 ctdbd_conn = messaging_ctdb_connection();
387 req = notifyd_send(msg_ctx, ev, msg_ctx, ctdbd_conn,
388 sys_notify_watch, sys_notify_ctx);
390 TALLOC_FREE(sys_notify_ctx);
393 tevent_req_set_callback(req, notifyd_stopped, msg_ctx);
398 static void notifyd_stopped(struct tevent_req *req)
402 ret = notifyd_recv(req);
404 DEBUG(1, ("notifyd stopped: %s\n", strerror(ret)));
407 static bool smbd_notifyd_init(struct messaging_context *msg, bool interactive,
408 struct server_id *ppid)
410 struct tevent_context *ev = messaging_tevent_context(msg);
411 struct tevent_req *req;
417 req = notifyd_req(msg, ev);
418 return (req != NULL);
423 DEBUG(1, ("%s: fork failed: %s\n", __func__,
429 if (am_parent != 0) {
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)));
443 req = notifyd_req(msg, ev);
447 tevent_req_set_callback(req, notifyd_stopped, msg);
449 /* Block those signals that we are not handling */
450 BlockSignals(True, SIGHUP);
451 BlockSignals(True, SIGUSR1);
453 messaging_send(msg, pid_to_procid(getppid()), MSG_SMB_NOTIFY_STARTED,
456 ok = tevent_req_poll(req, ev);
458 DBG_WARNING("tevent_req_poll returned %s\n", strerror(errno));
464 static void notifyd_init_trigger(struct tevent_req *req);
466 struct notifyd_init_state {
468 struct tevent_context *ev;
469 struct messaging_context *msg;
470 struct server_id *ppid;
473 static struct tevent_req *notifyd_init_send(struct tevent_context *ev,
475 struct messaging_context *msg,
476 struct server_id *ppid)
478 struct tevent_req *req = NULL;
479 struct tevent_req *subreq = NULL;
480 struct notifyd_init_state *state = NULL;
482 req = tevent_req_create(mem_ctx, &state, struct notifyd_init_state);
487 *state = (struct notifyd_init_state) {
493 subreq = tevent_wakeup_send(state, ev, tevent_timeval_current_ofs(1, 0));
494 if (tevent_req_nomem(subreq, req)) {
495 return tevent_req_post(req, ev);
498 tevent_req_set_callback(subreq, notifyd_init_trigger, req);
502 static void notifyd_init_trigger(struct tevent_req *subreq)
504 struct tevent_req *req = tevent_req_callback_data(
505 subreq, struct tevent_req);
506 struct notifyd_init_state *state = tevent_req_data(
507 req, struct notifyd_init_state);
510 DBG_NOTICE("Triggering notifyd startup\n");
512 ok = tevent_wakeup_recv(subreq);
515 tevent_req_error(req, ENOMEM);
519 state->ok = smbd_notifyd_init(state->msg, false, state->ppid);
521 DBG_WARNING("notifyd restarted\n");
522 tevent_req_done(req);
526 DBG_NOTICE("notifyd startup failed, rescheduling\n");
528 subreq = tevent_wakeup_send(state, state->ev,
529 tevent_timeval_current_ofs(1, 0));
530 if (tevent_req_nomem(subreq, req)) {
531 DBG_ERR("scheduling notifyd restart failed, giving up\n");
535 tevent_req_set_callback(subreq, notifyd_init_trigger, req);
539 static bool notifyd_init_recv(struct tevent_req *req)
541 struct notifyd_init_state *state = tevent_req_data(
542 req, struct notifyd_init_state);
547 static void notifyd_started(struct tevent_req *req)
551 ok = notifyd_init_recv(req);
554 DBG_ERR("Failed to restart notifyd, giving up\n");
559 static void cleanupd_stopped(struct tevent_req *req);
561 static bool cleanupd_init(struct messaging_context *msg, bool interactive,
562 struct server_id *ppid)
564 struct tevent_context *ev = messaging_tevent_context(msg);
565 struct server_id parent_id = messaging_server_id(msg);
566 struct tevent_req *req;
576 req = smbd_cleanupd_send(msg, ev, msg, parent_id.pid);
577 *ppid = messaging_server_id(msg);
578 return (req != NULL);
583 DBG_WARNING("pipe failed: %s\n", strerror(errno));
589 DBG_WARNING("fork failed: %s\n", strerror(errno));
598 rwret = sys_read(up_pipe[0], &c, 1);
602 DBG_WARNING("sys_read failed: %s\n", strerror(errno));
606 DBG_WARNING("cleanupd could not start\n");
610 DBG_WARNING("cleanupd returned %d\n", (int)c);
614 DBG_DEBUG("Started cleanupd pid=%d\n", (int)pid);
616 if (am_parent != NULL) {
617 add_child_pid(am_parent, pid);
620 *ppid = pid_to_procid(pid);
626 status = smbd_reinit_after_fork(msg, ev, true, "cleanupd");
627 if (!NT_STATUS_IS_OK(status)) {
628 DBG_WARNING("reinit_after_fork failed: %s\n",
631 sys_write(up_pipe[1], &c, 1);
636 req = smbd_cleanupd_send(msg, ev, msg, parent_id.pid);
638 DBG_WARNING("smbd_cleanupd_send failed\n");
640 sys_write(up_pipe[1], &c, 1);
645 tevent_req_set_callback(req, cleanupd_stopped, msg);
648 rwret = sys_write(up_pipe[1], &c, 1);
652 DBG_WARNING("sys_write failed: %s\n", strerror(errno));
656 DBG_WARNING("sys_write could not write result\n");
660 ok = tevent_req_poll(req, ev);
662 DBG_WARNING("tevent_req_poll returned %s\n", strerror(errno));
667 static void cleanupd_stopped(struct tevent_req *req)
671 status = smbd_cleanupd_recv(req);
672 DBG_WARNING("cleanupd stopped: %s\n", nt_errstr(status));
675 static void cleanupd_init_trigger(struct tevent_req *req);
677 struct cleanup_init_state {
679 struct tevent_context *ev;
680 struct messaging_context *msg;
681 struct server_id *ppid;
684 static struct tevent_req *cleanupd_init_send(struct tevent_context *ev,
686 struct messaging_context *msg,
687 struct server_id *ppid)
689 struct tevent_req *req = NULL;
690 struct tevent_req *subreq = NULL;
691 struct cleanup_init_state *state = NULL;
693 req = tevent_req_create(mem_ctx, &state, struct cleanup_init_state);
698 *state = (struct cleanup_init_state) {
704 subreq = tevent_wakeup_send(state, ev, tevent_timeval_current_ofs(0, 0));
705 if (tevent_req_nomem(subreq, req)) {
706 return tevent_req_post(req, ev);
709 tevent_req_set_callback(subreq, cleanupd_init_trigger, req);
713 static void cleanupd_init_trigger(struct tevent_req *subreq)
715 struct tevent_req *req = tevent_req_callback_data(
716 subreq, struct tevent_req);
717 struct cleanup_init_state *state = tevent_req_data(
718 req, struct cleanup_init_state);
721 DBG_NOTICE("Triggering cleanupd startup\n");
723 ok = tevent_wakeup_recv(subreq);
726 tevent_req_error(req, ENOMEM);
730 state->ok = cleanupd_init(state->msg, false, state->ppid);
732 DBG_WARNING("cleanupd restarted\n");
733 tevent_req_done(req);
737 DBG_NOTICE("cleanupd startup failed, rescheduling\n");
739 subreq = tevent_wakeup_send(state, state->ev,
740 tevent_timeval_current_ofs(1, 0));
741 if (tevent_req_nomem(subreq, req)) {
742 DBG_ERR("scheduling cleanupd restart failed, giving up\n");
746 tevent_req_set_callback(subreq, cleanupd_init_trigger, req);
750 static bool cleanupd_init_recv(struct tevent_req *req)
752 struct cleanup_init_state *state = tevent_req_data(
753 req, struct cleanup_init_state);
759 at most every smbd:cleanuptime seconds (default 20), we scan the BRL
760 and locking database for entries to cleanup. As a side effect this
761 also cleans up dead entries in the connections database (due to the
762 traversal in message_send_all()
764 Using a timer for this prevents a flood of traversals when a large
765 number of clients disconnect at the same time (perhaps due to a
769 static void cleanup_timeout_fn(struct tevent_context *event_ctx,
770 struct tevent_timer *te,
774 struct smbd_parent_context *parent =
775 talloc_get_type_abort(private_data,
776 struct smbd_parent_context);
778 parent->cleanup_te = NULL;
780 messaging_send_buf(parent->msg_ctx, parent->cleanupd,
781 MSG_SMB_BRL_VALIDATE, NULL, 0);
784 static void cleanupd_started(struct tevent_req *req)
788 struct smbd_parent_context *parent = tevent_req_callback_data(
789 req, struct smbd_parent_context);
791 ok = cleanupd_init_recv(req);
794 DBG_ERR("Failed to restart cleanupd, giving up\n");
798 status = messaging_send(parent->msg_ctx,
800 MSG_SMB_NOTIFY_CLEANUP,
802 if (!NT_STATUS_IS_OK(status)) {
803 DBG_ERR("messaging_send returned %s\n",
808 static void remove_child_pid(struct smbd_parent_context *parent,
810 bool unclean_shutdown)
812 struct smbd_child_pid *child;
816 for (child = parent->children; child != NULL; child = child->next) {
817 if (child->pid == pid) {
818 struct smbd_child_pid *tmp = child;
819 DLIST_REMOVE(parent->children, child);
821 parent->num_children -= 1;
827 /* not all forked child processes are added to the children list */
828 DEBUG(2, ("Could not find child %d -- ignoring\n", (int)pid));
832 if (pid == procid_to_pid(&parent->cleanupd)) {
833 struct tevent_req *req;
835 server_id_set_disconnected(&parent->cleanupd);
837 DBG_WARNING("Restarting cleanupd\n");
838 req = cleanupd_init_send(messaging_tevent_context(parent->msg_ctx),
843 DBG_ERR("Failed to restart cleanupd\n");
846 tevent_req_set_callback(req, cleanupd_started, parent);
850 if (pid == procid_to_pid(&parent->notifyd)) {
851 struct tevent_req *req;
852 struct tevent_context *ev = messaging_tevent_context(
855 server_id_set_disconnected(&parent->notifyd);
857 DBG_WARNING("Restarting notifyd\n");
858 req = notifyd_init_send(ev,
863 DBG_ERR("Failed to restart notifyd\n");
866 tevent_req_set_callback(req, notifyd_started, parent);
870 ok = cleanupdb_store_child(pid, unclean_shutdown);
872 DBG_ERR("cleanupdb_store_child failed\n");
876 if (!server_id_is_disconnected(&parent->cleanupd)) {
877 status = messaging_send(parent->msg_ctx,
879 MSG_SMB_NOTIFY_CLEANUP,
881 if (!NT_STATUS_IS_OK(status)) {
882 DBG_ERR("messaging_send returned %s\n",
887 if (unclean_shutdown) {
888 /* a child terminated uncleanly so tickle all
889 processes to see if they can grab any of the
892 DEBUG(3,(__location__ " Unclean shutdown of pid %u\n",
894 if (parent->cleanup_te == NULL) {
895 /* call the cleanup timer, but not too often */
896 int cleanup_time = lp_parm_int(-1, "smbd", "cleanuptime", 20);
897 parent->cleanup_te = tevent_add_timer(parent->ev_ctx,
899 timeval_current_ofs(cleanup_time, 0),
902 DEBUG(1,("Scheduled cleanup of brl and lock database after unclean shutdown\n"));
907 /****************************************************************************
908 Have we reached the process limit ?
909 ****************************************************************************/
911 static bool allowable_number_of_smbd_processes(struct smbd_parent_context *parent)
913 int max_processes = lp_max_smbd_processes();
918 return parent->num_children < max_processes;
921 static void smbd_sig_chld_handler(struct tevent_context *ev,
922 struct tevent_signal *se,
930 struct smbd_parent_context *parent =
931 talloc_get_type_abort(private_data,
932 struct smbd_parent_context);
934 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
935 bool unclean_shutdown = False;
937 /* If the child terminated normally, assume
938 it was an unclean shutdown unless the
941 if (WIFEXITED(status)) {
942 unclean_shutdown = WEXITSTATUS(status);
944 /* If the child terminated due to a signal
945 we always assume it was unclean.
947 if (WIFSIGNALED(status)) {
948 unclean_shutdown = True;
950 remove_child_pid(parent, pid, unclean_shutdown);
954 static void smbd_setup_sig_chld_handler(struct smbd_parent_context *parent)
956 struct tevent_signal *se;
958 se = tevent_add_signal(parent->ev_ctx,
959 parent, /* mem_ctx */
961 smbd_sig_chld_handler,
964 exit_server("failed to setup SIGCHLD handler");
968 static void smbd_open_socket_close_fn(struct tevent_context *ev,
969 struct tevent_fd *fde,
973 /* this might be the socket_wrapper swrap_close() */
977 static void smbd_accept_connection(struct tevent_context *ev,
978 struct tevent_fd *fde,
982 struct smbd_open_socket *s = talloc_get_type_abort(private_data,
983 struct smbd_open_socket);
984 struct messaging_context *msg_ctx = s->parent->msg_ctx;
985 struct sockaddr_storage addr;
986 socklen_t in_addrlen = sizeof(addr);
990 fd = accept(s->fd, (struct sockaddr *)(void *)&addr,&in_addrlen);
991 if (fd == -1 && errno == EINTR)
995 DEBUG(0,("accept: %s\n",
999 smb_set_close_on_exec(fd);
1001 if (s->parent->interactive) {
1002 reinit_after_fork(msg_ctx, ev, true, NULL);
1003 smbd_process(ev, msg_ctx, fd, true);
1004 exit_server_cleanly("end of interactive mode");
1008 if (!allowable_number_of_smbd_processes(s->parent)) {
1015 NTSTATUS status = NT_STATUS_OK;
1018 * Can't use TALLOC_FREE here. Nulling out the argument to it
1019 * would overwrite memory we've just freed.
1021 talloc_free(s->parent);
1024 /* Stop zombies, the parent explicitly handles
1025 * them, counting worker smbds. */
1028 status = smbd_reinit_after_fork(msg_ctx, ev, true, NULL);
1029 if (!NT_STATUS_IS_OK(status)) {
1030 if (NT_STATUS_EQUAL(status,
1031 NT_STATUS_TOO_MANY_OPENED_FILES)) {
1032 DEBUG(0,("child process cannot initialize "
1033 "because too many files are open\n"));
1036 if (lp_clustering() &&
1038 status, NT_STATUS_INTERNAL_DB_ERROR) ||
1040 status, NT_STATUS_CONNECTION_REFUSED))) {
1041 DEBUG(1, ("child process cannot initialize "
1042 "because connection to CTDB "
1044 nt_errstr(status)));
1048 DEBUG(0,("reinit_after_fork() failed\n"));
1049 smb_panic("reinit_after_fork() failed");
1052 smbd_process(ev, msg_ctx, fd, false);
1054 exit_server_cleanly("end of child");
1059 DEBUG(0,("smbd_accept_connection: fork() failed: %s\n",
1063 /* The parent doesn't need this socket */
1066 /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
1067 Clear the closed fd info out of server_fd --
1068 and more importantly, out of client_fd in
1069 util_sock.c, to avoid a possible
1070 getpeername failure if we reopen the logs
1071 and use %I in the filename.
1075 add_child_pid(s->parent, pid);
1078 /* Force parent to check log size after
1079 * spawning child. Fix from
1080 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
1081 * parent smbd will log to logserver.smb. It
1082 * writes only two messages for each child
1083 * started/finished. But each child writes,
1084 * say, 50 messages also in logserver.smb,
1085 * begining with the debug_count of the
1086 * parent, before the child opens its own log
1087 * file logserver.client. In a worst case
1088 * scenario the size of logserver.smb would be
1089 * checked after about 50*50=2500 messages
1092 force_check_log_size();
1095 static bool smbd_open_one_socket(struct smbd_parent_context *parent,
1096 struct tevent_context *ev_ctx,
1097 const struct sockaddr_storage *ifss,
1100 struct smbd_open_socket *s;
1102 s = talloc(parent, struct smbd_open_socket);
1108 s->fd = open_socket_in(SOCK_STREAM,
1110 parent->sockets == NULL ? 0 : 2,
1114 DEBUG(0,("smbd_open_one_socket: open_socket_in: "
1115 "%s\n", strerror(errno)));
1118 * We ignore an error here, as we've done before
1123 /* ready to listen */
1124 set_socket_options(s->fd, "SO_KEEPALIVE");
1125 set_socket_options(s->fd, lp_socket_options());
1127 /* Set server socket to
1128 * non-blocking for the accept. */
1129 set_blocking(s->fd, False);
1131 if (listen(s->fd, SMBD_LISTEN_BACKLOG) == -1) {
1132 DEBUG(0,("smbd_open_one_socket: listen: "
1133 "%s\n", strerror(errno)));
1139 s->fde = tevent_add_fd(ev_ctx,
1141 s->fd, TEVENT_FD_READ,
1142 smbd_accept_connection,
1145 DEBUG(0,("smbd_open_one_socket: "
1146 "tevent_add_fd: %s\n",
1152 tevent_fd_set_close_fn(s->fde, smbd_open_socket_close_fn);
1154 DLIST_ADD_END(parent->sockets, s);
1159 /****************************************************************************
1160 Open the socket communication.
1161 ****************************************************************************/
1163 static bool open_sockets_smbd(struct smbd_parent_context *parent,
1164 struct tevent_context *ev_ctx,
1165 struct messaging_context *msg_ctx,
1166 const char *smb_ports)
1168 int num_interfaces = iface_count();
1171 unsigned dns_port = 0;
1178 smbd_setup_sig_chld_handler(parent);
1180 ports = lp_smb_ports();
1182 /* use a reasonable default set of ports - listing on 445 and 139 */
1185 l = str_list_make_v3(talloc_tos(), smb_ports, NULL);
1186 ports = discard_const_p(const char *, l);
1189 for (j = 0; ports && ports[j]; j++) {
1190 unsigned port = atoi(ports[j]);
1192 if (port == 0 || port > 0xffff) {
1193 exit_server_cleanly("Invalid port in the config or on "
1194 "the commandline specified!");
1198 if (lp_interfaces() && lp_bind_interfaces_only()) {
1199 /* We have been given an interfaces line, and been
1200 told to only bind to those interfaces. Create a
1201 socket per interface and bind to only these.
1204 /* Now open a listen socket for each of the
1206 for(i = 0; i < num_interfaces; i++) {
1207 const struct sockaddr_storage *ifss =
1208 iface_n_sockaddr_storage(i);
1210 DEBUG(0,("open_sockets_smbd: "
1211 "interface %d has NULL IP address !\n",
1216 for (j = 0; ports && ports[j]; j++) {
1217 unsigned port = atoi(ports[j]);
1219 /* Keep the first port for mDNS service
1222 if (dns_port == 0) {
1226 if (!smbd_open_one_socket(parent,
1235 /* Just bind to 0.0.0.0 - accept connections
1238 const char *sock_addr;
1240 const char *sock_ptr;
1243 sock_addr = "::,0.0.0.0";
1245 sock_addr = "0.0.0.0";
1248 for (sock_ptr=sock_addr;
1249 next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,"); ) {
1250 for (j = 0; ports && ports[j]; j++) {
1251 struct sockaddr_storage ss;
1252 unsigned port = atoi(ports[j]);
1254 /* Keep the first port for mDNS service
1257 if (dns_port == 0) {
1261 /* open an incoming socket */
1262 if (!interpret_string_addr(&ss, sock_tok,
1263 AI_NUMERICHOST|AI_PASSIVE)) {
1268 * If we fail to open any sockets
1269 * in this loop the parent-sockets == NULL
1270 * case below will prevent us from starting.
1273 (void)smbd_open_one_socket(parent,
1281 if (parent->sockets == NULL) {
1282 DEBUG(0,("open_sockets_smbd: No "
1283 "sockets available to bind to.\n"));
1287 /* Listen to messages */
1289 messaging_register(msg_ctx, NULL, MSG_SHUTDOWN, msg_exit_server);
1290 messaging_register(msg_ctx, ev_ctx, MSG_SMB_CONF_UPDATED,
1291 smbd_parent_conf_updated);
1292 messaging_register(msg_ctx, NULL, MSG_SMB_STAT_CACHE_DELETE,
1293 smb_stat_cache_delete);
1294 messaging_register(msg_ctx, NULL, MSG_DEBUG, smbd_msg_debug);
1295 messaging_register(msg_ctx, NULL, MSG_SMB_FORCE_TDIS,
1296 smb_parent_send_to_children);
1297 messaging_register(msg_ctx, NULL, MSG_SMB_KILL_CLIENT_IP,
1298 smb_parent_send_to_children);
1299 messaging_register(msg_ctx, NULL, MSG_SMB_TELL_NUM_CHILDREN,
1300 smb_tell_num_children);
1302 messaging_register(msg_ctx, NULL,
1303 ID_CACHE_DELETE, smbd_parent_id_cache_delete);
1304 messaging_register(msg_ctx, NULL,
1305 ID_CACHE_KILL, smbd_parent_id_cache_kill);
1306 messaging_register(msg_ctx, NULL, MSG_SMB_NOTIFY_STARTED,
1307 smb_parent_send_to_children);
1309 #ifdef CLUSTER_SUPPORT
1310 if (lp_clustering()) {
1311 struct ctdbd_connection *conn = messaging_ctdb_connection();
1313 register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE,
1314 smbd_parent_ctdb_reconfigured, msg_ctx);
1315 register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY,
1316 smbd_parent_ctdb_reconfigured, msg_ctx);
1321 messaging_register(msg_ctx, NULL, MSG_SMB_INJECT_FAULT,
1325 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
1326 messaging_register(msg_ctx, NULL, MSG_SMB_SLEEP, msg_sleep);
1329 if (lp_multicast_dns_register() && (dns_port != 0)) {
1330 #ifdef WITH_DNSSD_SUPPORT
1331 smbd_setup_mdns_registration(ev_ctx,
1334 #ifdef WITH_AVAHI_SUPPORT
1337 avahi_conn = avahi_start_register(ev_ctx,
1340 if (avahi_conn == NULL) {
1341 DEBUG(10, ("avahi_start_register failed\n"));
1351 handle stdin becoming readable when we are in --foreground mode
1353 static void smbd_stdin_handler(struct tevent_context *ev,
1354 struct tevent_fd *fde,
1359 if (read(0, &c, 1) != 1) {
1360 /* we have reached EOF on stdin, which means the
1361 parent has exited. Shutdown the server */
1362 exit_server_cleanly("EOF on stdin");
1366 struct smbd_parent_tevent_trace_state {
1370 static void smbd_parent_tevent_trace_callback(enum tevent_trace_point point,
1373 struct smbd_parent_tevent_trace_state *state =
1374 (struct smbd_parent_tevent_trace_state *)private_data;
1377 case TEVENT_TRACE_BEFORE_WAIT:
1379 case TEVENT_TRACE_AFTER_WAIT:
1381 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
1382 TALLOC_FREE(state->frame);
1383 state->frame = talloc_stackframe();
1385 case TEVENT_TRACE_AFTER_LOOP_ONCE:
1386 TALLOC_FREE(state->frame);
1393 static void smbd_parent_loop(struct tevent_context *ev_ctx,
1394 struct smbd_parent_context *parent)
1396 struct smbd_parent_tevent_trace_state trace_state = {
1401 tevent_set_trace_callback(ev_ctx, smbd_parent_tevent_trace_callback,
1404 /* now accept incoming connections - forking a new process
1405 for each incoming connection */
1406 DEBUG(2,("waiting for connections\n"));
1408 ret = tevent_loop_wait(ev_ctx);
1410 DEBUG(0, ("tevent_loop_wait failed: %d, %s, exiting\n",
1411 ret, strerror(errno)));
1414 TALLOC_FREE(trace_state.frame);
1416 /* NOTREACHED return True; */
1420 /****************************************************************************
1421 Initialise connect, service and file structs.
1422 ****************************************************************************/
1424 static bool init_structs(void )
1427 * Set the machine NETBIOS name if not already
1428 * set from the config file.
1434 if (!secrets_init())
1440 static void smbd_parent_sig_term_handler(struct tevent_context *ev,
1441 struct tevent_signal *se,
1447 exit_server_cleanly("termination signal");
1450 static void smbd_parent_sig_hup_handler(struct tevent_context *ev,
1451 struct tevent_signal *se,
1457 struct smbd_parent_context *parent =
1458 talloc_get_type_abort(private_data,
1459 struct smbd_parent_context);
1461 change_to_root_user();
1462 DEBUG(1,("parent: Reloading services after SIGHUP\n"));
1463 reload_services(NULL, NULL, false);
1465 printing_subsystem_update(parent->ev_ctx, parent->msg_ctx, true);
1468 struct smbd_claim_version_state {
1469 TALLOC_CTX *mem_ctx;
1473 static void smbd_claim_version_parser(const struct g_lock_rec *locks,
1475 const uint8_t *data,
1479 struct smbd_claim_version_state *state = private_data;
1482 state->version = NULL;
1485 if (data[datalen-1] != '\0') {
1486 DBG_WARNING("Invalid samba version\n");
1487 dump_data(DBGLVL_WARNING, data, datalen);
1488 state->version = NULL;
1491 state->version = talloc_strdup(state->mem_ctx, (const char *)data);
1494 static NTSTATUS smbd_claim_version(struct messaging_context *msg,
1495 const char *version)
1497 const char *name = "samba_version_string";
1498 struct smbd_claim_version_state state;
1499 struct g_lock_ctx *ctx;
1502 ctx = g_lock_ctx_init(msg, msg);
1504 DBG_WARNING("g_lock_ctx_init failed\n");
1505 return NT_STATUS_UNSUCCESSFUL;
1508 status = g_lock_lock(ctx, string_term_tdb_data(name), G_LOCK_READ,
1509 (struct timeval) { .tv_sec = 60 });
1510 if (!NT_STATUS_IS_OK(status)) {
1511 DBG_WARNING("g_lock_lock(G_LOCK_READ) failed: %s\n",
1517 state = (struct smbd_claim_version_state) { .mem_ctx = ctx };
1519 status = g_lock_dump(ctx, string_term_tdb_data(name),
1520 smbd_claim_version_parser, &state);
1521 if (!NT_STATUS_IS_OK(status) &&
1522 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1523 DBG_ERR("Could not read samba_version_string\n");
1524 g_lock_unlock(ctx, string_term_tdb_data(name));
1529 if ((state.version != NULL) && (strcmp(version, state.version) == 0)) {
1531 * Leave the read lock for us around. Someone else already
1532 * set the version correctly
1535 return NT_STATUS_OK;
1538 status = g_lock_lock(ctx, string_term_tdb_data(name), G_LOCK_WRITE,
1539 (struct timeval) { .tv_sec = 60 });
1540 if (!NT_STATUS_IS_OK(status)) {
1541 DBG_WARNING("g_lock_lock(G_LOCK_WRITE) failed: %s\n",
1543 DBG_ERR("smbd %s already running, refusing to start "
1544 "version %s\n", state.version, version);
1546 return NT_STATUS_SXS_VERSION_CONFLICT;
1549 status = g_lock_write_data(ctx, string_term_tdb_data(name),
1550 (const uint8_t *)version,
1552 if (!NT_STATUS_IS_OK(status)) {
1553 DBG_WARNING("g_lock_write_data failed: %s\n",
1559 status = g_lock_lock(ctx, string_term_tdb_data(name), G_LOCK_READ,
1560 (struct timeval) { .tv_sec = 60 });
1561 if (!NT_STATUS_IS_OK(status)) {
1562 DBG_WARNING("g_lock_lock(G_LOCK_READ) failed: %s\n",
1569 * Leave "ctx" dangling so that g_lock.tdb keeps opened.
1571 return NT_STATUS_OK;
1574 /****************************************************************************
1576 ****************************************************************************/
1578 /* Declare prototype for build_options() to avoid having to run it through
1579 mkproto.h. Mixing $(builddir) and $(srcdir) source files in the current
1580 prototype generation system is too complicated. */
1582 extern void build_options(bool screen);
1584 int main(int argc,const char *argv[])
1586 /* shall I run as a daemon */
1587 bool is_daemon = false;
1588 bool interactive = false;
1590 bool no_process_group = false;
1591 bool log_stdout = false;
1593 char *profile_level = NULL;
1596 bool print_build_options = False;
1597 struct server_id main_server_id = {0};
1602 OPT_NO_PROCESS_GROUP,
1605 struct poptOption long_options[] = {
1608 .longName = "daemon",
1610 .argInfo = POPT_ARG_NONE,
1613 .descrip = "Become a daemon (default)" ,
1616 .longName = "interactive",
1618 .argInfo = POPT_ARG_NONE,
1620 .val = OPT_INTERACTIVE,
1621 .descrip = "Run interactive (not a daemon) and log to stdout",
1624 .longName = "foreground",
1626 .argInfo = POPT_ARG_NONE,
1629 .descrip = "Run daemon in foreground (for daemontools, etc.)",
1632 .longName = "no-process-group",
1634 .argInfo = POPT_ARG_NONE,
1636 .val = OPT_NO_PROCESS_GROUP,
1637 .descrip = "Don't create a new process group" ,
1640 .longName = "log-stdout",
1642 .argInfo = POPT_ARG_NONE,
1644 .val = OPT_LOG_STDOUT,
1645 .descrip = "Log to stdout" ,
1648 .longName = "build-options",
1650 .argInfo = POPT_ARG_NONE,
1653 .descrip = "Print build options" ,
1658 .argInfo = POPT_ARG_STRING,
1661 .descrip = "Listen on the specified ports",
1664 .longName = "profiling-level",
1666 .argInfo = POPT_ARG_STRING,
1667 .arg = &profile_level,
1669 .descrip = "Set profiling level","PROFILE_LEVEL",
1674 struct smbd_parent_context *parent = NULL;
1677 struct tevent_context *ev_ctx;
1678 struct messaging_context *msg_ctx;
1679 struct server_id server_id;
1680 struct tevent_signal *se;
1681 int profiling_level;
1682 char *np_dir = NULL;
1683 static const struct smbd_shim smbd_shim_fns =
1685 .cancel_pending_lock_requests_by_fid = smbd_cancel_pending_lock_requests_by_fid,
1686 .send_stat_cache_delete_message = smbd_send_stat_cache_delete_message,
1687 .change_to_root_user = smbd_change_to_root_user,
1688 .become_authenticated_pipe_user = smbd_become_authenticated_pipe_user,
1689 .unbecome_authenticated_pipe_user = smbd_unbecome_authenticated_pipe_user,
1691 .contend_level2_oplocks_begin = smbd_contend_level2_oplocks_begin,
1692 .contend_level2_oplocks_end = smbd_contend_level2_oplocks_end,
1694 .become_root = smbd_become_root,
1695 .unbecome_root = smbd_unbecome_root,
1697 .exit_server = smbd_exit_server,
1698 .exit_server_cleanly = smbd_exit_server_cleanly,
1702 * Do this before any other talloc operation
1704 talloc_enable_null_tracking();
1705 frame = talloc_stackframe();
1707 setup_logging(argv[0], DEBUG_DEFAULT_STDOUT);
1711 set_smbd_shim(&smbd_shim_fns);
1713 smbd_init_globals();
1717 #ifdef HAVE_SET_AUTH_PARAMETERS
1718 set_auth_parameters(argc,argv);
1721 pc = poptGetContext("smbd", argc, argv, long_options, 0);
1722 while((opt = poptGetNextOpt(pc)) != -1) {
1727 case OPT_INTERACTIVE:
1733 case OPT_NO_PROCESS_GROUP:
1734 no_process_group = true;
1736 case OPT_LOG_STDOUT:
1740 print_build_options = True;
1743 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1744 poptBadOption(pc, 0), poptStrerror(opt));
1745 poptPrintUsage(pc, stderr, 0);
1749 poptFreeContext(pc);
1757 setup_logging(argv[0], DEBUG_STDOUT);
1759 setup_logging(argv[0], DEBUG_FILE);
1762 if (print_build_options) {
1763 build_options(True); /* Display output to screen as well as debug */
1768 /* needed for SecureWare on SCO */
1772 set_remote_machine_name("smbd", False);
1774 if (interactive && (DEBUGLEVEL >= 9)) {
1775 talloc_enable_leak_report();
1778 if (log_stdout && Fork) {
1779 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
1784 * We want to die early if we can't open /dev/urandom
1786 generate_random_buffer(NULL, 0);
1788 /* get initial effective uid and gid */
1791 /* make absolutely sure we run as root - to handle cases where people
1792 are crazy enough to have it setuid */
1793 gain_root_privilege();
1794 gain_root_group_privilege();
1797 dump_core_setup("smbd", lp_logfile(talloc_tos()));
1799 /* we are never interested in SIGPIPE */
1800 BlockSignals(True,SIGPIPE);
1803 /* we are never interested in SIGFPE */
1804 BlockSignals(True,SIGFPE);
1807 #if defined(SIGUSR2)
1808 /* We are no longer interested in USR2 */
1809 BlockSignals(True,SIGUSR2);
1812 /* POSIX demands that signals are inherited. If the invoking process has
1813 * these signals masked, we will have problems, as we won't recieve them. */
1814 BlockSignals(False, SIGHUP);
1815 BlockSignals(False, SIGUSR1);
1816 BlockSignals(False, SIGTERM);
1818 /* Ensure we leave no zombies until we
1819 * correctly set up child handling below. */
1823 /* we want total control over the permissions on created files,
1824 so set our umask to 0 */
1829 DEBUG(0,("smbd version %s started.\n", samba_version_string()));
1830 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1832 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
1833 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
1835 /* Output the build options to the debug log */
1836 build_options(False);
1838 if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4) {
1839 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
1843 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1844 DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
1849 * This calls unshare(CLONE_FS); on linux
1850 * in order to check if the running kernel/container
1851 * environment supports it.
1853 per_thread_cwd_check();
1855 if (!cluster_probe_ok()) {
1859 /* Init the security context and global current_user */
1863 * Initialize the event context. The event context needs to be
1864 * initialized before the messaging context, cause the messaging
1865 * context holds an event context.
1867 ev_ctx = global_event_context();
1868 if (ev_ctx == NULL) {
1873 * Init the messaging context
1874 * FIXME: This should only call messaging_init()
1876 msg_ctx = global_messaging_context();
1877 if (msg_ctx == NULL) {
1882 * Reloading of the printers will not work here as we don't have a
1883 * server info and rpc services set up. It will be called later.
1885 if (!reload_services(NULL, NULL, false)) {
1889 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
1890 && !lp_parm_bool(-1, "server role check", "inhibit", false)) {
1891 DEBUG(0, ("server role = 'active directory domain controller' not compatible with running smbd standalone. \n"));
1892 DEBUGADD(0, ("You should start 'samba' instead, and it will control starting smbd if required\n"));
1896 /* ...NOTE... Log files are working from this point! */
1898 DEBUG(3,("loaded services\n"));
1902 if (!profile_setup(msg_ctx, False)) {
1903 DEBUG(0,("ERROR: failed to setup profiling\n"));
1907 if (profile_level != NULL) {
1908 profiling_level = atoi(profile_level);
1910 profiling_level = lp_smbd_profiling_level();
1912 main_server_id = messaging_server_id(msg_ctx);
1913 set_profile_level(profiling_level, &main_server_id);
1915 if (!is_daemon && !is_a_socket(0)) {
1917 DEBUG(3, ("Standard input is not a socket, "
1918 "assuming -D option\n"));
1922 * Setting is_daemon here prevents us from eventually calling
1923 * the open_sockets_inetd()
1929 if (is_daemon && !interactive) {
1930 DEBUG(3, ("Becoming a daemon.\n"));
1931 become_daemon(Fork, no_process_group, log_stdout);
1936 * If we're interactive we want to set our own process group for
1937 * signal management.
1939 if (interactive && !no_process_group)
1940 setpgid( (pid_t)0, (pid_t)0);
1943 if (!directory_exist(lp_lock_directory()))
1944 mkdir(lp_lock_directory(), 0755);
1946 if (!directory_exist(lp_pid_directory()))
1947 mkdir(lp_pid_directory(), 0755);
1950 pidfile_create(lp_pid_directory(), "smbd");
1952 status = reinit_after_fork(msg_ctx, ev_ctx, false, NULL);
1953 if (!NT_STATUS_IS_OK(status)) {
1954 exit_daemon("reinit_after_fork() failed", map_errno_from_nt_status(status));
1959 * Do not initialize the parent-child-pipe before becoming a
1960 * daemon: this is used to detect a died parent in the child
1963 status = init_before_fork();
1964 if (!NT_STATUS_IS_OK(status)) {
1965 exit_daemon(nt_errstr(status), map_errno_from_nt_status(status));
1969 parent = talloc_zero(ev_ctx, struct smbd_parent_context);
1971 exit_server("talloc(struct smbd_parent_context) failed");
1973 parent->interactive = interactive;
1974 parent->ev_ctx = ev_ctx;
1975 parent->msg_ctx = msg_ctx;
1978 se = tevent_add_signal(parent->ev_ctx,
1981 smbd_parent_sig_term_handler,
1984 exit_server("failed to setup SIGTERM handler");
1986 se = tevent_add_signal(parent->ev_ctx,
1989 smbd_parent_sig_hup_handler,
1992 exit_server("failed to setup SIGHUP handler");
1995 /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
1997 if (smbd_memcache() == NULL) {
1998 exit_daemon("no memcache available", EACCES);
2001 memcache_set_global(smbd_memcache());
2003 /* Initialise the password backed before the global_sam_sid
2004 to ensure that we fetch from ldap before we make a domain sid up */
2006 if(!initialize_password_db(false, ev_ctx))
2009 if (!secrets_init()) {
2010 exit_daemon("smbd can not open secrets.tdb", EACCES);
2013 if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) {
2014 struct loadparm_context *lp_ctx = loadparm_init_s3(NULL, loadparm_s3_helpers());
2015 if (!open_schannel_session_store(NULL, lp_ctx)) {
2016 exit_daemon("ERROR: Samba cannot open schannel store for secured NETLOGON operations.", EACCES);
2018 TALLOC_FREE(lp_ctx);
2021 if(!get_global_sam_sid()) {
2022 exit_daemon("Samba cannot create a SAM SID", EACCES);
2025 server_id = messaging_server_id(msg_ctx);
2026 status = smbXsrv_version_global_init(&server_id);
2027 if (!NT_STATUS_IS_OK(status)) {
2028 exit_daemon("Samba cannot init server context", EACCES);
2031 status = smbXsrv_client_global_init();
2032 if (!NT_STATUS_IS_OK(status)) {
2033 exit_daemon("Samba cannot init clients context", EACCES);
2036 status = smbXsrv_session_global_init(msg_ctx);
2037 if (!NT_STATUS_IS_OK(status)) {
2038 exit_daemon("Samba cannot init session context", EACCES);
2041 status = smbXsrv_tcon_global_init();
2042 if (!NT_STATUS_IS_OK(status)) {
2043 exit_daemon("Samba cannot init tcon context", EACCES);
2046 if (!locking_init())
2047 exit_daemon("Samba cannot init locking", EACCES);
2049 if (!leases_db_init(false)) {
2050 exit_daemon("Samba cannot init leases", EACCES);
2053 if (!smbd_notifyd_init(msg_ctx, interactive, &parent->notifyd)) {
2054 exit_daemon("Samba cannot init notification", EACCES);
2057 if (!cleanupd_init(msg_ctx, interactive, &parent->cleanupd)) {
2058 exit_daemon("Samba cannot init the cleanupd", EACCES);
2061 if (!messaging_parent_dgm_cleanup_init(msg_ctx)) {
2065 if (!smbd_scavenger_init(NULL, msg_ctx, ev_ctx)) {
2066 exit_daemon("Samba cannot init scavenging", EACCES);
2069 if (!W_ERROR_IS_OK(registry_init_full()))
2070 exit_daemon("Samba cannot init registry", EACCES);
2072 /* Open the share_info.tdb here, so we don't have to open
2073 after the fork on every single connection. This is a small
2074 performance improvment and reduces the total number of system
2076 if (!share_info_db_init()) {
2077 exit_daemon("ERROR: failed to load share info db.", EACCES);
2080 status = init_system_session_info(NULL);
2081 if (!NT_STATUS_IS_OK(status)) {
2082 DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
2083 nt_errstr(status)));
2087 if (!init_guest_session_info(NULL)) {
2088 DEBUG(0,("ERROR: failed to setup guest info.\n"));
2092 if (!file_init_global()) {
2093 DEBUG(0, ("ERROR: file_init_global() failed\n"));
2096 status = smbXsrv_open_global_init();
2097 if (!NT_STATUS_IS_OK(status)) {
2098 exit_daemon("Samba cannot init global open", map_errno_from_nt_status(status));
2101 if (lp_clustering() && !lp_allow_unsafe_cluster_upgrade()) {
2102 status = smbd_claim_version(msg_ctx, samba_version_string());
2103 if (!NT_STATUS_IS_OK(status)) {
2104 DBG_WARNING("Could not claim version: %s\n",
2110 /* This MUST be done before start_epmd() because otherwise
2111 * start_epmd() forks and races against dcesrv_ep_setup() to
2112 * call directory_create_or_exist() */
2113 if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
2114 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
2115 lp_ncalrpc_dir(), strerror(errno)));
2119 np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
2121 DEBUG(0, ("%s: Out of memory\n", __location__));
2125 if (!directory_create_or_exist_strict(np_dir, geteuid(), 0700)) {
2126 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
2127 np_dir, strerror(errno)));
2131 if (is_daemon && !interactive) {
2132 if (rpc_epmapper_daemon() == RPC_DAEMON_FORK) {
2133 start_epmd(ev_ctx, msg_ctx);
2137 if (!dcesrv_ep_setup(ev_ctx, msg_ctx)) {
2138 exit_daemon("Samba cannot setup ep pipe", EACCES);
2141 if (is_daemon && !interactive) {
2142 daemon_ready("smbd");
2145 /* only start other daemons if we are running as a daemon
2146 * -- bad things will happen if smbd is launched via inetd
2147 * and we fork a copy of ourselves here */
2148 if (is_daemon && !interactive) {
2150 if (rpc_lsasd_daemon() == RPC_DAEMON_FORK) {
2151 start_lsasd(ev_ctx, msg_ctx);
2154 if (rpc_fss_daemon() == RPC_DAEMON_FORK) {
2155 start_fssd(ev_ctx, msg_ctx);
2158 if (!lp__disable_spoolss() &&
2159 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
2160 bool bgq = lp_parm_bool(-1, "smbd", "backgroundqueue", true);
2162 if (!printing_subsystem_init(ev_ctx, msg_ctx, true, bgq)) {
2163 exit_daemon("Samba failed to init printing subsystem", EACCES);
2167 #ifdef WITH_SPOTLIGHT
2168 if ((rpc_mdssvc_mode() == RPC_SERVICE_MODE_EXTERNAL) &&
2169 (rpc_mdssd_daemon() == RPC_DAEMON_FORK)) {
2170 start_mdssd(ev_ctx, msg_ctx);
2173 } else if (!lp__disable_spoolss() &&
2174 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
2175 if (!printing_subsystem_init(ev_ctx, msg_ctx, false, false)) {
2186 /* Started from inetd. fd 0 is the socket. */
2187 /* We will abort gracefully when the client or remote system
2191 /* close stdin, stdout (if not logging to it), but not stderr */
2192 close_low_fds(true, !debug_get_output_is_stdout(), false);
2199 smbd_setup_sig_chld_handler(parent);
2201 smbd_process(ev_ctx, msg_ctx, sock, true);
2203 exit_server_cleanly(NULL);
2207 if (!open_sockets_smbd(parent, ev_ctx, msg_ctx, ports))
2208 exit_server("open_sockets_smbd() failed");
2210 /* do a printer update now that all messaging has been set up,
2211 * before we allow clients to start connecting */
2212 if (!lp__disable_spoolss() &&
2213 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
2214 printing_subsystem_update(ev_ctx, msg_ctx, false);
2218 /* make sure we always have a valid stackframe */
2219 frame = talloc_stackframe();
2222 /* if we are running in the foreground then look for
2223 EOF on stdin, and exit if it happens. This allows
2224 us to die if the parent process dies
2225 Only do this on a pipe or socket, no other device.
2228 if (fstat(0, &st) != 0) {
2231 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
2232 tevent_add_fd(ev_ctx,
2241 smbd_parent_loop(ev_ctx, parent);
2243 exit_server_cleanly(NULL);