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.
28 #include <sys/types.h>
34 #if defined(DEBUG) && defined(HAVE_MTRACE)
47 #include "sighandlers.h"
53 static void Show_Version PARAMS(( void ));
54 static void Show_Help PARAMS(( void ));
56 static void Pidfile_Create PARAMS(( pid_t pid ));
57 static void Pidfile_Delete PARAMS(( void ));
59 static void Fill_Version PARAMS(( void ));
61 static void Random_Init PARAMS(( void ));
63 static void Setup_FDStreams PARAMS(( int fd ));
65 static bool NGIRCd_Init PARAMS(( bool ));
69 * The main() function of ngIRCd.
71 * Here all starts: this function is called by the operating system loader,
72 * it is the first portion of code executed of ngIRCd.
74 * @param argc The number of arguments passed to ngIRCd on the command line.
75 * @param argv An array containing all the arguments passed to ngIRCd.
76 * @return Global exit code of ngIRCd, zero on success.
79 main(int argc, const char *argv[])
81 bool ok, configtest = false;
82 bool NGIRCd_NoDaemon = false;
86 #if defined(DEBUG) && defined(HAVE_MTRACE)
87 /* enable GNU libc memory tracing when running in debug mode
88 * and functionality available */
94 NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
95 NGIRCd_Passive = false;
100 NGIRCd_Sniffer = false;
102 strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
103 strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
107 /* parse conmmand line */
108 for (i = 1; i < argc; i++) {
110 if (argv[i][0] == '-' && argv[i][1] == '-') {
112 if (strcmp(argv[i], "--config") == 0) {
114 /* Ok, there's an parameter left */
115 strlcpy(NGIRCd_ConfFile, argv[i+1],
116 sizeof(NGIRCd_ConfFile));
121 if (strcmp(argv[i], "--configtest") == 0) {
126 if (strcmp(argv[i], "--debug") == 0) {
131 if (strcmp(argv[i], "--help") == 0) {
133 puts(""); Show_Help( ); puts( "" );
136 if (strcmp(argv[i], "--nodaemon") == 0) {
137 NGIRCd_NoDaemon = true;
140 if (strcmp(argv[i], "--passive") == 0) {
141 NGIRCd_Passive = true;
145 if (strcmp(argv[i], "--sniffer") == 0) {
146 NGIRCd_Sniffer = true;
150 if (strcmp(argv[i], "--version") == 0) {
155 else if(argv[i][0] == '-' && argv[i][1] != '-') {
157 for (n = 1; n < strlen(argv[i]); n++) {
160 if (argv[i][n] == 'd') {
165 if (argv[i][n] == 'f') {
166 if (!argv[i][n+1] && i+1 < argc) {
167 /* Ok, next character is a blank */
168 strlcpy(NGIRCd_ConfFile, argv[i+1],
169 sizeof(NGIRCd_ConfFile));
171 /* go to the following parameter */
178 if (argv[i][n] == 'h') {
180 puts(""); Show_Help(); puts("");
184 if (argv[i][n] == 'n') {
185 NGIRCd_NoDaemon = true;
188 if (argv[i][n] == 'p') {
189 NGIRCd_Passive = true;
193 if (argv[i][n] == 's') {
194 NGIRCd_Sniffer = true;
198 if (argv[i][n] == 't') {
203 if (argv[i][n] == 'V') {
209 printf("%s: invalid option \"-%c\"!\n",
210 PACKAGE_NAME, argv[i][n]);
211 printf("Try \"%s --help\" for more information.\n",
219 printf("%s: invalid option \"%s\"!\n",
220 PACKAGE_NAME, argv[i]);
221 printf("Try \"%s --help\" for more information.\n",
227 /* Debug level for "VERSION" command */
228 NGIRCd_DebugLevel[0] = '\0';
231 strcpy(NGIRCd_DebugLevel, "1");
234 if (NGIRCd_Sniffer) {
236 strcpy(NGIRCd_DebugLevel, "2");
241 Show_Version(); puts("");
245 while (!NGIRCd_SignalQuit) {
246 /* Initialize global variables */
247 NGIRCd_Start = time(NULL);
248 (void)strftime(NGIRCd_StartStr, 64,
249 "%a %b %d %Y at %H:%M:%S (%Z)",
250 localtime(&NGIRCd_Start));
252 NGIRCd_SignalRestart = false;
253 NGIRCd_SignalQuit = false;
255 /* Initialize modules, part I */
256 Log_Init(!NGIRCd_NoDaemon);
261 /* Initialize the "main program": chroot environment, user and
263 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
264 Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
268 /* Initialize modules, part II: these functions are eventually
269 * called with already dropped privileges ... */
275 if (!io_library_init(CONNECTION_POOL)) {
277 "Fatal: Could not initialize IO routines: %s",
282 if (!Signals_Init()) {
284 "Fatal: Could not set up signal handlers: %s",
289 /* Create protocol and server identification. The syntax
290 * used by ngIRCd in PASS commands and the known "extended
291 * flags" are described in doc/Protocol.txt. */
293 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
294 PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
297 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
299 if (Conf_OperCanMode)
300 strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
302 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
303 PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
305 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
307 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
309 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
311 Channel_InitPredefined();
313 if (Conn_InitListeners() < 1) {
315 "Server isn't listening on a single port!" );
317 "%s exiting due to fatal errors!", PACKAGE_NAME);
339 * Generate ngIRCd "version strings".
341 * The ngIRCd version information is generated once and then stored in the
342 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
343 * usage, for example by the IRC command "VERSION" and the --version command
349 NGIRCd_VersionAddition[0] = '\0';
352 if (NGIRCd_VersionAddition[0])
353 strlcat(NGIRCd_VersionAddition, "+",
354 sizeof NGIRCd_VersionAddition);
355 strlcat(NGIRCd_VersionAddition, "CHARCONV",
356 sizeof NGIRCd_VersionAddition);
359 if (NGIRCd_VersionAddition[0])
360 strlcat(NGIRCd_VersionAddition, "+",
361 sizeof NGIRCd_VersionAddition);
362 strlcat(NGIRCd_VersionAddition, "DEBUG",
363 sizeof NGIRCd_VersionAddition);
366 if (NGIRCd_VersionAddition[0])
367 strlcat(NGIRCd_VersionAddition, "+",
368 sizeof NGIRCd_VersionAddition);
369 strlcat(NGIRCd_VersionAddition, "IDENT",
370 sizeof NGIRCd_VersionAddition);
373 if (NGIRCd_VersionAddition[0])
374 strlcat(NGIRCd_VersionAddition, "+",
375 sizeof(NGIRCd_VersionAddition));
376 strlcat(NGIRCd_VersionAddition, "IPv6",
377 sizeof(NGIRCd_VersionAddition));
380 if (NGIRCd_VersionAddition[0])
381 strlcat(NGIRCd_VersionAddition, "+",
382 sizeof NGIRCd_VersionAddition);
383 strlcat(NGIRCd_VersionAddition, "IRCPLUS",
384 sizeof NGIRCd_VersionAddition);
387 if (NGIRCd_VersionAddition[0])
388 strlcat(NGIRCd_VersionAddition, "+",
389 sizeof NGIRCd_VersionAddition);
390 strlcat(NGIRCd_VersionAddition, "PAM",
391 sizeof NGIRCd_VersionAddition);
394 if (NGIRCd_VersionAddition[0])
395 strlcat(NGIRCd_VersionAddition, "+",
396 sizeof NGIRCd_VersionAddition);
397 strlcat(NGIRCd_VersionAddition, "RFC",
398 sizeof NGIRCd_VersionAddition);
401 if (NGIRCd_VersionAddition[0])
402 strlcat(NGIRCd_VersionAddition, "+",
403 sizeof NGIRCd_VersionAddition);
404 strlcat(NGIRCd_VersionAddition, "SNIFFER",
405 sizeof NGIRCd_VersionAddition);
408 if (NGIRCd_VersionAddition[0])
409 strlcat(NGIRCd_VersionAddition, "+",
410 sizeof NGIRCd_VersionAddition);
411 strlcat(NGIRCd_VersionAddition, "SSL",
412 sizeof NGIRCd_VersionAddition);
415 if (NGIRCd_VersionAddition[0])
416 strlcat(NGIRCd_VersionAddition, "+",
417 sizeof NGIRCd_VersionAddition);
418 strlcat(NGIRCd_VersionAddition, "SYSLOG",
419 sizeof NGIRCd_VersionAddition);
422 if (NGIRCd_VersionAddition[0])
423 strlcat(NGIRCd_VersionAddition, "+",
424 sizeof NGIRCd_VersionAddition);
425 strlcat(NGIRCd_VersionAddition, "TCPWRAP",
426 sizeof NGIRCd_VersionAddition);
429 if (NGIRCd_VersionAddition[0])
430 strlcat(NGIRCd_VersionAddition, "+",
431 sizeof NGIRCd_VersionAddition);
432 strlcat(NGIRCd_VersionAddition, "ZLIB",
433 sizeof NGIRCd_VersionAddition);
435 if (NGIRCd_VersionAddition[0])
436 strlcat(NGIRCd_VersionAddition, "-",
437 sizeof(NGIRCd_VersionAddition));
439 strlcat(NGIRCd_VersionAddition, HOST_CPU,
440 sizeof(NGIRCd_VersionAddition));
441 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
442 strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
443 sizeof(NGIRCd_VersionAddition));
444 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
445 strlcat(NGIRCd_VersionAddition, HOST_OS,
446 sizeof(NGIRCd_VersionAddition));
448 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
449 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
454 * Display copyright and version information of ngIRCd on the console.
459 puts( NGIRCd_Version );
460 puts( "Copyright (c)2001-2014 Alexander Barton (<alex@barton.de>) and Contributors." );
461 puts( "Homepage: <http://ngircd.barton.de/>\n" );
462 puts( "This is free software; see the source for copying conditions. There is NO" );
463 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
468 * Display a short help text on the console.
469 * This help depends on the configuration of the executable and only shows
470 * options that are actually enabled.
476 puts( " -d, --debug log extra debug messages" );
478 puts( " -f, --config <f> use file <f> as configuration file" );
479 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
480 puts( " -p, --passive disable automatic connections to other servers" );
482 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
484 puts( " -t, --configtest read, validate and display configuration; then exit" );
485 puts( " -V, --version output version information and exit" );
486 puts( " -h, --help display this help and exit" );
491 * Delete the file containing the process ID (PID).
494 Pidfile_Delete( void )
496 /* Pidfile configured? */
497 if( ! Conf_PidFile[0] ) return;
500 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
503 if( unlink( Conf_PidFile ))
504 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
505 } /* Pidfile_Delete */
509 * Create the file containing the process ID of ngIRCd ("PID file").
511 * @param pid The process ID to be stored in this file.
514 Pidfile_Create(pid_t pid)
520 /* Pidfile configured? */
521 if( ! Conf_PidFile[0] ) return;
524 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
527 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
529 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
533 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
534 if (len < 0 || len >= (int)sizeof pidbuf) {
535 Log(LOG_ERR, "Error converting process ID!");
540 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
541 Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
544 if (close(pidfd) != 0)
545 Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
547 } /* Pidfile_Create */
551 * Redirect stdin, stdout and stderr to appropriate file handles.
553 * @param fd The file handle stdin, stdout and stderr should be redirected to.
556 Setup_FDStreams(int fd)
564 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
565 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
566 } /* Setup_FDStreams */
569 #if !defined(SINGLE_USER_OS)
572 * Get user and group ID of unprivileged "nobody" user.
575 * @param gid Group ID
576 * @return true on success.
579 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
585 * It can return EINVAL instead of EPERM
586 * so, if we are already unprivileged,
587 * use id of current user.
589 if (geteuid() && getuid()) {
596 pwd = getpwnam("nobody");
600 if (!pwd->pw_uid || !pwd->pw_gid)
608 } /* NGIRCd_getNobodyID */
613 #ifdef HAVE_ARC4RANDOM
621 Random_Init_Kern(const char *file)
625 int fd = open(file, O_RDONLY);
627 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
636 * Initialize libc rand(3) number generator
641 if (Random_Init_Kern("/dev/urandom"))
643 if (Random_Init_Kern("/dev/random"))
645 if (Random_Init_Kern("/dev/arandom"))
647 srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
653 * Initialize ngIRCd daemon.
655 * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
656 * foreground (and not as a daemon).
657 * @return true on success.
660 NGIRCd_Init(bool NGIRCd_NoDaemon)
662 static bool initialized;
663 bool chrooted = false;
666 int real_errno, fd = -1;
672 if (!NGIRCd_NoDaemon) {
673 /* open /dev/null before chroot() */
674 fd = open( "/dev/null", O_RDWR);
676 Log(LOG_WARNING, "Could not open /dev/null: %s",
680 /* SSL initialization */
681 if (!ConnSSL_InitLibrary()) {
682 Log(LOG_ERR, "Error during SSL initialization!");
687 if (Conf_Chroot[0]) {
688 if (chdir(Conf_Chroot) != 0) {
689 Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
690 Conf_Chroot, strerror(errno));
694 if (chroot(Conf_Chroot) != 0) {
696 "Can't change root directory to \"%s\": %s!",
697 Conf_Chroot, strerror(errno));
702 "Changed root and working directory to \"%s\".",
707 #if !defined(SINGLE_USER_OS)
712 "ServerUID must not be %s(0), using \"nobody\" instead.",
713 pwd ? pwd->pw_name : "?");
714 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
716 "Could not get user/group ID of user \"nobody\": %s",
717 errno ? strerror(errno) : "not found" );
722 /* Change group ID */
723 if (getgid() != Conf_GID) {
724 if (setgid(Conf_GID) != 0) {
726 grp = getgrgid(Conf_GID);
727 Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
728 grp ? grp->gr_name : "?", Conf_GID,
729 strerror(real_errno));
730 if (real_errno != EPERM)
733 if (setgroups(0, NULL) != 0) {
735 Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
737 if (real_errno != EPERM)
744 if (getuid() != Conf_UID) {
745 if (setuid(Conf_UID) != 0) {
747 pwd = getpwuid(Conf_UID);
748 Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
749 pwd ? pwd->pw_name : "?", Conf_UID,
750 strerror(real_errno));
751 if (real_errno != EPERM)
758 /* Normally a child process is forked which isn't any longer
759 * connected to ther controlling terminal. Use "--nodaemon"
760 * to disable this "daemon mode" (useful for debugging). */
761 if (!NGIRCd_NoDaemon) {
764 /* "Old" process: exit. */
770 "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
771 PACKAGE_NAME, strerror(errno));
775 /* New child process */
779 setpgrp(0, getpid());
782 Log(LOG_ERR, "Can't change directory to '/': %s!",
785 /* Detach stdin, stdout and stderr */
794 /* Check UID/GID we are running as, can be different from values
795 * configured (e. g. if we were already started with a UID>0. */
799 pwd = getpwuid(Conf_UID);
800 grp = getgrgid(Conf_GID);
802 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
803 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
804 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
807 Log(LOG_INFO, "Running with root directory \"%s\".",
811 Log(LOG_INFO, "Not running with changed root directory.");
813 /* Change working directory to home directory of the user we are
814 * running as (only when running in daemon mode and not in chroot) */
820 if (chdir(pwd->pw_dir) == 0)
822 "Changed working directory to \"%s\" ...",
826 "Can't change working directory to \"%s\": %s!",
827 pwd->pw_dir, strerror(errno));
829 Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);