]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
Merge branch 'MorePrivacy'
[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 an string argument is true or false.
982  *
983  * @param Arg   Input string.
984  * @returns     true if string has been parsed as "yes"/"true"/"on".
985  */
986 static bool
987 Check_ArgIsTrue( const char *Arg )
988 {
989         if( strcasecmp( Arg, "yes" ) == 0 ) return true;
990         if( strcasecmp( Arg, "true" ) == 0 ) return true;
991         if( atoi( Arg ) != 0 ) return true;
992
993         return false;
994 }
995
996 /**
997  * Handle setting of "MaxNickLength".
998  *
999  * @param Line  Line number in configuration file.
1000  * @raram Arg   Input string.
1001  * @returns     New configured maximum nick name length.
1002  */
1003 static unsigned int
1004 Handle_MaxNickLength(int Line, const char *Arg)
1005 {
1006         unsigned new;
1007
1008         new = (unsigned) atoi(Arg) + 1;
1009         if (new > CLIENT_NICK_LEN) {
1010                 Config_Error(LOG_WARNING,
1011                              "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
1012                              NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
1013                 return CLIENT_NICK_LEN;
1014         }
1015         if (new < 2) {
1016                 Config_Error(LOG_WARNING,
1017                              "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
1018                              NGIRCd_ConfFile, Line);
1019                 return 2;
1020         }
1021         return new;
1022 }
1023
1024 /**
1025  * Output a warning messages if IDENT is configured but not compiled in.
1026  */
1027 static void
1028 WarnIdent(int UNUSED Line)
1029 {
1030 #ifndef IDENTAUTH
1031         if (Conf_Ident) {
1032                 /* user has enabled ident lookups explicitly, but ... */
1033                 Config_Error(LOG_WARNING,
1034                         "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
1035                         NGIRCd_ConfFile, Line);
1036         }
1037 #endif
1038 }
1039
1040 /**
1041  * Output a warning messages if IPv6 is configured but not compiled in.
1042  */
1043 static void
1044 WarnIPv6(int UNUSED Line)
1045 {
1046 #ifndef WANT_IPV6
1047         if (Conf_ConnectIPv6) {
1048                 /* user has enabled IPv6 explicitly, but ... */
1049                 Config_Error(LOG_WARNING,
1050                         "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
1051                         NGIRCd_ConfFile, Line);
1052         }
1053 #endif
1054 }
1055
1056 /**
1057  * Output a warning messages if PAM is configured but not compiled in.
1058  */
1059 static void
1060 WarnPAM(int UNUSED Line)
1061 {
1062 #ifndef PAM
1063         if (Conf_PAM) {
1064                 Config_Error(LOG_WARNING,
1065                         "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
1066                         NGIRCd_ConfFile, Line);
1067         }
1068 #endif
1069 }
1070
1071 /**
1072  * Handle legacy "NoXXX" options in [GLOBAL] section.
1073  *
1074  * TODO: This function and support for "NoXXX" could be removed starting
1075  * with ngIRCd release 19 (one release after marking it "deprecated").
1076  *
1077  * @param Var   Variable name.
1078  * @param Arg   Argument string.
1079  * @returns     true if a NoXXX option has been processed; false otherwise.
1080  */
1081 static bool
1082 CheckLegacyNoOption(const char *Var, const char *Arg)
1083 {
1084         if(strcasecmp(Var, "NoDNS") == 0) {
1085                 Conf_DNS = !Check_ArgIsTrue( Arg );
1086                 return true;
1087         }
1088         if (strcasecmp(Var, "NoIdent") == 0) {
1089                 Conf_Ident = !Check_ArgIsTrue(Arg);
1090                 return true;
1091         }
1092         if(strcasecmp(Var, "NoPAM") == 0) {
1093                 Conf_PAM = !Check_ArgIsTrue(Arg);
1094                 return true;
1095         }
1096         return false;
1097 }
1098
1099 /**
1100  * Handle deprecated legacy options in [GLOBAL] section.
1101  *
1102  * TODO: This function and support for these options in the [Global] section
1103  * could be removed starting with ngIRCd release 19 (one release after
1104  * marking it "deprecated").
1105  *
1106  * @param Var   Variable name.
1107  * @param Arg   Argument string.
1108  * @returns     true if a legacy option has been processed; false otherwise.
1109  */
1110 static const char*
1111 CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
1112 {
1113         if (strcasecmp(Var, "AllowRemoteOper") == 0
1114             || strcasecmp(Var, "ChrootDir") == 0
1115             || strcasecmp(Var, "ConnectIPv4") == 0
1116             || strcasecmp(Var, "ConnectIPv6") == 0
1117             || strcasecmp(Var, "OperCanUseMode") == 0
1118             || strcasecmp(Var, "OperServerMode") == 0
1119             || strcasecmp(Var, "PredefChannelsOnly") == 0
1120             || strcasecmp(Var, "SyslogFacility") == 0
1121             || strcasecmp(Var, "WebircPassword") == 0) {
1122                 Handle_OPTIONS(Line, Var, Arg);
1123                 return "[Options]";
1124         }
1125         if (strcasecmp(Var, "ConnectRetry") == 0
1126             || strcasecmp(Var, "MaxConnections") == 0
1127             || strcasecmp(Var, "MaxConnectionsIP") == 0
1128             || strcasecmp(Var, "MaxJoins") == 0
1129             || strcasecmp(Var, "MaxNickLength") == 0
1130             || strcasecmp(Var, "PingTimeout") == 0
1131             || strcasecmp(Var, "PongTimeout") == 0) {
1132                 Handle_LIMITS(Line, Var, Arg);
1133                 return "[Limits]";
1134         }
1135 #ifdef SSL_SUPPORT
1136         if (strcasecmp(Var, "SSLCertFile") == 0
1137             || strcasecmp(Var, "SSLDHFile") == 0
1138             || strcasecmp(Var, "SSLKeyFile") == 0
1139             || strcasecmp(Var, "SSLKeyFilePassword") == 0
1140             || strcasecmp(Var, "SSLPorts") == 0) {
1141                 Handle_SSL(Line, Var + 3, Arg);
1142                 return "[SSL]";
1143         }
1144 #endif
1145
1146         return NULL;
1147 }
1148
1149 /**
1150  * Strip "no" prefix of a string.
1151  *
1152  * TODO: This function and support for "NoXXX" should be removed starting
1153  * with ngIRCd release 19! (One release after marking it "deprecated").
1154  *
1155  * @param str   Pointer to input string starting with "no".
1156  * @returns     New pointer to string without "no" prefix.
1157  */
1158 static const char *
1159 NoNo(const char *str)
1160 {
1161         assert(strncasecmp("no", str, 2) == 0 && str[2]);
1162         return str + 2;
1163 }
1164
1165 /**
1166  * Invert "boolean" string.
1167  *
1168  * TODO: This function and support for "NoXXX" should be removed starting
1169  * with ngIRCd release 19! (One release after marking it "deprecated").
1170  *
1171  * @param arg   "Boolean" input string.
1172  * @returns     Pointer to inverted "boolean string".
1173  */
1174 static const char *
1175 InvertArg(const char *arg)
1176 {
1177         return yesno_to_str(!Check_ArgIsTrue(arg));
1178 }
1179
1180 /**
1181  * Handle variable in [Global] configuration section.
1182  *
1183  * @param Line  Line numer in configuration file.
1184  * @param Var   Variable name.
1185  * @param Arg   Variable argument.
1186  */
1187 static void
1188 Handle_GLOBAL( int Line, char *Var, char *Arg )
1189 {
1190         struct passwd *pwd;
1191         struct group *grp;
1192         size_t len;
1193         const char *section;
1194
1195         assert(Line > 0);
1196         assert(Var != NULL);
1197         assert(Arg != NULL);
1198
1199         if (strcasecmp(Var, "Name") == 0) {
1200                 len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
1201                 if (len >= sizeof(Conf_ServerName))
1202                         Config_Error_TooLong(Line, Var);
1203                 return;
1204         }
1205         if (strcasecmp(Var, "AdminInfo1") == 0) {
1206                 len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
1207                 if (len >= sizeof(Conf_ServerAdmin1))
1208                         Config_Error_TooLong(Line, Var);
1209                 return;
1210         }
1211         if (strcasecmp(Var, "AdminInfo2") == 0) {
1212                 len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
1213                 if (len >= sizeof(Conf_ServerAdmin2))
1214                         Config_Error_TooLong(Line, Var);
1215                 return;
1216         }
1217         if (strcasecmp(Var, "AdminEMail") == 0) {
1218                 len = strlcpy(Conf_ServerAdminMail, Arg,
1219                         sizeof(Conf_ServerAdminMail));
1220                 if (len >= sizeof(Conf_ServerAdminMail))
1221                         Config_Error_TooLong(Line, Var);
1222                 return;
1223         }
1224         if (strcasecmp(Var, "Info") == 0) {
1225                 len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
1226                 if (len >= sizeof(Conf_ServerInfo))
1227                         Config_Error_TooLong(Line, Var);
1228                 return;
1229         }
1230         if (strcasecmp(Var, "Listen") == 0) {
1231                 if (Conf_ListenAddress) {
1232                         Config_Error(LOG_ERR,
1233                                      "Multiple Listen= options, ignoring: %s",
1234                                      Arg);
1235                         return;
1236                 }
1237                 Conf_ListenAddress = strdup_warn(Arg);
1238                 /* If allocation fails, we're in trouble: we cannot ignore the
1239                  * error -- otherwise ngircd would listen on all interfaces. */
1240                 if (!Conf_ListenAddress) {
1241                         Config_Error(LOG_ALERT,
1242                                      "%s exiting due to fatal errors!",
1243                                      PACKAGE_NAME);
1244                         exit(1);
1245                 }
1246                 return;
1247         }
1248         if (strcasecmp(Var, "MotdFile") == 0) {
1249                 len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
1250                 if (len >= sizeof(Conf_MotdFile))
1251                         Config_Error_TooLong(Line, Var);
1252                 return;
1253         }
1254         if (strcasecmp(Var, "MotdPhrase") == 0) {
1255                 len = strlen(Arg);
1256                 if (len == 0)
1257                         return;
1258                 if (len >= LINE_LEN) {
1259                         Config_Error_TooLong(Line, Var);
1260                         return;
1261                 }
1262                 if (!array_copyb(&Conf_Motd, Arg, len + 1))
1263                         Config_Error(LOG_WARNING,
1264                                      "%s, line %d: Could not append MotdPhrase: %s",
1265                                      NGIRCd_ConfFile, Line, strerror(errno));
1266                 Using_MotdFile = false;
1267                 return;
1268         }
1269         if(strcasecmp(Var, "Password") == 0) {
1270                 len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
1271                 if (len >= sizeof(Conf_ServerPwd))
1272                         Config_Error_TooLong(Line, Var);
1273                 return;
1274         }
1275         if (strcasecmp(Var, "PidFile") == 0) {
1276                 len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
1277                 if (len >= sizeof(Conf_PidFile))
1278                         Config_Error_TooLong(Line, Var);
1279                 return;
1280         }
1281         if (strcasecmp(Var, "Ports") == 0) {
1282                 ports_parse(&Conf_ListenPorts, Line, Arg);
1283                 return;
1284         }
1285         if (strcasecmp(Var, "ServerGID") == 0) {
1286                 grp = getgrnam(Arg);
1287                 if (grp)
1288                         Conf_GID = grp->gr_gid;
1289                 else {
1290                         Conf_GID = (unsigned int)atoi(Arg);
1291                         if (!Conf_GID && strcmp(Arg, "0"))
1292                                 Config_Error_NaN(Line, Var);
1293                 }
1294                 return;
1295         }
1296         if (strcasecmp(Var, "ServerUID") == 0) {
1297                 pwd = getpwnam(Arg);
1298                 if (pwd)
1299                         Conf_UID = pwd->pw_uid;
1300                 else {
1301                         Conf_UID = (unsigned int)atoi(Arg);
1302                         if (!Conf_UID && strcmp(Arg, "0"))
1303                                 Config_Error_NaN(Line, Var);
1304                 }
1305                 return;
1306         }
1307
1308         if (CheckLegacyNoOption(Var, Arg)) {
1309                 /* TODO: This function and support for "NoXXX" could be
1310                  * be removed starting with ngIRCd release 19 (one release
1311                  * after marking it "deprecated"). */
1312                 Config_Error(LOG_WARNING,
1313                              "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
1314                              NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
1315                 if (strcasecmp(Var, "NoIdent") == 0)
1316                         WarnIdent(Line);
1317                 else if (strcasecmp(Var, "NoPam") == 0)
1318                         WarnPAM(Line);
1319                 return;
1320         }
1321         if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
1322                 /** TODO: This function and support for these options in the
1323                  * [Global] section could be removed starting with ngIRCd
1324                  * release 19 (one release after marking it "deprecated"). */
1325                 if (strncasecmp(Var, "SSL", 3) == 0) {
1326                         Config_Error(LOG_WARNING,
1327                                      "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
1328                                      NGIRCd_ConfFile, Line, Var, section,
1329                                      Var + 3);
1330                 } else {
1331                         Config_Error(LOG_WARNING,
1332                                      "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
1333                                      NGIRCd_ConfFile, Line, Var, section);
1334                 }
1335                 return;
1336         }
1337
1338         Config_Error_Section(Line, Var, "Global");
1339 }
1340
1341 /**
1342  * Handle variable in [Limits] configuration section.
1343  *
1344  * @param Line  Line numer in configuration file.
1345  * @param Var   Variable name.
1346  * @param Arg   Variable argument.
1347  */
1348 static void
1349 Handle_LIMITS(int Line, char *Var, char *Arg)
1350 {
1351         assert(Line > 0);
1352         assert(Var != NULL);
1353         assert(Arg != NULL);
1354
1355         if (strcasecmp(Var, "ConnectRetry") == 0) {
1356                 Conf_ConnectRetry = atoi(Arg);
1357                 if (Conf_ConnectRetry < 5) {
1358                         Config_Error(LOG_WARNING,
1359                                      "%s, line %d: Value of \"ConnectRetry\" too low!",
1360                                      NGIRCd_ConfFile, Line);
1361                         Conf_ConnectRetry = 5;
1362                 }
1363                 return;
1364         }
1365         if (strcasecmp(Var, "MaxConnections") == 0) {
1366                 Conf_MaxConnections = atol(Arg);
1367                 if (!Conf_MaxConnections && strcmp(Arg, "0"))
1368                         Config_Error_NaN(Line, Var);
1369                 return;
1370         }
1371         if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
1372                 Conf_MaxConnectionsIP = atoi(Arg);
1373                 if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
1374                         Config_Error_NaN(Line, Var);
1375                 return;
1376         }
1377         if (strcasecmp(Var, "MaxJoins") == 0) {
1378                 Conf_MaxJoins = atoi(Arg);
1379                 if (!Conf_MaxJoins && strcmp(Arg, "0"))
1380                         Config_Error_NaN(Line, Var);
1381                 return;
1382         }
1383         if (strcasecmp(Var, "MaxNickLength") == 0) {
1384                 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1385                 return;
1386         }
1387         if (strcasecmp(Var, "PingTimeout") == 0) {
1388                 Conf_PingTimeout = atoi(Arg);
1389                 if (Conf_PingTimeout < 5) {
1390                         Config_Error(LOG_WARNING,
1391                                      "%s, line %d: Value of \"PingTimeout\" too low!",
1392                                      NGIRCd_ConfFile, Line);
1393                         Conf_PingTimeout = 5;
1394                 }
1395                 return;
1396         }
1397         if (strcasecmp(Var, "PongTimeout") == 0) {
1398                 Conf_PongTimeout = atoi(Arg);
1399                 if (Conf_PongTimeout < 5) {
1400                         Config_Error(LOG_WARNING,
1401                                      "%s, line %d: Value of \"PongTimeout\" too low!",
1402                                      NGIRCd_ConfFile, Line);
1403                         Conf_PongTimeout = 5;
1404                 }
1405                 return;
1406         }
1407
1408         Config_Error_Section(Line, Var, "Limits");
1409 }
1410
1411 /**
1412  * Handle variable in [Options] configuration section.
1413  *
1414  * @param Line  Line numer in configuration file.
1415  * @param Var   Variable name.
1416  * @param Arg   Variable argument.
1417  */
1418 static void
1419 Handle_OPTIONS(int Line, char *Var, char *Arg)
1420 {
1421         size_t len;
1422
1423         assert(Line > 0);
1424         assert(Var != NULL);
1425         assert(Arg != NULL);
1426
1427         if (strcasecmp(Var, "AllowRemoteOper") == 0) {
1428                 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
1429                 return;
1430         }
1431         if (strcasecmp(Var, "ChrootDir") == 0) {
1432                 len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
1433                 if (len >= sizeof(Conf_Chroot))
1434                         Config_Error_TooLong(Line, Var);
1435                 return;
1436         }
1437         if (strcasecmp(Var, "CloakHost") == 0) {
1438                 len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
1439                 if (len >= sizeof(Conf_CloakHost))
1440                         Config_Error_TooLong(Line, Var);
1441                 return;
1442         }
1443         if (strcasecmp(Var, "CloakUserToNick") == 0) {
1444                 Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
1445                 return;
1446         }
1447         if (strcasecmp(Var, "ConnectIPv6") == 0) {
1448                 Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
1449                 WarnIPv6(Line);
1450                 return;
1451         }
1452         if (strcasecmp(Var, "ConnectIPv4") == 0) {
1453                 Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
1454                 return;
1455         }
1456         if (strcasecmp(Var, "DNS") == 0) {
1457                 Conf_DNS = Check_ArgIsTrue(Arg);
1458                 return;
1459         }
1460         if (strcasecmp(Var, "Ident") == 0) {
1461                 Conf_Ident = Check_ArgIsTrue(Arg);
1462                 WarnIdent(Line);
1463                 return;
1464         }
1465         if (strcasecmp(Var, "MorePrivacy") == 0) {
1466                 Conf_MorePrivacy = Check_ArgIsTrue(Arg);
1467                 return;
1468         }
1469         if (strcasecmp(Var, "NoticeAuth") == 0) {
1470                 Conf_NoticeAuth = Check_ArgIsTrue(Arg);
1471                 return;
1472         }
1473         if (strcasecmp(Var, "OperCanUseMode") == 0) {
1474                 Conf_OperCanMode = Check_ArgIsTrue(Arg);
1475                 return;
1476         }
1477         if (strcasecmp(Var, "OperServerMode") == 0) {
1478                 Conf_OperServerMode = Check_ArgIsTrue(Arg);
1479                 return;
1480         }
1481         if (strcasecmp(Var, "PAM") == 0) {
1482                 Conf_PAM = Check_ArgIsTrue(Arg);
1483                 WarnPAM(Line);
1484                 return;
1485         }
1486         if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
1487                 Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
1488                 return;
1489         }
1490 #ifndef STRICT_RFC
1491         if (strcasecmp(Var, "RequireAuthPing") == 0) {
1492                 Conf_AuthPing = Check_ArgIsTrue(Arg);
1493                 return;
1494         }
1495 #endif
1496         if (strcasecmp(Var, "ScrubCTCP") == 0) {
1497                 Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
1498                 return;
1499         }
1500 #ifdef SYSLOG
1501         if (strcasecmp(Var, "SyslogFacility") == 0) {
1502                 Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
1503                                                            Conf_SyslogFacility);
1504                 return;
1505         }
1506 #endif
1507         if (strcasecmp(Var, "WebircPassword") == 0) {
1508                 len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
1509                 if (len >= sizeof(Conf_WebircPwd))
1510                         Config_Error_TooLong(Line, Var);
1511                 return;
1512         }
1513
1514         Config_Error_Section(Line, Var, "Options");
1515 }
1516
1517 #ifdef SSL_SUPPORT
1518
1519 /**
1520  * Handle variable in [SSL] configuration section.
1521  *
1522  * @param Line  Line numer in configuration file.
1523  * @param Var   Variable name.
1524  * @param Arg   Variable argument.
1525  */
1526 static void
1527 Handle_SSL(int Line, char *Var, char *Arg)
1528 {
1529         assert(Line > 0);
1530         assert(Var != NULL);
1531         assert(Arg != NULL);
1532
1533         if (strcasecmp(Var, "CertFile") == 0) {
1534                 assert(Conf_SSLOptions.CertFile == NULL);
1535                 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1536                 return;
1537         }
1538         if (strcasecmp(Var, "DHFile") == 0) {
1539                 assert(Conf_SSLOptions.DHFile == NULL);
1540                 Conf_SSLOptions.DHFile = strdup_warn(Arg);
1541                 return;
1542         }
1543         if (strcasecmp(Var, "KeyFile") == 0) {
1544                 assert(Conf_SSLOptions.KeyFile == NULL);
1545                 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1546                 return;
1547         }
1548         if (strcasecmp(Var, "KeyFilePassword") == 0) {
1549                 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1550                 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1551                         Config_Error(LOG_ERR,
1552                                      "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
1553                                      NGIRCd_ConfFile, Line, Var,
1554                                      strerror(errno));
1555                 return;
1556         }
1557         if (strcasecmp(Var, "Ports") == 0) {
1558                 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1559                 return;
1560         }
1561
1562         Config_Error_Section(Line, Var, "SSL");
1563 }
1564
1565 #endif
1566
1567 /**
1568  * Handle variable in [Operator] configuration section.
1569  *
1570  * @param Line  Line numer in configuration file.
1571  * @param Var   Variable name.
1572  * @param Arg   Variable argument.
1573  */
1574 static void
1575 Handle_OPERATOR( int Line, char *Var, char *Arg )
1576 {
1577         size_t len;
1578         struct Conf_Oper *op;
1579
1580         assert( Line > 0 );
1581         assert( Var != NULL );
1582         assert( Arg != NULL );
1583         assert( Conf_Oper_Count > 0 );
1584
1585         op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
1586         if (!op) {
1587                 Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
1588                 return;
1589         }
1590
1591         if (strcasecmp(Var, "Name") == 0) {
1592                 /* Name of IRC operator */
1593                 len = strlcpy(op->name, Arg, sizeof(op->name));
1594                 if (len >= sizeof(op->name))
1595                                 Config_Error_TooLong(Line, Var);
1596                 return;
1597         }
1598         if (strcasecmp(Var, "Password") == 0) {
1599                 /* Password of IRC operator */
1600                 len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
1601                 if (len >= sizeof(op->pwd))
1602                                 Config_Error_TooLong(Line, Var);
1603                 return;
1604         }
1605         if (strcasecmp(Var, "Mask") == 0) {
1606                 if (op->mask)
1607                         return; /* Hostname already configured */
1608                 op->mask = strdup_warn( Arg );
1609                 return;
1610         }
1611
1612         Config_Error_Section(Line, Var, "Operator");
1613 }
1614
1615 /**
1616  * Handle variable in [Server] configuration section.
1617  *
1618  * @param Line  Line numer in configuration file.
1619  * @param Var   Variable name.
1620  * @param Arg   Variable argument.
1621  */
1622 static void
1623 Handle_SERVER( int Line, char *Var, char *Arg )
1624 {
1625         long port;
1626         size_t len;
1627
1628         assert( Line > 0 );
1629         assert( Var != NULL );
1630         assert( Arg != NULL );
1631
1632         /* Ignore server block if no space is left in server configuration structure */
1633         if( New_Server_Idx <= NONE ) return;
1634
1635         if( strcasecmp( Var, "Host" ) == 0 ) {
1636                 /* Hostname of the server */
1637                 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1638                 if (len >= sizeof( New_Server.host ))
1639                         Config_Error_TooLong ( Line, Var );
1640                 return;
1641         }
1642         if( strcasecmp( Var, "Name" ) == 0 ) {
1643                 /* Name of the server ("Nick"/"ID") */
1644                 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1645                 if (len >= sizeof( New_Server.name ))
1646                         Config_Error_TooLong( Line, Var );
1647                 return;
1648         }
1649         if (strcasecmp(Var, "Bind") == 0) {
1650                 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1651                         return;
1652
1653                 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1654                                 NGIRCd_ConfFile, Line, Arg);
1655                 return;
1656         }
1657         if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1658                 /* Password of this server which is sent to the peer */
1659                 if (*Arg == ':') {
1660                         Config_Error(LOG_ERR,
1661                                 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1662                                                                                 NGIRCd_ConfFile, Line);
1663                 }
1664                 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1665                 if (len >= sizeof( New_Server.pwd_in ))
1666                         Config_Error_TooLong( Line, Var );
1667                 return;
1668         }
1669         if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1670                 /* Passwort of the peer which must be received */
1671                 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1672                 if (len >= sizeof( New_Server.pwd_out ))
1673                         Config_Error_TooLong( Line, Var );
1674                 return;
1675         }
1676         if( strcasecmp( Var, "Port" ) == 0 ) {
1677                 /* Port to which this server should connect */
1678                 port = atol( Arg );
1679                 if (port >= 0 && port < 0xFFFF)
1680                         New_Server.port = (UINT16)port;
1681                 else
1682                         Config_Error(LOG_ERR,
1683                                 "%s, line %d (section \"Server\"): Illegal port number %ld!",
1684                                 NGIRCd_ConfFile, Line, port );
1685                 return;
1686         }
1687 #ifdef SSL_SUPPORT
1688         if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1689                 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1690                 return;
1691         }
1692 #endif
1693         if( strcasecmp( Var, "Group" ) == 0 ) {
1694                 /* Server group */
1695                 New_Server.group = atoi( Arg );
1696                 if (!New_Server.group && strcmp(Arg, "0"))
1697                         Config_Error_NaN(Line, Var);
1698                 return;
1699         }
1700         if( strcasecmp( Var, "Passive" ) == 0 ) {
1701                 if (Check_ArgIsTrue(Arg))
1702                         New_Server.flags |= CONF_SFLAG_DISABLED;
1703                 return;
1704         }
1705         if (strcasecmp(Var, "ServiceMask") == 0) {
1706                 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1707                               sizeof(New_Server.svs_mask));
1708                 if (len >= sizeof(New_Server.svs_mask))
1709                         Config_Error_TooLong(Line, Var);
1710                 return;
1711         }
1712
1713         Config_Error_Section(Line, Var, "Server");
1714 }
1715
1716 /**
1717  * Copy channel name into channel structure.
1718  *
1719  * If the channel name is not valid because of a missing prefix ('#', '&'),
1720  * a default prefix of '#' will be added.
1721  *
1722  * @param new_chan      New already allocated channel structure.
1723  * @param name          Name of the new channel.
1724  * @returns             true on success, false otherwise.
1725  */
1726 static bool
1727 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1728 {
1729         size_t size = sizeof(new_chan->name);
1730         char *dest = new_chan->name;
1731
1732         if (!Channel_IsValidName(name)) {
1733                 /*
1734                  * maybe user forgot to add a '#'.
1735                  * This is only here for user convenience.
1736                  */
1737                 *dest = '#';
1738                 --size;
1739                 ++dest;
1740         }
1741         return size > strlcpy(dest, name, size);
1742 }
1743
1744 /**
1745  * Handle variable in [Channel] configuration section.
1746  *
1747  * @param Line  Line numer in configuration file.
1748  * @param Var   Variable name.
1749  * @param Arg   Variable argument.
1750  */
1751 static void
1752 Handle_CHANNEL(int Line, char *Var, char *Arg)
1753 {
1754         size_t len;
1755         size_t chancount;
1756         struct Conf_Channel *chan;
1757
1758         assert( Line > 0 );
1759         assert( Var != NULL );
1760         assert( Arg != NULL );
1761         assert(Conf_Channel_Count > 0);
1762
1763         chancount = Conf_Channel_Count - 1;
1764
1765         chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1766         if (!chan) {
1767                 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1768                 return;
1769         }
1770         if (strcasecmp(Var, "Name") == 0) {
1771                 if (!Handle_Channelname(chan, Arg))
1772                         Config_Error_TooLong(Line, Var);
1773                 return;
1774         }
1775         if (strcasecmp(Var, "Modes") == 0) {
1776                 /* Initial modes */
1777                 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1778                 if (len >= sizeof(chan->modes))
1779                         Config_Error_TooLong( Line, Var );
1780                 return;
1781         }
1782         if( strcasecmp( Var, "Topic" ) == 0 ) {
1783                 /* Initial topic */
1784                 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1785                 if (len >= sizeof(chan->topic))
1786                         Config_Error_TooLong( Line, Var );
1787                 return;
1788         }
1789         if( strcasecmp( Var, "Key" ) == 0 ) {
1790                 /* Initial Channel Key (mode k) */
1791                 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1792                 if (len >= sizeof(chan->key))
1793                         Config_Error_TooLong(Line, Var);
1794                 return;
1795         }
1796         if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1797                 /* maximum user limit, mode l */
1798                 chan->maxusers = (unsigned long) atol(Arg);
1799                 if (!chan->maxusers && strcmp(Arg, "0"))
1800                         Config_Error_NaN(Line, Var);
1801                 return;
1802         }
1803         if (strcasecmp(Var, "KeyFile") == 0) {
1804                 /* channel keys */
1805                 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1806                 if (len >= sizeof(chan->keyfile))
1807                         Config_Error_TooLong(Line, Var);
1808                 return;
1809         }
1810
1811         Config_Error_Section(Line, Var, "Channel");
1812 }
1813
1814 /**
1815  * Validate server configuration.
1816  *
1817  * Please note that this function uses exit(1) on fatal errors and therefore
1818  * can result in ngIRCd terminating!
1819  *
1820  * @param Configtest    true if the daemon has been called with "--configtest".
1821  * @param Rehash        true if re-reading configuration on runtime.
1822  * @returns             true if configuration is valid.
1823  */
1824 static bool
1825 Validate_Config(bool Configtest, bool Rehash)
1826 {
1827         /* Validate configuration settings. */
1828
1829 #ifdef DEBUG
1830         int i, servers, servers_once;
1831 #endif
1832         bool config_valid = true;
1833         char *ptr;
1834
1835         /* Validate configured server name, see RFC 2812 section 2.3.1 */
1836         ptr = Conf_ServerName;
1837         do {
1838                 if (*ptr >= 'a' && *ptr <= 'z') continue;
1839                 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1840                 if (*ptr >= '0' && *ptr <= '9') continue;
1841                 if (ptr > Conf_ServerName) {
1842                         if (*ptr == '.' || *ptr == '-')
1843                                 continue;
1844                 }
1845                 Conf_ServerName[0] = '\0';
1846                 break;
1847         } while (*(++ptr));
1848
1849         if (!Conf_ServerName[0]) {
1850                 /* No server name configured! */
1851                 config_valid = false;
1852                 Config_Error(LOG_ALERT,
1853                              "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1854                              NGIRCd_ConfFile);
1855                 if (!Configtest && !Rehash) {
1856                         Config_Error(LOG_ALERT,
1857                                      "%s exiting due to fatal errors!",
1858                                      PACKAGE_NAME);
1859                         exit(1);
1860                 }
1861         }
1862
1863         if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1864                 /* No dot in server name! */
1865                 config_valid = false;
1866                 Config_Error(LOG_ALERT,
1867                              "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1868                              NGIRCd_ConfFile);
1869                 if (!Configtest) {
1870                         Config_Error(LOG_ALERT,
1871                                      "%s exiting due to fatal errors!",
1872                                      PACKAGE_NAME);
1873                         exit(1);
1874                 }
1875         }
1876
1877 #ifdef STRICT_RFC
1878         if (!Conf_ServerAdminMail[0]) {
1879                 /* No administrative contact configured! */
1880                 config_valid = false;
1881                 Config_Error(LOG_ALERT,
1882                              "No administrator email address configured in \"%s\" ('AdminEMail')!",
1883                              NGIRCd_ConfFile);
1884                 if (!Configtest) {
1885                         Config_Error(LOG_ALERT,
1886                                      "%s exiting due to fatal errors!",
1887                                      PACKAGE_NAME);
1888                         exit(1);
1889                 }
1890         }
1891 #endif
1892
1893         if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1894             && !Conf_ServerAdminMail[0]) {
1895                 /* No administrative information configured! */
1896                 Config_Error(LOG_WARNING,
1897                              "No administrative information configured but required by RFC!");
1898         }
1899
1900 #ifdef PAM
1901         if (Conf_ServerPwd[0])
1902                 Config_Error(LOG_ERR,
1903                              "This server uses PAM, \"Password\" in [Global] section will be ignored!");
1904 #endif
1905
1906 #ifdef DEBUG
1907         servers = servers_once = 0;
1908         for (i = 0; i < MAX_SERVERS; i++) {
1909                 if (Conf_Server[i].name[0]) {
1910                         servers++;
1911                         if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1912                                 servers_once++;
1913                 }
1914         }
1915         Log(LOG_DEBUG,
1916             "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1917             Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1918 #endif
1919
1920         return config_valid;
1921 }
1922
1923 /**
1924  * Output "line too long" warning.
1925  *
1926  * @param Line  Line number in configuration file.
1927  * @param Item  Affected variable name.
1928  */
1929 static void
1930 Config_Error_TooLong ( const int Line, const char *Item )
1931 {
1932         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1933 }
1934
1935 /**
1936  * Output "unknown variable" warning.
1937  *
1938  * @param Line          Line number in configuration file.
1939  * @param Item          Affected variable name.
1940  * @param Section       Section name.
1941  */
1942 static void
1943 Config_Error_Section(const int Line, const char *Item, const char *Section)
1944 {
1945         Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
1946                      NGIRCd_ConfFile, Line, Section, Item);
1947 }
1948
1949 /**
1950  * Output "not a number" warning.
1951  *
1952  * @param Line  Line number in configuration file.
1953  * @param Item  Affected variable name.
1954  */
1955 static void
1956 Config_Error_NaN( const int Line, const char *Item )
1957 {
1958         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1959                                                 NGIRCd_ConfFile, Line, Item );
1960 }
1961
1962 /**
1963  * Output configuration error to console and/or logfile.
1964  *
1965  * On runtime, the normal log functions of the daemon are used. But when
1966  * testing the configuration ("--configtest"), all messages go directly
1967  * to the console.
1968  *
1969  * @param Level         Severity level of the message.
1970  * @param Format        Format string; see printf() function.
1971  */
1972 #ifdef PROTOTYPES
1973 static void Config_Error( const int Level, const char *Format, ... )
1974 #else
1975 static void Config_Error( Level, Format, va_alist )
1976 const int Level;
1977 const char *Format;
1978 va_dcl
1979 #endif
1980 {
1981         char msg[MAX_LOG_MSG_LEN];
1982         va_list ap;
1983
1984         assert( Format != NULL );
1985
1986 #ifdef PROTOTYPES
1987         va_start( ap, Format );
1988 #else
1989         va_start( ap );
1990 #endif
1991         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1992         va_end( ap );
1993
1994         if (!Use_Log) {
1995                 if (Level <= LOG_WARNING)
1996                         printf(" - %s\n", msg);
1997                 else
1998                         puts(msg);
1999         } else
2000                 Log(Level, "%s", msg);
2001 }
2002
2003 #ifdef DEBUG
2004
2005 /**
2006  * Dump internal state of the "configuration module".
2007  */
2008 GLOBAL void
2009 Conf_DebugDump(void)
2010 {
2011         int i;
2012
2013         Log(LOG_DEBUG, "Configured servers:");
2014         for (i = 0; i < MAX_SERVERS; i++) {
2015                 if (! Conf_Server[i].name[0])
2016                         continue;
2017                 Log(LOG_DEBUG,
2018                     " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
2019                     Conf_Server[i].name, Conf_Server[i].host,
2020                     Conf_Server[i].port, Conf_Server[i].lasttry,
2021                     Conf_Server[i].group, Conf_Server[i].flags,
2022                     Conf_Server[i].conn_id);
2023         }
2024 }
2025
2026 #endif
2027
2028 /**
2029  * Initialize server configuration structur to default values.
2030  *
2031  * @param Server        Pointer to server structure to initialize.
2032  */
2033 static void
2034 Init_Server_Struct( CONF_SERVER *Server )
2035 {
2036         assert( Server != NULL );
2037
2038         memset( Server, 0, sizeof (CONF_SERVER) );
2039
2040         Server->group = NONE;
2041         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
2042
2043         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
2044
2045         Proc_InitStruct(&Server->res_stat);
2046         Server->conn_id = NONE;
2047         memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
2048 }
2049
2050 /* -eof- */