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