source: vendor/current/source3/smbd/server.c

Last change on this file was 989, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.7

File size: 44.4 KB
Line 
1/*
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
9
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.
14
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.
19
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/>.
22*/
23
24#include "includes.h"
25#include "system/filesys.h"
26#include "popt_common.h"
27#include "smbd/smbd.h"
28#include "smbd/globals.h"
29#include "registry/reg_init_full.h"
30#include "libcli/auth/schannel.h"
31#include "secrets.h"
32#include "../lib/util/memcache.h"
33#include "ctdbd_conn.h"
34#include "util_cluster.h"
35#include "printing/queue_process.h"
36#include "rpc_server/rpc_service_setup.h"
37#include "rpc_server/rpc_config.h"
38#include "serverid.h"
39#include "passdb.h"
40#include "auth.h"
41#include "messages.h"
42#include "smbprofile.h"
43#include "lib/id_cache.h"
44#include "lib/param/param.h"
45#include "lib/background.h"
46#include "lib/conn_tdb.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
55#ifdef CLUSTER_SUPPORT
56#include "ctdb_protocol.h"
57#endif
58
59struct smbd_open_socket;
60struct smbd_child_pid;
61
62struct smbd_parent_context {
63 bool interactive;
64
65 struct tevent_context *ev_ctx;
66 struct messaging_context *msg_ctx;
67
68 /* the list of listening sockets */
69 struct smbd_open_socket *sockets;
70
71 /* the list of current child processes */
72 struct smbd_child_pid *children;
73 size_t num_children;
74
75 struct server_id cleanupd;
76
77 struct tevent_timer *cleanup_te;
78};
79
80struct smbd_open_socket {
81 struct smbd_open_socket *prev, *next;
82 struct smbd_parent_context *parent;
83 int fd;
84 struct tevent_fd *fde;
85};
86
87struct smbd_child_pid {
88 struct smbd_child_pid *prev, *next;
89 pid_t pid;
90};
91
92extern void start_epmd(struct tevent_context *ev_ctx,
93 struct messaging_context *msg_ctx);
94
95extern void start_lsasd(struct tevent_context *ev_ctx,
96 struct messaging_context *msg_ctx);
97
98extern void start_fssd(struct tevent_context *ev_ctx,
99 struct messaging_context *msg_ctx);
100
101extern void start_mdssd(struct tevent_context *ev_ctx,
102 struct messaging_context *msg_ctx);
103
104/*******************************************************************
105 What to do when smb.conf is updated.
106 ********************************************************************/
107
108static void smbd_parent_conf_updated(struct messaging_context *msg,
109 void *private_data,
110 uint32_t msg_type,
111 struct server_id server_id,
112 DATA_BLOB *data)
113{
114 struct tevent_context *ev_ctx =
115 talloc_get_type_abort(private_data, struct tevent_context);
116
117 DEBUG(10,("smbd_parent_conf_updated: Got message saying smb.conf was "
118 "updated. Reloading.\n"));
119 change_to_root_user();
120 reload_services(NULL, NULL, false);
121 printing_subsystem_update(ev_ctx, msg, false);
122}
123
124/*******************************************************************
125 Delete a statcache entry.
126 ********************************************************************/
127
128static void smb_stat_cache_delete(struct messaging_context *msg,
129 void *private_data,
130 uint32_t msg_tnype,
131 struct server_id server_id,
132 DATA_BLOB *data)
133{
134 const char *name = (const char *)data->data;
135 DEBUG(10,("smb_stat_cache_delete: delete name %s\n", name));
136 stat_cache_delete(name);
137}
138
139/****************************************************************************
140 Send a SIGTERM to our process group.
141*****************************************************************************/
142
143static void killkids(void)
144{
145 if(am_parent) kill(0,SIGTERM);
146}
147
148static void msg_exit_server(struct messaging_context *msg,
149 void *private_data,
150 uint32_t msg_type,
151 struct server_id server_id,
152 DATA_BLOB *data)
153{
154 DEBUG(3, ("got a SHUTDOWN message\n"));
155 exit_server_cleanly(NULL);
156}
157
158#ifdef DEVELOPER
159static void msg_inject_fault(struct messaging_context *msg,
160 void *private_data,
161 uint32_t msg_type,
162 struct server_id src,
163 DATA_BLOB *data)
164{
165 int sig;
166 struct server_id_buf tmp;
167
168 if (data->length != sizeof(sig)) {
169 DEBUG(0, ("Process %s sent bogus signal injection request\n",
170 server_id_str_buf(src, &tmp)));
171 return;
172 }
173
174 sig = *(int *)data->data;
175 if (sig == -1) {
176 exit_server("internal error injected");
177 return;
178 }
179
180#if HAVE_STRSIGNAL
181 DEBUG(0, ("Process %s requested injection of signal %d (%s)\n",
182 server_id_str_buf(src, &tmp), sig, strsignal(sig)));
183#else
184 DEBUG(0, ("Process %s requested injection of signal %d\n",
185 server_id_str_buf(src, &tmp), sig));
186#endif
187
188 kill(getpid(), sig);
189}
190#endif /* DEVELOPER */
191
192static NTSTATUS messaging_send_to_children(struct messaging_context *msg_ctx,
193 uint32_t msg_type, DATA_BLOB* data)
194{
195 NTSTATUS status;
196 struct smbd_parent_context *parent = am_parent;
197 struct smbd_child_pid *child;
198
199 if (parent == NULL) {
200 return NT_STATUS_INTERNAL_ERROR;
201 }
202
203 for (child = parent->children; child != NULL; child = child->next) {
204 status = messaging_send(parent->msg_ctx,
205 pid_to_procid(child->pid),
206 msg_type, data);
207 if (!NT_STATUS_IS_OK(status)) {
208 return status;
209 }
210 }
211 return NT_STATUS_OK;
212}
213
214static void smb_parent_send_to_children(struct messaging_context *ctx,
215 void* data,
216 uint32_t msg_type,
217 struct server_id srv_id,
218 DATA_BLOB* msg_data)
219{
220 messaging_send_to_children(ctx, msg_type, msg_data);
221}
222
223/*
224 * Parent smbd process sets its own debug level first and then
225 * sends a message to all the smbd children to adjust their debug
226 * level to that of the parent.
227 */
228
229static void smbd_msg_debug(struct messaging_context *msg_ctx,
230 void *private_data,
231 uint32_t msg_type,
232 struct server_id server_id,
233 DATA_BLOB *data)
234{
235 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
236
237 messaging_send_to_children(msg_ctx, MSG_DEBUG, data);
238}
239
240static void smbd_parent_id_cache_kill(struct messaging_context *msg_ctx,
241 void *private_data,
242 uint32_t msg_type,
243 struct server_id server_id,
244 DATA_BLOB* data)
245{
246 const char *msg = (data && data->data)
247 ? (const char *)data->data : "<NULL>";
248 struct id_cache_ref id;
249
250 if (!id_cache_ref_parse(msg, &id)) {
251 DEBUG(0, ("Invalid ?ID: %s\n", msg));
252 return;
253 }
254
255 id_cache_delete_from_cache(&id);
256
257 messaging_send_to_children(msg_ctx, msg_type, data);
258}
259
260static void smbd_parent_id_cache_delete(struct messaging_context *ctx,
261 void* data,
262 uint32_t msg_type,
263 struct server_id srv_id,
264 DATA_BLOB* msg_data)
265{
266 id_cache_delete_message(ctx, data, msg_type, srv_id, msg_data);
267
268 messaging_send_to_children(ctx, msg_type, msg_data);
269}
270
271#ifdef CLUSTER_SUPPORT
272static int smbd_parent_ctdb_reconfigured(
273 uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid,
274 const uint8_t *msg, size_t msglen, void *private_data)
275{
276 struct messaging_context *msg_ctx = talloc_get_type_abort(
277 private_data, struct messaging_context);
278
279 DEBUG(10, ("Got %s message\n", (dst_srvid == CTDB_SRVID_RECONFIGURE)
280 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
281
282 /*
283 * Someone from the family died, validate our locks
284 */
285
286 if (am_parent) {
287 messaging_send_buf(msg_ctx, am_parent->cleanupd,
288 MSG_SMB_BRL_VALIDATE, NULL, 0);
289 }
290
291 return 0;
292}
293#endif
294
295static void add_child_pid(struct smbd_parent_context *parent,
296 pid_t pid)
297{
298 struct smbd_child_pid *child;
299
300 child = talloc_zero(parent, struct smbd_child_pid);
301 if (child == NULL) {
302 DEBUG(0, ("Could not add child struct -- malloc failed\n"));
303 return;
304 }
305 child->pid = pid;
306 DLIST_ADD(parent->children, child);
307 parent->num_children += 1;
308}
309
310static void smb_tell_num_children(struct messaging_context *ctx, void *data,
311 uint32_t msg_type, struct server_id srv_id,
312 DATA_BLOB *msg_data)
313{
314 uint8_t buf[sizeof(uint32_t)];
315
316 if (am_parent) {
317 SIVAL(buf, 0, am_parent->num_children);
318 messaging_send_buf(ctx, srv_id, MSG_SMB_NUM_CHILDREN,
319 buf, sizeof(buf));
320 }
321}
322
323static void notifyd_stopped(struct tevent_req *req);
324
325static struct tevent_req *notifyd_req(struct messaging_context *msg_ctx,
326 struct tevent_context *ev)
327{
328 struct tevent_req *req;
329 sys_notify_watch_fn sys_notify_watch = NULL;
330 struct sys_notify_context *sys_notify_ctx = NULL;
331
332 if (lp_kernel_change_notify()) {
333
334#ifdef HAVE_INOTIFY
335 if (lp_parm_bool(-1, "notify", "inotify", true)) {
336 sys_notify_watch = inotify_watch;
337 }
338#endif
339
340#ifdef HAVE_FAM
341 if (lp_parm_bool(-1, "notify", "fam",
342 (sys_notify_watch == NULL))) {
343 sys_notify_watch = fam_watch;
344 }
345#endif
346 }
347
348 if (sys_notify_watch != NULL) {
349 sys_notify_ctx = sys_notify_context_create(msg_ctx, ev);
350 if (sys_notify_ctx == NULL) {
351 return NULL;
352 }
353 }
354
355 req = notifyd_send(msg_ctx, ev, msg_ctx,
356 messaging_ctdbd_connection(),
357 sys_notify_watch, sys_notify_ctx);
358 if (req == NULL) {
359 TALLOC_FREE(sys_notify_ctx);
360 return NULL;
361 }
362 tevent_req_set_callback(req, notifyd_stopped, msg_ctx);
363
364 return req;
365}
366
367static void notifyd_stopped(struct tevent_req *req)
368{
369 int ret;
370
371 ret = notifyd_recv(req);
372 TALLOC_FREE(req);
373 DEBUG(1, ("notifyd stopped: %s\n", strerror(ret)));
374}
375
376static bool smbd_notifyd_init(struct messaging_context *msg, bool interactive)
377{
378 struct tevent_context *ev = messaging_tevent_context(msg);
379 struct tevent_req *req;
380 pid_t pid;
381 NTSTATUS status;
382
383 if (interactive) {
384 req = notifyd_req(msg, ev);
385 return (req != NULL);
386 }
387
388 pid = fork();
389 if (pid == -1) {
390 DEBUG(1, ("%s: fork failed: %s\n", __func__,
391 strerror(errno)));
392 return false;
393 }
394
395 if (pid != 0) {
396 return true;
397 }
398
399 status = smbd_reinit_after_fork(msg, ev, true, "smbd-notifyd");
400 if (!NT_STATUS_IS_OK(status)) {
401 DEBUG(1, ("%s: reinit_after_fork failed: %s\n",
402 __func__, nt_errstr(status)));
403 exit(1);
404 }
405
406 req = notifyd_req(msg, ev);
407 if (req == NULL) {
408 exit(1);
409 }
410 tevent_req_set_callback(req, notifyd_stopped, msg);
411 return tevent_req_poll(req, ev);
412}
413
414static void cleanupd_stopped(struct tevent_req *req);
415
416static bool cleanupd_init(struct messaging_context *msg, bool interactive,
417 struct server_id *ppid)
418{
419 struct tevent_context *ev = messaging_tevent_context(msg);
420 struct server_id parent_id = messaging_server_id(msg);
421 struct tevent_req *req;
422 pid_t pid;
423 NTSTATUS status;
424 ssize_t rwret;
425 int ret;
426 bool ok;
427 char c;
428 int up_pipe[2];
429
430 if (interactive) {
431 req = smbd_cleanupd_send(msg, ev, msg, parent_id.pid);
432 *ppid = messaging_server_id(msg);
433 return (req != NULL);
434 }
435
436 ret = pipe(up_pipe);
437 if (ret == -1) {
438 DBG_WARNING("pipe failed: %s\n", strerror(errno));
439 return false;
440 }
441
442 pid = fork();
443 if (pid == -1) {
444 DBG_WARNING("fork failed: %s\n", strerror(errno));
445 close(up_pipe[0]);
446 close(up_pipe[1]);
447 return false;
448 }
449
450 if (pid != 0) {
451
452 close(up_pipe[1]);
453 rwret = sys_read(up_pipe[0], &c, 1);
454 close(up_pipe[0]);
455
456 if (rwret == -1) {
457 DBG_WARNING("sys_read failed: %s\n", strerror(errno));
458 return false;
459 }
460 if (rwret == 0) {
461 DBG_WARNING("cleanupd could not start\n");
462 return false;
463 }
464 if (c != 0) {
465 DBG_WARNING("cleanupd returned %d\n", (int)c);
466 return false;
467 }
468
469 DBG_DEBUG("Started cleanupd pid=%d\n", (int)pid);
470
471 if (am_parent != NULL) {
472 add_child_pid(am_parent, pid);
473 }
474
475 *ppid = pid_to_procid(pid);
476 return true;
477 }
478
479 close(up_pipe[0]);
480
481 status = smbd_reinit_after_fork(msg, ev, true, "cleanupd");
482 if (!NT_STATUS_IS_OK(status)) {
483 DBG_WARNING("reinit_after_fork failed: %s\n",
484 nt_errstr(status));
485 c = 1;
486 sys_write(up_pipe[1], &c, 1);
487
488 exit(1);
489 }
490
491 req = smbd_cleanupd_send(msg, ev, msg, parent_id.pid);
492 if (req == NULL) {
493 DBG_WARNING("smbd_cleanupd_send failed\n");
494 c = 2;
495 sys_write(up_pipe[1], &c, 1);
496
497 exit(1);
498 }
499
500 tevent_req_set_callback(req, cleanupd_stopped, msg);
501
502 c = 0;
503 rwret = sys_write(up_pipe[1], &c, 1);
504 close(up_pipe[1]);
505
506 if (rwret == -1) {
507 DBG_WARNING("sys_write failed: %s\n", strerror(errno));
508 exit(1);
509 }
510 if (rwret != 1) {
511 DBG_WARNING("sys_write could not write result\n");
512 exit(1);
513 }
514
515 ok = tevent_req_poll(req, ev);
516 if (!ok) {
517 DBG_WARNING("tevent_req_poll returned %s\n", strerror(errno));
518 }
519 exit(0);
520}
521
522static void cleanupd_stopped(struct tevent_req *req)
523{
524 NTSTATUS status;
525
526 status = smbd_cleanupd_recv(req);
527 DBG_WARNING("cleanupd stopped: %s\n", nt_errstr(status));
528}
529
530/*
531 at most every smbd:cleanuptime seconds (default 20), we scan the BRL
532 and locking database for entries to cleanup. As a side effect this
533 also cleans up dead entries in the connections database (due to the
534 traversal in message_send_all()
535
536 Using a timer for this prevents a flood of traversals when a large
537 number of clients disconnect at the same time (perhaps due to a
538 network outage).
539*/
540
541static void cleanup_timeout_fn(struct tevent_context *event_ctx,
542 struct tevent_timer *te,
543 struct timeval now,
544 void *private_data)
545{
546 struct smbd_parent_context *parent =
547 talloc_get_type_abort(private_data,
548 struct smbd_parent_context);
549
550 parent->cleanup_te = NULL;
551
552 messaging_send_buf(parent->msg_ctx, parent->cleanupd,
553 MSG_SMB_UNLOCK, NULL, 0);
554}
555
556static void remove_child_pid(struct smbd_parent_context *parent,
557 pid_t pid,
558 bool unclean_shutdown)
559{
560 struct smbd_child_pid *child;
561 struct iovec iov[2];
562 NTSTATUS status;
563
564 for (child = parent->children; child != NULL; child = child->next) {
565 if (child->pid == pid) {
566 struct smbd_child_pid *tmp = child;
567 DLIST_REMOVE(parent->children, child);
568 TALLOC_FREE(tmp);
569 parent->num_children -= 1;
570 break;
571 }
572 }
573
574 if (child == NULL) {
575 /* not all forked child processes are added to the children list */
576 DEBUG(2, ("Could not find child %d -- ignoring\n", (int)pid));
577 return;
578 }
579
580 if (child->pid == procid_to_pid(&parent->cleanupd)) {
581 bool ok;
582
583 DBG_WARNING("Restarting cleanupd\n");
584 ok = cleanupd_init(parent->msg_ctx, false, &parent->cleanupd);
585 if (!ok) {
586 DBG_ERR("Failed to restart cleanupd\n");
587 }
588 return;
589 }
590
591 iov[0] = (struct iovec) { .iov_base = (uint8_t *)&pid,
592 .iov_len = sizeof(pid) };
593 iov[1] = (struct iovec) { .iov_base = (uint8_t *)&unclean_shutdown,
594 .iov_len = sizeof(bool) };
595
596 status = messaging_send_iov(parent->msg_ctx, parent->cleanupd,
597 MSG_SMB_NOTIFY_CLEANUP,
598 iov, ARRAY_SIZE(iov), NULL, 0);
599 DEBUG(10, ("messaging_send_iov returned %s\n", nt_errstr(status)));
600
601 if (unclean_shutdown) {
602 /* a child terminated uncleanly so tickle all
603 processes to see if they can grab any of the
604 pending locks
605 */
606 DEBUG(3,(__location__ " Unclean shutdown of pid %u\n",
607 (unsigned int)pid));
608 if (parent->cleanup_te == NULL) {
609 /* call the cleanup timer, but not too often */
610 int cleanup_time = lp_parm_int(-1, "smbd", "cleanuptime", 20);
611 parent->cleanup_te = tevent_add_timer(parent->ev_ctx,
612 parent,
613 timeval_current_ofs(cleanup_time, 0),
614 cleanup_timeout_fn,
615 parent);
616 DEBUG(1,("Scheduled cleanup of brl and lock database after unclean shutdown\n"));
617 }
618 }
619}
620
621/****************************************************************************
622 Have we reached the process limit ?
623****************************************************************************/
624
625static bool allowable_number_of_smbd_processes(struct smbd_parent_context *parent)
626{
627 int max_processes = lp_max_smbd_processes();
628
629 if (!max_processes)
630 return True;
631
632 return parent->num_children < max_processes;
633}
634
635static void smbd_sig_chld_handler(struct tevent_context *ev,
636 struct tevent_signal *se,
637 int signum,
638 int count,
639 void *siginfo,
640 void *private_data)
641{
642 pid_t pid;
643 int status;
644 struct smbd_parent_context *parent =
645 talloc_get_type_abort(private_data,
646 struct smbd_parent_context);
647
648 while ((pid = sys_waitpid(-1, &status, WNOHANG)) > 0) {
649 bool unclean_shutdown = False;
650
651 /* If the child terminated normally, assume
652 it was an unclean shutdown unless the
653 status is 0
654 */
655 if (WIFEXITED(status)) {
656 unclean_shutdown = WEXITSTATUS(status);
657 }
658 /* If the child terminated due to a signal
659 we always assume it was unclean.
660 */
661 if (WIFSIGNALED(status)) {
662 unclean_shutdown = True;
663 }
664 remove_child_pid(parent, pid, unclean_shutdown);
665 }
666}
667
668static void smbd_setup_sig_chld_handler(struct smbd_parent_context *parent)
669{
670 struct tevent_signal *se;
671
672 se = tevent_add_signal(parent->ev_ctx,
673 parent, /* mem_ctx */
674 SIGCHLD, 0,
675 smbd_sig_chld_handler,
676 parent);
677 if (!se) {
678 exit_server("failed to setup SIGCHLD handler");
679 }
680}
681
682static void smbd_open_socket_close_fn(struct tevent_context *ev,
683 struct tevent_fd *fde,
684 int fd,
685 void *private_data)
686{
687 /* this might be the socket_wrapper swrap_close() */
688 close(fd);
689}
690
691static void smbd_accept_connection(struct tevent_context *ev,
692 struct tevent_fd *fde,
693 uint16_t flags,
694 void *private_data)
695{
696 struct smbd_open_socket *s = talloc_get_type_abort(private_data,
697 struct smbd_open_socket);
698 struct messaging_context *msg_ctx = s->parent->msg_ctx;
699 struct sockaddr_storage addr;
700 socklen_t in_addrlen = sizeof(addr);
701 int fd;
702 pid_t pid = 0;
703
704 fd = accept(s->fd, (struct sockaddr *)(void *)&addr,&in_addrlen);
705 if (fd == -1 && errno == EINTR)
706 return;
707
708 if (fd == -1) {
709 DEBUG(0,("accept: %s\n",
710 strerror(errno)));
711 return;
712 }
713
714 if (s->parent->interactive) {
715 reinit_after_fork(msg_ctx, ev, true, NULL);
716 smbd_process(ev, msg_ctx, fd, true);
717 exit_server_cleanly("end of interactive mode");
718 return;
719 }
720
721 if (!allowable_number_of_smbd_processes(s->parent)) {
722 close(fd);
723 return;
724 }
725
726 pid = fork();
727 if (pid == 0) {
728 NTSTATUS status = NT_STATUS_OK;
729
730 /*
731 * Can't use TALLOC_FREE here. Nulling out the argument to it
732 * would overwrite memory we've just freed.
733 */
734 talloc_free(s->parent);
735 s = NULL;
736
737 /* Stop zombies, the parent explicitly handles
738 * them, counting worker smbds. */
739 CatchChild();
740
741 status = smbd_reinit_after_fork(msg_ctx, ev, true, NULL);
742 if (!NT_STATUS_IS_OK(status)) {
743 if (NT_STATUS_EQUAL(status,
744 NT_STATUS_TOO_MANY_OPENED_FILES)) {
745 DEBUG(0,("child process cannot initialize "
746 "because too many files are open\n"));
747 goto exit;
748 }
749 if (lp_clustering() &&
750 NT_STATUS_EQUAL(status,
751 NT_STATUS_INTERNAL_DB_ERROR)) {
752 DEBUG(1,("child process cannot initialize "
753 "because connection to CTDB "
754 "has failed\n"));
755 goto exit;
756 }
757
758 DEBUG(0,("reinit_after_fork() failed\n"));
759 smb_panic("reinit_after_fork() failed");
760 }
761
762 smbd_process(ev, msg_ctx, fd, false);
763 exit:
764 exit_server_cleanly("end of child");
765 return;
766 }
767
768 if (pid < 0) {
769 DEBUG(0,("smbd_accept_connection: fork() failed: %s\n",
770 strerror(errno)));
771 }
772
773 /* The parent doesn't need this socket */
774 close(fd);
775
776 /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
777 Clear the closed fd info out of server_fd --
778 and more importantly, out of client_fd in
779 util_sock.c, to avoid a possible
780 getpeername failure if we reopen the logs
781 and use %I in the filename.
782 */
783
784 if (pid != 0) {
785 add_child_pid(s->parent, pid);
786 }
787
788 /* Force parent to check log size after
789 * spawning child. Fix from
790 * klausr@ITAP.Physik.Uni-Stuttgart.De. The
791 * parent smbd will log to logserver.smb. It
792 * writes only two messages for each child
793 * started/finished. But each child writes,
794 * say, 50 messages also in logserver.smb,
795 * begining with the debug_count of the
796 * parent, before the child opens its own log
797 * file logserver.client. In a worst case
798 * scenario the size of logserver.smb would be
799 * checked after about 50*50=2500 messages
800 * (ca. 100kb).
801 * */
802 force_check_log_size();
803}
804
805static bool smbd_open_one_socket(struct smbd_parent_context *parent,
806 struct tevent_context *ev_ctx,
807 const struct sockaddr_storage *ifss,
808 uint16_t port)
809{
810 struct smbd_open_socket *s;
811
812 s = talloc(parent, struct smbd_open_socket);
813 if (!s) {
814 return false;
815 }
816
817 s->parent = parent;
818 s->fd = open_socket_in(SOCK_STREAM,
819 port,
820 parent->sockets == NULL ? 0 : 2,
821 ifss,
822 true);
823 if (s->fd == -1) {
824 DEBUG(0,("smbd_open_one_socket: open_socket_in: "
825 "%s\n", strerror(errno)));
826 TALLOC_FREE(s);
827 /*
828 * We ignore an error here, as we've done before
829 */
830 return true;
831 }
832
833 /* ready to listen */
834 set_socket_options(s->fd, "SO_KEEPALIVE");
835 set_socket_options(s->fd, lp_socket_options());
836
837 /* Set server socket to
838 * non-blocking for the accept. */
839 set_blocking(s->fd, False);
840
841 if (listen(s->fd, SMBD_LISTEN_BACKLOG) == -1) {
842 DEBUG(0,("smbd_open_one_socket: listen: "
843 "%s\n", strerror(errno)));
844 close(s->fd);
845 TALLOC_FREE(s);
846 return false;
847 }
848
849 s->fde = tevent_add_fd(ev_ctx,
850 s,
851 s->fd, TEVENT_FD_READ,
852 smbd_accept_connection,
853 s);
854 if (!s->fde) {
855 DEBUG(0,("smbd_open_one_socket: "
856 "tevent_add_fd: %s\n",
857 strerror(errno)));
858 close(s->fd);
859 TALLOC_FREE(s);
860 return false;
861 }
862 tevent_fd_set_close_fn(s->fde, smbd_open_socket_close_fn);
863
864 DLIST_ADD_END(parent->sockets, s);
865
866 return true;
867}
868
869/****************************************************************************
870 Open the socket communication.
871****************************************************************************/
872
873static bool open_sockets_smbd(struct smbd_parent_context *parent,
874 struct tevent_context *ev_ctx,
875 struct messaging_context *msg_ctx,
876 const char *smb_ports)
877{
878 int num_interfaces = iface_count();
879 int i,j;
880 const char **ports;
881 unsigned dns_port = 0;
882
883#ifdef HAVE_ATEXIT
884 atexit(killkids);
885#endif
886
887 /* Stop zombies */
888 smbd_setup_sig_chld_handler(parent);
889
890 ports = lp_smb_ports();
891
892 /* use a reasonable default set of ports - listing on 445 and 139 */
893 if (smb_ports) {
894 char **l;
895 l = str_list_make_v3(talloc_tos(), smb_ports, NULL);
896 ports = discard_const_p(const char *, l);
897 }
898
899 for (j = 0; ports && ports[j]; j++) {
900 unsigned port = atoi(ports[j]);
901
902 if (port == 0 || port > 0xffff) {
903 exit_server_cleanly("Invalid port in the config or on "
904 "the commandline specified!");
905 }
906 }
907
908 if (lp_interfaces() && lp_bind_interfaces_only()) {
909 /* We have been given an interfaces line, and been
910 told to only bind to those interfaces. Create a
911 socket per interface and bind to only these.
912 */
913
914 /* Now open a listen socket for each of the
915 interfaces. */
916 for(i = 0; i < num_interfaces; i++) {
917 const struct sockaddr_storage *ifss =
918 iface_n_sockaddr_storage(i);
919 if (ifss == NULL) {
920 DEBUG(0,("open_sockets_smbd: "
921 "interface %d has NULL IP address !\n",
922 i));
923 continue;
924 }
925
926 for (j = 0; ports && ports[j]; j++) {
927 unsigned port = atoi(ports[j]);
928
929 /* Keep the first port for mDNS service
930 * registration.
931 */
932 if (dns_port == 0) {
933 dns_port = port;
934 }
935
936 if (!smbd_open_one_socket(parent,
937 ev_ctx,
938 ifss,
939 port)) {
940 return false;
941 }
942 }
943 }
944 } else {
945 /* Just bind to 0.0.0.0 - accept connections
946 from anywhere. */
947
948 const char *sock_addr;
949 char *sock_tok;
950 const char *sock_ptr;
951
952#if HAVE_IPV6
953 sock_addr = "::,0.0.0.0";
954#else
955 sock_addr = "0.0.0.0";
956#endif
957
958 for (sock_ptr=sock_addr;
959 next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,"); ) {
960 for (j = 0; ports && ports[j]; j++) {
961 struct sockaddr_storage ss;
962 unsigned port = atoi(ports[j]);
963
964 /* Keep the first port for mDNS service
965 * registration.
966 */
967 if (dns_port == 0) {
968 dns_port = port;
969 }
970
971 /* open an incoming socket */
972 if (!interpret_string_addr(&ss, sock_tok,
973 AI_NUMERICHOST|AI_PASSIVE)) {
974 continue;
975 }
976
977 /*
978 * If we fail to open any sockets
979 * in this loop the parent-sockets == NULL
980 * case below will prevent us from starting.
981 */
982
983 (void)smbd_open_one_socket(parent,
984 ev_ctx,
985 &ss,
986 port);
987 }
988 }
989 }
990
991 if (parent->sockets == NULL) {
992 DEBUG(0,("open_sockets_smbd: No "
993 "sockets available to bind to.\n"));
994 return false;
995 }
996
997 /* Setup the main smbd so that we can get messages. Note that
998 do this after starting listening. This is needed as when in
999 clustered mode, ctdb won't allow us to start doing database
1000 operations until it has gone thru a full startup, which
1001 includes checking to see that smbd is listening. */
1002
1003 if (!serverid_register(messaging_server_id(msg_ctx),
1004 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
1005 |FLAG_MSG_PRINT_GENERAL
1006 |FLAG_MSG_DBWRAP)) {
1007 DEBUG(0, ("open_sockets_smbd: Failed to register "
1008 "myself in serverid.tdb\n"));
1009 return false;
1010 }
1011
1012 /* Listen to messages */
1013
1014 messaging_register(msg_ctx, NULL, MSG_SHUTDOWN, msg_exit_server);
1015 messaging_register(msg_ctx, ev_ctx, MSG_SMB_CONF_UPDATED,
1016 smbd_parent_conf_updated);
1017 messaging_register(msg_ctx, NULL, MSG_SMB_STAT_CACHE_DELETE,
1018 smb_stat_cache_delete);
1019 messaging_register(msg_ctx, NULL, MSG_DEBUG, smbd_msg_debug);
1020 messaging_register(msg_ctx, NULL, MSG_SMB_FORCE_TDIS,
1021 smb_parent_send_to_children);
1022 messaging_register(msg_ctx, NULL, MSG_SMB_KILL_CLIENT_IP,
1023 smb_parent_send_to_children);
1024 messaging_register(msg_ctx, NULL, MSG_SMB_TELL_NUM_CHILDREN,
1025 smb_tell_num_children);
1026
1027 messaging_register(msg_ctx, NULL,
1028 ID_CACHE_DELETE, smbd_parent_id_cache_delete);
1029 messaging_register(msg_ctx, NULL,
1030 ID_CACHE_KILL, smbd_parent_id_cache_kill);
1031
1032#ifdef CLUSTER_SUPPORT
1033 if (lp_clustering()) {
1034 struct ctdbd_connection *conn = messaging_ctdbd_connection();
1035
1036 register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE,
1037 smbd_parent_ctdb_reconfigured, msg_ctx);
1038 register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY,
1039 smbd_parent_ctdb_reconfigured, msg_ctx);
1040 }
1041#endif
1042
1043#ifdef DEVELOPER
1044 messaging_register(msg_ctx, NULL, MSG_SMB_INJECT_FAULT,
1045 msg_inject_fault);
1046#endif
1047
1048 if (lp_multicast_dns_register() && (dns_port != 0)) {
1049#ifdef WITH_DNSSD_SUPPORT
1050 smbd_setup_mdns_registration(ev_ctx,
1051 parent, dns_port);
1052#endif
1053#ifdef WITH_AVAHI_SUPPORT
1054 void *avahi_conn;
1055
1056 avahi_conn = avahi_start_register(ev_ctx,
1057 ev_ctx,
1058 dns_port);
1059 if (avahi_conn == NULL) {
1060 DEBUG(10, ("avahi_start_register failed\n"));
1061 }
1062#endif
1063 }
1064
1065 return true;
1066}
1067
1068
1069/*
1070 handle stdin becoming readable when we are in --foreground mode
1071 */
1072static void smbd_stdin_handler(struct tevent_context *ev,
1073 struct tevent_fd *fde,
1074 uint16_t flags,
1075 void *private_data)
1076{
1077 char c;
1078 if (read(0, &c, 1) != 1) {
1079 /* we have reached EOF on stdin, which means the
1080 parent has exited. Shutdown the server */
1081 exit_server_cleanly("EOF on stdin");
1082 }
1083}
1084
1085struct smbd_parent_tevent_trace_state {
1086 TALLOC_CTX *frame;
1087};
1088
1089static void smbd_parent_tevent_trace_callback(enum tevent_trace_point point,
1090 void *private_data)
1091{
1092 struct smbd_parent_tevent_trace_state *state =
1093 (struct smbd_parent_tevent_trace_state *)private_data;
1094
1095 switch (point) {
1096 case TEVENT_TRACE_BEFORE_WAIT:
1097 break;
1098 case TEVENT_TRACE_AFTER_WAIT:
1099 break;
1100 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
1101 TALLOC_FREE(state->frame);
1102 state->frame = talloc_stackframe();
1103 break;
1104 case TEVENT_TRACE_AFTER_LOOP_ONCE:
1105 TALLOC_FREE(state->frame);
1106 break;
1107 }
1108
1109 errno = 0;
1110}
1111
1112static void smbd_parent_loop(struct tevent_context *ev_ctx,
1113 struct smbd_parent_context *parent)
1114{
1115 struct smbd_parent_tevent_trace_state trace_state = {
1116 .frame = NULL,
1117 };
1118 int ret = 0;
1119
1120 tevent_set_trace_callback(ev_ctx, smbd_parent_tevent_trace_callback,
1121 &trace_state);
1122
1123 /* now accept incoming connections - forking a new process
1124 for each incoming connection */
1125 DEBUG(2,("waiting for connections\n"));
1126
1127 ret = tevent_loop_wait(ev_ctx);
1128 if (ret != 0) {
1129 DEBUG(0, ("tevent_loop_wait failed: %d, %s, exiting\n",
1130 ret, strerror(errno)));
1131 }
1132
1133 TALLOC_FREE(trace_state.frame);
1134
1135/* NOTREACHED return True; */
1136}
1137
1138
1139/****************************************************************************
1140 Initialise connect, service and file structs.
1141****************************************************************************/
1142
1143static bool init_structs(void )
1144{
1145 /*
1146 * Set the machine NETBIOS name if not already
1147 * set from the config file.
1148 */
1149
1150 if (!init_names())
1151 return False;
1152
1153 if (!secrets_init())
1154 return False;
1155
1156 return True;
1157}
1158
1159static void smbd_parent_sig_term_handler(struct tevent_context *ev,
1160 struct tevent_signal *se,
1161 int signum,
1162 int count,
1163 void *siginfo,
1164 void *private_data)
1165{
1166 exit_server_cleanly("termination signal");
1167}
1168
1169static void smbd_parent_sig_hup_handler(struct tevent_context *ev,
1170 struct tevent_signal *se,
1171 int signum,
1172 int count,
1173 void *siginfo,
1174 void *private_data)
1175{
1176 struct smbd_parent_context *parent =
1177 talloc_get_type_abort(private_data,
1178 struct smbd_parent_context);
1179
1180 change_to_root_user();
1181 DEBUG(1,("parent: Reloading services after SIGHUP\n"));
1182 reload_services(NULL, NULL, false);
1183
1184 printing_subsystem_update(parent->ev_ctx, parent->msg_ctx, true);
1185}
1186
1187/****************************************************************************
1188 main program.
1189****************************************************************************/
1190
1191/* Declare prototype for build_options() to avoid having to run it through
1192 mkproto.h. Mixing $(builddir) and $(srcdir) source files in the current
1193 prototype generation system is too complicated. */
1194
1195extern void build_options(bool screen);
1196
1197 int main(int argc,const char *argv[])
1198{
1199 /* shall I run as a daemon */
1200 bool is_daemon = false;
1201 bool interactive = false;
1202 bool Fork = true;
1203 bool no_process_group = false;
1204 bool log_stdout = false;
1205 char *ports = NULL;
1206 char *profile_level = NULL;
1207 int opt;
1208 poptContext pc;
1209 bool print_build_options = False;
1210 enum {
1211 OPT_DAEMON = 1000,
1212 OPT_INTERACTIVE,
1213 OPT_FORK,
1214 OPT_NO_PROCESS_GROUP,
1215 OPT_LOG_STDOUT
1216 };
1217 struct poptOption long_options[] = {
1218 POPT_AUTOHELP
1219 {"daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
1220 {"interactive", 'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE, "Run interactive (not a daemon)"},
1221 {"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Run daemon in foreground (for daemontools, etc.)" },
1222 {"no-process-group", '\0', POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
1223 {"log-stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
1224 {"build-options", 'b', POPT_ARG_NONE, NULL, 'b', "Print build options" },
1225 {"port", 'p', POPT_ARG_STRING, &ports, 0, "Listen on the specified ports"},
1226 {"profiling-level", 'P', POPT_ARG_STRING, &profile_level, 0, "Set profiling level","PROFILE_LEVEL"},
1227 POPT_COMMON_SAMBA
1228 POPT_TABLEEND
1229 };
1230 struct smbd_parent_context *parent = NULL;
1231 TALLOC_CTX *frame;
1232 NTSTATUS status;
1233 struct tevent_context *ev_ctx;
1234 struct messaging_context *msg_ctx;
1235 struct server_id server_id;
1236 struct tevent_signal *se;
1237 int profiling_level;
1238 char *np_dir = NULL;
1239 static const struct smbd_shim smbd_shim_fns =
1240 {
1241 .cancel_pending_lock_requests_by_fid = smbd_cancel_pending_lock_requests_by_fid,
1242 .send_stat_cache_delete_message = smbd_send_stat_cache_delete_message,
1243 .change_to_root_user = smbd_change_to_root_user,
1244 .become_authenticated_pipe_user = smbd_become_authenticated_pipe_user,
1245 .unbecome_authenticated_pipe_user = smbd_unbecome_authenticated_pipe_user,
1246
1247 .contend_level2_oplocks_begin = smbd_contend_level2_oplocks_begin,
1248 .contend_level2_oplocks_end = smbd_contend_level2_oplocks_end,
1249
1250 .become_root = smbd_become_root,
1251 .unbecome_root = smbd_unbecome_root,
1252
1253 .exit_server = smbd_exit_server,
1254 .exit_server_cleanly = smbd_exit_server_cleanly,
1255 };
1256
1257 /*
1258 * Do this before any other talloc operation
1259 */
1260 talloc_enable_null_tracking();
1261 frame = talloc_stackframe();
1262
1263 setup_logging(argv[0], DEBUG_DEFAULT_STDOUT);
1264
1265 smb_init_locale();
1266
1267 set_smbd_shim(&smbd_shim_fns);
1268
1269 smbd_init_globals();
1270
1271 TimeInit();
1272
1273#ifdef HAVE_SET_AUTH_PARAMETERS
1274 set_auth_parameters(argc,argv);
1275#endif
1276
1277 pc = poptGetContext("smbd", argc, argv, long_options, 0);
1278 while((opt = poptGetNextOpt(pc)) != -1) {
1279 switch (opt) {
1280 case OPT_DAEMON:
1281 is_daemon = true;
1282 break;
1283 case OPT_INTERACTIVE:
1284 interactive = true;
1285 break;
1286 case OPT_FORK:
1287 Fork = false;
1288 break;
1289 case OPT_NO_PROCESS_GROUP:
1290 no_process_group = true;
1291 break;
1292 case OPT_LOG_STDOUT:
1293 log_stdout = true;
1294 break;
1295 case 'b':
1296 print_build_options = True;
1297 break;
1298 default:
1299 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1300 poptBadOption(pc, 0), poptStrerror(opt));
1301 poptPrintUsage(pc, stderr, 0);
1302 exit(1);
1303 }
1304 }
1305 poptFreeContext(pc);
1306
1307 if (interactive) {
1308 Fork = False;
1309 log_stdout = True;
1310 }
1311
1312 if (log_stdout) {
1313 setup_logging(argv[0], DEBUG_STDOUT);
1314 } else {
1315 setup_logging(argv[0], DEBUG_FILE);
1316 }
1317
1318 if (print_build_options) {
1319 build_options(True); /* Display output to screen as well as debug */
1320 exit(0);
1321 }
1322
1323#ifdef HAVE_SETLUID
1324 /* needed for SecureWare on SCO */
1325 setluid(0);
1326#endif
1327
1328 set_remote_machine_name("smbd", False);
1329
1330 if (interactive && (DEBUGLEVEL >= 9)) {
1331 talloc_enable_leak_report();
1332 }
1333
1334 if (log_stdout && Fork) {
1335 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
1336 exit(1);
1337 }
1338
1339 /*
1340 * We want to die early if we can't open /dev/urandom
1341 */
1342 generate_random_buffer(NULL, 0);
1343
1344 /* get initial effective uid and gid */
1345 sec_init();
1346
1347 /* make absolutely sure we run as root - to handle cases where people
1348 are crazy enough to have it setuid */
1349 gain_root_privilege();
1350 gain_root_group_privilege();
1351
1352 fault_setup();
1353 dump_core_setup("smbd", lp_logfile(talloc_tos()));
1354
1355 /* we are never interested in SIGPIPE */
1356 BlockSignals(True,SIGPIPE);
1357
1358#if defined(SIGFPE)
1359 /* we are never interested in SIGFPE */
1360 BlockSignals(True,SIGFPE);
1361#endif
1362
1363#if defined(SIGUSR2)
1364 /* We are no longer interested in USR2 */
1365 BlockSignals(True,SIGUSR2);
1366#endif
1367
1368 /* POSIX demands that signals are inherited. If the invoking process has
1369 * these signals masked, we will have problems, as we won't recieve them. */
1370 BlockSignals(False, SIGHUP);
1371 BlockSignals(False, SIGUSR1);
1372 BlockSignals(False, SIGTERM);
1373
1374 /* Ensure we leave no zombies until we
1375 * correctly set up child handling below. */
1376
1377 CatchChild();
1378
1379 /* we want total control over the permissions on created files,
1380 so set our umask to 0 */
1381 umask(0);
1382
1383 reopen_logs();
1384
1385 DEBUG(0,("smbd version %s started.\n", samba_version_string()));
1386 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1387
1388 DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
1389 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
1390
1391 /* Output the build options to the debug log */
1392 build_options(False);
1393
1394 if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4) {
1395 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
1396 exit(1);
1397 }
1398
1399 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1400 DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
1401 exit(1);
1402 }
1403
1404 if (!cluster_probe_ok()) {
1405 exit(1);
1406 }
1407
1408 /* Init the security context and global current_user */
1409 init_sec_ctx();
1410
1411 /*
1412 * Initialize the event context. The event context needs to be
1413 * initialized before the messaging context, cause the messaging
1414 * context holds an event context.
1415 */
1416 ev_ctx = server_event_context();
1417 if (ev_ctx == NULL) {
1418 exit(1);
1419 }
1420
1421 /*
1422 * Init the messaging context
1423 * FIXME: This should only call messaging_init()
1424 */
1425 msg_ctx = server_messaging_context();
1426 if (msg_ctx == NULL) {
1427 exit(1);
1428 }
1429
1430 /*
1431 * Reloading of the printers will not work here as we don't have a
1432 * server info and rpc services set up. It will be called later.
1433 */
1434 if (!reload_services(NULL, NULL, false)) {
1435 exit(1);
1436 }
1437
1438 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
1439 && !lp_parm_bool(-1, "server role check", "inhibit", false)) {
1440 DEBUG(0, ("server role = 'active directory domain controller' not compatible with running smbd standalone. \n"));
1441 DEBUGADD(0, ("You should start 'samba' instead, and it will control starting smbd if required\n"));
1442 exit(1);
1443 }
1444
1445 /* ...NOTE... Log files are working from this point! */
1446
1447 DEBUG(3,("loaded services\n"));
1448
1449 init_structs();
1450
1451 if (!profile_setup(msg_ctx, False)) {
1452 DEBUG(0,("ERROR: failed to setup profiling\n"));
1453 return -1;
1454 }
1455
1456 if (profile_level != NULL) {
1457 profiling_level = atoi(profile_level);
1458 } else {
1459 profiling_level = lp_smbd_profiling_level();
1460 }
1461 set_profile_level(profiling_level, messaging_server_id(msg_ctx));
1462
1463 if (!is_daemon && !is_a_socket(0)) {
1464 if (!interactive) {
1465 DEBUG(3, ("Standard input is not a socket, "
1466 "assuming -D option\n"));
1467 }
1468
1469 /*
1470 * Setting is_daemon here prevents us from eventually calling
1471 * the open_sockets_inetd()
1472 */
1473
1474 is_daemon = True;
1475 }
1476
1477 if (is_daemon && !interactive) {
1478 DEBUG(3, ("Becoming a daemon.\n"));
1479 become_daemon(Fork, no_process_group, log_stdout);
1480 }
1481
1482#if HAVE_SETPGID
1483 /*
1484 * If we're interactive we want to set our own process group for
1485 * signal management.
1486 */
1487 if (interactive && !no_process_group)
1488 setpgid( (pid_t)0, (pid_t)0);
1489#endif
1490
1491 if (!directory_exist(lp_lock_directory()))
1492 mkdir(lp_lock_directory(), 0755);
1493
1494 if (!directory_exist(lp_pid_directory()))
1495 mkdir(lp_pid_directory(), 0755);
1496
1497 if (is_daemon)
1498 pidfile_create(lp_pid_directory(), "smbd");
1499
1500 status = reinit_after_fork(msg_ctx, ev_ctx, false, NULL);
1501 if (!NT_STATUS_IS_OK(status)) {
1502 exit_daemon("reinit_after_fork() failed", map_errno_from_nt_status(status));
1503 }
1504
1505 if (!interactive) {
1506 /*
1507 * Do not initialize the parent-child-pipe before becoming a
1508 * daemon: this is used to detect a died parent in the child
1509 * process.
1510 */
1511 status = init_before_fork();
1512 if (!NT_STATUS_IS_OK(status)) {
1513 exit_daemon(nt_errstr(status), map_errno_from_nt_status(status));
1514 }
1515 }
1516
1517 parent = talloc_zero(ev_ctx, struct smbd_parent_context);
1518 if (!parent) {
1519 exit_server("talloc(struct smbd_parent_context) failed");
1520 }
1521 parent->interactive = interactive;
1522 parent->ev_ctx = ev_ctx;
1523 parent->msg_ctx = msg_ctx;
1524 am_parent = parent;
1525
1526 se = tevent_add_signal(parent->ev_ctx,
1527 parent,
1528 SIGTERM, 0,
1529 smbd_parent_sig_term_handler,
1530 parent);
1531 if (!se) {
1532 exit_server("failed to setup SIGTERM handler");
1533 }
1534 se = tevent_add_signal(parent->ev_ctx,
1535 parent,
1536 SIGHUP, 0,
1537 smbd_parent_sig_hup_handler,
1538 parent);
1539 if (!se) {
1540 exit_server("failed to setup SIGHUP handler");
1541 }
1542
1543 /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
1544
1545 if (smbd_memcache() == NULL) {
1546 exit_daemon("no memcache available", EACCES);
1547 }
1548
1549 memcache_set_global(smbd_memcache());
1550
1551 /* Initialise the password backed before the global_sam_sid
1552 to ensure that we fetch from ldap before we make a domain sid up */
1553
1554 if(!initialize_password_db(false, ev_ctx))
1555 exit(1);
1556
1557 if (!secrets_init()) {
1558 exit_daemon("smbd can not open secrets.tdb", EACCES);
1559 }
1560
1561 if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) {
1562 struct loadparm_context *lp_ctx = loadparm_init_s3(NULL, loadparm_s3_helpers());
1563 if (!open_schannel_session_store(NULL, lp_ctx)) {
1564 exit_daemon("ERROR: Samba cannot open schannel store for secured NETLOGON operations.", EACCES);
1565 }
1566 TALLOC_FREE(lp_ctx);
1567 }
1568
1569 if(!get_global_sam_sid()) {
1570 exit_daemon("Samba cannot create a SAM SID", EACCES);
1571 }
1572
1573 server_id = messaging_server_id(msg_ctx);
1574 status = smbXsrv_version_global_init(&server_id);
1575 if (!NT_STATUS_IS_OK(status)) {
1576 exit_daemon("Samba cannot init server context", EACCES);
1577 }
1578
1579 status = smbXsrv_session_global_init();
1580 if (!NT_STATUS_IS_OK(status)) {
1581 exit_daemon("Samba cannot init session context", EACCES);
1582 }
1583
1584 status = smbXsrv_tcon_global_init();
1585 if (!NT_STATUS_IS_OK(status)) {
1586 exit_daemon("Samba cannot init tcon context", EACCES);
1587 }
1588
1589 if (!locking_init())
1590 exit_daemon("Samba cannot init locking", EACCES);
1591
1592 if (!leases_db_init(false)) {
1593 exit_daemon("Samba cannot init leases", EACCES);
1594 }
1595
1596 if (!smbd_notifyd_init(msg_ctx, interactive)) {
1597 exit_daemon("Samba cannot init notification", EACCES);
1598 }
1599
1600 if (!cleanupd_init(msg_ctx, interactive, &parent->cleanupd)) {
1601 exit_daemon("Samba cannot init the cleanupd", EACCES);
1602 }
1603
1604 if (!messaging_parent_dgm_cleanup_init(msg_ctx)) {
1605 exit(1);
1606 }
1607
1608 if (!smbd_scavenger_init(NULL, msg_ctx, ev_ctx)) {
1609 exit_daemon("Samba cannot init scavenging", EACCES);
1610 }
1611
1612 if (!serverid_parent_init(ev_ctx)) {
1613 exit_daemon("Samba cannot init server id", EACCES);
1614 }
1615
1616 if (!W_ERROR_IS_OK(registry_init_full()))
1617 exit_daemon("Samba cannot init registry", EACCES);
1618
1619 /* Open the share_info.tdb here, so we don't have to open
1620 after the fork on every single connection. This is a small
1621 performance improvment and reduces the total number of system
1622 fds used. */
1623 if (!share_info_db_init()) {
1624 exit_daemon("ERROR: failed to load share info db.", EACCES);
1625 }
1626
1627 status = init_system_session_info();
1628 if (!NT_STATUS_IS_OK(status)) {
1629 DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
1630 nt_errstr(status)));
1631 return -1;
1632 }
1633
1634 if (!init_guest_info()) {
1635 DEBUG(0,("ERROR: failed to setup guest info.\n"));
1636 return -1;
1637 }
1638
1639 if (!file_init_global()) {
1640 DEBUG(0, ("ERROR: file_init_global() failed\n"));
1641 return -1;
1642 }
1643 status = smbXsrv_open_global_init();
1644 if (!NT_STATUS_IS_OK(status)) {
1645 exit_daemon("Samba cannot init global open", map_errno_from_nt_status(status));
1646 }
1647
1648 /* This MUST be done before start_epmd() because otherwise
1649 * start_epmd() forks and races against dcesrv_ep_setup() to
1650 * call directory_create_or_exist() */
1651 if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
1652 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
1653 lp_ncalrpc_dir(), strerror(errno)));
1654 return -1;
1655 }
1656
1657 np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
1658 if (!np_dir) {
1659 DEBUG(0, ("%s: Out of memory\n", __location__));
1660 return -1;
1661 }
1662
1663 if (!directory_create_or_exist_strict(np_dir, geteuid(), 0700)) {
1664 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
1665 np_dir, strerror(errno)));
1666 return -1;
1667 }
1668
1669 if (is_daemon && !interactive) {
1670 if (rpc_epmapper_daemon() == RPC_DAEMON_FORK) {
1671 start_epmd(ev_ctx, msg_ctx);
1672 }
1673 }
1674
1675 if (!dcesrv_ep_setup(ev_ctx, msg_ctx)) {
1676 exit_daemon("Samba cannot setup ep pipe", EACCES);
1677 }
1678
1679 if (is_daemon && !interactive) {
1680 daemon_ready("smbd");
1681 }
1682
1683 /* only start other daemons if we are running as a daemon
1684 * -- bad things will happen if smbd is launched via inetd
1685 * and we fork a copy of ourselves here */
1686 if (is_daemon && !interactive) {
1687
1688 if (rpc_lsasd_daemon() == RPC_DAEMON_FORK) {
1689 start_lsasd(ev_ctx, msg_ctx);
1690 }
1691
1692 if (rpc_fss_daemon() == RPC_DAEMON_FORK) {
1693 start_fssd(ev_ctx, msg_ctx);
1694 }
1695
1696 if (!lp__disable_spoolss() &&
1697 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
1698 bool bgq = lp_parm_bool(-1, "smbd", "backgroundqueue", true);
1699
1700 if (!printing_subsystem_init(ev_ctx, msg_ctx, true, bgq)) {
1701 exit_daemon("Samba failed to init printing subsystem", EACCES);
1702 }
1703 }
1704
1705#ifdef WITH_SPOTLIGHT
1706 if ((rpc_mdssvc_mode() == RPC_SERVICE_MODE_EXTERNAL) &&
1707 (rpc_mdssd_daemon() == RPC_DAEMON_FORK)) {
1708 start_mdssd(ev_ctx, msg_ctx);
1709 }
1710#endif
1711 } else if (!lp__disable_spoolss() &&
1712 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
1713 if (!printing_subsystem_init(ev_ctx, msg_ctx, false, false)) {
1714 exit(1);
1715 }
1716 }
1717
1718 if (!is_daemon) {
1719 int sock;
1720
1721 /* inetd mode */
1722 TALLOC_FREE(frame);
1723
1724 /* Started from inetd. fd 0 is the socket. */
1725 /* We will abort gracefully when the client or remote system
1726 goes away */
1727 sock = dup(0);
1728
1729 /* close stdin, stdout (if not logging to it), but not stderr */
1730 close_low_fds(true, !debug_get_output_is_stdout(), false);
1731
1732#ifdef HAVE_ATEXIT
1733 atexit(killkids);
1734#endif
1735
1736 /* Stop zombies */
1737 smbd_setup_sig_chld_handler(parent);
1738
1739 smbd_process(ev_ctx, msg_ctx, sock, true);
1740
1741 exit_server_cleanly(NULL);
1742 return(0);
1743 }
1744
1745 if (!open_sockets_smbd(parent, ev_ctx, msg_ctx, ports))
1746 exit_server("open_sockets_smbd() failed");
1747
1748 /* do a printer update now that all messaging has been set up,
1749 * before we allow clients to start connecting */
1750 if (!lp__disable_spoolss() &&
1751 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
1752 printing_subsystem_update(ev_ctx, msg_ctx, false);
1753 }
1754
1755 TALLOC_FREE(frame);
1756 /* make sure we always have a valid stackframe */
1757 frame = talloc_stackframe();
1758
1759 if (!Fork) {
1760 /* if we are running in the foreground then look for
1761 EOF on stdin, and exit if it happens. This allows
1762 us to die if the parent process dies
1763 Only do this on a pipe or socket, no other device.
1764 */
1765 struct stat st;
1766 if (fstat(0, &st) != 0) {
1767 return false;
1768 }
1769 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
1770 tevent_add_fd(ev_ctx,
1771 parent,
1772 0,
1773 TEVENT_FD_READ,
1774 smbd_stdin_handler,
1775 NULL);
1776 }
1777 }
1778
1779 smbd_parent_loop(ev_ctx, parent);
1780
1781 exit_server_cleanly(NULL);
1782 TALLOC_FREE(frame);
1783 return(0);
1784}
Note: See TracBrowser for help on using the repository browser.