]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
Clarify that "CAFile" is not set by default
[ngircd-alex.git] / src / ngircd / ngircd.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2024 Alexander Barton (alex@barton.de) and Contributors.
4  *
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.
10  */
11
12 #define GLOBAL_INIT
13 #include "portab.h"
14
15 /**
16  * @file
17  * The main program, including the C function main() which is called
18  * by the loader of the operating system.
19  */
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <pwd.h>
32 #include <grp.h>
33
34 #if defined(DEBUG) && defined(HAVE_MTRACE)
35 #include <mcheck.h>
36 #endif
37
38 #include "conn.h"
39 #include "class.h"
40 #include "channel.h"
41 #include "conf.h"
42 #include "log.h"
43 #include "sighandlers.h"
44 #include "io.h"
45
46 #include "ngircd.h"
47
48 static void Show_Version PARAMS(( void ));
49 static void Show_Help PARAMS(( void ));
50
51 static void Pidfile_Create PARAMS(( pid_t pid ));
52 static void Pidfile_Delete PARAMS(( void ));
53
54 static void Fill_Version PARAMS(( void ));
55
56 static void Random_Init PARAMS(( void ));
57
58 static void Setup_FDStreams PARAMS(( int fd ));
59
60 static bool NGIRCd_Init PARAMS(( bool ));
61
62
63 /**
64  * The main() function of ngIRCd.
65  *
66  * Here all starts: this function is called by the operating system loader,
67  * it is the first portion of code executed of ngIRCd.
68  *
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.
72  */
73 GLOBAL int
74 main(int argc, const char *argv[])
75 {
76         bool ok, configtest = false;
77         bool NGIRCd_NoDaemon = false, NGIRCd_NoSyslog = false;
78         int i;
79         size_t n;
80
81 #if defined(DEBUG) && defined(HAVE_MTRACE)
82         /* enable GNU libc memory tracing when running in debug mode
83          * and functionality available */
84         mtrace();
85 #endif
86
87         umask(0077);
88
89         NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
90         NGIRCd_Passive = false;
91         NGIRCd_Debug = false;
92 #ifdef SNIFFER
93         NGIRCd_Sniffer = false;
94 #endif
95
96         Fill_Version();
97
98         /* parse conmmand line */
99         for (i = 1; i < argc; i++) {
100                 ok = false;
101                 if (argv[i][0] == '-' && argv[i][1] == '-') {
102                         /* long option */
103                         if (strcmp(argv[i], "--config") == 0) {
104                                 if (i + 1 < argc) {
105                                         /* Ok, there's an parameter left */
106                                         strlcpy(NGIRCd_ConfFile, argv[i+1],
107                                                 sizeof(NGIRCd_ConfFile));
108                                         /* next parameter */
109                                         i++; ok = true;
110                                 }
111                         }
112                         if (strcmp(argv[i], "--configtest") == 0) {
113                                 configtest = true;
114                                 ok = true;
115                         }
116                         if (strcmp(argv[i], "--debug") == 0) {
117                                 NGIRCd_Debug = true;
118                                 ok = true;
119                         }
120                         if (strcmp(argv[i], "--help") == 0) {
121                                 Show_Version();
122                                 puts(""); Show_Help( ); puts( "" );
123                                 exit(0);
124                         }
125                         if (strcmp(argv[i], "--nodaemon") == 0) {
126                                 NGIRCd_NoDaemon = true;
127                                 NGIRCd_NoSyslog = true;
128                                 ok = true;
129                         }
130                         if (strcmp(argv[i], "--passive") == 0) {
131                                 NGIRCd_Passive = true;
132                                 ok = true;
133                         }
134 #ifdef SNIFFER
135                         if (strcmp(argv[i], "--sniffer") == 0) {
136                                 NGIRCd_Sniffer = true;
137                                 ok = true;
138                         }
139 #endif
140 #ifdef SYSLOG
141                         if (strcmp(argv[i], "--syslog") == 0) {
142                                 NGIRCd_NoSyslog = false;
143                                 ok = true;
144                         }
145 #endif
146                         if (strcmp(argv[i], "--version") == 0) {
147                                 Show_Version();
148                                 exit(0);
149                         }
150                 }
151                 else if(argv[i][0] == '-' && argv[i][1] != '-') {
152                         /* short option */
153                         for (n = 1; n < strlen(argv[i]); n++) {
154                                 ok = false;
155                                 if (argv[i][n] == 'd') {
156                                         NGIRCd_Debug = true;
157                                         ok = true;
158                                 }
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));
164
165                                                 /* go to the following parameter */
166                                                 i++;
167                                                 n = strlen(argv[i]);
168                                                 ok = true;
169                                         }
170                                 }
171
172                                 if (argv[i][n] == 'h') {
173                                         Show_Version();
174                                         puts(""); Show_Help(); puts("");
175                                         exit(1);
176                                 }
177
178                                 if (argv[i][n] == 'n') {
179                                         NGIRCd_NoDaemon = true;
180                                         NGIRCd_NoSyslog = true;
181                                         ok = true;
182                                 }
183                                 if (argv[i][n] == 'p') {
184                                         NGIRCd_Passive = true;
185                                         ok = true;
186                                 }
187 #ifdef SNIFFER
188                                 if (argv[i][n] == 's') {
189                                         NGIRCd_Sniffer = true;
190                                         ok = true;
191                                 }
192 #endif
193                                 if (argv[i][n] == 't') {
194                                         configtest = true;
195                                         ok = true;
196                                 }
197
198                                 if (argv[i][n] == 'V') {
199                                         Show_Version();
200                                         exit(1);
201                                 }
202 #ifdef SYSLOG
203                                 if (argv[i][n] == 'y') {
204                                         NGIRCd_NoSyslog = false;
205                                         ok = true;
206                                 }
207 #endif
208
209                                 if (!ok) {
210                                         fprintf(stderr,
211                                                 "%s: invalid option \"-%c\"!\n",
212                                                 PACKAGE_NAME, argv[i][n]);
213                                         fprintf(stderr,
214                                                 "Try \"%s --help\" for more information.\n",
215                                                 PACKAGE_NAME);
216                                         exit(2);
217                                 }
218                         }
219
220                 }
221                 if (!ok) {
222                         fprintf(stderr, "%s: invalid option \"%s\"!\n",
223                                 PACKAGE_NAME, argv[i]);
224                         fprintf(stderr, "Try \"%s --help\" for more information.\n",
225                                 PACKAGE_NAME);
226                         exit(2);
227                 }
228         }
229
230         /* Debug level for "VERSION" command */
231         NGIRCd_DebugLevel[0] = '\0';
232         if (NGIRCd_Debug)
233                 strcpy(NGIRCd_DebugLevel, "1");
234 #ifdef SNIFFER
235         if (NGIRCd_Sniffer) {
236                 NGIRCd_Debug = true;
237                 strcpy(NGIRCd_DebugLevel, "2");
238         }
239 #endif
240
241         if (configtest) {
242                 Show_Version(); puts("");
243                 exit(Conf_Test());
244         }
245
246         while (!NGIRCd_SignalQuit) {
247                 /* Initialize global variables */
248                 NGIRCd_Start = time(NULL);
249                 (void)strftime(NGIRCd_StartStr, 64,
250                                "%a %b %d %Y at %H:%M:%S (%Z)",
251                                localtime(&NGIRCd_Start));
252
253                 NGIRCd_SignalRestart = false;
254                 NGIRCd_SignalQuit = false;
255
256                 Log_Init(!NGIRCd_NoSyslog);
257                 Random_Init();
258                 Conf_Init();
259                 Log_ReInit();
260
261                 /* Initialize the "main program":
262                  * chroot environment, user and group ID, ... */
263                 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
264                         Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
265                         exit(1);
266                 }
267
268                 if (!io_library_init(CONNECTION_POOL)) {
269                         Log(LOG_ALERT,
270                             "Fatal: Could not initialize IO routines: %s",
271                             strerror(errno));
272                         exit(1);
273                 }
274
275                 if (!Signals_Init()) {
276                         Log(LOG_ALERT,
277                             "Fatal: Could not set up signal handlers: %s",
278                             strerror(errno));
279                         exit(1);
280                 }
281
282                 Channel_Init();
283                 Conn_Init();
284                 Class_Init();
285                 Client_Init();
286
287                 /* Create protocol and server identification. The syntax
288                  * used by ngIRCd in PASS commands and the known "extended
289                  * flags" are described in doc/Protocol.txt. */
290 #ifdef IRCPLUS
291                 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
292                          PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
293                          IRCPLUSFLAGS);
294 #ifdef ZLIB
295                 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
296 #endif
297                 if (Conf_OperCanMode)
298                         strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
299 #else /* IRCPLUS */
300                 snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
301                          PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
302 #endif /* IRCPLUS */
303                 strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
304 #ifdef ZLIB
305                 strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
306 #endif
307                 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
308
309                 Channel_InitPredefined();
310
311                 if (Conn_InitListeners() < 1) {
312                         Log(LOG_ALERT,
313                             "Server isn't listening on a single port!" );
314                         Log(LOG_ALERT,
315                             "%s exiting due to fatal errors!", PACKAGE_NAME);
316                         Pidfile_Delete();
317                         exit(1);
318                 }
319
320                 /* Main Run Loop */
321                 Conn_Handler();
322
323                 Conn_Exit();
324                 Client_Exit();
325                 Channel_Exit();
326                 Class_Exit();
327                 Log_Exit();
328                 Signals_Exit();
329         }
330         Pidfile_Delete();
331
332         return 0;
333 } /* main */
334
335
336 /**
337  * Generate ngIRCd "version strings".
338  *
339  * The ngIRCd version information is generated once and then stored in the
340  * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
341  * usage, for example by the IRC command "VERSION" and the --version command
342  * line switch.
343  */
344 static void
345 Fill_Version(void)
346 {
347         NGIRCd_VersionAddition[0] = '\0';
348
349 #ifdef ICONV
350         if (NGIRCd_VersionAddition[0])
351                 strlcat(NGIRCd_VersionAddition, "+",
352                         sizeof NGIRCd_VersionAddition);
353         strlcat(NGIRCd_VersionAddition, "CHARCONV",
354                 sizeof NGIRCd_VersionAddition);
355 #endif
356 #ifdef DEBUG
357         if (NGIRCd_VersionAddition[0])
358                 strlcat(NGIRCd_VersionAddition, "+",
359                         sizeof NGIRCd_VersionAddition);
360         strlcat(NGIRCd_VersionAddition, "DEBUG",
361                 sizeof NGIRCd_VersionAddition);
362 #endif
363 #ifdef IDENTAUTH
364         if (NGIRCd_VersionAddition[0])
365                 strlcat(NGIRCd_VersionAddition, "+",
366                         sizeof NGIRCd_VersionAddition);
367         strlcat(NGIRCd_VersionAddition, "IDENT",
368                 sizeof NGIRCd_VersionAddition);
369 #endif
370 #ifdef WANT_IPV6
371         if (NGIRCd_VersionAddition[0])
372                 strlcat(NGIRCd_VersionAddition, "+",
373                         sizeof(NGIRCd_VersionAddition));
374         strlcat(NGIRCd_VersionAddition, "IPv6",
375                 sizeof(NGIRCd_VersionAddition));
376 #endif
377 #ifdef IRCPLUS
378         if (NGIRCd_VersionAddition[0])
379                 strlcat(NGIRCd_VersionAddition, "+",
380                         sizeof NGIRCd_VersionAddition);
381         strlcat(NGIRCd_VersionAddition, "IRCPLUS",
382                 sizeof NGIRCd_VersionAddition);
383 #endif
384 #ifdef PAM
385         if (NGIRCd_VersionAddition[0])
386                 strlcat(NGIRCd_VersionAddition, "+",
387                         sizeof NGIRCd_VersionAddition);
388         strlcat(NGIRCd_VersionAddition, "PAM",
389                 sizeof NGIRCd_VersionAddition);
390 #endif
391 #ifdef STRICT_RFC
392         if (NGIRCd_VersionAddition[0])
393                 strlcat(NGIRCd_VersionAddition, "+",
394                         sizeof NGIRCd_VersionAddition);
395         strlcat(NGIRCd_VersionAddition, "RFC",
396                 sizeof NGIRCd_VersionAddition);
397 #endif
398 #ifdef SNIFFER
399         if (NGIRCd_VersionAddition[0])
400                 strlcat(NGIRCd_VersionAddition, "+",
401                         sizeof NGIRCd_VersionAddition);
402         strlcat(NGIRCd_VersionAddition, "SNIFFER",
403                 sizeof NGIRCd_VersionAddition);
404 #endif
405 #ifdef SSL_SUPPORT
406         if (NGIRCd_VersionAddition[0])
407                 strlcat(NGIRCd_VersionAddition, "+",
408                         sizeof NGIRCd_VersionAddition);
409         strlcat(NGIRCd_VersionAddition, "SSL",
410                 sizeof NGIRCd_VersionAddition);
411 #endif
412 #ifdef SYSLOG
413         if (NGIRCd_VersionAddition[0])
414                 strlcat(NGIRCd_VersionAddition, "+",
415                         sizeof NGIRCd_VersionAddition);
416         strlcat(NGIRCd_VersionAddition, "SYSLOG",
417                 sizeof NGIRCd_VersionAddition);
418 #endif
419 #ifdef TCPWRAP
420         if (NGIRCd_VersionAddition[0])
421                 strlcat(NGIRCd_VersionAddition, "+",
422                         sizeof NGIRCd_VersionAddition);
423         strlcat(NGIRCd_VersionAddition, "TCPWRAP",
424                 sizeof NGIRCd_VersionAddition);
425 #endif
426 #ifdef ZLIB
427         if (NGIRCd_VersionAddition[0])
428                 strlcat(NGIRCd_VersionAddition, "+",
429                         sizeof NGIRCd_VersionAddition);
430         strlcat(NGIRCd_VersionAddition, "ZLIB",
431                 sizeof NGIRCd_VersionAddition);
432 #endif
433         if (NGIRCd_VersionAddition[0])
434                 strlcat(NGIRCd_VersionAddition, "-",
435                         sizeof(NGIRCd_VersionAddition));
436
437         strlcat(NGIRCd_VersionAddition, HOST_CPU,
438                 sizeof(NGIRCd_VersionAddition));
439         strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
440         strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
441                 sizeof(NGIRCd_VersionAddition));
442         strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
443         strlcat(NGIRCd_VersionAddition, HOST_OS,
444                 sizeof(NGIRCd_VersionAddition));
445
446         snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
447                  PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
448 } /* Fill_Version */
449
450
451 /**
452  * Display copyright and version information of ngIRCd on the console.
453  */
454 static void
455 Show_Version( void )
456 {
457         puts( NGIRCd_Version );
458         puts( "Copyright (c)2001-2024 Alexander Barton (<alex@barton.de>) and Contributors." );
459         puts( "Homepage: <http://ngircd.barton.de/>\n" );
460         puts( "This is free software; see the source for copying conditions. There is NO" );
461         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
462 } /* Show_Version */
463
464
465 /**
466  * Display a short help text on the console.
467  * This help depends on the configuration of the executable and only shows
468  * options that are actually enabled.
469  */
470 static void
471 Show_Help( void )
472 {
473         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" );
477 #ifdef SNIFFER
478         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
479 #endif
480         puts( "  -t, --configtest   read, validate and display configuration; then exit" );
481         puts( "  -V, --version      output version information and exit" );
482 #ifdef SYSLOG
483         puts( "  -y, --syslog       log to syslog even when running in the foreground (-n)" );
484 #endif
485         puts( "  -h, --help         display this help and exit" );
486 } /* Show_Help */
487
488
489 /**
490  * Delete the file containing the process ID (PID).
491  */
492 static void
493 Pidfile_Delete( void )
494 {
495         /* Pidfile configured? */
496         if( ! Conf_PidFile[0] ) return;
497
498         LogDebug( "Removing PID file (%s) ...", Conf_PidFile );
499
500         if( unlink( Conf_PidFile ))
501                 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
502 } /* Pidfile_Delete */
503
504
505 /**
506  * Create the file containing the process ID of ngIRCd ("PID file").
507  *
508  * @param pid   The process ID to be stored in this file.
509  */
510 static void
511 Pidfile_Create(pid_t pid)
512 {
513         int pidfd;
514         char pidbuf[64];
515         int len;
516
517         /* Pidfile configured? */
518         if( ! Conf_PidFile[0] ) return;
519
520         LogDebug( "Creating PID file (%s) ...", Conf_PidFile );
521
522         pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
523         if ( pidfd < 0 ) {
524                 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
525                 return;
526         }
527
528         len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
529         if (len < 0 || len >= (int)sizeof pidbuf) {
530                 Log(LOG_ERR, "Error converting process ID!");
531                 close(pidfd);
532                 return;
533         }
534
535         if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
536                 Log(LOG_ERR, "Can't write PID file (%s): %s!", Conf_PidFile,
537                     strerror(errno));
538
539         if (close(pidfd) != 0)
540                 Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
541                     strerror(errno));
542 } /* Pidfile_Create */
543
544
545 /**
546  * Redirect stdin, stdout and stderr to appropriate file handles.
547  *
548  * @param fd    The file handle stdin, stdout and stderr should be redirected to.
549  */
550 static void
551 Setup_FDStreams(int fd)
552 {
553         if (fd < 0)
554                 return;
555
556         fflush(stdout);
557         fflush(stderr);
558
559         /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
560         dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
561 } /* Setup_FDStreams */
562
563
564 #if !defined(SINGLE_USER_OS)
565
566 /**
567  * Get user and group ID of unprivileged "nobody" user.
568  *
569  * @param uid   User ID
570  * @param gid   Group ID
571  * @return      true on success.
572  */
573 static bool
574 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
575 {
576         struct passwd *pwd;
577
578 #ifdef __CYGWIN__
579         /* Cygwin kludge.
580          * It can return EINVAL instead of EPERM
581          * so, if we are already unprivileged,
582          * use id of current user.
583          */
584         if (geteuid() && getuid()) {
585                 *uid = getuid();
586                 *gid = getgid();
587                 return true;
588         }
589 #endif
590
591         pwd = getpwnam("nobody");
592         if (!pwd)
593                 return false;
594
595         if (!pwd->pw_uid || !pwd->pw_gid)
596                 return false;
597
598         *uid = pwd->pw_uid;
599         *gid = pwd->pw_gid;
600         endpwent();
601
602         return true;
603 } /* NGIRCd_getNobodyID */
604
605 #endif
606
607
608 #ifdef HAVE_ARC4RANDOM
609 static void
610 Random_Init(void)
611 {
612
613 }
614 #else
615 static bool
616 Random_Init_Kern(const char *file)
617 {
618         unsigned int seed;
619         bool ret = false;
620         int fd = open(file, O_RDONLY);
621         if (fd >= 0) {
622                 if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
623                         ret = true;
624                 close(fd);
625                 srand(seed);
626         }
627         return ret;
628 }
629
630 /**
631  * Initialize libc rand(3) number generator
632  */
633 static void
634 Random_Init(void)
635 {
636         if (Random_Init_Kern("/dev/urandom"))
637                 return;
638         if (Random_Init_Kern("/dev/random"))
639                 return;
640         if (Random_Init_Kern("/dev/arandom"))
641                 return;
642         srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
643 }
644 #endif
645
646
647 /**
648  * Initialize ngIRCd daemon.
649  *
650  * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
651  *              foreground (and not as a daemon).
652  * @return true on success.
653  */
654 static bool
655 NGIRCd_Init(bool NGIRCd_NoDaemon)
656 {
657         static bool initialized;
658         bool chrooted = false;
659         struct passwd *pwd;
660         struct group *grp;
661         int real_errno, fd = -1;
662         pid_t pid;
663
664         if (initialized)
665                 return true;
666
667         if (!NGIRCd_NoDaemon) {
668                 /* open /dev/null before chroot() */
669                 fd = open( "/dev/null", O_RDWR);
670                 if (fd < 0)
671                         Log(LOG_WARNING, "Could not open /dev/null: %s",
672                             strerror(errno));
673         }
674
675         /* SSL initialization */
676         if (!ConnSSL_InitLibrary()) {
677                 Log(LOG_ERR, "Error during SSL initialization!");
678                 goto out;
679         }
680
681         /* Change root */
682         if (Conf_Chroot[0]) {
683                 if (chdir(Conf_Chroot) != 0) {
684                         Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
685                             Conf_Chroot, strerror(errno));
686                         goto out;
687                 }
688
689                 if (chroot(Conf_Chroot) != 0) {
690                         Log(LOG_ERR,
691                             "Can't change root directory to \"%s\": %s!",
692                             Conf_Chroot, strerror(errno));
693                         goto out;
694                 } else {
695                         chrooted = true;
696                         Log(LOG_INFO,
697                             "Changed root and working directory to \"%s\".",
698                             Conf_Chroot);
699                 }
700         }
701
702 #if !defined(SINGLE_USER_OS)
703         /* Check user ID */
704         if (Conf_UID == 0) {
705                 pwd = getpwuid(0);
706                 Log(LOG_INFO,
707                     "ServerUID must not be %s(0), using \"nobody\" instead.",
708                     pwd ? pwd->pw_name : "?");
709                 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
710                         Log(LOG_WARNING,
711                             "Could not get user/group ID of user \"nobody\": %s",
712                             errno ? strerror(errno) : "not found" );
713                         goto out;
714                 }
715         }
716
717         /* Change group ID */
718         if (getgid() != Conf_GID) {
719                 if (setgid(Conf_GID) != 0) {
720                         real_errno = errno;
721                         grp = getgrgid(Conf_GID);
722                         Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
723                             grp ? grp->gr_name : "?", Conf_GID,
724                             strerror(real_errno));
725                         if (real_errno != EPERM && real_errno != EINVAL)
726                                 goto out;
727                 }
728 #ifdef HAVE_SETGROUPS
729                 if (setgroups(0, NULL) != 0) {
730                         real_errno = errno;
731                         Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
732                                         strerror(errno));
733                         if (real_errno != EPERM)
734                                 goto out;
735                 }
736 #else
737                 Log(LOG_WARNING,
738                     "Can't drop supplementary group IDs: setgroups(3) missing!");
739 #endif
740         }
741 #endif
742
743         /* Change user ID */
744         if (getuid() != Conf_UID) {
745                 if (setuid(Conf_UID) != 0) {
746                         real_errno = errno;
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 && real_errno != EINVAL)
752                                 goto out;
753                 }
754         }
755
756         initialized = true;
757
758         /* Normally a child process is forked which isn't any longer
759          * connected to the controlling terminal. Use "--nodaemon"
760          * to disable this "daemon mode" (useful for debugging). */
761         if (!NGIRCd_NoDaemon) {
762                 pid = fork();
763                 if (pid > 0) {
764                         /* "Old" process: exit. */
765                         exit(0);
766                 }
767                 if (pid < 0) {
768                         /* Error!? */
769                         fprintf(stderr,
770                                 "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
771                                 PACKAGE_NAME, strerror(errno));
772                         exit(1);
773                 }
774
775                 /* New child process */
776 #ifdef HAVE_SETSID
777                 (void)setsid();
778 #else
779                 setpgrp(0, getpid());
780 #endif
781                 if (chdir("/") != 0)
782                         Log(LOG_ERR, "Can't change directory to '/': %s!",
783                                      strerror(errno));
784
785                 /* Detach stdin, stdout and stderr */
786                 Setup_FDStreams(fd);
787                 if (fd > 2)
788                         close(fd);
789         }
790         pid = getpid();
791
792         Pidfile_Create(pid);
793
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. */
796         Conf_UID = getuid();
797         Conf_GID = getgid();
798
799         pwd = getpwuid(Conf_UID);
800         grp = getgrgid(Conf_GID);
801
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);
805
806         if (chrooted) {
807                 Log(LOG_INFO, "Running with root directory \"%s\".",
808                     Conf_Chroot );
809                 return true;
810         } else
811                 Log(LOG_INFO, "Not running with changed root directory.");
812
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) */
815
816         if (NGIRCd_NoDaemon)
817                 return true;
818
819         if (pwd) {
820                 if (chdir(pwd->pw_dir) == 0)
821                         LogDebug(
822                             "Changed working directory to \"%s\" ...",
823                             pwd->pw_dir);
824                 else
825                         Log(LOG_ERR,
826                             "Can't change working directory to \"%s\": %s!",
827                             pwd->pw_dir, strerror(errno));
828         } else
829                 Log(LOG_ERR, "Can't get user information for UID %d!?", Conf_UID);
830
831         return true;
832  out:
833         if (fd > 2)
834                 close(fd);
835         return false;
836 } /* NGIRCd_Init */
837
838
839 /* -eof- */