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