From 3f16fb660407428cfa5d5e8b7f04380c66055328 Mon Sep 17 00:00:00 2001 From: Alexander Barton Date: Thu, 2 May 2019 00:00:25 +0200 Subject: [PATCH] WIP: Implement --name & --motd command line options --- src/ngircd/conf.c | 51 ++++++++++++++++++++++++++++++++++----------- src/ngircd/conf.h | 4 ++-- src/ngircd/ngircd.c | 40 ++++++++++++++++++++++++++++++++--- 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/src/ngircd/conf.c b/src/ngircd/conf.c index 1c5ba624..5c8514fc 100644 --- a/src/ngircd/conf.c +++ b/src/ngircd/conf.c @@ -51,8 +51,11 @@ static char Conf_MotdFile[FNAME_LEN]; static char Conf_HelpFile[FNAME_LEN]; static char Conf_IncludeDir[FNAME_LEN]; -static void Set_Defaults PARAMS(( bool InitServers )); -static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting )); +static void Set_Defaults PARAMS(( bool InitServers, const char *ServerName, + const char *MotdPhrase )); +static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting, + const char *ServerName, + const char *MotdPhrase )); static void Read_Config_File PARAMS(( const char *File, FILE *fd )); static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash )); @@ -238,11 +241,14 @@ ports_parse(array *a, const char *File, int Line, char *Arg) /** * Initialize configuration module. + * + * @param ServerName Name of the server (when available) or NULL. + * @param MotdPhrase MOTD phrase (when available) or NULL. */ GLOBAL void -Conf_Init( void ) +Conf_Init(const char *ServerName, const char *MotdPhrase) { - Read_Config(false, true); + Read_Config(false, true, ServerName, MotdPhrase); Validate_Config(false, false); } @@ -254,7 +260,7 @@ Conf_Init( void ) GLOBAL bool Conf_Rehash( void ) { - if (!Read_Config(false, false)) + if (!Read_Config(false, false, NULL, NULL)) return false; Validate_Config(false, true); @@ -320,11 +326,14 @@ opers_puts(void) * This function waits for a keypress of the user when stdin/stdout are valid * tty's ("you can read our nice message and we can read in your keypress"). * + * @param ServerName Name of the server (when available) or NULL. + * @param MotdPhrase MOTD phrase (when available) or NULL. + * @return 0 on success, 1 on failure(s); therefore the result code can * directly be used by exit() when running "ngircd --configtest". */ GLOBAL int -Conf_Test( void ) +Conf_Test(const char *ServerName, const char *MotdPhrase) { struct passwd *pwd; struct group *grp; @@ -335,7 +344,7 @@ Conf_Test( void ) Use_Log = false; - if (!Read_Config(true, true)) + if (!Read_Config(true, true, ServerName, MotdPhrase)) return 1; config_valid = Validate_Config(true, false); @@ -732,15 +741,20 @@ Conf_NickIsBlocked(const char *Nick) /** * Initialize configuration settings with their default values. + * + * @param InirServers Initialize the server structure? + * @param ServerName Name of the server (when available) or NULL. + * @param MotdPhrase MOTD phrase (when available) or NULL. */ static void -Set_Defaults(bool InitServers) +Set_Defaults(bool InitServers, const char *ServerName, const char *MotdPhrase) { int i; char random[RANDOM_SALT_LEN + 1]; /* Global */ - strcpy(Conf_ServerName, ""); + strlcpy(Conf_ServerName, ServerName ? ServerName : "", + sizeof(Conf_ServerName)); strcpy(Conf_ServerAdmin1, ""); strcpy(Conf_ServerAdmin2, ""); strcpy(Conf_ServerAdminMail, ""); @@ -751,6 +765,10 @@ Set_Defaults(bool InitServers) Conf_ListenAddress = NULL; array_free(&Conf_ListenPorts); array_free(&Conf_Motd); + if (MotdPhrase) { + array_copyb(&Conf_Motd, MotdPhrase, strlen(MotdPhrase) + 1); + Using_MotdFile = false; + } array_free(&Conf_Helptext); strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile)); strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile)); @@ -890,11 +908,14 @@ Read_TextFile(const char *Filename, const char *Name, array *Destination) * can result in ngIRCd terminating! * * @param IsStarting Flag indicating if ngIRCd is starting or not. + * @param ServerName Name of the server (when available) or NULL. + * @param MotdPhrase MOTD phrase (when available) or NULL. * @returns true when the configuration file has been read * successfully; false otherwise. */ static bool -Read_Config(bool TestOnly, bool IsStarting) +Read_Config(bool TestOnly, bool IsStarting, const char *ServerName, + const char *MotdPhrase) { const UINT16 defaultport = 6667; char *ptr, file[FNAME_LEN]; @@ -918,7 +939,7 @@ Read_Config(bool TestOnly, bool IsStarting) } opers_free(); - Set_Defaults(IsStarting); + Set_Defaults(IsStarting, ServerName, MotdPhrase); if (TestOnly) Config_Error(LOG_INFO, @@ -1312,6 +1333,12 @@ Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg ) assert(Arg != NULL); if (strcasecmp(Var, "Name") == 0) { + if (Conf_ServerName[0]) { + Config_Error(LOG_WARNING, + "%s, line %d: Server name already set, \"%s\" ignored!", + File, Line, Var); + return; + } len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName)); if (len >= sizeof(Conf_ServerName)) Config_Error_TooLong(File, Line, Var); @@ -2081,7 +2108,7 @@ Validate_Config(bool Configtest, bool Rehash) /* No server name configured! */ config_valid = false; Config_Error(LOG_ALERT, - "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!", + "No (valid) server name configured in \"%s\" or on the command line!", NGIRCd_ConfFile); if (!Configtest && !Rehash) { Config_Error(LOG_ALERT, diff --git a/src/ngircd/conf.h b/src/ngircd/conf.h index 0d2965c1..6af70821 100644 --- a/src/ngircd/conf.h +++ b/src/ngircd/conf.h @@ -257,9 +257,9 @@ GLOBAL int Conf_SyslogFacility; #endif -GLOBAL void Conf_Init PARAMS((void)); +GLOBAL void Conf_Init PARAMS((const char *ServerName, const char *MotdPhrase)); GLOBAL bool Conf_Rehash PARAMS((void)); -GLOBAL int Conf_Test PARAMS((void)); +GLOBAL int Conf_Test PARAMS((const char *ServerName, const char *MotdPhrase)); GLOBAL void Conf_UnsetServer PARAMS(( CONN_ID Idx )); GLOBAL bool Conf_SetServer PARAMS(( int ConfServer, CONN_ID Idx )); diff --git a/src/ngircd/ngircd.c b/src/ngircd/ngircd.c index 41255ca0..7144b206 100644 --- a/src/ngircd/ngircd.c +++ b/src/ngircd/ngircd.c @@ -73,6 +73,7 @@ static bool NGIRCd_Init PARAMS(( bool )); GLOBAL int main(int argc, const char *argv[]) { + const char *name_ptr = NULL, *motd_ptr = NULL; bool ok, configtest = false; bool NGIRCd_NoDaemon = false; int i; @@ -106,7 +107,7 @@ main(int argc, const char *argv[]) /* long option */ if (strcmp(argv[i], "--config") == 0) { if (i + 1 < argc) { - /* Ok, there's an parameter left */ + /* Ok, there's a parameter left */ strlcpy(NGIRCd_ConfFile, argv[i+1], sizeof(NGIRCd_ConfFile)); /* next parameter */ @@ -128,6 +129,20 @@ main(int argc, const char *argv[]) puts(""); Show_Help( ); puts( "" ); exit(0); } + if (strcmp(argv[i], "--motd") == 0) { + if (i + 1 < argc) { + /* Next parameter is MOTD phrase */ + motd_ptr = argv[i +1]; + i++; ok = true; + } + } + if (strcmp(argv[i], "--name") == 0) { + if (i + 1 < argc) { + /* Next parameter is server name */ + name_ptr = argv[i +1]; + i++; ok = true; + } + } if (strcmp(argv[i], "--nodaemon") == 0) { NGIRCd_NoDaemon = true; ok = true; @@ -176,10 +191,27 @@ main(int argc, const char *argv[]) exit(1); } + if (argv[i][n] == 'i') { + if (!argv[i][n+1] && i+1 < argc) { + name_ptr = argv[i +1]; + i++; n = strlen(argv[i]); + ok = true; + } + } + + if (argv[i][n] == 'm') { + if (!argv[i][n+1] && i+1 < argc) { + motd_ptr = argv[i +1]; + i++; n = strlen(argv[i]); + ok = true; + } + } + if (argv[i][n] == 'n') { NGIRCd_NoDaemon = true; ok = true; } + if (argv[i][n] == 'p') { NGIRCd_Passive = true; ok = true; @@ -236,7 +268,7 @@ main(int argc, const char *argv[]) if (configtest) { Show_Version(); puts(""); - exit(Conf_Test()); + exit(Conf_Test(name_ptr, motd_ptr)); } while (!NGIRCd_SignalQuit) { @@ -251,7 +283,7 @@ main(int argc, const char *argv[]) Log_Init(!NGIRCd_NoDaemon); Random_Init(); - Conf_Init(); + Conf_Init(name_ptr, motd_ptr); Log_ReInit(); /* Initialize the "main program": @@ -470,6 +502,8 @@ Show_Help( void ) puts( " -d, --debug log extra debug messages" ); #endif puts( " -f, --config use file as configuration file" ); + puts( " -i, --name set the server ID (\"name\") to " ); + puts( " -m, --motd set the MOTD phrase to "); puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" ); puts( " -p, --passive disable automatic connections to other servers" ); #ifdef SNIFFER -- 2.39.2