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