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