2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2019 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') {
203 printf("%s: invalid option \"-%c\"!\n",
204 PACKAGE_NAME, argv[i][n]);
205 printf("Try \"%s --help\" for more information.\n",
213 printf("%s: invalid option \"%s\"!\n",
214 PACKAGE_NAME, argv[i]);
215 printf("Try \"%s --help\" for more information.\n",
221 /* Debug level for "VERSION" command */
222 NGIRCd_DebugLevel[0] = '\0';
225 strcpy(NGIRCd_DebugLevel, "1");
228 if (NGIRCd_Sniffer) {
230 strcpy(NGIRCd_DebugLevel, "2");
235 Show_Version(); puts("");
239 while (!NGIRCd_SignalQuit) {
240 /* Initialize global variables */
241 NGIRCd_Start = time(NULL);
242 (void)strftime(NGIRCd_StartStr, 64,
243 "%a %b %d %Y at %H:%M:%S (%Z)",
244 localtime(&NGIRCd_Start));
246 NGIRCd_SignalRestart = false;
247 NGIRCd_SignalQuit = false;
249 Log_Init(!NGIRCd_NoDaemon);
254 /* Initialize the "main program":
255 * chroot environment, user and group ID, ... */
256 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
257 Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
261 if (!io_library_init(CONNECTION_POOL)) {
263 "Fatal: Could not initialize IO routines: %s",
268 if (!Signals_Init()) {
270 "Fatal: Could not set up signal handlers: %s",
280 /* Create protocol and server identification. The syntax
281 * used by ngIRCd in PASS commands and the known "extended
282 * flags" are described in doc/Protocol.txt. */
284 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
285 PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
288 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
290 if (Conf_OperCanMode)
291 strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
293 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
294 PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
296 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
298 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
300 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
302 Channel_InitPredefined();
304 if (Conn_InitListeners() < 1) {
306 "Server isn't listening on a single port!" );
308 "%s exiting due to fatal errors!", PACKAGE_NAME);
330 * Generate ngIRCd "version strings".
332 * The ngIRCd version information is generated once and then stored in the
333 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
334 * usage, for example by the IRC command "VERSION" and the --version command
340 NGIRCd_VersionAddition[0] = '\0';
343 if (NGIRCd_VersionAddition[0])
344 strlcat(NGIRCd_VersionAddition, "+",
345 sizeof NGIRCd_VersionAddition);
346 strlcat(NGIRCd_VersionAddition, "CHARCONV",
347 sizeof NGIRCd_VersionAddition);
350 if (NGIRCd_VersionAddition[0])
351 strlcat(NGIRCd_VersionAddition, "+",
352 sizeof NGIRCd_VersionAddition);
353 strlcat(NGIRCd_VersionAddition, "DEBUG",
354 sizeof NGIRCd_VersionAddition);
357 if (NGIRCd_VersionAddition[0])
358 strlcat(NGIRCd_VersionAddition, "+",
359 sizeof NGIRCd_VersionAddition);
360 strlcat(NGIRCd_VersionAddition, "IDENT",
361 sizeof NGIRCd_VersionAddition);
364 if (NGIRCd_VersionAddition[0])
365 strlcat(NGIRCd_VersionAddition, "+",
366 sizeof(NGIRCd_VersionAddition));
367 strlcat(NGIRCd_VersionAddition, "IPv6",
368 sizeof(NGIRCd_VersionAddition));
371 if (NGIRCd_VersionAddition[0])
372 strlcat(NGIRCd_VersionAddition, "+",
373 sizeof NGIRCd_VersionAddition);
374 strlcat(NGIRCd_VersionAddition, "IRCPLUS",
375 sizeof NGIRCd_VersionAddition);
378 if (NGIRCd_VersionAddition[0])
379 strlcat(NGIRCd_VersionAddition, "+",
380 sizeof NGIRCd_VersionAddition);
381 strlcat(NGIRCd_VersionAddition, "PAM",
382 sizeof NGIRCd_VersionAddition);
385 if (NGIRCd_VersionAddition[0])
386 strlcat(NGIRCd_VersionAddition, "+",
387 sizeof NGIRCd_VersionAddition);
388 strlcat(NGIRCd_VersionAddition, "RFC",
389 sizeof NGIRCd_VersionAddition);
392 if (NGIRCd_VersionAddition[0])
393 strlcat(NGIRCd_VersionAddition, "+",
394 sizeof NGIRCd_VersionAddition);
395 strlcat(NGIRCd_VersionAddition, "SNIFFER",
396 sizeof NGIRCd_VersionAddition);
399 if (NGIRCd_VersionAddition[0])
400 strlcat(NGIRCd_VersionAddition, "+",
401 sizeof NGIRCd_VersionAddition);
402 strlcat(NGIRCd_VersionAddition, "SSL",
403 sizeof NGIRCd_VersionAddition);
406 if (NGIRCd_VersionAddition[0])
407 strlcat(NGIRCd_VersionAddition, "+",
408 sizeof NGIRCd_VersionAddition);
409 strlcat(NGIRCd_VersionAddition, "SYSLOG",
410 sizeof NGIRCd_VersionAddition);
413 if (NGIRCd_VersionAddition[0])
414 strlcat(NGIRCd_VersionAddition, "+",
415 sizeof NGIRCd_VersionAddition);
416 strlcat(NGIRCd_VersionAddition, "TCPWRAP",
417 sizeof NGIRCd_VersionAddition);
420 if (NGIRCd_VersionAddition[0])
421 strlcat(NGIRCd_VersionAddition, "+",
422 sizeof NGIRCd_VersionAddition);
423 strlcat(NGIRCd_VersionAddition, "ZLIB",
424 sizeof NGIRCd_VersionAddition);
426 if (NGIRCd_VersionAddition[0])
427 strlcat(NGIRCd_VersionAddition, "-",
428 sizeof(NGIRCd_VersionAddition));
430 strlcat(NGIRCd_VersionAddition, HOST_CPU,
431 sizeof(NGIRCd_VersionAddition));
432 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
433 strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
434 sizeof(NGIRCd_VersionAddition));
435 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
436 strlcat(NGIRCd_VersionAddition, HOST_OS,
437 sizeof(NGIRCd_VersionAddition));
439 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
440 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
445 * Display copyright and version information of ngIRCd on the console.
450 puts( NGIRCd_Version );
451 puts( "Copyright (c)2001-2019 Alexander Barton (<alex@barton.de>) and Contributors." );
452 puts( "Homepage: <http://ngircd.barton.de/>\n" );
453 puts( "This is free software; see the source for copying conditions. There is NO" );
454 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
459 * Display a short help text on the console.
460 * This help depends on the configuration of the executable and only shows
461 * options that are actually enabled.
467 puts( " -d, --debug log extra debug messages" );
469 puts( " -f, --config <f> use file <f> as configuration file" );
470 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
471 puts( " -p, --passive disable automatic connections to other servers" );
473 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
475 puts( " -t, --configtest read, validate and display configuration; then exit" );
476 puts( " -V, --version output version information and exit" );
477 puts( " -h, --help display this help and exit" );
482 * Delete the file containing the process ID (PID).
485 Pidfile_Delete( void )
487 /* Pidfile configured? */
488 if( ! Conf_PidFile[0] ) return;
491 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
494 if( unlink( Conf_PidFile ))
495 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
496 } /* Pidfile_Delete */
500 * Create the file containing the process ID of ngIRCd ("PID file").
502 * @param pid The process ID to be stored in this file.
505 Pidfile_Create(pid_t pid)
511 /* Pidfile configured? */
512 if( ! Conf_PidFile[0] ) return;
515 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
518 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
520 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
524 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
525 if (len < 0 || len >= (int)sizeof pidbuf) {
526 Log(LOG_ERR, "Error converting process ID!");
531 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
532 Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
535 if (close(pidfd) != 0)
536 Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
538 } /* Pidfile_Create */
542 * Redirect stdin, stdout and stderr to appropriate file handles.
544 * @param fd The file handle stdin, stdout and stderr should be redirected to.
547 Setup_FDStreams(int fd)
555 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
556 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
557 } /* Setup_FDStreams */
560 #if !defined(SINGLE_USER_OS)
563 * Get user and group ID of unprivileged "nobody" user.
566 * @param gid Group ID
567 * @return true on success.
570 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
576 * It can return EINVAL instead of EPERM
577 * so, if we are already unprivileged,
578 * use id of current user.
580 if (geteuid() && getuid()) {
587 pwd = getpwnam("nobody");
591 if (!pwd->pw_uid || !pwd->pw_gid)
599 } /* NGIRCd_getNobodyID */
604 #ifdef HAVE_ARC4RANDOM
612 Random_Init_Kern(const char *file)
616 int fd = open(file, O_RDONLY);
618 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
627 * Initialize libc rand(3) number generator
632 if (Random_Init_Kern("/dev/urandom"))
634 if (Random_Init_Kern("/dev/random"))
636 if (Random_Init_Kern("/dev/arandom"))
638 srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
644 * Initialize ngIRCd daemon.
646 * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
647 * foreground (and not as a daemon).
648 * @return true on success.
651 NGIRCd_Init(bool NGIRCd_NoDaemon)
653 static bool initialized;
654 bool chrooted = false;
657 int real_errno, fd = -1;
663 if (!NGIRCd_NoDaemon) {
664 /* open /dev/null before chroot() */
665 fd = open( "/dev/null", O_RDWR);
667 Log(LOG_WARNING, "Could not open /dev/null: %s",
671 /* SSL initialization */
672 if (!ConnSSL_InitLibrary()) {
673 Log(LOG_ERR, "Error during SSL initialization!");
678 if (Conf_Chroot[0]) {
679 if (chdir(Conf_Chroot) != 0) {
680 Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
681 Conf_Chroot, strerror(errno));
685 if (chroot(Conf_Chroot) != 0) {
687 "Can't change root directory to \"%s\": %s!",
688 Conf_Chroot, strerror(errno));
693 "Changed root and working directory to \"%s\".",
698 #if !defined(SINGLE_USER_OS)
703 "ServerUID must not be %s(0), using \"nobody\" instead.",
704 pwd ? pwd->pw_name : "?");
705 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
707 "Could not get user/group ID of user \"nobody\": %s",
708 errno ? strerror(errno) : "not found" );
713 /* Change group ID */
714 if (getgid() != Conf_GID) {
715 if (setgid(Conf_GID) != 0) {
717 grp = getgrgid(Conf_GID);
718 Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
719 grp ? grp->gr_name : "?", Conf_GID,
720 strerror(real_errno));
721 if (real_errno != EPERM)
724 #ifdef HAVE_SETGROUPS
725 if (setgroups(0, NULL) != 0) {
727 Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
729 if (real_errno != EPERM)
734 "Can't drop supplementary group IDs: setgroups(3) missing!");
740 if (getuid() != Conf_UID) {
741 if (setuid(Conf_UID) != 0) {
743 pwd = getpwuid(Conf_UID);
744 Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
745 pwd ? pwd->pw_name : "?", Conf_UID,
746 strerror(real_errno));
747 if (real_errno != EPERM)
754 /* Normally a child process is forked which isn't any longer
755 * connected to ther controlling terminal. Use "--nodaemon"
756 * to disable this "daemon mode" (useful for debugging). */
757 if (!NGIRCd_NoDaemon) {
760 /* "Old" process: exit. */
766 "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
767 PACKAGE_NAME, strerror(errno));
771 /* New child process */
775 setpgrp(0, getpid());
778 Log(LOG_ERR, "Can't change directory to '/': %s!",
781 /* Detach stdin, stdout and stderr */
790 /* Check UID/GID we are running as, can be different from values
791 * configured (e. g. if we were already started with a UID>0. */
795 pwd = getpwuid(Conf_UID);
796 grp = getgrgid(Conf_GID);
798 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
799 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
800 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
803 Log(LOG_INFO, "Running with root directory \"%s\".",
807 Log(LOG_INFO, "Not running with changed root directory.");
809 /* Change working directory to home directory of the user we are
810 * running as (only when running in daemon mode and not in chroot) */
816 if (chdir(pwd->pw_dir) == 0)
818 "Changed working directory to \"%s\" ...",
822 "Can't change working directory to \"%s\": %s!",
823 pwd->pw_dir, strerror(errno));
825 Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);