s3:rpc_server: Remove dead code
[Samba.git] / source4 / smbd / server.c
blob95acb99b86c96faf3b47ccf53c0dbf1a476a4ba3
1 /*
2 Unix SMB/CIFS implementation.
4 Main SMB server routines
6 Copyright (C) Andrew Tridgell 1992-2005
7 Copyright (C) Martin Pool 2002
8 Copyright (C) Jelmer Vernooij 2002
9 Copyright (C) James J Myers 2003 <myersjj@samba.org>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "lib/events/events.h"
27 #include "version.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "system/dir.h"
30 #include "system/filesys.h"
31 #include "auth/gensec/gensec.h"
32 #include "libcli/auth/schannel.h"
33 #include "smbd/process_model.h"
34 #include "param/secrets.h"
35 #include "lib/util/pidfile.h"
36 #include "param/param.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "auth/session.h"
39 #include "lib/messaging/irpc.h"
40 #include "librpc/gen_ndr/ndr_irpc.h"
41 #include "cluster/cluster.h"
42 #include "dynconfig/dynconfig.h"
43 #include "lib/util/samba_modules.h"
44 #include "nsswitch/winbind_client.h"
45 #include "libds/common/roles.h"
46 #include "lib/util/tfork.h"
47 #include "dsdb/samdb/ldb_modules/util.h"
48 #include "lib/util/server_id.h"
50 #ifdef HAVE_PTHREAD
51 #include <pthread.h>
52 #endif
54 struct server_state {
55 struct tevent_context *event_ctx;
56 const char *binary_name;
60 recursively delete a directory tree
62 static void recursive_delete(const char *path)
64 DIR *dir;
65 struct dirent *de;
67 dir = opendir(path);
68 if (!dir) {
69 return;
72 for (de=readdir(dir);de;de=readdir(dir)) {
73 char *fname;
74 struct stat st;
76 if (ISDOT(de->d_name) || ISDOTDOT(de->d_name)) {
77 continue;
80 fname = talloc_asprintf(path, "%s/%s", path, de->d_name);
81 if (stat(fname, &st) != 0) {
82 continue;
84 if (S_ISDIR(st.st_mode)) {
85 recursive_delete(fname);
86 talloc_free(fname);
87 continue;
89 if (unlink(fname) != 0) {
90 DBG_ERR("Unabled to delete '%s' - %s\n",
91 fname, strerror(errno));
92 smb_panic("unable to cleanup tmp files");
94 talloc_free(fname);
96 closedir(dir);
100 cleanup temporary files. This is the new alternative to
101 TDB_CLEAR_IF_FIRST. Unfortunately TDB_CLEAR_IF_FIRST is not
102 efficient on unix systems due to the lack of scaling of the byte
103 range locking system. So instead of putting the burden on tdb to
104 cleanup tmp files, this function deletes them.
106 static void cleanup_tmp_files(struct loadparm_context *lp_ctx)
108 char *path;
109 TALLOC_CTX *mem_ctx = talloc_new(NULL);
110 if (mem_ctx == NULL) {
111 exit_daemon("Failed to create memory context",
112 ENOMEM);
115 path = smbd_tmp_path(mem_ctx, lp_ctx, NULL);
116 if (path == NULL) {
117 exit_daemon("Failed to cleanup temporary files",
118 EINVAL);
121 recursive_delete(path);
122 talloc_free(mem_ctx);
125 static void sig_hup(int sig)
127 debug_schedule_reopen_logs();
130 static void sig_term(int sig)
132 #ifdef HAVE_GETPGRP
133 if (getpgrp() == getpid()) {
135 * We're the process group leader, send
136 * SIGTERM to our process group.
138 kill(-getpgrp(), SIGTERM);
140 #endif
141 _exit(127);
144 static void sigterm_signal_handler(struct tevent_context *ev,
145 struct tevent_signal *se,
146 int signum, int count, void *siginfo,
147 void *private_data)
149 struct server_state *state = talloc_get_type_abort(
150 private_data, struct server_state);
152 DBG_DEBUG("Process %s got SIGTERM\n", state->binary_name);
153 TALLOC_FREE(state);
154 sig_term(SIGTERM);
158 setup signal masks
160 static void setup_signals(void)
162 /* we are never interested in SIGPIPE */
163 BlockSignals(true,SIGPIPE);
165 #if defined(SIGFPE)
166 /* we are never interested in SIGFPE */
167 BlockSignals(true,SIGFPE);
168 #endif
170 /* We are no longer interested in USR1 */
171 BlockSignals(true, SIGUSR1);
173 #if defined(SIGUSR2)
174 /* We are no longer interested in USR2 */
175 BlockSignals(true,SIGUSR2);
176 #endif
178 /* POSIX demands that signals are inherited. If the invoking process has
179 * these signals masked, we will have problems,
180 * as we won't receive them. */
181 BlockSignals(false, SIGHUP);
182 BlockSignals(false, SIGTERM);
184 CatchSignal(SIGHUP, sig_hup);
185 CatchSignal(SIGTERM, sig_term);
189 handle io on stdin
191 static void server_stdin_handler(struct tevent_context *event_ctx,
192 struct tevent_fd *fde,
193 uint16_t flags,
194 void *private_data)
196 struct server_state *state = talloc_get_type_abort(
197 private_data, struct server_state);
198 uint8_t c;
199 if (read(0, &c, 1) == 0) {
200 DBG_ERR("%s: EOF on stdin - PID %d terminating\n",
201 state->binary_name, (int)getpid());
202 #ifdef HAVE_GETPGRP
203 if (getpgrp() == getpid()) {
204 DBG_ERR("Sending SIGTERM from pid %d\n",
205 (int)getpid());
206 kill(-getpgrp(), SIGTERM);
208 #endif
209 TALLOC_FREE(state);
210 exit(0);
215 die if the user selected maximum runtime is exceeded
217 _NORETURN_ static void max_runtime_handler(struct tevent_context *ev,
218 struct tevent_timer *te,
219 struct timeval t, void *private_data)
221 struct server_state *state = talloc_get_type_abort(
222 private_data, struct server_state);
223 DBG_ERR("%s: maximum runtime exceeded - "
224 "terminating PID %d at %llu, current ts: %llu\n",
225 state->binary_name,
226 (int)getpid(),
227 (unsigned long long)t.tv_sec,
228 (unsigned long long)time(NULL));
229 TALLOC_FREE(state);
230 exit(0);
234 * When doing an in-place upgrade of Samba, the database format may have
235 * changed between versions. E.g. between 4.7 and 4.8 the DB changed from
236 * DN-based indexes to GUID-based indexes, so we have to re-index the DB after
237 * upgrading.
238 * This function handles migrating an older samba DB to a new Samba release.
239 * Note that we have to maintain DB compatibility between *all* older versions
240 * of Samba, not just the ones still under maintenance support.
242 static int handle_inplace_db_upgrade(struct ldb_context *ldb_ctx)
244 int ret;
247 * The DSDB stack will handle reindexing the DB (if needed) upon the first
248 * DB write. Open and close a transaction on the DB now to trigger a
249 * reindex if required, rather than waiting for the first write.
250 * We do this here to guarantee that the DB will have been re-indexed by
251 * the time the main samba code runs.
252 * Refer to dsdb_schema_set_indices_and_attributes() for the actual reindexing
253 * code, called from
254 * source4/dsdb/samdb/ldb_modules/schema_load.c:schema_load_start_transaction()
256 ret = ldb_transaction_start(ldb_ctx);
257 if (ret != LDB_SUCCESS) {
258 return ret;
261 ret = ldb_transaction_commit(ldb_ctx);
262 if (ret != LDB_SUCCESS) {
263 return ret;
265 return LDB_SUCCESS;
269 pre-open the key databases. This saves a lot of time in child
270 processes
272 static int prime_ldb_databases(struct tevent_context *event_ctx, bool *am_backup)
274 struct ldb_result *res = NULL;
275 struct ldb_dn *samba_dsdb_dn = NULL;
276 struct ldb_context *ldb_ctx = NULL;
277 struct ldb_context *pdb = NULL;
278 static const char *attrs[] = { "backupDate", NULL };
279 const char *msg = NULL;
280 int ret;
281 TALLOC_CTX *db_context = talloc_new(event_ctx);
282 if (db_context == NULL) {
283 return LDB_ERR_OPERATIONS_ERROR;
286 *am_backup = false;
288 /* note we deliberately leave these open, which allows them to be
289 * re-used in ldb_wrap_connect() */
290 ldb_ctx = samdb_connect(db_context,
291 event_ctx,
292 cmdline_lp_ctx,
293 system_session(cmdline_lp_ctx),
294 NULL,
296 if (ldb_ctx == NULL) {
297 talloc_free(db_context);
298 return LDB_ERR_OPERATIONS_ERROR;
301 ret = handle_inplace_db_upgrade(ldb_ctx);
302 if (ret != LDB_SUCCESS) {
303 talloc_free(db_context);
304 return ret;
307 pdb = privilege_connect(db_context, cmdline_lp_ctx);
308 if (pdb == NULL) {
309 talloc_free(db_context);
310 return LDB_ERR_OPERATIONS_ERROR;
313 /* check the root DB object to see if it's marked as a backup */
314 samba_dsdb_dn = ldb_dn_new(db_context, ldb_ctx, "@SAMBA_DSDB");
315 if (!samba_dsdb_dn) {
316 talloc_free(db_context);
317 return LDB_ERR_OPERATIONS_ERROR;
320 ret = dsdb_search_dn(ldb_ctx, db_context, &res, samba_dsdb_dn, attrs,
321 DSDB_FLAG_AS_SYSTEM);
322 if (ret != LDB_SUCCESS) {
323 talloc_free(db_context);
324 return ret;
327 if (res->count > 0) {
328 msg = ldb_msg_find_attr_as_string(res->msgs[0], "backupDate",
329 NULL);
330 if (msg != NULL) {
331 *am_backup = true;
334 return LDB_SUCCESS;
338 called from 'smbcontrol samba shutdown'
340 static void samba_parent_shutdown(struct imessaging_context *msg,
341 void *private_data,
342 uint32_t msg_type,
343 struct server_id src,
344 size_t num_fds,
345 int *fds,
346 DATA_BLOB *data)
348 struct server_state *state =
349 talloc_get_type_abort(private_data,
350 struct server_state);
351 struct server_id_buf src_buf;
352 struct server_id dst = imessaging_get_server_id(msg);
353 struct server_id_buf dst_buf;
355 if (num_fds != 0) {
356 DBG_WARNING("Received %zu fds, ignoring message\n", num_fds);
357 return;
360 DBG_ERR("samba_shutdown of %s %s: from %s\n",
361 state->binary_name,
362 server_id_str_buf(dst, &dst_buf),
363 server_id_str_buf(src, &src_buf));
365 TALLOC_FREE(state);
366 exit(0);
370 called when a fatal condition occurs in a child task
372 static NTSTATUS samba_terminate(struct irpc_message *msg,
373 struct samba_terminate *r)
375 struct server_state *state = talloc_get_type(msg->private_data,
376 struct server_state);
377 DBG_ERR("samba_terminate of %s %d: %s\n",
378 state->binary_name, (int)getpid(), r->in.reason);
379 TALLOC_FREE(state);
380 exit(1);
384 setup messaging for the top level samba (parent) task
386 static NTSTATUS setup_parent_messaging(struct server_state *state,
387 struct loadparm_context *lp_ctx)
389 struct imessaging_context *msg;
390 NTSTATUS status;
391 if (state == NULL) {
392 return NT_STATUS_UNSUCCESSFUL;
394 msg = imessaging_init(state->event_ctx,
395 lp_ctx,
396 cluster_id(getpid(), SAMBA_PARENT_TASKID),
397 state->event_ctx);
398 NT_STATUS_HAVE_NO_MEMORY(msg);
400 status = irpc_add_name(msg, "samba");
401 if (!NT_STATUS_IS_OK(status)) {
402 return status;
405 status = imessaging_register(msg, state, MSG_SHUTDOWN,
406 samba_parent_shutdown);
407 if (!NT_STATUS_IS_OK(status)) {
408 return status;
411 status = IRPC_REGISTER(msg, irpc, SAMBA_TERMINATE,
412 samba_terminate, state);
413 if (!NT_STATUS_IS_OK(status)) {
414 return status;
417 return NT_STATUS_OK;
422 show build info
424 static void show_build(void)
426 #define CONFIG_OPTION(n) { #n, dyn_ ## n }
427 struct {
428 const char *name;
429 const char *value;
430 } config_options[] = {
431 CONFIG_OPTION(BINDIR),
432 CONFIG_OPTION(SBINDIR),
433 CONFIG_OPTION(CONFIGFILE),
434 CONFIG_OPTION(NCALRPCDIR),
435 CONFIG_OPTION(LOGFILEBASE),
436 CONFIG_OPTION(LMHOSTSFILE),
437 CONFIG_OPTION(DATADIR),
438 CONFIG_OPTION(MODULESDIR),
439 CONFIG_OPTION(LOCKDIR),
440 CONFIG_OPTION(STATEDIR),
441 CONFIG_OPTION(CACHEDIR),
442 CONFIG_OPTION(PIDDIR),
443 CONFIG_OPTION(PRIVATE_DIR),
444 CONFIG_OPTION(CODEPAGEDIR),
445 CONFIG_OPTION(SETUPDIR),
446 CONFIG_OPTION(WINBINDD_SOCKET_DIR),
447 CONFIG_OPTION(NTP_SIGND_SOCKET_DIR),
448 { NULL, NULL}
450 int i;
452 printf("Samba version: %s\n", SAMBA_VERSION_STRING);
453 printf("Build environment:\n");
455 printf("Paths:\n");
456 for (i=0; config_options[i].name; i++) {
457 printf(" %s: %s\n",
458 config_options[i].name,
459 config_options[i].value);
462 exit(0);
465 static int event_ctx_destructor(struct tevent_context *event_ctx)
467 imessaging_dgm_unref_ev(event_ctx);
468 return 0;
471 #ifdef HAVE_PTHREAD
472 static int to_children_fd = -1;
473 static void atfork_prepare(void) {
475 static void atfork_parent(void) {
477 static void atfork_child(void) {
478 if (to_children_fd != -1) {
479 close(to_children_fd);
480 to_children_fd = -1;
483 #endif
486 main server.
488 static int binary_smbd_main(const char *binary_name,
489 int argc,
490 const char *argv[])
492 bool opt_daemon = false;
493 bool opt_fork = true;
494 bool opt_interactive = false;
495 bool opt_no_process_group = false;
496 bool db_is_backup = false;
497 int opt;
498 int ret;
499 poptContext pc;
500 #define _MODULE_PROTO(init) extern NTSTATUS init(TALLOC_CTX *);
501 STATIC_service_MODULES_PROTO;
502 init_module_fn static_init[] = { STATIC_service_MODULES };
503 init_module_fn *shared_init;
504 uint16_t stdin_event_flags;
505 NTSTATUS status;
506 const char *model = "prefork";
507 int max_runtime = 0;
508 struct stat st;
509 enum {
510 OPT_DAEMON = 1000,
511 OPT_FOREGROUND,
512 OPT_INTERACTIVE,
513 OPT_PROCESS_MODEL,
514 OPT_SHOW_BUILD,
515 OPT_NO_PROCESS_GROUP,
517 struct poptOption long_options[] = {
518 POPT_AUTOHELP
520 .longName = "daemon",
521 .shortName = 'D',
522 .argInfo = POPT_ARG_NONE,
523 .val = OPT_DAEMON,
524 .descrip = "Become a daemon (default)",
527 .longName = "foreground",
528 .shortName = 'F',
529 .argInfo = POPT_ARG_NONE,
530 .val = OPT_FOREGROUND,
531 .descrip = "Run the daemon in foreground",
534 .longName = "interactive",
535 .shortName = 'i',
536 .argInfo = POPT_ARG_NONE,
537 .val = OPT_INTERACTIVE,
538 .descrip = "Run interactive (not a daemon)",
541 .longName = "model",
542 .shortName = 'M',
543 .argInfo = POPT_ARG_STRING,
544 .val = OPT_PROCESS_MODEL,
545 .descrip = "Select process model",
546 .argDescrip = "MODEL",
549 .longName = "maximum-runtime",
550 .argInfo = POPT_ARG_INT,
551 .arg = &max_runtime,
552 .descrip = "set maximum runtime of the server process, "
553 "till autotermination",
554 .argDescrip = "seconds"
557 .longName = "show-build",
558 .shortName = 'b',
559 .argInfo = POPT_ARG_NONE,
560 .val = OPT_SHOW_BUILD,
561 .descrip = "show build info",
564 .longName = "no-process-group",
565 .argInfo = POPT_ARG_NONE,
566 .val = OPT_NO_PROCESS_GROUP,
567 .descrip = "Don't create a new process group",
569 POPT_COMMON_SAMBA
570 POPT_COMMON_VERSION
571 POPT_TABLEEND
573 struct server_state *state = NULL;
574 struct tevent_signal *se = NULL;
576 setproctitle("root process");
578 pc = poptGetContext(binary_name, argc, argv, long_options, 0);
579 while((opt = poptGetNextOpt(pc)) != -1) {
580 switch(opt) {
581 case OPT_DAEMON:
582 opt_daemon = true;
583 break;
584 case OPT_FOREGROUND:
585 opt_fork = false;
586 break;
587 case OPT_INTERACTIVE:
588 opt_interactive = true;
589 break;
590 case OPT_PROCESS_MODEL:
591 model = poptGetOptArg(pc);
592 break;
593 case OPT_SHOW_BUILD:
594 show_build();
595 break;
596 case OPT_NO_PROCESS_GROUP:
597 opt_no_process_group = true;
598 break;
599 default:
600 fprintf(stderr, "\nInvalid option %s: %s\n\n",
601 poptBadOption(pc, 0), poptStrerror(opt));
602 poptPrintUsage(pc, stderr, 0);
603 return 1;
607 if (opt_daemon && opt_interactive) {
608 fprintf(stderr,"\nERROR: "
609 "Option -i|--interactive is "
610 "not allowed together with -D|--daemon\n\n");
611 poptPrintUsage(pc, stderr, 0);
612 return 1;
613 } else if (!opt_interactive && opt_fork) {
614 /* default is --daemon */
615 opt_daemon = true;
618 poptFreeContext(pc);
620 talloc_enable_null_tracking();
622 setup_logging(binary_name, opt_interactive?DEBUG_STDOUT:DEBUG_FILE);
623 setup_signals();
625 /* we want total control over the permissions on created files,
626 so set our umask to 0 */
627 umask(0);
629 DEBUG(0,("%s version %s started.\n",
630 binary_name,
631 SAMBA_VERSION_STRING));
632 DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team"
633 " 1992-2020\n"));
635 if (sizeof(uint16_t) < 2 ||
636 sizeof(uint32_t) < 4 ||
637 sizeof(uint64_t) < 8) {
638 DEBUG(0,("ERROR: Samba is not configured correctly "
639 "for the word size on your machine\n"));
640 DEBUGADD(0,("sizeof(uint16_t) = %u, sizeof(uint32_t) %u, "
641 "sizeof(uint64_t) = %u\n",
642 (unsigned int)sizeof(uint16_t),
643 (unsigned int)sizeof(uint32_t),
644 (unsigned int)sizeof(uint64_t)));
645 return 1;
648 if (opt_daemon) {
649 DBG_NOTICE("Becoming a daemon.\n");
650 become_daemon(opt_fork, opt_no_process_group, false);
653 /* Create the memory context to hang everything off. */
654 state = talloc_zero(NULL, struct server_state);
655 if (state == NULL) {
656 exit_daemon("Samba cannot create server state", ENOMEM);
658 * return is never reached but is here to satisfy static
659 * checkers
661 return 1;
663 state->binary_name = binary_name;
665 cleanup_tmp_files(cmdline_lp_ctx);
667 if (!directory_exist(lpcfg_lock_directory(cmdline_lp_ctx))) {
668 mkdir(lpcfg_lock_directory(cmdline_lp_ctx), 0755);
671 if (!directory_exist(lpcfg_pid_directory(cmdline_lp_ctx))) {
672 mkdir(lpcfg_pid_directory(cmdline_lp_ctx), 0755);
675 pidfile_create(lpcfg_pid_directory(cmdline_lp_ctx), binary_name);
677 if (lpcfg_server_role(cmdline_lp_ctx) == ROLE_ACTIVE_DIRECTORY_DC) {
678 if (!open_schannel_session_store(state,
679 cmdline_lp_ctx)) {
680 TALLOC_FREE(state);
681 exit_daemon("Samba cannot open schannel store "
682 "for secured NETLOGON operations.", EACCES);
684 * return is never reached but is here to satisfy static
685 * checkers
687 return 1;
691 /* make sure we won't go through nss_winbind */
692 if (!winbind_off()) {
693 TALLOC_FREE(state);
694 exit_daemon("Samba failed to disable recusive "
695 "winbindd calls.", EACCES);
697 * return is never reached but is here to satisfy static
698 * checkers
700 return 1;
703 gensec_init(); /* FIXME: */
705 process_model_init(cmdline_lp_ctx);
707 shared_init = load_samba_modules(NULL, "service");
709 run_init_functions(NULL, static_init);
710 run_init_functions(NULL, shared_init);
712 talloc_free(shared_init);
714 /* the event context is the top level structure in smbd. Everything else
715 should hang off that */
716 state->event_ctx = s4_event_context_init(state);
718 if (state->event_ctx == NULL) {
719 TALLOC_FREE(state);
720 exit_daemon("Initializing event context failed", EACCES);
722 * return is never reached but is here to satisfy static
723 * checkers
725 return 1;
728 talloc_set_destructor(state->event_ctx, event_ctx_destructor);
730 if (opt_interactive) {
731 /* terminate when stdin goes away */
732 stdin_event_flags = TEVENT_FD_READ;
733 } else {
734 /* stay alive forever */
735 stdin_event_flags = 0;
738 #ifdef HAVE_SETPGID
740 * If we're interactive we want to set our own process group for
741 * signal management, unless --no-process-group specified.
743 if (opt_interactive && !opt_no_process_group)
744 setpgid((pid_t)0, (pid_t)0);
745 #endif
747 /* catch EOF on stdin */
748 #ifdef SIGTTIN
749 signal(SIGTTIN, SIG_IGN);
750 #endif
752 if (fstat(0, &st) != 0) {
753 TALLOC_FREE(state);
754 exit_daemon("Samba failed to set standard input handler",
755 ENOTTY);
757 * return is never reached but is here to satisfy static
758 * checkers
760 return 1;
763 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
764 struct tevent_fd *fde = tevent_add_fd(state->event_ctx,
765 state->event_ctx,
767 stdin_event_flags,
768 server_stdin_handler,
769 state);
770 if (fde == NULL) {
771 TALLOC_FREE(state);
772 exit_daemon("Initializing stdin failed", ENOMEM);
774 * return is never reached but is here to
775 * satisfy static checkers
777 return 1;
781 if (max_runtime) {
782 struct tevent_timer *te;
783 DBG_ERR("%s PID %d was called with maxruntime %d - "
784 "current ts %llu\n",
785 binary_name, (int)getpid(),
786 max_runtime, (unsigned long long) time(NULL));
787 te = tevent_add_timer(state->event_ctx, state->event_ctx,
788 timeval_current_ofs(max_runtime, 0),
789 max_runtime_handler,
790 state);
791 if (te == NULL) {
792 TALLOC_FREE(state);
793 exit_daemon("Maxruntime handler failed", ENOMEM);
795 * return is never reached but is here to
796 * satisfy static checkers
798 return 1;
802 se = tevent_add_signal(state->event_ctx,
803 state->event_ctx,
804 SIGTERM,
806 sigterm_signal_handler,
807 state);
808 if (se == NULL) {
809 TALLOC_FREE(state);
810 exit_daemon("Initialize SIGTERM handler failed", ENOMEM);
812 * return is never reached but is here to satisfy static
813 * checkers
815 return 1;
818 if (lpcfg_server_role(cmdline_lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC
819 && !lpcfg_parm_bool(cmdline_lp_ctx, NULL,
820 "server role check", "inhibit", false)
821 && !str_list_check_ci(lpcfg_server_services(cmdline_lp_ctx), "smb")
822 && !str_list_check_ci(lpcfg_dcerpc_endpoint_servers(cmdline_lp_ctx),
823 "remote")
824 && !str_list_check_ci(lpcfg_dcerpc_endpoint_servers(cmdline_lp_ctx),
825 "mapiproxy")) {
826 DEBUG(0, ("At this time the 'samba' binary should only be used "
827 "for either:\n"));
828 DEBUGADD(0, ("'server role = active directory domain "
829 "controller' or to access the ntvfs file server "
830 "with 'server services = +smb' or the rpc proxy "
831 "with 'dcerpc endpoint servers = remote'\n"));
832 DEBUGADD(0, ("You should start smbd/nmbd/winbindd instead for "
833 "domain member and standalone file server tasks\n"));
834 exit_daemon("Samba detected misconfigured 'server role' "
835 "and exited. Check logs for details", EINVAL);
838 ret = prime_ldb_databases(state->event_ctx, &db_is_backup);
839 if (ret != LDB_SUCCESS) {
840 TALLOC_FREE(state);
841 exit_daemon("Samba failed to prime database", EINVAL);
843 * return is never reached but is here to satisfy static
844 * checkers
846 return 1;
849 if (db_is_backup) {
850 TALLOC_FREE(state);
851 exit_daemon("Database is a backup. Please run samba-tool domain"
852 " backup restore", EINVAL);
854 * return is never reached but is here to satisfy static
855 * checkers
857 return 1;
860 status = setup_parent_messaging(state, cmdline_lp_ctx);
861 if (!NT_STATUS_IS_OK(status)) {
862 TALLOC_FREE(state);
863 exit_daemon("Samba failed to setup parent messaging",
864 NT_STATUS_V(status));
866 * return is never reached but is here to satisfy static
867 * checkers
869 return 1;
872 DBG_ERR("%s: using '%s' process model\n", binary_name, model);
875 int child_pipe[2];
876 int rc;
877 bool start_services = false;
879 rc = pipe(child_pipe);
880 if (rc < 0) {
881 TALLOC_FREE(state);
882 exit_daemon("Samba failed to open process control pipe",
883 errno);
885 * return is never reached but is here to satisfy static
886 * checkers
888 return 1;
890 smb_set_close_on_exec(child_pipe[0]);
891 smb_set_close_on_exec(child_pipe[1]);
893 #ifdef HAVE_PTHREAD
894 to_children_fd = child_pipe[1];
895 pthread_atfork(atfork_prepare, atfork_parent,
896 atfork_child);
897 start_services = true;
898 #else
899 pid_t pid;
900 struct tfork *t = NULL;
901 t = tfork_create();
902 if (t == NULL) {
903 exit_daemon(
904 "Samba unable to fork master process",
907 pid = tfork_child_pid(t);
908 if (pid == 0) {
909 start_services = false;
910 } else {
911 /* In the child process */
912 start_services = true;
913 close(child_pipe[1]);
915 #endif
916 if (start_services) {
917 status = server_service_startup(
918 state->event_ctx, cmdline_lp_ctx, model,
919 lpcfg_server_services(cmdline_lp_ctx),
920 child_pipe[0]);
921 if (!NT_STATUS_IS_OK(status)) {
922 TALLOC_FREE(state);
923 exit_daemon("Samba failed to start services",
924 NT_STATUS_V(status));
926 * return is never reached but is here to
927 * satisfy static checkers
929 return 1;
934 if (opt_daemon) {
935 daemon_ready("samba");
938 /* wait for events - this is where smbd sits for most of its
939 life */
940 tevent_loop_wait(state->event_ctx);
942 /* as everything hangs off this state->event context, freeing state
943 will initiate a clean shutdown of all services */
944 TALLOC_FREE(state);
946 return 0;
949 int main(int argc, const char *argv[])
951 setproctitle_init(argc, discard_const(argv), environ);
953 return binary_smbd_main("samba", argc, argv);