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