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