2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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"
56 static void Show_Version PARAMS(( void ));
57 static void Show_Help PARAMS(( void ));
59 static void Pidfile_Create PARAMS(( pid_t pid ));
60 static void Pidfile_Delete PARAMS(( void ));
62 static void Fill_Version PARAMS(( void ));
64 static void Random_Init PARAMS(( void ));
66 static void Setup_FDStreams PARAMS(( int fd ));
68 static bool NGIRCd_Init PARAMS(( bool ));
72 * The main() function of ngIRCd.
74 * Here all starts: this function is called by the operating system loader,
75 * it is the first portion of code executed of ngIRCd.
77 * @param argc The number of arguments passed to ngIRCd on the command line.
78 * @param argv An array containing all the arguments passed to ngIRCd.
79 * @return Global exit code of ngIRCd, zero on success.
82 main(int argc, const char *argv[])
84 bool ok, configtest = false;
85 bool NGIRCd_NoDaemon = false;
89 #if defined(DEBUG) && defined(HAVE_MTRACE)
90 /* enable GNU libc memory tracing when running in debug mode
91 * and functionality available */
97 NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
98 NGIRCd_Passive = false;
100 NGIRCd_Debug = false;
103 NGIRCd_Sniffer = false;
105 strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
106 strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
110 /* parse conmmand line */
111 for (i = 1; i < argc; i++) {
113 if (argv[i][0] == '-' && argv[i][1] == '-') {
115 if (strcmp(argv[i], "--config") == 0) {
117 /* Ok, there's an parameter left */
118 strlcpy(NGIRCd_ConfFile, argv[i+1],
119 sizeof(NGIRCd_ConfFile));
124 if (strcmp(argv[i], "--configtest") == 0) {
129 if (strcmp(argv[i], "--debug") == 0) {
134 if (strcmp(argv[i], "--help") == 0) {
136 puts(""); Show_Help( ); puts( "" );
139 if (strcmp(argv[i], "--nodaemon") == 0) {
140 NGIRCd_NoDaemon = true;
143 if (strcmp(argv[i], "--passive") == 0) {
144 NGIRCd_Passive = true;
148 if (strcmp(argv[i], "--sniffer") == 0) {
149 NGIRCd_Sniffer = true;
153 if (strcmp(argv[i], "--version") == 0) {
158 else if(argv[i][0] == '-' && argv[i][1] != '-') {
160 for (n = 1; n < strlen(argv[i]); n++) {
163 if (argv[i][n] == 'd') {
168 if (argv[i][n] == 'f') {
169 if (!argv[i][n+1] && i+1 < argc) {
170 /* Ok, next character is a blank */
171 strlcpy(NGIRCd_ConfFile, argv[i+1],
172 sizeof(NGIRCd_ConfFile));
174 /* go to the following parameter */
181 if (argv[i][n] == 'h') {
183 puts(""); Show_Help(); puts("");
187 if (argv[i][n] == 'n') {
188 NGIRCd_NoDaemon = true;
191 if (argv[i][n] == 'p') {
192 NGIRCd_Passive = true;
196 if (argv[i][n] == 's') {
197 NGIRCd_Sniffer = true;
201 if (argv[i][n] == 't') {
206 if (argv[i][n] == 'V') {
212 printf("%s: invalid option \"-%c\"!\n",
213 PACKAGE_NAME, argv[i][n]);
214 printf("Try \"%s --help\" for more information.\n",
222 printf("%s: invalid option \"%s\"!\n",
223 PACKAGE_NAME, argv[i]);
224 printf("Try \"%s --help\" for more information.\n",
230 /* Debug level for "VERSION" command */
231 NGIRCd_DebugLevel[0] = '\0';
234 strcpy(NGIRCd_DebugLevel, "1");
237 if (NGIRCd_Sniffer) {
239 strcpy(NGIRCd_DebugLevel, "2");
244 Show_Version(); puts("");
248 while (!NGIRCd_SignalQuit) {
249 /* Initialize global variables */
250 NGIRCd_Start = time(NULL);
251 (void)strftime(NGIRCd_StartStr, 64,
252 "%a %b %d %Y at %H:%M:%S (%Z)",
253 localtime(&NGIRCd_Start));
255 NGIRCd_SignalRestart = false;
256 NGIRCd_SignalQuit = false;
258 /* Initialize modules, part I */
259 Log_Init(!NGIRCd_NoDaemon);
264 /* Initialize the "main program": chroot environment, user and
266 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
267 Log(LOG_ALERT, "Fatal: Initialization failed");
271 /* Initialize modules, part II: these functions are eventually
272 * called with already dropped privileges ... */
278 if (!io_library_init(CONNECTION_POOL)) {
280 "Fatal: Could not initialize IO routines: %s",
285 if (!Signals_Init()) {
287 "Fatal: Could not set up signal handlers: %s",
292 /* Create protocol and server identification. The syntax
293 * used by ngIRCd in PASS commands and the known "extended
294 * flags" are described in doc/Protocol.txt. */
296 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
297 PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
300 strcat(NGIRCd_ProtoID, "Z");
302 if (Conf_OperCanMode)
303 strcat(NGIRCd_ProtoID, "o");
305 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
306 PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
308 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
310 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
312 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
314 Channel_InitPredefined();
316 if (Conn_InitListeners() < 1) {
318 "Server isn't listening on a single port!" );
320 "%s exiting due to fatal errors!", PACKAGE_NAME);
342 * Generate ngIRCd "version strings".
344 * The ngIRCd version information is generated once and then stored in the
345 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
346 * usage, for example by the IRC command "VERSION" and the --version command
352 NGIRCd_VersionAddition[0] = '\0';
355 if (NGIRCd_VersionAddition[0])
356 strlcat(NGIRCd_VersionAddition, "+",
357 sizeof NGIRCd_VersionAddition);
358 strlcat(NGIRCd_VersionAddition, "CHARCONV",
359 sizeof NGIRCd_VersionAddition);
362 if (NGIRCd_VersionAddition[0])
363 strlcat(NGIRCd_VersionAddition, "+",
364 sizeof NGIRCd_VersionAddition);
365 strlcat(NGIRCd_VersionAddition, "DEBUG",
366 sizeof NGIRCd_VersionAddition);
369 if (NGIRCd_VersionAddition[0])
370 strlcat(NGIRCd_VersionAddition, "+",
371 sizeof NGIRCd_VersionAddition);
372 strlcat(NGIRCd_VersionAddition, "IDENT",
373 sizeof NGIRCd_VersionAddition);
376 if (NGIRCd_VersionAddition[0])
377 strlcat(NGIRCd_VersionAddition, "+",
378 sizeof(NGIRCd_VersionAddition));
379 strlcat(NGIRCd_VersionAddition, "IPv6",
380 sizeof(NGIRCd_VersionAddition));
383 if (NGIRCd_VersionAddition[0])
384 strlcat(NGIRCd_VersionAddition, "+",
385 sizeof NGIRCd_VersionAddition);
386 strlcat(NGIRCd_VersionAddition, "IRCPLUS",
387 sizeof NGIRCd_VersionAddition);
390 if (NGIRCd_VersionAddition[0])
391 strlcat(NGIRCd_VersionAddition, "+",
392 sizeof NGIRCd_VersionAddition);
393 strlcat(NGIRCd_VersionAddition, "PAM",
394 sizeof NGIRCd_VersionAddition);
397 if (NGIRCd_VersionAddition[0])
398 strlcat(NGIRCd_VersionAddition, "+",
399 sizeof NGIRCd_VersionAddition);
400 strlcat(NGIRCd_VersionAddition, "RFC",
401 sizeof NGIRCd_VersionAddition);
404 if (NGIRCd_VersionAddition[0])
405 strlcat(NGIRCd_VersionAddition, "+",
406 sizeof NGIRCd_VersionAddition);
407 strlcat(NGIRCd_VersionAddition, "SNIFFER",
408 sizeof NGIRCd_VersionAddition);
411 if (NGIRCd_VersionAddition[0])
412 strlcat(NGIRCd_VersionAddition, "+",
413 sizeof NGIRCd_VersionAddition);
414 strlcat(NGIRCd_VersionAddition, "SSL",
415 sizeof NGIRCd_VersionAddition);
418 if (NGIRCd_VersionAddition[0])
419 strlcat(NGIRCd_VersionAddition, "+",
420 sizeof NGIRCd_VersionAddition);
421 strlcat(NGIRCd_VersionAddition, "SYSLOG",
422 sizeof NGIRCd_VersionAddition);
425 if (NGIRCd_VersionAddition[0])
426 strlcat(NGIRCd_VersionAddition, "+",
427 sizeof NGIRCd_VersionAddition);
428 strlcat(NGIRCd_VersionAddition, "TCPWRAP",
429 sizeof NGIRCd_VersionAddition);
432 if (NGIRCd_VersionAddition[0])
433 strlcat(NGIRCd_VersionAddition, "+",
434 sizeof NGIRCd_VersionAddition);
435 strlcat(NGIRCd_VersionAddition, "ZLIB",
436 sizeof NGIRCd_VersionAddition);
438 if (NGIRCd_VersionAddition[0])
439 strlcat(NGIRCd_VersionAddition, "-",
440 sizeof(NGIRCd_VersionAddition));
442 strlcat(NGIRCd_VersionAddition, HOST_CPU,
443 sizeof(NGIRCd_VersionAddition));
444 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
445 strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
446 sizeof(NGIRCd_VersionAddition));
447 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
448 strlcat(NGIRCd_VersionAddition, HOST_OS,
449 sizeof(NGIRCd_VersionAddition));
451 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
452 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
457 * Display copyright and version information of ngIRCd on the console.
462 puts( NGIRCd_Version );
463 puts( "Copyright (c)2001-2012 Alexander Barton (<alex@barton.de>) and Contributors." );
464 puts( "Homepage: <http://ngircd.barton.de/>\n" );
465 puts( "This is free software; see the source for copying conditions. There is NO" );
466 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
471 * Display a short help text on the console.
472 * This help depends on the configuration of the executable and only shows
473 * options that are actually enabled.
479 puts( " -d, --debug log extra debug messages" );
481 puts( " -f, --config <f> use file <f> as configuration file" );
482 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
483 puts( " -p, --passive disable automatic connections to other servers" );
485 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
487 puts( " -t, --configtest read, validate and display configuration; then exit" );
488 puts( " -V, --version output version information and exit" );
489 puts( " -h, --help display this help and exit" );
494 * Delete the file containing the process ID (PID).
497 Pidfile_Delete( void )
499 /* Pidfile configured? */
500 if( ! Conf_PidFile[0] ) return;
503 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
506 if( unlink( Conf_PidFile ))
507 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
508 } /* Pidfile_Delete */
512 * Create the file containing the process ID of ngIRCd ("PID file").
514 * @param pid The process ID to be stored in this file.
517 Pidfile_Create(pid_t pid)
523 /* Pidfile configured? */
524 if( ! Conf_PidFile[0] ) return;
527 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
530 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
532 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
536 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
537 if (len < 0 || len >= (int)sizeof pidbuf) {
538 Log(LOG_ERR, "Error converting pid");
543 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
544 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
546 if( close(pidfd) != 0 )
547 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
548 } /* Pidfile_Create */
552 * Redirect stdin, stdout and stderr to apropriate file handles.
554 * @param fd The file handle stdin, stdout and stderr should be redirected to.
557 Setup_FDStreams(int fd)
565 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
566 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
567 } /* Setup_FDStreams */
570 #if !defined(SINGLE_USER_OS)
573 * Get user and group ID of unprivileged "nobody" user.
576 * @param gid Group ID
577 * @return true on success.
580 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
586 * It can return EINVAL instead of EPERM
587 * so, if we are already unprivileged,
588 * use id of current user.
590 if (geteuid() && getuid()) {
597 pwd = getpwnam("nobody");
601 if (!pwd->pw_uid || !pwd->pw_gid)
609 } /* NGIRCd_getNobodyID */
615 Random_Init_Kern(const char *file)
619 int fd = open(file, O_RDONLY);
621 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
630 * Initialize libc rand(3) number generator
635 if (Random_Init_Kern("/dev/urandom"))
637 if (Random_Init_Kern("/dev/random"))
639 if (Random_Init_Kern("/dev/arandom"))
641 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())
676 "Error during SSL initialization, continuing without SSL ...");
679 if (Conf_Chroot[0]) {
680 if (chdir(Conf_Chroot) != 0) {
681 Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s",
682 Conf_Chroot, strerror(errno));
686 if (chroot(Conf_Chroot) != 0) {
688 "Can't change root directory to \"%s\": %s",
689 Conf_Chroot, strerror(errno));
694 "Changed root and working directory to \"%s\".",
699 #if !defined(SINGLE_USER_OS)
704 "ServerUID must not be %s(0), using \"nobody\" instead.",
705 pwd ? pwd->pw_name : "?");
706 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
708 "Could not get user/group ID of user \"nobody\": %s",
709 errno ? strerror(errno) : "not found" );
714 /* Change group ID */
715 if (getgid() != Conf_GID) {
716 if (setgid(Conf_GID) != 0) {
718 grp = getgrgid(Conf_GID);
719 Log(LOG_ERR, "Can't change group ID to %s(%u): %s",
720 grp ? grp->gr_name : "?", Conf_GID,
722 if (real_errno != EPERM)
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,
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 "Notice: 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);