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