/*****************************************************************************\ * Copyright (c) 2002 Pelle Johansson. * * All rights reserved. * * * * This file is part of the moftpd package. Use and distribution of * * this software is governed by the terms in the file LICENCE, which * * should have come with this package. * \*****************************************************************************/ /* $moftpd: main.c 1264 2005-04-06 13:32:27Z morth $ */ #include "system.h" #include "main.h" #include "connection.h" #include "server.h" #include "utf8fs/file.h" #include "confparse.h" #include "utf8fs/memory.h" #include "accounter.h" #include "events.h" static int doFork = 1; int debug = 0, accClientSock = -1; int running = 1, runningAsRoot; const char *configFile = ETCDIR "/moftpd.conf"; int forkConnections = 1, skipFd0 = 0, closeServers = 0, selfFailedFork = 1; size_t maxMmapSize = 10 * 1024 * 1024; int urgData = 0, reloadConfig = 0; uid_t unprivUid; gid_t unprivGid; const char *localeDir = SHAREDIR "/" PACKAGE_NAME "/locale/"; const char *localeSuffix = ".loc"; const char *pidFile = VARDIR "/run/" PACKAGE_NAME ".pid"; const char *templateDir = SHAREDIR "/" PACKAGE_NAME "/"; char *sslCertsPath; int forker, doFakeChroot; extern char *optarg; extern int optind; extern int fakeChroot, accServer; void sigkill(int unused) { running = 0; } void sigquit(int unused) { running = 0; } void sigurg(int unused) { urgData = 1; } void sigchld(int unused) { wait(NULL); } void sigint(int unused) { close_accounter (0); quit_all_connections("421 Server shutting down."); closeServers = 1; } void sighup(int unused) { sigint(unused); reloadConfig = 1; } int main(int argc, char *argv[]) { connection_t *conn; char startCwd[MAXPATHLEN]; int fd, wrotePidFile = 0; #if defined (RLIMIT_CORE) || defined (RLIMIT_FSIZE) struct rlimit rl; #endif signal(SIGKILL, sigkill); signal(SIGQUIT, sigquit); signal(SIGURG, sigurg); signal(SIGCHLD, sigchld); signal(SIGINT, sigint); signal(SIGHUP, sighup); signal (SIGPIPE, SIG_IGN); #ifdef SIGXFSZ signal (SIGXFSZ, SIG_IGN); #endif setprogname (argv[0]); unprivUid = geteuid(); unprivGid = getegid(); runningAsRoot = !unprivUid; mem_init (); events_init (); #ifdef USE_TLS sslCertsPath = pstring (tls_get_cert_dir (), NULL); #else sslCertsPath = pstring ("/etc/certs", NULL); #endif argc = parse_options(argc, &argv); if(argc < 0) return 1; if(argc && !getenv("MOFTP_CHILD")) usage(); /* * Use NDELAY since we might chroot. */ openlog (getprogname (), LOG_PID | LOG_NDELAY | (debug? LOG_PERROR : 0), LOG_FTP); if (!debug) { #ifdef RLIMIT_CORE if (getrlimit (RLIMIT_CORE, &rl)) rl.rlim_max = 0; rl.rlim_cur = 0; setrlimit (RLIMIT_CORE, &rl); #endif setlogmask (LOG_UPTO (LOG_INFO)); } #ifdef RLIMIT_FSIZE if (getrlimit (RLIMIT_FSIZE, &rl)) rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; setrlimit (RLIMIT_FSIZE, &rl); #endif getcwd (startCwd, sizeof (startCwd)); loadConfig: if (reloadConfig) chdir (startCwd); set_access (NULL, 1, -1, -1); // Stop symlink resolving if(read_config(configFile)) { if (!reloadConfig) { if (errno == ENOENT) { if (strncmp (configFile, templateDir, strlen (templateDir))) fprintf (stderr, "To run a default server, use -C