2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2023 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;
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;
95 strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
96 strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
100 /* parse conmmand line */
101 for (i = 1; i < argc; i++) {
103 if (argv[i][0] == '-' && argv[i][1] == '-') {
105 if (strcmp(argv[i], "--config") == 0) {
107 /* Ok, there's an parameter left */
108 strlcpy(NGIRCd_ConfFile, argv[i+1],
109 sizeof(NGIRCd_ConfFile));
114 if (strcmp(argv[i], "--configtest") == 0) {
118 if (strcmp(argv[i], "--debug") == 0) {
122 if (strcmp(argv[i], "--help") == 0) {
124 puts(""); Show_Help( ); puts( "" );
127 if (strcmp(argv[i], "--nodaemon") == 0) {
128 NGIRCd_NoDaemon = true;
131 if (strcmp(argv[i], "--passive") == 0) {
132 NGIRCd_Passive = true;
136 if (strcmp(argv[i], "--sniffer") == 0) {
137 NGIRCd_Sniffer = true;
141 if (strcmp(argv[i], "--version") == 0) {
146 else if(argv[i][0] == '-' && argv[i][1] != '-') {
148 for (n = 1; n < strlen(argv[i]); n++) {
150 if (argv[i][n] == 'd') {
154 if (argv[i][n] == 'f') {
155 if (!argv[i][n+1] && i+1 < argc) {
156 /* Ok, next character is a blank */
157 strlcpy(NGIRCd_ConfFile, argv[i+1],
158 sizeof(NGIRCd_ConfFile));
160 /* go to the following parameter */
167 if (argv[i][n] == 'h') {
169 puts(""); Show_Help(); puts("");
173 if (argv[i][n] == 'n') {
174 NGIRCd_NoDaemon = true;
177 if (argv[i][n] == 'p') {
178 NGIRCd_Passive = true;
182 if (argv[i][n] == 's') {
183 NGIRCd_Sniffer = true;
187 if (argv[i][n] == 't') {
192 if (argv[i][n] == 'V') {
199 "%s: invalid option \"-%c\"!\n",
200 PACKAGE_NAME, argv[i][n]);
202 "Try \"%s --help\" for more information.\n",
210 fprintf(stderr, "%s: invalid option \"%s\"!\n",
211 PACKAGE_NAME, argv[i]);
212 fprintf(stderr, "Try \"%s --help\" for more information.\n",
218 /* Debug level for "VERSION" command */
219 NGIRCd_DebugLevel[0] = '\0';
221 strcpy(NGIRCd_DebugLevel, "1");
223 if (NGIRCd_Sniffer) {
225 strcpy(NGIRCd_DebugLevel, "2");
230 Show_Version(); puts("");
234 while (!NGIRCd_SignalQuit) {
235 /* Initialize global variables */
236 NGIRCd_Start = time(NULL);
237 (void)strftime(NGIRCd_StartStr, 64,
238 "%a %b %d %Y at %H:%M:%S (%Z)",
239 localtime(&NGIRCd_Start));
241 NGIRCd_SignalRestart = false;
242 NGIRCd_SignalQuit = false;
244 Log_Init(!NGIRCd_NoDaemon);
249 /* Initialize the "main program":
250 * chroot environment, user and group ID, ... */
251 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
252 Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
256 if (!io_library_init(CONNECTION_POOL)) {
258 "Fatal: Could not initialize IO routines: %s",
263 if (!Signals_Init()) {
265 "Fatal: Could not set up signal handlers: %s",
275 /* Create protocol and server identification. The syntax
276 * used by ngIRCd in PASS commands and the known "extended
277 * flags" are described in doc/Protocol.txt. */
279 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
280 PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
283 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
285 if (Conf_OperCanMode)
286 strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
288 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
289 PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
291 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
293 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
295 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
297 Channel_InitPredefined();
299 if (Conn_InitListeners() < 1) {
301 "Server isn't listening on a single port!" );
303 "%s exiting due to fatal errors!", PACKAGE_NAME);
325 * Generate ngIRCd "version strings".
327 * The ngIRCd version information is generated once and then stored in the
328 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
329 * usage, for example by the IRC command "VERSION" and the --version command
335 NGIRCd_VersionAddition[0] = '\0';
338 if (NGIRCd_VersionAddition[0])
339 strlcat(NGIRCd_VersionAddition, "+",
340 sizeof NGIRCd_VersionAddition);
341 strlcat(NGIRCd_VersionAddition, "CHARCONV",
342 sizeof NGIRCd_VersionAddition);
345 if (NGIRCd_VersionAddition[0])
346 strlcat(NGIRCd_VersionAddition, "+",
347 sizeof NGIRCd_VersionAddition);
348 strlcat(NGIRCd_VersionAddition, "DEBUG",
349 sizeof NGIRCd_VersionAddition);
352 if (NGIRCd_VersionAddition[0])
353 strlcat(NGIRCd_VersionAddition, "+",
354 sizeof NGIRCd_VersionAddition);
355 strlcat(NGIRCd_VersionAddition, "IDENT",
356 sizeof NGIRCd_VersionAddition);
359 if (NGIRCd_VersionAddition[0])
360 strlcat(NGIRCd_VersionAddition, "+",
361 sizeof(NGIRCd_VersionAddition));
362 strlcat(NGIRCd_VersionAddition, "IPv6",
363 sizeof(NGIRCd_VersionAddition));
366 if (NGIRCd_VersionAddition[0])
367 strlcat(NGIRCd_VersionAddition, "+",
368 sizeof NGIRCd_VersionAddition);
369 strlcat(NGIRCd_VersionAddition, "IRCPLUS",
370 sizeof NGIRCd_VersionAddition);
373 if (NGIRCd_VersionAddition[0])
374 strlcat(NGIRCd_VersionAddition, "+",
375 sizeof NGIRCd_VersionAddition);
376 strlcat(NGIRCd_VersionAddition, "PAM",
377 sizeof NGIRCd_VersionAddition);
380 if (NGIRCd_VersionAddition[0])
381 strlcat(NGIRCd_VersionAddition, "+",
382 sizeof NGIRCd_VersionAddition);
383 strlcat(NGIRCd_VersionAddition, "RFC",
384 sizeof NGIRCd_VersionAddition);
387 if (NGIRCd_VersionAddition[0])
388 strlcat(NGIRCd_VersionAddition, "+",
389 sizeof NGIRCd_VersionAddition);
390 strlcat(NGIRCd_VersionAddition, "SNIFFER",
391 sizeof NGIRCd_VersionAddition);
394 if (NGIRCd_VersionAddition[0])
395 strlcat(NGIRCd_VersionAddition, "+",
396 sizeof NGIRCd_VersionAddition);
397 strlcat(NGIRCd_VersionAddition, "SSL",
398 sizeof NGIRCd_VersionAddition);
401 if (NGIRCd_VersionAddition[0])
402 strlcat(NGIRCd_VersionAddition, "+",
403 sizeof NGIRCd_VersionAddition);
404 strlcat(NGIRCd_VersionAddition, "SYSLOG",
405 sizeof NGIRCd_VersionAddition);
408 if (NGIRCd_VersionAddition[0])
409 strlcat(NGIRCd_VersionAddition, "+",
410 sizeof NGIRCd_VersionAddition);
411 strlcat(NGIRCd_VersionAddition, "TCPWRAP",
412 sizeof NGIRCd_VersionAddition);
415 if (NGIRCd_VersionAddition[0])
416 strlcat(NGIRCd_VersionAddition, "+",
417 sizeof NGIRCd_VersionAddition);
418 strlcat(NGIRCd_VersionAddition, "ZLIB",
419 sizeof NGIRCd_VersionAddition);
421 if (NGIRCd_VersionAddition[0])
422 strlcat(NGIRCd_VersionAddition, "-",
423 sizeof(NGIRCd_VersionAddition));
425 strlcat(NGIRCd_VersionAddition, HOST_CPU,
426 sizeof(NGIRCd_VersionAddition));
427 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
428 strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
429 sizeof(NGIRCd_VersionAddition));
430 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
431 strlcat(NGIRCd_VersionAddition, HOST_OS,
432 sizeof(NGIRCd_VersionAddition));
434 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
435 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
440 * Display copyright and version information of ngIRCd on the console.
445 puts( NGIRCd_Version );
446 puts( "Copyright (c)2001-2023 Alexander Barton (<alex@barton.de>) and Contributors." );
447 puts( "Homepage: <http://ngircd.barton.de/>\n" );
448 puts( "This is free software; see the source for copying conditions. There is NO" );
449 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
454 * Display a short help text on the console.
455 * This help depends on the configuration of the executable and only shows
456 * options that are actually enabled.
461 puts( " -d, --debug log extra debug messages" );
462 puts( " -f, --config <f> use file <f> as configuration file" );
463 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
464 puts( " -p, --passive disable automatic connections to other servers" );
466 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
468 puts( " -t, --configtest read, validate and display configuration; then exit" );
469 puts( " -V, --version output version information and exit" );
470 puts( " -h, --help display this help and exit" );
475 * Delete the file containing the process ID (PID).
478 Pidfile_Delete( void )
480 /* Pidfile configured? */
481 if( ! Conf_PidFile[0] ) return;
483 LogDebug( "Removing PID file (%s) ...", Conf_PidFile );
485 if( unlink( Conf_PidFile ))
486 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
487 } /* Pidfile_Delete */
491 * Create the file containing the process ID of ngIRCd ("PID file").
493 * @param pid The process ID to be stored in this file.
496 Pidfile_Create(pid_t pid)
502 /* Pidfile configured? */
503 if( ! Conf_PidFile[0] ) return;
505 LogDebug( "Creating PID file (%s) ...", Conf_PidFile );
507 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
509 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
513 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
514 if (len < 0 || len >= (int)sizeof pidbuf) {
515 Log(LOG_ERR, "Error converting process ID!");
520 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
521 Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
524 if (close(pidfd) != 0)
525 Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
527 } /* Pidfile_Create */
531 * Redirect stdin, stdout and stderr to appropriate file handles.
533 * @param fd The file handle stdin, stdout and stderr should be redirected to.
536 Setup_FDStreams(int fd)
544 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
545 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
546 } /* Setup_FDStreams */
549 #if !defined(SINGLE_USER_OS)
552 * Get user and group ID of unprivileged "nobody" user.
555 * @param gid Group ID
556 * @return true on success.
559 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
565 * It can return EINVAL instead of EPERM
566 * so, if we are already unprivileged,
567 * use id of current user.
569 if (geteuid() && getuid()) {
576 pwd = getpwnam("nobody");
580 if (!pwd->pw_uid || !pwd->pw_gid)
588 } /* NGIRCd_getNobodyID */
593 #ifdef HAVE_ARC4RANDOM
601 Random_Init_Kern(const char *file)
605 int fd = open(file, O_RDONLY);
607 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
616 * Initialize libc rand(3) number generator
621 if (Random_Init_Kern("/dev/urandom"))
623 if (Random_Init_Kern("/dev/random"))
625 if (Random_Init_Kern("/dev/arandom"))
627 srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
633 * Initialize ngIRCd daemon.
635 * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
636 * foreground (and not as a daemon).
637 * @return true on success.
640 NGIRCd_Init(bool NGIRCd_NoDaemon)
642 static bool initialized;
643 bool chrooted = false;
646 int real_errno, fd = -1;
652 if (!NGIRCd_NoDaemon) {
653 /* open /dev/null before chroot() */
654 fd = open( "/dev/null", O_RDWR);
656 Log(LOG_WARNING, "Could not open /dev/null: %s",
660 /* SSL initialization */
661 if (!ConnSSL_InitLibrary()) {
662 Log(LOG_ERR, "Error during SSL initialization!");
667 if (Conf_Chroot[0]) {
668 if (chdir(Conf_Chroot) != 0) {
669 Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
670 Conf_Chroot, strerror(errno));
674 if (chroot(Conf_Chroot) != 0) {
676 "Can't change root directory to \"%s\": %s!",
677 Conf_Chroot, strerror(errno));
682 "Changed root and working directory to \"%s\".",
687 #if !defined(SINGLE_USER_OS)
692 "ServerUID must not be %s(0), using \"nobody\" instead.",
693 pwd ? pwd->pw_name : "?");
694 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
696 "Could not get user/group ID of user \"nobody\": %s",
697 errno ? strerror(errno) : "not found" );
702 /* Change group ID */
703 if (getgid() != Conf_GID) {
704 if (setgid(Conf_GID) != 0) {
706 grp = getgrgid(Conf_GID);
707 Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
708 grp ? grp->gr_name : "?", Conf_GID,
709 strerror(real_errno));
710 if (real_errno != EPERM)
713 #ifdef HAVE_SETGROUPS
714 if (setgroups(0, NULL) != 0) {
716 Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
718 if (real_errno != EPERM)
723 "Can't drop supplementary group IDs: setgroups(3) missing!");
729 if (getuid() != Conf_UID) {
730 if (setuid(Conf_UID) != 0) {
732 pwd = getpwuid(Conf_UID);
733 Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
734 pwd ? pwd->pw_name : "?", Conf_UID,
735 strerror(real_errno));
736 if (real_errno != EPERM)
743 /* Normally a child process is forked which isn't any longer
744 * connected to ther controlling terminal. Use "--nodaemon"
745 * to disable this "daemon mode" (useful for debugging). */
746 if (!NGIRCd_NoDaemon) {
749 /* "Old" process: exit. */
755 "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
756 PACKAGE_NAME, strerror(errno));
760 /* New child process */
764 setpgrp(0, getpid());
767 Log(LOG_ERR, "Can't change directory to '/': %s!",
770 /* Detach stdin, stdout and stderr */
779 /* Check UID/GID we are running as, can be different from values
780 * configured (e. g. if we were already started with a UID>0. */
784 pwd = getpwuid(Conf_UID);
785 grp = getgrgid(Conf_GID);
787 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
788 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
789 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
792 Log(LOG_INFO, "Running with root directory \"%s\".",
796 Log(LOG_INFO, "Not running with changed root directory.");
798 /* Change working directory to home directory of the user we are
799 * running as (only when running in daemon mode and not in chroot) */
805 if (chdir(pwd->pw_dir) == 0)
807 "Changed working directory to \"%s\" ...",
811 "Can't change working directory to \"%s\": %s!",
812 pwd->pw_dir, strerror(errno));
814 Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);