pidl/lib: Add recursion detection logic to prevent looping.
[Samba.git] / source4 / samba / server.c
blob4698ac0320b85c2bb2295cfbab819ad9a031d584
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/cmdline.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 "samba/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 "nsswitch/winbind_client.h"
44 #include "libds/common/roles.h"
45 #include "lib/util/tfork.h"
46 #include "dsdb/samdb/ldb_modules/util.h"
47 #include "lib/util/server_id.h"
48 #include "server_util.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);
157 static void sighup_signal_handler(struct tevent_context *ev,
158 struct tevent_signal *se,
159 int signum, int count, void *siginfo,
160 void *private_data)
162 struct server_state *state = talloc_get_type_abort(
163 private_data, struct server_state);
165 DBG_DEBUG("Process %s got SIGHUP\n", state->binary_name);
167 reopen_logs_internal();
171 setup signal masks
173 static void setup_signals(void)
175 /* we are never interested in SIGPIPE */
176 BlockSignals(true,SIGPIPE);
178 #if defined(SIGFPE)
179 /* we are never interested in SIGFPE */
180 BlockSignals(true,SIGFPE);
181 #endif
183 /* We are no longer interested in USR1 */
184 BlockSignals(true, SIGUSR1);
186 #if defined(SIGUSR2)
187 /* We are no longer interested in USR2 */
188 BlockSignals(true,SIGUSR2);
189 #endif
191 /* POSIX demands that signals are inherited. If the invoking process has
192 * these signals masked, we will have problems,
193 * as we won't receive them. */
194 BlockSignals(false, SIGHUP);
195 BlockSignals(false, SIGTERM);
197 CatchSignal(SIGHUP, sig_hup);
198 CatchSignal(SIGTERM, sig_term);
202 handle io on stdin
204 static void server_stdin_handler(struct tevent_context *event_ctx,
205 struct tevent_fd *fde,
206 uint16_t flags,
207 void *private_data)
209 struct server_state *state = talloc_get_type_abort(
210 private_data, struct server_state);
211 uint8_t c;
212 if (read(0, &c, 1) == 0) {
213 DBG_ERR("%s: EOF on stdin - PID %d terminating\n",
214 state->binary_name, (int)getpid());
215 #ifdef HAVE_GETPGRP
216 if (getpgrp() == getpid()) {
217 DBG_ERR("Sending SIGTERM from pid %d\n",
218 (int)getpid());
219 kill(-getpgrp(), SIGTERM);
221 #endif
222 TALLOC_FREE(state);
223 exit(0);
228 die if the user selected maximum runtime is exceeded
230 _NORETURN_ static void max_runtime_handler(struct tevent_context *ev,
231 struct tevent_timer *te,
232 struct timeval t, void *private_data)
234 struct server_state *state = talloc_get_type_abort(
235 private_data, struct server_state);
236 DBG_ERR("%s: maximum runtime exceeded - "
237 "terminating PID %d at %llu, current ts: %llu\n",
238 state->binary_name,
239 (int)getpid(),
240 (unsigned long long)t.tv_sec,
241 (unsigned long long)time(NULL));
242 TALLOC_FREE(state);
243 exit(0);
247 * When doing an in-place upgrade of Samba, the database format may have
248 * changed between versions. E.g. between 4.7 and 4.8 the DB changed from
249 * DN-based indexes to GUID-based indexes, so we have to re-index the DB after
250 * upgrading.
251 * This function handles migrating an older samba DB to a new Samba release.
252 * Note that we have to maintain DB compatibility between *all* older versions
253 * of Samba, not just the ones still under maintenance support.
255 * Finally, while the transaction is open, check if we support the set
256 * domain functional level, if the DC functional level on our object
257 * is correct and if not to update it (except on the RODC)
259 static int handle_inplace_db_upgrade_check_and_update_fl(struct ldb_context *ldb_ctx,
260 struct loadparm_context *lp_ctx)
262 int ret;
265 * The DSDB stack will handle reindexing the DB (if needed) upon the first
266 * DB write. Open and close a transaction on the DB now to trigger a
267 * reindex if required, rather than waiting for the first write.
268 * We do this here to guarantee that the DB will have been re-indexed by
269 * the time the main samba code runs.
270 * Refer to dsdb_schema_set_indices_and_attributes() for the actual reindexing
271 * code, called from
272 * source4/dsdb/samdb/ldb_modules/schema_load.c:schema_load_start_transaction()
274 ret = ldb_transaction_start(ldb_ctx);
275 if (ret != LDB_SUCCESS) {
276 return ret;
279 ret = dsdb_check_and_update_fl(ldb_ctx, lp_ctx);
280 if (ret != LDB_SUCCESS) {
281 ldb_transaction_cancel(ldb_ctx);
282 return ret;
285 ret = ldb_transaction_commit(ldb_ctx);
286 if (ret != LDB_SUCCESS) {
287 return ret;
289 return LDB_SUCCESS;
293 pre-open the key databases. This saves a lot of time in child
294 processes
296 static int prime_ldb_databases(struct tevent_context *event_ctx, bool *am_backup)
298 struct ldb_result *res = NULL;
299 struct ldb_dn *samba_dsdb_dn = NULL;
300 struct ldb_context *ldb_ctx = NULL;
301 struct ldb_context *pdb = NULL;
302 static const char *attrs[] = { "backupDate", NULL };
303 struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx();
304 const char *msg = NULL;
305 int ret;
306 TALLOC_CTX *db_context = talloc_new(event_ctx);
307 if (db_context == NULL) {
308 return LDB_ERR_OPERATIONS_ERROR;
311 *am_backup = false;
313 /* note we deliberately leave these open, which allows them to be
314 * re-used in ldb_wrap_connect() */
315 ldb_ctx = samdb_connect(db_context,
316 event_ctx,
317 lp_ctx,
318 system_session(lp_ctx),
319 NULL,
321 if (ldb_ctx == NULL) {
322 talloc_free(db_context);
323 return LDB_ERR_OPERATIONS_ERROR;
326 ret = handle_inplace_db_upgrade_check_and_update_fl(ldb_ctx,
327 lp_ctx);
328 if (ret != LDB_SUCCESS) {
329 talloc_free(db_context);
330 return ret;
333 pdb = privilege_connect(db_context, lp_ctx);
334 if (pdb == NULL) {
335 talloc_free(db_context);
336 return LDB_ERR_OPERATIONS_ERROR;
339 /* check the root DB object to see if it's marked as a backup */
340 samba_dsdb_dn = ldb_dn_new(db_context, ldb_ctx, "@SAMBA_DSDB");
341 if (!samba_dsdb_dn) {
342 talloc_free(db_context);
343 return LDB_ERR_OPERATIONS_ERROR;
346 ret = dsdb_search_dn(ldb_ctx, db_context, &res, samba_dsdb_dn, attrs,
347 DSDB_FLAG_AS_SYSTEM);
348 if (ret != LDB_SUCCESS) {
349 talloc_free(db_context);
350 return ret;
353 if (res->count > 0) {
354 msg = ldb_msg_find_attr_as_string(res->msgs[0], "backupDate",
355 NULL);
356 if (msg != NULL) {
357 *am_backup = true;
360 return LDB_SUCCESS;
364 called from 'smbcontrol samba shutdown'
366 static void samba_parent_shutdown(struct imessaging_context *msg,
367 void *private_data,
368 uint32_t msg_type,
369 struct server_id src,
370 size_t num_fds,
371 int *fds,
372 DATA_BLOB *data)
374 struct server_state *state =
375 talloc_get_type_abort(private_data,
376 struct server_state);
377 struct server_id_buf src_buf;
378 struct server_id dst = imessaging_get_server_id(msg);
379 struct server_id_buf dst_buf;
381 if (num_fds != 0) {
382 DBG_WARNING("Received %zu fds, ignoring message\n", num_fds);
383 return;
386 DBG_ERR("samba_shutdown of %s %s: from %s\n",
387 state->binary_name,
388 server_id_str_buf(dst, &dst_buf),
389 server_id_str_buf(src, &src_buf));
391 TALLOC_FREE(state);
392 exit(0);
396 called when a fatal condition occurs in a child task
398 static NTSTATUS samba_terminate(struct irpc_message *msg,
399 struct samba_terminate *r)
401 struct server_state *state = talloc_get_type(msg->private_data,
402 struct server_state);
403 DBG_ERR("samba_terminate of %s %d: %s\n",
404 state->binary_name, (int)getpid(), r->in.reason);
405 TALLOC_FREE(state);
406 exit(1);
410 setup messaging for the top level samba (parent) task
412 static NTSTATUS setup_parent_messaging(struct server_state *state,
413 struct loadparm_context *lp_ctx)
415 struct imessaging_context *msg;
416 NTSTATUS status;
417 if (state == NULL) {
418 return NT_STATUS_UNSUCCESSFUL;
420 msg = imessaging_init(state->event_ctx,
421 lp_ctx,
422 cluster_id(getpid(), SAMBA_PARENT_TASKID),
423 state->event_ctx);
424 NT_STATUS_HAVE_NO_MEMORY(msg);
426 status = irpc_add_name(msg, "samba");
427 if (!NT_STATUS_IS_OK(status)) {
428 return status;
431 status = imessaging_register(msg, state, MSG_SHUTDOWN,
432 samba_parent_shutdown);
433 if (!NT_STATUS_IS_OK(status)) {
434 return status;
437 status = IRPC_REGISTER(msg, irpc, SAMBA_TERMINATE,
438 samba_terminate, state);
439 if (!NT_STATUS_IS_OK(status)) {
440 return status;
443 return NT_STATUS_OK;
448 show build info
450 static void show_build(void)
452 #define CONFIG_OPTION(n) { #n, dyn_ ## n }
453 struct {
454 const char *name;
455 const char *value;
456 } config_options[] = {
457 CONFIG_OPTION(BINDIR),
458 CONFIG_OPTION(SBINDIR),
459 CONFIG_OPTION(CONFIGFILE),
460 CONFIG_OPTION(NCALRPCDIR),
461 CONFIG_OPTION(LOGFILEBASE),
462 CONFIG_OPTION(LMHOSTSFILE),
463 CONFIG_OPTION(DATADIR),
464 CONFIG_OPTION(MODULESDIR),
465 CONFIG_OPTION(LOCKDIR),
466 CONFIG_OPTION(STATEDIR),
467 CONFIG_OPTION(CACHEDIR),
468 CONFIG_OPTION(PIDDIR),
469 CONFIG_OPTION(PRIVATE_DIR),
470 CONFIG_OPTION(CODEPAGEDIR),
471 CONFIG_OPTION(SETUPDIR),
472 CONFIG_OPTION(WINBINDD_SOCKET_DIR),
473 CONFIG_OPTION(NTP_SIGND_SOCKET_DIR),
474 { NULL, NULL}
476 int i;
478 printf("Samba version: %s\n", SAMBA_VERSION_STRING);
479 printf("Build environment:\n");
481 printf("Paths:\n");
482 for (i=0; config_options[i].name; i++) {
483 printf(" %s: %s\n",
484 config_options[i].name,
485 config_options[i].value);
488 exit(0);
491 static int event_ctx_destructor(struct tevent_context *event_ctx)
493 imessaging_dgm_unref_ev(event_ctx);
494 return 0;
497 #ifdef HAVE_PTHREAD
498 static int to_children_fd = -1;
499 static void atfork_prepare(void) {
501 static void atfork_parent(void) {
503 static void atfork_child(void) {
504 if (to_children_fd != -1) {
505 close(to_children_fd);
506 to_children_fd = -1;
509 #endif
512 main server.
514 static int binary_smbd_main(TALLOC_CTX *mem_ctx,
515 const char *binary_name,
516 int argc,
517 const char *argv[])
519 struct samba_cmdline_daemon_cfg *cmdline_daemon_cfg = NULL;
520 bool db_is_backup = false;
521 int opt;
522 int ret;
523 poptContext pc;
524 uint16_t stdin_event_flags;
525 NTSTATUS status;
526 const char *model = "prefork";
527 int max_runtime = 0;
528 struct stat st;
529 enum {
530 OPT_PROCESS_MODEL = 1000,
531 OPT_SHOW_BUILD,
533 struct poptOption long_options[] = {
534 POPT_AUTOHELP
536 .longName = "model",
537 .shortName = 'M',
538 .argInfo = POPT_ARG_STRING,
539 .val = OPT_PROCESS_MODEL,
540 .descrip = "Select process model",
541 .argDescrip = "MODEL",
544 .longName = "maximum-runtime",
545 .argInfo = POPT_ARG_INT,
546 .arg = &max_runtime,
547 .descrip = "set maximum runtime of the server process, "
548 "till autotermination",
549 .argDescrip = "seconds"
552 .longName = "show-build",
553 .shortName = 'b',
554 .argInfo = POPT_ARG_NONE,
555 .val = OPT_SHOW_BUILD,
556 .descrip = "show build info",
558 POPT_COMMON_SAMBA
559 POPT_COMMON_DAEMON
560 POPT_COMMON_VERSION
561 POPT_TABLEEND
563 struct server_state *state = NULL;
564 struct tevent_signal *se = NULL;
565 struct samba_tevent_trace_state *samba_tevent_trace_state = NULL;
566 struct loadparm_context *lp_ctx = NULL;
567 bool ok;
569 setproctitle("root process");
571 ok = samba_cmdline_init(mem_ctx,
572 SAMBA_CMDLINE_CONFIG_SERVER,
573 true /* require_smbconf */);
574 if (!ok) {
575 DBG_ERR("Failed to init cmdline parser!\n");
576 TALLOC_FREE(mem_ctx);
577 exit(1);
580 cmdline_daemon_cfg = samba_cmdline_get_daemon_cfg();
582 pc = samba_popt_get_context(binary_name,
583 argc,
584 argv,
585 long_options,
587 if (pc == NULL) {
588 DBG_ERR("Failed to setup popt context!\n");
589 TALLOC_FREE(mem_ctx);
590 exit(1);
593 while((opt = poptGetNextOpt(pc)) != -1) {
594 switch(opt) {
595 case OPT_PROCESS_MODEL:
596 model = poptGetOptArg(pc);
597 break;
598 case OPT_SHOW_BUILD:
599 show_build();
600 break;
601 default:
602 fprintf(stderr, "\nInvalid option %s: %s\n\n",
603 poptBadOption(pc, 0), poptStrerror(opt));
604 poptPrintUsage(pc, stderr, 0);
605 return 1;
609 if (cmdline_daemon_cfg->daemon && cmdline_daemon_cfg->interactive) {
610 fprintf(stderr,"\nERROR: "
611 "Option -i|--interactive is "
612 "not allowed together with -D|--daemon\n\n");
613 poptPrintUsage(pc, stderr, 0);
614 return 1;
615 } else if (!cmdline_daemon_cfg->interactive &&
616 cmdline_daemon_cfg->fork) {
617 /* default is --daemon */
618 cmdline_daemon_cfg->daemon = true;
621 poptFreeContext(pc);
623 lp_ctx = samba_cmdline_get_lp_ctx();
625 talloc_enable_null_tracking();
627 setup_signals();
629 /* we want total control over the permissions on created files,
630 so set our umask to 0 */
631 umask(0);
633 DEBUG(0,("%s version %s started.\n",
634 binary_name,
635 SAMBA_VERSION_STRING));
636 DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team"
637 " 1992-2023\n"));
639 if (sizeof(uint16_t) < 2 ||
640 sizeof(uint32_t) < 4 ||
641 sizeof(uint64_t) < 8) {
642 DEBUG(0,("ERROR: Samba is not configured correctly "
643 "for the word size on your machine\n"));
644 DEBUGADD(0,("sizeof(uint16_t) = %u, sizeof(uint32_t) %u, "
645 "sizeof(uint64_t) = %u\n",
646 (unsigned int)sizeof(uint16_t),
647 (unsigned int)sizeof(uint32_t),
648 (unsigned int)sizeof(uint64_t)));
649 return 1;
652 if (cmdline_daemon_cfg->daemon) {
653 DBG_NOTICE("Becoming a daemon.\n");
654 become_daemon(cmdline_daemon_cfg->fork,
655 cmdline_daemon_cfg->no_process_group,
656 false);
657 } else if (!cmdline_daemon_cfg->interactive) {
658 daemon_status("samba", "Starting process...");
661 /* Create the memory context to hang everything off. */
662 state = talloc_zero(mem_ctx, struct server_state);
663 if (state == NULL) {
664 exit_daemon("Samba cannot create server state", ENOMEM);
666 * return is never reached but is here to satisfy static
667 * checkers
669 return 1;
671 state->binary_name = binary_name;
673 cleanup_tmp_files(lp_ctx);
675 if (!directory_exist(lpcfg_lock_directory(lp_ctx))) {
676 mkdir(lpcfg_lock_directory(lp_ctx), 0755);
679 if (!directory_exist(lpcfg_pid_directory(lp_ctx))) {
680 mkdir(lpcfg_pid_directory(lp_ctx), 0755);
683 pidfile_create(lpcfg_pid_directory(lp_ctx), binary_name);
685 if (lpcfg_server_role(lp_ctx) == ROLE_ACTIVE_DIRECTORY_DC) {
686 if (!open_schannel_session_store(state,
687 lp_ctx)) {
688 TALLOC_FREE(state);
689 exit_daemon("Samba cannot open schannel store "
690 "for secured NETLOGON operations.", EACCES);
692 * return is never reached but is here to satisfy static
693 * checkers
695 return 1;
699 /* make sure we won't go through nss_winbind */
700 if (!winbind_off()) {
701 TALLOC_FREE(state);
702 exit_daemon("Samba failed to disable recursive "
703 "winbindd calls.", EACCES);
705 * return is never reached but is here to satisfy static
706 * checkers
708 return 1;
711 gensec_init(); /* FIXME: */
713 process_model_init(lp_ctx);
715 samba_service_init();
717 /* the event context is the top level structure in smbd. Everything else
718 should hang off that */
719 state->event_ctx = s4_event_context_init(state);
721 if (state->event_ctx == NULL) {
722 TALLOC_FREE(state);
723 exit_daemon("Initializing event context failed", EACCES);
725 * return is never reached but is here to satisfy static
726 * checkers
728 return 1;
731 talloc_set_destructor(state->event_ctx, event_ctx_destructor);
733 samba_tevent_trace_state = create_samba_tevent_trace_state(state);
734 if (samba_tevent_trace_state == NULL) {
735 exit_daemon("Samba failed to setup tevent tracing state",
736 ENOTTY);
738 * return is never reached but is here to satisfy static
739 * checkers
741 return 1;
744 tevent_set_trace_callback(state->event_ctx,
745 samba_tevent_trace_callback,
746 samba_tevent_trace_state);
748 if (cmdline_daemon_cfg->interactive) {
749 /* terminate when stdin goes away */
750 stdin_event_flags = TEVENT_FD_READ;
751 } else {
752 /* stay alive forever */
753 stdin_event_flags = 0;
756 #ifdef HAVE_SETPGID
758 * If we're interactive we want to set our own process group for
759 * signal management, unless --no-process-group specified.
761 if (cmdline_daemon_cfg->interactive &&
762 !cmdline_daemon_cfg->no_process_group)
764 setpgid((pid_t)0, (pid_t)0);
766 #endif
768 /* catch EOF on stdin */
769 #ifdef SIGTTIN
770 signal(SIGTTIN, SIG_IGN);
771 #endif
773 if (fstat(0, &st) != 0) {
774 TALLOC_FREE(state);
775 exit_daemon("Samba failed to set standard input handler",
776 ENOTTY);
778 * return is never reached but is here to satisfy static
779 * checkers
781 return 1;
784 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
785 struct tevent_fd *fde = tevent_add_fd(state->event_ctx,
786 state->event_ctx,
788 stdin_event_flags,
789 server_stdin_handler,
790 state);
791 if (fde == NULL) {
792 TALLOC_FREE(state);
793 exit_daemon("Initializing stdin failed", ENOMEM);
795 * return is never reached but is here to
796 * satisfy static checkers
798 return 1;
802 if (max_runtime) {
803 struct tevent_timer *te;
804 DBG_ERR("%s PID %d was called with maxruntime %d - "
805 "current ts %llu\n",
806 binary_name, (int)getpid(),
807 max_runtime, (unsigned long long) time(NULL));
808 te = tevent_add_timer(state->event_ctx, state->event_ctx,
809 timeval_current_ofs(max_runtime, 0),
810 max_runtime_handler,
811 state);
812 if (te == NULL) {
813 TALLOC_FREE(state);
814 exit_daemon("Maxruntime handler failed", ENOMEM);
816 * return is never reached but is here to
817 * satisfy static checkers
819 return 1;
823 se = tevent_add_signal(state->event_ctx,
824 state->event_ctx,
825 SIGTERM,
827 sigterm_signal_handler,
828 state);
829 if (se == NULL) {
830 TALLOC_FREE(state);
831 exit_daemon("Initialize SIGTERM handler failed", ENOMEM);
833 * return is never reached but is here to satisfy static
834 * checkers
836 return 1;
839 se = tevent_add_signal(state->event_ctx,
840 state->event_ctx,
841 SIGHUP,
843 sighup_signal_handler,
844 state);
845 if (se == NULL) {
846 TALLOC_FREE(state);
847 exit_daemon("Initialize SIGHUP handler failed", ENOMEM);
849 * return is never reached but is here to satisfy static
850 * checkers
852 return 1;
855 if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC
856 && !lpcfg_parm_bool(lp_ctx, NULL,
857 "server role check", "inhibit", false)
858 && !str_list_check_ci(lpcfg_server_services(lp_ctx), "smb")
859 && !str_list_check_ci(lpcfg_dcerpc_endpoint_servers(lp_ctx),
860 "remote")
861 && !str_list_check_ci(lpcfg_dcerpc_endpoint_servers(lp_ctx),
862 "mapiproxy")) {
863 DEBUG(0, ("At this time the 'samba' binary should only be used "
864 "for either:\n"));
865 DEBUGADD(0, ("'server role = active directory domain "
866 "controller' or the rpc proxy "
867 "with 'dcerpc endpoint servers = remote'\n"));
868 DEBUGADD(0, ("You should start smbd/nmbd/winbindd instead for "
869 "domain member and standalone file server tasks\n"));
870 exit_daemon("Samba detected misconfigured 'server role' "
871 "and exited. Check logs for details", EINVAL);
874 ret = prime_ldb_databases(state->event_ctx, &db_is_backup);
875 if (ret != LDB_SUCCESS) {
876 TALLOC_FREE(state);
877 exit_daemon("Samba failed to prime database", EINVAL);
879 * return is never reached but is here to satisfy static
880 * checkers
882 return 1;
885 if (db_is_backup) {
886 TALLOC_FREE(state);
887 exit_daemon("Database is a backup. Please run samba-tool domain"
888 " backup restore", EINVAL);
890 * return is never reached but is here to satisfy static
891 * checkers
893 return 1;
896 status = setup_parent_messaging(state, lp_ctx);
897 if (!NT_STATUS_IS_OK(status)) {
898 TALLOC_FREE(state);
899 exit_daemon("Samba failed to setup parent messaging",
900 NT_STATUS_V(status));
902 * return is never reached but is here to satisfy static
903 * checkers
905 return 1;
908 DBG_ERR("%s: using '%s' process model\n", binary_name, model);
911 int child_pipe[2];
912 int rc;
913 bool start_services = false;
915 rc = pipe(child_pipe);
916 if (rc < 0) {
917 TALLOC_FREE(state);
918 exit_daemon("Samba failed to open process control pipe",
919 errno);
921 * return is never reached but is here to satisfy static
922 * checkers
924 return 1;
926 smb_set_close_on_exec(child_pipe[0]);
927 smb_set_close_on_exec(child_pipe[1]);
929 #ifdef HAVE_PTHREAD
930 to_children_fd = child_pipe[1];
931 pthread_atfork(atfork_prepare, atfork_parent,
932 atfork_child);
933 start_services = true;
934 #else
935 pid_t pid;
936 struct tfork *t = NULL;
937 t = tfork_create();
938 if (t == NULL) {
939 exit_daemon(
940 "Samba unable to fork master process",
943 pid = tfork_child_pid(t);
944 if (pid == 0) {
945 start_services = false;
946 } else {
947 /* In the child process */
948 start_services = true;
949 close(child_pipe[1]);
951 #endif
952 if (start_services) {
953 status = server_service_startup(
954 state->event_ctx, lp_ctx, model,
955 lpcfg_server_services(lp_ctx),
956 child_pipe[0]);
957 if (!NT_STATUS_IS_OK(status)) {
958 TALLOC_FREE(state);
959 exit_daemon("Samba failed to start services",
960 NT_STATUS_V(status));
962 * return is never reached but is here to
963 * satisfy static checkers
965 return 1;
970 if (!cmdline_daemon_cfg->interactive) {
971 daemon_ready("samba");
974 /* wait for events - this is where smbd sits for most of its
975 life */
976 tevent_loop_wait(state->event_ctx);
978 /* as everything hangs off this state->event context, freeing state
979 will initiate a clean shutdown of all services */
980 TALLOC_FREE(state);
982 return 0;
985 int main(int argc, const char *argv[])
987 TALLOC_CTX *mem_ctx = NULL;
988 int rc;
990 mem_ctx = talloc_init("samba/server.c#main");
991 if (mem_ctx == NULL) {
992 exit(ENOMEM);
995 setproctitle_init(argc, discard_const(argv), environ);
997 rc = binary_smbd_main(mem_ctx, "samba", argc, argv);
999 TALLOC_FREE(mem_ctx);
1000 return rc;