2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2020 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.
16 * The main program, including the C function main() which is called
17 * by the loader of the operating system.
27 #include <sys/types.h>
33 #if defined(DEBUG) && defined(HAVE_MTRACE)
42 #include "sighandlers.h"
47 static void Show_Version PARAMS(( void ));
48 static void Show_Help PARAMS(( void ));
50 static void Pidfile_Create PARAMS(( pid_t pid ));
51 static void Pidfile_Delete PARAMS(( void ));
53 static void Fill_Version PARAMS(( void ));
55 static void Random_Init PARAMS(( void ));
57 static void Setup_FDStreams PARAMS(( int fd ));
59 static bool NGIRCd_Init PARAMS(( bool ));
63 * The main() function of ngIRCd.
65 * Here all starts: this function is called by the operating system loader,
66 * it is the first portion of code executed of ngIRCd.
68 * @param argc The number of arguments passed to ngIRCd on the command line.
69 * @param argv An array containing all the arguments passed to ngIRCd.
70 * @return Global exit code of ngIRCd, zero on success.
73 main(int argc, const char *argv[])
75 bool ok, configtest = false;
76 bool NGIRCd_NoDaemon = false;
80 #if defined(DEBUG) && defined(HAVE_MTRACE)
81 /* enable GNU libc memory tracing when running in debug mode
82 * and functionality available */
88 NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
89 NGIRCd_Passive = false;
94 NGIRCd_Sniffer = false;
96 strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
97 strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
101 /* parse conmmand line */
102 for (i = 1; i < argc; i++) {
104 if (argv[i][0] == '-' && argv[i][1] == '-') {
106 if (strcmp(argv[i], "--config") == 0) {
108 /* Ok, there's an parameter left */
109 strlcpy(NGIRCd_ConfFile, argv[i+1],
110 sizeof(NGIRCd_ConfFile));
115 if (strcmp(argv[i], "--configtest") == 0) {
120 if (strcmp(argv[i], "--debug") == 0) {
125 if (strcmp(argv[i], "--help") == 0) {
127 puts(""); Show_Help( ); puts( "" );
130 if (strcmp(argv[i], "--nodaemon") == 0) {
131 NGIRCd_NoDaemon = true;
134 if (strcmp(argv[i], "--passive") == 0) {
135 NGIRCd_Passive = true;
139 if (strcmp(argv[i], "--sniffer") == 0) {
140 NGIRCd_Sniffer = true;
144 if (strcmp(argv[i], "--version") == 0) {
149 else if(argv[i][0] == '-' && argv[i][1] != '-') {
151 for (n = 1; n < strlen(argv[i]); n++) {
154 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;
182 if (argv[i][n] == 'p') {
183 NGIRCd_Passive = true;
187 if (argv[i][n] == 's') {
188 NGIRCd_Sniffer = true;
192 if (argv[i][n] == 't') {
197 if (argv[i][n] == 'V') {
204 "%s: invalid option \"-%c\"!\n",
205 PACKAGE_NAME, argv[i][n]);
207 "Try \"%s --help\" for more information.\n",
215 fprintf(stderr, "%s: invalid option \"%s\"!\n",
216 PACKAGE_NAME, argv[i]);
217 fprintf(stderr, "Try \"%s --help\" for more information.\n",
223 /* Debug level for "VERSION" command */
224 NGIRCd_DebugLevel[0] = '\0';
227 strcpy(NGIRCd_DebugLevel, "1");
230 if (NGIRCd_Sniffer) {
232 strcpy(NGIRCd_DebugLevel, "2");
237 Show_Version(); puts("");
241 while (!NGIRCd_SignalQuit) {
242 /* Initialize global variables */
243 NGIRCd_Start = time(NULL);
244 (void)strftime(NGIRCd_StartStr, 64,
245 "%a %b %d %Y at %H:%M:%S (%Z)",
246 localtime(&NGIRCd_Start));
248 NGIRCd_SignalRestart = false;
249 NGIRCd_SignalQuit = false;
251 Log_Init(!NGIRCd_NoDaemon);
256 /* Initialize the "main program":
257 * chroot environment, user and group ID, ... */
258 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
259 Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
263 if (!io_library_init(CONNECTION_POOL)) {
265 "Fatal: Could not initialize IO routines: %s",
270 if (!Signals_Init()) {
272 "Fatal: Could not set up signal handlers: %s",
282 /* Create protocol and server identification. The syntax
283 * used by ngIRCd in PASS commands and the known "extended
284 * flags" are described in doc/Protocol.txt. */
286 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
287 PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
290 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
292 if (Conf_OperCanMode)
293 strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
295 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
296 PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
298 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
300 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
302 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
304 Channel_InitPredefined();
306 if (Conn_InitListeners() < 1) {
308 "Server isn't listening on a single port!" );
310 "%s exiting due to fatal errors!", PACKAGE_NAME);
332 * Generate ngIRCd "version strings".
334 * The ngIRCd version information is generated once and then stored in the
335 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
336 * usage, for example by the IRC command "VERSION" and the --version command
342 NGIRCd_VersionAddition[0] = '\0';
345 if (NGIRCd_VersionAddition[0])
346 strlcat(NGIRCd_VersionAddition, "+",
347 sizeof NGIRCd_VersionAddition);
348 strlcat(NGIRCd_VersionAddition, "CHARCONV",
349 sizeof NGIRCd_VersionAddition);
352 if (NGIRCd_VersionAddition[0])
353 strlcat(NGIRCd_VersionAddition, "+",
354 sizeof NGIRCd_VersionAddition);
355 strlcat(NGIRCd_VersionAddition, "DEBUG",
356 sizeof NGIRCd_VersionAddition);
359 if (NGIRCd_VersionAddition[0])
360 strlcat(NGIRCd_VersionAddition, "+",
361 sizeof NGIRCd_VersionAddition);
362 strlcat(NGIRCd_VersionAddition, "IDENT",
363 sizeof NGIRCd_VersionAddition);
366 if (NGIRCd_VersionAddition[0])
367 strlcat(NGIRCd_VersionAddition, "+",
368 sizeof(NGIRCd_VersionAddition));
369 strlcat(NGIRCd_VersionAddition, "IPv6",
370 sizeof(NGIRCd_VersionAddition));
373 if (NGIRCd_VersionAddition[0])
374 strlcat(NGIRCd_VersionAddition, "+",
375 sizeof NGIRCd_VersionAddition);
376 strlcat(NGIRCd_VersionAddition, "IRCPLUS",
377 sizeof NGIRCd_VersionAddition);
380 if (NGIRCd_VersionAddition[0])
381 strlcat(NGIRCd_VersionAddition, "+",
382 sizeof NGIRCd_VersionAddition);
383 strlcat(NGIRCd_VersionAddition, "PAM",
384 sizeof NGIRCd_VersionAddition);
387 if (NGIRCd_VersionAddition[0])
388 strlcat(NGIRCd_VersionAddition, "+",
389 sizeof NGIRCd_VersionAddition);
390 strlcat(NGIRCd_VersionAddition, "RFC",
391 sizeof NGIRCd_VersionAddition);
394 if (NGIRCd_VersionAddition[0])
395 strlcat(NGIRCd_VersionAddition, "+",
396 sizeof NGIRCd_VersionAddition);
397 strlcat(NGIRCd_VersionAddition, "SNIFFER",
398 sizeof NGIRCd_VersionAddition);
401 if (NGIRCd_VersionAddition[0])
402 strlcat(NGIRCd_VersionAddition, "+",
403 sizeof NGIRCd_VersionAddition);
404 strlcat(NGIRCd_VersionAddition, "SSL",
405 sizeof NGIRCd_VersionAddition);
408 if (NGIRCd_VersionAddition[0])
409 strlcat(NGIRCd_VersionAddition, "+",
410 sizeof NGIRCd_VersionAddition);
411 strlcat(NGIRCd_VersionAddition, "SYSLOG",
412 sizeof NGIRCd_VersionAddition);
415 if (NGIRCd_VersionAddition[0])
416 strlcat(NGIRCd_VersionAddition, "+",
417 sizeof NGIRCd_VersionAddition);
418 strlcat(NGIRCd_VersionAddition, "TCPWRAP",
419 sizeof NGIRCd_VersionAddition);
422 if (NGIRCd_VersionAddition[0])
423 strlcat(NGIRCd_VersionAddition, "+",
424 sizeof NGIRCd_VersionAddition);
425 strlcat(NGIRCd_VersionAddition, "ZLIB",
426 sizeof NGIRCd_VersionAddition);
428 if (NGIRCd_VersionAddition[0])
429 strlcat(NGIRCd_VersionAddition, "-",
430 sizeof(NGIRCd_VersionAddition));
432 strlcat(NGIRCd_VersionAddition, HOST_CPU,
433 sizeof(NGIRCd_VersionAddition));
434 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
435 strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
436 sizeof(NGIRCd_VersionAddition));
437 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
438 strlcat(NGIRCd_VersionAddition, HOST_OS,
439 sizeof(NGIRCd_VersionAddition));
441 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
442 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
447 * Display copyright and version information of ngIRCd on the console.
452 puts( NGIRCd_Version );
453 puts( "Copyright (c)2001-2020 Alexander Barton (<alex@barton.de>) and Contributors." );
454 puts( "Homepage: <http://ngircd.barton.de/>\n" );
455 puts( "This is free software; see the source for copying conditions. There is NO" );
456 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
461 * Display a short help text on the console.
462 * This help depends on the configuration of the executable and only shows
463 * options that are actually enabled.
469 puts( " -d, --debug log extra debug messages" );
471 puts( " -f, --config <f> use file <f> as configuration file" );
472 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
473 puts( " -p, --passive disable automatic connections to other servers" );
475 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
477 puts( " -t, --configtest read, validate and display configuration; then exit" );
478 puts( " -V, --version output version information and exit" );
479 puts( " -h, --help display this help and exit" );
484 * Delete the file containing the process ID (PID).
487 Pidfile_Delete( void )
489 /* Pidfile configured? */
490 if( ! Conf_PidFile[0] ) return;
493 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
496 if( unlink( Conf_PidFile ))
497 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
498 } /* Pidfile_Delete */
502 * Create the file containing the process ID of ngIRCd ("PID file").
504 * @param pid The process ID to be stored in this file.
507 Pidfile_Create(pid_t pid)
513 /* Pidfile configured? */
514 if( ! Conf_PidFile[0] ) return;
517 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
520 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
522 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
526 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
527 if (len < 0 || len >= (int)sizeof pidbuf) {
528 Log(LOG_ERR, "Error converting process ID!");
533 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
534 Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
537 if (close(pidfd) != 0)
538 Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
540 } /* Pidfile_Create */
544 * Redirect stdin, stdout and stderr to appropriate file handles.
546 * @param fd The file handle stdin, stdout and stderr should be redirected to.
549 Setup_FDStreams(int fd)
557 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
558 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
559 } /* Setup_FDStreams */
562 #if !defined(SINGLE_USER_OS)
565 * Get user and group ID of unprivileged "nobody" user.
568 * @param gid Group ID
569 * @return true on success.
572 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
578 * It can return EINVAL instead of EPERM
579 * so, if we are already unprivileged,
580 * use id of current user.
582 if (geteuid() && getuid()) {
589 pwd = getpwnam("nobody");
593 if (!pwd->pw_uid || !pwd->pw_gid)
601 } /* NGIRCd_getNobodyID */
606 #ifdef HAVE_ARC4RANDOM
614 Random_Init_Kern(const char *file)
618 int fd = open(file, O_RDONLY);
620 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
629 * Initialize libc rand(3) number generator
634 if (Random_Init_Kern("/dev/urandom"))
636 if (Random_Init_Kern("/dev/random"))
638 if (Random_Init_Kern("/dev/arandom"))
640 srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
646 * Initialize ngIRCd daemon.
648 * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
649 * foreground (and not as a daemon).
650 * @return true on success.
653 NGIRCd_Init(bool NGIRCd_NoDaemon)
655 static bool initialized;
656 bool chrooted = false;
659 int real_errno, fd = -1;
665 if (!NGIRCd_NoDaemon) {
666 /* open /dev/null before chroot() */
667 fd = open( "/dev/null", O_RDWR);
669 Log(LOG_WARNING, "Could not open /dev/null: %s",
673 /* SSL initialization */
674 if (!ConnSSL_InitLibrary()) {
675 Log(LOG_ERR, "Error during SSL initialization!");
680 if (Conf_Chroot[0]) {
681 if (chdir(Conf_Chroot) != 0) {
682 Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
683 Conf_Chroot, strerror(errno));
687 if (chroot(Conf_Chroot) != 0) {
689 "Can't change root directory to \"%s\": %s!",
690 Conf_Chroot, strerror(errno));
695 "Changed root and working directory to \"%s\".",
700 #if !defined(SINGLE_USER_OS)
705 "ServerUID must not be %s(0), using \"nobody\" instead.",
706 pwd ? pwd->pw_name : "?");
707 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
709 "Could not get user/group ID of user \"nobody\": %s",
710 errno ? strerror(errno) : "not found" );
715 /* Change group ID */
716 if (getgid() != Conf_GID) {
717 if (setgid(Conf_GID) != 0) {
719 grp = getgrgid(Conf_GID);
720 Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
721 grp ? grp->gr_name : "?", Conf_GID,
722 strerror(real_errno));
723 if (real_errno != EPERM)
726 #ifdef HAVE_SETGROUPS
727 if (setgroups(0, NULL) != 0) {
729 Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
731 if (real_errno != EPERM)
736 "Can't drop supplementary group IDs: setgroups(3) missing!");
742 if (getuid() != Conf_UID) {
743 if (setuid(Conf_UID) != 0) {
745 pwd = getpwuid(Conf_UID);
746 Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
747 pwd ? pwd->pw_name : "?", Conf_UID,
748 strerror(real_errno));
749 if (real_errno != EPERM)
756 /* Normally a child process is forked which isn't any longer
757 * connected to ther controlling terminal. Use "--nodaemon"
758 * to disable this "daemon mode" (useful for debugging). */
759 if (!NGIRCd_NoDaemon) {
762 /* "Old" process: exit. */
768 "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
769 PACKAGE_NAME, strerror(errno));
773 /* New child process */
777 setpgrp(0, getpid());
780 Log(LOG_ERR, "Can't change directory to '/': %s!",
783 /* Detach stdin, stdout and stderr */
792 /* Check UID/GID we are running as, can be different from values
793 * configured (e. g. if we were already started with a UID>0. */
797 pwd = getpwuid(Conf_UID);
798 grp = getgrgid(Conf_GID);
800 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
801 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
802 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
805 Log(LOG_INFO, "Running with root directory \"%s\".",
809 Log(LOG_INFO, "Not running with changed root directory.");
811 /* Change working directory to home directory of the user we are
812 * running as (only when running in daemon mode and not in chroot) */
818 if (chdir(pwd->pw_dir) == 0)
820 "Changed working directory to \"%s\" ...",
824 "Can't change working directory to \"%s\": %s!",
825 pwd->pw_dir, strerror(errno));
827 Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);