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