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