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