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