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