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