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