]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
Configuration: get rid of Conf_Oper_Count and Conf_Channel_Count
[ngircd-alex.git] / src / ngircd / conf.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
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 char Conf_MotdFile[FNAME_LEN];
59
60 static void Set_Defaults PARAMS(( bool InitServers ));
61 static bool Read_Config PARAMS(( bool ngircd_starting ));
62 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
63
64 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_OPTIONS PARAMS(( int Line, char *Var, char *Arg ));
67 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
68 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
69 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
70
71 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
72
73 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
74 static void Config_Error_Section PARAMS(( const int Line, const char *Item,
75                                           const char *Section ));
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
81 #ifdef WANT_IPV6
82 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
83 #else
84 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
85 #endif
86
87
88 #ifdef SSL_SUPPORT
89
90 static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
91
92 struct SSLOptions Conf_SSLOptions;
93
94 /**
95  * Initialize SSL configuration.
96  */
97 static void
98 ConfSSL_Init(void)
99 {
100         free(Conf_SSLOptions.KeyFile);
101         Conf_SSLOptions.KeyFile = NULL;
102
103         free(Conf_SSLOptions.CertFile);
104         Conf_SSLOptions.CertFile = NULL;
105
106         free(Conf_SSLOptions.DHFile);
107         Conf_SSLOptions.DHFile = NULL;
108         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
109 }
110
111 /**
112  * Make sure that a configured file is readable.
113  *
114  * Currently, this function is only used for SSL-related options ...
115  *
116  * @param Var Configuration variable
117  * @param Filename Configured filename
118  */
119 static void
120 CheckFileReadable(const char *Var, const char *Filename)
121 {
122         FILE *fp;
123
124         if (!Filename)
125                 return;
126
127         fp = fopen(Filename, "r");
128         if (fp)
129                 fclose(fp);
130         else
131                 Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
132                              Filename, Var, strerror(errno));
133 }
134
135 #endif
136
137
138 /**
139  * Duplicate string and warn on errors.
140  *
141  * @returns Pointer to string on success, NULL otherwise.
142  */
143 static char *
144 strdup_warn(const char *str)
145 {
146         char *ptr = strdup(str);
147         if (!ptr)
148                 Config_Error(LOG_ERR,
149                              "Could not allocate memory for string: %s", str);
150         return ptr;
151 }
152
153 /**
154  * Output a comma separated list of ports (integer values).
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  * Parse a comma separated string into an array of port numbers (integers).
175  */
176 static void
177 ports_parse(array *a, int Line, char *Arg)
178 {
179         char *ptr;
180         int port;
181         UINT16 port16;
182
183         array_trunc(a);
184
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  * Initialize configuration module.
205  */
206 GLOBAL void
207 Conf_Init( void )
208 {
209         Read_Config( true );
210         Validate_Config(false, false);
211 }
212
213 /**
214  * "Rehash" (reload) server configuration.
215  *
216  * @returns true if configuration has been re-read, false on errors.
217  */
218 GLOBAL bool
219 Conf_Rehash( void )
220 {
221         if (!Read_Config(false))
222                 return false;
223         Validate_Config(false, true);
224
225         /* Update CLIENT structure of local server */
226         Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
227         return true;
228 }
229
230 /**
231  * Output a boolean value as "yes/no" string.
232  */
233 static const char*
234 yesno_to_str(int boolean_value)
235 {
236         if (boolean_value)
237                 return "yes";
238         return "no";
239 }
240
241 /**
242  * Free all IRC operator configuration structures.
243  */
244 static void
245 opers_free(void)
246 {
247         struct Conf_Oper *op;
248         size_t len;
249
250         len = array_length(&Conf_Opers, sizeof(*op));
251         op = array_start(&Conf_Opers);
252         while (len--) {
253                 free(op->mask);
254                 op++;
255         }
256         array_free(&Conf_Opers);
257 }
258
259 /**
260  * Output all IRC operator configuration structures.
261  */
262 static void
263 opers_puts(void)
264 {
265         struct Conf_Oper *op;
266         size_t count, i;
267
268         count = array_length(&Conf_Opers, sizeof(*op));
269         op = array_start(&Conf_Opers);
270         for (i = 0; i < count; i++, op++) {
271                 if (!op->name[0])
272                         continue;
273
274                 puts("[OPERATOR]");
275                 printf("  Name = %s\n", op->name);
276                 printf("  Password = %s\n", op->pwd);
277                 printf("  Mask = %s\n\n", op->mask ? op->mask : "");
278         }
279 }
280
281 /**
282  * Read configuration, validate and output it.
283  *
284  * This function waits for a keypress of the user when stdin/stdout are valid
285  * tty's ("you can read our nice message and we can read in your keypress").
286  *
287  * @return      0 on succes, 1 on failure(s); therefore the result code can
288  *              directly be used by exit() when running "ngircd --configtest".
289  */
290 GLOBAL int
291 Conf_Test( void )
292 {
293         struct passwd *pwd;
294         struct group *grp;
295         unsigned int i;
296         bool config_valid;
297         size_t predef_channel_count;
298         struct Conf_Channel *predef_chan;
299
300         Use_Log = false;
301
302         if (! Read_Config(true))
303                 return 1;
304
305         config_valid = Validate_Config(true, false);
306
307         /* Valid tty? */
308         if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
309                 puts("OK, press enter to see a dump of your server configuration ...");
310                 getchar();
311         } else
312                 puts("Ok, dump of your server configuration follows:\n");
313
314         puts("[GLOBAL]");
315         printf("  Name = %s\n", Conf_ServerName);
316         printf("  AdminInfo1 = %s\n", Conf_ServerAdmin1);
317         printf("  AdminInfo2 = %s\n", Conf_ServerAdmin2);
318         printf("  AdminEMail = %s\n", Conf_ServerAdminMail);
319         printf("  Info = %s\n", Conf_ServerInfo);
320         printf("  Listen = %s\n", Conf_ListenAddress);
321         if (Using_MotdFile) {
322                 printf("  MotdFile = %s\n", Conf_MotdFile);
323                 printf("  MotdPhrase =\n");
324         } else {
325                 printf("  MotdFile = \n");
326                 printf("  MotdPhrase = %s\n", array_bytes(&Conf_Motd)
327                        ? (const char*) array_start(&Conf_Motd) : "");
328         }
329 #ifndef PAM
330         printf("  Password = %s\n", Conf_ServerPwd);
331 #endif
332         printf("  PidFile = %s\n", Conf_PidFile);
333         printf("  Ports = ");
334         ports_puts(&Conf_ListenPorts);
335         grp = getgrgid(Conf_GID);
336         if (grp)
337                 printf("  ServerGID = %s\n", grp->gr_name);
338         else
339                 printf("  ServerGID = %ld\n", (long)Conf_GID);
340         pwd = getpwuid(Conf_UID);
341         if (pwd)
342                 printf("  ServerUID = %s\n", pwd->pw_name);
343         else
344                 printf("  ServerUID = %ld\n", (long)Conf_UID);
345         puts("");
346
347         puts("[LIMITS]");
348         printf("  ConnectRetry = %d\n", Conf_ConnectRetry);
349         printf("  MaxConnections = %ld\n", Conf_MaxConnections);
350         printf("  MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
351         printf("  MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
352         printf("  MaxNickLength = %u\n", Conf_MaxNickLength - 1);
353         printf("  PingTimeout = %d\n", Conf_PingTimeout);
354         printf("  PongTimeout = %d\n", Conf_PongTimeout);
355         puts("");
356
357         puts("[OPTIONS]");
358         printf("  AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
359         printf("  ChrootDir = %s\n", Conf_Chroot);
360         printf("  CloakHost = %s\n", Conf_CloakHost);
361         printf("  CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
362 #ifdef WANT_IPV6
363         printf("  ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
364         printf("  ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
365 #endif
366         printf("  DNS = %s\n", yesno_to_str(Conf_DNS));
367 #ifdef IDENT
368         printf("  Ident = %s\n", yesno_to_str(Conf_Ident));
369 #endif
370         printf("  MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
371         printf("  NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
372         printf("  OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
373         printf("  OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
374 #ifdef PAM
375         printf("  PAM = %s\n", yesno_to_str(Conf_PAM));
376 #endif
377         printf("  PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
378 #ifndef STRICT_RFC
379         printf("  RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
380 #endif
381         printf("  ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
382 #ifdef SYSLOG
383         printf("  SyslogFacility = %s\n",
384                ngt_SyslogFacilityName(Conf_SyslogFacility));
385 #endif
386         printf("  WebircPassword = %s\n", Conf_WebircPwd);
387         puts("");
388
389 #ifdef SSL_SUPPORT
390         puts("[SSL]");
391         printf("  CertFile = %s\n", Conf_SSLOptions.CertFile
392                                         ? Conf_SSLOptions.CertFile : "");
393         printf("  DHFile = %s\n", Conf_SSLOptions.DHFile
394                                         ? Conf_SSLOptions.DHFile : "");
395         printf("  KeyFile = %s\n", Conf_SSLOptions.KeyFile
396                                         ? Conf_SSLOptions.KeyFile : "");
397         if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
398                 puts("  KeyFilePassword = <secret>");
399         else
400                 puts("  KeyFilePassword = ");
401         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
402         printf("  Ports = ");
403         ports_puts(&Conf_SSLOptions.ListenPorts);
404         puts("");
405 #endif
406
407         opers_puts();
408
409         for( i = 0; i < MAX_SERVERS; i++ ) {
410                 if( ! Conf_Server[i].name[0] ) continue;
411
412                 /* Valid "Server" section */
413                 puts( "[SERVER]" );
414                 printf( "  Name = %s\n", Conf_Server[i].name );
415                 printf( "  Host = %s\n", Conf_Server[i].host );
416                 printf( "  Port = %u\n", (unsigned int)Conf_Server[i].port );
417 #ifdef SSL_SUPPORT
418                 printf( "  SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
419 #endif
420                 printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
421                 printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
422                 printf( "  ServiceMask = %s\n", Conf_Server[i].svs_mask);
423                 printf( "  Group = %d\n", Conf_Server[i].group );
424                 printf( "  Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
425         }
426
427         predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
428         predef_chan = array_start(&Conf_Channels);
429
430         for (i = 0; i < predef_channel_count; i++, predef_chan++) {
431                 if (!predef_chan->name[0])
432                         continue;
433
434                 /* Valid "Channel" section */
435                 puts( "[CHANNEL]" );
436                 printf("  Name = %s\n", predef_chan->name);
437                 printf("  Modes = %s\n", predef_chan->modes);
438                 printf("  Key = %s\n", predef_chan->key);
439                 printf("  MaxUsers = %lu\n", predef_chan->maxusers);
440                 printf("  Topic = %s\n", predef_chan->topic);
441                 printf("  KeyFile = %s\n\n", predef_chan->keyfile);
442         }
443
444         return (config_valid ? 0 : 1);
445 }
446
447 /**
448  * Remove connection information from configured server.
449  *
450  * If the server is set as "once", delete it from our configuration;
451  * otherwise set the time for the next connection attempt.
452  *
453  * Non-server connections will be silently ignored.
454  */
455 GLOBAL void
456 Conf_UnsetServer( CONN_ID Idx )
457 {
458         int i;
459         time_t t;
460
461         /* Check all our configured servers */
462         for( i = 0; i < MAX_SERVERS; i++ ) {
463                 if( Conf_Server[i].conn_id != Idx ) continue;
464
465                 /* Gotcha! Mark server configuration as "unused": */
466                 Conf_Server[i].conn_id = NONE;
467
468                 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
469                         /* Delete configuration here */
470                         Init_Server_Struct( &Conf_Server[i] );
471                 } else {
472                         /* Set time for next connect attempt */
473                         t = time(NULL);
474                         if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
475                                 /* The connection has been "long", so we don't
476                                  * require the next attempt to be delayed. */
477                                 Conf_Server[i].lasttry =
478                                         t - Conf_ConnectRetry + RECONNECT_DELAY;
479                         } else
480                                 Conf_Server[i].lasttry = t;
481                 }
482         }
483 }
484
485 /**
486  * Set connection information for specified configured server.
487  */
488 GLOBAL void
489 Conf_SetServer( int ConfServer, CONN_ID Idx )
490 {
491         assert( ConfServer > NONE );
492         assert( Idx > NONE );
493
494         Conf_Server[ConfServer].conn_id = Idx;
495 }
496
497 /**
498  * Get index of server in configuration structure.
499  */
500 GLOBAL int
501 Conf_GetServer( CONN_ID Idx )
502 {
503         int i = 0;
504
505         assert( Idx > NONE );
506
507         for( i = 0; i < MAX_SERVERS; i++ ) {
508                 if( Conf_Server[i].conn_id == Idx ) return i;
509         }
510         return NONE;
511 }
512
513 /**
514  * Enable a server by name and adjust its port number.
515  *
516  * @returns     true if a server has been enabled and now has a valid port
517  *              number and host name for outgoing connections.
518  */
519 GLOBAL bool
520 Conf_EnableServer( const char *Name, UINT16 Port )
521 {
522         int i;
523
524         assert( Name != NULL );
525         for( i = 0; i < MAX_SERVERS; i++ ) {
526                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
527                         /* Gotcha! Set port and enable server: */
528                         Conf_Server[i].port = Port;
529                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
530                         return (Conf_Server[i].port && Conf_Server[i].host[0]);
531                 }
532         }
533         return false;
534 }
535
536 /**
537  * Enable a server by name.
538  *
539  * The server is only usable as outgoing server, if it has set a valid port
540  * number for outgoing connections!
541  * If not, you have to use Conf_EnableServer() function to make it available.
542  *
543  * @returns     true if a server has been enabled; false otherwise.
544  */
545 GLOBAL bool
546 Conf_EnablePassiveServer(const char *Name)
547 {
548         int i;
549
550         assert( Name != NULL );
551         for (i = 0; i < MAX_SERVERS; i++) {
552                 if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
553                     && (Conf_Server[i].port > 0)) {
554                         /* BINGO! Enable server */
555                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
556                         return true;
557                 }
558         }
559         return false;
560 }
561
562 /**
563  * Disable a server by name.
564  * An already established connection will be disconnected.
565  *
566  * @returns     true if a server was found and has been disabled.
567  */
568 GLOBAL bool
569 Conf_DisableServer( const char *Name )
570 {
571         int i;
572
573         assert( Name != NULL );
574         for( i = 0; i < MAX_SERVERS; i++ ) {
575                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
576                         /* Gotcha! Disable and disconnect server: */
577                         Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
578                         if( Conf_Server[i].conn_id > NONE )
579                                 Conn_Close(Conf_Server[i].conn_id, NULL,
580                                            "Server link terminated on operator request",
581                                            true);
582                         return true;
583                 }
584         }
585         return false;
586 }
587
588 /**
589  * Add a new remote server to our configuration.
590  *
591  * @param Name          Name of the new server.
592  * @param Port          Port number to connect to or 0 for incoming connections.
593  * @param Host          Host name to connect to.
594  * @param MyPwd         Password that will be sent to the peer.
595  * @param PeerPwd       Password that must be received from the peer.
596  * @returns             true if the new server has been added; false otherwise.
597  */
598 GLOBAL bool
599 Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
600                const char *MyPwd, const char *PeerPwd)
601 {
602         int i;
603
604         assert( Name != NULL );
605         assert( Host != NULL );
606         assert( MyPwd != NULL );
607         assert( PeerPwd != NULL );
608
609         /* Search unused item in server configuration structure */
610         for( i = 0; i < MAX_SERVERS; i++ ) {
611                 /* Is this item used? */
612                 if( ! Conf_Server[i].name[0] ) break;
613         }
614         if( i >= MAX_SERVERS ) return false;
615
616         Init_Server_Struct( &Conf_Server[i] );
617         strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
618         strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
619         strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
620         strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
621         Conf_Server[i].port = Port;
622         Conf_Server[i].flags = CONF_SFLAG_ONCE;
623
624         return true;
625 }
626
627 /**
628  * Check if the given nick name is an service.
629  *
630  * @returns true if the given nick name belongs to an "IRC service".
631  */
632 GLOBAL bool
633 Conf_IsService(int ConfServer, const char *Nick)
634 {
635         return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
636 }
637
638 /**
639  * Initialize configuration settings with their default values.
640  */
641 static void
642 Set_Defaults(bool InitServers)
643 {
644         int i;
645
646         /* Global */
647         strcpy(Conf_ServerName, "");
648         strcpy(Conf_ServerAdmin1, "");
649         strcpy(Conf_ServerAdmin2, "");
650         strcpy(Conf_ServerAdminMail, "");
651         snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
652                  PACKAGE_NAME, PACKAGE_VERSION);
653         free(Conf_ListenAddress);
654         Conf_ListenAddress = NULL;
655         array_free(&Conf_Motd);
656         strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
657         strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
658         strcpy(Conf_ServerPwd, "");
659         strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
660         Conf_UID = Conf_GID = 0;
661
662         /* Limits */
663         Conf_ConnectRetry = 60;
664         Conf_MaxConnections = 0;
665         Conf_MaxConnectionsIP = 5;
666         Conf_MaxJoins = 10;
667         Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
668         Conf_PingTimeout = 120;
669         Conf_PongTimeout = 20;
670
671         /* Options */
672         Conf_AllowRemoteOper = false;
673 #ifndef STRICT_RFC
674         Conf_AuthPing = false;
675 #endif
676         strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
677         strcpy(Conf_CloakHost, "");
678         Conf_CloakUserToNick = false;
679         Conf_ConnectIPv4 = true;
680 #ifdef WANT_IPV6
681         Conf_ConnectIPv6 = true;
682 #else
683         Conf_ConnectIPv6 = false;
684 #endif
685         Conf_DNS = true;
686 #ifdef IDENTAUTH
687         Conf_Ident = true;
688 #else
689         Conf_Ident = false;
690 #endif
691         Conf_MorePrivacy = false;
692         Conf_NoticeAuth = false;
693         Conf_OperCanMode = false;
694         Conf_OperServerMode = false;
695 #ifdef PAM
696         Conf_PAM = true;
697 #else
698         Conf_PAM = false;
699 #endif
700         Conf_PredefChannelsOnly = false;
701 #ifdef SYSLOG
702         Conf_ScrubCTCP = false;
703 #ifdef LOG_LOCAL5
704         Conf_SyslogFacility = LOG_LOCAL5;
705 #else
706         Conf_SyslogFacility = 0;
707 #endif
708 #endif
709
710         /* Initialize server configuration structures */
711         if (InitServers) {
712                 for (i = 0; i < MAX_SERVERS;
713                      Init_Server_Struct(&Conf_Server[i++]));
714         }
715 }
716
717 /**
718  * Get number of configured listening ports.
719  *
720  * @returns The number of ports (IPv4+IPv6) on which the server should listen.
721  */
722 static bool
723 no_listenports(void)
724 {
725         size_t cnt = array_bytes(&Conf_ListenPorts);
726 #ifdef SSL_SUPPORT
727         cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
728 #endif
729         return cnt == 0;
730 }
731
732 /**
733  * Read MOTD ("message of the day") file.
734  *
735  * @param filename      Name of the file to read.
736  */
737 static void
738 Read_Motd(const char *filename)
739 {
740         char line[127];
741         FILE *fp;
742
743         if (*filename == '\0')
744                 return;
745
746         fp = fopen(filename, "r");
747         if (!fp) {
748                 Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
749                                         filename, strerror(errno));
750                 return;
751         }
752
753         array_free(&Conf_Motd);
754         Using_MotdFile = true;
755
756         while (fgets(line, (int)sizeof line, fp)) {
757                 ngt_TrimLastChr( line, '\n');
758
759                 /* add text including \0 */
760                 if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
761                         Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
762                         break;
763                 }
764         }
765         fclose(fp);
766 }
767
768 /**
769  * Read ngIRCd configuration file.
770  *
771  * Please note that this function uses exit(1) on fatal errors and therefore
772  * can result in ngIRCd terminating!
773  *
774  * @param ngircd_starting       Flag indicating if ngIRCd is starting or not.
775  * @returns                     true when the configuration file has been read
776  *                              successfully; false otherwise.
777  */
778 static bool
779 Read_Config( bool ngircd_starting )
780 {
781         char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
782         const UINT16 defaultport = 6667;
783         int line, i, n;
784         size_t count;
785         FILE *fd;
786
787         /* Open configuration file */
788         fd = fopen( NGIRCd_ConfFile, "r" );
789         if( ! fd ) {
790                 /* No configuration file found! */
791                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
792                                         NGIRCd_ConfFile, strerror( errno ));
793                 if (!ngircd_starting)
794                         return false;
795                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
796                 exit( 1 );
797         }
798
799         opers_free();
800         Set_Defaults( ngircd_starting );
801
802         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
803
804         /* Clean up server configuration structure: mark all already
805          * configured servers as "once" so that they are deleted
806          * after the next disconnect and delete all unused servers.
807          * And delete all servers which are "duplicates" of servers
808          * that are already marked as "once" (such servers have been
809          * created by the last rehash but are now useless). */
810         for( i = 0; i < MAX_SERVERS; i++ ) {
811                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
812                 else {
813                         /* This structure is in use ... */
814                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
815                                 /* Check for duplicates */
816                                 for( n = 0; n < MAX_SERVERS; n++ ) {
817                                         if( n == i ) continue;
818
819                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
820                                                 Init_Server_Struct( &Conf_Server[n] );
821 #ifdef DEBUG
822                                                 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
823                                                                                                 n, i );
824 #endif
825                                         }
826                                 }
827                         } else {
828                                 /* Mark server as "once" */
829                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
830                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
831                         }
832                 }
833         }
834
835         /* Initialize variables */
836         line = 0;
837         strcpy( section, "" );
838         Init_Server_Struct( &New_Server );
839         New_Server_Idx = NONE;
840 #ifdef SSL_SUPPORT
841         ConfSSL_Init();
842 #endif
843         /* Read configuration file */
844         while( true ) {
845                 if( ! fgets( str, LINE_LEN, fd )) break;
846                 ngt_TrimStr( str );
847                 line++;
848
849                 /* Skip comments and empty lines */
850                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
851
852                 /* Is this the beginning of a new section? */
853                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
854                         strlcpy( section, str, sizeof( section ));
855                         if (strcasecmp(section, "[GLOBAL]") == 0 ||
856                             strcasecmp(section, "[LIMITS]") == 0 ||
857                             strcasecmp(section, "[OPTIONS]") == 0 ||
858                             strcasecmp(section, "[SSL]") == 0)
859                                 continue;
860
861                         if( strcasecmp( section, "[SERVER]" ) == 0 ) {
862                                 /* Check if there is already a server to add */
863                                 if( New_Server.name[0] ) {
864                                         /* Copy data to "real" server structure */
865                                         assert( New_Server_Idx > NONE );
866                                         Conf_Server[New_Server_Idx] = New_Server;
867                                 }
868
869                                 /* Re-init structure for new server */
870                                 Init_Server_Struct( &New_Server );
871
872                                 /* Search unused item in server configuration structure */
873                                 for( i = 0; i < MAX_SERVERS; i++ ) {
874                                         /* Is this item used? */
875                                         if( ! Conf_Server[i].name[0] ) break;
876                                 }
877                                 if( i >= MAX_SERVERS ) {
878                                         /* Oops, no free item found! */
879                                         Config_Error( LOG_ERR, "Too many servers configured." );
880                                         New_Server_Idx = NONE;
881                                 }
882                                 else New_Server_Idx = i;
883                                 continue;
884                         }
885
886                         if (strcasecmp(section, "[CHANNEL]") == 0) {
887                                 count = array_length(&Conf_Channels,
888                                                      sizeof(struct Conf_Channel));
889                                 if (!array_alloc(&Conf_Channels,
890                                                  sizeof(struct Conf_Channel),
891                                                  count)) {
892                                         Config_Error(LOG_ERR,
893                                                      "Could not allocate memory for new operator (line %d)",
894                                                      line);
895                                 }
896                                 continue;
897                         }
898
899                         if (strcasecmp(section, "[OPERATOR]") == 0) {
900                                 count = array_length(&Conf_Opers,
901                                                      sizeof(struct Conf_Oper));
902                                 if (!array_alloc(&Conf_Opers,
903                                                  sizeof(struct Conf_Oper),
904                                                  count)) {
905                                         Config_Error(LOG_ERR,
906                                                      "Could not allocate memory for new channel (line &d)",
907                                                      line);
908                                 }
909                                 continue;
910                         }
911
912                         Config_Error(LOG_ERR,
913                                      "%s, line %d: Unknown section \"%s\"!",
914                                      NGIRCd_ConfFile, line, section);
915                         section[0] = 0x1;
916                 }
917                 if( section[0] == 0x1 ) continue;
918
919                 /* Split line into variable name and parameters */
920                 ptr = strchr( str, '=' );
921                 if( ! ptr ) {
922                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
923                         continue;
924                 }
925                 *ptr = '\0';
926                 var = str; ngt_TrimStr( var );
927                 arg = ptr + 1; ngt_TrimStr( arg );
928
929                 if(strcasecmp(section, "[GLOBAL]") == 0)
930                         Handle_GLOBAL(line, var, arg);
931                 else if(strcasecmp(section, "[LIMITS]") == 0)
932                         Handle_LIMITS(line, var, arg);
933                 else if(strcasecmp(section, "[OPTIONS]") == 0)
934                         Handle_OPTIONS(line, var, arg);
935 #ifdef SSL_SUPPORT
936                 else if(strcasecmp(section, "[SSL]") == 0)
937                         Handle_SSL(line, var, arg);
938 #endif
939                 else if(strcasecmp(section, "[OPERATOR]") == 0)
940                         Handle_OPERATOR(line, var, arg);
941                 else if(strcasecmp(section, "[SERVER]") == 0)
942                         Handle_SERVER(line, var, arg);
943                 else if(strcasecmp(section, "[CHANNEL]") == 0)
944                         Handle_CHANNEL(line, var, arg);
945                 else
946                         Config_Error(LOG_ERR,
947                                      "%s, line %d: Variable \"%s\" outside section!",
948                                      NGIRCd_ConfFile, line, var);
949         }
950
951         /* Close configuration file */
952         fclose( fd );
953
954         /* Check if there is still a server to add */
955         if( New_Server.name[0] ) {
956                 /* Copy data to "real" server structure */
957                 assert( New_Server_Idx > NONE );
958                 Conf_Server[New_Server_Idx] = New_Server;
959         }
960
961         /* not a single listening port? Add default. */
962         if (no_listenports() &&
963                 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
964         {
965                 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
966                                         (unsigned int) defaultport, strerror(errno));
967
968                 exit(1);
969         }
970
971         if (!Conf_ListenAddress)
972                 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
973
974         if (!Conf_ListenAddress) {
975                 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
976                 exit(1);
977         }
978
979         /* No MOTD phrase configured? (re)try motd file. */
980         if (array_bytes(&Conf_Motd) == 0)
981                 Read_Motd(Conf_MotdFile);
982
983 #ifdef SSL_SUPPORT
984         /* Make sure that all SSL-related files are readable */
985         CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
986         CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
987         CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
988 #endif
989
990         return true;
991 }
992
993 /**
994  * Check whether a string argument is "true" or "false".
995  *
996  * @param Arg   Input string.
997  * @returns     true if the input string has been parsed as "yes", "true"
998  *              (case insensitive) or a non-zero integer value.
999  */
1000 static bool
1001 Check_ArgIsTrue(const char *Arg)
1002 {
1003         if (strcasecmp(Arg, "yes") == 0)
1004                 return true;
1005         if (strcasecmp(Arg, "true") == 0)
1006                 return true;
1007         if (atoi(Arg) != 0)
1008                 return true;
1009
1010         return false;
1011 }
1012
1013 /**
1014  * Handle setting of "MaxNickLength".
1015  *
1016  * @param Line  Line number in configuration file.
1017  * @raram Arg   Input string.
1018  * @returns     New configured maximum nick name length.
1019  */
1020 static unsigned int
1021 Handle_MaxNickLength(int Line, const char *Arg)
1022 {
1023         unsigned new;
1024
1025         new = (unsigned) atoi(Arg) + 1;
1026         if (new > CLIENT_NICK_LEN) {
1027                 Config_Error(LOG_WARNING,
1028                              "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1029                              NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1030                 return CLIENT_NICK_LEN;
1031         }
1032         if (new < 2) {
1033                 Config_Error(LOG_WARNING,
1034                              "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1035                              NGIRCd_ConfFile, Line);
1036                 return 2;
1037         }
1038         return new;
1039 }
1040
1041 /**
1042  * Output a warning messages if IDENT is configured but not compiled in.
1043  */
1044 static void
1045 WarnIdent(int UNUSED Line)
1046 {
1047 #ifndef IDENTAUTH
1048         if (Conf_Ident) {
1049                 /* user has enabled ident lookups explicitly, but ... */
1050                 Config_Error(LOG_WARNING,
1051                         "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1052                         NGIRCd_ConfFile, Line);
1053         }
1054 #endif
1055 }
1056
1057 /**
1058  * Output a warning messages if IPv6 is configured but not compiled in.
1059  */
1060 static void
1061 WarnIPv6(int UNUSED Line)
1062 {
1063 #ifndef WANT_IPV6
1064         if (Conf_ConnectIPv6) {
1065                 /* user has enabled IPv6 explicitly, but ... */
1066                 Config_Error(LOG_WARNING,
1067                         "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1068                         NGIRCd_ConfFile, Line);
1069         }
1070 #endif
1071 }
1072
1073 /**
1074  * Output a warning messages if PAM is configured but not compiled in.
1075  */
1076 static void
1077 WarnPAM(int UNUSED Line)
1078 {
1079 #ifndef PAM
1080         if (Conf_PAM) {
1081                 Config_Error(LOG_WARNING,
1082                         "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1083                         NGIRCd_ConfFile, Line);
1084         }
1085 #endif
1086 }
1087
1088 /**
1089  * Handle legacy "NoXXX" options in [GLOBAL] section.
1090  *
1091  * TODO: This function and support for "NoXXX" could be removed starting
1092  * with ngIRCd release 19 (one release after marking it "deprecated").
1093  *
1094  * @param Var   Variable name.
1095  * @param Arg   Argument string.
1096  * @returns     true if a NoXXX option has been processed; false otherwise.
1097  */
1098 static bool
1099 CheckLegacyNoOption(const char *Var, const char *Arg)
1100 {
1101         if(strcasecmp(Var, "NoDNS") == 0) {
1102                 Conf_DNS = !Check_ArgIsTrue( Arg );
1103                 return true;
1104         }
1105         if (strcasecmp(Var, "NoIdent") == 0) {
1106                 Conf_Ident = !Check_ArgIsTrue(Arg);
1107                 return true;
1108         }
1109         if(strcasecmp(Var, "NoPAM") == 0) {
1110                 Conf_PAM = !Check_ArgIsTrue(Arg);
1111                 return true;
1112         }
1113         return false;
1114 }
1115
1116 /**
1117  * Handle deprecated legacy options in [GLOBAL] section.
1118  *
1119  * TODO: This function and support for these options in the [Global] section
1120  * could be removed starting with ngIRCd release 19 (one release after
1121  * marking it "deprecated").
1122  *
1123  * @param Var   Variable name.
1124  * @param Arg   Argument string.
1125  * @returns     true if a legacy option has been processed; false otherwise.
1126  */
1127 static const char*
1128 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1129 {
1130         if (strcasecmp(Var, "AllowRemoteOper") == 0
1131             || strcasecmp(Var, "ChrootDir") == 0
1132             || strcasecmp(Var, "ConnectIPv4") == 0
1133             || strcasecmp(Var, "ConnectIPv6") == 0
1134             || strcasecmp(Var, "OperCanUseMode") == 0
1135             || strcasecmp(Var, "OperServerMode") == 0
1136             || strcasecmp(Var, "PredefChannelsOnly") == 0
1137             || strcasecmp(Var, "SyslogFacility") == 0
1138             || strcasecmp(Var, "WebircPassword") == 0) {
1139                 Handle_OPTIONS(Line, Var, Arg);
1140                 return "[Options]";
1141         }
1142         if (strcasecmp(Var, "ConnectRetry") == 0
1143             || strcasecmp(Var, "MaxConnections") == 0
1144             || strcasecmp(Var, "MaxConnectionsIP") == 0
1145             || strcasecmp(Var, "MaxJoins") == 0
1146             || strcasecmp(Var, "MaxNickLength") == 0
1147             || strcasecmp(Var, "PingTimeout") == 0
1148             || strcasecmp(Var, "PongTimeout") == 0) {
1149                 Handle_LIMITS(Line, Var, Arg);
1150                 return "[Limits]";
1151         }
1152 #ifdef SSL_SUPPORT
1153         if (strcasecmp(Var, "SSLCertFile") == 0
1154             || strcasecmp(Var, "SSLDHFile") == 0
1155             || strcasecmp(Var, "SSLKeyFile") == 0
1156             || strcasecmp(Var, "SSLKeyFilePassword") == 0
1157             || strcasecmp(Var, "SSLPorts") == 0) {
1158                 Handle_SSL(Line, Var + 3, Arg);
1159                 return "[SSL]";
1160         }
1161 #endif
1162
1163         return NULL;
1164 }
1165
1166 /**
1167  * Strip "no" prefix of a string.
1168  *
1169  * TODO: This function and support for "NoXXX" should be removed starting
1170  * with ngIRCd release 19! (One release after marking it "deprecated").
1171  *
1172  * @param str   Pointer to input string starting with "no".
1173  * @returns     New pointer to string without "no" prefix.
1174  */
1175 static const char *
1176 NoNo(const char *str)
1177 {
1178         assert(strncasecmp("no", str, 2) == 0 && str[2]);
1179         return str + 2;
1180 }
1181
1182 /**
1183  * Invert "boolean" string.
1184  *
1185  * TODO: This function and support for "NoXXX" should be removed starting
1186  * with ngIRCd release 19! (One release after marking it "deprecated").
1187  *
1188  * @param arg   "Boolean" input string.
1189  * @returns     Pointer to inverted "boolean string".
1190  */
1191 static const char *
1192 InvertArg(const char *arg)
1193 {
1194         return yesno_to_str(!Check_ArgIsTrue(arg));
1195 }
1196
1197 /**
1198  * Handle variable in [Global] configuration section.
1199  *
1200  * @param Line  Line numer in configuration file.
1201  * @param Var   Variable name.
1202  * @param Arg   Variable argument.
1203  */
1204 static void
1205 Handle_GLOBAL( int Line, char *Var, char *Arg )
1206 {
1207         struct passwd *pwd;
1208         struct group *grp;
1209         size_t len;
1210         const char *section;
1211
1212         assert(Line > 0);
1213         assert(Var != NULL);
1214         assert(Arg != NULL);
1215
1216         if (strcasecmp(Var, "Name") == 0) {
1217                 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1218                 if (len >= sizeof(Conf_ServerName))
1219                         Config_Error_TooLong(Line, Var);
1220                 return;
1221         }
1222         if (strcasecmp(Var, "AdminInfo1") == 0) {
1223                 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1224                 if (len >= sizeof(Conf_ServerAdmin1))
1225                         Config_Error_TooLong(Line, Var);
1226                 return;
1227         }
1228         if (strcasecmp(Var, "AdminInfo2") == 0) {
1229                 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1230                 if (len >= sizeof(Conf_ServerAdmin2))
1231                         Config_Error_TooLong(Line, Var);
1232                 return;
1233         }
1234         if (strcasecmp(Var, "AdminEMail") == 0) {
1235                 len = strlcpy(Conf_ServerAdminMail, Arg,
1236                         sizeof(Conf_ServerAdminMail));
1237                 if (len >= sizeof(Conf_ServerAdminMail))
1238                         Config_Error_TooLong(Line, Var);
1239                 return;
1240         }
1241         if (strcasecmp(Var, "Info") == 0) {
1242                 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1243                 if (len >= sizeof(Conf_ServerInfo))
1244                         Config_Error_TooLong(Line, Var);
1245                 return;
1246         }
1247         if (strcasecmp(Var, "Listen") == 0) {
1248                 if (Conf_ListenAddress) {
1249                         Config_Error(LOG_ERR,
1250                                      "Multiple Listen= options, ignoring: %s",
1251                                      Arg);
1252                         return;
1253                 }
1254                 Conf_ListenAddress = strdup_warn(Arg);
1255                 /* If allocation fails, we're in trouble: we cannot ignore the
1256                  * error -- otherwise ngircd would listen on all interfaces. */
1257                 if (!Conf_ListenAddress) {
1258                         Config_Error(LOG_ALERT,
1259                                      "%s exiting due to fatal errors!",
1260                                      PACKAGE_NAME);
1261                         exit(1);
1262                 }
1263                 return;
1264         }
1265         if (strcasecmp(Var, "MotdFile") == 0) {
1266                 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1267                 if (len >= sizeof(Conf_MotdFile))
1268                         Config_Error_TooLong(Line, Var);
1269                 return;
1270         }
1271         if (strcasecmp(Var, "MotdPhrase") == 0) {
1272                 len = strlen(Arg);
1273                 if (len == 0)
1274                         return;
1275                 if (len >= LINE_LEN) {
1276                         Config_Error_TooLong(Line, Var);
1277                         return;
1278                 }
1279                 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1280                         Config_Error(LOG_WARNING,
1281                                      "%s, line %d: Could not append MotdPhrase: %s",
1282                                      NGIRCd_ConfFile, Line, strerror(errno));
1283                 Using_MotdFile = false;
1284                 return;
1285         }
1286         if(strcasecmp(Var, "Password") == 0) {
1287                 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1288                 if (len >= sizeof(Conf_ServerPwd))
1289                         Config_Error_TooLong(Line, Var);
1290                 return;
1291         }
1292         if (strcasecmp(Var, "PidFile") == 0) {
1293                 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1294                 if (len >= sizeof(Conf_PidFile))
1295                         Config_Error_TooLong(Line, Var);
1296                 return;
1297         }
1298         if (strcasecmp(Var, "Ports") == 0) {
1299                 ports_parse(&Conf_ListenPorts, Line, Arg);
1300                 return;
1301         }
1302         if (strcasecmp(Var, "ServerGID") == 0) {
1303                 grp = getgrnam(Arg);
1304                 if (grp)
1305                         Conf_GID = grp->gr_gid;
1306                 else {
1307                         Conf_GID = (unsigned int)atoi(Arg);
1308                         if (!Conf_GID && strcmp(Arg, "0"))
1309                                 Config_Error_NaN(Line, Var);
1310                 }
1311                 return;
1312         }
1313         if (strcasecmp(Var, "ServerUID") == 0) {
1314                 pwd = getpwnam(Arg);
1315                 if (pwd)
1316                         Conf_UID = pwd->pw_uid;
1317                 else {
1318                         Conf_UID = (unsigned int)atoi(Arg);
1319                         if (!Conf_UID && strcmp(Arg, "0"))
1320                                 Config_Error_NaN(Line, Var);
1321                 }
1322                 return;
1323         }
1324
1325         if (CheckLegacyNoOption(Var, Arg)) {
1326                 /* TODO: This function and support for "NoXXX" could be
1327                  * be removed starting with ngIRCd release 19 (one release
1328                  * after marking it "deprecated"). */
1329                 Config_Error(LOG_WARNING,
1330                              "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1331                              NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1332                 if (strcasecmp(Var, "NoIdent") == 0)
1333                         WarnIdent(Line);
1334                 else if (strcasecmp(Var, "NoPam") == 0)
1335                         WarnPAM(Line);
1336                 return;
1337         }
1338         if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1339                 /** TODO: This function and support for these options in the
1340                  * [Global] section could be removed starting with ngIRCd
1341                  * release 19 (one release after marking it "deprecated"). */
1342                 if (strncasecmp(Var, "SSL", 3) == 0) {
1343                         Config_Error(LOG_WARNING,
1344                                      "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1345                                      NGIRCd_ConfFile, Line, Var, section,
1346                                      Var + 3);
1347                 } else {
1348                         Config_Error(LOG_WARNING,
1349                                      "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1350                                      NGIRCd_ConfFile, Line, Var, section);
1351                 }
1352                 return;
1353         }
1354
1355         Config_Error_Section(Line, Var, "Global");
1356 }
1357
1358 /**
1359  * Handle variable in [Limits] configuration section.
1360  *
1361  * @param Line  Line numer in configuration file.
1362  * @param Var   Variable name.
1363  * @param Arg   Variable argument.
1364  */
1365 static void
1366 Handle_LIMITS(int Line, char *Var, char *Arg)
1367 {
1368         assert(Line > 0);
1369         assert(Var != NULL);
1370         assert(Arg != NULL);
1371
1372         if (strcasecmp(Var, "ConnectRetry") == 0) {
1373                 Conf_ConnectRetry = atoi(Arg);
1374                 if (Conf_ConnectRetry < 5) {
1375                         Config_Error(LOG_WARNING,
1376                                      "%s, line %d: Value of \"ConnectRetry\" too low!",
1377                                      NGIRCd_ConfFile, Line);
1378                         Conf_ConnectRetry = 5;
1379                 }
1380                 return;
1381         }
1382         if (strcasecmp(Var, "MaxConnections") == 0) {
1383                 Conf_MaxConnections = atol(Arg);
1384                 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1385                         Config_Error_NaN(Line, Var);
1386                 return;
1387         }
1388         if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1389                 Conf_MaxConnectionsIP = atoi(Arg);
1390                 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1391                         Config_Error_NaN(Line, Var);
1392                 return;
1393         }
1394         if (strcasecmp(Var, "MaxJoins") == 0) {
1395                 Conf_MaxJoins = atoi(Arg);
1396                 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1397                         Config_Error_NaN(Line, Var);
1398                 return;
1399         }
1400         if (strcasecmp(Var, "MaxNickLength") == 0) {
1401                 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1402                 return;
1403         }
1404         if (strcasecmp(Var, "PingTimeout") == 0) {
1405                 Conf_PingTimeout = atoi(Arg);
1406                 if (Conf_PingTimeout < 5) {
1407                         Config_Error(LOG_WARNING,
1408                                      "%s, line %d: Value of \"PingTimeout\" too low!",
1409                                      NGIRCd_ConfFile, Line);
1410                         Conf_PingTimeout = 5;
1411                 }
1412                 return;
1413         }
1414         if (strcasecmp(Var, "PongTimeout") == 0) {
1415                 Conf_PongTimeout = atoi(Arg);
1416                 if (Conf_PongTimeout < 5) {
1417                         Config_Error(LOG_WARNING,
1418                                      "%s, line %d: Value of \"PongTimeout\" too low!",
1419                                      NGIRCd_ConfFile, Line);
1420                         Conf_PongTimeout = 5;
1421                 }
1422                 return;
1423         }
1424
1425         Config_Error_Section(Line, Var, "Limits");
1426 }
1427
1428 /**
1429  * Handle variable in [Options] configuration section.
1430  *
1431  * @param Line  Line numer in configuration file.
1432  * @param Var   Variable name.
1433  * @param Arg   Variable argument.
1434  */
1435 static void
1436 Handle_OPTIONS(int Line, char *Var, char *Arg)
1437 {
1438         size_t len;
1439
1440         assert(Line > 0);
1441         assert(Var != NULL);
1442         assert(Arg != NULL);
1443
1444         if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1445                 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1446                 return;
1447         }
1448         if (strcasecmp(Var, "ChrootDir") == 0) {
1449                 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1450                 if (len >= sizeof(Conf_Chroot))
1451                         Config_Error_TooLong(Line, Var);
1452                 return;
1453         }
1454         if (strcasecmp(Var, "CloakHost") == 0) {
1455                 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1456                 if (len >= sizeof(Conf_CloakHost))
1457                         Config_Error_TooLong(Line, Var);
1458                 return;
1459         }
1460         if (strcasecmp(Var, "CloakUserToNick") == 0) {
1461                 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1462                 return;
1463         }
1464         if (strcasecmp(Var, "ConnectIPv6") == 0) {
1465                 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1466                 WarnIPv6(Line);
1467                 return;
1468         }
1469         if (strcasecmp(Var, "ConnectIPv4") == 0) {
1470                 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1471                 return;
1472         }
1473         if (strcasecmp(Var, "DNS") == 0) {
1474                 Conf_DNS = Check_ArgIsTrue(Arg);
1475                 return;
1476         }
1477         if (strcasecmp(Var, "Ident") == 0) {
1478                 Conf_Ident = Check_ArgIsTrue(Arg);
1479                 WarnIdent(Line);
1480                 return;
1481         }
1482         if (strcasecmp(Var, "MorePrivacy") == 0) {
1483                 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1484                 return;
1485         }
1486         if (strcasecmp(Var, "NoticeAuth") == 0) {
1487                 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1488                 return;
1489         }
1490         if (strcasecmp(Var, "OperCanUseMode") == 0) {
1491                 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1492                 return;
1493         }
1494         if (strcasecmp(Var, "OperServerMode") == 0) {
1495                 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1496                 return;
1497         }
1498         if (strcasecmp(Var, "PAM") == 0) {
1499                 Conf_PAM = Check_ArgIsTrue(Arg);
1500                 WarnPAM(Line);
1501                 return;
1502         }
1503         if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1504                 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1505                 return;
1506         }
1507 #ifndef STRICT_RFC
1508         if (strcasecmp(Var, "RequireAuthPing") == 0) {
1509                 Conf_AuthPing = Check_ArgIsTrue(Arg);
1510                 return;
1511         }
1512 #endif
1513         if (strcasecmp(Var, "ScrubCTCP") == 0) {
1514                 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1515                 return;
1516         }
1517 #ifdef SYSLOG
1518         if (strcasecmp(Var, "SyslogFacility") == 0) {
1519                 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1520                                                            Conf_SyslogFacility);
1521                 return;
1522         }
1523 #endif
1524         if (strcasecmp(Var, "WebircPassword") == 0) {
1525                 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1526                 if (len >= sizeof(Conf_WebircPwd))
1527                         Config_Error_TooLong(Line, Var);
1528                 return;
1529         }
1530
1531         Config_Error_Section(Line, Var, "Options");
1532 }
1533
1534 #ifdef SSL_SUPPORT
1535
1536 /**
1537  * Handle variable in [SSL] configuration section.
1538  *
1539  * @param Line  Line numer in configuration file.
1540  * @param Var   Variable name.
1541  * @param Arg   Variable argument.
1542  */
1543 static void
1544 Handle_SSL(int Line, char *Var, char *Arg)
1545 {
1546         assert(Line > 0);
1547         assert(Var != NULL);
1548         assert(Arg != NULL);
1549
1550         if (strcasecmp(Var, "CertFile") == 0) {
1551                 assert(Conf_SSLOptions.CertFile == NULL);
1552                 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1553                 return;
1554         }
1555         if (strcasecmp(Var, "DHFile") == 0) {
1556                 assert(Conf_SSLOptions.DHFile == NULL);
1557                 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1558                 return;
1559         }
1560         if (strcasecmp(Var, "KeyFile") == 0) {
1561                 assert(Conf_SSLOptions.KeyFile == NULL);
1562                 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1563                 return;
1564         }
1565         if (strcasecmp(Var, "KeyFilePassword") == 0) {
1566                 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1567                 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1568                         Config_Error(LOG_ERR,
1569                                      "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1570                                      NGIRCd_ConfFile, Line, Var,
1571                                      strerror(errno));
1572                 return;
1573         }
1574         if (strcasecmp(Var, "Ports") == 0) {
1575                 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1576                 return;
1577         }
1578
1579         Config_Error_Section(Line, Var, "SSL");
1580 }
1581
1582 #endif
1583
1584 /**
1585  * Handle variable in [Operator] configuration section.
1586  *
1587  * @param Line  Line numer in configuration file.
1588  * @param Var   Variable name.
1589  * @param Arg   Variable argument.
1590  */
1591 static void
1592 Handle_OPERATOR( int Line, char *Var, char *Arg )
1593 {
1594         size_t len;
1595         struct Conf_Oper *op;
1596
1597         assert( Line > 0 );
1598         assert( Var != NULL );
1599         assert( Arg != NULL );
1600
1601         op = array_get(&Conf_Opers, sizeof(*op),
1602                          array_length(&Conf_Opers, sizeof(*op)) - 1);
1603         if (!op)
1604                 return;
1605
1606         if (strcasecmp(Var, "Name") == 0) {
1607                 /* Name of IRC operator */
1608                 len = strlcpy(op->name, Arg, sizeof(op->name));
1609                 if (len >= sizeof(op->name))
1610                                 Config_Error_TooLong(Line, Var);
1611                 return;
1612         }
1613         if (strcasecmp(Var, "Password") == 0) {
1614                 /* Password of IRC operator */
1615                 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1616                 if (len >= sizeof(op->pwd))
1617                                 Config_Error_TooLong(Line, Var);
1618                 return;
1619         }
1620         if (strcasecmp(Var, "Mask") == 0) {
1621                 if (op->mask)
1622                         return; /* Hostname already configured */
1623                 op->mask = strdup_warn( Arg );
1624                 return;
1625         }
1626
1627         Config_Error_Section(Line, Var, "Operator");
1628 }
1629
1630 /**
1631  * Handle variable in [Server] configuration section.
1632  *
1633  * @param Line  Line numer in configuration file.
1634  * @param Var   Variable name.
1635  * @param Arg   Variable argument.
1636  */
1637 static void
1638 Handle_SERVER( int Line, char *Var, char *Arg )
1639 {
1640         long port;
1641         size_t len;
1642
1643         assert( Line > 0 );
1644         assert( Var != NULL );
1645         assert( Arg != NULL );
1646
1647         /* Ignore server block if no space is left in server configuration structure */
1648         if( New_Server_Idx <= NONE ) return;
1649
1650         if( strcasecmp( Var, "Host" ) == 0 ) {
1651                 /* Hostname of the server */
1652                 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1653                 if (len >= sizeof( New_Server.host ))
1654                         Config_Error_TooLong ( Line, Var );
1655                 return;
1656         }
1657         if( strcasecmp( Var, "Name" ) == 0 ) {
1658                 /* Name of the server ("Nick"/"ID") */
1659                 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1660                 if (len >= sizeof( New_Server.name ))
1661                         Config_Error_TooLong( Line, Var );
1662                 return;
1663         }
1664         if (strcasecmp(Var, "Bind") == 0) {
1665                 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1666                         return;
1667
1668                 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1669                                 NGIRCd_ConfFile, Line, Arg);
1670                 return;
1671         }
1672         if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1673                 /* Password of this server which is sent to the peer */
1674                 if (*Arg == ':') {
1675                         Config_Error(LOG_ERR,
1676                                 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1677                                                                                 NGIRCd_ConfFile, Line);
1678                 }
1679                 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1680                 if (len >= sizeof( New_Server.pwd_in ))
1681                         Config_Error_TooLong( Line, Var );
1682                 return;
1683         }
1684         if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1685                 /* Passwort of the peer which must be received */
1686                 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1687                 if (len >= sizeof( New_Server.pwd_out ))
1688                         Config_Error_TooLong( Line, Var );
1689                 return;
1690         }
1691         if( strcasecmp( Var, "Port" ) == 0 ) {
1692                 /* Port to which this server should connect */
1693                 port = atol( Arg );
1694                 if (port >= 0 && port < 0xFFFF)
1695                         New_Server.port = (UINT16)port;
1696                 else
1697                         Config_Error(LOG_ERR,
1698                                 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1699                                 NGIRCd_ConfFile, Line, port );
1700                 return;
1701         }
1702 #ifdef SSL_SUPPORT
1703         if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1704                 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1705                 return;
1706         }
1707 #endif
1708         if( strcasecmp( Var, "Group" ) == 0 ) {
1709                 /* Server group */
1710                 New_Server.group = atoi( Arg );
1711                 if (!New_Server.group && strcmp(Arg, "0"))
1712                         Config_Error_NaN(Line, Var);
1713                 return;
1714         }
1715         if( strcasecmp( Var, "Passive" ) == 0 ) {
1716                 if (Check_ArgIsTrue(Arg))
1717                         New_Server.flags |= CONF_SFLAG_DISABLED;
1718                 return;
1719         }
1720         if (strcasecmp(Var, "ServiceMask") == 0) {
1721                 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1722                               sizeof(New_Server.svs_mask));
1723                 if (len >= sizeof(New_Server.svs_mask))
1724                         Config_Error_TooLong(Line, Var);
1725                 return;
1726         }
1727
1728         Config_Error_Section(Line, Var, "Server");
1729 }
1730
1731 /**
1732  * Copy channel name into channel structure.
1733  *
1734  * If the channel name is not valid because of a missing prefix ('#', '&'),
1735  * a default prefix of '#' will be added.
1736  *
1737  * @param new_chan      New already allocated channel structure.
1738  * @param name          Name of the new channel.
1739  * @returns             true on success, false otherwise.
1740  */
1741 static bool
1742 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1743 {
1744         size_t size = sizeof(new_chan->name);
1745         char *dest = new_chan->name;
1746
1747         if (!Channel_IsValidName(name)) {
1748                 /*
1749                  * maybe user forgot to add a '#'.
1750                  * This is only here for user convenience.
1751                  */
1752                 *dest = '#';
1753                 --size;
1754                 ++dest;
1755         }
1756         return size > strlcpy(dest, name, size);
1757 }
1758
1759 /**
1760  * Handle variable in [Channel] configuration section.
1761  *
1762  * @param Line  Line numer in configuration file.
1763  * @param Var   Variable name.
1764  * @param Arg   Variable argument.
1765  */
1766 static void
1767 Handle_CHANNEL(int Line, char *Var, char *Arg)
1768 {
1769         size_t len;
1770         struct Conf_Channel *chan;
1771
1772         assert( Line > 0 );
1773         assert( Var != NULL );
1774         assert( Arg != NULL );
1775
1776         chan = array_get(&Conf_Channels, sizeof(*chan),
1777                          array_length(&Conf_Channels, sizeof(*chan)) - 1);
1778         if (!chan)
1779                 return;
1780
1781         if (strcasecmp(Var, "Name") == 0) {
1782                 if (!Handle_Channelname(chan, Arg))
1783                         Config_Error_TooLong(Line, Var);
1784                 return;
1785         }
1786         if (strcasecmp(Var, "Modes") == 0) {
1787                 /* Initial modes */
1788                 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1789                 if (len >= sizeof(chan->modes))
1790                         Config_Error_TooLong( Line, Var );
1791                 return;
1792         }
1793         if( strcasecmp( Var, "Topic" ) == 0 ) {
1794                 /* Initial topic */
1795                 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1796                 if (len >= sizeof(chan->topic))
1797                         Config_Error_TooLong( Line, Var );
1798                 return;
1799         }
1800         if( strcasecmp( Var, "Key" ) == 0 ) {
1801                 /* Initial Channel Key (mode k) */
1802                 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1803                 if (len >= sizeof(chan->key))
1804                         Config_Error_TooLong(Line, Var);
1805                 return;
1806         }
1807         if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1808                 /* maximum user limit, mode l */
1809                 chan->maxusers = (unsigned long) atol(Arg);
1810                 if (!chan->maxusers && strcmp(Arg, "0"))
1811                         Config_Error_NaN(Line, Var);
1812                 return;
1813         }
1814         if (strcasecmp(Var, "KeyFile") == 0) {
1815                 /* channel keys */
1816                 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1817                 if (len >= sizeof(chan->keyfile))
1818                         Config_Error_TooLong(Line, Var);
1819                 return;
1820         }
1821
1822         Config_Error_Section(Line, Var, "Channel");
1823 }
1824
1825 /**
1826  * Validate server configuration.
1827  *
1828  * Please note that this function uses exit(1) on fatal errors and therefore
1829  * can result in ngIRCd terminating!
1830  *
1831  * @param Configtest    true if the daemon has been called with "--configtest".
1832  * @param Rehash        true if re-reading configuration on runtime.
1833  * @returns             true if configuration is valid.
1834  */
1835 static bool
1836 Validate_Config(bool Configtest, bool Rehash)
1837 {
1838         /* Validate configuration settings. */
1839
1840 #ifdef DEBUG
1841         int i, servers, servers_once;
1842 #endif
1843         bool config_valid = true;
1844         char *ptr;
1845
1846         /* Validate configured server name, see RFC 2812 section 2.3.1 */
1847         ptr = Conf_ServerName;
1848         do {
1849                 if (*ptr >= 'a' && *ptr <= 'z') continue;
1850                 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1851                 if (*ptr >= '0' && *ptr <= '9') continue;
1852                 if (ptr > Conf_ServerName) {
1853                         if (*ptr == '.' || *ptr == '-')
1854                                 continue;
1855                 }
1856                 Conf_ServerName[0] = '\0';
1857                 break;
1858         } while (*(++ptr));
1859
1860         if (!Conf_ServerName[0]) {
1861                 /* No server name configured! */
1862                 config_valid = false;
1863                 Config_Error(LOG_ALERT,
1864                              "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1865                              NGIRCd_ConfFile);
1866                 if (!Configtest && !Rehash) {
1867                         Config_Error(LOG_ALERT,
1868                                      "%s exiting due to fatal errors!",
1869                                      PACKAGE_NAME);
1870                         exit(1);
1871                 }
1872         }
1873
1874         if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1875                 /* No dot in server name! */
1876                 config_valid = false;
1877                 Config_Error(LOG_ALERT,
1878                              "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1879                              NGIRCd_ConfFile);
1880                 if (!Configtest) {
1881                         Config_Error(LOG_ALERT,
1882                                      "%s exiting due to fatal errors!",
1883                                      PACKAGE_NAME);
1884                         exit(1);
1885                 }
1886         }
1887
1888 #ifdef STRICT_RFC
1889         if (!Conf_ServerAdminMail[0]) {
1890                 /* No administrative contact configured! */
1891                 config_valid = false;
1892                 Config_Error(LOG_ALERT,
1893                              "No administrator email address configured in \"%s\" ('AdminEMail')!",
1894                              NGIRCd_ConfFile);
1895                 if (!Configtest) {
1896                         Config_Error(LOG_ALERT,
1897                                      "%s exiting due to fatal errors!",
1898                                      PACKAGE_NAME);
1899                         exit(1);
1900                 }
1901         }
1902 #endif
1903
1904         if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1905             && !Conf_ServerAdminMail[0]) {
1906                 /* No administrative information configured! */
1907                 Config_Error(LOG_WARNING,
1908                              "No administrative information configured but required by RFC!");
1909         }
1910
1911 #ifdef PAM
1912         if (Conf_ServerPwd[0])
1913                 Config_Error(LOG_ERR,
1914                              "This server uses PAM, \"Password\" in [Global] section will be ignored!");
1915 #endif
1916
1917 #ifdef DEBUG
1918         servers = servers_once = 0;
1919         for (i = 0; i < MAX_SERVERS; i++) {
1920                 if (Conf_Server[i].name[0]) {
1921                         servers++;
1922                         if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1923                                 servers_once++;
1924                 }
1925         }
1926         Log(LOG_DEBUG,
1927             "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
1928             array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
1929             servers, servers_once,
1930             array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
1931 #endif
1932
1933         return config_valid;
1934 }
1935
1936 /**
1937  * Output "line too long" warning.
1938  *
1939  * @param Line  Line number in configuration file.
1940  * @param Item  Affected variable name.
1941  */
1942 static void
1943 Config_Error_TooLong ( const int Line, const char *Item )
1944 {
1945         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1946 }
1947
1948 /**
1949  * Output "unknown variable" warning.
1950  *
1951  * @param Line          Line number in configuration file.
1952  * @param Item          Affected variable name.
1953  * @param Section       Section name.
1954  */
1955 static void
1956 Config_Error_Section(const int Line, const char *Item, const char *Section)
1957 {
1958         Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
1959                      NGIRCd_ConfFile, Line, Section, Item);
1960 }
1961
1962 /**
1963  * Output "not a number" warning.
1964  *
1965  * @param Line  Line number in configuration file.
1966  * @param Item  Affected variable name.
1967  */
1968 static void
1969 Config_Error_NaN( const int Line, const char *Item )
1970 {
1971         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1972                                                 NGIRCd_ConfFile, Line, Item );
1973 }
1974
1975 /**
1976  * Output configuration error to console and/or logfile.
1977  *
1978  * On runtime, the normal log functions of the daemon are used. But when
1979  * testing the configuration ("--configtest"), all messages go directly
1980  * to the console.
1981  *
1982  * @param Level         Severity level of the message.
1983  * @param Format        Format string; see printf() function.
1984  */
1985 #ifdef PROTOTYPES
1986 static void Config_Error( const int Level, const char *Format, ... )
1987 #else
1988 static void Config_Error( Level, Format, va_alist )
1989 const int Level;
1990 const char *Format;
1991 va_dcl
1992 #endif
1993 {
1994         char msg[MAX_LOG_MSG_LEN];
1995         va_list ap;
1996
1997         assert( Format != NULL );
1998
1999 #ifdef PROTOTYPES
2000         va_start( ap, Format );
2001 #else
2002         va_start( ap );
2003 #endif
2004         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
2005         va_end( ap );
2006
2007         if (!Use_Log) {
2008                 if (Level <= LOG_WARNING)
2009                         printf(" - %s\n", msg);
2010                 else
2011                         puts(msg);
2012         } else
2013                 Log(Level, "%s", msg);
2014 }
2015
2016 #ifdef DEBUG
2017
2018 /**
2019  * Dump internal state of the "configuration module".
2020  */
2021 GLOBAL void
2022 Conf_DebugDump(void)
2023 {
2024         int i;
2025
2026         Log(LOG_DEBUG, "Configured servers:");
2027         for (i = 0; i < MAX_SERVERS; i++) {
2028                 if (! Conf_Server[i].name[0])
2029                         continue;
2030                 Log(LOG_DEBUG,
2031                     " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2032                     Conf_Server[i].name, Conf_Server[i].host,
2033                     Conf_Server[i].port, Conf_Server[i].lasttry,
2034                     Conf_Server[i].group, Conf_Server[i].flags,
2035                     Conf_Server[i].conn_id);
2036         }
2037 }
2038
2039 #endif
2040
2041 /**
2042  * Initialize server configuration structur to default values.
2043  *
2044  * @param Server        Pointer to server structure to initialize.
2045  */
2046 static void
2047 Init_Server_Struct( CONF_SERVER *Server )
2048 {
2049         assert( Server != NULL );
2050
2051         memset( Server, 0, sizeof (CONF_SERVER) );
2052
2053         Server->group = NONE;
2054         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2055
2056         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2057
2058         Proc_InitStruct(&Server->res_stat);
2059         Server->conn_id = NONE;
2060         memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));
2061 }
2062
2063 /* -eof- */