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