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