]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/ngircd.c
Don't abort startup when setgid/setuid() fails with EINVAL
[ngircd-alex.git] / src / ngircd / ngircd.c
index 68b34cb6b5d5e2df3a3501fa13165ca14f198d5b..c2169c4377af04b1f2c11884903ed5b26341a56a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2019 Alexander Barton (alex@barton.de) and Contributors.
+ * Copyright (c)2001-2024 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
  *
  * 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.
  */
 
  * Please read the file COPYING, README and AUTHORS for more information.
  */
 
+#define GLOBAL_INIT
 #include "portab.h"
 
 /**
 #include "portab.h"
 
 /**
@@ -73,7 +74,7 @@ GLOBAL int
 main(int argc, const char *argv[])
 {
        bool ok, configtest = false;
 main(int argc, const char *argv[])
 {
        bool ok, configtest = false;
-       bool NGIRCd_NoDaemon = false;
+       bool NGIRCd_NoDaemon = false, NGIRCd_NoSyslog = false;
        int i;
        size_t n;
 
        int i;
        size_t n;
 
@@ -87,14 +88,10 @@ main(int argc, const char *argv[])
 
        NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
        NGIRCd_Passive = false;
 
        NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
        NGIRCd_Passive = false;
-#ifdef DEBUG
        NGIRCd_Debug = false;
        NGIRCd_Debug = false;
-#endif
 #ifdef SNIFFER
        NGIRCd_Sniffer = false;
 #endif
 #ifdef SNIFFER
        NGIRCd_Sniffer = false;
 #endif
-       strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
-       strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
 
        Fill_Version();
 
 
        Fill_Version();
 
@@ -116,12 +113,10 @@ main(int argc, const char *argv[])
                                configtest = true;
                                ok = true;
                        }
                                configtest = true;
                                ok = true;
                        }
-#ifdef DEBUG
                        if (strcmp(argv[i], "--debug") == 0) {
                                NGIRCd_Debug = true;
                                ok = true;
                        }
                        if (strcmp(argv[i], "--debug") == 0) {
                                NGIRCd_Debug = true;
                                ok = true;
                        }
-#endif
                        if (strcmp(argv[i], "--help") == 0) {
                                Show_Version();
                                puts(""); Show_Help( ); puts( "" );
                        if (strcmp(argv[i], "--help") == 0) {
                                Show_Version();
                                puts(""); Show_Help( ); puts( "" );
@@ -129,6 +124,7 @@ main(int argc, const char *argv[])
                        }
                        if (strcmp(argv[i], "--nodaemon") == 0) {
                                NGIRCd_NoDaemon = true;
                        }
                        if (strcmp(argv[i], "--nodaemon") == 0) {
                                NGIRCd_NoDaemon = true;
+                               NGIRCd_NoSyslog = true;
                                ok = true;
                        }
                        if (strcmp(argv[i], "--passive") == 0) {
                                ok = true;
                        }
                        if (strcmp(argv[i], "--passive") == 0) {
@@ -140,6 +136,12 @@ main(int argc, const char *argv[])
                                NGIRCd_Sniffer = true;
                                ok = true;
                        }
                                NGIRCd_Sniffer = true;
                                ok = true;
                        }
+#endif
+#ifdef SYSLOG
+                       if (strcmp(argv[i], "--syslog") == 0) {
+                               NGIRCd_NoSyslog = false;
+                               ok = true;
+                       }
 #endif
                        if (strcmp(argv[i], "--version") == 0) {
                                Show_Version();
 #endif
                        if (strcmp(argv[i], "--version") == 0) {
                                Show_Version();
@@ -150,12 +152,10 @@ main(int argc, const char *argv[])
                        /* short option */
                        for (n = 1; n < strlen(argv[i]); n++) {
                                ok = false;
                        /* short option */
                        for (n = 1; n < strlen(argv[i]); n++) {
                                ok = false;
-#ifdef DEBUG
                                if (argv[i][n] == 'd') {
                                        NGIRCd_Debug = true;
                                        ok = true;
                                }
                                if (argv[i][n] == 'd') {
                                        NGIRCd_Debug = true;
                                        ok = true;
                                }
-#endif
                                if (argv[i][n] == 'f') {
                                        if (!argv[i][n+1] && i+1 < argc) {
                                                /* Ok, next character is a blank */
                                if (argv[i][n] == 'f') {
                                        if (!argv[i][n+1] && i+1 < argc) {
                                                /* Ok, next character is a blank */
@@ -177,6 +177,7 @@ main(int argc, const char *argv[])
 
                                if (argv[i][n] == 'n') {
                                        NGIRCd_NoDaemon = true;
 
                                if (argv[i][n] == 'n') {
                                        NGIRCd_NoDaemon = true;
+                                       NGIRCd_NoSyslog = true;
                                        ok = true;
                                }
                                if (argv[i][n] == 'p') {
                                        ok = true;
                                }
                                if (argv[i][n] == 'p') {
@@ -198,6 +199,12 @@ main(int argc, const char *argv[])
                                        Show_Version();
                                        exit(1);
                                }
                                        Show_Version();
                                        exit(1);
                                }
+#ifdef SYSLOG
+                               if (argv[i][n] == 'y') {
+                                       NGIRCd_NoSyslog = false;
+                                       ok = true;
+                               }
+#endif
 
                                if (!ok) {
                                        fprintf(stderr,
 
                                if (!ok) {
                                        fprintf(stderr,
@@ -222,10 +229,8 @@ main(int argc, const char *argv[])
 
        /* Debug level for "VERSION" command */
        NGIRCd_DebugLevel[0] = '\0';
 
        /* Debug level for "VERSION" command */
        NGIRCd_DebugLevel[0] = '\0';
-#ifdef DEBUG
        if (NGIRCd_Debug)
                strcpy(NGIRCd_DebugLevel, "1");
        if (NGIRCd_Debug)
                strcpy(NGIRCd_DebugLevel, "1");
-#endif
 #ifdef SNIFFER
        if (NGIRCd_Sniffer) {
                NGIRCd_Debug = true;
 #ifdef SNIFFER
        if (NGIRCd_Sniffer) {
                NGIRCd_Debug = true;
@@ -248,7 +253,7 @@ main(int argc, const char *argv[])
                NGIRCd_SignalRestart = false;
                NGIRCd_SignalQuit = false;
 
                NGIRCd_SignalRestart = false;
                NGIRCd_SignalQuit = false;
 
-               Log_Init(!NGIRCd_NoDaemon);
+               Log_Init(!NGIRCd_NoSyslog);
                Random_Init();
                Conf_Init();
                Log_ReInit();
                Random_Init();
                Conf_Init();
                Log_ReInit();
@@ -450,7 +455,7 @@ static void
 Show_Version( void )
 {
        puts( NGIRCd_Version );
 Show_Version( void )
 {
        puts( NGIRCd_Version );
-       puts( "Copyright (c)2001-2019 Alexander Barton (<alex@barton.de>) and Contributors." );
+       puts( "Copyright (c)2001-2024 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." );
        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." );
@@ -465,9 +470,7 @@ Show_Version( void )
 static void
 Show_Help( void )
 {
 static void
 Show_Help( void )
 {
-#ifdef DEBUG
        puts( "  -d, --debug        log extra debug messages" );
        puts( "  -d, --debug        log extra debug messages" );
-#endif
        puts( "  -f, --config <f>   use file <f> as configuration file" );
        puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
        puts( "  -p, --passive      disable automatic connections to other servers" );
        puts( "  -f, --config <f>   use file <f> as configuration file" );
        puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
        puts( "  -p, --passive      disable automatic connections to other servers" );
@@ -476,6 +479,9 @@ Show_Help( void )
 #endif
        puts( "  -t, --configtest   read, validate and display configuration; then exit" );
        puts( "  -V, --version      output version information and exit" );
 #endif
        puts( "  -t, --configtest   read, validate and display configuration; then exit" );
        puts( "  -V, --version      output version information and exit" );
+#ifdef SYSLOG
+       puts( "  -y, --syslog       log to syslog even when running in the foreground (-n)" );
+#endif
        puts( "  -h, --help         display this help and exit" );
 } /* Show_Help */
 
        puts( "  -h, --help         display this help and exit" );
 } /* Show_Help */
 
@@ -489,9 +495,7 @@ Pidfile_Delete( void )
        /* Pidfile configured? */
        if( ! Conf_PidFile[0] ) return;
 
        /* Pidfile configured? */
        if( ! Conf_PidFile[0] ) return;
 
-#ifdef DEBUG
-       Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
-#endif
+       LogDebug( "Removing PID file (%s) ...", Conf_PidFile );
 
        if( unlink( Conf_PidFile ))
                Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
 
        if( unlink( Conf_PidFile ))
                Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
@@ -513,9 +517,7 @@ Pidfile_Create(pid_t pid)
        /* Pidfile configured? */
        if( ! Conf_PidFile[0] ) return;
 
        /* Pidfile configured? */
        if( ! Conf_PidFile[0] ) return;
 
-#ifdef DEBUG
-       Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
-#endif
+       LogDebug( "Creating PID file (%s) ...", Conf_PidFile );
 
        pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        if ( pidfd < 0 ) {
 
        pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        if ( pidfd < 0 ) {
@@ -720,7 +722,7 @@ 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));
                        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 && real_errno != EINVAL)
                                goto out;
                }
 #ifdef HAVE_SETGROUPS
                                goto out;
                }
 #ifdef HAVE_SETGROUPS
@@ -746,7 +748,7 @@ NGIRCd_Init(bool NGIRCd_NoDaemon)
                        Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
                            pwd ? pwd->pw_name : "?", Conf_UID,
                            strerror(real_errno));
                        Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
                            pwd ? pwd->pw_name : "?", Conf_UID,
                            strerror(real_errno));
-                       if (real_errno != EPERM)
+                       if (real_errno != EPERM && real_errno != EINVAL)
                                goto out;
                }
        }
                                goto out;
                }
        }
@@ -754,7 +756,7 @@ NGIRCd_Init(bool NGIRCd_NoDaemon)
        initialized = true;
 
        /* Normally a child process is forked which isn't any longer
        initialized = true;
 
        /* Normally a child process is forked which isn't any longer
-        * connected to ther controlling terminal. Use "--nodaemon"
+        * connected to the controlling terminal. Use "--nodaemon"
         * to disable this "daemon mode" (useful for debugging). */
        if (!NGIRCd_NoDaemon) {
                pid = fork();
         * to disable this "daemon mode" (useful for debugging). */
        if (!NGIRCd_NoDaemon) {
                pid = fork();
@@ -816,7 +818,7 @@ NGIRCd_Init(bool NGIRCd_NoDaemon)
 
        if (pwd) {
                if (chdir(pwd->pw_dir) == 0)
 
        if (pwd) {
                if (chdir(pwd->pw_dir) == 0)
-                       Log(LOG_DEBUG,
+                       LogDebug(
                            "Changed working directory to \"%s\" ...",
                            pwd->pw_dir);
                else
                            "Changed working directory to \"%s\" ...",
                            pwd->pw_dir);
                else
@@ -824,7 +826,7 @@ NGIRCd_Init(bool NGIRCd_NoDaemon)
                            "Can't change working directory to \"%s\": %s!",
                            pwd->pw_dir, strerror(errno));
        } else
                            "Can't change working directory to \"%s\": %s!",
                            pwd->pw_dir, strerror(errno));
        } else
-               Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);
+               Log(LOG_ERR, "Can't get user information for UID %d!?", Conf_UID);
 
        return true;
  out:
 
        return true;
  out: