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