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