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