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