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