97ecb10ff0176edf525a99b05e87eb1661942dcc
[ngircd-alex.git] / src / ngircd / conf.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * Configuration management (reading, parsing & validation)
12  */
13
14
15 #include "portab.h"
16
17 #include "imp.h"
18 #include <assert.h>
19 #include <errno.h>
20 #ifdef PROTOTYPES
21 #       include <stdarg.h>
22 #else
23 #       include <varargs.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <unistd.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 #ifdef HAVE_CTYPE_H
36 # include <ctype.h>
37 #endif
38
39 #include "array.h"
40 #include "ngircd.h"
41 #include "conn.h"
42 #include "client.h"
43 #include "defines.h"
44 #include "log.h"
45 #include "match.h"
46 #include "resolve.h"
47 #include "tool.h"
48
49 #include "exp.h"
50 #include "conf.h"
51
52
53 static bool Use_Log = true;
54 static CONF_SERVER New_Server;
55 static int New_Server_Idx;
56
57
58 static void Set_Defaults PARAMS(( bool InitServers ));
59 static bool Read_Config PARAMS(( bool ngircd_starting ));
60 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
61
62 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
63 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
66
67 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
68
69 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
70 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
71
72 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
73
74 #ifdef WANT_IPV6
75 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
76 #else
77 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
78 #endif
79
80 #ifdef SSL_SUPPORT
81 struct SSLOptions Conf_SSLOptions;
82
83 static void
84 ConfSSL_Init(void)
85 {
86         free(Conf_SSLOptions.KeyFile);
87         Conf_SSLOptions.KeyFile = NULL;
88
89         free(Conf_SSLOptions.CertFile);
90         Conf_SSLOptions.CertFile = NULL;
91
92         free(Conf_SSLOptions.DHFile);
93         Conf_SSLOptions.DHFile = NULL;
94         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
95 }
96
97
98 static void
99 ConfSSL_Puts(void)
100 {
101         if (Conf_SSLOptions.KeyFile)
102                 printf( "  SSLKeyFile = %s\n", Conf_SSLOptions.KeyFile);
103         if (Conf_SSLOptions.CertFile)
104                 printf( "  SSLCertFile = %s\n", Conf_SSLOptions.CertFile);
105         if (Conf_SSLOptions.DHFile)
106                 printf( "  SSLDHFile = %s\n", Conf_SSLOptions.DHFile);
107         if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
108                 puts("  SSLKeyFilePassword = <secret>"  );
109         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
110 }
111 #endif
112
113 static char *
114 strdup_warn(const char *str)
115 {
116         char *ptr = strdup(str);
117         if (!ptr)
118                 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
119         return ptr;
120 }
121
122
123 static void
124 ports_puts(array *a)
125 {
126         size_t len;
127         UINT16 *ports;
128         len = array_length(a, sizeof(UINT16));
129         if (len--) {
130                 ports = (UINT16*) array_start(a);
131                 printf("%u", (unsigned int) *ports);
132                 while (len--) {
133                         ports++;
134                         printf(", %u", (unsigned int) *ports);
135                 }
136         }
137         putc('\n', stdout);
138 }
139
140
141 static void
142 ports_parse(array *a, int Line, char *Arg)
143 {
144         char *ptr;
145         int port;
146         UINT16 port16;
147
148         array_trunc(a);
149
150         /* Ports on that the server should listen. More port numbers
151          * must be separated by "," */
152         ptr = strtok( Arg, "," );
153         while (ptr) {
154                 ngt_TrimStr( ptr );
155                 port = atol( ptr );
156                 if (port > 0 && port < 0xFFFF) {
157                         port16 = (UINT16) port;
158                         if (!array_catb(a, (char*)&port16, sizeof port16))
159                                 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
160                                                         NGIRCd_ConfFile, Line, port, strerror(errno));
161                 } else {
162                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
163                                                                         NGIRCd_ConfFile, Line, port );
164                 }
165
166                 ptr = strtok( NULL, "," );
167         }
168 }
169
170
171 GLOBAL void
172 Conf_Init( void )
173 {
174         Read_Config( true );
175         Validate_Config(false, false);
176 } /* Config_Init */
177
178
179 GLOBAL bool
180 Conf_Rehash( void )
181 {
182         if (!Read_Config(false))
183                 return false;
184         Validate_Config(false, true);
185
186         /* Update CLIENT structure of local server */
187         Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
188         return true;
189 } /* Config_Rehash */
190
191
192 static const char*
193 yesno_to_str(int boolean_value)
194 {
195         if (boolean_value)
196                 return "yes";
197         return "no";
198 }
199
200
201 GLOBAL int
202 Conf_Test( void )
203 {
204         /* Read configuration, validate and output it. */
205
206         struct passwd *pwd;
207         struct group *grp;
208         unsigned int i;
209         char *topic;
210         bool config_valid;
211
212         Use_Log = false;
213
214         if (! Read_Config(true))
215                 return 1;
216
217         config_valid = Validate_Config(true, false);
218
219         /* If stdin and stdout ("you can read our nice message and we can
220          * read in your keypress") are valid tty's, wait for a key: */
221         if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
222                 puts( "OK, press enter to see a dump of your service configuration ..." );
223                 getchar( );
224         } else {
225                 puts( "Ok, dump of your server configuration follows:\n" );
226         }
227
228         puts( "[GLOBAL]" );
229         printf( "  Name = %s\n", Conf_ServerName );
230         printf( "  Info = %s\n", Conf_ServerInfo );
231         printf( "  Password = %s\n", Conf_ServerPwd );
232         printf( "  AdminInfo1 = %s\n", Conf_ServerAdmin1 );
233         printf( "  AdminInfo2 = %s\n", Conf_ServerAdmin2 );
234         printf( "  AdminEMail = %s\n", Conf_ServerAdminMail );
235         printf( "  MotdFile = %s\n", Conf_MotdFile );
236         printf( "  MotdPhrase = %s\n", Conf_MotdPhrase );
237         printf( "  ChrootDir = %s\n", Conf_Chroot );
238         printf( "  PidFile = %s\n", Conf_PidFile);
239         printf("  Listen = %s\n", Conf_ListenAddress);
240         fputs("  Ports = ", stdout);
241
242         ports_puts(&Conf_ListenPorts);
243 #ifdef SSL_SUPPORT
244         fputs("  SSLPorts = ", stdout);
245         ports_puts(&Conf_SSLOptions.ListenPorts);
246         ConfSSL_Puts();
247 #endif
248
249         pwd = getpwuid( Conf_UID );
250         if( pwd ) printf( "  ServerUID = %s\n", pwd->pw_name );
251         else printf( "  ServerUID = %ld\n", (long)Conf_UID );
252         grp = getgrgid( Conf_GID );
253         if( grp ) printf( "  ServerGID = %s\n", grp->gr_name );
254         else printf( "  ServerGID = %ld\n", (long)Conf_GID );
255         printf( "  PingTimeout = %d\n", Conf_PingTimeout );
256         printf( "  PongTimeout = %d\n", Conf_PongTimeout );
257         printf( "  ConnectRetry = %d\n", Conf_ConnectRetry );
258         printf( "  OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
259         printf( "  OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
260         printf( "  PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
261         printf( "  NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
262
263 #ifdef WANT_IPV6
264         printf("  ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
265         printf("  ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
266 #endif
267         printf( "  MaxConnections = %ld\n", Conf_MaxConnections);
268         printf( "  MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
269         printf( "  MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
270         printf( "  MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
271
272         for( i = 0; i < Conf_Oper_Count; i++ ) {
273                 if( ! Conf_Oper[i].name[0] ) continue;
274
275                 /* Valid "Operator" section */
276                 puts( "[OPERATOR]" );
277                 printf( "  Name = %s\n", Conf_Oper[i].name );
278                 printf( "  Password = %s\n", Conf_Oper[i].pwd );
279                 if ( Conf_Oper[i].mask ) printf( "  Mask = %s\n", Conf_Oper[i].mask );
280                 puts( "" );
281         }
282
283         for( i = 0; i < MAX_SERVERS; i++ ) {
284                 if( ! Conf_Server[i].name[0] ) continue;
285
286                 /* Valid "Server" section */
287                 puts( "[SERVER]" );
288                 printf( "  Name = %s\n", Conf_Server[i].name );
289                 printf( "  Host = %s\n", Conf_Server[i].host );
290                 printf( "  Port = %u\n", (unsigned int)Conf_Server[i].port );
291 #ifdef SSL_SUPPORT
292                 printf( "  SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
293 #endif
294                 printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
295                 printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
296                 printf( "  ServiceMask = %s\n", Conf_Server[i].svs_mask);
297                 printf( "  Group = %d\n", Conf_Server[i].group );
298                 printf( "  Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
299         }
300
301         for( i = 0; i < Conf_Channel_Count; i++ ) {
302                 if( ! Conf_Channel[i].name[0] ) continue;
303
304                 /* Valid "Channel" section */
305                 puts( "[CHANNEL]" );
306                 printf( "  Name = %s\n", Conf_Channel[i].name );
307                 printf( "  Modes = %s\n", Conf_Channel[i].modes );
308                 printf( "  Key = %s\n", Conf_Channel[i].key );
309                 printf( "  MaxUsers = %lu\n", Conf_Channel[i].maxusers );
310
311                 topic = (char*)array_start(&Conf_Channel[i].topic);
312                 printf( "  Topic = %s\n\n", topic ? topic : "");
313         }
314
315         return (config_valid ? 0 : 1);
316 } /* Conf_Test */
317
318
319 GLOBAL void
320 Conf_UnsetServer( CONN_ID Idx )
321 {
322         /* Set next time for next connection attempt, if this is a server
323          * link that is (still) configured here. If the server is set as
324          * "once", delete it from our configuration.
325          * Non-Server-Connections will be silently ignored. */
326
327         int i;
328         time_t t;
329
330         /* Check all our configured servers */
331         for( i = 0; i < MAX_SERVERS; i++ ) {
332                 if( Conf_Server[i].conn_id != Idx ) continue;
333
334                 /* Gotcha! Mark server configuration as "unused": */
335                 Conf_Server[i].conn_id = NONE;
336
337                 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
338                         /* Delete configuration here */
339                         Init_Server_Struct( &Conf_Server[i] );
340                 } else {
341                         /* Set time for next connect attempt */
342                         t = time(NULL);
343                         if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
344                                 /* The connection has been "long", so we don't
345                                  * require the next attempt to be delayed. */
346                                 Conf_Server[i].lasttry =
347                                         t - Conf_ConnectRetry + RECONNECT_DELAY;
348                         } else
349                                 Conf_Server[i].lasttry = t;
350                 }
351         }
352 } /* Conf_UnsetServer */
353
354
355 GLOBAL void
356 Conf_SetServer( int ConfServer, CONN_ID Idx )
357 {
358         /* Set connection for specified configured server */
359
360         assert( ConfServer > NONE );
361         assert( Idx > NONE );
362
363         Conf_Server[ConfServer].conn_id = Idx;
364 } /* Conf_SetServer */
365
366
367 GLOBAL int
368 Conf_GetServer( CONN_ID Idx )
369 {
370         /* Get index of server in configuration structure */
371
372         int i = 0;
373
374         assert( Idx > NONE );
375
376         for( i = 0; i < MAX_SERVERS; i++ ) {
377                 if( Conf_Server[i].conn_id == Idx ) return i;
378         }
379         return NONE;
380 } /* Conf_GetServer */
381
382
383 GLOBAL bool
384 Conf_EnableServer( char *Name, UINT16 Port )
385 {
386         /* Enable specified server and adjust port */
387
388         int i;
389
390         assert( Name != NULL );
391
392         for( i = 0; i < MAX_SERVERS; i++ ) {
393                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
394                         /* Gotcha! Set port and enable server: */
395                         Conf_Server[i].port = Port;
396                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
397                         return true;
398                 }
399         }
400         return false;
401 } /* Conf_EnableServer */
402
403
404 GLOBAL bool
405 Conf_EnablePassiveServer(const char *Name)
406 {
407         /* Enable specified server */
408         int i;
409
410         assert( Name != NULL );
411         for (i = 0; i < MAX_SERVERS; i++) {
412                 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
413                         /* BINGO! Enable server */
414                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
415                         return true;
416                 }
417         }
418         return false;
419 } /* Conf_EnablePassiveServer */
420
421
422 GLOBAL bool
423 Conf_DisableServer( char *Name )
424 {
425         /* Enable specified server and adjust port */
426
427         int i;
428
429         assert( Name != NULL );
430
431         for( i = 0; i < MAX_SERVERS; i++ ) {
432                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
433                         /* Gotcha! Disable and disconnect server: */
434                         Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
435                         if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
436                         return true;
437                 }
438         }
439         return false;
440 } /* Conf_DisableServer */
441
442
443 GLOBAL bool
444 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
445 {
446         /* Add new server to configuration */
447
448         int i;
449
450         assert( Name != NULL );
451         assert( Host != NULL );
452         assert( MyPwd != NULL );
453         assert( PeerPwd != NULL );
454
455         /* Search unused item in server configuration structure */
456         for( i = 0; i < MAX_SERVERS; i++ ) {
457                 /* Is this item used? */
458                 if( ! Conf_Server[i].name[0] ) break;
459         }
460         if( i >= MAX_SERVERS ) return false;
461
462         Init_Server_Struct( &Conf_Server[i] );
463         strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
464         strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
465         strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
466         strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
467         Conf_Server[i].port = Port;
468         Conf_Server[i].flags = CONF_SFLAG_ONCE;
469
470         return true;
471 } /* Conf_AddServer */
472
473
474 /**
475  * Check if the given nick name is an service
476  */
477 GLOBAL bool
478 Conf_IsService(int ConfServer, char *Nick)
479 {
480         return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
481 } /* Conf_IsService */
482
483
484 static void
485 Set_Defaults( bool InitServers )
486 {
487         /* Initialize configuration variables with default values. */
488
489         int i;
490
491         strcpy( Conf_ServerName, "" );
492         snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
493         strcpy( Conf_ServerPwd, "" );
494
495         strcpy( Conf_ServerAdmin1, "" );
496         strcpy( Conf_ServerAdmin2, "" );
497         strcpy( Conf_ServerAdminMail, "" );
498
499         strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
500         strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
501
502         strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
503
504         strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
505
506         strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
507
508         free(Conf_ListenAddress);
509         Conf_ListenAddress = NULL;
510         Conf_UID = Conf_GID = 0;
511
512         Conf_PingTimeout = 120;
513         Conf_PongTimeout = 20;
514
515         Conf_ConnectRetry = 60;
516
517         Conf_Oper_Count = 0;
518         Conf_Channel_Count = 0;
519
520         Conf_OperCanMode = false;
521         Conf_NoDNS = false;
522         Conf_PredefChannelsOnly = false;
523         Conf_OperServerMode = false;
524
525         Conf_ConnectIPv4 = true;
526         Conf_ConnectIPv6 = true;
527
528         Conf_MaxConnections = 0;
529         Conf_MaxConnectionsIP = 5;
530         Conf_MaxJoins = 10;
531         Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
532
533         /* Initialize server configuration structures */
534         if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
535 } /* Set_Defaults */
536
537
538 static bool
539 Read_Config( bool ngircd_starting )
540 {
541         /* Read configuration file. */
542
543         char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
544         const UINT16 defaultport = 6667;
545         int line, i, n;
546         FILE *fd;
547
548         /* Open configuration file */
549         fd = fopen( NGIRCd_ConfFile, "r" );
550         if( ! fd ) {
551                 /* No configuration file found! */
552                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
553                                         NGIRCd_ConfFile, strerror( errno ));
554                 if (!ngircd_starting)
555                         return false;
556                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
557                 exit( 1 );
558         }
559
560         Set_Defaults( ngircd_starting );
561
562         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
563
564         /* Clean up server configuration structure: mark all already
565          * configured servers as "once" so that they are deleted
566          * after the next disconnect and delete all unused servers.
567          * And delete all servers which are "duplicates" of servers
568          * that are already marked as "once" (such servers have been
569          * created by the last rehash but are now useless). */
570         for( i = 0; i < MAX_SERVERS; i++ ) {
571                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
572                 else {
573                         /* This structure is in use ... */
574                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
575                                 /* Check for duplicates */
576                                 for( n = 0; n < MAX_SERVERS; n++ ) {
577                                         if( n == i ) continue;
578
579                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
580                                                 Init_Server_Struct( &Conf_Server[n] );
581 #ifdef DEBUG
582                                                 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
583                                                                                                 n, i );
584 #endif
585                                         }
586                                 }
587                         } else {
588                                 /* Mark server as "once" */
589                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
590                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
591                         }
592                 }
593         }
594
595         /* Initialize variables */
596         line = 0;
597         strcpy( section, "" );
598         Init_Server_Struct( &New_Server );
599         New_Server_Idx = NONE;
600 #ifdef SSL_SUPPORT
601         ConfSSL_Init();
602 #endif
603         /* Read configuration file */
604         while( true ) {
605                 if( ! fgets( str, LINE_LEN, fd )) break;
606                 ngt_TrimStr( str );
607                 line++;
608
609                 /* Skip comments and empty lines */
610                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
611
612                 /* Is this the beginning of a new section? */
613                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
614                         strlcpy( section, str, sizeof( section ));
615                         if( strcasecmp( section, "[GLOBAL]" ) == 0 )
616                                 continue;
617
618                         if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
619                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
620                                         Config_Error( LOG_ERR, "Too many operators configured.");
621                                 else {
622                                         /* Initialize new operator structure */
623                                         Conf_Oper[Conf_Oper_Count].name[0] = '\0';
624                                         Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
625                                         if (Conf_Oper[Conf_Oper_Count].mask) {
626                                                 free(Conf_Oper[Conf_Oper_Count].mask );
627                                                 Conf_Oper[Conf_Oper_Count].mask = NULL;
628                                         }
629                                         Conf_Oper_Count++;
630                                 }
631                                 continue;
632                         }
633                         if( strcasecmp( section, "[SERVER]" ) == 0 ) {
634                                 /* Check if there is already a server to add */
635                                 if( New_Server.name[0] ) {
636                                         /* Copy data to "real" server structure */
637                                         assert( New_Server_Idx > NONE );
638                                         Conf_Server[New_Server_Idx] = New_Server;
639                                 }
640
641                                 /* Re-init structure for new server */
642                                 Init_Server_Struct( &New_Server );
643
644                                 /* Search unused item in server configuration structure */
645                                 for( i = 0; i < MAX_SERVERS; i++ ) {
646                                         /* Is this item used? */
647                                         if( ! Conf_Server[i].name[0] ) break;
648                                 }
649                                 if( i >= MAX_SERVERS ) {
650                                         /* Oops, no free item found! */
651                                         Config_Error( LOG_ERR, "Too many servers configured." );
652                                         New_Server_Idx = NONE;
653                                 }
654                                 else New_Server_Idx = i;
655                                 continue;
656                         }
657                         if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
658                                 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
659                                         Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
660                                 } else {
661                                         /* Initialize new channel structure */
662                                         strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
663                                         strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
664                                         strcpy( Conf_Channel[Conf_Channel_Count].key, "" );
665                                         Conf_Channel[Conf_Channel_Count].maxusers = 0;
666                                         array_free(&Conf_Channel[Conf_Channel_Count].topic);
667                                         Conf_Channel_Count++;
668                                 }
669                                 continue;
670                         }
671                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
672                         section[0] = 0x1;
673                 }
674                 if( section[0] == 0x1 ) continue;
675
676                 /* Split line into variable name and parameters */
677                 ptr = strchr( str, '=' );
678                 if( ! ptr ) {
679                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
680                         continue;
681                 }
682                 *ptr = '\0';
683                 var = str; ngt_TrimStr( var );
684                 arg = ptr + 1; ngt_TrimStr( arg );
685
686                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
687                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
688                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
689                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
690                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
691         }
692
693         /* Close configuration file */
694         fclose( fd );
695
696         /* Check if there is still a server to add */
697         if( New_Server.name[0] ) {
698                 /* Copy data to "real" server structure */
699                 assert( New_Server_Idx > NONE );
700                 Conf_Server[New_Server_Idx] = New_Server;
701         }
702
703         if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
704                 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
705                         Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
706                                                         (unsigned int) defaultport, strerror(errno));
707                         exit( 1 );
708                 }
709         }
710
711         if (!Conf_ListenAddress)
712                 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
713
714         if (!Conf_ListenAddress) {
715                 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
716                 exit(1);
717         }
718         return true;
719 } /* Read_Config */
720
721
722 static bool
723 Check_ArgIsTrue( const char *Arg )
724 {
725         if( strcasecmp( Arg, "yes" ) == 0 ) return true;
726         if( strcasecmp( Arg, "true" ) == 0 ) return true;
727         if( atoi( Arg ) != 0 ) return true;
728
729         return false;
730 } /* Check_ArgIsTrue */
731
732
733 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
734 {
735         unsigned new;
736
737         new = (unsigned) atoi(Arg) + 1;
738         if (new > CLIENT_NICK_LEN) {
739                 Config_Error(LOG_WARNING,
740                              "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
741                              NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
742                 return CLIENT_NICK_LEN;
743         }
744         if (new < 2) {
745                 Config_Error(LOG_WARNING,
746                              "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
747                              NGIRCd_ConfFile, Line);
748                 return 2;
749         }
750         return new;
751 } /* Handle_MaxNickLength */
752
753
754 static void
755 Handle_GLOBAL( int Line, char *Var, char *Arg )
756 {
757         struct passwd *pwd;
758         struct group *grp;
759         size_t len;
760         
761         assert( Line > 0 );
762         assert( Var != NULL );
763         assert( Arg != NULL );
764         
765         if( strcasecmp( Var, "Name" ) == 0 ) {
766                 /* Server name */
767                 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
768                 if (len >= sizeof( Conf_ServerName ))
769                         Config_Error_TooLong( Line, Var );
770                 return;
771         }
772         if( strcasecmp( Var, "Info" ) == 0 ) {
773                 /* Info text of server */
774                 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
775                 if (len >= sizeof( Conf_ServerInfo ))
776                         Config_Error_TooLong ( Line, Var );
777                 return;
778         }
779         if( strcasecmp( Var, "Password" ) == 0 ) {
780                 /* Global server password */
781                 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
782                 if (len >= sizeof( Conf_ServerPwd ))
783                         Config_Error_TooLong( Line, Var );
784                 return;
785         }
786         if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
787                 /* Administrative info #1 */
788                 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
789                 if (len >= sizeof( Conf_ServerAdmin1 ))
790                         Config_Error_TooLong ( Line, Var );
791                 return;
792         }
793         if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
794                 /* Administrative info #2 */
795                 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
796                 if (len >= sizeof( Conf_ServerAdmin2 ))
797                         Config_Error_TooLong ( Line, Var );
798                 return;
799         }
800         if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
801                 /* Administrative email contact */
802                 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
803                 if (len >= sizeof( Conf_ServerAdminMail ))
804                         Config_Error_TooLong( Line, Var );
805                 return;
806         }
807
808         if( strcasecmp( Var, "Ports" ) == 0 ) {
809                 ports_parse(&Conf_ListenPorts, Line, Arg);
810                 return;
811         }
812         if( strcasecmp( Var, "MotdFile" ) == 0 ) {
813                 /* "Message of the day" (MOTD) file */
814                 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
815                 if (len >= sizeof( Conf_MotdFile ))
816                         Config_Error_TooLong( Line, Var );
817                 return;
818         }
819         if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
820                 /* "Message of the day" phrase (instead of file) */
821                 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
822                 if (len >= sizeof( Conf_MotdPhrase ))
823                         Config_Error_TooLong( Line, Var );
824                 return;
825         }
826         if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
827                 /* directory for chroot() */
828                 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
829                 if (len >= sizeof( Conf_Chroot ))
830                         Config_Error_TooLong( Line, Var );
831                 return;
832         }
833         if ( strcasecmp( Var, "PidFile" ) == 0 ) {
834                 /* name of pidfile */
835                 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
836                 if (len >= sizeof( Conf_PidFile ))
837                         Config_Error_TooLong( Line, Var );
838                 return;
839         }
840         if( strcasecmp( Var, "ServerUID" ) == 0 ) {
841                 /* UID the daemon should switch to */
842                 pwd = getpwnam( Arg );
843                 if( pwd ) Conf_UID = pwd->pw_uid;
844                 else {
845 #ifdef HAVE_ISDIGIT
846                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
847                         else
848 #endif
849                         Conf_UID = (unsigned int)atoi( Arg );
850                 }
851                 return;
852         }
853         if( strcasecmp( Var, "ServerGID" ) == 0 ) {
854                 /* GID the daemon should use */
855                 grp = getgrnam( Arg );
856                 if( grp ) Conf_GID = grp->gr_gid;
857                 else {
858 #ifdef HAVE_ISDIGIT
859                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
860                         else
861 #endif
862                         Conf_GID = (unsigned int)atoi( Arg );
863                 }
864                 return;
865         }
866         if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
867                 /* PING timeout */
868                 Conf_PingTimeout = atoi( Arg );
869                 if( Conf_PingTimeout < 5 ) {
870                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
871                                                                         NGIRCd_ConfFile, Line );
872                         Conf_PingTimeout = 5;
873                 }
874                 return;
875         }
876         if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
877                 /* PONG timeout */
878                 Conf_PongTimeout = atoi( Arg );
879                 if( Conf_PongTimeout < 5 ) {
880                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
881                                                                         NGIRCd_ConfFile, Line );
882                         Conf_PongTimeout = 5;
883                 }
884                 return;
885         }
886         if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
887                 /* Seconds between connection attempts to other servers */
888                 Conf_ConnectRetry = atoi( Arg );
889                 if( Conf_ConnectRetry < 5 ) {
890                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
891                                                                         NGIRCd_ConfFile, Line );
892                         Conf_ConnectRetry = 5;
893                 }
894                 return;
895         }
896         if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
897                 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
898                 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
899                 return;
900         }
901         if( strcasecmp( Var, "NoDNS" ) == 0 ) {
902                 /* don't do reverse dns lookups when clients connect? */
903                 Conf_NoDNS = Check_ArgIsTrue( Arg );
904                 return;
905         }
906 #ifdef WANT_IPV6
907         /* the default setting for all the WANT_IPV6 special options is 'true' */
908         if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
909                 /* connect to other hosts using ipv6, if they have an AAAA record? */
910                 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
911                 return;
912         }
913         if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
914                 /* connect to other hosts using ipv4.
915                  * again, this can be used for ipv6-only setups */
916                 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
917                 return;
918         }
919 #endif
920         if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
921                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
922                 Conf_OperCanMode = Check_ArgIsTrue( Arg );
923                 return;
924         }
925         if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
926                 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
927                 Conf_OperServerMode = Check_ArgIsTrue( Arg );
928                 return;
929         }
930         if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
931                 /* Maximum number of connections. 0 -> "no limit". */
932 #ifdef HAVE_ISDIGIT
933                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
934                 else
935 #endif
936                 Conf_MaxConnections = atol( Arg );
937                 return;
938         }
939         if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
940                 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
941 #ifdef HAVE_ISDIGIT
942                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
943                 else
944 #endif
945                 Conf_MaxConnectionsIP = atoi( Arg );
946                 return;
947         }
948         if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
949                 /* Maximum number of channels a user can join. 0 -> "no limit". */
950 #ifdef HAVE_ISDIGIT
951                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
952                 else
953 #endif
954                 Conf_MaxJoins = atoi( Arg );
955                 return;
956         }
957         if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
958                 /* Maximum length of a nick name; must be same on all servers
959                  * within the IRC network! */
960                 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
961                 return;
962         }
963
964         if( strcasecmp( Var, "Listen" ) == 0 ) {
965                 /* IP-Address to bind sockets */
966                 if (Conf_ListenAddress) {
967                         Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
968                         return;
969                 }
970                 Conf_ListenAddress = strdup_warn(Arg);
971                 /*
972                  * if allocation fails, we're in trouble:
973                  * we cannot ignore the error -- otherwise ngircd
974                  * would listen on all interfaces.
975                  */
976                 if (!Conf_ListenAddress) {
977                         Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
978                         exit(1);
979                 }
980                 return;
981         }
982
983 #ifdef SSL_SUPPORT
984         if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
985                 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
986                 return;
987         }
988
989         if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
990                 assert(Conf_SSLOptions.KeyFile == NULL );
991                 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
992                 return;
993         }
994         if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
995                 assert(Conf_SSLOptions.CertFile == NULL );
996                 Conf_SSLOptions.CertFile = strdup_warn(Arg);
997                 return;
998         }
999
1000         if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1001                 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1002                 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1003                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1004                                                                 NGIRCd_ConfFile, Line, Var, strerror(errno));
1005                 return;
1006         }
1007         if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1008                 assert(Conf_SSLOptions.DHFile == NULL);
1009                 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1010                 return;
1011         }
1012 #endif
1013         Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1014                                                                 NGIRCd_ConfFile, Line, Var);
1015 } /* Handle_GLOBAL */
1016
1017
1018 static void
1019 Handle_OPERATOR( int Line, char *Var, char *Arg )
1020 {
1021         unsigned int opercount;
1022         size_t len;
1023         assert( Line > 0 );
1024         assert( Var != NULL );
1025         assert( Arg != NULL );
1026         assert( Conf_Oper_Count > 0 );
1027
1028         if ( Conf_Oper_Count == 0 )
1029                 return;
1030
1031         opercount = Conf_Oper_Count - 1;
1032
1033         if( strcasecmp( Var, "Name" ) == 0 ) {
1034                 /* Name of IRC operator */
1035                 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1036                 if (len >= sizeof( Conf_Oper[opercount].name ))
1037                                 Config_Error_TooLong( Line, Var );
1038                 return;
1039         }
1040         if( strcasecmp( Var, "Password" ) == 0 ) {
1041                 /* Password of IRC operator */
1042                 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1043                 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1044                                 Config_Error_TooLong( Line, Var );
1045                 return;
1046         }
1047         if( strcasecmp( Var, "Mask" ) == 0 ) {
1048                 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1049
1050                 Conf_Oper[opercount].mask = strdup_warn( Arg );
1051                 return;
1052         }
1053         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1054                                                                 NGIRCd_ConfFile, Line, Var );
1055 } /* Handle_OPERATOR */
1056
1057
1058 static void
1059 Handle_SERVER( int Line, char *Var, char *Arg )
1060 {
1061         long port;
1062         size_t len;
1063         
1064         assert( Line > 0 );
1065         assert( Var != NULL );
1066         assert( Arg != NULL );
1067
1068         /* Ignore server block if no space is left in server configuration structure */
1069         if( New_Server_Idx <= NONE ) return;
1070
1071         if( strcasecmp( Var, "Host" ) == 0 ) {
1072                 /* Hostname of the server */
1073                 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1074                 if (len >= sizeof( New_Server.host ))
1075                         Config_Error_TooLong ( Line, Var );
1076                 return;
1077         }
1078         if( strcasecmp( Var, "Name" ) == 0 ) {
1079                 /* Name of the server ("Nick"/"ID") */
1080                 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1081                 if (len >= sizeof( New_Server.name ))
1082                         Config_Error_TooLong( Line, Var );
1083                 return;
1084         }
1085         if (strcasecmp(Var, "Bind") == 0) {
1086                 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1087                         return;
1088
1089                 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1090                                 NGIRCd_ConfFile, Line, Arg);
1091                 return;
1092         }
1093         if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1094                 /* Password of this server which is sent to the peer */
1095                 if (*Arg == ':') {
1096                         Config_Error(LOG_ERR,
1097                                 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1098                                                                                 NGIRCd_ConfFile, Line);
1099                 }
1100                 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1101                 if (len >= sizeof( New_Server.pwd_in ))
1102                         Config_Error_TooLong( Line, Var );
1103                 return;
1104         }
1105         if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1106                 /* Passwort of the peer which must be received */
1107                 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1108                 if (len >= sizeof( New_Server.pwd_out ))
1109                         Config_Error_TooLong( Line, Var );
1110                 return;
1111         }
1112         if( strcasecmp( Var, "Port" ) == 0 ) {
1113                 /* Port to which this server should connect */
1114                 port = atol( Arg );
1115                 if( port > 0 && port < 0xFFFF )
1116                         New_Server.port = (UINT16)port;
1117                 else
1118                         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1119                                                                                 NGIRCd_ConfFile, Line, port );
1120                 return;
1121         }
1122 #ifdef SSL_SUPPORT
1123         if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1124                 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1125                 return;
1126         }
1127 #endif
1128         if( strcasecmp( Var, "Group" ) == 0 ) {
1129                 /* Server group */
1130 #ifdef HAVE_ISDIGIT
1131                 if( ! isdigit( (int)*Arg ))
1132                         Config_Error_NaN( Line, Var );
1133                 else
1134 #endif
1135                 New_Server.group = atoi( Arg );
1136                 return;
1137         }
1138         if( strcasecmp( Var, "Passive" ) == 0 ) {
1139                 if (Check_ArgIsTrue(Arg))
1140                         New_Server.flags |= CONF_SFLAG_DISABLED;
1141                 return;
1142         }
1143         if (strcasecmp(Var, "ServiceMask") == 0) {
1144                 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1145                               sizeof(New_Server.svs_mask));
1146                 if (len >= sizeof(New_Server.svs_mask))
1147                         Config_Error_TooLong(Line, Var);
1148                 return;
1149         }
1150         
1151         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1152                                                                 NGIRCd_ConfFile, Line, Var );
1153 } /* Handle_SERVER */
1154
1155
1156 static bool
1157 Handle_Channelname(size_t chancount, const char *name)
1158 {
1159         size_t size = sizeof( Conf_Channel[chancount].name );
1160         char *dest = Conf_Channel[chancount].name;
1161
1162         if (*name && *name != '#') {
1163                 *dest = '#';
1164                 --size;
1165                 ++dest;
1166         }
1167         return size > strlcpy(dest, name, size);
1168 }
1169
1170
1171 static void
1172 Handle_CHANNEL( int Line, char *Var, char *Arg )
1173 {
1174         size_t len;
1175         size_t chancount = 0;
1176
1177         assert( Line > 0 );
1178         assert( Var != NULL );
1179         assert( Arg != NULL );
1180         if (Conf_Channel_Count > 0)
1181                 chancount = Conf_Channel_Count - 1;
1182
1183         if( strcasecmp( Var, "Name" ) == 0 ) {
1184                 if (!Handle_Channelname(chancount, Arg))
1185                         Config_Error_TooLong( Line, Var );
1186                 return;
1187         }
1188         if( strcasecmp( Var, "Modes" ) == 0 ) {
1189                 /* Initial modes */
1190                 len = strlcpy( Conf_Channel[chancount].modes, Arg, sizeof( Conf_Channel[chancount].modes ));
1191                 if (len >= sizeof( Conf_Channel[chancount].modes ))
1192                         Config_Error_TooLong( Line, Var );
1193                 return;
1194         }
1195         if( strcasecmp( Var, "Topic" ) == 0 ) {
1196                 /* Initial topic */
1197                 if (!array_copys( &Conf_Channel[chancount].topic, Arg))
1198                         Config_Error_TooLong( Line, Var );
1199                 return;
1200         }
1201
1202         if( strcasecmp( Var, "Key" ) == 0 ) {
1203                 /* Initial Channel Key (mode k) */
1204                 len = strlcpy(Conf_Channel[chancount].key, Arg, sizeof(Conf_Channel[chancount].key));
1205                 if (len >= sizeof( Conf_Channel[chancount].key ))
1206                         Config_Error_TooLong(Line, Var);
1207                 return;
1208         }
1209
1210         if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1211                 /* maximum user limit, mode l */
1212                 Conf_Channel[chancount].maxusers = (unsigned long) atol(Arg);
1213                 if (Conf_Channel[chancount].maxusers == 0)
1214                         Config_Error_NaN(Line, Var);
1215                 return;
1216         }
1217
1218         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1219                                                                 NGIRCd_ConfFile, Line, Var );
1220 } /* Handle_CHANNEL */
1221
1222
1223 static bool
1224 Validate_Config(bool Configtest, bool Rehash)
1225 {
1226         /* Validate configuration settings. */
1227
1228 #ifdef DEBUG
1229         int i, servers, servers_once;
1230 #endif
1231         bool config_valid = true;
1232         char *ptr;
1233
1234         /* Validate configured server name, see RFC 2812 section 2.3.1 */
1235         ptr = Conf_ServerName;
1236         do {
1237                 if (*ptr >= 'a' && *ptr <= 'z') continue;
1238                 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1239                 if (*ptr >= '0' && *ptr <= '9') continue;
1240                 if (ptr > Conf_ServerName) {
1241                         if (*ptr == '.' || *ptr == '-')
1242                                 continue;
1243                 }
1244                 Conf_ServerName[0] = '\0';
1245                 break;
1246         } while (*(++ptr));
1247
1248         if (!Conf_ServerName[0]) {
1249                 /* No server name configured! */
1250                 config_valid = false;
1251                 Config_Error(LOG_ALERT,
1252                              "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1253                              NGIRCd_ConfFile);
1254                 if (!Configtest && !Rehash) {
1255                         Config_Error(LOG_ALERT,
1256                                      "%s exiting due to fatal errors!",
1257                                      PACKAGE_NAME);
1258                         exit(1);
1259                 }
1260         }
1261
1262         if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1263                 /* No dot in server name! */
1264                 config_valid = false;
1265                 Config_Error(LOG_ALERT,
1266                              "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1267                              NGIRCd_ConfFile);
1268                 if (!Configtest) {
1269                         Config_Error(LOG_ALERT,
1270                                      "%s exiting due to fatal errors!",
1271                                      PACKAGE_NAME);
1272                         exit(1);
1273                 }
1274         }
1275
1276 #ifdef STRICT_RFC
1277         if (!Conf_ServerAdminMail[0]) {
1278                 /* No administrative contact configured! */
1279                 config_valid = false;
1280                 Config_Error(LOG_ALERT,
1281                              "No administrator email address configured in \"%s\" ('AdminEMail')!",
1282                              NGIRCd_ConfFile);
1283                 if (!Configtest) {
1284                         Config_Error(LOG_ALERT,
1285                                      "%s exiting due to fatal errors!",
1286                                      PACKAGE_NAME);
1287                         exit(1);
1288                 }
1289         }
1290 #endif
1291
1292         if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1293             && !Conf_ServerAdminMail[0]) {
1294                 /* No administrative information configured! */
1295                 Config_Error(LOG_WARNING,
1296                              "No administrative information configured but required by RFC!");
1297         }
1298
1299 #ifdef DEBUG
1300         servers = servers_once = 0;
1301         for (i = 0; i < MAX_SERVERS; i++) {
1302                 if (Conf_Server[i].name[0]) {
1303                         servers++;
1304                         if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1305                                 servers_once++;
1306                 }
1307         }
1308         Log(LOG_DEBUG,
1309             "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1310             Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1311 #endif
1312
1313         return config_valid;
1314 } /* Validate_Config */
1315
1316
1317 static void
1318 Config_Error_TooLong ( const int Line, const char *Item )
1319 {
1320         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1321 }
1322
1323
1324 static void
1325 Config_Error_NaN( const int Line, const char *Item )
1326 {
1327         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1328                                                 NGIRCd_ConfFile, Line, Item );
1329 }
1330
1331
1332 #ifdef PROTOTYPES
1333 static void Config_Error( const int Level, const char *Format, ... )
1334 #else
1335 static void Config_Error( Level, Format, va_alist )
1336 const int Level;
1337 const char *Format;
1338 va_dcl
1339 #endif
1340 {
1341         /* Error! Write to console and/or logfile. */
1342
1343         char msg[MAX_LOG_MSG_LEN];
1344         va_list ap;
1345
1346         assert( Format != NULL );
1347
1348 #ifdef PROTOTYPES
1349         va_start( ap, Format );
1350 #else
1351         va_start( ap );
1352 #endif
1353         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1354         va_end( ap );
1355         
1356         /* During "normal operations" the log functions of the daemon should
1357          * be used, but during testing of the configuration file, all messages
1358          * should go directly to the console: */
1359         if (Use_Log) Log( Level, "%s", msg );
1360         else puts( msg );
1361 } /* Config_Error */
1362
1363
1364 static void
1365 Init_Server_Struct( CONF_SERVER *Server )
1366 {
1367         /* Initialize server configuration structur to default values */
1368
1369         assert( Server != NULL );
1370
1371         memset( Server, 0, sizeof (CONF_SERVER) );
1372
1373         Server->group = NONE;
1374         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1375
1376         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1377
1378         Resolve_Init(&Server->res_stat);
1379         Server->conn_id = NONE;
1380         memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1381 } /* Init_Server_Struct */
1382
1383
1384 /* -eof- */