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