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