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