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