2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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.
29 #include <sys/types.h>
35 #if defined(DEBUG) && defined(HAVE_MTRACE)
48 #include "sighandlers.h"
55 static void Show_Version PARAMS(( void ));
56 static void Show_Help PARAMS(( void ));
58 static void Pidfile_Create PARAMS(( pid_t pid ));
59 static void Pidfile_Delete PARAMS(( void ));
61 static void Fill_Version PARAMS(( void ));
63 static void Random_Init PARAMS(( void ));
65 static void Setup_FDStreams PARAMS(( int fd ));
67 static bool NGIRCd_Init PARAMS(( bool ));
71 * The main() function of ngIRCd.
73 * Here all starts: this function is called by the operating system loader,
74 * it is the first portion of code executed of ngIRCd.
76 * @param argc The number of arguments passed to ngIRCd on the command line.
77 * @param argv An array containing all the arguments passed to ngIRCd.
78 * @return Global exit code of ngIRCd, zero on success.
81 main(int argc, const char *argv[])
83 bool ok, configtest = false;
84 bool NGIRCd_NoDaemon = false;
88 #if defined(DEBUG) && defined(HAVE_MTRACE)
89 /* enable GNU libc memory tracing when running in debug mode
90 * and functionality available */
96 NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
97 NGIRCd_Passive = false;
102 NGIRCd_Sniffer = false;
104 strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
105 strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
109 /* parse conmmand line */
110 for (i = 1; i < argc; i++) {
112 if (argv[i][0] == '-' && argv[i][1] == '-') {
114 if (strcmp(argv[i], "--config") == 0) {
116 /* Ok, there's an parameter left */
117 strlcpy(NGIRCd_ConfFile, argv[i+1],
118 sizeof(NGIRCd_ConfFile));
123 if (strcmp(argv[i], "--configtest") == 0) {
128 if (strcmp(argv[i], "--debug") == 0) {
133 if (strcmp(argv[i], "--help") == 0) {
135 puts(""); Show_Help( ); puts( "" );
138 if (strcmp(argv[i], "--nodaemon") == 0) {
139 NGIRCd_NoDaemon = true;
142 if (strcmp(argv[i], "--passive") == 0) {
143 NGIRCd_Passive = true;
147 if (strcmp(argv[i], "--sniffer") == 0) {
148 NGIRCd_Sniffer = true;
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;
190 if (argv[i][n] == 'p') {
191 NGIRCd_Passive = true;
195 if (argv[i][n] == 's') {
196 NGIRCd_Sniffer = true;
200 if (argv[i][n] == 't') {
205 if (argv[i][n] == 'V') {
211 printf("%s: invalid option \"-%c\"!\n",
212 PACKAGE_NAME, argv[i][n]);
213 printf("Try \"%s --help\" for more information.\n",
221 printf("%s: invalid option \"%s\"!\n",
222 PACKAGE_NAME, argv[i]);
223 printf("Try \"%s --help\" for more information.\n",
229 /* Debug level for "VERSION" command */
230 NGIRCd_DebugLevel[0] = '\0';
233 strcpy(NGIRCd_DebugLevel, "1");
236 if (NGIRCd_Sniffer) {
238 strcpy(NGIRCd_DebugLevel, "2");
243 Show_Version(); puts("");
247 while (!NGIRCd_SignalQuit) {
248 /* Initialize global variables */
249 NGIRCd_Start = time(NULL);
250 (void)strftime(NGIRCd_StartStr, 64,
251 "%a %b %d %Y at %H:%M:%S (%Z)",
252 localtime(&NGIRCd_Start));
254 NGIRCd_SignalRestart = false;
255 NGIRCd_SignalQuit = false;
257 /* Initialize modules, part I */
258 Log_Init(!NGIRCd_NoDaemon);
263 /* Initialize the "main program": chroot environment, user and
265 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
266 Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
270 /* Initialize modules, part II: these functions are eventually
271 * called with already dropped privileges ... */
277 if (!io_library_init(CONNECTION_POOL)) {
279 "Fatal: Could not initialize IO routines: %s",
284 if (!Signals_Init()) {
286 "Fatal: Could not set up signal handlers: %s",
291 /* Create protocol and server identification. The syntax
292 * used by ngIRCd in PASS commands and the known "extended
293 * flags" are described in doc/Protocol.txt. */
295 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
296 PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
299 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
301 if (Conf_OperCanMode)
302 strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
304 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
305 PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
307 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
309 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
311 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
313 Channel_InitPredefined();
315 if (Conn_InitListeners() < 1) {
317 "Server isn't listening on a single port!" );
319 "%s exiting due to fatal errors!", PACKAGE_NAME);
341 * Generate ngIRCd "version strings".
343 * The ngIRCd version information is generated once and then stored in the
344 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
345 * usage, for example by the IRC command "VERSION" and the --version command
351 NGIRCd_VersionAddition[0] = '\0';
354 if (NGIRCd_VersionAddition[0])
355 strlcat(NGIRCd_VersionAddition, "+",
356 sizeof NGIRCd_VersionAddition);
357 strlcat(NGIRCd_VersionAddition, "CHARCONV",
358 sizeof NGIRCd_VersionAddition);
361 if (NGIRCd_VersionAddition[0])
362 strlcat(NGIRCd_VersionAddition, "+",
363 sizeof NGIRCd_VersionAddition);
364 strlcat(NGIRCd_VersionAddition, "DEBUG",
365 sizeof NGIRCd_VersionAddition);
368 if (NGIRCd_VersionAddition[0])
369 strlcat(NGIRCd_VersionAddition, "+",
370 sizeof NGIRCd_VersionAddition);
371 strlcat(NGIRCd_VersionAddition, "IDENT",
372 sizeof NGIRCd_VersionAddition);
375 if (NGIRCd_VersionAddition[0])
376 strlcat(NGIRCd_VersionAddition, "+",
377 sizeof(NGIRCd_VersionAddition));
378 strlcat(NGIRCd_VersionAddition, "IPv6",
379 sizeof(NGIRCd_VersionAddition));
382 if (NGIRCd_VersionAddition[0])
383 strlcat(NGIRCd_VersionAddition, "+",
384 sizeof NGIRCd_VersionAddition);
385 strlcat(NGIRCd_VersionAddition, "IRCPLUS",
386 sizeof NGIRCd_VersionAddition);
389 if (NGIRCd_VersionAddition[0])
390 strlcat(NGIRCd_VersionAddition, "+",
391 sizeof NGIRCd_VersionAddition);
392 strlcat(NGIRCd_VersionAddition, "PAM",
393 sizeof NGIRCd_VersionAddition);
396 if (NGIRCd_VersionAddition[0])
397 strlcat(NGIRCd_VersionAddition, "+",
398 sizeof NGIRCd_VersionAddition);
399 strlcat(NGIRCd_VersionAddition, "RFC",
400 sizeof NGIRCd_VersionAddition);
403 if (NGIRCd_VersionAddition[0])
404 strlcat(NGIRCd_VersionAddition, "+",
405 sizeof NGIRCd_VersionAddition);
406 strlcat(NGIRCd_VersionAddition, "SNIFFER",
407 sizeof NGIRCd_VersionAddition);
410 if (NGIRCd_VersionAddition[0])
411 strlcat(NGIRCd_VersionAddition, "+",
412 sizeof NGIRCd_VersionAddition);
413 strlcat(NGIRCd_VersionAddition, "SSL",
414 sizeof NGIRCd_VersionAddition);
417 if (NGIRCd_VersionAddition[0])
418 strlcat(NGIRCd_VersionAddition, "+",
419 sizeof NGIRCd_VersionAddition);
420 strlcat(NGIRCd_VersionAddition, "SYSLOG",
421 sizeof NGIRCd_VersionAddition);
424 if (NGIRCd_VersionAddition[0])
425 strlcat(NGIRCd_VersionAddition, "+",
426 sizeof NGIRCd_VersionAddition);
427 strlcat(NGIRCd_VersionAddition, "TCPWRAP",
428 sizeof NGIRCd_VersionAddition);
431 if (NGIRCd_VersionAddition[0])
432 strlcat(NGIRCd_VersionAddition, "+",
433 sizeof NGIRCd_VersionAddition);
434 strlcat(NGIRCd_VersionAddition, "ZLIB",
435 sizeof NGIRCd_VersionAddition);
437 if (NGIRCd_VersionAddition[0])
438 strlcat(NGIRCd_VersionAddition, "-",
439 sizeof(NGIRCd_VersionAddition));
441 strlcat(NGIRCd_VersionAddition, HOST_CPU,
442 sizeof(NGIRCd_VersionAddition));
443 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
444 strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
445 sizeof(NGIRCd_VersionAddition));
446 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
447 strlcat(NGIRCd_VersionAddition, HOST_OS,
448 sizeof(NGIRCd_VersionAddition));
450 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
451 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
456 * Display copyright and version information of ngIRCd on the console.
461 puts( NGIRCd_Version );
462 puts( "Copyright (c)2001-2014 Alexander Barton (<alex@barton.de>) and Contributors." );
463 puts( "Homepage: <http://ngircd.barton.de/>\n" );
464 puts( "This is free software; see the source for copying conditions. There is NO" );
465 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
470 * Display a short help text on the console.
471 * This help depends on the configuration of the executable and only shows
472 * options that are actually enabled.
478 puts( " -d, --debug log extra debug messages" );
480 puts( " -f, --config <f> use file <f> as configuration file" );
481 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
482 puts( " -p, --passive disable automatic connections to other servers" );
484 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
486 puts( " -t, --configtest read, validate and display configuration; then exit" );
487 puts( " -V, --version output version information and exit" );
488 puts( " -h, --help display this help and exit" );
493 * Delete the file containing the process ID (PID).
496 Pidfile_Delete( void )
498 /* Pidfile configured? */
499 if( ! Conf_PidFile[0] ) return;
502 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
505 if( unlink( Conf_PidFile ))
506 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
507 } /* Pidfile_Delete */
511 * Create the file containing the process ID of ngIRCd ("PID file").
513 * @param pid The process ID to be stored in this file.
516 Pidfile_Create(pid_t pid)
522 /* Pidfile configured? */
523 if( ! Conf_PidFile[0] ) return;
526 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
529 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
531 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
535 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
536 if (len < 0 || len >= (int)sizeof pidbuf) {
537 Log(LOG_ERR, "Error converting process ID!");
542 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
543 Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
546 if (close(pidfd) != 0)
547 Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
549 } /* Pidfile_Create */
553 * Redirect stdin, stdout and stderr to appropriate file handles.
555 * @param fd The file handle stdin, stdout and stderr should be redirected to.
558 Setup_FDStreams(int fd)
566 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
567 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
568 } /* Setup_FDStreams */
571 #if !defined(SINGLE_USER_OS)
574 * Get user and group ID of unprivileged "nobody" user.
577 * @param gid Group ID
578 * @return true on success.
581 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
587 * It can return EINVAL instead of EPERM
588 * so, if we are already unprivileged,
589 * use id of current user.
591 if (geteuid() && getuid()) {
598 pwd = getpwnam("nobody");
602 if (!pwd->pw_uid || !pwd->pw_gid)
610 } /* NGIRCd_getNobodyID */
615 #ifdef HAVE_ARC4RANDOM
623 Random_Init_Kern(const char *file)
627 int fd = open(file, O_RDONLY);
629 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
638 * Initialize libc rand(3) number generator
643 if (Random_Init_Kern("/dev/urandom"))
645 if (Random_Init_Kern("/dev/random"))
647 if (Random_Init_Kern("/dev/arandom"))
649 srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
655 * Initialize ngIRCd daemon.
657 * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
658 * foreground (and not as a daemon).
659 * @return true on success.
662 NGIRCd_Init(bool NGIRCd_NoDaemon)
664 static bool initialized;
665 bool chrooted = false;
668 int real_errno, fd = -1;
674 if (!NGIRCd_NoDaemon) {
675 /* open /dev/null before chroot() */
676 fd = open( "/dev/null", O_RDWR);
678 Log(LOG_WARNING, "Could not open /dev/null: %s",
682 /* SSL initialization */
683 if (!ConnSSL_InitLibrary()) {
684 Log(LOG_ERR, "Error during SSL initialization!");
689 if (Conf_Chroot[0]) {
690 if (chdir(Conf_Chroot) != 0) {
691 Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
692 Conf_Chroot, strerror(errno));
696 if (chroot(Conf_Chroot) != 0) {
698 "Can't change root directory to \"%s\": %s!",
699 Conf_Chroot, strerror(errno));
704 "Changed root and working directory to \"%s\".",
709 #if !defined(SINGLE_USER_OS)
714 "ServerUID must not be %s(0), using \"nobody\" instead.",
715 pwd ? pwd->pw_name : "?");
716 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
718 "Could not get user/group ID of user \"nobody\": %s",
719 errno ? strerror(errno) : "not found" );
724 /* Change group ID */
725 if (getgid() != Conf_GID) {
726 if (setgid(Conf_GID) != 0) {
728 grp = getgrgid(Conf_GID);
729 Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
730 grp ? grp->gr_name : "?", Conf_GID,
731 strerror(real_errno));
732 if (real_errno != EPERM)
735 if (setgroups(0, NULL) != 0) {
737 Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
739 if (real_errno != EPERM)
746 if (getuid() != Conf_UID) {
747 if (setuid(Conf_UID) != 0) {
749 pwd = getpwuid(Conf_UID);
750 Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
751 pwd ? pwd->pw_name : "?", Conf_UID,
752 strerror(real_errno));
753 if (real_errno != EPERM)
760 /* Normally a child process is forked which isn't any longer
761 * connected to ther controlling terminal. Use "--nodaemon"
762 * to disable this "daemon mode" (useful for debugging). */
763 if (!NGIRCd_NoDaemon) {
766 /* "Old" process: exit. */
772 "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
773 PACKAGE_NAME, strerror(errno));
777 /* New child process */
781 setpgrp(0, getpid());
784 Log(LOG_ERR, "Can't change directory to '/': %s!",
787 /* Detach stdin, stdout and stderr */
796 /* Check UID/GID we are running as, can be different from values
797 * configured (e. g. if we were already started with a UID>0. */
801 pwd = getpwuid(Conf_UID);
802 grp = getgrgid(Conf_GID);
804 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
805 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
806 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
809 Log(LOG_INFO, "Running with root directory \"%s\".",
813 Log(LOG_INFO, "Not running with changed root directory.");
815 /* Change working directory to home directory of the user we are
816 * running as (only when running in daemon mode and not in chroot) */
822 if (chdir(pwd->pw_dir) == 0)
824 "Changed working directory to \"%s\" ...",
828 "Can't change working directory to \"%s\": %s!",
829 pwd->pw_dir, strerror(errno));
831 Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);