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