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