]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
Don't log "ngIRCd hello message" two times
[ngircd-alex.git] / src / ngircd / conf.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  * Configuration management (reading, parsing & validation)
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #ifdef PROTOTYPES
23 #       include <stdarg.h>
24 #else
25 #       include <varargs.h>
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <unistd.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #ifdef HAVE_CTYPE_H
38 # include <ctype.h>
39 #endif
40
41 #include "array.h"
42 #include "ngircd.h"
43 #include "conn.h"
44 #include "channel.h"
45 #include "defines.h"
46 #include "log.h"
47 #include "match.h"
48 #include "tool.h"
49
50 #include "exp.h"
51 #include "conf.h"
52
53
54 static bool Use_Log = true, Using_MotdFile = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
57
58 static char Conf_MotdFile[FNAME_LEN];
59
60 static void Set_Defaults PARAMS(( bool InitServers ));
61 static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
62 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
63
64 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
68 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
69 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
70
71 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
72
73 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
74 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
75                                           const char *Section ));
76 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
77
78 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
79
80
81 #ifdef WANT_IPV6
82 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
83 #else
84 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
85 #endif
86
87
88 #ifdef SSL_SUPPORT
89
90 static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
91
92 struct SSLOptions Conf_SSLOptions;
93
94 /**
95  * Initialize SSL configuration.
96  */
97 static void
98 ConfSSL_Init(void)
99 {
100         free(Conf_SSLOptions.KeyFile);
101         Conf_SSLOptions.KeyFile = NULL;
102
103         free(Conf_SSLOptions.CertFile);
104         Conf_SSLOptions.CertFile = NULL;
105
106         free(Conf_SSLOptions.DHFile);
107         Conf_SSLOptions.DHFile = NULL;
108         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
109 }
110
111 /**
112  * Make sure that a configured file is readable.
113  *
114  * Currently, this function is only used for SSL-related options ...
115  *
116  * @param Var Configuration variable
117  * @param Filename Configured filename
118  */
119 static void
120 CheckFileReadable(const char *Var, const char *Filename)
121 {
122         FILE *fp;
123
124         if (!Filename)
125                 return;
126
127         fp = fopen(Filename, "r");
128         if (fp)
129                 fclose(fp);
130         else
131                 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
132                              Filename, Var, strerror(errno));
133 }
134
135 #endif
136
137
138 /**
139  * Duplicate string and warn on errors.
140  *
141  * @returns Pointer to string on success, NULL otherwise.
142  */
143 static char *
144 strdup_warn(const char *str)
145 {
146         char *ptr = strdup(str);
147         if (!ptr)
148                 Config_Error(LOG_ERR,
149                              "Could not allocate memory for string: %s", str);
150         return ptr;
151 }
152
153 /**
154  * Output a comma separated list of ports (integer values).
155  */
156 static void
157 ports_puts(array *a)
158 {
159         size_t len;
160         UINT16 *ports;
161         len = array_length(a, sizeof(UINT16));
162         if (len--) {
163                 ports = (UINT16*) array_start(a);
164                 printf("%u", (unsigned int) *ports);
165                 while (len--) {
166                         ports++;
167                         printf(", %u", (unsigned int) *ports);
168                 }
169         }
170         putc('\n', stdout);
171 }
172
173 /**
174  * Parse a comma separated string into an array of port numbers (integers).
175  */
176 static void
177 ports_parse(array *a, int Line, char *Arg)
178 {
179         char *ptr;
180         int port;
181         UINT16 port16;
182
183         array_trunc(a);
184
185         ptr = strtok( Arg, "," );
186         while (ptr) {
187                 ngt_TrimStr(ptr);
188                 port = atoi(ptr);
189                 if (port > 0 && port < 0xFFFF) {
190                         port16 = (UINT16) port;
191                         if (!array_catb(a, (char*)&port16, sizeof port16))
192                                 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
193                                                         NGIRCd_ConfFile, Line, port, strerror(errno));
194                 } else {
195                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
196                                                                         NGIRCd_ConfFile, Line, port );
197                 }
198
199                 ptr = strtok( NULL, "," );
200         }
201 }
202
203 /**
204  * Initialize configuration module.
205  */
206 GLOBAL void
207 Conf_Init( void )
208 {
209         Read_Config(false, true);
210         Validate_Config(false, false);
211 }
212
213 /**
214  * "Rehash" (reload) server configuration.
215  *
216  * @returns true if configuration has been re-read, false on errors.
217  */
218 GLOBAL bool
219 Conf_Rehash( void )
220 {
221         if (!Read_Config(false, false))
222                 return false;
223         Validate_Config(false, true);
224
225         /* Update CLIENT structure of local server */
226         Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
227         return true;
228 }
229
230 /**
231  * Output a boolean value as "yes/no" string.
232  */
233 static const char*
234 yesno_to_str(int boolean_value)
235 {
236         if (boolean_value)
237                 return "yes";
238         return "no";
239 }
240
241 /**
242  * Free all IRC operator configuration structures.
243  */
244 static void
245 opers_free(void)
246 {
247         struct Conf_Oper *op;
248         size_t len;
249
250         len = array_length(&Conf_Opers, sizeof(*op));
251         op = array_start(&Conf_Opers);
252         while (len--) {
253                 free(op->mask);
254                 op++;
255         }
256         array_free(&Conf_Opers);
257 }
258
259 /**
260  * Output all IRC operator configuration structures.
261  */
262 static void
263 opers_puts(void)
264 {
265         struct Conf_Oper *op;
266         size_t count, i;
267
268         count = array_length(&Conf_Opers, sizeof(*op));
269         op = array_start(&Conf_Opers);
270         for (i = 0; i < count; i++, op++) {
271                 if (!op->name[0])
272                         continue;
273
274                 puts("[OPERATOR]");
275                 printf("  Name = %s\n", op->name);
276                 printf("  Password = %s\n", op->pwd);
277                 printf("  Mask = %s\n\n", op->mask ? op->mask : "");
278         }
279 }
280
281 /**
282  * Read configuration, validate and output it.
283  *
284  * This function waits for a keypress of the user when stdin/stdout are valid
285  * tty's ("you can read our nice message and we can read in your keypress").
286  *
287  * @return      0 on succes, 1 on failure(s); therefore the result code can
288  *              directly be used by exit() when running "ngircd --configtest".
289  */
290 GLOBAL int
291 Conf_Test( void )
292 {
293         struct passwd *pwd;
294         struct group *grp;
295         unsigned int i;
296         bool config_valid;
297         size_t predef_channel_count;
298         struct Conf_Channel *predef_chan;
299
300         Use_Log = false;
301
302         if (!Read_Config(true, true))
303                 return 1;
304
305         config_valid = Validate_Config(true, false);
306
307         /* Valid tty? */
308         if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
309                 puts("OK, press enter to see a dump of your server configuration ...");
310                 getchar();
311         } else
312                 puts("Ok, dump of your server configuration follows:\n");
313
314         puts("[GLOBAL]");
315         printf("  Name = %s\n", Conf_ServerName);
316         printf("  AdminInfo1 = %s\n", Conf_ServerAdmin1);
317         printf("  AdminInfo2 = %s\n", Conf_ServerAdmin2);
318         printf("  AdminEMail = %s\n", Conf_ServerAdminMail);
319         printf("  Info = %s\n", Conf_ServerInfo);
320         printf("  Listen = %s\n", Conf_ListenAddress);
321         if (Using_MotdFile) {
322                 printf("  MotdFile = %s\n", Conf_MotdFile);
323                 printf("  MotdPhrase =\n");
324         } else {
325                 printf("  MotdFile = \n");
326                 printf("  MotdPhrase = %s\n", array_bytes(&Conf_Motd)
327                        ? (const char*) array_start(&Conf_Motd) : "");
328         }
329 #ifndef PAM
330         printf("  Password = %s\n", Conf_ServerPwd);
331 #endif
332         printf("  PidFile = %s\n", Conf_PidFile);
333         printf("  Ports = ");
334         ports_puts(&Conf_ListenPorts);
335         grp = getgrgid(Conf_GID);
336         if (grp)
337                 printf("  ServerGID = %s\n", grp->gr_name);
338         else
339                 printf("  ServerGID = %ld\n", (long)Conf_GID);
340         pwd = getpwuid(Conf_UID);
341         if (pwd)
342                 printf("  ServerUID = %s\n", pwd->pw_name);
343         else
344                 printf("  ServerUID = %ld\n", (long)Conf_UID);
345         puts("");
346
347         puts("[LIMITS]");
348         printf("  ConnectRetry = %d\n", Conf_ConnectRetry);
349         printf("  MaxConnections = %ld\n", Conf_MaxConnections);
350         printf("  MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
351         printf("  MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
352         printf("  MaxNickLength = %u\n", Conf_MaxNickLength - 1);
353         printf("  PingTimeout = %d\n", Conf_PingTimeout);
354         printf("  PongTimeout = %d\n", Conf_PongTimeout);
355         puts("");
356
357         puts("[OPTIONS]");
358         printf("  AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
359         printf("  ChrootDir = %s\n", Conf_Chroot);
360         printf("  CloakHost = %s\n", Conf_CloakHost);
361         printf("  CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
362 #ifdef WANT_IPV6
363         printf("  ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
364         printf("  ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
365 #endif
366         printf("  DNS = %s\n", yesno_to_str(Conf_DNS));
367 #ifdef IDENT
368         printf("  Ident = %s\n", yesno_to_str(Conf_Ident));
369 #endif
370         printf("  MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
371         printf("  NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
372         printf("  OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
373         printf("  OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
374 #ifdef PAM
375         printf("  PAM = %s\n", yesno_to_str(Conf_PAM));
376         printf("  PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
377 #endif
378         printf("  PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
379 #ifndef STRICT_RFC
380         printf("  RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
381 #endif
382         printf("  ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
383 #ifdef SYSLOG
384         printf("  SyslogFacility = %s\n",
385                ngt_SyslogFacilityName(Conf_SyslogFacility));
386 #endif
387         printf("  WebircPassword = %s\n", Conf_WebircPwd);
388         puts("");
389
390 #ifdef SSL_SUPPORT
391         puts("[SSL]");
392         printf("  CertFile = %s\n", Conf_SSLOptions.CertFile
393                                         ? Conf_SSLOptions.CertFile : "");
394         printf("  DHFile = %s\n", Conf_SSLOptions.DHFile
395                                         ? Conf_SSLOptions.DHFile : "");
396         printf("  KeyFile = %s\n", Conf_SSLOptions.KeyFile
397                                         ? Conf_SSLOptions.KeyFile : "");
398         if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
399                 puts("  KeyFilePassword = <secret>");
400         else
401                 puts("  KeyFilePassword = ");
402         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
403         printf("  Ports = ");
404         ports_puts(&Conf_SSLOptions.ListenPorts);
405         puts("");
406 #endif
407
408         opers_puts();
409
410         for( i = 0; i < MAX_SERVERS; i++ ) {
411                 if( ! Conf_Server[i].name[0] ) continue;
412
413                 /* Valid "Server" section */
414                 puts( "[SERVER]" );
415                 printf( "  Name = %s\n", Conf_Server[i].name );
416                 printf( "  Host = %s\n", Conf_Server[i].host );
417                 printf( "  Port = %u\n", (unsigned int)Conf_Server[i].port );
418 #ifdef SSL_SUPPORT
419                 printf( "  SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
420 #endif
421                 printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
422                 printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
423                 printf( "  ServiceMask = %s\n", Conf_Server[i].svs_mask);
424                 printf( "  Group = %d\n", Conf_Server[i].group );
425                 printf( "  Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
426         }
427
428         predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
429         predef_chan = array_start(&Conf_Channels);
430
431         for (i = 0; i < predef_channel_count; i++, predef_chan++) {
432                 if (!predef_chan->name[0])
433                         continue;
434
435                 /* Valid "Channel" section */
436                 puts( "[CHANNEL]" );
437                 printf("  Name = %s\n", predef_chan->name);
438                 printf("  Modes = %s\n", predef_chan->modes);
439                 printf("  Key = %s\n", predef_chan->key);
440                 printf("  MaxUsers = %lu\n", predef_chan->maxusers);
441                 printf("  Topic = %s\n", predef_chan->topic);
442                 printf("  KeyFile = %s\n\n", predef_chan->keyfile);
443         }
444
445         return (config_valid ? 0 : 1);
446 }
447
448 /**
449  * Remove connection information from configured server.
450  *
451  * If the server is set as "once", delete it from our configuration;
452  * otherwise set the time for the next connection attempt.
453  *
454  * Non-server connections will be silently ignored.
455  */
456 GLOBAL void
457 Conf_UnsetServer( CONN_ID Idx )
458 {
459         int i;
460         time_t t;
461
462         /* Check all our configured servers */
463         for( i = 0; i < MAX_SERVERS; i++ ) {
464                 if( Conf_Server[i].conn_id != Idx ) continue;
465
466                 /* Gotcha! Mark server configuration as "unused": */
467                 Conf_Server[i].conn_id = NONE;
468
469                 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
470                         /* Delete configuration here */
471                         Init_Server_Struct( &Conf_Server[i] );
472                 } else {
473                         /* Set time for next connect attempt */
474                         t = time(NULL);
475                         if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
476                                 /* The connection has been "long", so we don't
477                                  * require the next attempt to be delayed. */
478                                 Conf_Server[i].lasttry =
479                                         t - Conf_ConnectRetry + RECONNECT_DELAY;
480                         } else
481                                 Conf_Server[i].lasttry = t;
482                 }
483         }
484 }
485
486 /**
487  * Set connection information for specified configured server.
488  */
489 GLOBAL void
490 Conf_SetServer( int ConfServer, CONN_ID Idx )
491 {
492         assert( ConfServer > NONE );
493         assert( Idx > NONE );
494
495         Conf_Server[ConfServer].conn_id = Idx;
496 }
497
498 /**
499  * Get index of server in configuration structure.
500  */
501 GLOBAL int
502 Conf_GetServer( CONN_ID Idx )
503 {
504         int i = 0;
505
506         assert( Idx > NONE );
507
508         for( i = 0; i < MAX_SERVERS; i++ ) {
509                 if( Conf_Server[i].conn_id == Idx ) return i;
510         }
511         return NONE;
512 }
513
514 /**
515  * Enable a server by name and adjust its port number.
516  *
517  * @returns     true if a server has been enabled and now has a valid port
518  *              number and host name for outgoing connections.
519  */
520 GLOBAL bool
521 Conf_EnableServer( const char *Name, UINT16 Port )
522 {
523         int i;
524
525         assert( Name != NULL );
526         for( i = 0; i < MAX_SERVERS; i++ ) {
527                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
528                         /* Gotcha! Set port and enable server: */
529                         Conf_Server[i].port = Port;
530                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
531                         return (Conf_Server[i].port && Conf_Server[i].host[0]);
532                 }
533         }
534         return false;
535 }
536
537 /**
538  * Enable a server by name.
539  *
540  * The server is only usable as outgoing server, if it has set a valid port
541  * number for outgoing connections!
542  * If not, you have to use Conf_EnableServer() function to make it available.
543  *
544  * @returns     true if a server has been enabled; false otherwise.
545  */
546 GLOBAL bool
547 Conf_EnablePassiveServer(const char *Name)
548 {
549         int i;
550
551         assert( Name != NULL );
552         for (i = 0; i < MAX_SERVERS; i++) {
553                 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
554                     && (Conf_Server[i].port > 0)) {
555                         /* BINGO! Enable server */
556                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
557                         return true;
558                 }
559         }
560         return false;
561 }
562
563 /**
564  * Disable a server by name.
565  * An already established connection will be disconnected.
566  *
567  * @returns     true if a server was found and has been disabled.
568  */
569 GLOBAL bool
570 Conf_DisableServer( const char *Name )
571 {
572         int i;
573
574         assert( Name != NULL );
575         for( i = 0; i < MAX_SERVERS; i++ ) {
576                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
577                         /* Gotcha! Disable and disconnect server: */
578                         Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
579                         if( Conf_Server[i].conn_id > NONE )
580                                 Conn_Close(Conf_Server[i].conn_id, NULL,
581                                            "Server link terminated on operator request",
582                                            true);
583                         return true;
584                 }
585         }
586         return false;
587 }
588
589 /**
590  * Add a new remote server to our configuration.
591  *
592  * @param Name          Name of the new server.
593  * @param Port          Port number to connect to or 0 for incoming connections.
594  * @param Host          Host name to connect to.
595  * @param MyPwd         Password that will be sent to the peer.
596  * @param PeerPwd       Password that must be received from the peer.
597  * @returns             true if the new server has been added; false otherwise.
598  */
599 GLOBAL bool
600 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
601                const char *MyPwd, const char *PeerPwd)
602 {
603         int i;
604
605         assert( Name != NULL );
606         assert( Host != NULL );
607         assert( MyPwd != NULL );
608         assert( PeerPwd != NULL );
609
610         /* Search unused item in server configuration structure */
611         for( i = 0; i < MAX_SERVERS; i++ ) {
612                 /* Is this item used? */
613                 if( ! Conf_Server[i].name[0] ) break;
614         }
615         if( i >= MAX_SERVERS ) return false;
616
617         Init_Server_Struct( &Conf_Server[i] );
618         strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
619         strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
620         strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
621         strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
622         Conf_Server[i].port = Port;
623         Conf_Server[i].flags = CONF_SFLAG_ONCE;
624
625         return true;
626 }
627
628 /**
629  * Check if the given nick name is an service.
630  *
631  * @returns true if the given nick name belongs to an "IRC service".
632  */
633 GLOBAL bool
634 Conf_IsService(int ConfServer, const char *Nick)
635 {
636         return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
637 }
638
639 /**
640  * Initialize configuration settings with their default values.
641  */
642 static void
643 Set_Defaults(bool InitServers)
644 {
645         int i;
646
647         /* Global */
648         strcpy(Conf_ServerName, "");
649         strcpy(Conf_ServerAdmin1, "");
650         strcpy(Conf_ServerAdmin2, "");
651         strcpy(Conf_ServerAdminMail, "");
652         snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
653                  PACKAGE_NAME, PACKAGE_VERSION);
654         free(Conf_ListenAddress);
655         Conf_ListenAddress = NULL;
656         array_free(&Conf_Motd);
657         strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
658         strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
659         strcpy(Conf_ServerPwd, "");
660         strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
661         Conf_UID = Conf_GID = 0;
662
663         /* Limits */
664         Conf_ConnectRetry = 60;
665         Conf_MaxConnections = 0;
666         Conf_MaxConnectionsIP = 5;
667         Conf_MaxJoins = 10;
668         Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
669         Conf_PingTimeout = 120;
670         Conf_PongTimeout = 20;
671
672         /* Options */
673         Conf_AllowRemoteOper = false;
674 #ifndef STRICT_RFC
675         Conf_AuthPing = false;
676 #endif
677         strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
678         strcpy(Conf_CloakHost, "");
679         Conf_CloakUserToNick = false;
680         Conf_ConnectIPv4 = true;
681 #ifdef WANT_IPV6
682         Conf_ConnectIPv6 = true;
683 #else
684         Conf_ConnectIPv6 = false;
685 #endif
686         Conf_DNS = true;
687 #ifdef IDENTAUTH
688         Conf_Ident = true;
689 #else
690         Conf_Ident = false;
691 #endif
692         Conf_MorePrivacy = false;
693         Conf_NoticeAuth = false;
694         Conf_OperCanMode = false;
695         Conf_OperServerMode = false;
696 #ifdef PAM
697         Conf_PAM = true;
698 #else
699         Conf_PAM = false;
700 #endif
701         Conf_PAMIsOptional = false;
702         Conf_PredefChannelsOnly = false;
703 #ifdef SYSLOG
704         Conf_ScrubCTCP = false;
705 #ifdef LOG_LOCAL5
706         Conf_SyslogFacility = LOG_LOCAL5;
707 #else
708         Conf_SyslogFacility = 0;
709 #endif
710 #endif
711
712         /* Initialize server configuration structures */
713         if (InitServers) {
714                 for (i = 0; i < MAX_SERVERS;
715                      Init_Server_Struct(&Conf_Server[i++]));
716         }
717 }
718
719 /**
720  * Get number of configured listening ports.
721  *
722  * @returns The number of ports (IPv4+IPv6) on which the server should listen.
723  */
724 static bool
725 no_listenports(void)
726 {
727         size_t cnt = array_bytes(&Conf_ListenPorts);
728 #ifdef SSL_SUPPORT
729         cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
730 #endif
731         return cnt == 0;
732 }
733
734 /**
735  * Read MOTD ("message of the day") file.
736  *
737  * @param filename      Name of the file to read.
738  */
739 static void
740 Read_Motd(const char *filename)
741 {
742         char line[127];
743         FILE *fp;
744
745         if (*filename == '\0')
746                 return;
747
748         fp = fopen(filename, "r");
749         if (!fp) {
750                 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
751                                         filename, strerror(errno));
752                 return;
753         }
754
755         array_free(&Conf_Motd);
756         Using_MotdFile = true;
757
758         while (fgets(line, (int)sizeof line, fp)) {
759                 ngt_TrimLastChr( line, '\n');
760
761                 /* add text including \0 */
762                 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
763                         Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
764                         break;
765                 }
766         }
767         fclose(fp);
768 }
769
770 /**
771  * Read ngIRCd configuration file.
772  *
773  * Please note that this function uses exit(1) on fatal errors and therefore
774  * can result in ngIRCd terminating!
775  *
776  * @param ngircd_starting       Flag indicating if ngIRCd is starting or not.
777  * @returns                     true when the configuration file has been read
778  *                              successfully; false otherwise.
779  */
780 static bool
781 Read_Config(bool TestOnly, bool IsStarting)
782 {
783         char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
784         const UINT16 defaultport = 6667;
785         int line, i, n;
786         size_t count;
787         FILE *fd;
788
789         /* Open configuration file */
790         fd = fopen( NGIRCd_ConfFile, "r" );
791         if( ! fd ) {
792                 /* No configuration file found! */
793                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
794                                         NGIRCd_ConfFile, strerror( errno ));
795                 if (!IsStarting)
796                         return false;
797                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
798                 exit( 1 );
799         }
800
801         opers_free();
802         Set_Defaults(IsStarting);
803
804         if (TestOnly)
805                 Config_Error(LOG_INFO,
806                              "Reading configuration from \"%s\" ...",
807                              NGIRCd_ConfFile );
808
809         /* Clean up server configuration structure: mark all already
810          * configured servers as "once" so that they are deleted
811          * after the next disconnect and delete all unused servers.
812          * And delete all servers which are "duplicates" of servers
813          * that are already marked as "once" (such servers have been
814          * created by the last rehash but are now useless). */
815         for( i = 0; i < MAX_SERVERS; i++ ) {
816                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
817                 else {
818                         /* This structure is in use ... */
819                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
820                                 /* Check for duplicates */
821                                 for( n = 0; n < MAX_SERVERS; n++ ) {
822                                         if( n == i ) continue;
823
824                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
825                                                 Init_Server_Struct( &Conf_Server[n] );
826 #ifdef DEBUG
827                                                 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
828                                                                                                 n, i );
829 #endif
830                                         }
831                                 }
832                         } else {
833                                 /* Mark server as "once" */
834                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
835                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
836                         }
837                 }
838         }
839
840         /* Initialize variables */
841         line = 0;
842         strcpy( section, "" );
843         Init_Server_Struct( &New_Server );
844         New_Server_Idx = NONE;
845 #ifdef SSL_SUPPORT
846         ConfSSL_Init();
847 #endif
848         /* Read configuration file */
849         while( true ) {
850                 if( ! fgets( str, LINE_LEN, fd )) break;
851                 ngt_TrimStr( str );
852                 line++;
853
854                 /* Skip comments and empty lines */
855                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
856
857                 /* Is this the beginning of a new section? */
858                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
859                         strlcpy( section, str, sizeof( section ));
860                         if (strcasecmp(section, "[GLOBAL]") == 0
861                             || strcasecmp(section, "[LIMITS]") == 0
862                             || strcasecmp(section, "[OPTIONS]") == 0
863 #ifdef SSL_SUPPORT
864                             || strcasecmp(section, "[SSL]") == 0
865 #endif
866                             )
867                                 continue;
868
869                         if( strcasecmp( section, "[SERVER]" ) == 0 ) {
870                                 /* Check if there is already a server to add */
871                                 if( New_Server.name[0] ) {
872                                         /* Copy data to "real" server structure */
873                                         assert( New_Server_Idx > NONE );
874                                         Conf_Server[New_Server_Idx] = New_Server;
875                                 }
876
877                                 /* Re-init structure for new server */
878                                 Init_Server_Struct( &New_Server );
879
880                                 /* Search unused item in server configuration structure */
881                                 for( i = 0; i < MAX_SERVERS; i++ ) {
882                                         /* Is this item used? */
883                                         if( ! Conf_Server[i].name[0] ) break;
884                                 }
885                                 if( i >= MAX_SERVERS ) {
886                                         /* Oops, no free item found! */
887                                         Config_Error( LOG_ERR, "Too many servers configured." );
888                                         New_Server_Idx = NONE;
889                                 }
890                                 else New_Server_Idx = i;
891                                 continue;
892                         }
893
894                         if (strcasecmp(section, "[CHANNEL]") == 0) {
895                                 count = array_length(&Conf_Channels,
896                                                      sizeof(struct Conf_Channel));
897                                 if (!array_alloc(&Conf_Channels,
898                                                  sizeof(struct Conf_Channel),
899                                                  count)) {
900                                         Config_Error(LOG_ERR,
901                                                      "Could not allocate memory for new operator (line %d)",
902                                                      line);
903                                 }
904                                 continue;
905                         }
906
907                         if (strcasecmp(section, "[OPERATOR]") == 0) {
908                                 count = array_length(&Conf_Opers,
909                                                      sizeof(struct Conf_Oper));
910                                 if (!array_alloc(&Conf_Opers,
911                                                  sizeof(struct Conf_Oper),
912                                                  count)) {
913                                         Config_Error(LOG_ERR,
914                                                      "Could not allocate memory for new channel (line &d)",
915                                                      line);
916                                 }
917                                 continue;
918                         }
919
920                         Config_Error(LOG_ERR,
921                                      "%s, line %d: Unknown section \"%s\"!",
922                                      NGIRCd_ConfFile, line, section);
923                         section[0] = 0x1;
924                 }
925                 if( section[0] == 0x1 ) continue;
926
927                 /* Split line into variable name and parameters */
928                 ptr = strchr( str, '=' );
929                 if( ! ptr ) {
930                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
931                         continue;
932                 }
933                 *ptr = '\0';
934                 var = str; ngt_TrimStr( var );
935                 arg = ptr + 1; ngt_TrimStr( arg );
936
937                 if(strcasecmp(section, "[GLOBAL]") == 0)
938                         Handle_GLOBAL(line, var, arg);
939                 else if(strcasecmp(section, "[LIMITS]") == 0)
940                         Handle_LIMITS(line, var, arg);
941                 else if(strcasecmp(section, "[OPTIONS]") == 0)
942                         Handle_OPTIONS(line, var, arg);
943 #ifdef SSL_SUPPORT
944                 else if(strcasecmp(section, "[SSL]") == 0)
945                         Handle_SSL(line, var, arg);
946 #endif
947                 else if(strcasecmp(section, "[OPERATOR]") == 0)
948                         Handle_OPERATOR(line, var, arg);
949                 else if(strcasecmp(section, "[SERVER]") == 0)
950                         Handle_SERVER(line, var, arg);
951                 else if(strcasecmp(section, "[CHANNEL]") == 0)
952                         Handle_CHANNEL(line, var, arg);
953                 else
954                         Config_Error(LOG_ERR,
955                                      "%s, line %d: Variable \"%s\" outside section!",
956                                      NGIRCd_ConfFile, line, var);
957         }
958
959         /* Close configuration file */
960         fclose( fd );
961
962         /* Check if there is still a server to add */
963         if( New_Server.name[0] ) {
964                 /* Copy data to "real" server structure */
965                 assert( New_Server_Idx > NONE );
966                 Conf_Server[New_Server_Idx] = New_Server;
967         }
968
969         /* not a single listening port? Add default. */
970         if (no_listenports() &&
971                 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
972         {
973                 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
974                                         (unsigned int) defaultport, strerror(errno));
975
976                 exit(1);
977         }
978
979         if (!Conf_ListenAddress)
980                 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
981
982         if (!Conf_ListenAddress) {
983                 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
984                 exit(1);
985         }
986
987         /* No MOTD phrase configured? (re)try motd file. */
988         if (array_bytes(&Conf_Motd) == 0)
989                 Read_Motd(Conf_MotdFile);
990
991 #ifdef SSL_SUPPORT
992         /* Make sure that all SSL-related files are readable */
993         CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
994         CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
995         CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
996 #endif
997
998         return true;
999 }
1000
1001 /**
1002  * Check whether a string argument is "true" or "false".
1003  *
1004  * @param Arg   Input string.
1005  * @returns     true if the input string has been parsed as "yes", "true"
1006  *              (case insensitive) or a non-zero integer value.
1007  */
1008 static bool
1009 Check_ArgIsTrue(const char *Arg)
1010 {
1011         if (strcasecmp(Arg, "yes") == 0)
1012                 return true;
1013         if (strcasecmp(Arg, "true") == 0)
1014                 return true;
1015         if (atoi(Arg) != 0)
1016                 return true;
1017
1018         return false;
1019 }
1020
1021 /**
1022  * Handle setting of "MaxNickLength".
1023  *
1024  * @param Line  Line number in configuration file.
1025  * @raram Arg   Input string.
1026  * @returns     New configured maximum nick name length.
1027  */
1028 static unsigned int
1029 Handle_MaxNickLength(int Line, const char *Arg)
1030 {
1031         unsigned new;
1032
1033         new = (unsigned) atoi(Arg) + 1;
1034         if (new > CLIENT_NICK_LEN) {
1035                 Config_Error(LOG_WARNING,
1036                              "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1037                              NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1038                 return CLIENT_NICK_LEN;
1039         }
1040         if (new < 2) {
1041                 Config_Error(LOG_WARNING,
1042                              "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1043                              NGIRCd_ConfFile, Line);
1044                 return 2;
1045         }
1046         return new;
1047 }
1048
1049 /**
1050  * Output a warning messages if IDENT is configured but not compiled in.
1051  */
1052 static void
1053 WarnIdent(int UNUSED Line)
1054 {
1055 #ifndef IDENTAUTH
1056         if (Conf_Ident) {
1057                 /* user has enabled ident lookups explicitly, but ... */
1058                 Config_Error(LOG_WARNING,
1059                         "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1060                         NGIRCd_ConfFile, Line);
1061         }
1062 #endif
1063 }
1064
1065 /**
1066  * Output a warning messages if IPv6 is configured but not compiled in.
1067  */
1068 static void
1069 WarnIPv6(int UNUSED Line)
1070 {
1071 #ifndef WANT_IPV6
1072         if (Conf_ConnectIPv6) {
1073                 /* user has enabled IPv6 explicitly, but ... */
1074                 Config_Error(LOG_WARNING,
1075                         "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1076                         NGIRCd_ConfFile, Line);
1077         }
1078 #endif
1079 }
1080
1081 /**
1082  * Output a warning messages if PAM is configured but not compiled in.
1083  */
1084 static void
1085 WarnPAM(int UNUSED Line)
1086 {
1087 #ifndef PAM
1088         if (Conf_PAM) {
1089                 Config_Error(LOG_WARNING,
1090                         "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1091                         NGIRCd_ConfFile, Line);
1092         }
1093 #endif
1094 }
1095
1096 /**
1097  * Handle legacy "NoXXX" options in [GLOBAL] section.
1098  *
1099  * TODO: This function and support for "NoXXX" could be removed starting
1100  * with ngIRCd release 19 (one release after marking it "deprecated").
1101  *
1102  * @param Var   Variable name.
1103  * @param Arg   Argument string.
1104  * @returns     true if a NoXXX option has been processed; false otherwise.
1105  */
1106 static bool
1107 CheckLegacyNoOption(const char *Var, const char *Arg)
1108 {
1109         if(strcasecmp(Var, "NoDNS") == 0) {
1110                 Conf_DNS = !Check_ArgIsTrue( Arg );
1111                 return true;
1112         }
1113         if (strcasecmp(Var, "NoIdent") == 0) {
1114                 Conf_Ident = !Check_ArgIsTrue(Arg);
1115                 return true;
1116         }
1117         if(strcasecmp(Var, "NoPAM") == 0) {
1118                 Conf_PAM = !Check_ArgIsTrue(Arg);
1119                 return true;
1120         }
1121         return false;
1122 }
1123
1124 /**
1125  * Handle deprecated legacy options in [GLOBAL] section.
1126  *
1127  * TODO: This function and support for these options in the [Global] section
1128  * could be removed starting with ngIRCd release 19 (one release after
1129  * marking it "deprecated").
1130  *
1131  * @param Var   Variable name.
1132  * @param Arg   Argument string.
1133  * @returns     true if a legacy option has been processed; false otherwise.
1134  */
1135 static const char*
1136 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1137 {
1138         if (strcasecmp(Var, "AllowRemoteOper") == 0
1139             || strcasecmp(Var, "ChrootDir") == 0
1140             || strcasecmp(Var, "ConnectIPv4") == 0
1141             || strcasecmp(Var, "ConnectIPv6") == 0
1142             || strcasecmp(Var, "OperCanUseMode") == 0
1143             || strcasecmp(Var, "OperServerMode") == 0
1144             || strcasecmp(Var, "PredefChannelsOnly") == 0
1145             || strcasecmp(Var, "SyslogFacility") == 0
1146             || strcasecmp(Var, "WebircPassword") == 0) {
1147                 Handle_OPTIONS(Line, Var, Arg);
1148                 return "[Options]";
1149         }
1150         if (strcasecmp(Var, "ConnectRetry") == 0
1151             || strcasecmp(Var, "MaxConnections") == 0
1152             || strcasecmp(Var, "MaxConnectionsIP") == 0
1153             || strcasecmp(Var, "MaxJoins") == 0
1154             || strcasecmp(Var, "MaxNickLength") == 0
1155             || strcasecmp(Var, "PingTimeout") == 0
1156             || strcasecmp(Var, "PongTimeout") == 0) {
1157                 Handle_LIMITS(Line, Var, Arg);
1158                 return "[Limits]";
1159         }
1160 #ifdef SSL_SUPPORT
1161         if (strcasecmp(Var, "SSLCertFile") == 0
1162             || strcasecmp(Var, "SSLDHFile") == 0
1163             || strcasecmp(Var, "SSLKeyFile") == 0
1164             || strcasecmp(Var, "SSLKeyFilePassword") == 0
1165             || strcasecmp(Var, "SSLPorts") == 0) {
1166                 Handle_SSL(Line, Var + 3, Arg);
1167                 return "[SSL]";
1168         }
1169 #endif
1170
1171         return NULL;
1172 }
1173
1174 /**
1175  * Strip "no" prefix of a string.
1176  *
1177  * TODO: This function and support for "NoXXX" should be removed starting
1178  * with ngIRCd release 19! (One release after marking it "deprecated").
1179  *
1180  * @param str   Pointer to input string starting with "no".
1181  * @returns     New pointer to string without "no" prefix.
1182  */
1183 static const char *
1184 NoNo(const char *str)
1185 {
1186         assert(strncasecmp("no", str, 2) == 0 && str[2]);
1187         return str + 2;
1188 }
1189
1190 /**
1191  * Invert "boolean" string.
1192  *
1193  * TODO: This function and support for "NoXXX" should be removed starting
1194  * with ngIRCd release 19! (One release after marking it "deprecated").
1195  *
1196  * @param arg   "Boolean" input string.
1197  * @returns     Pointer to inverted "boolean string".
1198  */
1199 static const char *
1200 InvertArg(const char *arg)
1201 {
1202         return yesno_to_str(!Check_ArgIsTrue(arg));
1203 }
1204
1205 /**
1206  * Handle variable in [Global] configuration section.
1207  *
1208  * @param Line  Line numer in configuration file.
1209  * @param Var   Variable name.
1210  * @param Arg   Variable argument.
1211  */
1212 static void
1213 Handle_GLOBAL( int Line, char *Var, char *Arg )
1214 {
1215         struct passwd *pwd;
1216         struct group *grp;
1217         size_t len;
1218         const char *section;
1219
1220         assert(Line > 0);
1221         assert(Var != NULL);
1222         assert(Arg != NULL);
1223
1224         if (strcasecmp(Var, "Name") == 0) {
1225                 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1226                 if (len >= sizeof(Conf_ServerName))
1227                         Config_Error_TooLong(Line, Var);
1228                 return;
1229         }
1230         if (strcasecmp(Var, "AdminInfo1") == 0) {
1231                 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1232                 if (len >= sizeof(Conf_ServerAdmin1))
1233                         Config_Error_TooLong(Line, Var);
1234                 return;
1235         }
1236         if (strcasecmp(Var, "AdminInfo2") == 0) {
1237                 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1238                 if (len >= sizeof(Conf_ServerAdmin2))
1239                         Config_Error_TooLong(Line, Var);
1240                 return;
1241         }
1242         if (strcasecmp(Var, "AdminEMail") == 0) {
1243                 len = strlcpy(Conf_ServerAdminMail, Arg,
1244                         sizeof(Conf_ServerAdminMail));
1245                 if (len >= sizeof(Conf_ServerAdminMail))
1246                         Config_Error_TooLong(Line, Var);
1247                 return;
1248         }
1249         if (strcasecmp(Var, "Info") == 0) {
1250                 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1251                 if (len >= sizeof(Conf_ServerInfo))
1252                         Config_Error_TooLong(Line, Var);
1253                 return;
1254         }
1255         if (strcasecmp(Var, "Listen") == 0) {
1256                 if (Conf_ListenAddress) {
1257                         Config_Error(LOG_ERR,
1258                                      "Multiple Listen= options, ignoring: %s",
1259                                      Arg);
1260                         return;
1261                 }
1262                 Conf_ListenAddress = strdup_warn(Arg);
1263                 /* If allocation fails, we're in trouble: we cannot ignore the
1264                  * error -- otherwise ngircd would listen on all interfaces. */
1265                 if (!Conf_ListenAddress) {
1266                         Config_Error(LOG_ALERT,
1267                                      "%s exiting due to fatal errors!",
1268                                      PACKAGE_NAME);
1269                         exit(1);
1270                 }
1271                 return;
1272         }
1273         if (strcasecmp(Var, "MotdFile") == 0) {
1274                 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1275                 if (len >= sizeof(Conf_MotdFile))
1276                         Config_Error_TooLong(Line, Var);
1277                 return;
1278         }
1279         if (strcasecmp(Var, "MotdPhrase") == 0) {
1280                 len = strlen(Arg);
1281                 if (len == 0)
1282                         return;
1283                 if (len >= LINE_LEN) {
1284                         Config_Error_TooLong(Line, Var);
1285                         return;
1286                 }
1287                 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1288                         Config_Error(LOG_WARNING,
1289                                      "%s, line %d: Could not append MotdPhrase: %s",
1290                                      NGIRCd_ConfFile, Line, strerror(errno));
1291                 Using_MotdFile = false;
1292                 return;
1293         }
1294         if(strcasecmp(Var, "Password") == 0) {
1295                 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1296                 if (len >= sizeof(Conf_ServerPwd))
1297                         Config_Error_TooLong(Line, Var);
1298                 return;
1299         }
1300         if (strcasecmp(Var, "PidFile") == 0) {
1301                 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1302                 if (len >= sizeof(Conf_PidFile))
1303                         Config_Error_TooLong(Line, Var);
1304                 return;
1305         }
1306         if (strcasecmp(Var, "Ports") == 0) {
1307                 ports_parse(&Conf_ListenPorts, Line, Arg);
1308                 return;
1309         }
1310         if (strcasecmp(Var, "ServerGID") == 0) {
1311                 grp = getgrnam(Arg);
1312                 if (grp)
1313                         Conf_GID = grp->gr_gid;
1314                 else {
1315                         Conf_GID = (unsigned int)atoi(Arg);
1316                         if (!Conf_GID && strcmp(Arg, "0"))
1317                                 Config_Error(LOG_WARNING,
1318                                              "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
1319                                              NGIRCd_ConfFile, Line, Var);
1320                 }
1321                 return;
1322         }
1323         if (strcasecmp(Var, "ServerUID") == 0) {
1324                 pwd = getpwnam(Arg);
1325                 if (pwd)
1326                         Conf_UID = pwd->pw_uid;
1327                 else {
1328                         Conf_UID = (unsigned int)atoi(Arg);
1329                         if (!Conf_UID && strcmp(Arg, "0"))
1330                                 Config_Error(LOG_WARNING,
1331                                              "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
1332                                              NGIRCd_ConfFile, Line, Var);
1333                 }
1334                 return;
1335         }
1336
1337         if (CheckLegacyNoOption(Var, Arg)) {
1338                 /* TODO: This function and support for "NoXXX" could be
1339                  * be removed starting with ngIRCd release 19 (one release
1340                  * after marking it "deprecated"). */
1341                 Config_Error(LOG_WARNING,
1342                              "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1343                              NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1344                 if (strcasecmp(Var, "NoIdent") == 0)
1345                         WarnIdent(Line);
1346                 else if (strcasecmp(Var, "NoPam") == 0)
1347                         WarnPAM(Line);
1348                 return;
1349         }
1350         if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1351                 /** TODO: This function and support for these options in the
1352                  * [Global] section could be removed starting with ngIRCd
1353                  * release 19 (one release after marking it "deprecated"). */
1354                 if (strncasecmp(Var, "SSL", 3) == 0) {
1355                         Config_Error(LOG_WARNING,
1356                                      "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1357                                      NGIRCd_ConfFile, Line, Var, section,
1358                                      Var + 3);
1359                 } else {
1360                         Config_Error(LOG_WARNING,
1361                                      "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1362                                      NGIRCd_ConfFile, Line, Var, section);
1363                 }
1364                 return;
1365         }
1366
1367         Config_Error_Section(Line, Var, "Global");
1368 }
1369
1370 /**
1371  * Handle variable in [Limits] configuration section.
1372  *
1373  * @param Line  Line numer in configuration file.
1374  * @param Var   Variable name.
1375  * @param Arg   Variable argument.
1376  */
1377 static void
1378 Handle_LIMITS(int Line, char *Var, char *Arg)
1379 {
1380         assert(Line > 0);
1381         assert(Var != NULL);
1382         assert(Arg != NULL);
1383
1384         if (strcasecmp(Var, "ConnectRetry") == 0) {
1385                 Conf_ConnectRetry = atoi(Arg);
1386                 if (Conf_ConnectRetry < 5) {
1387                         Config_Error(LOG_WARNING,
1388                                      "%s, line %d: Value of \"ConnectRetry\" too low!",
1389                                      NGIRCd_ConfFile, Line);
1390                         Conf_ConnectRetry = 5;
1391                 }
1392                 return;
1393         }
1394         if (strcasecmp(Var, "MaxConnections") == 0) {
1395                 Conf_MaxConnections = atol(Arg);
1396                 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1397                         Config_Error_NaN(Line, Var);
1398                 return;
1399         }
1400         if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1401                 Conf_MaxConnectionsIP = atoi(Arg);
1402                 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1403                         Config_Error_NaN(Line, Var);
1404                 return;
1405         }
1406         if (strcasecmp(Var, "MaxJoins") == 0) {
1407                 Conf_MaxJoins = atoi(Arg);
1408                 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1409                         Config_Error_NaN(Line, Var);
1410                 return;
1411         }
1412         if (strcasecmp(Var, "MaxNickLength") == 0) {
1413                 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1414                 return;
1415         }
1416         if (strcasecmp(Var, "PingTimeout") == 0) {
1417                 Conf_PingTimeout = atoi(Arg);
1418                 if (Conf_PingTimeout < 5) {
1419                         Config_Error(LOG_WARNING,
1420                                      "%s, line %d: Value of \"PingTimeout\" too low!",
1421                                      NGIRCd_ConfFile, Line);
1422                         Conf_PingTimeout = 5;
1423                 }
1424                 return;
1425         }
1426         if (strcasecmp(Var, "PongTimeout") == 0) {
1427                 Conf_PongTimeout = atoi(Arg);
1428                 if (Conf_PongTimeout < 5) {
1429                         Config_Error(LOG_WARNING,
1430                                      "%s, line %d: Value of \"PongTimeout\" too low!",
1431                                      NGIRCd_ConfFile, Line);
1432                         Conf_PongTimeout = 5;
1433                 }
1434                 return;
1435         }
1436
1437         Config_Error_Section(Line, Var, "Limits");
1438 }
1439
1440 /**
1441  * Handle variable in [Options] configuration section.
1442  *
1443  * @param Line  Line numer in configuration file.
1444  * @param Var   Variable name.
1445  * @param Arg   Variable argument.
1446  */
1447 static void
1448 Handle_OPTIONS(int Line, char *Var, char *Arg)
1449 {
1450         size_t len;
1451
1452         assert(Line > 0);
1453         assert(Var != NULL);
1454         assert(Arg != NULL);
1455
1456         if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1457                 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1458                 return;
1459         }
1460         if (strcasecmp(Var, "ChrootDir") == 0) {
1461                 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1462                 if (len >= sizeof(Conf_Chroot))
1463                         Config_Error_TooLong(Line, Var);
1464                 return;
1465         }
1466         if (strcasecmp(Var, "CloakHost") == 0) {
1467                 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1468                 if (len >= sizeof(Conf_CloakHost))
1469                         Config_Error_TooLong(Line, Var);
1470                 return;
1471         }
1472         if (strcasecmp(Var, "CloakUserToNick") == 0) {
1473                 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1474                 return;
1475         }
1476         if (strcasecmp(Var, "ConnectIPv6") == 0) {
1477                 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1478                 WarnIPv6(Line);
1479                 return;
1480         }
1481         if (strcasecmp(Var, "ConnectIPv4") == 0) {
1482                 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1483                 return;
1484         }
1485         if (strcasecmp(Var, "DNS") == 0) {
1486                 Conf_DNS = Check_ArgIsTrue(Arg);
1487                 return;
1488         }
1489         if (strcasecmp(Var, "Ident") == 0) {
1490                 Conf_Ident = Check_ArgIsTrue(Arg);
1491                 WarnIdent(Line);
1492                 return;
1493         }
1494         if (strcasecmp(Var, "MorePrivacy") == 0) {
1495                 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1496                 return;
1497         }
1498         if (strcasecmp(Var, "NoticeAuth") == 0) {
1499                 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1500                 return;
1501         }
1502         if (strcasecmp(Var, "OperCanUseMode") == 0) {
1503                 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1504                 return;
1505         }
1506         if (strcasecmp(Var, "OperServerMode") == 0) {
1507                 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1508                 return;
1509         }
1510         if (strcasecmp(Var, "PAM") == 0) {
1511                 Conf_PAM = Check_ArgIsTrue(Arg);
1512                 WarnPAM(Line);
1513                 return;
1514         }
1515         if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
1516                 Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
1517                 return;
1518         }
1519         if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1520                 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1521                 return;
1522         }
1523 #ifndef STRICT_RFC
1524         if (strcasecmp(Var, "RequireAuthPing") == 0) {
1525                 Conf_AuthPing = Check_ArgIsTrue(Arg);
1526                 return;
1527         }
1528 #endif
1529         if (strcasecmp(Var, "ScrubCTCP") == 0) {
1530                 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1531                 return;
1532         }
1533 #ifdef SYSLOG
1534         if (strcasecmp(Var, "SyslogFacility") == 0) {
1535                 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1536                                                            Conf_SyslogFacility);
1537                 return;
1538         }
1539 #endif
1540         if (strcasecmp(Var, "WebircPassword") == 0) {
1541                 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1542                 if (len >= sizeof(Conf_WebircPwd))
1543                         Config_Error_TooLong(Line, Var);
1544                 return;
1545         }
1546
1547         Config_Error_Section(Line, Var, "Options");
1548 }
1549
1550 #ifdef SSL_SUPPORT
1551
1552 /**
1553  * Handle variable in [SSL] configuration section.
1554  *
1555  * @param Line  Line numer in configuration file.
1556  * @param Var   Variable name.
1557  * @param Arg   Variable argument.
1558  */
1559 static void
1560 Handle_SSL(int Line, char *Var, char *Arg)
1561 {
1562         assert(Line > 0);
1563         assert(Var != NULL);
1564         assert(Arg != NULL);
1565
1566         if (strcasecmp(Var, "CertFile") == 0) {
1567                 assert(Conf_SSLOptions.CertFile == NULL);
1568                 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1569                 return;
1570         }
1571         if (strcasecmp(Var, "DHFile") == 0) {
1572                 assert(Conf_SSLOptions.DHFile == NULL);
1573                 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1574                 return;
1575         }
1576         if (strcasecmp(Var, "KeyFile") == 0) {
1577                 assert(Conf_SSLOptions.KeyFile == NULL);
1578                 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1579                 return;
1580         }
1581         if (strcasecmp(Var, "KeyFilePassword") == 0) {
1582                 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1583                 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1584                         Config_Error(LOG_ERR,
1585                                      "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1586                                      NGIRCd_ConfFile, Line, Var,
1587                                      strerror(errno));
1588                 return;
1589         }
1590         if (strcasecmp(Var, "Ports") == 0) {
1591                 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1592                 return;
1593         }
1594
1595         Config_Error_Section(Line, Var, "SSL");
1596 }
1597
1598 #endif
1599
1600 /**
1601  * Handle variable in [Operator] configuration section.
1602  *
1603  * @param Line  Line numer in configuration file.
1604  * @param Var   Variable name.
1605  * @param Arg   Variable argument.
1606  */
1607 static void
1608 Handle_OPERATOR( int Line, char *Var, char *Arg )
1609 {
1610         size_t len;
1611         struct Conf_Oper *op;
1612
1613         assert( Line > 0 );
1614         assert( Var != NULL );
1615         assert( Arg != NULL );
1616
1617         op = array_get(&Conf_Opers, sizeof(*op),
1618                          array_length(&Conf_Opers, sizeof(*op)) - 1);
1619         if (!op)
1620                 return;
1621
1622         if (strcasecmp(Var, "Name") == 0) {
1623                 /* Name of IRC operator */
1624                 len = strlcpy(op->name, Arg, sizeof(op->name));
1625                 if (len >= sizeof(op->name))
1626                                 Config_Error_TooLong(Line, Var);
1627                 return;
1628         }
1629         if (strcasecmp(Var, "Password") == 0) {
1630                 /* Password of IRC operator */
1631                 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1632                 if (len >= sizeof(op->pwd))
1633                                 Config_Error_TooLong(Line, Var);
1634                 return;
1635         }
1636         if (strcasecmp(Var, "Mask") == 0) {
1637                 if (op->mask)
1638                         return; /* Hostname already configured */
1639                 op->mask = strdup_warn( Arg );
1640                 return;
1641         }
1642
1643         Config_Error_Section(Line, Var, "Operator");
1644 }
1645
1646 /**
1647  * Handle variable in [Server] configuration section.
1648  *
1649  * @param Line  Line numer in configuration file.
1650  * @param Var   Variable name.
1651  * @param Arg   Variable argument.
1652  */
1653 static void
1654 Handle_SERVER( int Line, char *Var, char *Arg )
1655 {
1656         long port;
1657         size_t len;
1658
1659         assert( Line > 0 );
1660         assert( Var != NULL );
1661         assert( Arg != NULL );
1662
1663         /* Ignore server block if no space is left in server configuration structure */
1664         if( New_Server_Idx <= NONE ) return;
1665
1666         if( strcasecmp( Var, "Host" ) == 0 ) {
1667                 /* Hostname of the server */
1668                 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1669                 if (len >= sizeof( New_Server.host ))
1670                         Config_Error_TooLong ( Line, Var );
1671                 return;
1672         }
1673         if( strcasecmp( Var, "Name" ) == 0 ) {
1674                 /* Name of the server ("Nick"/"ID") */
1675                 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1676                 if (len >= sizeof( New_Server.name ))
1677                         Config_Error_TooLong( Line, Var );
1678                 return;
1679         }
1680         if (strcasecmp(Var, "Bind") == 0) {
1681                 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1682                         return;
1683
1684                 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1685                                 NGIRCd_ConfFile, Line, Arg);
1686                 return;
1687         }
1688         if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1689                 /* Password of this server which is sent to the peer */
1690                 if (*Arg == ':') {
1691                         Config_Error(LOG_ERR,
1692                                 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1693                                                                                 NGIRCd_ConfFile, Line);
1694                 }
1695                 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1696                 if (len >= sizeof( New_Server.pwd_in ))
1697                         Config_Error_TooLong( Line, Var );
1698                 return;
1699         }
1700         if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1701                 /* Passwort of the peer which must be received */
1702                 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1703                 if (len >= sizeof( New_Server.pwd_out ))
1704                         Config_Error_TooLong( Line, Var );
1705                 return;
1706         }
1707         if( strcasecmp( Var, "Port" ) == 0 ) {
1708                 /* Port to which this server should connect */
1709                 port = atol( Arg );
1710                 if (port >= 0 && port < 0xFFFF)
1711                         New_Server.port = (UINT16)port;
1712                 else
1713                         Config_Error(LOG_ERR,
1714                                 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1715                                 NGIRCd_ConfFile, Line, port );
1716                 return;
1717         }
1718 #ifdef SSL_SUPPORT
1719         if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1720                 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1721                 return;
1722         }
1723 #endif
1724         if( strcasecmp( Var, "Group" ) == 0 ) {
1725                 /* Server group */
1726                 New_Server.group = atoi( Arg );
1727                 if (!New_Server.group && strcmp(Arg, "0"))
1728                         Config_Error_NaN(Line, Var);
1729                 return;
1730         }
1731         if( strcasecmp( Var, "Passive" ) == 0 ) {
1732                 if (Check_ArgIsTrue(Arg))
1733                         New_Server.flags |= CONF_SFLAG_DISABLED;
1734                 return;
1735         }
1736         if (strcasecmp(Var, "ServiceMask") == 0) {
1737                 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1738                               sizeof(New_Server.svs_mask));
1739                 if (len >= sizeof(New_Server.svs_mask))
1740                         Config_Error_TooLong(Line, Var);
1741                 return;
1742         }
1743
1744         Config_Error_Section(Line, Var, "Server");
1745 }
1746
1747 /**
1748  * Copy channel name into channel structure.
1749  *
1750  * If the channel name is not valid because of a missing prefix ('#', '&'),
1751  * a default prefix of '#' will be added.
1752  *
1753  * @param new_chan      New already allocated channel structure.
1754  * @param name          Name of the new channel.
1755  * @returns             true on success, false otherwise.
1756  */
1757 static bool
1758 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1759 {
1760         size_t size = sizeof(new_chan->name);
1761         char *dest = new_chan->name;
1762
1763         if (!Channel_IsValidName(name)) {
1764                 /*
1765                  * maybe user forgot to add a '#'.
1766                  * This is only here for user convenience.
1767                  */
1768                 *dest = '#';
1769                 --size;
1770                 ++dest;
1771         }
1772         return size > strlcpy(dest, name, size);
1773 }
1774
1775 /**
1776  * Handle variable in [Channel] configuration section.
1777  *
1778  * @param Line  Line numer in configuration file.
1779  * @param Var   Variable name.
1780  * @param Arg   Variable argument.
1781  */
1782 static void
1783 Handle_CHANNEL(int Line, char *Var, char *Arg)
1784 {
1785         size_t len;
1786         struct Conf_Channel *chan;
1787
1788         assert( Line > 0 );
1789         assert( Var != NULL );
1790         assert( Arg != NULL );
1791
1792         chan = array_get(&Conf_Channels, sizeof(*chan),
1793                          array_length(&Conf_Channels, sizeof(*chan)) - 1);
1794         if (!chan)
1795                 return;
1796
1797         if (strcasecmp(Var, "Name") == 0) {
1798                 if (!Handle_Channelname(chan, Arg))
1799                         Config_Error_TooLong(Line, Var);
1800                 return;
1801         }
1802         if (strcasecmp(Var, "Modes") == 0) {
1803                 /* Initial modes */
1804                 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1805                 if (len >= sizeof(chan->modes))
1806                         Config_Error_TooLong( Line, Var );
1807                 return;
1808         }
1809         if( strcasecmp( Var, "Topic" ) == 0 ) {
1810                 /* Initial topic */
1811                 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1812                 if (len >= sizeof(chan->topic))
1813                         Config_Error_TooLong( Line, Var );
1814                 return;
1815         }
1816         if( strcasecmp( Var, "Key" ) == 0 ) {
1817                 /* Initial Channel Key (mode k) */
1818                 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1819                 if (len >= sizeof(chan->key))
1820                         Config_Error_TooLong(Line, Var);
1821                 return;
1822         }
1823         if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1824                 /* maximum user limit, mode l */
1825                 chan->maxusers = (unsigned long) atol(Arg);
1826                 if (!chan->maxusers && strcmp(Arg, "0"))
1827                         Config_Error_NaN(Line, Var);
1828                 return;
1829         }
1830         if (strcasecmp(Var, "KeyFile") == 0) {
1831                 /* channel keys */
1832                 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1833                 if (len >= sizeof(chan->keyfile))
1834                         Config_Error_TooLong(Line, Var);
1835                 return;
1836         }
1837
1838         Config_Error_Section(Line, Var, "Channel");
1839 }
1840
1841 /**
1842  * Validate server configuration.
1843  *
1844  * Please note that this function uses exit(1) on fatal errors and therefore
1845  * can result in ngIRCd terminating!
1846  *
1847  * @param Configtest    true if the daemon has been called with "--configtest".
1848  * @param Rehash        true if re-reading configuration on runtime.
1849  * @returns             true if configuration is valid.
1850  */
1851 static bool
1852 Validate_Config(bool Configtest, bool Rehash)
1853 {
1854         /* Validate configuration settings. */
1855
1856 #ifdef DEBUG
1857         int i, servers, servers_once;
1858 #endif
1859         bool config_valid = true;
1860         char *ptr;
1861
1862         /* Validate configured server name, see RFC 2812 section 2.3.1 */
1863         ptr = Conf_ServerName;
1864         do {
1865                 if (*ptr >= 'a' && *ptr <= 'z') continue;
1866                 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1867                 if (*ptr >= '0' && *ptr <= '9') continue;
1868                 if (ptr > Conf_ServerName) {
1869                         if (*ptr == '.' || *ptr == '-')
1870                                 continue;
1871                 }
1872                 Conf_ServerName[0] = '\0';
1873                 break;
1874         } while (*(++ptr));
1875
1876         if (!Conf_ServerName[0]) {
1877                 /* No server name configured! */
1878                 config_valid = false;
1879                 Config_Error(LOG_ALERT,
1880                              "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1881                              NGIRCd_ConfFile);
1882                 if (!Configtest && !Rehash) {
1883                         Config_Error(LOG_ALERT,
1884                                      "%s exiting due to fatal errors!",
1885                                      PACKAGE_NAME);
1886                         exit(1);
1887                 }
1888         }
1889
1890         if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1891                 /* No dot in server name! */
1892                 config_valid = false;
1893                 Config_Error(LOG_ALERT,
1894                              "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1895                              NGIRCd_ConfFile);
1896                 if (!Configtest) {
1897                         Config_Error(LOG_ALERT,
1898                                      "%s exiting due to fatal errors!",
1899                                      PACKAGE_NAME);
1900                         exit(1);
1901                 }
1902         }
1903
1904 #ifdef STRICT_RFC
1905         if (!Conf_ServerAdminMail[0]) {
1906                 /* No administrative contact configured! */
1907                 config_valid = false;
1908                 Config_Error(LOG_ALERT,
1909                              "No administrator email address configured in \"%s\" ('AdminEMail')!",
1910                              NGIRCd_ConfFile);
1911                 if (!Configtest) {
1912                         Config_Error(LOG_ALERT,
1913                                      "%s exiting due to fatal errors!",
1914                                      PACKAGE_NAME);
1915                         exit(1);
1916                 }
1917         }
1918 #endif
1919
1920         if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1921             && !Conf_ServerAdminMail[0]) {
1922                 /* No administrative information configured! */
1923                 Config_Error(LOG_WARNING,
1924                              "No administrative information configured but required by RFC!");
1925         }
1926
1927 #ifdef PAM
1928         if (Conf_ServerPwd[0])
1929                 Config_Error(LOG_ERR,
1930                              "This server uses PAM, \"Password\" in [Global] section will be ignored!");
1931 #endif
1932
1933 #ifdef DEBUG
1934         servers = servers_once = 0;
1935         for (i = 0; i < MAX_SERVERS; i++) {
1936                 if (Conf_Server[i].name[0]) {
1937                         servers++;
1938                         if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1939                                 servers_once++;
1940                 }
1941         }
1942         Log(LOG_DEBUG,
1943             "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
1944             array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
1945             servers, servers_once,
1946             array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
1947 #endif
1948
1949         return config_valid;
1950 }
1951
1952 /**
1953  * Output "line too long" warning.
1954  *
1955  * @param Line  Line number in configuration file.
1956  * @param Item  Affected variable name.
1957  */
1958 static void
1959 Config_Error_TooLong ( const int Line, const char *Item )
1960 {
1961         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1962 }
1963
1964 /**
1965  * Output "unknown variable" warning.
1966  *
1967  * @param Line          Line number in configuration file.
1968  * @param Item          Affected variable name.
1969  * @param Section       Section name.
1970  */
1971 static void
1972 Config_Error_Section(const int Line, const char *Item, const char *Section)
1973 {
1974         Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
1975                      NGIRCd_ConfFile, Line, Section, Item);
1976 }
1977
1978 /**
1979  * Output "not a number" warning.
1980  *
1981  * @param Line  Line number in configuration file.
1982  * @param Item  Affected variable name.
1983  */
1984 static void
1985 Config_Error_NaN( const int Line, const char *Item )
1986 {
1987         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1988                                                 NGIRCd_ConfFile, Line, Item );
1989 }
1990
1991 /**
1992  * Output configuration error to console and/or logfile.
1993  *
1994  * On runtime, the normal log functions of the daemon are used. But when
1995  * testing the configuration ("--configtest"), all messages go directly
1996  * to the console.
1997  *
1998  * @param Level         Severity level of the message.
1999  * @param Format        Format string; see printf() function.
2000  */
2001 #ifdef PROTOTYPES
2002 static void Config_Error( const int Level, const char *Format, ... )
2003 #else
2004 static void Config_Error( Level, Format, va_alist )
2005 const int Level;
2006 const char *Format;
2007 va_dcl
2008 #endif
2009 {
2010         char msg[MAX_LOG_MSG_LEN];
2011         va_list ap;
2012
2013         assert( Format != NULL );
2014
2015 #ifdef PROTOTYPES
2016         va_start( ap, Format );
2017 #else
2018         va_start( ap );
2019 #endif
2020         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2021         va_end( ap );
2022
2023         if (!Use_Log) {
2024                 if (Level <= LOG_WARNING)
2025                         printf(" - %s\n", msg);
2026                 else
2027                         puts(msg);
2028         } else
2029                 Log(Level, "%s", msg);
2030 }
2031
2032 #ifdef DEBUG
2033
2034 /**
2035  * Dump internal state of the "configuration module".
2036  */
2037 GLOBAL void
2038 Conf_DebugDump(void)
2039 {
2040         int i;
2041
2042         Log(LOG_DEBUG, "Configured servers:");
2043         for (i = 0; i < MAX_SERVERS; i++) {
2044                 if (! Conf_Server[i].name[0])
2045                         continue;
2046                 Log(LOG_DEBUG,
2047                     " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2048                     Conf_Server[i].name, Conf_Server[i].host,
2049                     Conf_Server[i].port, Conf_Server[i].lasttry,
2050                     Conf_Server[i].group, Conf_Server[i].flags,
2051                     Conf_Server[i].conn_id);
2052         }
2053 }
2054
2055 #endif
2056
2057 /**
2058  * Initialize server configuration structur to default values.
2059  *
2060  * @param Server        Pointer to server structure to initialize.
2061  */
2062 static void
2063 Init_Server_Struct( CONF_SERVER *Server )
2064 {
2065         assert( Server != NULL );
2066
2067         memset( Server, 0, sizeof (CONF_SERVER) );
2068
2069         Server->group = NONE;
2070         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2071
2072         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2073
2074         Proc_InitStruct(&Server->res_stat);
2075         Server->conn_id = NONE;
2076         memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));
2077 }
2078
2079 /* -eof- */