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