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