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