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