diff options
Diffstat (limited to 'src/common/core.c')
-rw-r--r-- | src/common/core.c | 143 |
1 files changed, 121 insertions, 22 deletions
diff --git a/src/common/core.c b/src/common/core.c index 201d4f5e8..9a131d042 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -26,23 +26,27 @@ #include "common/cbasetypes.h" #include "common/console.h" #include "common/db.h" +#include "common/des.h" +#include "common/grfio.h" #include "common/memmgr.h" #include "common/mmo.h" -#include "common/random.h" +#include "common/nullpo.h" #include "common/showmsg.h" #include "common/strlib.h" #include "common/sysinfo.h" -#include "common/nullpo.h" +#include "common/timer.h" +#include "common/utils.h" #ifndef MINICORE # include "common/HPM.h" # include "common/conf.h" # include "common/ers.h" +# include "common/md5calc.h" +# include "common/mutex.h" +# include "common/random.h" # include "common/socket.h" # include "common/sql.h" # include "common/thread.h" -# include "common/timer.h" -# include "common/utils.h" #endif #ifndef _WIN32 @@ -54,6 +58,28 @@ #include <stdio.h> #include <stdlib.h> +/* + * Uncomment the line below if you want to silence the root warning on startup + * (not recommended, as it opens the machine to security risks. You should + * never ever run software as root unless it requires the extra privileges + * (which Hercules does not.) + * More info: + * http://www.tldp.org/HOWTO/Security-HOWTO/local-security.html + * http://www.gentoo.org/doc/en/security/security-handbook.xml?style=printable&part=1&chap=1#doc_chap4 + * http://wiki.centos.org/TipsAndTricks/BecomingRoot + * http://fedoraproject.org/wiki/Configuring_Sudo + * https://help.ubuntu.com/community/RootSudo + * http://www.freebsdwiki.net/index.php/Root + * + * If your service provider forces (or encourages) you to run server software + * as root, please complain to them before and after uncommenting this line, + * since it is a very bad idea. + * Please note that NO SUPPORT will be given if you uncomment the following line. + */ +//#define I_AM_AWARE_OF_THE_RISK_AND_STILL_WANT_TO_RUN_HERCULES_AS_ROOT +// And don't complain to us if the XYZ plugin you installed wiped your hard disk, or worse. +// Note: This feature is deprecated, and should not be used. + /// Called when a terminate signal is received. void (*shutdown_callback)(void) = NULL; @@ -74,7 +100,8 @@ struct core_interface *core = &core_s; #ifndef POSIX #define compat_signal(signo, func) signal((signo), (func)) #else -sigfunc *compat_signal(int signo, sigfunc *func) { +sigfunc *compat_signal(int signo, sigfunc *func) +{ struct sigaction sact, oact; sact.sa_handler = func; @@ -95,7 +122,8 @@ sigfunc *compat_signal(int signo, sigfunc *func) { * CORE : Console events for Windows *--------------------------------------*/ #ifdef _WIN32 -static BOOL WINAPI console_handler(DWORD c_event) { +static BOOL WINAPI console_handler(DWORD c_event) +{ switch(c_event) { case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT: @@ -111,7 +139,8 @@ static BOOL WINAPI console_handler(DWORD c_event) { return TRUE; } -static void cevents_init(void) { +static void cevents_init(void) +{ if (SetConsoleCtrlHandler(console_handler,TRUE)==FALSE) ShowWarning ("Unable to install the console handler!\n"); } @@ -120,7 +149,8 @@ static void cevents_init(void) { /*====================================== * CORE : Signal Sub Function *--------------------------------------*/ -static void sig_proc(int sn) { +static void sig_proc(int sn) +{ static int is_called = 0; switch (sn) { @@ -153,7 +183,8 @@ static void sig_proc(int sn) { } } -void signals_init (void) { +void signals_init (void) +{ compat_signal(SIGTERM, sig_proc); compat_signal(SIGINT, sig_proc); #ifndef _DEBUG // need unhandled exceptions to debug on Windows @@ -172,14 +203,55 @@ void signals_init (void) { /** * Warns the user if executed as superuser (root) + * + * @retval false if the check didn't pass and the program should be terminated. */ -void usercheck(void) { +bool usercheck(void) +{ +#ifndef _WIN32 if (sysinfo->is_superuser()) { - ShowWarning("You are running Hercules with root privileges, it is not necessary.\n"); + if (!isatty(fileno(stdin))) { +#ifdef BUILDBOT + return true; +#else // BUILDBOT + ShowFatalError("You are running Hercules with root privileges, it is not necessary, nor recommended. " + "Aborting.\n"); + return false; // Don't allow noninteractive execution regardless. +#endif // BUILDBOT + } + ShowError("You are running Hercules with root privileges, it is not necessary, nor recommended.\n"); +#ifdef I_AM_AWARE_OF_THE_RISK_AND_STILL_WANT_TO_RUN_HERCULES_AS_ROOT +#ifndef BUILDBOT +#warning This Hercules build is not eligible to obtain support by the developers. +#warning The setting I_AM_AWARE_OF_THE_RISK_AND_STILL_WANT_TO_RUN_HERCULES_AS_ROOT is deprecated and should not be used. +#endif // BUILDBOT +#else // not I_AM_AWARE_OF_THE_RISK_AND_STILL_WANT_TO_RUN_HERCULES_AS_ROOT + ShowNotice("Execution will be paused for 60 seconds. Press Ctrl-C if you wish to quit.\n"); + ShowNotice("If you want to get rid of this message, please open %s and uncomment, near the top, the line saying:\n" + "\t\"//#define I_AM_AWARE_OF_THE_RISK_AND_STILL_WANT_TO_RUN_HERCULES_AS_ROOT\"\n", __FILE__); + ShowNotice("Note: In a near future, this courtesy notice will go away. " + "Please update your infrastructure not to require root privileges before then.\n"); + ShowWarning("It's recommended that you " CL_WHITE "press CTRL-C now!" CL_RESET "\n"); + { + int i; + for (i = 0; i < 60; i++) { + ShowMessage("\a *"); + HSleep(1); + } + } + ShowMessage("\n"); + ShowNotice("Resuming operations with root privileges. " + CL_RED "If anything breaks, you get to keep the pieces, " + "and the Hercules developers won't be able to help you." + CL_RESET "\n"); +#endif // I_AM_AWARE_OF_THE_RISK_AND_STILL_WANT_TO_RUN_HERCULES_AS_ROOT } +#endif // not _WIN32 + return true; } -void core_defaults(void) { +void core_defaults(void) +{ nullpo_defaults(); #ifndef MINICORE hpm_defaults(); @@ -191,24 +263,34 @@ void core_defaults(void) { malloc_defaults(); showmsg_defaults(); cmdline_defaults(); + des_defaults(); + grfio_defaults(); // Note: grfio is lazily loaded. grfio->init() and grfio->final() are not automatically called. #ifndef MINICORE + mutex_defaults(); libconfig_defaults(); sql_defaults(); timer_defaults(); db_defaults(); socket_defaults(); + rnd_defaults(); + md5_defaults(); + thread_defaults(); #endif } + /** * Returns the source (core or plugin name) for the given command-line argument */ -const char *cmdline_arg_source(struct CmdlineArgData *arg) { +const char *cmdline_arg_source(struct CmdlineArgData *arg) +{ #ifdef MINICORE return "core"; #else // !MINICORE + nullpo_retr(NULL, arg); return HPM->pid2name(arg->pluginID); #endif // MINICORE } + /** * Defines a command line argument. * @@ -220,9 +302,11 @@ const char *cmdline_arg_source(struct CmdlineArgData *arg) { * @param options options associated to the command-line argument. @see enum cmdline_options. * @return the success status. */ -bool cmdline_arg_add(unsigned int pluginID, const char *name, char shortname, CmdlineExecFunc func, const char *help, unsigned int options) { +bool cmdline_arg_add(unsigned int pluginID, const char *name, char shortname, CmdlineExecFunc func, const char *help, unsigned int options) +{ struct CmdlineArgData *data = NULL; + nullpo_retr(false, name); VECTOR_ENSURE(cmdline->args_data, 1, 1); VECTOR_PUSHZEROED(cmdline->args_data); data = &VECTOR_LAST(cmdline->args_data); @@ -230,11 +314,15 @@ bool cmdline_arg_add(unsigned int pluginID, const char *name, char shortname, Cm data->name = aStrdup(name); data->shortname = shortname; data->func = func; - data->help = aStrdup(help); + if (help) + data->help = aStrdup(help); + else + data->help = NULL; data->options = options; return true; } + /** * Help screen to be displayed by '--help'. */ @@ -258,6 +346,7 @@ static CMDLINEARG(help) } return false; } + /** * Info screen to be displayed by '--version'. */ @@ -268,6 +357,7 @@ static CMDLINEARG(version) ShowInfo("Open "CL_WHITE"readme.txt"CL_RESET" for more information.\n"); return false; } + /** * Checks if there is a value available for the current argument * @@ -285,6 +375,7 @@ bool cmdline_arg_next_value(const char *name, int current_arg, int argc) return true; } + /** * Executes the command line arguments handlers. * @@ -306,11 +397,15 @@ bool cmdline_arg_next_value(const char *name, int current_arg, int argc) int cmdline_exec(int argc, char **argv, unsigned int options) { int count = 0, i; + + nullpo_ret(argv); for (i = 1; i < argc; i++) { int j; struct CmdlineArgData *data = NULL; const char *arg = argv[i]; if (arg[0] != '-') { // All arguments must begin with '-' + if ((options&(CMDLINE_OPT_SILENT|CMDLINE_OPT_PREINIT)) != 0) + continue; ShowError("Invalid option '%s'.\n", argv[i]); exit(EXIT_FAILURE); } @@ -348,6 +443,7 @@ int cmdline_exec(int argc, char **argv, unsigned int options) } return count; } + /** * Defines the global command-line arguments. */ @@ -391,10 +487,12 @@ void cmdline_defaults(void) cmdline->arg_next_value = cmdline_arg_next_value; cmdline->arg_source = cmdline_arg_source; } + /*====================================== * CORE : MAINROUTINE *--------------------------------------*/ -int main (int argc, char **argv) { +int main (int argc, char **argv) +{ int retval = EXIT_SUCCESS; {// initialize program arguments char *p1 = SERVER_NAME = argv[0]; @@ -423,7 +521,8 @@ int main (int argc, char **argv) { if (!(showmsg->silent&0x1)) console->display_title(); - usercheck(); + if (!usercheck()) + return EXIT_FAILURE; #ifdef MINICORE // minimalist Core do_init(argc,argv); @@ -432,7 +531,7 @@ int main (int argc, char **argv) { set_server_type(); Sql_Init(); - rathread_init(); + thread->init(); DB->init(); signals_init(); @@ -443,8 +542,7 @@ int main (int argc, char **argv) { timer->init(); /* timer first */ - rnd_init(); - srand((unsigned int)timer->gettick()); + rnd->init(); console->init(); @@ -469,8 +567,9 @@ int main (int argc, char **argv) { timer->final(); sockt->final(); DB->final(); - rathread_final(); + thread->final(); ers_final(); + rnd->final(); #endif cmdline->final(); //sysinfo->final(); Called by iMalloc->final() |