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