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/conn_tdb.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 "libcli/smb/smb_direct.h"
58 #include "libcli/smb/smb_direct_client.h"
60 #ifdef CLUSTER_SUPPORT
61 #include "ctdb_protocol.h"
64 struct smbd_open_socket;
65 struct smbd_child_pid;
67 struct smbd_parent_context {
70 struct tevent_context *ev_ctx;
71 struct messaging_context *msg_ctx;
73 /* the list of listening sockets */
74 struct smbd_open_socket *sockets;
76 /* Messaging control socket to smb_direct_daemon*/
77 struct smb_direct_socket *smbd_ctr_socket;
79 /* the list of current child processes */
80 struct smbd_child_pid *children;
83 struct server_id cleanupd;
84 struct server_id notifyd;
85 pid_t smb_direct_daemon_pid;
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;
96 struct smb_direct_socket *smb_direct_socket;
99 struct smbd_child_pid {
100 struct smbd_child_pid *prev, *next;
104 extern void start_epmd(struct tevent_context *ev_ctx,
105 struct messaging_context *msg_ctx);
107 extern void start_lsasd(struct tevent_context *ev_ctx,
108 struct messaging_context *msg_ctx);
110 extern void start_fssd(struct tevent_context *ev_ctx,
111 struct messaging_context *msg_ctx);
113 extern void start_mdssd(struct tevent_context *ev_ctx,
114 struct messaging_context *msg_ctx);
116 /*******************************************************************
117 What to do when smb.conf is updated.
118 ********************************************************************/
120 static void smbd_parent_conf_updated(struct messaging_context *msg,
123 struct server_id server_id,
126 struct tevent_context *ev_ctx =
127 talloc_get_type_abort(private_data, struct tevent_context);
129 DEBUG(10,("smbd_parent_conf_updated: Got message saying smb.conf was "
130 "updated. Reloading.\n"));
131 change_to_root_user();
132 reload_services(NULL, NULL, false);
133 printing_subsystem_update(ev_ctx, msg, false);
136 /*******************************************************************
137 Delete a statcache entry.
138 ********************************************************************/
140 static void smb_stat_cache_delete(struct messaging_context *msg,
143 struct server_id server_id,
146 const char *name = (const char *)data->data;
147 DEBUG(10,("smb_stat_cache_delete: delete name %s\n", name));
148 stat_cache_delete(name);
151 /****************************************************************************
152 Send a SIGTERM to our process group.
153 *****************************************************************************/
155 static void killkids(void)
157 if(am_parent) kill(0,SIGTERM);
160 static void msg_exit_server(struct messaging_context *msg,
163 struct server_id server_id,
166 DEBUG(3, ("got a SHUTDOWN message\n"));
167 exit_server_cleanly(NULL);
171 static void msg_inject_fault(struct messaging_context *msg,
174 struct server_id src,
178 struct server_id_buf tmp;
180 if (data->length != sizeof(sig)) {
181 DEBUG(0, ("Process %s sent bogus signal injection request\n",
182 server_id_str_buf(src, &tmp)));
186 sig = *(int *)data->data;
188 exit_server("internal error injected");
193 DEBUG(0, ("Process %s requested injection of signal %d (%s)\n",
194 server_id_str_buf(src, &tmp), sig, strsignal(sig)));
196 DEBUG(0, ("Process %s requested injection of signal %d\n",
197 server_id_str_buf(src, &tmp), sig));
202 #endif /* DEVELOPER */
204 static NTSTATUS messaging_send_to_children(struct messaging_context *msg_ctx,
205 uint32_t msg_type, DATA_BLOB* data)
208 struct smbd_parent_context *parent = am_parent;
209 struct smbd_child_pid *child;
211 if (parent == NULL) {
212 return NT_STATUS_INTERNAL_ERROR;
215 for (child = parent->children; child != NULL; child = child->next) {
216 status = messaging_send(parent->msg_ctx,
217 pid_to_procid(child->pid),
219 if (!NT_STATUS_IS_OK(status)) {
220 DBG_DEBUG("messaging_send(%d) failed: %s\n",
221 (int)child->pid, nt_errstr(status));
227 static void smb_parent_send_to_children(struct messaging_context *ctx,
230 struct server_id srv_id,
233 messaging_send_to_children(ctx, msg_type, msg_data);
237 * Parent smbd process sets its own debug level first and then
238 * sends a message to all the smbd children to adjust their debug
239 * level to that of the parent.
242 static void smbd_msg_debug(struct messaging_context *msg_ctx,
245 struct server_id server_id,
248 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
250 messaging_send_to_children(msg_ctx, MSG_DEBUG, data);
253 static void smbd_parent_id_cache_kill(struct messaging_context *msg_ctx,
256 struct server_id server_id,
259 const char *msg = (data && data->data)
260 ? (const char *)data->data : "<NULL>";
261 struct id_cache_ref id;
263 if (!id_cache_ref_parse(msg, &id)) {
264 DEBUG(0, ("Invalid ?ID: %s\n", msg));
268 id_cache_delete_from_cache(&id);
270 messaging_send_to_children(msg_ctx, msg_type, data);
273 static void smbd_parent_id_cache_delete(struct messaging_context *ctx,
276 struct server_id srv_id,
279 id_cache_delete_message(ctx, data, msg_type, srv_id, msg_data);
281 messaging_send_to_children(ctx, msg_type, msg_data);
284 #ifdef CLUSTER_SUPPORT
285 static int smbd_parent_ctdb_reconfigured(
286 struct tevent_context *ev,
287 uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid,
288 const uint8_t *msg, size_t msglen, void *private_data)
290 struct messaging_context *msg_ctx = talloc_get_type_abort(
291 private_data, struct messaging_context);
293 DEBUG(10, ("Got %s message\n", (dst_srvid == CTDB_SRVID_RECONFIGURE)
294 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
297 * Someone from the family died, validate our locks
301 messaging_send_buf(msg_ctx, am_parent->cleanupd,
302 MSG_SMB_BRL_VALIDATE, NULL, 0);
309 static void add_child_pid(struct smbd_parent_context *parent,
312 struct smbd_child_pid *child;
314 child = talloc_zero(parent, struct smbd_child_pid);
316 DEBUG(0, ("Could not add child struct -- malloc failed\n"));
320 DLIST_ADD(parent->children, child);
321 parent->num_children += 1;
324 static void smb_tell_num_children(struct messaging_context *ctx, void *data,
325 uint32_t msg_type, struct server_id srv_id,
328 uint8_t buf[sizeof(uint32_t)];
331 SIVAL(buf, 0, am_parent->num_children);
332 messaging_send_buf(ctx, srv_id, MSG_SMB_NUM_CHILDREN,
337 static void notifyd_stopped(struct tevent_req *req);
339 static struct tevent_req *notifyd_req(struct messaging_context *msg_ctx,
340 struct tevent_context *ev)
342 struct tevent_req *req;
343 sys_notify_watch_fn sys_notify_watch = NULL;
344 struct sys_notify_context *sys_notify_ctx = NULL;
345 struct ctdbd_connection *ctdbd_conn = NULL;
347 if (lp_kernel_change_notify()) {
350 if (lp_parm_bool(-1, "notify", "inotify", true)) {
351 sys_notify_watch = inotify_watch;
356 if (lp_parm_bool(-1, "notify", "fam",
357 (sys_notify_watch == NULL))) {
358 sys_notify_watch = fam_watch;
363 if (sys_notify_watch != NULL) {
364 sys_notify_ctx = sys_notify_context_create(msg_ctx, ev);
365 if (sys_notify_ctx == NULL) {
370 if (lp_clustering()) {
371 ctdbd_conn = messaging_ctdb_connection();
374 req = notifyd_send(msg_ctx, ev, msg_ctx, ctdbd_conn,
375 sys_notify_watch, sys_notify_ctx);
377 TALLOC_FREE(sys_notify_ctx);
380 tevent_req_set_callback(req, notifyd_stopped, msg_ctx);
385 static void notifyd_stopped(struct tevent_req *req)
389 ret = notifyd_recv(req);
391 DEBUG(1, ("notifyd stopped: %s\n", strerror(ret)));
394 static bool smbd_notifyd_init(struct messaging_context *msg, bool interactive,
395 struct server_id *ppid)
397 struct tevent_context *ev = messaging_tevent_context(msg);
398 struct tevent_req *req;
404 req = notifyd_req(msg, ev);
405 return (req != NULL);
410 DEBUG(1, ("%s: fork failed: %s\n", __func__,
416 if (am_parent != 0) {
417 add_child_pid(am_parent, pid);
419 *ppid = pid_to_procid(pid);
423 status = smbd_reinit_after_fork(msg, ev, true, "smbd-notifyd");
424 if (!NT_STATUS_IS_OK(status)) {
425 DEBUG(1, ("%s: reinit_after_fork failed: %s\n",
426 __func__, nt_errstr(status)));
430 req = notifyd_req(msg, ev);
434 tevent_req_set_callback(req, notifyd_stopped, msg);
436 /* Block those signals that we are not handling */
437 BlockSignals(True, SIGHUP);
438 BlockSignals(True, SIGUSR1);
440 messaging_send(msg, pid_to_procid(getppid()), MSG_SMB_NOTIFY_STARTED,
443 ok = tevent_req_poll(req, ev);
445 DBG_WARNING("tevent_req_poll returned %s\n", strerror(errno));
451 static void notifyd_init_trigger(struct tevent_req *req);
453 struct notifyd_init_state {
455 struct tevent_context *ev;
456 struct messaging_context *msg;
457 struct server_id *ppid;
460 static struct tevent_req *notifyd_init_send(struct tevent_context *ev,
462 struct messaging_context *msg,
463 struct server_id *ppid)
465 struct tevent_req *req = NULL;
466 struct tevent_req *subreq = NULL;
467 struct notifyd_init_state *state = NULL;
469 req = tevent_req_create(mem_ctx, &state, struct notifyd_init_state);
474 *state = (struct notifyd_init_state) {
480 subreq = tevent_wakeup_send(state, ev, tevent_timeval_current_ofs(1, 0));
481 if (tevent_req_nomem(subreq, req)) {
482 return tevent_req_post(req, ev);
485 tevent_req_set_callback(subreq, notifyd_init_trigger, req);
489 static void notifyd_init_trigger(struct tevent_req *subreq)
491 struct tevent_req *req = tevent_req_callback_data(
492 subreq, struct tevent_req);
493 struct notifyd_init_state *state = tevent_req_data(
494 req, struct notifyd_init_state);
497 DBG_NOTICE("Triggering notifyd startup\n");
499 ok = tevent_wakeup_recv(subreq);
502 tevent_req_error(req, ENOMEM);
506 state->ok = smbd_notifyd_init(state->msg, false, state->ppid);
508 DBG_WARNING("notifyd restarted\n");
509 tevent_req_done(req);
513 DBG_NOTICE("notifyd startup failed, rescheduling\n");
515 subreq = tevent_wakeup_send(state, state->ev,
516 tevent_timeval_current_ofs(1, 0));
517 if (tevent_req_nomem(subreq, req)) {
518 DBG_ERR("scheduling notifyd restart failed, giving up\n");
522 tevent_req_set_callback(subreq, notifyd_init_trigger, req);
526 static bool notifyd_init_recv(struct tevent_req *req)
528 struct notifyd_init_state *state = tevent_req_data(
529 req, struct notifyd_init_state);
534 static void notifyd_started(struct tevent_req *req)
538 ok = notifyd_init_recv(req);
541 DBG_ERR("Failed to restart notifyd, giving up\n");
546 static void cleanupd_stopped(struct tevent_req *req);
548 static bool cleanupd_init(struct messaging_context *msg, bool interactive,
549 struct server_id *ppid)
551 struct tevent_context *ev = messaging_tevent_context(msg);
552 struct server_id parent_id = messaging_server_id(msg);
553 struct tevent_req *req;
563 req = smbd_cleanupd_send(msg, ev, msg, parent_id.pid);
564 *ppid = messaging_server_id(msg);
565 return (req != NULL);
570 DBG_WARNING("pipe failed: %s\n", strerror(errno));
576 DBG_WARNING("fork failed: %s\n", strerror(errno));
585 rwret = sys_read(up_pipe[0], &c, 1);
589 DBG_WARNING("sys_read failed: %s\n", strerror(errno));
593 DBG_WARNING("cleanupd could not start\n");
597 DBG_WARNING("cleanupd returned %d\n", (int)c);
601 DBG_DEBUG("Started cleanupd pid=%d\n", (int)pid);
603 if (am_parent != NULL) {
604 add_child_pid(am_parent, pid);
607 *ppid = pid_to_procid(pid);
613 status = smbd_reinit_after_fork(msg, ev, true, "cleanupd");
614 if (!NT_STATUS_IS_OK(status)) {
615 DBG_WARNING("reinit_after_fork failed: %s\n",
618 sys_write(up_pipe[1], &c, 1);
623 req = smbd_cleanupd_send(msg, ev, msg, parent_id.pid);
625 DBG_WARNING("smbd_cleanupd_send failed\n");
627 sys_write(up_pipe[1], &c, 1);
632 tevent_req_set_callback(req, cleanupd_stopped, msg);
635 rwret = sys_write(up_pipe[1], &c, 1);
639 DBG_WARNING("sys_write failed: %s\n", strerror(errno));
643 DBG_WARNING("sys_write could not write result\n");
647 ok = tevent_req_poll(req, ev);
649 DBG_WARNING("tevent_req_poll returned %s\n", strerror(errno));
654 static void cleanupd_stopped(struct tevent_req *req)
658 status = smbd_cleanupd_recv(req);
659 DBG_WARNING("cleanupd stopped: %s\n", nt_errstr(status));
662 static void cleanupd_init_trigger(struct tevent_req *req);
664 struct cleanup_init_state {
666 struct tevent_context *ev;
667 struct messaging_context *msg;
668 struct server_id *ppid;
671 static struct tevent_req *cleanupd_init_send(struct tevent_context *ev,
673 struct messaging_context *msg,
674 struct server_id *ppid)
676 struct tevent_req *req = NULL;
677 struct tevent_req *subreq = NULL;
678 struct cleanup_init_state *state = NULL;
680 req = tevent_req_create(mem_ctx, &state, struct cleanup_init_state);
685 *state = (struct cleanup_init_state) {
691 subreq = tevent_wakeup_send(state, ev, tevent_timeval_current_ofs(0, 0));
692 if (tevent_req_nomem(subreq, req)) {
693 return tevent_req_post(req, ev);
696 tevent_req_set_callback(subreq, cleanupd_init_trigger, req);
700 static void cleanupd_init_trigger(struct tevent_req *subreq)
702 struct tevent_req *req = tevent_req_callback_data(
703 subreq, struct tevent_req);
704 struct cleanup_init_state *state = tevent_req_data(
705 req, struct cleanup_init_state);
708 DBG_NOTICE("Triggering cleanupd startup\n");
710 ok = tevent_wakeup_recv(subreq);
713 tevent_req_error(req, ENOMEM);
717 state->ok = cleanupd_init(state->msg, false, state->ppid);
719 DBG_WARNING("cleanupd restarted\n");
720 tevent_req_done(req);
724 DBG_NOTICE("cleanupd startup failed, rescheduling\n");
726 subreq = tevent_wakeup_send(state, state->ev,
727 tevent_timeval_current_ofs(1, 0));
728 if (tevent_req_nomem(subreq, req)) {
729 DBG_ERR("scheduling cleanupd restart failed, giving up\n");
733 tevent_req_set_callback(subreq, cleanupd_init_trigger, req);
737 static bool cleanupd_init_recv(struct tevent_req *req)
739 struct cleanup_init_state *state = tevent_req_data(
740 req, struct cleanup_init_state);
746 at most every smbd:cleanuptime seconds (default 20), we scan the BRL
747 and locking database for entries to cleanup. As a side effect this
748 also cleans up dead entries in the connections database (due to the
749 traversal in message_send_all()
751 Using a timer for this prevents a flood of traversals when a large
752 number of clients disconnect at the same time (perhaps due to a
756 static void cleanup_timeout_fn(struct tevent_context *event_ctx,
757 struct tevent_timer *te,
761 struct smbd_parent_context *parent =
762 talloc_get_type_abort(private_data,
763 struct smbd_parent_context);
765 parent->cleanup_te = NULL;
767 messaging_send_buf(parent->msg_ctx, parent->cleanupd,
768 MSG_SMB_BRL_VALIDATE, NULL, 0);
771 static void cleanupd_started(struct tevent_req *req)
775 struct smbd_parent_context *parent = tevent_req_callback_data(
776 req, struct smbd_parent_context);
778 ok = cleanupd_init_recv(req);
781 DBG_ERR("Failed to restart cleanupd, giving up\n");
785 status = messaging_send(parent->msg_ctx,
787 MSG_SMB_NOTIFY_CLEANUP,
789 if (!NT_STATUS_IS_OK(status)) {
790 DBG_ERR("messaging_send returned %s\n",
795 static void smb_direct_daemon_stopped(struct tevent_req *req);
797 static void smb_direct_daemon_accept_done(struct tevent_req *subreq)
799 struct tevent_context *ev = tevent_req_callback_data(subreq,
800 struct tevent_context);
801 struct smb_direct_connection *c = NULL;
803 struct sockaddr_storage laddr;
804 struct sockaddr_storage raddr;
810 DEBUG(0,("%s:%s: here...\n", __location__, __func__));
811 status = smb_direct_listener_accept_recv(subreq, ev,
812 &c, &fd, &laddr, &raddr);
813 /* no TALLOC_FREE(subreq)! we want to get more connections */
814 if (!NT_STATUS_IS_OK(status)) {
820 DEBUG(0,("%s:%s: here...\n", __location__, __func__));
821 status = smb_direct_connection_setup_events(c, ev);
822 if (!NT_STATUS_IS_OK(status)) {
829 //TODO: pass the caller
832 DEBUG(0,("%s:%s: here...\n", __location__, __func__));
835 static bool smb_direct_daemon_init(struct tevent_context *ev,
836 struct messaging_context *msg,
840 struct tevent_req *req;
853 struct smb_direct_listener *l = NULL;
854 struct tevent_req *subreq = NULL;
855 struct sockaddr_storage bind_addr;
858 DBG_ERR("setup listen ...\n");
861 in_addr_to_sockaddr_storage(&bind_addr, ip);
863 status = smbd_direct_listener_setup(ev, &bind_addr, &l);
864 if (!NT_STATUS_IS_OK(status)) {
865 DBG_ERR("smbd_direct_listener_setup() => %s\n",
870 // TODO: create an socketpair() with shutdown(SHUT_RD) or a pipe()
871 // and pass it allong, we'll to sendmsg() from our side
872 // and pass the metadata and the connection fd to the calling
873 // process. If the other end closes we need to free the
876 subreq = smb_direct_listener_accept_send(ev, ev, l);
877 if (subreq == NULL) {
879 status = NT_STATUS_NO_MEMORY;
880 DBG_ERR("smbd_direct_listener_setup() => %s\n",
884 tevent_req_set_callback(subreq,
885 smb_direct_daemon_accept_done,
888 req = tevent_wakeup_send(ev, ev, timeval_current_ofs(999999,0));
890 return (req != NULL);
895 DBG_WARNING("pipe failed: %s\n", strerror(errno));
901 DBG_WARNING("fork failed: %s\n", strerror(errno));
910 rwret = sys_read(up_pipe[0], &c, 1);
914 DBG_WARNING("sys_read failed: %s\n", strerror(errno));
918 DBG_WARNING("smb_direct_daemon could not start\n");
922 DBG_WARNING("smb_direct_daemon returned %d\n", (int)c);
926 DBG_DEBUG("Started smb_direct_daemon pid=%d\n", (int)pid);
928 if (am_parent != NULL) {
929 add_child_pid(am_parent, pid);
938 status = smbd_reinit_after_fork(msg, ev, true, "smb_direct_daemon");
939 if (!NT_STATUS_IS_OK(status)) {
940 DBG_WARNING("reinit_after_fork failed: %s\n",
943 sys_write(up_pipe[1], &c, 1);
948 req = smb_direct_daemon_send(ev, ev);
950 DBG_WARNING("smb_direct_daemon_send failed\n");
952 sys_write(up_pipe[1], &c, 1);
957 tevent_req_set_callback(req, smb_direct_daemon_stopped, msg);
960 rwret = sys_write(up_pipe[1], &c, 1);
964 DBG_WARNING("sys_write failed: %s\n", strerror(errno));
968 DBG_WARNING("sys_write could not write result\n");
972 ok = tevent_req_poll(req, ev);
974 DBG_WARNING("tevent_req_poll returned %s\n", strerror(errno));
977 ret = smb_direct_daemon_recv(req);
981 DBG_WARNING("smb_direct_daemon_recv returned [%s]\n",
988 static void smb_direct_daemon_stopped(struct tevent_req *req)
992 ret = smb_direct_daemon_recv(req);
993 DBG_WARNING("smb_direct_daemon stopped: [%s/%d]\n",
997 static void smb_direct_daemon_init_trigger(struct tevent_req *req);
999 struct smb_direct_daemon_init_state {
1001 struct tevent_context *ev;
1002 struct messaging_context *msg;
1003 pid_t *smb_direct_daemon_pid;
1006 static struct tevent_req *smb_direct_daemon_init_send(
1007 TALLOC_CTX *mem_ctx,
1008 struct tevent_context *ev,
1009 struct messaging_context *msg,
1010 pid_t *smb_direct_daemon_pid)
1012 struct tevent_req *req = NULL;
1013 struct tevent_req *subreq = NULL;
1014 struct smb_direct_daemon_init_state *state = NULL;
1016 req = tevent_req_create(mem_ctx, &state, struct smb_direct_daemon_init_state);
1021 *state = (struct smb_direct_daemon_init_state) {
1024 .smb_direct_daemon_pid = smb_direct_daemon_pid
1027 subreq = tevent_wakeup_send(state, ev, tevent_timeval_current_ofs(0, 0));
1028 if (tevent_req_nomem(subreq, req)) {
1029 return tevent_req_post(req, ev);
1032 tevent_req_set_callback(subreq, smb_direct_daemon_init_trigger, req);
1036 static void smb_direct_daemon_init_trigger(struct tevent_req *subreq)
1038 struct tevent_req *req = tevent_req_callback_data(
1039 subreq, struct tevent_req);
1040 struct smb_direct_daemon_init_state *state = tevent_req_data(
1041 req, struct smb_direct_daemon_init_state);
1044 DBG_NOTICE("Triggering smb_direct_daemon startup\n");
1046 ok = tevent_wakeup_recv(subreq);
1047 TALLOC_FREE(subreq);
1049 tevent_req_error(req, ENOMEM);
1053 state->ok = smb_direct_daemon_init(state->ev, state->msg,
1054 false, state->smb_direct_daemon_pid);
1056 DBG_WARNING("smb_direct_daemon restarted\n");
1057 tevent_req_done(req);
1061 DBG_NOTICE("smb_direct_daemon startup failed, rescheduling\n");
1063 subreq = tevent_wakeup_send(state, state->ev,
1064 tevent_timeval_current_ofs(1, 0));
1065 if (tevent_req_nomem(subreq, req)) {
1066 DBG_ERR("smb_direct_daemon restart failed, giving up\n");
1070 tevent_req_set_callback(subreq, smb_direct_daemon_init_trigger, req);
1074 static bool smb_direct_daemon_init_recv(struct tevent_req *req)
1076 struct smb_direct_daemon_init_state *state = tevent_req_data(
1077 req, struct smb_direct_daemon_init_state);
1078 bool ok = state->ok;
1080 tevent_req_received(req);
1085 static void smb_direct_daemon_started(struct tevent_req *req)
1089 ok = smb_direct_daemon_init_recv(req);
1092 DBG_ERR("Failed to restart smb_direct_daemon, giving up\n");
1097 static void remove_child_pid(struct smbd_parent_context *parent,
1099 bool unclean_shutdown)
1101 struct smbd_child_pid *child;
1105 for (child = parent->children; child != NULL; child = child->next) {
1106 if (child->pid == pid) {
1107 struct smbd_child_pid *tmp = child;
1108 DLIST_REMOVE(parent->children, child);
1110 parent->num_children -= 1;
1115 if (child == NULL) {
1116 /* not all forked child processes are added to the children list */
1117 DEBUG(2, ("Could not find child %d -- ignoring\n", (int)pid));
1121 if (pid == procid_to_pid(&parent->cleanupd)) {
1122 struct tevent_req *req;
1124 server_id_set_disconnected(&parent->cleanupd);
1126 DBG_WARNING("Restarting cleanupd\n");
1127 req = cleanupd_init_send(messaging_tevent_context(parent->msg_ctx),
1132 DBG_ERR("Failed to restart cleanupd\n");
1135 tevent_req_set_callback(req, cleanupd_started, parent);
1139 if (pid == procid_to_pid(&parent->notifyd)) {
1140 struct tevent_req *req;
1141 struct tevent_context *ev = messaging_tevent_context(
1144 server_id_set_disconnected(&parent->notifyd);
1146 DBG_WARNING("Restarting notifyd\n");
1147 req = notifyd_init_send(ev,
1152 DBG_ERR("Failed to restart notifyd\n");
1155 tevent_req_set_callback(req, notifyd_started, parent);
1159 if (pid == parent->smb_direct_daemon_pid) {
1160 struct tevent_req *req;
1161 struct tevent_context *ev = messaging_tevent_context(
1164 DBG_WARNING("Restarting smb_direct_daemon\n");
1166 req = smb_direct_daemon_init_send(
1167 ev, ev, parent->msg_ctx, &parent->smb_direct_daemon_pid);
1169 DBG_ERR("Failed to restart smb_direct_daemon\n");
1172 tevent_req_set_callback(req, smb_direct_daemon_started, parent);
1177 ok = cleanupdb_store_child(pid, unclean_shutdown);
1179 DBG_ERR("cleanupdb_store_child failed\n");
1183 if (!server_id_is_disconnected(&parent->cleanupd)) {
1184 status = messaging_send(parent->msg_ctx,
1186 MSG_SMB_NOTIFY_CLEANUP,
1188 if (!NT_STATUS_IS_OK(status)) {
1189 DBG_ERR("messaging_send returned %s\n",
1194 if (unclean_shutdown) {
1195 /* a child terminated uncleanly so tickle all
1196 processes to see if they can grab any of the
1199 DEBUG(3,(__location__ " Unclean shutdown of pid %u\n",
1200 (unsigned int)pid));
1201 if (parent->cleanup_te == NULL) {
1202 /* call the cleanup timer, but not too often */
1203 int cleanup_time = lp_parm_int(-1, "smbd", "cleanuptime", 20);
1204 parent->cleanup_te = tevent_add_timer(parent->ev_ctx,
1206 timeval_current_ofs(cleanup_time, 0),
1209 DEBUG(1,("Scheduled cleanup of brl and lock database after unclean shutdown\n"));
1214 /****************************************************************************
1215 Have we reached the process limit ?
1216 ****************************************************************************/
1218 static bool allowable_number_of_smbd_processes(struct smbd_parent_context *parent)
1220 int max_processes = lp_max_smbd_processes();
1225 return parent->num_children < max_processes;
1228 static void smbd_sig_chld_handler(struct tevent_context *ev,
1229 struct tevent_signal *se,
1237 struct smbd_parent_context *parent =
1238 talloc_get_type_abort(private_data,
1239 struct smbd_parent_context);
1241 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1242 bool unclean_shutdown = False;
1244 /* If the child terminated normally, assume
1245 it was an unclean shutdown unless the
1248 if (WIFEXITED(status)) {
1249 unclean_shutdown = WEXITSTATUS(status);
1251 /* If the child terminated due to a signal
1252 we always assume it was unclean.
1254 if (WIFSIGNALED(status)) {
1255 unclean_shutdown = True;
1257 remove_child_pid(parent, pid, unclean_shutdown);
1261 static void smbd_setup_sig_chld_handler(struct smbd_parent_context *parent)
1263 struct tevent_signal *se;
1265 se = tevent_add_signal(parent->ev_ctx,
1266 parent, /* mem_ctx */
1268 smbd_sig_chld_handler,
1271 exit_server("failed to setup SIGCHLD handler");
1275 static void smbd_open_socket_close_fn(struct tevent_context *ev,
1276 struct tevent_fd *fde,
1280 /* this might be the socket_wrapper swrap_close() */
1284 static void smbd_accept_connection(struct tevent_context *ev,
1285 struct tevent_fd *fde,
1289 struct smbd_open_socket *s = talloc_get_type_abort(private_data,
1290 struct smbd_open_socket);
1291 struct messaging_context *msg_ctx = s->parent->msg_ctx;
1292 struct sockaddr_storage addr;
1293 socklen_t in_addrlen = sizeof(addr);
1297 DBG_ERR("smbd got connection\n");
1299 if (!s->smb_direct) {
1300 fd = accept(s->fd, (struct sockaddr *)(void *)&addr,&in_addrlen);
1302 DBG_ERR("smbd got SMB-D connection\n");
1303 fd = smb_direct_accept(s->smb_direct_socket);
1306 if (fd == -1 && errno == EINTR)
1310 DEBUG(0,("accept: %s\n",
1314 smb_set_close_on_exec(fd);
1316 if (s->parent->interactive) {
1317 reinit_after_fork(msg_ctx, ev, true, NULL);
1318 smbd_process(ev, msg_ctx, fd, true);
1319 exit_server_cleanly("end of interactive mode");
1323 if (!allowable_number_of_smbd_processes(s->parent)) {
1330 NTSTATUS status = NT_STATUS_OK;
1333 * Can't use TALLOC_FREE here. Nulling out the argument to it
1334 * would overwrite memory we've just freed.
1336 talloc_free(s->parent);
1339 /* Stop zombies, the parent explicitly handles
1340 * them, counting worker smbds. */
1343 status = smbd_reinit_after_fork(msg_ctx, ev, true, NULL);
1344 if (!NT_STATUS_IS_OK(status)) {
1345 if (NT_STATUS_EQUAL(status,
1346 NT_STATUS_TOO_MANY_OPENED_FILES)) {
1347 DEBUG(0,("child process cannot initialize "
1348 "because too many files are open\n"));
1351 if (lp_clustering() &&
1353 status, NT_STATUS_INTERNAL_DB_ERROR) ||
1355 status, NT_STATUS_CONNECTION_REFUSED))) {
1356 DEBUG(1, ("child process cannot initialize "
1357 "because connection to CTDB "
1359 nt_errstr(status)));
1363 DEBUG(0,("reinit_after_fork() failed\n"));
1364 smb_panic("reinit_after_fork() failed");
1367 smbd_process(ev, msg_ctx, fd, false);
1369 exit_server_cleanly("end of child");
1374 DEBUG(0,("smbd_accept_connection: fork() failed: %s\n",
1378 /* The parent doesn't need this socket */
1381 /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
1382 Clear the closed fd info out of server_fd --
1383 and more importantly, out of client_fd in
1384 util_sock.c, to avoid a possible
1385 getpeername failure if we reopen the logs
1386 and use %I in the filename.
1390 add_child_pid(s->parent, pid);
1393 /* Force parent to check log size after
1394 * spawning child. Fix from
1395 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
1396 * parent smbd will log to logserver.smb. It
1397 * writes only two messages for each child
1398 * started/finished. But each child writes,
1399 * say, 50 messages also in logserver.smb,
1400 * begining with the debug_count of the
1401 * parent, before the child opens its own log
1402 * file logserver.client. In a worst case
1403 * scenario the size of logserver.smb would be
1404 * checked after about 50*50=2500 messages
1407 force_check_log_size();
1410 static bool smbd_open_one_socket(struct smbd_parent_context *parent,
1411 struct tevent_context *ev_ctx,
1412 const struct sockaddr_storage *ifss,
1415 struct smbd_open_socket *s;
1417 s = talloc(parent, struct smbd_open_socket);
1423 s->fd = open_socket_in(SOCK_STREAM,
1425 parent->sockets == NULL ? 0 : 2,
1429 DEBUG(0,("smbd_open_one_socket: open_socket_in: "
1430 "%s\n", strerror(errno)));
1433 * We ignore an error here, as we've done before
1438 /* ready to listen */
1439 set_socket_options(s->fd, "SO_KEEPALIVE");
1440 set_socket_options(s->fd, lp_socket_options());
1442 /* Set server socket to
1443 * non-blocking for the accept. */
1444 set_blocking(s->fd, False);
1446 if (listen(s->fd, SMBD_LISTEN_BACKLOG) == -1) {
1447 DEBUG(0,("smbd_open_one_socket: listen: "
1448 "%s\n", strerror(errno)));
1454 s->fde = tevent_add_fd(ev_ctx,
1456 s->fd, TEVENT_FD_READ,
1457 smbd_accept_connection,
1460 DEBUG(0,("smbd_open_one_socket: "
1461 "tevent_add_fd: %s\n",
1467 tevent_fd_set_close_fn(s->fde, smbd_open_socket_close_fn);
1469 DLIST_ADD_END(parent->sockets, s);
1474 static bool smb_direct_client_start(struct smbd_parent_context *parent)
1476 struct smbd_open_socket *s = NULL;
1477 struct smb_direct_socket *smbds = NULL;
1478 struct sdd_listen_params params;
1483 parent->smbd_ctr_socket = smb_direct_socket(parent);
1484 if (parent->smbd_ctr_socket == NULL) {
1485 DBG_ERR("smb_direct_socket failed\n");
1489 ok = smb_direct_ping(parent->smbd_ctr_socket,
1490 0xdeadbeef, &ping_data);
1492 DBG_ERR("smb_direct_ping failed\n");
1496 DBG_ERR("smb_direct_ping got [%" PRIx32 "]\n", ping_data);
1498 s = talloc_zero(parent, struct smbd_open_socket);
1503 result = smb_direct_bind(parent->smbd_ctr_socket, NULL);
1505 DBG_ERR("smb_direct_bind failed\n");
1509 /* TODO: setup smb-d params ... */
1510 ZERO_STRUCT(params);
1512 smbds = smb_direct_listen(parent->smbd_ctr_socket, ¶ms);
1513 if (smbds == NULL) {
1514 DBG_ERR("smb_direct_listen failed\n");
1518 s->smb_direct_socket = smbds;
1519 s->fd = smb_direct_socket_get_fd(smbds);
1520 s->smb_direct = true;
1522 s->fde = tevent_add_fd(parent->ev_ctx,
1524 s->fd, TEVENT_FD_READ,
1525 smbd_accept_connection,
1527 if (s->fde == NULL) {
1528 DBG_ERR("tevent_add_fd [%s]\n", strerror(errno));
1531 TALLOC_FREE(parent->smbd_ctr_socket);
1535 tevent_fd_set_close_fn(s->fde, smbd_open_socket_close_fn);
1537 DLIST_ADD_END(parent->sockets, s);
1542 /****************************************************************************
1543 Open the socket communication.
1544 ****************************************************************************/
1546 static bool open_sockets_smbd(struct smbd_parent_context *parent,
1547 struct tevent_context *ev_ctx,
1548 struct messaging_context *msg_ctx,
1549 const char *smb_ports)
1551 int num_interfaces = iface_count();
1554 unsigned dns_port = 0;
1561 smbd_setup_sig_chld_handler(parent);
1563 ports = lp_smb_ports();
1565 /* use a reasonable default set of ports - listing on 445 and 139 */
1568 l = str_list_make_v3(talloc_tos(), smb_ports, NULL);
1569 ports = discard_const_p(const char *, l);
1572 for (j = 0; ports && ports[j]; j++) {
1573 unsigned port = atoi(ports[j]);
1575 if (port == 0 || port > 0xffff) {
1576 exit_server_cleanly("Invalid port in the config or on "
1577 "the commandline specified!");
1581 if (lp_interfaces() && lp_bind_interfaces_only()) {
1582 /* We have been given an interfaces line, and been
1583 told to only bind to those interfaces. Create a
1584 socket per interface and bind to only these.
1587 /* Now open a listen socket for each of the
1589 for(i = 0; i < num_interfaces; i++) {
1590 const struct sockaddr_storage *ifss =
1591 iface_n_sockaddr_storage(i);
1593 DEBUG(0,("open_sockets_smbd: "
1594 "interface %d has NULL IP address !\n",
1599 for (j = 0; ports && ports[j]; j++) {
1600 unsigned port = atoi(ports[j]);
1602 /* Keep the first port for mDNS service
1605 if (dns_port == 0) {
1609 if (!smbd_open_one_socket(parent,
1618 /* Just bind to 0.0.0.0 - accept connections
1621 const char *sock_addr;
1623 const char *sock_ptr;
1626 sock_addr = "::,0.0.0.0";
1628 sock_addr = "0.0.0.0";
1631 for (sock_ptr=sock_addr;
1632 next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,"); ) {
1633 for (j = 0; ports && ports[j]; j++) {
1634 struct sockaddr_storage ss;
1635 unsigned port = atoi(ports[j]);
1637 /* Keep the first port for mDNS service
1640 if (dns_port == 0) {
1644 /* open an incoming socket */
1645 if (!interpret_string_addr(&ss, sock_tok,
1646 AI_NUMERICHOST|AI_PASSIVE)) {
1651 * If we fail to open any sockets
1652 * in this loop the parent-sockets == NULL
1653 * case below will prevent us from starting.
1656 (void)smbd_open_one_socket(parent,
1664 if (lp_parm_bool(-1, "smbdirect", "enable", true)) {
1667 ok = smb_direct_client_start(parent);
1669 DBG_ERR("smb_direct_client_start failed\n");
1674 if (parent->sockets == NULL) {
1675 DEBUG(0,("open_sockets_smbd: No "
1676 "sockets available to bind to.\n"));
1680 /* Listen to messages */
1682 messaging_register(msg_ctx, NULL, MSG_SHUTDOWN, msg_exit_server);
1683 messaging_register(msg_ctx, ev_ctx, MSG_SMB_CONF_UPDATED,
1684 smbd_parent_conf_updated);
1685 messaging_register(msg_ctx, NULL, MSG_SMB_STAT_CACHE_DELETE,
1686 smb_stat_cache_delete);
1687 messaging_register(msg_ctx, NULL, MSG_DEBUG, smbd_msg_debug);
1688 messaging_register(msg_ctx, NULL, MSG_SMB_FORCE_TDIS,
1689 smb_parent_send_to_children);
1690 messaging_register(msg_ctx, NULL, MSG_SMB_KILL_CLIENT_IP,
1691 smb_parent_send_to_children);
1692 messaging_register(msg_ctx, NULL, MSG_SMB_TELL_NUM_CHILDREN,
1693 smb_tell_num_children);
1695 messaging_register(msg_ctx, NULL,
1696 ID_CACHE_DELETE, smbd_parent_id_cache_delete);
1697 messaging_register(msg_ctx, NULL,
1698 ID_CACHE_KILL, smbd_parent_id_cache_kill);
1699 messaging_register(msg_ctx, NULL, MSG_SMB_NOTIFY_STARTED,
1700 smb_parent_send_to_children);
1702 #ifdef CLUSTER_SUPPORT
1703 if (lp_clustering()) {
1704 struct ctdbd_connection *conn = messaging_ctdb_connection();
1706 register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE,
1707 smbd_parent_ctdb_reconfigured, msg_ctx);
1708 register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY,
1709 smbd_parent_ctdb_reconfigured, msg_ctx);
1714 messaging_register(msg_ctx, NULL, MSG_SMB_INJECT_FAULT,
1718 if (lp_multicast_dns_register() && (dns_port != 0)) {
1719 #ifdef WITH_DNSSD_SUPPORT
1720 smbd_setup_mdns_registration(ev_ctx,
1723 #ifdef WITH_AVAHI_SUPPORT
1726 avahi_conn = avahi_start_register(ev_ctx,
1729 if (avahi_conn == NULL) {
1730 DEBUG(10, ("avahi_start_register failed\n"));
1740 handle stdin becoming readable when we are in --foreground mode
1742 static void smbd_stdin_handler(struct tevent_context *ev,
1743 struct tevent_fd *fde,
1748 if (read(0, &c, 1) != 1) {
1749 /* we have reached EOF on stdin, which means the
1750 parent has exited. Shutdown the server */
1751 exit_server_cleanly("EOF on stdin");
1755 struct smbd_parent_tevent_trace_state {
1759 static void smbd_parent_tevent_trace_callback(enum tevent_trace_point point,
1762 struct smbd_parent_tevent_trace_state *state =
1763 (struct smbd_parent_tevent_trace_state *)private_data;
1766 case TEVENT_TRACE_BEFORE_WAIT:
1768 case TEVENT_TRACE_AFTER_WAIT:
1770 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
1771 TALLOC_FREE(state->frame);
1772 state->frame = talloc_stackframe();
1774 case TEVENT_TRACE_AFTER_LOOP_ONCE:
1775 TALLOC_FREE(state->frame);
1782 static void smbd_parent_loop(struct tevent_context *ev_ctx,
1783 struct smbd_parent_context *parent)
1785 struct smbd_parent_tevent_trace_state trace_state = {
1790 tevent_set_trace_callback(ev_ctx, smbd_parent_tevent_trace_callback,
1793 /* now accept incoming connections - forking a new process
1794 for each incoming connection */
1795 DEBUG(2,("waiting for connections\n"));
1797 ret = tevent_loop_wait(ev_ctx);
1799 DEBUG(0, ("tevent_loop_wait failed: %d, %s, exiting\n",
1800 ret, strerror(errno)));
1803 TALLOC_FREE(trace_state.frame);
1805 /* NOTREACHED return True; */
1809 /****************************************************************************
1810 Initialise connect, service and file structs.
1811 ****************************************************************************/
1813 static bool init_structs(void )
1816 * Set the machine NETBIOS name if not already
1817 * set from the config file.
1823 if (!secrets_init())
1829 static void smbd_parent_sig_term_handler(struct tevent_context *ev,
1830 struct tevent_signal *se,
1836 exit_server_cleanly("termination signal");
1839 static void smbd_parent_sig_hup_handler(struct tevent_context *ev,
1840 struct tevent_signal *se,
1846 struct smbd_parent_context *parent =
1847 talloc_get_type_abort(private_data,
1848 struct smbd_parent_context);
1850 change_to_root_user();
1851 DEBUG(1,("parent: Reloading services after SIGHUP\n"));
1852 reload_services(NULL, NULL, false);
1854 printing_subsystem_update(parent->ev_ctx, parent->msg_ctx, true);
1857 struct smbd_claim_version_state {
1858 TALLOC_CTX *mem_ctx;
1862 static void smbd_claim_version_parser(const struct g_lock_rec *locks,
1864 const uint8_t *data,
1868 struct smbd_claim_version_state *state = private_data;
1871 state->version = NULL;
1874 if (data[datalen-1] != '\0') {
1875 DBG_WARNING("Invalid samba version\n");
1876 dump_data(DBGLVL_WARNING, data, datalen);
1877 state->version = NULL;
1880 state->version = talloc_strdup(state->mem_ctx, (const char *)data);
1883 static NTSTATUS smbd_claim_version(struct messaging_context *msg,
1884 const char *version)
1886 const char *name = "samba_version_string";
1887 struct smbd_claim_version_state state;
1888 struct g_lock_ctx *ctx;
1891 ctx = g_lock_ctx_init(msg, msg);
1893 DBG_WARNING("g_lock_ctx_init failed\n");
1894 return NT_STATUS_UNSUCCESSFUL;
1897 status = g_lock_lock(ctx, string_term_tdb_data(name), G_LOCK_READ,
1898 (struct timeval) { .tv_sec = 60 });
1899 if (!NT_STATUS_IS_OK(status)) {
1900 DBG_WARNING("g_lock_lock(G_LOCK_READ) failed: %s\n",
1906 state = (struct smbd_claim_version_state) { .mem_ctx = ctx };
1908 status = g_lock_dump(ctx, string_term_tdb_data(name),
1909 smbd_claim_version_parser, &state);
1910 if (!NT_STATUS_IS_OK(status) &&
1911 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1912 DBG_ERR("Could not read samba_version_string\n");
1913 g_lock_unlock(ctx, string_term_tdb_data(name));
1918 if ((state.version != NULL) && (strcmp(version, state.version) == 0)) {
1920 * Leave the read lock for us around. Someone else already
1921 * set the version correctly
1924 return NT_STATUS_OK;
1927 status = g_lock_lock(ctx, string_term_tdb_data(name), G_LOCK_WRITE,
1928 (struct timeval) { .tv_sec = 60 });
1929 if (!NT_STATUS_IS_OK(status)) {
1930 DBG_WARNING("g_lock_lock(G_LOCK_WRITE) failed: %s\n",
1932 DBG_ERR("smbd %s already running, refusing to start "
1933 "version %s\n", state.version, version);
1935 return NT_STATUS_SXS_VERSION_CONFLICT;
1938 status = g_lock_write_data(ctx, string_term_tdb_data(name),
1939 (const uint8_t *)version,
1941 if (!NT_STATUS_IS_OK(status)) {
1942 DBG_WARNING("g_lock_write_data failed: %s\n",
1948 status = g_lock_lock(ctx, string_term_tdb_data(name), G_LOCK_READ,
1949 (struct timeval) { .tv_sec = 60 });
1950 if (!NT_STATUS_IS_OK(status)) {
1951 DBG_WARNING("g_lock_lock(G_LOCK_READ) failed: %s\n",
1958 * Leave "ctx" dangling so that g_lock.tdb keeps opened.
1960 return NT_STATUS_OK;
1963 /****************************************************************************
1965 ****************************************************************************/
1967 /* Declare prototype for build_options() to avoid having to run it through
1968 mkproto.h. Mixing $(builddir) and $(srcdir) source files in the current
1969 prototype generation system is too complicated. */
1971 extern void build_options(bool screen);
1973 int main(int argc,const char *argv[])
1975 /* shall I run as a daemon */
1976 bool is_daemon = false;
1977 bool interactive = false;
1979 bool no_process_group = false;
1980 bool log_stdout = false;
1982 char *profile_level = NULL;
1985 bool print_build_options = False;
1986 struct server_id main_server_id = {0};
1991 OPT_NO_PROCESS_GROUP,
1994 struct poptOption long_options[] = {
1996 {"daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
1997 {"interactive", 'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE, "Run interactive (not a daemon) and log to stdout"},
1998 {"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Run daemon in foreground (for daemontools, etc.)" },
1999 {"no-process-group", '\0', POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
2000 {"log-stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
2001 {"build-options", 'b', POPT_ARG_NONE, NULL, 'b', "Print build options" },
2002 {"port", 'p', POPT_ARG_STRING, &ports, 0, "Listen on the specified ports"},
2003 {"profiling-level", 'P', POPT_ARG_STRING, &profile_level, 0, "Set profiling level","PROFILE_LEVEL"},
2007 struct smbd_parent_context *parent = NULL;
2010 struct tevent_context *ev_ctx;
2011 struct messaging_context *msg_ctx;
2012 struct server_id server_id;
2013 struct tevent_signal *se;
2014 int profiling_level;
2015 char *np_dir = NULL;
2016 static const struct smbd_shim smbd_shim_fns =
2018 .cancel_pending_lock_requests_by_fid = smbd_cancel_pending_lock_requests_by_fid,
2019 .send_stat_cache_delete_message = smbd_send_stat_cache_delete_message,
2020 .change_to_root_user = smbd_change_to_root_user,
2021 .become_authenticated_pipe_user = smbd_become_authenticated_pipe_user,
2022 .unbecome_authenticated_pipe_user = smbd_unbecome_authenticated_pipe_user,
2024 .contend_level2_oplocks_begin = smbd_contend_level2_oplocks_begin,
2025 .contend_level2_oplocks_end = smbd_contend_level2_oplocks_end,
2027 .become_root = smbd_become_root,
2028 .unbecome_root = smbd_unbecome_root,
2030 .exit_server = smbd_exit_server,
2031 .exit_server_cleanly = smbd_exit_server_cleanly,
2035 * Do this before any other talloc operation
2037 talloc_enable_null_tracking();
2038 frame = talloc_stackframe();
2040 setup_logging(argv[0], DEBUG_DEFAULT_STDOUT);
2044 set_smbd_shim(&smbd_shim_fns);
2046 smbd_init_globals();
2050 #ifdef HAVE_SET_AUTH_PARAMETERS
2051 set_auth_parameters(argc,argv);
2054 pc = poptGetContext("smbd", argc, argv, long_options, 0);
2055 while((opt = poptGetNextOpt(pc)) != -1) {
2060 case OPT_INTERACTIVE:
2066 case OPT_NO_PROCESS_GROUP:
2067 no_process_group = true;
2069 case OPT_LOG_STDOUT:
2073 print_build_options = True;
2076 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
2077 poptBadOption(pc, 0), poptStrerror(opt));
2078 poptPrintUsage(pc, stderr, 0);
2082 poptFreeContext(pc);
2090 setup_logging(argv[0], DEBUG_STDOUT);
2092 setup_logging(argv[0], DEBUG_FILE);
2095 if (print_build_options) {
2096 build_options(True); /* Display output to screen as well as debug */
2101 /* needed for SecureWare on SCO */
2105 set_remote_machine_name("smbd", False);
2107 if (interactive && (DEBUGLEVEL >= 9)) {
2108 talloc_enable_leak_report();
2111 if (log_stdout && Fork) {
2112 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
2117 * We want to die early if we can't open /dev/urandom
2119 generate_random_buffer(NULL, 0);
2121 /* get initial effective uid and gid */
2124 /* make absolutely sure we run as root - to handle cases where people
2125 are crazy enough to have it setuid */
2126 gain_root_privilege();
2127 gain_root_group_privilege();
2130 dump_core_setup("smbd", lp_logfile(talloc_tos()));
2132 /* we are never interested in SIGPIPE */
2133 BlockSignals(True,SIGPIPE);
2136 /* we are never interested in SIGFPE */
2137 BlockSignals(True,SIGFPE);
2140 #if defined(SIGUSR2)
2141 /* We are no longer interested in USR2 */
2142 BlockSignals(True,SIGUSR2);
2145 /* POSIX demands that signals are inherited. If the invoking process has
2146 * these signals masked, we will have problems, as we won't recieve them. */
2147 BlockSignals(False, SIGHUP);
2148 BlockSignals(False, SIGUSR1);
2149 BlockSignals(False, SIGTERM);
2151 /* Ensure we leave no zombies until we
2152 * correctly set up child handling below. */
2156 /* we want total control over the permissions on created files,
2157 so set our umask to 0 */
2162 DEBUG(0,("smbd version %s started.\n", samba_version_string()));
2163 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
2165 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
2166 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
2168 /* Output the build options to the debug log */
2169 build_options(False);
2171 if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4) {
2172 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
2176 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
2177 DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
2181 if (!cluster_probe_ok()) {
2185 /* Init the security context and global current_user */
2189 * Initialize the event context. The event context needs to be
2190 * initialized before the messaging context, cause the messaging
2191 * context holds an event context.
2193 ev_ctx = server_event_context();
2194 if (ev_ctx == NULL) {
2199 * Init the messaging context
2200 * FIXME: This should only call messaging_init()
2202 msg_ctx = server_messaging_context();
2203 if (msg_ctx == NULL) {
2208 * Reloading of the printers will not work here as we don't have a
2209 * server info and rpc services set up. It will be called later.
2211 if (!reload_services(NULL, NULL, false)) {
2215 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
2216 && !lp_parm_bool(-1, "server role check", "inhibit", false)) {
2217 DEBUG(0, ("server role = 'active directory domain controller' not compatible with running smbd standalone. \n"));
2218 DEBUGADD(0, ("You should start 'samba' instead, and it will control starting smbd if required\n"));
2222 /* ...NOTE... Log files are working from this point! */
2224 DEBUG(3,("loaded services\n"));
2228 if (!profile_setup(msg_ctx, False)) {
2229 DEBUG(0,("ERROR: failed to setup profiling\n"));
2233 if (profile_level != NULL) {
2234 profiling_level = atoi(profile_level);
2236 profiling_level = lp_smbd_profiling_level();
2238 main_server_id = messaging_server_id(msg_ctx);
2239 set_profile_level(profiling_level, &main_server_id);
2241 if (!is_daemon && !is_a_socket(0)) {
2243 DEBUG(3, ("Standard input is not a socket, "
2244 "assuming -D option\n"));
2248 * Setting is_daemon here prevents us from eventually calling
2249 * the open_sockets_inetd()
2255 if (is_daemon && !interactive) {
2256 DEBUG(3, ("Becoming a daemon.\n"));
2257 become_daemon(Fork, no_process_group, log_stdout);
2262 * If we're interactive we want to set our own process group for
2263 * signal management.
2265 if (interactive && !no_process_group)
2266 setpgid( (pid_t)0, (pid_t)0);
2269 if (!directory_exist(lp_lock_directory()))
2270 mkdir(lp_lock_directory(), 0755);
2272 if (!directory_exist(lp_pid_directory()))
2273 mkdir(lp_pid_directory(), 0755);
2276 pidfile_create(lp_pid_directory(), "smbd");
2278 status = reinit_after_fork(msg_ctx, ev_ctx, false, NULL);
2279 if (!NT_STATUS_IS_OK(status)) {
2280 exit_daemon("reinit_after_fork() failed", map_errno_from_nt_status(status));
2285 * Do not initialize the parent-child-pipe before becoming a
2286 * daemon: this is used to detect a died parent in the child
2289 status = init_before_fork();
2290 if (!NT_STATUS_IS_OK(status)) {
2291 exit_daemon(nt_errstr(status), map_errno_from_nt_status(status));
2295 parent = talloc_zero(ev_ctx, struct smbd_parent_context);
2297 exit_server("talloc(struct smbd_parent_context) failed");
2299 parent->interactive = interactive;
2300 parent->ev_ctx = ev_ctx;
2301 parent->msg_ctx = msg_ctx;
2304 se = tevent_add_signal(parent->ev_ctx,
2307 smbd_parent_sig_term_handler,
2310 exit_server("failed to setup SIGTERM handler");
2312 se = tevent_add_signal(parent->ev_ctx,
2315 smbd_parent_sig_hup_handler,
2318 exit_server("failed to setup SIGHUP handler");
2321 /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
2323 if (smbd_memcache() == NULL) {
2324 exit_daemon("no memcache available", EACCES);
2327 memcache_set_global(smbd_memcache());
2329 /* Initialise the password backed before the global_sam_sid
2330 to ensure that we fetch from ldap before we make a domain sid up */
2332 if(!initialize_password_db(false, ev_ctx))
2335 if (!secrets_init()) {
2336 exit_daemon("smbd can not open secrets.tdb", EACCES);
2339 if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) {
2340 struct loadparm_context *lp_ctx = loadparm_init_s3(NULL, loadparm_s3_helpers());
2341 if (!open_schannel_session_store(NULL, lp_ctx)) {
2342 exit_daemon("ERROR: Samba cannot open schannel store for secured NETLOGON operations.", EACCES);
2344 TALLOC_FREE(lp_ctx);
2347 if(!get_global_sam_sid()) {
2348 exit_daemon("Samba cannot create a SAM SID", EACCES);
2351 server_id = messaging_server_id(msg_ctx);
2352 status = smbXsrv_version_global_init(&server_id);
2353 if (!NT_STATUS_IS_OK(status)) {
2354 exit_daemon("Samba cannot init server context", EACCES);
2357 status = smbXsrv_client_global_init();
2358 if (!NT_STATUS_IS_OK(status)) {
2359 exit_daemon("Samba cannot init clients context", EACCES);
2362 status = smbXsrv_session_global_init(msg_ctx);
2363 if (!NT_STATUS_IS_OK(status)) {
2364 exit_daemon("Samba cannot init session context", EACCES);
2367 status = smbXsrv_tcon_global_init();
2368 if (!NT_STATUS_IS_OK(status)) {
2369 exit_daemon("Samba cannot init tcon context", EACCES);
2372 if (!locking_init())
2373 exit_daemon("Samba cannot init locking", EACCES);
2375 if (!leases_db_init(false)) {
2376 exit_daemon("Samba cannot init leases", EACCES);
2379 if (!smbd_notifyd_init(msg_ctx, interactive, &parent->notifyd)) {
2380 exit_daemon("Samba cannot init notification", EACCES);
2383 if (!cleanupd_init(msg_ctx, interactive, &parent->cleanupd)) {
2384 exit_daemon("Samba cannot init the cleanupd", EACCES);
2387 if (!smb_direct_daemon_init(ev_ctx, msg_ctx, interactive,
2388 &parent->smb_direct_daemon_pid))
2390 exit_daemon("Samba cannot init the smb_direct_daemon", EACCES);
2393 if (!messaging_parent_dgm_cleanup_init(msg_ctx)) {
2397 if (!smbd_scavenger_init(NULL, msg_ctx, ev_ctx)) {
2398 exit_daemon("Samba cannot init scavenging", EACCES);
2401 if (!W_ERROR_IS_OK(registry_init_full()))
2402 exit_daemon("Samba cannot init registry", EACCES);
2404 /* Open the share_info.tdb here, so we don't have to open
2405 after the fork on every single connection. This is a small
2406 performance improvment and reduces the total number of system
2408 if (!share_info_db_init()) {
2409 exit_daemon("ERROR: failed to load share info db.", EACCES);
2412 status = init_system_session_info(NULL);
2413 if (!NT_STATUS_IS_OK(status)) {
2414 DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
2415 nt_errstr(status)));
2419 if (!init_guest_session_info(NULL)) {
2420 DEBUG(0,("ERROR: failed to setup guest info.\n"));
2424 if (!file_init_global()) {
2425 DEBUG(0, ("ERROR: file_init_global() failed\n"));
2428 status = smbXsrv_open_global_init();
2429 if (!NT_STATUS_IS_OK(status)) {
2430 exit_daemon("Samba cannot init global open", map_errno_from_nt_status(status));
2433 if (lp_clustering() && !lp_allow_unsafe_cluster_upgrade()) {
2434 status = smbd_claim_version(msg_ctx, samba_version_string());
2435 if (!NT_STATUS_IS_OK(status)) {
2436 DBG_WARNING("Could not claim version: %s\n",
2442 /* This MUST be done before start_epmd() because otherwise
2443 * start_epmd() forks and races against dcesrv_ep_setup() to
2444 * call directory_create_or_exist() */
2445 if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
2446 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
2447 lp_ncalrpc_dir(), strerror(errno)));
2451 np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
2453 DEBUG(0, ("%s: Out of memory\n", __location__));
2457 if (!directory_create_or_exist_strict(np_dir, geteuid(), 0700)) {
2458 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
2459 np_dir, strerror(errno)));
2463 if (is_daemon && !interactive) {
2464 if (rpc_epmapper_daemon() == RPC_DAEMON_FORK) {
2465 start_epmd(ev_ctx, msg_ctx);
2469 if (!dcesrv_ep_setup(ev_ctx, msg_ctx)) {
2470 exit_daemon("Samba cannot setup ep pipe", EACCES);
2473 if (is_daemon && !interactive) {
2474 daemon_ready("smbd");
2477 /* only start other daemons if we are running as a daemon
2478 * -- bad things will happen if smbd is launched via inetd
2479 * and we fork a copy of ourselves here */
2480 if (is_daemon && !interactive) {
2482 if (rpc_lsasd_daemon() == RPC_DAEMON_FORK) {
2483 start_lsasd(ev_ctx, msg_ctx);
2486 if (rpc_fss_daemon() == RPC_DAEMON_FORK) {
2487 start_fssd(ev_ctx, msg_ctx);
2490 if (!lp__disable_spoolss() &&
2491 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
2492 bool bgq = lp_parm_bool(-1, "smbd", "backgroundqueue", true);
2494 if (!printing_subsystem_init(ev_ctx, msg_ctx, true, bgq)) {
2495 exit_daemon("Samba failed to init printing subsystem", EACCES);
2499 #ifdef WITH_SPOTLIGHT
2500 if ((rpc_mdssvc_mode() == RPC_SERVICE_MODE_EXTERNAL) &&
2501 (rpc_mdssd_daemon() == RPC_DAEMON_FORK)) {
2502 start_mdssd(ev_ctx, msg_ctx);
2505 } else if (!lp__disable_spoolss() &&
2506 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
2507 if (!printing_subsystem_init(ev_ctx, msg_ctx, false, false)) {
2518 /* Started from inetd. fd 0 is the socket. */
2519 /* We will abort gracefully when the client or remote system
2523 /* close stdin, stdout (if not logging to it), but not stderr */
2524 close_low_fds(true, !debug_get_output_is_stdout(), false);
2531 smbd_setup_sig_chld_handler(parent);
2533 smbd_process(ev_ctx, msg_ctx, sock, true);
2535 exit_server_cleanly(NULL);
2539 if (!open_sockets_smbd(parent, ev_ctx, msg_ctx, ports))
2540 exit_server("open_sockets_smbd() failed");
2542 /* do a printer update now that all messaging has been set up,
2543 * before we allow clients to start connecting */
2544 if (!lp__disable_spoolss() &&
2545 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
2546 printing_subsystem_update(ev_ctx, msg_ctx, false);
2550 /* make sure we always have a valid stackframe */
2551 frame = talloc_stackframe();
2554 /* if we are running in the foreground then look for
2555 EOF on stdin, and exit if it happens. This allows
2556 us to die if the parent process dies
2557 Only do this on a pipe or socket, no other device.
2560 if (fstat(0, &st) != 0) {
2563 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
2564 tevent_add_fd(ev_ctx,
2573 smbd_parent_loop(ev_ctx, parent);
2575 exit_server_cleanly(NULL);