]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
configtest: print ssl config options even when unset
[ngircd-alex.git] / src / ngircd / conf.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2009 Alexander Barton (alex@barton.de)
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  * Configuration management (reading, parsing & validation)
12  */
13
14
15 #include "portab.h"
16
17 #include "imp.h"
18 #include <assert.h>
19 #include <errno.h>
20 #ifdef PROTOTYPES
21 #       include <stdarg.h>
22 #else
23 #       include <varargs.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <unistd.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 #ifdef HAVE_CTYPE_H
36 # include <ctype.h>
37 #endif
38
39 #include "array.h"
40 #include "ngircd.h"
41 #include "conn.h"
42 #include "client.h"
43 #include "channel.h"
44 #include "defines.h"
45 #include "log.h"
46 #include "match.h"
47 #include "resolve.h"
48 #include "tool.h"
49
50 #include "exp.h"
51 #include "conf.h"
52
53
54 static bool Use_Log = true;
55 static CONF_SERVER New_Server;
56 static int New_Server_Idx;
57
58 static size_t Conf_Channel_Count;
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static bool Read_Config PARAMS(( bool ngircd_starting ));
61 static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
62
63 static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
64 static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
65 static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
66 static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
67
68 static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
69
70 static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
71 static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
72
73 static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
74
75 #ifdef WANT_IPV6
76 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
77 #else
78 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
79 #endif
80
81 #ifdef SSL_SUPPORT
82 struct SSLOptions Conf_SSLOptions;
83
84 static void
85 ConfSSL_Init(void)
86 {
87         free(Conf_SSLOptions.KeyFile);
88         Conf_SSLOptions.KeyFile = NULL;
89
90         free(Conf_SSLOptions.CertFile);
91         Conf_SSLOptions.CertFile = NULL;
92
93         free(Conf_SSLOptions.DHFile);
94         Conf_SSLOptions.DHFile = NULL;
95         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
96 }
97
98 static bool
99 ssl_print_configvar(const char *name, const char *file)
100 {
101         FILE *fp;
102
103         if (!file) {
104                 printf("  %s =\n", name);
105                 return true;
106         }
107
108         fp = fopen(file, "r");
109         if (fp)
110                 fclose(fp);
111         else
112                 fprintf(stderr, "ERROR: %s \"%s\": %s\n",
113                         name, file, strerror(errno));
114
115         printf("  %s = %s\n", name, file);
116         return fp != NULL;
117 }
118
119 static bool
120 ConfSSL_Puts(void)
121 {
122         bool ret;
123
124         ret = ssl_print_configvar("SSLKeyFile", Conf_SSLOptions.KeyFile);
125
126         if (!ssl_print_configvar("SSLCertFile", Conf_SSLOptions.CertFile))
127                 ret = false;
128
129         if (!ssl_print_configvar("SSLDHFile", Conf_SSLOptions.DHFile))
130                 ret = false;
131
132         if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
133                 puts("  SSLKeyFilePassword = <secret>");
134
135         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
136
137         return ret;
138 }
139 #endif
140
141 static char *
142 strdup_warn(const char *str)
143 {
144         char *ptr = strdup(str);
145         if (!ptr)
146                 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
147         return ptr;
148 }
149
150
151 static void
152 ports_puts(array *a)
153 {
154         size_t len;
155         UINT16 *ports;
156         len = array_length(a, sizeof(UINT16));
157         if (len--) {
158                 ports = (UINT16*) array_start(a);
159                 printf("%u", (unsigned int) *ports);
160                 while (len--) {
161                         ports++;
162                         printf(", %u", (unsigned int) *ports);
163                 }
164         }
165         putc('\n', stdout);
166 }
167
168
169 static void
170 ports_parse(array *a, int Line, char *Arg)
171 {
172         char *ptr;
173         int port;
174         UINT16 port16;
175
176         array_trunc(a);
177
178         /* Ports on that the server should listen. More port numbers
179          * must be separated by "," */
180         ptr = strtok( Arg, "," );
181         while (ptr) {
182                 ngt_TrimStr(ptr);
183                 port = atoi(ptr);
184                 if (port > 0 && port < 0xFFFF) {
185                         port16 = (UINT16) port;
186                         if (!array_catb(a, (char*)&port16, sizeof port16))
187                                 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
188                                                         NGIRCd_ConfFile, Line, port, strerror(errno));
189                 } else {
190                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
191                                                                         NGIRCd_ConfFile, Line, port );
192                 }
193
194                 ptr = strtok( NULL, "," );
195         }
196 }
197
198
199 GLOBAL void
200 Conf_Init( void )
201 {
202         Read_Config( true );
203         Validate_Config(false, false);
204 } /* Config_Init */
205
206
207 GLOBAL bool
208 Conf_Rehash( void )
209 {
210         if (!Read_Config(false))
211                 return false;
212         Validate_Config(false, true);
213
214         /* Update CLIENT structure of local server */
215         Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
216         return true;
217 } /* Config_Rehash */
218
219
220 static const char*
221 yesno_to_str(int boolean_value)
222 {
223         if (boolean_value)
224                 return "yes";
225         return "no";
226 }
227
228
229 GLOBAL int
230 Conf_Test( void )
231 {
232         /* Read configuration, validate and output it. */
233
234         struct passwd *pwd;
235         struct group *grp;
236         unsigned int i;
237         bool config_valid;
238         size_t predef_channel_count;
239         struct Conf_Channel *predef_chan;
240
241         Use_Log = false;
242
243         if (! Read_Config(true))
244                 return 1;
245
246         config_valid = Validate_Config(true, false);
247
248         /* If stdin and stdout ("you can read our nice message and we can
249          * read in your keypress") are valid tty's, wait for a key: */
250         if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
251                 puts( "OK, press enter to see a dump of your service configuration ..." );
252                 getchar( );
253         } else {
254                 puts( "Ok, dump of your server configuration follows:\n" );
255         }
256
257         puts( "[GLOBAL]" );
258         printf( "  Name = %s\n", Conf_ServerName );
259         printf( "  Info = %s\n", Conf_ServerInfo );
260         printf( "  Password = %s\n", Conf_ServerPwd );
261         printf( "  AdminInfo1 = %s\n", Conf_ServerAdmin1 );
262         printf( "  AdminInfo2 = %s\n", Conf_ServerAdmin2 );
263         printf( "  AdminEMail = %s\n", Conf_ServerAdminMail );
264         printf( "  MotdFile = %s\n", Conf_MotdFile );
265         printf( "  MotdPhrase = %s\n", Conf_MotdPhrase );
266         printf( "  ChrootDir = %s\n", Conf_Chroot );
267         printf( "  PidFile = %s\n", Conf_PidFile);
268         printf("  Listen = %s\n", Conf_ListenAddress);
269         fputs("  Ports = ", stdout);
270         ports_puts(&Conf_ListenPorts);
271 #ifdef SSL_SUPPORT
272         fputs("  SSLPorts = ", stdout);
273         ports_puts(&Conf_SSLOptions.ListenPorts);
274         if (!ConfSSL_Puts())
275                 config_valid = false;
276 #endif
277
278         pwd = getpwuid(Conf_UID);
279         if (pwd)
280                 printf("  ServerUID = %s\n", pwd->pw_name);
281         else
282                 printf("  ServerUID = %ld\n", (long)Conf_UID);
283         grp = getgrgid(Conf_GID);
284         if (grp)
285                 printf("  ServerGID = %s\n", grp->gr_name);
286         else
287                 printf("  ServerGID = %ld\n", (long)Conf_GID);
288         printf("  PingTimeout = %d\n", Conf_PingTimeout);
289         printf("  PongTimeout = %d\n", Conf_PongTimeout);
290         printf("  ConnectRetry = %d\n", Conf_ConnectRetry);
291         printf("  OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
292         printf("  OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
293         printf("  AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
294         printf("  PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
295         printf("  NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
296         printf("  NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
297
298 #ifdef WANT_IPV6
299         printf("  ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
300         printf("  ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
301 #endif
302         printf("  MaxConnections = %ld\n", Conf_MaxConnections);
303         printf("  MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
304         printf("  MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
305         printf("  MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
306
307         for( i = 0; i < Conf_Oper_Count; i++ ) {
308                 if( ! Conf_Oper[i].name[0] ) continue;
309
310                 /* Valid "Operator" section */
311                 puts( "[OPERATOR]" );
312                 printf( "  Name = %s\n", Conf_Oper[i].name );
313                 printf( "  Password = %s\n", Conf_Oper[i].pwd );
314                 if ( Conf_Oper[i].mask ) printf( "  Mask = %s\n", Conf_Oper[i].mask );
315                 puts( "" );
316         }
317
318         for( i = 0; i < MAX_SERVERS; i++ ) {
319                 if( ! Conf_Server[i].name[0] ) continue;
320
321                 /* Valid "Server" section */
322                 puts( "[SERVER]" );
323                 printf( "  Name = %s\n", Conf_Server[i].name );
324                 printf( "  Host = %s\n", Conf_Server[i].host );
325                 printf( "  Port = %u\n", (unsigned int)Conf_Server[i].port );
326 #ifdef SSL_SUPPORT
327                 printf( "  SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
328 #endif
329                 printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
330                 printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
331                 printf( "  ServiceMask = %s\n", Conf_Server[i].svs_mask);
332                 printf( "  Group = %d\n", Conf_Server[i].group );
333                 printf( "  Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
334         }
335
336         predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
337         predef_chan = array_start(&Conf_Channels);
338
339         for (i = 0; i < predef_channel_count; i++, predef_chan++) {
340                 if (!predef_chan->name[0])
341                         continue;
342
343                 /* Valid "Channel" section */
344                 puts( "[CHANNEL]" );
345                 printf("  Name = %s\n", predef_chan->name);
346                 printf("  Modes = %s\n", predef_chan->modes);
347                 printf("  Key = %s\n", predef_chan->key);
348                 printf("  MaxUsers = %lu\n", predef_chan->maxusers);
349                 printf("  Topic = %s\n", predef_chan->topic);
350                 printf("  KeyFile = %s\n\n", predef_chan->keyfile);
351         }
352
353         return (config_valid ? 0 : 1);
354 } /* Conf_Test */
355
356
357 GLOBAL void
358 Conf_UnsetServer( CONN_ID Idx )
359 {
360         /* Set next time for next connection attempt, if this is a server
361          * link that is (still) configured here. If the server is set as
362          * "once", delete it from our configuration.
363          * Non-Server-Connections will be silently ignored. */
364
365         int i;
366         time_t t;
367
368         /* Check all our configured servers */
369         for( i = 0; i < MAX_SERVERS; i++ ) {
370                 if( Conf_Server[i].conn_id != Idx ) continue;
371
372                 /* Gotcha! Mark server configuration as "unused": */
373                 Conf_Server[i].conn_id = NONE;
374
375                 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
376                         /* Delete configuration here */
377                         Init_Server_Struct( &Conf_Server[i] );
378                 } else {
379                         /* Set time for next connect attempt */
380                         t = time(NULL);
381                         if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
382                                 /* The connection has been "long", so we don't
383                                  * require the next attempt to be delayed. */
384                                 Conf_Server[i].lasttry =
385                                         t - Conf_ConnectRetry + RECONNECT_DELAY;
386                         } else
387                                 Conf_Server[i].lasttry = t;
388                 }
389         }
390 } /* Conf_UnsetServer */
391
392
393 GLOBAL void
394 Conf_SetServer( int ConfServer, CONN_ID Idx )
395 {
396         /* Set connection for specified configured server */
397
398         assert( ConfServer > NONE );
399         assert( Idx > NONE );
400
401         Conf_Server[ConfServer].conn_id = Idx;
402 } /* Conf_SetServer */
403
404
405 GLOBAL int
406 Conf_GetServer( CONN_ID Idx )
407 {
408         /* Get index of server in configuration structure */
409
410         int i = 0;
411
412         assert( Idx > NONE );
413
414         for( i = 0; i < MAX_SERVERS; i++ ) {
415                 if( Conf_Server[i].conn_id == Idx ) return i;
416         }
417         return NONE;
418 } /* Conf_GetServer */
419
420
421 GLOBAL bool
422 Conf_EnableServer( const char *Name, UINT16 Port )
423 {
424         /* Enable specified server and adjust port */
425
426         int i;
427
428         assert( Name != NULL );
429
430         for( i = 0; i < MAX_SERVERS; i++ ) {
431                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
432                         /* Gotcha! Set port and enable server: */
433                         Conf_Server[i].port = Port;
434                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
435                         return (Conf_Server[i].port && Conf_Server[i].host[0]);
436                 }
437         }
438         return false;
439 } /* Conf_EnableServer */
440
441
442 GLOBAL bool
443 Conf_EnablePassiveServer(const char *Name)
444 {
445         /* Enable specified server */
446         int i;
447
448         assert( Name != NULL );
449         for (i = 0; i < MAX_SERVERS; i++) {
450                 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
451                         /* BINGO! Enable server */
452                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
453                         return true;
454                 }
455         }
456         return false;
457 } /* Conf_EnablePassiveServer */
458
459
460 GLOBAL bool
461 Conf_DisableServer( const char *Name )
462 {
463         /* Enable specified server and adjust port */
464
465         int i;
466
467         assert( Name != NULL );
468
469         for( i = 0; i < MAX_SERVERS; i++ ) {
470                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
471                         /* Gotcha! Disable and disconnect server: */
472                         Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
473                         if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
474                         return true;
475                 }
476         }
477         return false;
478 } /* Conf_DisableServer */
479
480
481 GLOBAL bool
482 Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
483 {
484         /* Add new server to configuration */
485
486         int i;
487
488         assert( Name != NULL );
489         assert( Host != NULL );
490         assert( MyPwd != NULL );
491         assert( PeerPwd != NULL );
492
493         /* Search unused item in server configuration structure */
494         for( i = 0; i < MAX_SERVERS; i++ ) {
495                 /* Is this item used? */
496                 if( ! Conf_Server[i].name[0] ) break;
497         }
498         if( i >= MAX_SERVERS ) return false;
499
500         Init_Server_Struct( &Conf_Server[i] );
501         strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
502         strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
503         strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
504         strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
505         Conf_Server[i].port = Port;
506         Conf_Server[i].flags = CONF_SFLAG_ONCE;
507
508         return true;
509 } /* Conf_AddServer */
510
511
512 /**
513  * Check if the given nick name is an service
514  */
515 GLOBAL bool
516 Conf_IsService(int ConfServer, const char *Nick)
517 {
518         return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
519 } /* Conf_IsService */
520
521
522 /**
523  * Initialize configuration settings with their default values.
524  */
525 static void
526 Set_Defaults(bool InitServers)
527 {
528         int i;
529
530         strcpy(Conf_ServerName, "");
531         snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
532                  PACKAGE_NAME, PACKAGE_VERSION);
533         strcpy(Conf_ServerPwd, "");
534
535         strcpy(Conf_ServerAdmin1, "");
536         strcpy(Conf_ServerAdmin2, "");
537         strcpy(Conf_ServerAdminMail, "");
538
539         strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
540         strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
541         strlcpy(Conf_MotdPhrase, MOTD_PHRASE, sizeof(Conf_MotdPhrase));
542
543         Conf_UID = Conf_GID = 0;
544         strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
545         strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
546
547         free(Conf_ListenAddress);
548         Conf_ListenAddress = NULL;
549
550         Conf_PingTimeout = 120;
551         Conf_PongTimeout = 20;
552         Conf_ConnectRetry = 60;
553         Conf_NoDNS = false;
554         Conf_NoIdent = false;
555
556         Conf_Oper_Count = 0;
557         Conf_Channel_Count = 0;
558
559         Conf_OperCanMode = false;
560         Conf_OperServerMode = false;
561         Conf_AllowRemoteOper = false;
562         Conf_PredefChannelsOnly = false;
563
564         Conf_ConnectIPv4 = true;
565         Conf_ConnectIPv6 = true;
566
567         Conf_MaxConnections = 0;
568         Conf_MaxConnectionsIP = 5;
569         Conf_MaxJoins = 10;
570         Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
571
572         /* Initialize server configuration structures */
573         if (InitServers) {
574                 for (i = 0; i < MAX_SERVERS;
575                      Init_Server_Struct(&Conf_Server[i++]));
576         }
577 } /* Set_Defaults */
578
579
580 static bool
581 no_listenports(void)
582 {
583         size_t cnt = array_bytes(&Conf_ListenPorts);
584 #ifdef SSL_SUPPORT
585         cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
586 #endif
587         return cnt == 0;
588 }
589
590 static bool
591 Read_Config( bool ngircd_starting )
592 {
593         /* Read configuration file. */
594
595         char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
596         const UINT16 defaultport = 6667;
597         int line, i, n;
598         FILE *fd;
599
600         /* Open configuration file */
601         fd = fopen( NGIRCd_ConfFile, "r" );
602         if( ! fd ) {
603                 /* No configuration file found! */
604                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
605                                         NGIRCd_ConfFile, strerror( errno ));
606                 if (!ngircd_starting)
607                         return false;
608                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
609                 exit( 1 );
610         }
611
612         Set_Defaults( ngircd_starting );
613
614         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
615
616         /* Clean up server configuration structure: mark all already
617          * configured servers as "once" so that they are deleted
618          * after the next disconnect and delete all unused servers.
619          * And delete all servers which are "duplicates" of servers
620          * that are already marked as "once" (such servers have been
621          * created by the last rehash but are now useless). */
622         for( i = 0; i < MAX_SERVERS; i++ ) {
623                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
624                 else {
625                         /* This structure is in use ... */
626                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
627                                 /* Check for duplicates */
628                                 for( n = 0; n < MAX_SERVERS; n++ ) {
629                                         if( n == i ) continue;
630
631                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
632                                                 Init_Server_Struct( &Conf_Server[n] );
633 #ifdef DEBUG
634                                                 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
635                                                                                                 n, i );
636 #endif
637                                         }
638                                 }
639                         } else {
640                                 /* Mark server as "once" */
641                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
642                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
643                         }
644                 }
645         }
646
647         /* Initialize variables */
648         line = 0;
649         strcpy( section, "" );
650         Init_Server_Struct( &New_Server );
651         New_Server_Idx = NONE;
652 #ifdef SSL_SUPPORT
653         ConfSSL_Init();
654 #endif
655         /* Read configuration file */
656         while( true ) {
657                 if( ! fgets( str, LINE_LEN, fd )) break;
658                 ngt_TrimStr( str );
659                 line++;
660
661                 /* Skip comments and empty lines */
662                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
663
664                 /* Is this the beginning of a new section? */
665                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
666                         strlcpy( section, str, sizeof( section ));
667                         if( strcasecmp( section, "[GLOBAL]" ) == 0 )
668                                 continue;
669
670                         if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
671                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
672                                         Config_Error( LOG_ERR, "Too many operators configured.");
673                                 else {
674                                         /* Initialize new operator structure */
675                                         Conf_Oper[Conf_Oper_Count].name[0] = '\0';
676                                         Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
677                                         if (Conf_Oper[Conf_Oper_Count].mask) {
678                                                 free(Conf_Oper[Conf_Oper_Count].mask );
679                                                 Conf_Oper[Conf_Oper_Count].mask = NULL;
680                                         }
681                                         Conf_Oper_Count++;
682                                 }
683                                 continue;
684                         }
685                         if( strcasecmp( section, "[SERVER]" ) == 0 ) {
686                                 /* Check if there is already a server to add */
687                                 if( New_Server.name[0] ) {
688                                         /* Copy data to "real" server structure */
689                                         assert( New_Server_Idx > NONE );
690                                         Conf_Server[New_Server_Idx] = New_Server;
691                                 }
692
693                                 /* Re-init structure for new server */
694                                 Init_Server_Struct( &New_Server );
695
696                                 /* Search unused item in server configuration structure */
697                                 for( i = 0; i < MAX_SERVERS; i++ ) {
698                                         /* Is this item used? */
699                                         if( ! Conf_Server[i].name[0] ) break;
700                                 }
701                                 if( i >= MAX_SERVERS ) {
702                                         /* Oops, no free item found! */
703                                         Config_Error( LOG_ERR, "Too many servers configured." );
704                                         New_Server_Idx = NONE;
705                                 }
706                                 else New_Server_Idx = i;
707                                 continue;
708                         }
709                         if (strcasecmp(section, "[CHANNEL]") == 0) {
710                                 Conf_Channel_Count++;
711                                 continue;
712                         }
713
714                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
715                         section[0] = 0x1;
716                 }
717                 if( section[0] == 0x1 ) continue;
718
719                 /* Split line into variable name and parameters */
720                 ptr = strchr( str, '=' );
721                 if( ! ptr ) {
722                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
723                         continue;
724                 }
725                 *ptr = '\0';
726                 var = str; ngt_TrimStr( var );
727                 arg = ptr + 1; ngt_TrimStr( arg );
728
729                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
730                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
731                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
732                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
733                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
734         }
735
736         /* Close configuration file */
737         fclose( fd );
738
739         /* Check if there is still a server to add */
740         if( New_Server.name[0] ) {
741                 /* Copy data to "real" server structure */
742                 assert( New_Server_Idx > NONE );
743                 Conf_Server[New_Server_Idx] = New_Server;
744         }
745
746         /* not a single listening port? Add default. */
747         if (no_listenports() &&
748                 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
749         {
750                 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
751                                         (unsigned int) defaultport, strerror(errno));
752
753                 exit(1);
754         }
755
756         if (!Conf_ListenAddress)
757                 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
758
759         if (!Conf_ListenAddress) {
760                 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
761                 exit(1);
762         }
763         return true;
764 } /* Read_Config */
765
766
767 static bool
768 Check_ArgIsTrue( const char *Arg )
769 {
770         if( strcasecmp( Arg, "yes" ) == 0 ) return true;
771         if( strcasecmp( Arg, "true" ) == 0 ) return true;
772         if( atoi( Arg ) != 0 ) return true;
773
774         return false;
775 } /* Check_ArgIsTrue */
776
777
778 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
779 {
780         unsigned new;
781
782         new = (unsigned) atoi(Arg) + 1;
783         if (new > CLIENT_NICK_LEN) {
784                 Config_Error(LOG_WARNING,
785                              "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
786                              NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
787                 return CLIENT_NICK_LEN;
788         }
789         if (new < 2) {
790                 Config_Error(LOG_WARNING,
791                              "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
792                              NGIRCd_ConfFile, Line);
793                 return 2;
794         }
795         return new;
796 } /* Handle_MaxNickLength */
797
798
799 static void
800 Handle_GLOBAL( int Line, char *Var, char *Arg )
801 {
802         struct passwd *pwd;
803         struct group *grp;
804         size_t len;
805         
806         assert( Line > 0 );
807         assert( Var != NULL );
808         assert( Arg != NULL );
809         
810         if( strcasecmp( Var, "Name" ) == 0 ) {
811                 /* Server name */
812                 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
813                 if (len >= sizeof( Conf_ServerName ))
814                         Config_Error_TooLong( Line, Var );
815                 return;
816         }
817         if( strcasecmp( Var, "Info" ) == 0 ) {
818                 /* Info text of server */
819                 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
820                 if (len >= sizeof( Conf_ServerInfo ))
821                         Config_Error_TooLong ( Line, Var );
822                 return;
823         }
824         if( strcasecmp( Var, "Password" ) == 0 ) {
825                 /* Global server password */
826                 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
827                 if (len >= sizeof( Conf_ServerPwd ))
828                         Config_Error_TooLong( Line, Var );
829                 return;
830         }
831         if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
832                 /* Administrative info #1 */
833                 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
834                 if (len >= sizeof( Conf_ServerAdmin1 ))
835                         Config_Error_TooLong ( Line, Var );
836                 return;
837         }
838         if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
839                 /* Administrative info #2 */
840                 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
841                 if (len >= sizeof( Conf_ServerAdmin2 ))
842                         Config_Error_TooLong ( Line, Var );
843                 return;
844         }
845         if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
846                 /* Administrative email contact */
847                 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
848                 if (len >= sizeof( Conf_ServerAdminMail ))
849                         Config_Error_TooLong( Line, Var );
850                 return;
851         }
852
853         if( strcasecmp( Var, "Ports" ) == 0 ) {
854                 ports_parse(&Conf_ListenPorts, Line, Arg);
855                 return;
856         }
857         if( strcasecmp( Var, "MotdFile" ) == 0 ) {
858                 /* "Message of the day" (MOTD) file */
859                 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
860                 if (len >= sizeof( Conf_MotdFile ))
861                         Config_Error_TooLong( Line, Var );
862                 return;
863         }
864         if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
865                 /* "Message of the day" phrase (instead of file) */
866                 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
867                 if (len >= sizeof( Conf_MotdPhrase ))
868                         Config_Error_TooLong( Line, Var );
869                 return;
870         }
871         if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
872                 /* directory for chroot() */
873                 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
874                 if (len >= sizeof( Conf_Chroot ))
875                         Config_Error_TooLong( Line, Var );
876                 return;
877         }
878         if ( strcasecmp( Var, "PidFile" ) == 0 ) {
879                 /* name of pidfile */
880                 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
881                 if (len >= sizeof( Conf_PidFile ))
882                         Config_Error_TooLong( Line, Var );
883                 return;
884         }
885         if( strcasecmp( Var, "ServerUID" ) == 0 ) {
886                 /* UID the daemon should switch to */
887                 pwd = getpwnam( Arg );
888                 if( pwd ) Conf_UID = pwd->pw_uid;
889                 else {
890 #ifdef HAVE_ISDIGIT
891                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
892                         else
893 #endif
894                         Conf_UID = (unsigned int)atoi( Arg );
895                 }
896                 return;
897         }
898         if( strcasecmp( Var, "ServerGID" ) == 0 ) {
899                 /* GID the daemon should use */
900                 grp = getgrnam( Arg );
901                 if( grp ) Conf_GID = grp->gr_gid;
902                 else {
903 #ifdef HAVE_ISDIGIT
904                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
905                         else
906 #endif
907                         Conf_GID = (unsigned int)atoi( Arg );
908                 }
909                 return;
910         }
911         if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
912                 /* PING timeout */
913                 Conf_PingTimeout = atoi( Arg );
914                 if( Conf_PingTimeout < 5 ) {
915                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
916                                                                         NGIRCd_ConfFile, Line );
917                         Conf_PingTimeout = 5;
918                 }
919                 return;
920         }
921         if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
922                 /* PONG timeout */
923                 Conf_PongTimeout = atoi( Arg );
924                 if( Conf_PongTimeout < 5 ) {
925                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
926                                                                         NGIRCd_ConfFile, Line );
927                         Conf_PongTimeout = 5;
928                 }
929                 return;
930         }
931         if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
932                 /* Seconds between connection attempts to other servers */
933                 Conf_ConnectRetry = atoi( Arg );
934                 if( Conf_ConnectRetry < 5 ) {
935                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
936                                                                         NGIRCd_ConfFile, Line );
937                         Conf_ConnectRetry = 5;
938                 }
939                 return;
940         }
941         if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
942                 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
943                 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
944                 return;
945         }
946         if( strcasecmp( Var, "NoDNS" ) == 0 ) {
947                 /* don't do reverse dns lookups when clients connect? */
948                 Conf_NoDNS = Check_ArgIsTrue( Arg );
949                 return;
950         }
951         if (strcasecmp(Var, "NoIdent") == 0) {
952                 /* don't do IDENT lookups when clients connect? */
953                 Conf_NoIdent = Check_ArgIsTrue(Arg);
954 #ifndef IDENTAUTH
955                 if (!Conf_NoIdent) {
956                         /* user has enabled ident lookups explicitly, but ... */
957                         Config_Error(LOG_WARNING,
958                                 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
959                                 NGIRCd_ConfFile, Line);
960                 }
961 #endif
962                 return;
963         }
964 #ifdef WANT_IPV6
965         /* the default setting for all the WANT_IPV6 special options is 'true' */
966         if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
967                 /* connect to other hosts using ipv6, if they have an AAAA record? */
968                 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
969                 return;
970         }
971         if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
972                 /* connect to other hosts using ipv4.
973                  * again, this can be used for ipv6-only setups */
974                 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
975                 return;
976         }
977 #endif
978         if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
979                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
980                 Conf_OperCanMode = Check_ArgIsTrue( Arg );
981                 return;
982         }
983         if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
984                 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
985                 Conf_OperServerMode = Check_ArgIsTrue( Arg );
986                 return;
987         }
988         if(strcasecmp(Var, "AllowRemoteOper") == 0) {
989                 /* Are remote IRC operators allowed to control this server? */
990                 Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
991                 return;
992         }
993         if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
994                 /* Maximum number of connections. 0 -> "no limit". */
995 #ifdef HAVE_ISDIGIT
996                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
997                 else
998 #endif
999                 Conf_MaxConnections = atol( Arg );
1000                 return;
1001         }
1002         if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
1003                 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
1004 #ifdef HAVE_ISDIGIT
1005                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1006                 else
1007 #endif
1008                 Conf_MaxConnectionsIP = atoi( Arg );
1009                 return;
1010         }
1011         if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
1012                 /* Maximum number of channels a user can join. 0 -> "no limit". */
1013 #ifdef HAVE_ISDIGIT
1014                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
1015                 else
1016 #endif
1017                 Conf_MaxJoins = atoi( Arg );
1018                 return;
1019         }
1020         if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
1021                 /* Maximum length of a nick name; must be same on all servers
1022                  * within the IRC network! */
1023                 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1024                 return;
1025         }
1026
1027         if( strcasecmp( Var, "Listen" ) == 0 ) {
1028                 /* IP-Address to bind sockets */
1029                 if (Conf_ListenAddress) {
1030                         Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
1031                         return;
1032                 }
1033                 Conf_ListenAddress = strdup_warn(Arg);
1034                 /*
1035                  * if allocation fails, we're in trouble:
1036                  * we cannot ignore the error -- otherwise ngircd
1037                  * would listen on all interfaces.
1038                  */
1039                 if (!Conf_ListenAddress) {
1040                         Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1041                         exit(1);
1042                 }
1043                 return;
1044         }
1045
1046 #ifdef SSL_SUPPORT
1047         if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1048                 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1049                 return;
1050         }
1051
1052         if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1053                 assert(Conf_SSLOptions.KeyFile == NULL );
1054                 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1055                 return;
1056         }
1057         if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1058                 assert(Conf_SSLOptions.CertFile == NULL );
1059                 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1060                 return;
1061         }
1062
1063         if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1064                 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1065                 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1066                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1067                                                                 NGIRCd_ConfFile, Line, Var, strerror(errno));
1068                 return;
1069         }
1070         if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1071                 assert(Conf_SSLOptions.DHFile == NULL);
1072                 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1073                 return;
1074         }
1075 #endif
1076         Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1077                                                                 NGIRCd_ConfFile, Line, Var);
1078 } /* Handle_GLOBAL */
1079
1080
1081 static void
1082 Handle_OPERATOR( int Line, char *Var, char *Arg )
1083 {
1084         unsigned int opercount;
1085         size_t len;
1086         assert( Line > 0 );
1087         assert( Var != NULL );
1088         assert( Arg != NULL );
1089         assert( Conf_Oper_Count > 0 );
1090
1091         if ( Conf_Oper_Count == 0 )
1092                 return;
1093
1094         opercount = Conf_Oper_Count - 1;
1095
1096         if( strcasecmp( Var, "Name" ) == 0 ) {
1097                 /* Name of IRC operator */
1098                 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1099                 if (len >= sizeof( Conf_Oper[opercount].name ))
1100                                 Config_Error_TooLong( Line, Var );
1101                 return;
1102         }
1103         if( strcasecmp( Var, "Password" ) == 0 ) {
1104                 /* Password of IRC operator */
1105                 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1106                 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1107                                 Config_Error_TooLong( Line, Var );
1108                 return;
1109         }
1110         if( strcasecmp( Var, "Mask" ) == 0 ) {
1111                 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1112
1113                 Conf_Oper[opercount].mask = strdup_warn( Arg );
1114                 return;
1115         }
1116         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1117                                                                 NGIRCd_ConfFile, Line, Var );
1118 } /* Handle_OPERATOR */
1119
1120
1121 static void
1122 Handle_SERVER( int Line, char *Var, char *Arg )
1123 {
1124         long port;
1125         size_t len;
1126         
1127         assert( Line > 0 );
1128         assert( Var != NULL );
1129         assert( Arg != NULL );
1130
1131         /* Ignore server block if no space is left in server configuration structure */
1132         if( New_Server_Idx <= NONE ) return;
1133
1134         if( strcasecmp( Var, "Host" ) == 0 ) {
1135                 /* Hostname of the server */
1136                 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1137                 if (len >= sizeof( New_Server.host ))
1138                         Config_Error_TooLong ( Line, Var );
1139                 return;
1140         }
1141         if( strcasecmp( Var, "Name" ) == 0 ) {
1142                 /* Name of the server ("Nick"/"ID") */
1143                 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1144                 if (len >= sizeof( New_Server.name ))
1145                         Config_Error_TooLong( Line, Var );
1146                 return;
1147         }
1148         if (strcasecmp(Var, "Bind") == 0) {
1149                 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1150                         return;
1151
1152                 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1153                                 NGIRCd_ConfFile, Line, Arg);
1154                 return;
1155         }
1156         if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1157                 /* Password of this server which is sent to the peer */
1158                 if (*Arg == ':') {
1159                         Config_Error(LOG_ERR,
1160                                 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1161                                                                                 NGIRCd_ConfFile, Line);
1162                 }
1163                 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1164                 if (len >= sizeof( New_Server.pwd_in ))
1165                         Config_Error_TooLong( Line, Var );
1166                 return;
1167         }
1168         if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1169                 /* Passwort of the peer which must be received */
1170                 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1171                 if (len >= sizeof( New_Server.pwd_out ))
1172                         Config_Error_TooLong( Line, Var );
1173                 return;
1174         }
1175         if( strcasecmp( Var, "Port" ) == 0 ) {
1176                 /* Port to which this server should connect */
1177                 port = atol( Arg );
1178                 if( port > 0 && port < 0xFFFF )
1179                         New_Server.port = (UINT16)port;
1180                 else
1181                         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1182                                                                                 NGIRCd_ConfFile, Line, port );
1183                 return;
1184         }
1185 #ifdef SSL_SUPPORT
1186         if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1187                 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1188                 return;
1189         }
1190 #endif
1191         if( strcasecmp( Var, "Group" ) == 0 ) {
1192                 /* Server group */
1193 #ifdef HAVE_ISDIGIT
1194                 if( ! isdigit( (int)*Arg ))
1195                         Config_Error_NaN( Line, Var );
1196                 else
1197 #endif
1198                 New_Server.group = atoi( Arg );
1199                 return;
1200         }
1201         if( strcasecmp( Var, "Passive" ) == 0 ) {
1202                 if (Check_ArgIsTrue(Arg))
1203                         New_Server.flags |= CONF_SFLAG_DISABLED;
1204                 return;
1205         }
1206         if (strcasecmp(Var, "ServiceMask") == 0) {
1207                 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1208                               sizeof(New_Server.svs_mask));
1209                 if (len >= sizeof(New_Server.svs_mask))
1210                         Config_Error_TooLong(Line, Var);
1211                 return;
1212         }
1213
1214         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1215                                                                 NGIRCd_ConfFile, Line, Var );
1216 } /* Handle_SERVER */
1217
1218
1219 static bool
1220 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1221 {
1222         size_t size = sizeof(new_chan->name);
1223         char *dest = new_chan->name;
1224
1225         if (!Channel_IsValidName(name)) {
1226                 /*
1227                  * maybe user forgot to add a '#'.
1228                  * This is only here for user convenience.
1229                  */
1230                 *dest = '#';
1231                 --size;
1232                 ++dest;
1233         }
1234         return size > strlcpy(dest, name, size);
1235 }
1236
1237
1238 static void
1239 Handle_CHANNEL(int Line, char *Var, char *Arg)
1240 {
1241         size_t len;
1242         size_t chancount;
1243         struct Conf_Channel *chan;
1244
1245         assert( Line > 0 );
1246         assert( Var != NULL );
1247         assert( Arg != NULL );
1248         assert(Conf_Channel_Count > 0);
1249
1250         chancount = Conf_Channel_Count - 1;
1251
1252         chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1253         if (!chan) {
1254                 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1255                 return;
1256         }
1257         if (strcasecmp(Var, "Name") == 0) {
1258                 if (!Handle_Channelname(chan, Arg))
1259                         Config_Error_TooLong(Line, Var);
1260                 return;
1261         }
1262         if (strcasecmp(Var, "Modes") == 0) {
1263                 /* Initial modes */
1264                 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1265                 if (len >= sizeof(chan->modes))
1266                         Config_Error_TooLong( Line, Var );
1267                 return;
1268         }
1269         if( strcasecmp( Var, "Topic" ) == 0 ) {
1270                 /* Initial topic */
1271                 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1272                 if (len >= sizeof(chan->topic))
1273                         Config_Error_TooLong( Line, Var );
1274                 return;
1275         }
1276         if( strcasecmp( Var, "Key" ) == 0 ) {
1277                 /* Initial Channel Key (mode k) */
1278                 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1279                 if (len >= sizeof(chan->key))
1280                         Config_Error_TooLong(Line, Var);
1281                 return;
1282         }
1283         if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1284                 /* maximum user limit, mode l */
1285                 chan->maxusers = (unsigned long) atol(Arg);
1286                 if (chan->maxusers == 0)
1287                         Config_Error_NaN(Line, Var);
1288                 return;
1289         }
1290         if (strcasecmp(Var, "KeyFile") == 0) {
1291                 /* channel keys */
1292                 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1293                 if (len >= sizeof(chan->keyfile))
1294                         Config_Error_TooLong(Line, Var);
1295                 return;
1296         }
1297
1298         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1299                                                                 NGIRCd_ConfFile, Line, Var );
1300 } /* Handle_CHANNEL */
1301
1302
1303 static bool
1304 Validate_Config(bool Configtest, bool Rehash)
1305 {
1306         /* Validate configuration settings. */
1307
1308 #ifdef DEBUG
1309         int i, servers, servers_once;
1310 #endif
1311         bool config_valid = true;
1312         char *ptr;
1313
1314         /* Validate configured server name, see RFC 2812 section 2.3.1 */
1315         ptr = Conf_ServerName;
1316         do {
1317                 if (*ptr >= 'a' && *ptr <= 'z') continue;
1318                 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1319                 if (*ptr >= '0' && *ptr <= '9') continue;
1320                 if (ptr > Conf_ServerName) {
1321                         if (*ptr == '.' || *ptr == '-')
1322                                 continue;
1323                 }
1324                 Conf_ServerName[0] = '\0';
1325                 break;
1326         } while (*(++ptr));
1327
1328         if (!Conf_ServerName[0]) {
1329                 /* No server name configured! */
1330                 config_valid = false;
1331                 Config_Error(LOG_ALERT,
1332                              "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1333                              NGIRCd_ConfFile);
1334                 if (!Configtest && !Rehash) {
1335                         Config_Error(LOG_ALERT,
1336                                      "%s exiting due to fatal errors!",
1337                                      PACKAGE_NAME);
1338                         exit(1);
1339                 }
1340         }
1341
1342         if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1343                 /* No dot in server name! */
1344                 config_valid = false;
1345                 Config_Error(LOG_ALERT,
1346                              "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1347                              NGIRCd_ConfFile);
1348                 if (!Configtest) {
1349                         Config_Error(LOG_ALERT,
1350                                      "%s exiting due to fatal errors!",
1351                                      PACKAGE_NAME);
1352                         exit(1);
1353                 }
1354         }
1355
1356 #ifdef STRICT_RFC
1357         if (!Conf_ServerAdminMail[0]) {
1358                 /* No administrative contact configured! */
1359                 config_valid = false;
1360                 Config_Error(LOG_ALERT,
1361                              "No administrator email address configured in \"%s\" ('AdminEMail')!",
1362                              NGIRCd_ConfFile);
1363                 if (!Configtest) {
1364                         Config_Error(LOG_ALERT,
1365                                      "%s exiting due to fatal errors!",
1366                                      PACKAGE_NAME);
1367                         exit(1);
1368                 }
1369         }
1370 #endif
1371
1372         if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1373             && !Conf_ServerAdminMail[0]) {
1374                 /* No administrative information configured! */
1375                 Config_Error(LOG_WARNING,
1376                              "No administrative information configured but required by RFC!");
1377         }
1378
1379 #ifdef DEBUG
1380         servers = servers_once = 0;
1381         for (i = 0; i < MAX_SERVERS; i++) {
1382                 if (Conf_Server[i].name[0]) {
1383                         servers++;
1384                         if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1385                                 servers_once++;
1386                 }
1387         }
1388         Log(LOG_DEBUG,
1389             "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1390             Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1391 #endif
1392
1393         return config_valid;
1394 } /* Validate_Config */
1395
1396
1397 static void
1398 Config_Error_TooLong ( const int Line, const char *Item )
1399 {
1400         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1401 }
1402
1403
1404 static void
1405 Config_Error_NaN( const int Line, const char *Item )
1406 {
1407         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1408                                                 NGIRCd_ConfFile, Line, Item );
1409 }
1410
1411
1412 #ifdef PROTOTYPES
1413 static void Config_Error( const int Level, const char *Format, ... )
1414 #else
1415 static void Config_Error( Level, Format, va_alist )
1416 const int Level;
1417 const char *Format;
1418 va_dcl
1419 #endif
1420 {
1421         /* Error! Write to console and/or logfile. */
1422
1423         char msg[MAX_LOG_MSG_LEN];
1424         va_list ap;
1425
1426         assert( Format != NULL );
1427
1428 #ifdef PROTOTYPES
1429         va_start( ap, Format );
1430 #else
1431         va_start( ap );
1432 #endif
1433         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1434         va_end( ap );
1435         
1436         /* During "normal operations" the log functions of the daemon should
1437          * be used, but during testing of the configuration file, all messages
1438          * should go directly to the console: */
1439         if (Use_Log) Log( Level, "%s", msg );
1440         else puts( msg );
1441 } /* Config_Error */
1442
1443
1444 static void
1445 Init_Server_Struct( CONF_SERVER *Server )
1446 {
1447         /* Initialize server configuration structur to default values */
1448
1449         assert( Server != NULL );
1450
1451         memset( Server, 0, sizeof (CONF_SERVER) );
1452
1453         Server->group = NONE;
1454         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1455
1456         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1457
1458         Resolve_Init(&Server->res_stat);
1459         Server->conn_id = NONE;
1460         memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1461 } /* Init_Server_Struct */
1462
1463
1464 /* -eof- */