]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
add const qualifier to pointers where possible
[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 true;
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 Read_Config( bool ngircd_starting )
547 {
548         /* Read configuration file. */
549
550         char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
551         const UINT16 defaultport = 6667;
552         int line, i, n;
553         FILE *fd;
554
555         /* Open configuration file */
556         fd = fopen( NGIRCd_ConfFile, "r" );
557         if( ! fd ) {
558                 /* No configuration file found! */
559                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
560                                         NGIRCd_ConfFile, strerror( errno ));
561                 if (!ngircd_starting)
562                         return false;
563                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
564                 exit( 1 );
565         }
566
567         Set_Defaults( ngircd_starting );
568
569         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
570
571         /* Clean up server configuration structure: mark all already
572          * configured servers as "once" so that they are deleted
573          * after the next disconnect and delete all unused servers.
574          * And delete all servers which are "duplicates" of servers
575          * that are already marked as "once" (such servers have been
576          * created by the last rehash but are now useless). */
577         for( i = 0; i < MAX_SERVERS; i++ ) {
578                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
579                 else {
580                         /* This structure is in use ... */
581                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
582                                 /* Check for duplicates */
583                                 for( n = 0; n < MAX_SERVERS; n++ ) {
584                                         if( n == i ) continue;
585
586                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
587                                                 Init_Server_Struct( &Conf_Server[n] );
588 #ifdef DEBUG
589                                                 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
590                                                                                                 n, i );
591 #endif
592                                         }
593                                 }
594                         } else {
595                                 /* Mark server as "once" */
596                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
597                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
598                         }
599                 }
600         }
601
602         /* Initialize variables */
603         line = 0;
604         strcpy( section, "" );
605         Init_Server_Struct( &New_Server );
606         New_Server_Idx = NONE;
607 #ifdef SSL_SUPPORT
608         ConfSSL_Init();
609 #endif
610         /* Read configuration file */
611         while( true ) {
612                 if( ! fgets( str, LINE_LEN, fd )) break;
613                 ngt_TrimStr( str );
614                 line++;
615
616                 /* Skip comments and empty lines */
617                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
618
619                 /* Is this the beginning of a new section? */
620                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
621                         strlcpy( section, str, sizeof( section ));
622                         if( strcasecmp( section, "[GLOBAL]" ) == 0 )
623                                 continue;
624
625                         if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
626                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
627                                         Config_Error( LOG_ERR, "Too many operators configured.");
628                                 else {
629                                         /* Initialize new operator structure */
630                                         Conf_Oper[Conf_Oper_Count].name[0] = '\0';
631                                         Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
632                                         if (Conf_Oper[Conf_Oper_Count].mask) {
633                                                 free(Conf_Oper[Conf_Oper_Count].mask );
634                                                 Conf_Oper[Conf_Oper_Count].mask = NULL;
635                                         }
636                                         Conf_Oper_Count++;
637                                 }
638                                 continue;
639                         }
640                         if( strcasecmp( section, "[SERVER]" ) == 0 ) {
641                                 /* Check if there is already a server to add */
642                                 if( New_Server.name[0] ) {
643                                         /* Copy data to "real" server structure */
644                                         assert( New_Server_Idx > NONE );
645                                         Conf_Server[New_Server_Idx] = New_Server;
646                                 }
647
648                                 /* Re-init structure for new server */
649                                 Init_Server_Struct( &New_Server );
650
651                                 /* Search unused item in server configuration structure */
652                                 for( i = 0; i < MAX_SERVERS; i++ ) {
653                                         /* Is this item used? */
654                                         if( ! Conf_Server[i].name[0] ) break;
655                                 }
656                                 if( i >= MAX_SERVERS ) {
657                                         /* Oops, no free item found! */
658                                         Config_Error( LOG_ERR, "Too many servers configured." );
659                                         New_Server_Idx = NONE;
660                                 }
661                                 else New_Server_Idx = i;
662                                 continue;
663                         }
664                         if (strcasecmp(section, "[CHANNEL]") == 0) {
665                                 Conf_Channel_Count++;
666                                 continue;
667                         }
668
669                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
670                         section[0] = 0x1;
671                 }
672                 if( section[0] == 0x1 ) continue;
673
674                 /* Split line into variable name and parameters */
675                 ptr = strchr( str, '=' );
676                 if( ! ptr ) {
677                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
678                         continue;
679                 }
680                 *ptr = '\0';
681                 var = str; ngt_TrimStr( var );
682                 arg = ptr + 1; ngt_TrimStr( arg );
683
684                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
685                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
686                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
687                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
688                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
689         }
690
691         /* Close configuration file */
692         fclose( fd );
693
694         /* Check if there is still a server to add */
695         if( New_Server.name[0] ) {
696                 /* Copy data to "real" server structure */
697                 assert( New_Server_Idx > NONE );
698                 Conf_Server[New_Server_Idx] = New_Server;
699         }
700
701         if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
702                 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
703                         Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
704                                                         (unsigned int) defaultport, strerror(errno));
705                         exit( 1 );
706                 }
707         }
708
709         if (!Conf_ListenAddress)
710                 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
711
712         if (!Conf_ListenAddress) {
713                 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
714                 exit(1);
715         }
716         return true;
717 } /* Read_Config */
718
719
720 static bool
721 Check_ArgIsTrue( const char *Arg )
722 {
723         if( strcasecmp( Arg, "yes" ) == 0 ) return true;
724         if( strcasecmp( Arg, "true" ) == 0 ) return true;
725         if( atoi( Arg ) != 0 ) return true;
726
727         return false;
728 } /* Check_ArgIsTrue */
729
730
731 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
732 {
733         unsigned new;
734
735         new = (unsigned) atoi(Arg) + 1;
736         if (new > CLIENT_NICK_LEN) {
737                 Config_Error(LOG_WARNING,
738                              "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
739                              NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
740                 return CLIENT_NICK_LEN;
741         }
742         if (new < 2) {
743                 Config_Error(LOG_WARNING,
744                              "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
745                              NGIRCd_ConfFile, Line);
746                 return 2;
747         }
748         return new;
749 } /* Handle_MaxNickLength */
750
751
752 static void
753 Handle_GLOBAL( int Line, char *Var, char *Arg )
754 {
755         struct passwd *pwd;
756         struct group *grp;
757         size_t len;
758         
759         assert( Line > 0 );
760         assert( Var != NULL );
761         assert( Arg != NULL );
762         
763         if( strcasecmp( Var, "Name" ) == 0 ) {
764                 /* Server name */
765                 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
766                 if (len >= sizeof( Conf_ServerName ))
767                         Config_Error_TooLong( Line, Var );
768                 return;
769         }
770         if( strcasecmp( Var, "Info" ) == 0 ) {
771                 /* Info text of server */
772                 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
773                 if (len >= sizeof( Conf_ServerInfo ))
774                         Config_Error_TooLong ( Line, Var );
775                 return;
776         }
777         if( strcasecmp( Var, "Password" ) == 0 ) {
778                 /* Global server password */
779                 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
780                 if (len >= sizeof( Conf_ServerPwd ))
781                         Config_Error_TooLong( Line, Var );
782                 return;
783         }
784         if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
785                 /* Administrative info #1 */
786                 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
787                 if (len >= sizeof( Conf_ServerAdmin1 ))
788                         Config_Error_TooLong ( Line, Var );
789                 return;
790         }
791         if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
792                 /* Administrative info #2 */
793                 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
794                 if (len >= sizeof( Conf_ServerAdmin2 ))
795                         Config_Error_TooLong ( Line, Var );
796                 return;
797         }
798         if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
799                 /* Administrative email contact */
800                 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
801                 if (len >= sizeof( Conf_ServerAdminMail ))
802                         Config_Error_TooLong( Line, Var );
803                 return;
804         }
805
806         if( strcasecmp( Var, "Ports" ) == 0 ) {
807                 ports_parse(&Conf_ListenPorts, Line, Arg);
808                 return;
809         }
810         if( strcasecmp( Var, "MotdFile" ) == 0 ) {
811                 /* "Message of the day" (MOTD) file */
812                 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
813                 if (len >= sizeof( Conf_MotdFile ))
814                         Config_Error_TooLong( Line, Var );
815                 return;
816         }
817         if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
818                 /* "Message of the day" phrase (instead of file) */
819                 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
820                 if (len >= sizeof( Conf_MotdPhrase ))
821                         Config_Error_TooLong( Line, Var );
822                 return;
823         }
824         if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
825                 /* directory for chroot() */
826                 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
827                 if (len >= sizeof( Conf_Chroot ))
828                         Config_Error_TooLong( Line, Var );
829                 return;
830         }
831         if ( strcasecmp( Var, "PidFile" ) == 0 ) {
832                 /* name of pidfile */
833                 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
834                 if (len >= sizeof( Conf_PidFile ))
835                         Config_Error_TooLong( Line, Var );
836                 return;
837         }
838         if( strcasecmp( Var, "ServerUID" ) == 0 ) {
839                 /* UID the daemon should switch to */
840                 pwd = getpwnam( Arg );
841                 if( pwd ) Conf_UID = pwd->pw_uid;
842                 else {
843 #ifdef HAVE_ISDIGIT
844                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
845                         else
846 #endif
847                         Conf_UID = (unsigned int)atoi( Arg );
848                 }
849                 return;
850         }
851         if( strcasecmp( Var, "ServerGID" ) == 0 ) {
852                 /* GID the daemon should use */
853                 grp = getgrnam( Arg );
854                 if( grp ) Conf_GID = grp->gr_gid;
855                 else {
856 #ifdef HAVE_ISDIGIT
857                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
858                         else
859 #endif
860                         Conf_GID = (unsigned int)atoi( Arg );
861                 }
862                 return;
863         }
864         if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
865                 /* PING timeout */
866                 Conf_PingTimeout = atoi( Arg );
867                 if( Conf_PingTimeout < 5 ) {
868                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
869                                                                         NGIRCd_ConfFile, Line );
870                         Conf_PingTimeout = 5;
871                 }
872                 return;
873         }
874         if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
875                 /* PONG timeout */
876                 Conf_PongTimeout = atoi( Arg );
877                 if( Conf_PongTimeout < 5 ) {
878                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
879                                                                         NGIRCd_ConfFile, Line );
880                         Conf_PongTimeout = 5;
881                 }
882                 return;
883         }
884         if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
885                 /* Seconds between connection attempts to other servers */
886                 Conf_ConnectRetry = atoi( Arg );
887                 if( Conf_ConnectRetry < 5 ) {
888                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
889                                                                         NGIRCd_ConfFile, Line );
890                         Conf_ConnectRetry = 5;
891                 }
892                 return;
893         }
894         if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
895                 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
896                 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
897                 return;
898         }
899         if( strcasecmp( Var, "NoDNS" ) == 0 ) {
900                 /* don't do reverse dns lookups when clients connect? */
901                 Conf_NoDNS = Check_ArgIsTrue( Arg );
902                 return;
903         }
904         if (strcasecmp(Var, "NoIdent") == 0) {
905                 /* don't do IDENT lookups when clients connect? */
906                 Conf_NoIdent = Check_ArgIsTrue(Arg);
907 #ifndef IDENTAUTH
908                 if (!Conf_NoIdent) {
909                         /* user has enabled ident lookups explicitly, but ... */
910                         Config_Error(LOG_WARNING,
911                                 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
912                                 NGIRCd_ConfFile, Line);
913                 }
914 #endif
915                 return;
916         }
917 #ifdef WANT_IPV6
918         /* the default setting for all the WANT_IPV6 special options is 'true' */
919         if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
920                 /* connect to other hosts using ipv6, if they have an AAAA record? */
921                 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
922                 return;
923         }
924         if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
925                 /* connect to other hosts using ipv4.
926                  * again, this can be used for ipv6-only setups */
927                 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
928                 return;
929         }
930 #endif
931         if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
932                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
933                 Conf_OperCanMode = Check_ArgIsTrue( Arg );
934                 return;
935         }
936         if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
937                 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
938                 Conf_OperServerMode = Check_ArgIsTrue( Arg );
939                 return;
940         }
941         if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
942                 /* Maximum number of connections. 0 -> "no limit". */
943 #ifdef HAVE_ISDIGIT
944                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
945                 else
946 #endif
947                 Conf_MaxConnections = atol( Arg );
948                 return;
949         }
950         if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
951                 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
952 #ifdef HAVE_ISDIGIT
953                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
954                 else
955 #endif
956                 Conf_MaxConnectionsIP = atoi( Arg );
957                 return;
958         }
959         if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
960                 /* Maximum number of channels a user can join. 0 -> "no limit". */
961 #ifdef HAVE_ISDIGIT
962                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
963                 else
964 #endif
965                 Conf_MaxJoins = atoi( Arg );
966                 return;
967         }
968         if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
969                 /* Maximum length of a nick name; must be same on all servers
970                  * within the IRC network! */
971                 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
972                 return;
973         }
974
975         if( strcasecmp( Var, "Listen" ) == 0 ) {
976                 /* IP-Address to bind sockets */
977                 if (Conf_ListenAddress) {
978                         Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
979                         return;
980                 }
981                 Conf_ListenAddress = strdup_warn(Arg);
982                 /*
983                  * if allocation fails, we're in trouble:
984                  * we cannot ignore the error -- otherwise ngircd
985                  * would listen on all interfaces.
986                  */
987                 if (!Conf_ListenAddress) {
988                         Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
989                         exit(1);
990                 }
991                 return;
992         }
993
994 #ifdef SSL_SUPPORT
995         if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
996                 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
997                 return;
998         }
999
1000         if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1001                 assert(Conf_SSLOptions.KeyFile == NULL );
1002                 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1003                 return;
1004         }
1005         if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1006                 assert(Conf_SSLOptions.CertFile == NULL );
1007                 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1008                 return;
1009         }
1010
1011         if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1012                 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1013                 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1014                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1015                                                                 NGIRCd_ConfFile, Line, Var, strerror(errno));
1016                 return;
1017         }
1018         if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1019                 assert(Conf_SSLOptions.DHFile == NULL);
1020                 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1021                 return;
1022         }
1023 #endif
1024         Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1025                                                                 NGIRCd_ConfFile, Line, Var);
1026 } /* Handle_GLOBAL */
1027
1028
1029 static void
1030 Handle_OPERATOR( int Line, char *Var, char *Arg )
1031 {
1032         unsigned int opercount;
1033         size_t len;
1034         assert( Line > 0 );
1035         assert( Var != NULL );
1036         assert( Arg != NULL );
1037         assert( Conf_Oper_Count > 0 );
1038
1039         if ( Conf_Oper_Count == 0 )
1040                 return;
1041
1042         opercount = Conf_Oper_Count - 1;
1043
1044         if( strcasecmp( Var, "Name" ) == 0 ) {
1045                 /* Name of IRC operator */
1046                 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1047                 if (len >= sizeof( Conf_Oper[opercount].name ))
1048                                 Config_Error_TooLong( Line, Var );
1049                 return;
1050         }
1051         if( strcasecmp( Var, "Password" ) == 0 ) {
1052                 /* Password of IRC operator */
1053                 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1054                 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1055                                 Config_Error_TooLong( Line, Var );
1056                 return;
1057         }
1058         if( strcasecmp( Var, "Mask" ) == 0 ) {
1059                 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1060
1061                 Conf_Oper[opercount].mask = strdup_warn( Arg );
1062                 return;
1063         }
1064         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1065                                                                 NGIRCd_ConfFile, Line, Var );
1066 } /* Handle_OPERATOR */
1067
1068
1069 static void
1070 Handle_SERVER( int Line, char *Var, char *Arg )
1071 {
1072         long port;
1073         size_t len;
1074         
1075         assert( Line > 0 );
1076         assert( Var != NULL );
1077         assert( Arg != NULL );
1078
1079         /* Ignore server block if no space is left in server configuration structure */
1080         if( New_Server_Idx <= NONE ) return;
1081
1082         if( strcasecmp( Var, "Host" ) == 0 ) {
1083                 /* Hostname of the server */
1084                 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1085                 if (len >= sizeof( New_Server.host ))
1086                         Config_Error_TooLong ( Line, Var );
1087                 return;
1088         }
1089         if( strcasecmp( Var, "Name" ) == 0 ) {
1090                 /* Name of the server ("Nick"/"ID") */
1091                 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1092                 if (len >= sizeof( New_Server.name ))
1093                         Config_Error_TooLong( Line, Var );
1094                 return;
1095         }
1096         if (strcasecmp(Var, "Bind") == 0) {
1097                 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1098                         return;
1099
1100                 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1101                                 NGIRCd_ConfFile, Line, Arg);
1102                 return;
1103         }
1104         if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1105                 /* Password of this server which is sent to the peer */
1106                 if (*Arg == ':') {
1107                         Config_Error(LOG_ERR,
1108                                 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1109                                                                                 NGIRCd_ConfFile, Line);
1110                 }
1111                 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1112                 if (len >= sizeof( New_Server.pwd_in ))
1113                         Config_Error_TooLong( Line, Var );
1114                 return;
1115         }
1116         if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1117                 /* Passwort of the peer which must be received */
1118                 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1119                 if (len >= sizeof( New_Server.pwd_out ))
1120                         Config_Error_TooLong( Line, Var );
1121                 return;
1122         }
1123         if( strcasecmp( Var, "Port" ) == 0 ) {
1124                 /* Port to which this server should connect */
1125                 port = atol( Arg );
1126                 if( port > 0 && port < 0xFFFF )
1127                         New_Server.port = (UINT16)port;
1128                 else
1129                         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1130                                                                                 NGIRCd_ConfFile, Line, port );
1131                 return;
1132         }
1133 #ifdef SSL_SUPPORT
1134         if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1135                 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1136                 return;
1137         }
1138 #endif
1139         if( strcasecmp( Var, "Group" ) == 0 ) {
1140                 /* Server group */
1141 #ifdef HAVE_ISDIGIT
1142                 if( ! isdigit( (int)*Arg ))
1143                         Config_Error_NaN( Line, Var );
1144                 else
1145 #endif
1146                 New_Server.group = atoi( Arg );
1147                 return;
1148         }
1149         if( strcasecmp( Var, "Passive" ) == 0 ) {
1150                 if (Check_ArgIsTrue(Arg))
1151                         New_Server.flags |= CONF_SFLAG_DISABLED;
1152                 return;
1153         }
1154         if (strcasecmp(Var, "ServiceMask") == 0) {
1155                 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1156                               sizeof(New_Server.svs_mask));
1157                 if (len >= sizeof(New_Server.svs_mask))
1158                         Config_Error_TooLong(Line, Var);
1159                 return;
1160         }
1161
1162         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1163                                                                 NGIRCd_ConfFile, Line, Var );
1164 } /* Handle_SERVER */
1165
1166
1167 static bool
1168 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1169 {
1170         size_t size = sizeof(new_chan->name);
1171         char *dest = new_chan->name;
1172
1173         if (!Channel_IsValidName(name)) {
1174                 /*
1175                  * maybe user forgot to add a '#'.
1176                  * This is only here for user convenience.
1177                  */
1178                 *dest = '#';
1179                 --size;
1180                 ++dest;
1181         }
1182         return size > strlcpy(dest, name, size);
1183 }
1184
1185
1186 static void
1187 Handle_CHANNEL(int Line, char *Var, char *Arg)
1188 {
1189         size_t len;
1190         size_t chancount;
1191         struct Conf_Channel *chan;
1192
1193         assert( Line > 0 );
1194         assert( Var != NULL );
1195         assert( Arg != NULL );
1196         assert(Conf_Channel_Count > 0);
1197
1198         chancount = Conf_Channel_Count - 1;
1199
1200         chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1201         if (!chan) {
1202                 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1203                 return;
1204         }
1205         if (strcasecmp(Var, "Name") == 0) {
1206                 if (!Handle_Channelname(chan, Arg))
1207                         Config_Error_TooLong(Line, Var);
1208                 return;
1209         }
1210         if (strcasecmp(Var, "Modes") == 0) {
1211                 /* Initial modes */
1212                 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1213                 if (len >= sizeof(chan->modes))
1214                         Config_Error_TooLong( Line, Var );
1215                 return;
1216         }
1217         if( strcasecmp( Var, "Topic" ) == 0 ) {
1218                 /* Initial topic */
1219                 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1220                 if (len >= sizeof(chan->topic))
1221                         Config_Error_TooLong( Line, Var );
1222                 return;
1223         }
1224         if( strcasecmp( Var, "Key" ) == 0 ) {
1225                 /* Initial Channel Key (mode k) */
1226                 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1227                 if (len >= sizeof(chan->key))
1228                         Config_Error_TooLong(Line, Var);
1229                 return;
1230         }
1231         if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1232                 /* maximum user limit, mode l */
1233                 chan->maxusers = (unsigned long) atol(Arg);
1234                 if (chan->maxusers == 0)
1235                         Config_Error_NaN(Line, Var);
1236                 return;
1237         }
1238         if (strcasecmp(Var, "KeyFile") == 0) {
1239                 /* channel keys */
1240                 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1241                 if (len >= sizeof(chan->keyfile))
1242                         Config_Error_TooLong(Line, Var);
1243                 return;
1244         }
1245
1246         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1247                                                                 NGIRCd_ConfFile, Line, Var );
1248 } /* Handle_CHANNEL */
1249
1250
1251 static bool
1252 Validate_Config(bool Configtest, bool Rehash)
1253 {
1254         /* Validate configuration settings. */
1255
1256 #ifdef DEBUG
1257         int i, servers, servers_once;
1258 #endif
1259         bool config_valid = true;
1260         char *ptr;
1261
1262         /* Validate configured server name, see RFC 2812 section 2.3.1 */
1263         ptr = Conf_ServerName;
1264         do {
1265                 if (*ptr >= 'a' && *ptr <= 'z') continue;
1266                 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1267                 if (*ptr >= '0' && *ptr <= '9') continue;
1268                 if (ptr > Conf_ServerName) {
1269                         if (*ptr == '.' || *ptr == '-')
1270                                 continue;
1271                 }
1272                 Conf_ServerName[0] = '\0';
1273                 break;
1274         } while (*(++ptr));
1275
1276         if (!Conf_ServerName[0]) {
1277                 /* No server name configured! */
1278                 config_valid = false;
1279                 Config_Error(LOG_ALERT,
1280                              "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1281                              NGIRCd_ConfFile);
1282                 if (!Configtest && !Rehash) {
1283                         Config_Error(LOG_ALERT,
1284                                      "%s exiting due to fatal errors!",
1285                                      PACKAGE_NAME);
1286                         exit(1);
1287                 }
1288         }
1289
1290         if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1291                 /* No dot in server name! */
1292                 config_valid = false;
1293                 Config_Error(LOG_ALERT,
1294                              "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1295                              NGIRCd_ConfFile);
1296                 if (!Configtest) {
1297                         Config_Error(LOG_ALERT,
1298                                      "%s exiting due to fatal errors!",
1299                                      PACKAGE_NAME);
1300                         exit(1);
1301                 }
1302         }
1303
1304 #ifdef STRICT_RFC
1305         if (!Conf_ServerAdminMail[0]) {
1306                 /* No administrative contact configured! */
1307                 config_valid = false;
1308                 Config_Error(LOG_ALERT,
1309                              "No administrator email address configured in \"%s\" ('AdminEMail')!",
1310                              NGIRCd_ConfFile);
1311                 if (!Configtest) {
1312                         Config_Error(LOG_ALERT,
1313                                      "%s exiting due to fatal errors!",
1314                                      PACKAGE_NAME);
1315                         exit(1);
1316                 }
1317         }
1318 #endif
1319
1320         if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1321             && !Conf_ServerAdminMail[0]) {
1322                 /* No administrative information configured! */
1323                 Config_Error(LOG_WARNING,
1324                              "No administrative information configured but required by RFC!");
1325         }
1326
1327 #ifdef DEBUG
1328         servers = servers_once = 0;
1329         for (i = 0; i < MAX_SERVERS; i++) {
1330                 if (Conf_Server[i].name[0]) {
1331                         servers++;
1332                         if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1333                                 servers_once++;
1334                 }
1335         }
1336         Log(LOG_DEBUG,
1337             "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1338             Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1339 #endif
1340
1341         return config_valid;
1342 } /* Validate_Config */
1343
1344
1345 static void
1346 Config_Error_TooLong ( const int Line, const char *Item )
1347 {
1348         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1349 }
1350
1351
1352 static void
1353 Config_Error_NaN( const int Line, const char *Item )
1354 {
1355         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1356                                                 NGIRCd_ConfFile, Line, Item );
1357 }
1358
1359
1360 #ifdef PROTOTYPES
1361 static void Config_Error( const int Level, const char *Format, ... )
1362 #else
1363 static void Config_Error( Level, Format, va_alist )
1364 const int Level;
1365 const char *Format;
1366 va_dcl
1367 #endif
1368 {
1369         /* Error! Write to console and/or logfile. */
1370
1371         char msg[MAX_LOG_MSG_LEN];
1372         va_list ap;
1373
1374         assert( Format != NULL );
1375
1376 #ifdef PROTOTYPES
1377         va_start( ap, Format );
1378 #else
1379         va_start( ap );
1380 #endif
1381         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1382         va_end( ap );
1383         
1384         /* During "normal operations" the log functions of the daemon should
1385          * be used, but during testing of the configuration file, all messages
1386          * should go directly to the console: */
1387         if (Use_Log) Log( Level, "%s", msg );
1388         else puts( msg );
1389 } /* Config_Error */
1390
1391
1392 static void
1393 Init_Server_Struct( CONF_SERVER *Server )
1394 {
1395         /* Initialize server configuration structur to default values */
1396
1397         assert( Server != NULL );
1398
1399         memset( Server, 0, sizeof (CONF_SERVER) );
1400
1401         Server->group = NONE;
1402         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1403
1404         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1405
1406         Resolve_Init(&Server->res_stat);
1407         Server->conn_id = NONE;
1408         memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1409 } /* Init_Server_Struct */
1410
1411
1412 /* -eof- */