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