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