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