2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2022 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.
17 * The main program, including the C function main() which is called
18 * by the loader of the operating system.
28 #include <sys/types.h>
34 #if defined(DEBUG) && defined(HAVE_MTRACE)
43 #include "sighandlers.h"
48 static void Show_Version PARAMS(( void ));
49 static void Show_Help PARAMS(( void ));
51 static void Pidfile_Create PARAMS(( pid_t pid ));
52 static void Pidfile_Delete PARAMS(( void ));
54 static void Fill_Version PARAMS(( void ));
56 static void Random_Init PARAMS(( void ));
58 static void Setup_FDStreams PARAMS(( int fd ));
60 static bool NGIRCd_Init PARAMS(( bool ));
64 * The main() function of ngIRCd.
66 * Here all starts: this function is called by the operating system loader,
67 * it is the first portion of code executed of ngIRCd.
69 * @param argc The number of arguments passed to ngIRCd on the command line.
70 * @param argv An array containing all the arguments passed to ngIRCd.
71 * @return Global exit code of ngIRCd, zero on success.
74 main(int argc, const char *argv[])
76 bool ok, configtest = false;
77 bool NGIRCd_NoDaemon = false, NGIRCd_NoSyslog = false;
81 #if defined(DEBUG) && defined(HAVE_MTRACE)
82 /* enable GNU libc memory tracing when running in debug mode
83 * and functionality available */
89 NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
90 NGIRCd_Passive = false;
95 NGIRCd_Sniffer = false;
97 strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
98 strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
102 /* parse conmmand line */
103 for (i = 1; i < argc; i++) {
105 if (argv[i][0] == '-' && argv[i][1] == '-') {
107 if (strcmp(argv[i], "--config") == 0) {
109 /* Ok, there's an parameter left */
110 strlcpy(NGIRCd_ConfFile, argv[i+1],
111 sizeof(NGIRCd_ConfFile));
116 if (strcmp(argv[i], "--configtest") == 0) {
121 if (strcmp(argv[i], "--debug") == 0) {
126 if (strcmp(argv[i], "--help") == 0) {
128 puts(""); Show_Help( ); puts( "" );
131 if (strcmp(argv[i], "--nodaemon") == 0) {
132 NGIRCd_NoDaemon = true;
133 NGIRCd_NoSyslog = true;
136 if (strcmp(argv[i], "--passive") == 0) {
137 NGIRCd_Passive = true;
141 if (strcmp(argv[i], "--sniffer") == 0) {
142 NGIRCd_Sniffer = true;
146 if (strcmp(argv[i], "--version") == 0) {
151 else if(argv[i][0] == '-' && argv[i][1] != '-') {
153 for (n = 1; n < strlen(argv[i]); n++) {
156 if (argv[i][n] == 'd') {
161 if (argv[i][n] == 'f') {
162 if (!argv[i][n+1] && i+1 < argc) {
163 /* Ok, next character is a blank */
164 strlcpy(NGIRCd_ConfFile, argv[i+1],
165 sizeof(NGIRCd_ConfFile));
167 /* go to the following parameter */
174 if (argv[i][n] == 'h') {
176 puts(""); Show_Help(); puts("");
180 if (argv[i][n] == 'n') {
181 NGIRCd_NoDaemon = true;
182 NGIRCd_NoSyslog = true;
185 if (argv[i][n] == 'p') {
186 NGIRCd_Passive = true;
190 if (argv[i][n] == 's') {
191 NGIRCd_Sniffer = true;
195 if (argv[i][n] == 't') {
200 if (argv[i][n] == 'V') {
207 "%s: invalid option \"-%c\"!\n",
208 PACKAGE_NAME, argv[i][n]);
210 "Try \"%s --help\" for more information.\n",
218 fprintf(stderr, "%s: invalid option \"%s\"!\n",
219 PACKAGE_NAME, argv[i]);
220 fprintf(stderr, "Try \"%s --help\" for more information.\n",
226 /* Debug level for "VERSION" command */
227 NGIRCd_DebugLevel[0] = '\0';
230 strcpy(NGIRCd_DebugLevel, "1");
233 if (NGIRCd_Sniffer) {
235 strcpy(NGIRCd_DebugLevel, "2");
240 Show_Version(); puts("");
244 while (!NGIRCd_SignalQuit) {
245 /* Initialize global variables */
246 NGIRCd_Start = time(NULL);
247 (void)strftime(NGIRCd_StartStr, 64,
248 "%a %b %d %Y at %H:%M:%S (%Z)",
249 localtime(&NGIRCd_Start));
251 NGIRCd_SignalRestart = false;
252 NGIRCd_SignalQuit = false;
254 Log_Init(!NGIRCd_NoSyslog);
259 /* Initialize the "main program":
260 * chroot environment, user and group ID, ... */
261 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
262 Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
266 if (!io_library_init(CONNECTION_POOL)) {
268 "Fatal: Could not initialize IO routines: %s",
273 if (!Signals_Init()) {
275 "Fatal: Could not set up signal handlers: %s",
285 /* Create protocol and server identification. The syntax
286 * used by ngIRCd in PASS commands and the known "extended
287 * flags" are described in doc/Protocol.txt. */
289 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
290 PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
293 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
295 if (Conf_OperCanMode)
296 strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
298 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
299 PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
301 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
303 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
305 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
307 Channel_InitPredefined();
309 if (Conn_InitListeners() < 1) {
311 "Server isn't listening on a single port!" );
313 "%s exiting due to fatal errors!", PACKAGE_NAME);
335 * Generate ngIRCd "version strings".
337 * The ngIRCd version information is generated once and then stored in the
338 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
339 * usage, for example by the IRC command "VERSION" and the --version command
345 NGIRCd_VersionAddition[0] = '\0';
348 if (NGIRCd_VersionAddition[0])
349 strlcat(NGIRCd_VersionAddition, "+",
350 sizeof NGIRCd_VersionAddition);
351 strlcat(NGIRCd_VersionAddition, "CHARCONV",
352 sizeof NGIRCd_VersionAddition);
355 if (NGIRCd_VersionAddition[0])
356 strlcat(NGIRCd_VersionAddition, "+",
357 sizeof NGIRCd_VersionAddition);
358 strlcat(NGIRCd_VersionAddition, "DEBUG",
359 sizeof NGIRCd_VersionAddition);
362 if (NGIRCd_VersionAddition[0])
363 strlcat(NGIRCd_VersionAddition, "+",
364 sizeof NGIRCd_VersionAddition);
365 strlcat(NGIRCd_VersionAddition, "IDENT",
366 sizeof NGIRCd_VersionAddition);
369 if (NGIRCd_VersionAddition[0])
370 strlcat(NGIRCd_VersionAddition, "+",
371 sizeof(NGIRCd_VersionAddition));
372 strlcat(NGIRCd_VersionAddition, "IPv6",
373 sizeof(NGIRCd_VersionAddition));
376 if (NGIRCd_VersionAddition[0])
377 strlcat(NGIRCd_VersionAddition, "+",
378 sizeof NGIRCd_VersionAddition);
379 strlcat(NGIRCd_VersionAddition, "IRCPLUS",
380 sizeof NGIRCd_VersionAddition);
383 if (NGIRCd_VersionAddition[0])
384 strlcat(NGIRCd_VersionAddition, "+",
385 sizeof NGIRCd_VersionAddition);
386 strlcat(NGIRCd_VersionAddition, "PAM",
387 sizeof NGIRCd_VersionAddition);
390 if (NGIRCd_VersionAddition[0])
391 strlcat(NGIRCd_VersionAddition, "+",
392 sizeof NGIRCd_VersionAddition);
393 strlcat(NGIRCd_VersionAddition, "RFC",
394 sizeof NGIRCd_VersionAddition);
397 if (NGIRCd_VersionAddition[0])
398 strlcat(NGIRCd_VersionAddition, "+",
399 sizeof NGIRCd_VersionAddition);
400 strlcat(NGIRCd_VersionAddition, "SNIFFER",
401 sizeof NGIRCd_VersionAddition);
404 if (NGIRCd_VersionAddition[0])
405 strlcat(NGIRCd_VersionAddition, "+",
406 sizeof NGIRCd_VersionAddition);
407 strlcat(NGIRCd_VersionAddition, "SSL",
408 sizeof NGIRCd_VersionAddition);
411 if (NGIRCd_VersionAddition[0])
412 strlcat(NGIRCd_VersionAddition, "+",
413 sizeof NGIRCd_VersionAddition);
414 strlcat(NGIRCd_VersionAddition, "SYSLOG",
415 sizeof NGIRCd_VersionAddition);
418 if (NGIRCd_VersionAddition[0])
419 strlcat(NGIRCd_VersionAddition, "+",
420 sizeof NGIRCd_VersionAddition);
421 strlcat(NGIRCd_VersionAddition, "TCPWRAP",
422 sizeof NGIRCd_VersionAddition);
425 if (NGIRCd_VersionAddition[0])
426 strlcat(NGIRCd_VersionAddition, "+",
427 sizeof NGIRCd_VersionAddition);
428 strlcat(NGIRCd_VersionAddition, "ZLIB",
429 sizeof NGIRCd_VersionAddition);
431 if (NGIRCd_VersionAddition[0])
432 strlcat(NGIRCd_VersionAddition, "-",
433 sizeof(NGIRCd_VersionAddition));
435 strlcat(NGIRCd_VersionAddition, HOST_CPU,
436 sizeof(NGIRCd_VersionAddition));
437 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
438 strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
439 sizeof(NGIRCd_VersionAddition));
440 strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
441 strlcat(NGIRCd_VersionAddition, HOST_OS,
442 sizeof(NGIRCd_VersionAddition));
444 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
445 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
450 * Display copyright and version information of ngIRCd on the console.
455 puts( NGIRCd_Version );
456 puts( "Copyright (c)2001-2022 Alexander Barton (<alex@barton.de>) and Contributors." );
457 puts( "Homepage: <http://ngircd.barton.de/>\n" );
458 puts( "This is free software; see the source for copying conditions. There is NO" );
459 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
464 * Display a short help text on the console.
465 * This help depends on the configuration of the executable and only shows
466 * options that are actually enabled.
472 puts( " -d, --debug log extra debug messages" );
474 puts( " -f, --config <f> use file <f> as configuration file" );
475 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
476 puts( " -p, --passive disable automatic connections to other servers" );
478 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
480 puts( " -t, --configtest read, validate and display configuration; then exit" );
481 puts( " -V, --version output version information and exit" );
482 puts( " -h, --help display this help and exit" );
487 * Delete the file containing the process ID (PID).
490 Pidfile_Delete( void )
492 /* Pidfile configured? */
493 if( ! Conf_PidFile[0] ) return;
496 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
499 if( unlink( Conf_PidFile ))
500 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
501 } /* Pidfile_Delete */
505 * Create the file containing the process ID of ngIRCd ("PID file").
507 * @param pid The process ID to be stored in this file.
510 Pidfile_Create(pid_t pid)
516 /* Pidfile configured? */
517 if( ! Conf_PidFile[0] ) return;
520 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
523 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
525 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
529 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
530 if (len < 0 || len >= (int)sizeof pidbuf) {
531 Log(LOG_ERR, "Error converting process ID!");
536 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
537 Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
540 if (close(pidfd) != 0)
541 Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
543 } /* Pidfile_Create */
547 * Redirect stdin, stdout and stderr to appropriate file handles.
549 * @param fd The file handle stdin, stdout and stderr should be redirected to.
552 Setup_FDStreams(int fd)
560 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
561 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
562 } /* Setup_FDStreams */
565 #if !defined(SINGLE_USER_OS)
568 * Get user and group ID of unprivileged "nobody" user.
571 * @param gid Group ID
572 * @return true on success.
575 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
581 * It can return EINVAL instead of EPERM
582 * so, if we are already unprivileged,
583 * use id of current user.
585 if (geteuid() && getuid()) {
592 pwd = getpwnam("nobody");
596 if (!pwd->pw_uid || !pwd->pw_gid)
604 } /* NGIRCd_getNobodyID */
609 #ifdef HAVE_ARC4RANDOM
617 Random_Init_Kern(const char *file)
621 int fd = open(file, O_RDONLY);
623 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
632 * Initialize libc rand(3) number generator
637 if (Random_Init_Kern("/dev/urandom"))
639 if (Random_Init_Kern("/dev/random"))
641 if (Random_Init_Kern("/dev/arandom"))
643 srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
649 * Initialize ngIRCd daemon.
651 * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
652 * foreground (and not as a daemon).
653 * @return true on success.
656 NGIRCd_Init(bool NGIRCd_NoDaemon)
658 static bool initialized;
659 bool chrooted = false;
662 int real_errno, fd = -1;
668 if (!NGIRCd_NoDaemon) {
669 /* open /dev/null before chroot() */
670 fd = open( "/dev/null", O_RDWR);
672 Log(LOG_WARNING, "Could not open /dev/null: %s",
676 /* SSL initialization */
677 if (!ConnSSL_InitLibrary()) {
678 Log(LOG_ERR, "Error during SSL initialization!");
683 if (Conf_Chroot[0]) {
684 if (chdir(Conf_Chroot) != 0) {
685 Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
686 Conf_Chroot, strerror(errno));
690 if (chroot(Conf_Chroot) != 0) {
692 "Can't change root directory to \"%s\": %s!",
693 Conf_Chroot, strerror(errno));
698 "Changed root and working directory to \"%s\".",
703 #if !defined(SINGLE_USER_OS)
708 "ServerUID must not be %s(0), using \"nobody\" instead.",
709 pwd ? pwd->pw_name : "?");
710 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
712 "Could not get user/group ID of user \"nobody\": %s",
713 errno ? strerror(errno) : "not found" );
718 /* Change group ID */
719 if (getgid() != Conf_GID) {
720 if (setgid(Conf_GID) != 0) {
722 grp = getgrgid(Conf_GID);
723 Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
724 grp ? grp->gr_name : "?", Conf_GID,
725 strerror(real_errno));
726 if (real_errno != EPERM)
729 #ifdef HAVE_SETGROUPS
730 if (setgroups(0, NULL) != 0) {
732 Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
734 if (real_errno != EPERM)
739 "Can't drop supplementary group IDs: setgroups(3) missing!");
745 if (getuid() != Conf_UID) {
746 if (setuid(Conf_UID) != 0) {
748 pwd = getpwuid(Conf_UID);
749 Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
750 pwd ? pwd->pw_name : "?", Conf_UID,
751 strerror(real_errno));
752 if (real_errno != EPERM)
759 /* Normally a child process is forked which isn't any longer
760 * connected to ther controlling terminal. Use "--nodaemon"
761 * to disable this "daemon mode" (useful for debugging). */
762 if (!NGIRCd_NoDaemon) {
765 /* "Old" process: exit. */
771 "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
772 PACKAGE_NAME, strerror(errno));
776 /* New child process */
780 setpgrp(0, getpid());
783 Log(LOG_ERR, "Can't change directory to '/': %s!",
786 /* Detach stdin, stdout and stderr */
795 /* Check UID/GID we are running as, can be different from values
796 * configured (e. g. if we were already started with a UID>0. */
800 pwd = getpwuid(Conf_UID);
801 grp = getgrgid(Conf_GID);
803 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
804 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
805 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
808 Log(LOG_INFO, "Running with root directory \"%s\".",
812 Log(LOG_INFO, "Not running with changed root directory.");
814 /* Change working directory to home directory of the user we are
815 * running as (only when running in daemon mode and not in chroot) */
821 if (chdir(pwd->pw_dir) == 0)
823 "Changed working directory to \"%s\" ...",
827 "Can't change working directory to \"%s\": %s!",
828 pwd->pw_dir, strerror(errno));
830 Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);