2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2022 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;
95 NGIRCd_Sniffer = false;
97 strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
98 strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
102 /* parse conmmand line */
103 for (i = 1; i < argc; i++) {
105 if (argv[i][0] == '-' && argv[i][1] == '-') {
107 if (strcmp(argv[i], "--config") == 0) {
109 /* Ok, there's an parameter left */
110 strlcpy(NGIRCd_ConfFile, argv[i+1],
111 sizeof(NGIRCd_ConfFile));
116 if (strcmp(argv[i], "--configtest") == 0) {
121 if (strcmp(argv[i], "--debug") == 0) {
126 if (strcmp(argv[i], "--help") == 0) {
128 puts(""); Show_Help( ); puts( "" );
131 if (strcmp(argv[i], "--nodaemon") == 0) {
132 NGIRCd_NoDaemon = true;
133 NGIRCd_NoSyslog = true;
136 if (strcmp(argv[i], "--passive") == 0) {
137 NGIRCd_Passive = true;
141 if (strcmp(argv[i], "--sniffer") == 0) {
142 NGIRCd_Sniffer = true;
147 if (strcmp(argv[i], "--syslog") == 0) {
148 NGIRCd_NoSyslog = false;
152 if (strcmp(argv[i], "--version") == 0) {
157 else if(argv[i][0] == '-' && argv[i][1] != '-') {
159 for (n = 1; n < strlen(argv[i]); n++) {
162 if (argv[i][n] == 'd') {
167 if (argv[i][n] == 'f') {
168 if (!argv[i][n+1] && i+1 < argc) {
169 /* Ok, next character is a blank */
170 strlcpy(NGIRCd_ConfFile, argv[i+1],
171 sizeof(NGIRCd_ConfFile));
173 /* go to the following parameter */
180 if (argv[i][n] == 'h') {
182 puts(""); Show_Help(); puts("");
186 if (argv[i][n] == 'n') {
187 NGIRCd_NoDaemon = true;
188 NGIRCd_NoSyslog = true;
191 if (argv[i][n] == 'p') {
192 NGIRCd_Passive = true;
196 if (argv[i][n] == 's') {
197 NGIRCd_Sniffer = true;
201 if (argv[i][n] == 't') {
206 if (argv[i][n] == 'V') {
211 if (argv[i][n] == 'y') {
212 NGIRCd_NoSyslog = false;
219 "%s: invalid option \"-%c\"!\n",
220 PACKAGE_NAME, argv[i][n]);
222 "Try \"%s --help\" for more information.\n",
230 fprintf(stderr, "%s: invalid option \"%s\"!\n",
231 PACKAGE_NAME, argv[i]);
232 fprintf(stderr, "Try \"%s --help\" for more information.\n",
238 /* Debug level for "VERSION" command */
239 NGIRCd_DebugLevel[0] = '\0';
242 strcpy(NGIRCd_DebugLevel, "1");
245 if (NGIRCd_Sniffer) {
247 strcpy(NGIRCd_DebugLevel, "2");
252 Show_Version(); puts("");
256 while (!NGIRCd_SignalQuit) {
257 /* Initialize global variables */
258 NGIRCd_Start = time(NULL);
259 (void)strftime(NGIRCd_StartStr, 64,
260 "%a %b %d %Y at %H:%M:%S (%Z)",
261 localtime(&NGIRCd_Start));
263 NGIRCd_SignalRestart = false;
264 NGIRCd_SignalQuit = false;
266 Log_Init(!NGIRCd_NoSyslog);
271 /* Initialize the "main program":
272 * chroot environment, user and group ID, ... */
273 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
274 Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
278 if (!io_library_init(CONNECTION_POOL)) {
280 "Fatal: Could not initialize IO routines: %s",
285 if (!Signals_Init()) {
287 "Fatal: Could not set up signal handlers: %s",
297 /* Create protocol and server identification. The syntax
298 * used by ngIRCd in PASS commands and the known "extended
299 * flags" are described in doc/Protocol.txt. */
301 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
302 PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
305 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
307 if (Conf_OperCanMode)
308 strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
310 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
311 PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
313 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
315 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
317 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
319 Channel_InitPredefined();
321 if (Conn_InitListeners() < 1) {
323 "Server isn't listening on a single port!" );
325 "%s exiting due to fatal errors!", PACKAGE_NAME);
347 * Generate ngIRCd "version strings".
349 * The ngIRCd version information is generated once and then stored in the
350 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
351 * usage, for example by the IRC command "VERSION" and the --version command
357 NGIRCd_VersionAddition[0] = '\0';
360 if (NGIRCd_VersionAddition[0])
361 strlcat(NGIRCd_VersionAddition, "+",
362 sizeof NGIRCd_VersionAddition);
363 strlcat(NGIRCd_VersionAddition, "CHARCONV",
364 sizeof NGIRCd_VersionAddition);
367 if (NGIRCd_VersionAddition[0])
368 strlcat(NGIRCd_VersionAddition, "+",
369 sizeof NGIRCd_VersionAddition);
370 strlcat(NGIRCd_VersionAddition, "DEBUG",
371 sizeof NGIRCd_VersionAddition);
374 if (NGIRCd_VersionAddition[0])
375 strlcat(NGIRCd_VersionAddition, "+",
376 sizeof NGIRCd_VersionAddition);
377 strlcat(NGIRCd_VersionAddition, "IDENT",
378 sizeof NGIRCd_VersionAddition);
381 if (NGIRCd_VersionAddition[0])
382 strlcat(NGIRCd_VersionAddition, "+",
383 sizeof(NGIRCd_VersionAddition));
384 strlcat(NGIRCd_VersionAddition, "IPv6",
385 sizeof(NGIRCd_VersionAddition));
388 if (NGIRCd_VersionAddition[0])
389 strlcat(NGIRCd_VersionAddition, "+",
390 sizeof NGIRCd_VersionAddition);
391 strlcat(NGIRCd_VersionAddition, "IRCPLUS",
392 sizeof NGIRCd_VersionAddition);
395 if (NGIRCd_VersionAddition[0])
396 strlcat(NGIRCd_VersionAddition, "+",
397 sizeof NGIRCd_VersionAddition);
398 strlcat(NGIRCd_VersionAddition, "PAM",
399 sizeof NGIRCd_VersionAddition);
402 if (NGIRCd_VersionAddition[0])
403 strlcat(NGIRCd_VersionAddition, "+",
404 sizeof NGIRCd_VersionAddition);
405 strlcat(NGIRCd_VersionAddition, "RFC",
406 sizeof NGIRCd_VersionAddition);
409 if (NGIRCd_VersionAddition[0])
410 strlcat(NGIRCd_VersionAddition, "+",
411 sizeof NGIRCd_VersionAddition);
412 strlcat(NGIRCd_VersionAddition, "SNIFFER",
413 sizeof NGIRCd_VersionAddition);
416 if (NGIRCd_VersionAddition[0])
417 strlcat(NGIRCd_VersionAddition, "+",
418 sizeof NGIRCd_VersionAddition);
419 strlcat(NGIRCd_VersionAddition, "SSL",
420 sizeof NGIRCd_VersionAddition);
423 if (NGIRCd_VersionAddition[0])
424 strlcat(NGIRCd_VersionAddition, "+",
425 sizeof NGIRCd_VersionAddition);
426 strlcat(NGIRCd_VersionAddition, "SYSLOG",
427 sizeof NGIRCd_VersionAddition);
430 if (NGIRCd_VersionAddition[0])
431 strlcat(NGIRCd_VersionAddition, "+",
432 sizeof NGIRCd_VersionAddition);
433 strlcat(NGIRCd_VersionAddition, "TCPWRAP",
434 sizeof NGIRCd_VersionAddition);
437 if (NGIRCd_VersionAddition[0])
438 strlcat(NGIRCd_VersionAddition, "+",
439 sizeof NGIRCd_VersionAddition);
440 strlcat(NGIRCd_VersionAddition, "ZLIB",
441 sizeof NGIRCd_VersionAddition);
443 if (NGIRCd_VersionAddition[0])
444 strlcat(NGIRCd_VersionAddition, "-",
445 sizeof(NGIRCd_VersionAddition));
447 strlcat(NGIRCd_VersionAddition, HOST_CPU,
448 sizeof(NGIRCd_VersionAddition));
449 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
450 strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
451 sizeof(NGIRCd_VersionAddition));
452 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
453 strlcat(NGIRCd_VersionAddition, HOST_OS,
454 sizeof(NGIRCd_VersionAddition));
456 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
457 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
462 * Display copyright and version information of ngIRCd on the console.
467 puts( NGIRCd_Version );
468 puts( "Copyright (c)2001-2022 Alexander Barton (<alex@barton.de>) and Contributors." );
469 puts( "Homepage: <http://ngircd.barton.de/>\n" );
470 puts( "This is free software; see the source for copying conditions. There is NO" );
471 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
476 * Display a short help text on the console.
477 * This help depends on the configuration of the executable and only shows
478 * options that are actually enabled.
484 puts( " -d, --debug log extra debug messages" );
486 puts( " -f, --config <f> use file <f> as configuration file" );
487 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
488 puts( " -p, --passive disable automatic connections to other servers" );
490 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
492 puts( " -t, --configtest read, validate and display configuration; then exit" );
493 puts( " -V, --version output version information and exit" );
495 puts( " -y, --syslog log to syslog even when using -n" );
497 puts( " -h, --help display this help and exit" );
502 * Delete the file containing the process ID (PID).
505 Pidfile_Delete( void )
507 /* Pidfile configured? */
508 if( ! Conf_PidFile[0] ) return;
511 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
514 if( unlink( Conf_PidFile ))
515 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
516 } /* Pidfile_Delete */
520 * Create the file containing the process ID of ngIRCd ("PID file").
522 * @param pid The process ID to be stored in this file.
525 Pidfile_Create(pid_t pid)
531 /* Pidfile configured? */
532 if( ! Conf_PidFile[0] ) return;
535 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
538 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
540 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
544 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
545 if (len < 0 || len >= (int)sizeof pidbuf) {
546 Log(LOG_ERR, "Error converting process ID!");
551 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
552 Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
555 if (close(pidfd) != 0)
556 Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
558 } /* Pidfile_Create */
562 * Redirect stdin, stdout and stderr to appropriate file handles.
564 * @param fd The file handle stdin, stdout and stderr should be redirected to.
567 Setup_FDStreams(int fd)
575 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
576 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
577 } /* Setup_FDStreams */
580 #if !defined(SINGLE_USER_OS)
583 * Get user and group ID of unprivileged "nobody" user.
586 * @param gid Group ID
587 * @return true on success.
590 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
596 * It can return EINVAL instead of EPERM
597 * so, if we are already unprivileged,
598 * use id of current user.
600 if (geteuid() && getuid()) {
607 pwd = getpwnam("nobody");
611 if (!pwd->pw_uid || !pwd->pw_gid)
619 } /* NGIRCd_getNobodyID */
624 #ifdef HAVE_ARC4RANDOM
632 Random_Init_Kern(const char *file)
636 int fd = open(file, O_RDONLY);
638 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
647 * Initialize libc rand(3) number generator
652 if (Random_Init_Kern("/dev/urandom"))
654 if (Random_Init_Kern("/dev/random"))
656 if (Random_Init_Kern("/dev/arandom"))
658 srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
664 * Initialize ngIRCd daemon.
666 * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
667 * foreground (and not as a daemon).
668 * @return true on success.
671 NGIRCd_Init(bool NGIRCd_NoDaemon)
673 static bool initialized;
674 bool chrooted = false;
677 int real_errno, fd = -1;
683 if (!NGIRCd_NoDaemon) {
684 /* open /dev/null before chroot() */
685 fd = open( "/dev/null", O_RDWR);
687 Log(LOG_WARNING, "Could not open /dev/null: %s",
691 /* SSL initialization */
692 if (!ConnSSL_InitLibrary()) {
693 Log(LOG_ERR, "Error during SSL initialization!");
698 if (Conf_Chroot[0]) {
699 if (chdir(Conf_Chroot) != 0) {
700 Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
701 Conf_Chroot, strerror(errno));
705 if (chroot(Conf_Chroot) != 0) {
707 "Can't change root directory to \"%s\": %s!",
708 Conf_Chroot, strerror(errno));
713 "Changed root and working directory to \"%s\".",
718 #if !defined(SINGLE_USER_OS)
723 "ServerUID must not be %s(0), using \"nobody\" instead.",
724 pwd ? pwd->pw_name : "?");
725 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
727 "Could not get user/group ID of user \"nobody\": %s",
728 errno ? strerror(errno) : "not found" );
733 /* Change group ID */
734 if (getgid() != Conf_GID) {
735 if (setgid(Conf_GID) != 0) {
737 grp = getgrgid(Conf_GID);
738 Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
739 grp ? grp->gr_name : "?", Conf_GID,
740 strerror(real_errno));
741 if (real_errno != EPERM)
744 #ifdef HAVE_SETGROUPS
745 if (setgroups(0, NULL) != 0) {
747 Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
749 if (real_errno != EPERM)
754 "Can't drop supplementary group IDs: setgroups(3) missing!");
760 if (getuid() != Conf_UID) {
761 if (setuid(Conf_UID) != 0) {
763 pwd = getpwuid(Conf_UID);
764 Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
765 pwd ? pwd->pw_name : "?", Conf_UID,
766 strerror(real_errno));
767 if (real_errno != EPERM)
774 /* Normally a child process is forked which isn't any longer
775 * connected to ther controlling terminal. Use "--nodaemon"
776 * to disable this "daemon mode" (useful for debugging). */
777 if (!NGIRCd_NoDaemon) {
780 /* "Old" process: exit. */
786 "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
787 PACKAGE_NAME, strerror(errno));
791 /* New child process */
795 setpgrp(0, getpid());
798 Log(LOG_ERR, "Can't change directory to '/': %s!",
801 /* Detach stdin, stdout and stderr */
810 /* Check UID/GID we are running as, can be different from values
811 * configured (e. g. if we were already started with a UID>0. */
815 pwd = getpwuid(Conf_UID);
816 grp = getgrgid(Conf_GID);
818 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
819 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
820 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
823 Log(LOG_INFO, "Running with root directory \"%s\".",
827 Log(LOG_INFO, "Not running with changed root directory.");
829 /* Change working directory to home directory of the user we are
830 * running as (only when running in daemon mode and not in chroot) */
836 if (chdir(pwd->pw_dir) == 0)
838 "Changed working directory to \"%s\" ...",
842 "Can't change working directory to \"%s\": %s!",
843 pwd->pw_dir, strerror(errno));
845 Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);