]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/ngircd.c
WIP: Implement --name & --motd command line options
[ngircd-alex.git] / src / ngircd / ngircd.c
index 1b20597d8aa26cf7dbd0c2e490786ea910bab8d5..7144b206ef23800d105dd720a45242e243fb9042 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
+ * Copyright (c)2001-2020 Alexander Barton (alex@barton.de) and Contributors.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -9,6 +9,7 @@
  * Please read the file COPYING, README and AUTHORS for more information.
  */
 
+#define GLOBAL_INIT
 #include "portab.h"
 
 /**
@@ -72,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;
@@ -105,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 */
@@ -125,7 +127,21 @@ main(int argc, const char *argv[])
                        if (strcmp(argv[i], "--help") == 0) {
                                Show_Version();
                                puts(""); Show_Help( ); puts( "" );
-                               exit(1);
+                               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;
@@ -143,7 +159,7 @@ main(int argc, const char *argv[])
 #endif
                        if (strcmp(argv[i], "--version") == 0) {
                                Show_Version();
-                               exit(1);
+                               exit(0);
                        }
                }
                else if(argv[i][0] == '-' && argv[i][1] != '-') {
@@ -175,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;
@@ -200,21 +233,23 @@ main(int argc, const char *argv[])
                                }
 
                                if (!ok) {
-                                       printf("%s: invalid option \"-%c\"!\n",
-                                              PACKAGE_NAME, argv[i][n]);
-                                       printf("Try \"%s --help\" for more information.\n",
-                                              PACKAGE_NAME);
-                                       exit(1);
+                                       fprintf(stderr,
+                                               "%s: invalid option \"-%c\"!\n",
+                                               PACKAGE_NAME, argv[i][n]);
+                                       fprintf(stderr,
+                                               "Try \"%s --help\" for more information.\n",
+                                               PACKAGE_NAME);
+                                       exit(2);
                                }
                        }
 
                }
                if (!ok) {
-                       printf("%s: invalid option \"%s\"!\n",
-                              PACKAGE_NAME, argv[i]);
-                       printf("Try \"%s --help\" for more information.\n",
-                              PACKAGE_NAME);
-                       exit(1);
+                       fprintf(stderr, "%s: invalid option \"%s\"!\n",
+                               PACKAGE_NAME, argv[i]);
+                       fprintf(stderr, "Try \"%s --help\" for more information.\n",
+                               PACKAGE_NAME);
+                       exit(2);
                }
        }
 
@@ -233,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) {
@@ -246,26 +281,18 @@ main(int argc, const char *argv[])
                NGIRCd_SignalRestart = false;
                NGIRCd_SignalQuit = false;
 
-               /* Initialize modules, part I */
                Log_Init(!NGIRCd_NoDaemon);
                Random_Init();
-               Conf_Init();
+               Conf_Init(name_ptr, motd_ptr);
                Log_ReInit();
 
-               /* Initialize the "main program": chroot environment, user and
-                * group ID, ... */
+               /* Initialize the "main program":
+                * chroot environment, user and group ID, ... */
                if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
                        Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
                        exit(1);
                }
 
-               /* Initialize modules, part II: these functions are eventually
-                * called with already dropped privileges ... */
-               Channel_Init();
-               Client_Init();
-               Conn_Init();
-               Class_Init();
-
                if (!io_library_init(CONNECTION_POOL)) {
                        Log(LOG_ALERT,
                            "Fatal: Could not initialize IO routines: %s",
@@ -280,6 +307,11 @@ main(int argc, const char *argv[])
                        exit(1);
                }
 
+               Channel_Init();
+               Conn_Init();
+               Class_Init();
+               Client_Init();
+
                /* Create protocol and server identification. The syntax
                 * used by ngIRCd in PASS commands and the known "extended
                 * flags" are described in doc/Protocol.txt. */
@@ -451,7 +483,7 @@ static void
 Show_Version( void )
 {
        puts( NGIRCd_Version );
-       puts( "Copyright (c)2001-2014 Alexander Barton (<alex@barton.de>) and Contributors." );
+       puts( "Copyright (c)2001-2020 Alexander Barton (<alex@barton.de>) and Contributors." );
        puts( "Homepage: <http://ngircd.barton.de/>\n" );
        puts( "This is free software; see the source for copying conditions. There is NO" );
        puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
@@ -470,6 +502,8 @@ Show_Help( void )
        puts( "  -d, --debug        log extra debug messages" );
 #endif
        puts( "  -f, --config <f>   use file <f> as configuration file" );
+       puts( "  -i, --name <id>    set the server ID (\"name\") to <id>" );
+       puts( "  -m, --motd <txt>   set the MOTD phrase to <txt>");
        puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
        puts( "  -p, --passive      disable automatic connections to other servers" );
 #ifdef SNIFFER
@@ -530,7 +564,7 @@ Pidfile_Create(pid_t pid)
                close(pidfd);
                return;
        }
-       
+
        if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
                Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
                    strerror(errno));
@@ -721,9 +755,10 @@ NGIRCd_Init(bool NGIRCd_NoDaemon)
                        Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
                            grp ? grp->gr_name : "?", Conf_GID,
                            strerror(real_errno));
-                       if (real_errno != EPERM) 
+                       if (real_errno != EPERM)
                                goto out;
                }
+#ifdef HAVE_SETGROUPS
                if (setgroups(0, NULL) != 0) {
                        real_errno = errno;
                        Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
@@ -731,6 +766,10 @@ NGIRCd_Init(bool NGIRCd_NoDaemon)
                        if (real_errno != EPERM)
                                goto out;
                }
+#else
+               Log(LOG_WARNING,
+                   "Can't drop supplementary group IDs: setgroups(3) missing!");
+#endif
        }
 #endif