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