c82ac2bf1fbf57814a69ce13d3076e30100854b5
[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 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\n", predef_chan->topic);
317         }
318
319         return (config_valid ? 0 : 1);
320 } /* Conf_Test */
321
322
323 GLOBAL void
324 Conf_UnsetServer( CONN_ID Idx )
325 {
326         /* Set next time for next connection attempt, if this is a server
327          * link that is (still) configured here. If the server is set as
328          * "once", delete it from our configuration.
329          * Non-Server-Connections will be silently ignored. */
330
331         int i;
332         time_t t;
333
334         /* Check all our configured servers */
335         for( i = 0; i < MAX_SERVERS; i++ ) {
336                 if( Conf_Server[i].conn_id != Idx ) continue;
337
338                 /* Gotcha! Mark server configuration as "unused": */
339                 Conf_Server[i].conn_id = NONE;
340
341                 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
342                         /* Delete configuration here */
343                         Init_Server_Struct( &Conf_Server[i] );
344                 } else {
345                         /* Set time for next connect attempt */
346                         t = time(NULL);
347                         if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
348                                 /* The connection has been "long", so we don't
349                                  * require the next attempt to be delayed. */
350                                 Conf_Server[i].lasttry =
351                                         t - Conf_ConnectRetry + RECONNECT_DELAY;
352                         } else
353                                 Conf_Server[i].lasttry = t;
354                 }
355         }
356 } /* Conf_UnsetServer */
357
358
359 GLOBAL void
360 Conf_SetServer( int ConfServer, CONN_ID Idx )
361 {
362         /* Set connection for specified configured server */
363
364         assert( ConfServer > NONE );
365         assert( Idx > NONE );
366
367         Conf_Server[ConfServer].conn_id = Idx;
368 } /* Conf_SetServer */
369
370
371 GLOBAL int
372 Conf_GetServer( CONN_ID Idx )
373 {
374         /* Get index of server in configuration structure */
375
376         int i = 0;
377
378         assert( Idx > NONE );
379
380         for( i = 0; i < MAX_SERVERS; i++ ) {
381                 if( Conf_Server[i].conn_id == Idx ) return i;
382         }
383         return NONE;
384 } /* Conf_GetServer */
385
386
387 GLOBAL bool
388 Conf_EnableServer( char *Name, UINT16 Port )
389 {
390         /* Enable specified server and adjust port */
391
392         int i;
393
394         assert( Name != NULL );
395
396         for( i = 0; i < MAX_SERVERS; i++ ) {
397                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
398                         /* Gotcha! Set port and enable server: */
399                         Conf_Server[i].port = Port;
400                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
401                         return true;
402                 }
403         }
404         return false;
405 } /* Conf_EnableServer */
406
407
408 GLOBAL bool
409 Conf_EnablePassiveServer(const char *Name)
410 {
411         /* Enable specified server */
412         int i;
413
414         assert( Name != NULL );
415         for (i = 0; i < MAX_SERVERS; i++) {
416                 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
417                         /* BINGO! Enable server */
418                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
419                         return true;
420                 }
421         }
422         return false;
423 } /* Conf_EnablePassiveServer */
424
425
426 GLOBAL bool
427 Conf_DisableServer( char *Name )
428 {
429         /* Enable specified server and adjust port */
430
431         int i;
432
433         assert( Name != NULL );
434
435         for( i = 0; i < MAX_SERVERS; i++ ) {
436                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
437                         /* Gotcha! Disable and disconnect server: */
438                         Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
439                         if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
440                         return true;
441                 }
442         }
443         return false;
444 } /* Conf_DisableServer */
445
446
447 GLOBAL bool
448 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
449 {
450         /* Add new server to configuration */
451
452         int i;
453
454         assert( Name != NULL );
455         assert( Host != NULL );
456         assert( MyPwd != NULL );
457         assert( PeerPwd != NULL );
458
459         /* Search unused item in server configuration structure */
460         for( i = 0; i < MAX_SERVERS; i++ ) {
461                 /* Is this item used? */
462                 if( ! Conf_Server[i].name[0] ) break;
463         }
464         if( i >= MAX_SERVERS ) return false;
465
466         Init_Server_Struct( &Conf_Server[i] );
467         strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
468         strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
469         strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
470         strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
471         Conf_Server[i].port = Port;
472         Conf_Server[i].flags = CONF_SFLAG_ONCE;
473
474         return true;
475 } /* Conf_AddServer */
476
477
478 /**
479  * Check if the given nick name is an service
480  */
481 GLOBAL bool
482 Conf_IsService(int ConfServer, char *Nick)
483 {
484         return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
485 } /* Conf_IsService */
486
487
488 static void
489 Set_Defaults( bool InitServers )
490 {
491         /* Initialize configuration variables with default values. */
492
493         int i;
494
495         strcpy( Conf_ServerName, "" );
496         snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
497         strcpy( Conf_ServerPwd, "" );
498
499         strcpy( Conf_ServerAdmin1, "" );
500         strcpy( Conf_ServerAdmin2, "" );
501         strcpy( Conf_ServerAdminMail, "" );
502
503         strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
504         strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
505
506         strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
507
508         strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
509
510         strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
511
512         free(Conf_ListenAddress);
513         Conf_ListenAddress = NULL;
514         Conf_UID = Conf_GID = 0;
515
516         Conf_PingTimeout = 120;
517         Conf_PongTimeout = 20;
518
519         Conf_ConnectRetry = 60;
520
521         Conf_Oper_Count = 0;
522         Conf_Channel_Count = 0;
523
524         Conf_OperCanMode = false;
525         Conf_NoDNS = false;
526         Conf_NoIdent = false;
527         Conf_PredefChannelsOnly = false;
528         Conf_OperServerMode = false;
529
530         Conf_ConnectIPv4 = true;
531         Conf_ConnectIPv6 = true;
532
533         Conf_MaxConnections = 0;
534         Conf_MaxConnectionsIP = 5;
535         Conf_MaxJoins = 10;
536         Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
537
538         /* Initialize server configuration structures */
539         if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
540 } /* Set_Defaults */
541
542
543 static bool
544 Read_Config( bool ngircd_starting )
545 {
546         /* Read configuration file. */
547
548         char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
549         const UINT16 defaultport = 6667;
550         int line, i, n;
551         FILE *fd;
552
553         /* Open configuration file */
554         fd = fopen( NGIRCd_ConfFile, "r" );
555         if( ! fd ) {
556                 /* No configuration file found! */
557                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
558                                         NGIRCd_ConfFile, strerror( errno ));
559                 if (!ngircd_starting)
560                         return false;
561                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
562                 exit( 1 );
563         }
564
565         Set_Defaults( ngircd_starting );
566
567         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
568
569         /* Clean up server configuration structure: mark all already
570          * configured servers as "once" so that they are deleted
571          * after the next disconnect and delete all unused servers.
572          * And delete all servers which are "duplicates" of servers
573          * that are already marked as "once" (such servers have been
574          * created by the last rehash but are now useless). */
575         for( i = 0; i < MAX_SERVERS; i++ ) {
576                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
577                 else {
578                         /* This structure is in use ... */
579                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
580                                 /* Check for duplicates */
581                                 for( n = 0; n < MAX_SERVERS; n++ ) {
582                                         if( n == i ) continue;
583
584                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
585                                                 Init_Server_Struct( &Conf_Server[n] );
586 #ifdef DEBUG
587                                                 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
588                                                                                                 n, i );
589 #endif
590                                         }
591                                 }
592                         } else {
593                                 /* Mark server as "once" */
594                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
595                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
596                         }
597                 }
598         }
599
600         /* Initialize variables */
601         line = 0;
602         strcpy( section, "" );
603         Init_Server_Struct( &New_Server );
604         New_Server_Idx = NONE;
605 #ifdef SSL_SUPPORT
606         ConfSSL_Init();
607 #endif
608         /* Read configuration file */
609         while( true ) {
610                 if( ! fgets( str, LINE_LEN, fd )) break;
611                 ngt_TrimStr( str );
612                 line++;
613
614                 /* Skip comments and empty lines */
615                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
616
617                 /* Is this the beginning of a new section? */
618                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
619                         strlcpy( section, str, sizeof( section ));
620                         if( strcasecmp( section, "[GLOBAL]" ) == 0 )
621                                 continue;
622
623                         if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
624                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
625                                         Config_Error( LOG_ERR, "Too many operators configured.");
626                                 else {
627                                         /* Initialize new operator structure */
628                                         Conf_Oper[Conf_Oper_Count].name[0] = '\0';
629                                         Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
630                                         if (Conf_Oper[Conf_Oper_Count].mask) {
631                                                 free(Conf_Oper[Conf_Oper_Count].mask );
632                                                 Conf_Oper[Conf_Oper_Count].mask = NULL;
633                                         }
634                                         Conf_Oper_Count++;
635                                 }
636                                 continue;
637                         }
638                         if( strcasecmp( section, "[SERVER]" ) == 0 ) {
639                                 /* Check if there is already a server to add */
640                                 if( New_Server.name[0] ) {
641                                         /* Copy data to "real" server structure */
642                                         assert( New_Server_Idx > NONE );
643                                         Conf_Server[New_Server_Idx] = New_Server;
644                                 }
645
646                                 /* Re-init structure for new server */
647                                 Init_Server_Struct( &New_Server );
648
649                                 /* Search unused item in server configuration structure */
650                                 for( i = 0; i < MAX_SERVERS; i++ ) {
651                                         /* Is this item used? */
652                                         if( ! Conf_Server[i].name[0] ) break;
653                                 }
654                                 if( i >= MAX_SERVERS ) {
655                                         /* Oops, no free item found! */
656                                         Config_Error( LOG_ERR, "Too many servers configured." );
657                                         New_Server_Idx = NONE;
658                                 }
659                                 else New_Server_Idx = i;
660                                 continue;
661                         }
662                         if (strcasecmp(section, "[CHANNEL]") == 0) {
663                                 Conf_Channel_Count++;
664                                 continue;
665                         }
666
667                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
668                         section[0] = 0x1;
669                 }
670                 if( section[0] == 0x1 ) continue;
671
672                 /* Split line into variable name and parameters */
673                 ptr = strchr( str, '=' );
674                 if( ! ptr ) {
675                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
676                         continue;
677                 }
678                 *ptr = '\0';
679                 var = str; ngt_TrimStr( var );
680                 arg = ptr + 1; ngt_TrimStr( arg );
681
682                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
683                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
684                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
685                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
686                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
687         }
688
689         /* Close configuration file */
690         fclose( fd );
691
692         /* Check if there is still a server to add */
693         if( New_Server.name[0] ) {
694                 /* Copy data to "real" server structure */
695                 assert( New_Server_Idx > NONE );
696                 Conf_Server[New_Server_Idx] = New_Server;
697         }
698
699         if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
700                 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
701                         Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
702                                                         (unsigned int) defaultport, strerror(errno));
703                         exit( 1 );
704                 }
705         }
706
707         if (!Conf_ListenAddress)
708                 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
709
710         if (!Conf_ListenAddress) {
711                 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
712                 exit(1);
713         }
714         return true;
715 } /* Read_Config */
716
717
718 static bool
719 Check_ArgIsTrue( const char *Arg )
720 {
721         if( strcasecmp( Arg, "yes" ) == 0 ) return true;
722         if( strcasecmp( Arg, "true" ) == 0 ) return true;
723         if( atoi( Arg ) != 0 ) return true;
724
725         return false;
726 } /* Check_ArgIsTrue */
727
728
729 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
730 {
731         unsigned new;
732
733         new = (unsigned) atoi(Arg) + 1;
734         if (new > CLIENT_NICK_LEN) {
735                 Config_Error(LOG_WARNING,
736                              "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
737                              NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
738                 return CLIENT_NICK_LEN;
739         }
740         if (new < 2) {
741                 Config_Error(LOG_WARNING,
742                              "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
743                              NGIRCd_ConfFile, Line);
744                 return 2;
745         }
746         return new;
747 } /* Handle_MaxNickLength */
748
749
750 static void
751 Handle_GLOBAL( int Line, char *Var, char *Arg )
752 {
753         struct passwd *pwd;
754         struct group *grp;
755         size_t len;
756         
757         assert( Line > 0 );
758         assert( Var != NULL );
759         assert( Arg != NULL );
760         
761         if( strcasecmp( Var, "Name" ) == 0 ) {
762                 /* Server name */
763                 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
764                 if (len >= sizeof( Conf_ServerName ))
765                         Config_Error_TooLong( Line, Var );
766                 return;
767         }
768         if( strcasecmp( Var, "Info" ) == 0 ) {
769                 /* Info text of server */
770                 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
771                 if (len >= sizeof( Conf_ServerInfo ))
772                         Config_Error_TooLong ( Line, Var );
773                 return;
774         }
775         if( strcasecmp( Var, "Password" ) == 0 ) {
776                 /* Global server password */
777                 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
778                 if (len >= sizeof( Conf_ServerPwd ))
779                         Config_Error_TooLong( Line, Var );
780                 return;
781         }
782         if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
783                 /* Administrative info #1 */
784                 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
785                 if (len >= sizeof( Conf_ServerAdmin1 ))
786                         Config_Error_TooLong ( Line, Var );
787                 return;
788         }
789         if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
790                 /* Administrative info #2 */
791                 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
792                 if (len >= sizeof( Conf_ServerAdmin2 ))
793                         Config_Error_TooLong ( Line, Var );
794                 return;
795         }
796         if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
797                 /* Administrative email contact */
798                 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
799                 if (len >= sizeof( Conf_ServerAdminMail ))
800                         Config_Error_TooLong( Line, Var );
801                 return;
802         }
803
804         if( strcasecmp( Var, "Ports" ) == 0 ) {
805                 ports_parse(&Conf_ListenPorts, Line, Arg);
806                 return;
807         }
808         if( strcasecmp( Var, "MotdFile" ) == 0 ) {
809                 /* "Message of the day" (MOTD) file */
810                 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
811                 if (len >= sizeof( Conf_MotdFile ))
812                         Config_Error_TooLong( Line, Var );
813                 return;
814         }
815         if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
816                 /* "Message of the day" phrase (instead of file) */
817                 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
818                 if (len >= sizeof( Conf_MotdPhrase ))
819                         Config_Error_TooLong( Line, Var );
820                 return;
821         }
822         if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
823                 /* directory for chroot() */
824                 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
825                 if (len >= sizeof( Conf_Chroot ))
826                         Config_Error_TooLong( Line, Var );
827                 return;
828         }
829         if ( strcasecmp( Var, "PidFile" ) == 0 ) {
830                 /* name of pidfile */
831                 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
832                 if (len >= sizeof( Conf_PidFile ))
833                         Config_Error_TooLong( Line, Var );
834                 return;
835         }
836         if( strcasecmp( Var, "ServerUID" ) == 0 ) {
837                 /* UID the daemon should switch to */
838                 pwd = getpwnam( Arg );
839                 if( pwd ) Conf_UID = pwd->pw_uid;
840                 else {
841 #ifdef HAVE_ISDIGIT
842                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
843                         else
844 #endif
845                         Conf_UID = (unsigned int)atoi( Arg );
846                 }
847                 return;
848         }
849         if( strcasecmp( Var, "ServerGID" ) == 0 ) {
850                 /* GID the daemon should use */
851                 grp = getgrnam( Arg );
852                 if( grp ) Conf_GID = grp->gr_gid;
853                 else {
854 #ifdef HAVE_ISDIGIT
855                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
856                         else
857 #endif
858                         Conf_GID = (unsigned int)atoi( Arg );
859                 }
860                 return;
861         }
862         if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
863                 /* PING timeout */
864                 Conf_PingTimeout = atoi( Arg );
865                 if( Conf_PingTimeout < 5 ) {
866                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
867                                                                         NGIRCd_ConfFile, Line );
868                         Conf_PingTimeout = 5;
869                 }
870                 return;
871         }
872         if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
873                 /* PONG timeout */
874                 Conf_PongTimeout = atoi( Arg );
875                 if( Conf_PongTimeout < 5 ) {
876                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
877                                                                         NGIRCd_ConfFile, Line );
878                         Conf_PongTimeout = 5;
879                 }
880                 return;
881         }
882         if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
883                 /* Seconds between connection attempts to other servers */
884                 Conf_ConnectRetry = atoi( Arg );
885                 if( Conf_ConnectRetry < 5 ) {
886                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
887                                                                         NGIRCd_ConfFile, Line );
888                         Conf_ConnectRetry = 5;
889                 }
890                 return;
891         }
892         if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
893                 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
894                 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
895                 return;
896         }
897         if( strcasecmp( Var, "NoDNS" ) == 0 ) {
898                 /* don't do reverse dns lookups when clients connect? */
899                 Conf_NoDNS = Check_ArgIsTrue( Arg );
900                 return;
901         }
902         if (strcasecmp(Var, "NoIdent") == 0) {
903                 /* don't do IDENT lookups when clients connect? */
904                 Conf_NoIdent = Check_ArgIsTrue(Arg);
905 #ifndef IDENTAUTH
906                 if (!Conf_NoIdent) {
907                         /* user has enabled ident lookups explicitly, but ... */
908                         Config_Error(LOG_WARNING,
909                                 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
910                                 NGIRCd_ConfFile, Line);
911                 }
912 #endif
913                 return;
914         }
915 #ifdef WANT_IPV6
916         /* the default setting for all the WANT_IPV6 special options is 'true' */
917         if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
918                 /* connect to other hosts using ipv6, if they have an AAAA record? */
919                 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
920                 return;
921         }
922         if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
923                 /* connect to other hosts using ipv4.
924                  * again, this can be used for ipv6-only setups */
925                 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
926                 return;
927         }
928 #endif
929         if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
930                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
931                 Conf_OperCanMode = Check_ArgIsTrue( Arg );
932                 return;
933         }
934         if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
935                 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
936                 Conf_OperServerMode = Check_ArgIsTrue( Arg );
937                 return;
938         }
939         if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
940                 /* Maximum number of connections. 0 -> "no limit". */
941 #ifdef HAVE_ISDIGIT
942                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
943                 else
944 #endif
945                 Conf_MaxConnections = atol( Arg );
946                 return;
947         }
948         if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
949                 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
950 #ifdef HAVE_ISDIGIT
951                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
952                 else
953 #endif
954                 Conf_MaxConnectionsIP = atoi( Arg );
955                 return;
956         }
957         if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
958                 /* Maximum number of channels a user can join. 0 -> "no limit". */
959 #ifdef HAVE_ISDIGIT
960                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
961                 else
962 #endif
963                 Conf_MaxJoins = atoi( Arg );
964                 return;
965         }
966         if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
967                 /* Maximum length of a nick name; must be same on all servers
968                  * within the IRC network! */
969                 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
970                 return;
971         }
972
973         if( strcasecmp( Var, "Listen" ) == 0 ) {
974                 /* IP-Address to bind sockets */
975                 if (Conf_ListenAddress) {
976                         Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
977                         return;
978                 }
979                 Conf_ListenAddress = strdup_warn(Arg);
980                 /*
981                  * if allocation fails, we're in trouble:
982                  * we cannot ignore the error -- otherwise ngircd
983                  * would listen on all interfaces.
984                  */
985                 if (!Conf_ListenAddress) {
986                         Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
987                         exit(1);
988                 }
989                 return;
990         }
991
992 #ifdef SSL_SUPPORT
993         if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
994                 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
995                 return;
996         }
997
998         if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
999                 assert(Conf_SSLOptions.KeyFile == NULL );
1000                 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1001                 return;
1002         }
1003         if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1004                 assert(Conf_SSLOptions.CertFile == NULL );
1005                 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1006                 return;
1007         }
1008
1009         if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1010                 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1011                 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1012                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1013                                                                 NGIRCd_ConfFile, Line, Var, strerror(errno));
1014                 return;
1015         }
1016         if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1017                 assert(Conf_SSLOptions.DHFile == NULL);
1018                 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1019                 return;
1020         }
1021 #endif
1022         Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1023                                                                 NGIRCd_ConfFile, Line, Var);
1024 } /* Handle_GLOBAL */
1025
1026
1027 static void
1028 Handle_OPERATOR( int Line, char *Var, char *Arg )
1029 {
1030         unsigned int opercount;
1031         size_t len;
1032         assert( Line > 0 );
1033         assert( Var != NULL );
1034         assert( Arg != NULL );
1035         assert( Conf_Oper_Count > 0 );
1036
1037         if ( Conf_Oper_Count == 0 )
1038                 return;
1039
1040         opercount = Conf_Oper_Count - 1;
1041
1042         if( strcasecmp( Var, "Name" ) == 0 ) {
1043                 /* Name of IRC operator */
1044                 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1045                 if (len >= sizeof( Conf_Oper[opercount].name ))
1046                                 Config_Error_TooLong( Line, Var );
1047                 return;
1048         }
1049         if( strcasecmp( Var, "Password" ) == 0 ) {
1050                 /* Password of IRC operator */
1051                 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1052                 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1053                                 Config_Error_TooLong( Line, Var );
1054                 return;
1055         }
1056         if( strcasecmp( Var, "Mask" ) == 0 ) {
1057                 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1058
1059                 Conf_Oper[opercount].mask = strdup_warn( Arg );
1060                 return;
1061         }
1062         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1063                                                                 NGIRCd_ConfFile, Line, Var );
1064 } /* Handle_OPERATOR */
1065
1066
1067 static void
1068 Handle_SERVER( int Line, char *Var, char *Arg )
1069 {
1070         long port;
1071         size_t len;
1072         
1073         assert( Line > 0 );
1074         assert( Var != NULL );
1075         assert( Arg != NULL );
1076
1077         /* Ignore server block if no space is left in server configuration structure */
1078         if( New_Server_Idx <= NONE ) return;
1079
1080         if( strcasecmp( Var, "Host" ) == 0 ) {
1081                 /* Hostname of the server */
1082                 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1083                 if (len >= sizeof( New_Server.host ))
1084                         Config_Error_TooLong ( Line, Var );
1085                 return;
1086         }
1087         if( strcasecmp( Var, "Name" ) == 0 ) {
1088                 /* Name of the server ("Nick"/"ID") */
1089                 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1090                 if (len >= sizeof( New_Server.name ))
1091                         Config_Error_TooLong( Line, Var );
1092                 return;
1093         }
1094         if (strcasecmp(Var, "Bind") == 0) {
1095                 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1096                         return;
1097
1098                 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1099                                 NGIRCd_ConfFile, Line, Arg);
1100                 return;
1101         }
1102         if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1103                 /* Password of this server which is sent to the peer */
1104                 if (*Arg == ':') {
1105                         Config_Error(LOG_ERR,
1106                                 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1107                                                                                 NGIRCd_ConfFile, Line);
1108                 }
1109                 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1110                 if (len >= sizeof( New_Server.pwd_in ))
1111                         Config_Error_TooLong( Line, Var );
1112                 return;
1113         }
1114         if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1115                 /* Passwort of the peer which must be received */
1116                 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1117                 if (len >= sizeof( New_Server.pwd_out ))
1118                         Config_Error_TooLong( Line, Var );
1119                 return;
1120         }
1121         if( strcasecmp( Var, "Port" ) == 0 ) {
1122                 /* Port to which this server should connect */
1123                 port = atol( Arg );
1124                 if( port > 0 && port < 0xFFFF )
1125                         New_Server.port = (UINT16)port;
1126                 else
1127                         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1128                                                                                 NGIRCd_ConfFile, Line, port );
1129                 return;
1130         }
1131 #ifdef SSL_SUPPORT
1132         if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1133                 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1134                 return;
1135         }
1136 #endif
1137         if( strcasecmp( Var, "Group" ) == 0 ) {
1138                 /* Server group */
1139 #ifdef HAVE_ISDIGIT
1140                 if( ! isdigit( (int)*Arg ))
1141                         Config_Error_NaN( Line, Var );
1142                 else
1143 #endif
1144                 New_Server.group = atoi( Arg );
1145                 return;
1146         }
1147         if( strcasecmp( Var, "Passive" ) == 0 ) {
1148                 if (Check_ArgIsTrue(Arg))
1149                         New_Server.flags |= CONF_SFLAG_DISABLED;
1150                 return;
1151         }
1152         if (strcasecmp(Var, "ServiceMask") == 0) {
1153                 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1154                               sizeof(New_Server.svs_mask));
1155                 if (len >= sizeof(New_Server.svs_mask))
1156                         Config_Error_TooLong(Line, Var);
1157                 return;
1158         }
1159
1160         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1161                                                                 NGIRCd_ConfFile, Line, Var );
1162 } /* Handle_SERVER */
1163
1164
1165 static bool
1166 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1167 {
1168         size_t size = sizeof(new_chan->name);
1169         char *dest = new_chan->name;
1170
1171         /*
1172          * channels must begin with &, +, or '#', if it is
1173          * missing, add a '#'. This is only here for user convenience.
1174          */
1175         if (*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
1237         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1238                                                                 NGIRCd_ConfFile, Line, Var );
1239 } /* Handle_CHANNEL */
1240
1241
1242 static bool
1243 Validate_Config(bool Configtest, bool Rehash)
1244 {
1245         /* Validate configuration settings. */
1246
1247 #ifdef DEBUG
1248         int i, servers, servers_once;
1249 #endif
1250         bool config_valid = true;
1251         char *ptr;
1252
1253         /* Validate configured server name, see RFC 2812 section 2.3.1 */
1254         ptr = Conf_ServerName;
1255         do {
1256                 if (*ptr >= 'a' && *ptr <= 'z') continue;
1257                 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1258                 if (*ptr >= '0' && *ptr <= '9') continue;
1259                 if (ptr > Conf_ServerName) {
1260                         if (*ptr == '.' || *ptr == '-')
1261                                 continue;
1262                 }
1263                 Conf_ServerName[0] = '\0';
1264                 break;
1265         } while (*(++ptr));
1266
1267         if (!Conf_ServerName[0]) {
1268                 /* No server name configured! */
1269                 config_valid = false;
1270                 Config_Error(LOG_ALERT,
1271                              "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1272                              NGIRCd_ConfFile);
1273                 if (!Configtest && !Rehash) {
1274                         Config_Error(LOG_ALERT,
1275                                      "%s exiting due to fatal errors!",
1276                                      PACKAGE_NAME);
1277                         exit(1);
1278                 }
1279         }
1280
1281         if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1282                 /* No dot in server name! */
1283                 config_valid = false;
1284                 Config_Error(LOG_ALERT,
1285                              "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1286                              NGIRCd_ConfFile);
1287                 if (!Configtest) {
1288                         Config_Error(LOG_ALERT,
1289                                      "%s exiting due to fatal errors!",
1290                                      PACKAGE_NAME);
1291                         exit(1);
1292                 }
1293         }
1294
1295 #ifdef STRICT_RFC
1296         if (!Conf_ServerAdminMail[0]) {
1297                 /* No administrative contact configured! */
1298                 config_valid = false;
1299                 Config_Error(LOG_ALERT,
1300                              "No administrator email address configured in \"%s\" ('AdminEMail')!",
1301                              NGIRCd_ConfFile);
1302                 if (!Configtest) {
1303                         Config_Error(LOG_ALERT,
1304                                      "%s exiting due to fatal errors!",
1305                                      PACKAGE_NAME);
1306                         exit(1);
1307                 }
1308         }
1309 #endif
1310
1311         if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1312             && !Conf_ServerAdminMail[0]) {
1313                 /* No administrative information configured! */
1314                 Config_Error(LOG_WARNING,
1315                              "No administrative information configured but required by RFC!");
1316         }
1317
1318 #ifdef DEBUG
1319         servers = servers_once = 0;
1320         for (i = 0; i < MAX_SERVERS; i++) {
1321                 if (Conf_Server[i].name[0]) {
1322                         servers++;
1323                         if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1324                                 servers_once++;
1325                 }
1326         }
1327         Log(LOG_DEBUG,
1328             "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1329             Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1330 #endif
1331
1332         return config_valid;
1333 } /* Validate_Config */
1334
1335
1336 static void
1337 Config_Error_TooLong ( const int Line, const char *Item )
1338 {
1339         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1340 }
1341
1342
1343 static void
1344 Config_Error_NaN( const int Line, const char *Item )
1345 {
1346         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1347                                                 NGIRCd_ConfFile, Line, Item );
1348 }
1349
1350
1351 #ifdef PROTOTYPES
1352 static void Config_Error( const int Level, const char *Format, ... )
1353 #else
1354 static void Config_Error( Level, Format, va_alist )
1355 const int Level;
1356 const char *Format;
1357 va_dcl
1358 #endif
1359 {
1360         /* Error! Write to console and/or logfile. */
1361
1362         char msg[MAX_LOG_MSG_LEN];
1363         va_list ap;
1364
1365         assert( Format != NULL );
1366
1367 #ifdef PROTOTYPES
1368         va_start( ap, Format );
1369 #else
1370         va_start( ap );
1371 #endif
1372         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1373         va_end( ap );
1374         
1375         /* During "normal operations" the log functions of the daemon should
1376          * be used, but during testing of the configuration file, all messages
1377          * should go directly to the console: */
1378         if (Use_Log) Log( Level, "%s", msg );
1379         else puts( msg );
1380 } /* Config_Error */
1381
1382
1383 static void
1384 Init_Server_Struct( CONF_SERVER *Server )
1385 {
1386         /* Initialize server configuration structur to default values */
1387
1388         assert( Server != NULL );
1389
1390         memset( Server, 0, sizeof (CONF_SERVER) );
1391
1392         Server->group = NONE;
1393         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1394
1395         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1396
1397         Resolve_Init(&Server->res_stat);
1398         Server->conn_id = NONE;
1399         memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1400 } /* Init_Server_Struct */
1401
1402
1403 /* -eof- */