2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2024 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
17 * The main program, including the C function main() which is called
18 * by the loader of the operating system.
28 #include <sys/types.h>
34 #if defined(DEBUG) && defined(HAVE_MTRACE)
43 #include "sighandlers.h"
48 static void Show_Version PARAMS(( void ));
49 static void Show_Help PARAMS(( void ));
51 static void Pidfile_Create PARAMS(( pid_t pid ));
52 static void Pidfile_Delete PARAMS(( void ));
54 static void Fill_Version PARAMS(( void ));
56 static void Random_Init PARAMS(( void ));
58 static void Setup_FDStreams PARAMS(( int fd ));
60 static bool NGIRCd_Init PARAMS(( bool ));
64 * The main() function of ngIRCd.
66 * Here all starts: this function is called by the operating system loader,
67 * it is the first portion of code executed of ngIRCd.
69 * @param argc The number of arguments passed to ngIRCd on the command line.
70 * @param argv An array containing all the arguments passed to ngIRCd.
71 * @return Global exit code of ngIRCd, zero on success.
74 main(int argc, const char *argv[])
76 bool ok, configtest = false;
77 bool NGIRCd_NoDaemon = false, NGIRCd_NoSyslog = false;
81 #if defined(DEBUG) && defined(HAVE_MTRACE)
82 /* enable GNU libc memory tracing when running in debug mode
83 * and functionality available */
89 NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
90 NGIRCd_Passive = false;
93 NGIRCd_Sniffer = false;
98 /* parse conmmand line */
99 for (i = 1; i < argc; i++) {
101 if (argv[i][0] == '-' && argv[i][1] == '-') {
103 if (strcmp(argv[i], "--config") == 0) {
105 /* Ok, there's an parameter left */
106 strlcpy(NGIRCd_ConfFile, argv[i+1],
107 sizeof(NGIRCd_ConfFile));
112 if (strcmp(argv[i], "--configtest") == 0) {
116 if (strcmp(argv[i], "--debug") == 0) {
120 if (strcmp(argv[i], "--help") == 0) {
122 puts(""); Show_Help( ); puts( "" );
125 if (strcmp(argv[i], "--nodaemon") == 0) {
126 NGIRCd_NoDaemon = true;
127 NGIRCd_NoSyslog = true;
130 if (strcmp(argv[i], "--passive") == 0) {
131 NGIRCd_Passive = true;
135 if (strcmp(argv[i], "--sniffer") == 0) {
136 NGIRCd_Sniffer = true;
141 if (strcmp(argv[i], "--syslog") == 0) {
142 NGIRCd_NoSyslog = false;
146 if (strcmp(argv[i], "--version") == 0) {
151 else if(argv[i][0] == '-' && argv[i][1] != '-') {
153 for (n = 1; n < strlen(argv[i]); n++) {
155 if (argv[i][n] == 'd') {
159 if (argv[i][n] == 'f') {
160 if (!argv[i][n+1] && i+1 < argc) {
161 /* Ok, next character is a blank */
162 strlcpy(NGIRCd_ConfFile, argv[i+1],
163 sizeof(NGIRCd_ConfFile));
165 /* go to the following parameter */
172 if (argv[i][n] == 'h') {
174 puts(""); Show_Help(); puts("");
178 if (argv[i][n] == 'n') {
179 NGIRCd_NoDaemon = true;
180 NGIRCd_NoSyslog = true;
183 if (argv[i][n] == 'p') {
184 NGIRCd_Passive = true;
188 if (argv[i][n] == 's') {
189 NGIRCd_Sniffer = true;
193 if (argv[i][n] == 't') {
198 if (argv[i][n] == 'V') {
203 if (argv[i][n] == 'y') {
204 NGIRCd_NoSyslog = false;
211 "%s: invalid option \"-%c\"!\n",
212 PACKAGE_NAME, argv[i][n]);
214 "Try \"%s --help\" for more information.\n",
222 fprintf(stderr, "%s: invalid option \"%s\"!\n",
223 PACKAGE_NAME, argv[i]);
224 fprintf(stderr, "Try \"%s --help\" for more information.\n",
230 /* Debug level for "VERSION" command */
231 NGIRCd_DebugLevel[0] = '\0';
233 strcpy(NGIRCd_DebugLevel, "1");
235 if (NGIRCd_Sniffer) {
237 strcpy(NGIRCd_DebugLevel, "2");
242 Show_Version(); puts("");
246 while (!NGIRCd_SignalQuit) {
247 /* Initialize global variables */
248 NGIRCd_Start = time(NULL);
249 (void)strftime(NGIRCd_StartStr, 64,
250 "%a %b %d %Y at %H:%M:%S (%Z)",
251 localtime(&NGIRCd_Start));
253 NGIRCd_SignalRestart = false;
254 NGIRCd_SignalQuit = false;
256 Log_Init(!NGIRCd_NoSyslog);
261 /* Initialize the "main program":
262 * chroot environment, user and group ID, ... */
263 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
264 Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
268 if (!io_library_init(CONNECTION_POOL)) {
270 "Fatal: Could not initialize IO routines: %s",
275 if (!Signals_Init()) {
277 "Fatal: Could not set up signal handlers: %s",
287 /* Create protocol and server identification. The syntax
288 * used by ngIRCd in PASS commands and the known "extended
289 * flags" are described in doc/Protocol.txt. */
291 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
292 PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
295 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
297 if (Conf_OperCanMode)
298 strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
300 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
301 PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
303 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
305 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
307 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
309 Channel_InitPredefined();
311 if (Conn_InitListeners() < 1) {
313 "Server isn't listening on a single port!" );
315 "%s exiting due to fatal errors!", PACKAGE_NAME);
337 * Generate ngIRCd "version strings".
339 * The ngIRCd version information is generated once and then stored in the
340 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
341 * usage, for example by the IRC command "VERSION" and the --version command
347 NGIRCd_VersionAddition[0] = '\0';
350 if (NGIRCd_VersionAddition[0])
351 strlcat(NGIRCd_VersionAddition, "+",
352 sizeof NGIRCd_VersionAddition);
353 strlcat(NGIRCd_VersionAddition, "CHARCONV",
354 sizeof NGIRCd_VersionAddition);
357 if (NGIRCd_VersionAddition[0])
358 strlcat(NGIRCd_VersionAddition, "+",
359 sizeof NGIRCd_VersionAddition);
360 strlcat(NGIRCd_VersionAddition, "DEBUG",
361 sizeof NGIRCd_VersionAddition);
364 if (NGIRCd_VersionAddition[0])
365 strlcat(NGIRCd_VersionAddition, "+",
366 sizeof NGIRCd_VersionAddition);
367 strlcat(NGIRCd_VersionAddition, "IDENT",
368 sizeof NGIRCd_VersionAddition);
371 if (NGIRCd_VersionAddition[0])
372 strlcat(NGIRCd_VersionAddition, "+",
373 sizeof(NGIRCd_VersionAddition));
374 strlcat(NGIRCd_VersionAddition, "IPv6",
375 sizeof(NGIRCd_VersionAddition));
378 if (NGIRCd_VersionAddition[0])
379 strlcat(NGIRCd_VersionAddition, "+",
380 sizeof NGIRCd_VersionAddition);
381 strlcat(NGIRCd_VersionAddition, "IRCPLUS",
382 sizeof NGIRCd_VersionAddition);
385 if (NGIRCd_VersionAddition[0])
386 strlcat(NGIRCd_VersionAddition, "+",
387 sizeof NGIRCd_VersionAddition);
388 strlcat(NGIRCd_VersionAddition, "PAM",
389 sizeof NGIRCd_VersionAddition);
392 if (NGIRCd_VersionAddition[0])
393 strlcat(NGIRCd_VersionAddition, "+",
394 sizeof NGIRCd_VersionAddition);
395 strlcat(NGIRCd_VersionAddition, "RFC",
396 sizeof NGIRCd_VersionAddition);
399 if (NGIRCd_VersionAddition[0])
400 strlcat(NGIRCd_VersionAddition, "+",
401 sizeof NGIRCd_VersionAddition);
402 strlcat(NGIRCd_VersionAddition, "SNIFFER",
403 sizeof NGIRCd_VersionAddition);
406 if (NGIRCd_VersionAddition[0])
407 strlcat(NGIRCd_VersionAddition, "+",
408 sizeof NGIRCd_VersionAddition);
409 strlcat(NGIRCd_VersionAddition, "SSL",
410 sizeof NGIRCd_VersionAddition);
413 if (NGIRCd_VersionAddition[0])
414 strlcat(NGIRCd_VersionAddition, "+",
415 sizeof NGIRCd_VersionAddition);
416 strlcat(NGIRCd_VersionAddition, "SYSLOG",
417 sizeof NGIRCd_VersionAddition);
420 if (NGIRCd_VersionAddition[0])
421 strlcat(NGIRCd_VersionAddition, "+",
422 sizeof NGIRCd_VersionAddition);
423 strlcat(NGIRCd_VersionAddition, "TCPWRAP",
424 sizeof NGIRCd_VersionAddition);
427 if (NGIRCd_VersionAddition[0])
428 strlcat(NGIRCd_VersionAddition, "+",
429 sizeof NGIRCd_VersionAddition);
430 strlcat(NGIRCd_VersionAddition, "ZLIB",
431 sizeof NGIRCd_VersionAddition);
433 if (NGIRCd_VersionAddition[0])
434 strlcat(NGIRCd_VersionAddition, "-",
435 sizeof(NGIRCd_VersionAddition));
437 strlcat(NGIRCd_VersionAddition, HOST_CPU,
438 sizeof(NGIRCd_VersionAddition));
439 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
440 strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
441 sizeof(NGIRCd_VersionAddition));
442 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
443 strlcat(NGIRCd_VersionAddition, HOST_OS,
444 sizeof(NGIRCd_VersionAddition));
446 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
447 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
452 * Display copyright and version information of ngIRCd on the console.
457 puts( NGIRCd_Version );
458 puts( "Copyright (c)2001-2024 Alexander Barton (<alex@barton.de>) and Contributors." );
459 puts( "Homepage: <http://ngircd.barton.de/>\n" );
460 puts( "This is free software; see the source for copying conditions. There is NO" );
461 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
466 * Display a short help text on the console.
467 * This help depends on the configuration of the executable and only shows
468 * options that are actually enabled.
473 puts( " -d, --debug log extra debug messages" );
474 puts( " -f, --config <f> use file <f> as configuration file" );
475 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
476 puts( " -p, --passive disable automatic connections to other servers" );
478 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
480 puts( " -t, --configtest read, validate and display configuration; then exit" );
481 puts( " -V, --version output version information and exit" );
483 puts( " -y, --syslog log to syslog even when running in the foreground (-n)" );
485 puts( " -h, --help display this help and exit" );
490 * Delete the file containing the process ID (PID).
493 Pidfile_Delete( void )
495 /* Pidfile configured? */
496 if( ! Conf_PidFile[0] ) return;
498 LogDebug( "Removing PID file (%s) ...", Conf_PidFile );
500 if( unlink( Conf_PidFile ))
501 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
502 } /* Pidfile_Delete */
506 * Create the file containing the process ID of ngIRCd ("PID file").
508 * @param pid The process ID to be stored in this file.
511 Pidfile_Create(pid_t pid)
517 /* Pidfile configured? */
518 if( ! Conf_PidFile[0] ) return;
520 LogDebug( "Creating PID file (%s) ...", Conf_PidFile );
522 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
524 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
528 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
529 if (len < 0 || len >= (int)sizeof pidbuf) {
530 Log(LOG_ERR, "Error converting process ID!");
535 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
536 Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
539 if (close(pidfd) != 0)
540 Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
542 } /* Pidfile_Create */
546 * Redirect stdin, stdout and stderr to appropriate file handles.
548 * @param fd The file handle stdin, stdout and stderr should be redirected to.
551 Setup_FDStreams(int fd)
559 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
560 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
561 } /* Setup_FDStreams */
564 #if !defined(SINGLE_USER_OS)
567 * Get user and group ID of unprivileged "nobody" user.
570 * @param gid Group ID
571 * @return true on success.
574 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
580 * It can return EINVAL instead of EPERM
581 * so, if we are already unprivileged,
582 * use id of current user.
584 if (geteuid() && getuid()) {
591 pwd = getpwnam("nobody");
595 if (!pwd->pw_uid || !pwd->pw_gid)
603 } /* NGIRCd_getNobodyID */
608 #ifdef HAVE_ARC4RANDOM
616 Random_Init_Kern(const char *file)
620 int fd = open(file, O_RDONLY);
622 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
631 * Initialize libc rand(3) number generator
636 if (Random_Init_Kern("/dev/urandom"))
638 if (Random_Init_Kern("/dev/random"))
640 if (Random_Init_Kern("/dev/arandom"))
642 srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
648 * Initialize ngIRCd daemon.
650 * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
651 * foreground (and not as a daemon).
652 * @return true on success.
655 NGIRCd_Init(bool NGIRCd_NoDaemon)
657 static bool initialized;
658 bool chrooted = false;
661 int real_errno, fd = -1;
667 if (!NGIRCd_NoDaemon) {
668 /* open /dev/null before chroot() */
669 fd = open( "/dev/null", O_RDWR);
671 Log(LOG_WARNING, "Could not open /dev/null: %s",
675 /* SSL initialization */
676 if (!ConnSSL_InitLibrary()) {
677 Log(LOG_ERR, "Error during SSL initialization!");
682 if (Conf_Chroot[0]) {
683 if (chdir(Conf_Chroot) != 0) {
684 Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
685 Conf_Chroot, strerror(errno));
689 if (chroot(Conf_Chroot) != 0) {
691 "Can't change root directory to \"%s\": %s!",
692 Conf_Chroot, strerror(errno));
697 "Changed root and working directory to \"%s\".",
702 #if !defined(SINGLE_USER_OS)
707 "ServerUID must not be %s(0), using \"nobody\" instead.",
708 pwd ? pwd->pw_name : "?");
709 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
711 "Could not get user/group ID of user \"nobody\": %s",
712 errno ? strerror(errno) : "not found" );
717 /* Change group ID */
718 if (getgid() != Conf_GID) {
719 if (setgid(Conf_GID) != 0) {
721 grp = getgrgid(Conf_GID);
722 Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
723 grp ? grp->gr_name : "?", Conf_GID,
724 strerror(real_errno));
725 if (real_errno != EPERM && real_errno != EINVAL)
728 #ifdef HAVE_SETGROUPS
729 if (setgroups(0, NULL) != 0) {
731 Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
733 if (real_errno != EPERM)
738 "Can't drop supplementary group IDs: setgroups(3) missing!");
744 if (getuid() != Conf_UID) {
745 if (setuid(Conf_UID) != 0) {
747 pwd = getpwuid(Conf_UID);
748 Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
749 pwd ? pwd->pw_name : "?", Conf_UID,
750 strerror(real_errno));
751 if (real_errno != EPERM && real_errno != EINVAL)
758 /* Normally a child process is forked which isn't any longer
759 * connected to the controlling terminal. Use "--nodaemon"
760 * to disable this "daemon mode" (useful for debugging). */
761 if (!NGIRCd_NoDaemon) {
764 /* "Old" process: exit. */
770 "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
771 PACKAGE_NAME, strerror(errno));
775 /* New child process */
779 setpgrp(0, getpid());
782 Log(LOG_ERR, "Can't change directory to '/': %s!",
785 /* Detach stdin, stdout and stderr */
794 /* Check UID/GID we are running as, can be different from values
795 * configured (e. g. if we were already started with a UID>0. */
799 pwd = getpwuid(Conf_UID);
800 grp = getgrgid(Conf_GID);
802 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
803 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
804 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
807 Log(LOG_INFO, "Running with root directory \"%s\".",
811 Log(LOG_INFO, "Not running with changed root directory.");
813 /* Change working directory to home directory of the user we are
814 * running as (only when running in daemon mode and not in chroot) */
820 if (chdir(pwd->pw_dir) == 0)
822 "Changed working directory to \"%s\" ...",
826 "Can't change working directory to \"%s\": %s!",
827 pwd->pw_dir, strerror(errno));
829 Log(LOG_ERR, "Can't get user information for UID %d!?", Conf_UID);