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