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