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