]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
Complain if MyPassword starts with ':'. (Reported by Ben Korvemaker)
[ngircd-alex.git] / src / ngircd / conf.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 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 static char UNUSED id[] = "$Id: conf.c,v 1.85 2005/09/02 13:58:52 fw Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #ifdef PROTOTYPES
23 #       include <stdarg.h>
24 #else
25 #       include <varargs.h>
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <unistd.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #ifdef HAVE_CTYPE_H
38 # include <ctype.h>
39 #endif
40
41 #include "array.h"
42 #include "ngircd.h"
43 #include "conn.h"
44 #include "client.h"
45 #include "defines.h"
46 #include "log.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
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static void Read_Config PARAMS(( void ));
61 static void Validate_Config PARAMS(( bool TestOnly ));
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
76 static void
77 ports_puts(array *a)
78 {
79         unsigned int len;
80         UINT16 *ports;
81         len = array_length(a, sizeof(UINT16));
82         if (len--) {
83                 ports = (UINT16*) array_start(a);
84                 printf("%u", (unsigned int) *ports);
85                 while (len--) {
86                         ports++;
87                         printf(", %u", (unsigned int) *ports);
88                 }
89         }
90         putc('\n', stdout);
91 }
92
93
94 static void
95 ports_parse(array *a, int Line, char *Arg)
96 {
97         char *ptr;
98         int port;
99         UINT16 port16;
100
101         array_trunc(a);
102
103         /* Ports on that the server should listen. More port numbers
104          * must be separated by "," */
105         ptr = strtok( Arg, "," );
106         while (ptr) {
107                 ngt_TrimStr( ptr );
108                 port = atol( ptr );
109                 if (port > 0 && port < 0xFFFF) {
110                         port16 = (UINT16) port;
111                         if (!array_catb(a, (char*)&port16, sizeof port16))
112                                 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
113                                                         NGIRCd_ConfFile, Line, port, strerror(errno));
114                 } else {
115                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
116                                                                         NGIRCd_ConfFile, Line, port );
117                 }
118
119                 ptr = strtok( NULL, "," );
120         }
121 }
122
123
124 GLOBAL void
125 Conf_Init( void )
126 {
127         Set_Defaults( true );
128         Read_Config( );
129         Validate_Config( false );
130 } /* Config_Init */
131
132
133 GLOBAL void
134 Conf_Rehash( void )
135 {
136         Set_Defaults( false );
137         Read_Config( );
138         Validate_Config( false );
139 } /* Config_Rehash */
140
141
142 GLOBAL int
143 Conf_Test( void )
144 {
145         /* Read configuration, validate and output it. */
146
147         struct passwd *pwd;
148         struct group *grp;
149         unsigned int i;
150         char *topic;
151
152         Use_Log = false;
153         Set_Defaults( true );
154
155         Read_Config( );
156         Validate_Config( true );
157
158         /* If stdin and stdout ("you can read our nice message and we can
159          * read in your keypress") are valid tty's, wait for a key: */
160         if( isatty( fileno( stdin )) && isatty( fileno( stdout )))
161         {
162                 puts( "OK, press enter to see a dump of your service configuration ..." );
163                 getchar( );
164         }
165         else puts( "Ok, dump of your server configuration follows:\n" );
166
167         puts( "[GLOBAL]" );
168         printf( "  Name = %s\n", Conf_ServerName );
169         printf( "  Info = %s\n", Conf_ServerInfo );
170         printf( "  Password = %s\n", Conf_ServerPwd );
171         printf( "  AdminInfo1 = %s\n", Conf_ServerAdmin1 );
172         printf( "  AdminInfo2 = %s\n", Conf_ServerAdmin2 );
173         printf( "  AdminEMail = %s\n", Conf_ServerAdminMail );
174         printf( "  MotdFile = %s\n", Conf_MotdFile );
175         printf( "  MotdPhrase = %s\n", Conf_MotdPhrase );
176         printf( "  ChrootDir = %s\n", Conf_Chroot );
177         printf( "  PidFile = %s\n", Conf_PidFile);
178         fputs("  Ports = ", stdout);
179
180         ports_puts(&Conf_ListenPorts);
181
182         printf( "  Listen = %s\n", Conf_ListenAddress );
183         pwd = getpwuid( Conf_UID );
184         if( pwd ) printf( "  ServerUID = %s\n", pwd->pw_name );
185         else printf( "  ServerUID = %ld\n", (long)Conf_UID );
186         grp = getgrgid( Conf_GID );
187         if( grp ) printf( "  ServerGID = %s\n", grp->gr_name );
188         else printf( "  ServerGID = %ld\n", (long)Conf_GID );
189         printf( "  PingTimeout = %d\n", Conf_PingTimeout );
190         printf( "  PongTimeout = %d\n", Conf_PongTimeout );
191         printf( "  ConnectRetry = %d\n", Conf_ConnectRetry );
192         printf( "  OperCanUseMode = %s\n", Conf_OperCanMode == true? "yes" : "no" );
193         printf( "  OperServerMode = %s\n", Conf_OperServerMode == true? "yes" : "no" );
194         if( Conf_MaxConnections > 0 ) printf( "  MaxConnections = %ld\n", Conf_MaxConnections );
195         else printf( "  MaxConnections = -1\n" );
196         if( Conf_MaxConnectionsIP > 0 ) printf( "  MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP );
197         else printf( "  MaxConnectionsIP = -1\n" );
198         if( Conf_MaxJoins > 0 ) printf( "  MaxJoins = %d\n", Conf_MaxJoins );
199         else printf( "  MaxJoins = -1\n" );
200         puts( "" );
201
202         for( i = 0; i < Conf_Oper_Count; i++ )
203         {
204                 if( ! Conf_Oper[i].name[0] ) continue;
205                 
206                 /* Valid "Operator" section */
207                 puts( "[OPERATOR]" );
208                 printf( "  Name = %s\n", Conf_Oper[i].name );
209                 printf( "  Password = %s\n", Conf_Oper[i].pwd );
210                 if ( Conf_Oper[i].mask ) printf( "  Mask = %s\n", Conf_Oper[i].mask );
211                 puts( "" );
212         }
213
214         for( i = 0; i < MAX_SERVERS; i++ )
215         {
216                 if( ! Conf_Server[i].name[0] ) continue;
217                 
218                 /* Valid "Server" section */
219                 puts( "[SERVER]" );
220                 printf( "  Name = %s\n", Conf_Server[i].name );
221                 printf( "  Host = %s\n", Conf_Server[i].host );
222                 printf( "  Port = %d\n", Conf_Server[i].port );
223                 printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
224                 printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
225                 printf( "  Group = %d\n", Conf_Server[i].group );
226                 puts( "" );
227         }
228
229         for( i = 0; i < Conf_Channel_Count; i++ )
230         {
231                 if( ! Conf_Channel[i].name[0] ) continue;
232                 
233                 /* Valid "Channel" section */
234                 puts( "[CHANNEL]" );
235                 printf( "  Name = %s\n", Conf_Channel[i].name );
236                 printf( "  Modes = %s\n", Conf_Channel[i].modes );
237
238                 topic = (char*)array_start(&Conf_Channel[i].topic);
239                 printf( "  Topic = %s\n", topic ? topic : "");
240                 puts( "" );
241         }
242         
243         return 0;
244 } /* Conf_Test */
245
246
247 GLOBAL void
248 Conf_UnsetServer( CONN_ID Idx )
249 {
250         /* Set next time for next connection attempt, if this is a server
251          * link that is (still) configured here. If the server is set as
252          * "once", delete it from our configuration.
253          * Non-Server-Connections will be silently ignored. */
254
255         int i;
256
257         /* Check all our configured servers */
258         for( i = 0; i < MAX_SERVERS; i++ )
259         {
260                 if( Conf_Server[i].conn_id != Idx ) continue;
261
262                 /* Gotcha! Mark server configuration as "unused": */
263                 Conf_Server[i].conn_id = NONE;
264
265                 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
266                 {
267                         /* Delete configuration here */
268                         Init_Server_Struct( &Conf_Server[i] );
269                 }
270                 else
271                 {
272                         /* Set time for next connect attempt */
273                         if( Conf_Server[i].lasttry <  time( NULL ) - Conf_ConnectRetry )
274                         {
275                                 /* Okay, the connection was established "long enough": */
276                                 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
277                         }
278                 }
279         }
280 } /* Conf_UnsetServer */
281
282
283 GLOBAL void
284 Conf_SetServer( int ConfServer, CONN_ID Idx )
285 {
286         /* Set connection for specified configured server */
287
288         assert( ConfServer > NONE );
289         assert( Idx > NONE );
290
291         Conf_Server[ConfServer].conn_id = Idx;
292 } /* Conf_SetServer */
293
294
295 GLOBAL int
296 Conf_GetServer( CONN_ID Idx )
297 {
298         /* Get index of server in configuration structure */
299         
300         int i = 0;
301         
302         assert( Idx > NONE );
303
304         for( i = 0; i < MAX_SERVERS; i++ )
305         {
306                 if( Conf_Server[i].conn_id == Idx ) return i;
307         }
308         return NONE;
309 } /* Conf_GetServer */
310
311
312 GLOBAL bool
313 Conf_EnableServer( char *Name, UINT16 Port )
314 {
315         /* Enable specified server and adjust port */
316
317         int i;
318
319         assert( Name != NULL );
320
321         for( i = 0; i < MAX_SERVERS; i++ )
322         {
323                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
324                 {
325                         /* Gotcha! Set port and enable server: */
326                         Conf_Server[i].port = Port;
327                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
328                         return true;
329                 }
330         }
331         return false;
332 } /* Conf_EnableServer */
333
334
335 GLOBAL bool
336 Conf_DisableServer( char *Name )
337 {
338         /* Enable specified server and adjust port */
339
340         int i;
341
342         assert( Name != NULL );
343
344         for( i = 0; i < MAX_SERVERS; i++ )
345         {
346                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
347                 {
348                         /* Gotcha! Disable and disconnect server: */
349                         Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
350                         if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
351                         return true;
352                 }
353         }
354         return false;
355 } /* Conf_DisableServer */
356
357
358 GLOBAL bool
359 Conf_AddServer( char *Name, UINT16 Port, char *Host, char *MyPwd, char *PeerPwd )
360 {
361         /* Add new server to configuration */
362
363         int i;
364
365         assert( Name != NULL );
366         assert( Host != NULL );
367         assert( MyPwd != NULL );
368         assert( PeerPwd != NULL );
369
370         /* Search unused item in server configuration structure */
371         for( i = 0; i < MAX_SERVERS; i++ )
372         {
373                 /* Is this item used? */
374                 if( ! Conf_Server[i].name[0] ) break;
375         }
376         if( i >= MAX_SERVERS ) return false;
377
378         Init_Server_Struct( &Conf_Server[i] );
379         strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
380         strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
381         strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
382         strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
383         Conf_Server[i].port = Port;
384         Conf_Server[i].flags = CONF_SFLAG_ONCE;
385         
386         return true;
387 } /* Conf_AddServer */
388
389
390 static void
391 Set_Defaults( bool InitServers )
392 {
393         /* Initialize configuration variables with default values. */
394
395         int i;
396
397         strcpy( Conf_ServerName, "" );
398         snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
399         strcpy( Conf_ServerPwd, "" );
400
401         strcpy( Conf_ServerAdmin1, "" );
402         strcpy( Conf_ServerAdmin2, "" );
403         strcpy( Conf_ServerAdminMail, "" );
404
405         strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
406         strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
407
408         strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
409
410         strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
411
412         strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
413
414         strcpy( Conf_ListenAddress, "" );
415
416         Conf_UID = Conf_GID = 0;
417         
418         Conf_PingTimeout = 120;
419         Conf_PongTimeout = 20;
420
421         Conf_ConnectRetry = 60;
422
423         Conf_Oper_Count = 0;
424         Conf_Channel_Count = 0;
425
426         Conf_OperCanMode = false;
427         Conf_OperServerMode = false;
428         
429         Conf_MaxConnections = -1;
430         Conf_MaxConnectionsIP = 5;
431         Conf_MaxJoins = 10;
432
433         /* Initialize server configuration structures */
434         if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
435 } /* Set_Defaults */
436
437
438 static void
439 Read_Config( void )
440 {
441         /* Read configuration file. */
442
443         char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
444         const UINT16 defaultport = 6667;
445         int line, i, n;
446         FILE *fd;
447
448         /* Open configuration file */
449         fd = fopen( NGIRCd_ConfFile, "r" );
450         if( ! fd )
451         {
452                 /* No configuration file found! */
453                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
454                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
455                 exit( 1 );
456         }
457
458         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
459
460         /* Clean up server configuration structure: mark all already
461          * configured servers as "once" so that they are deleted
462          * after the next disconnect and delete all unused servers.
463          * And delete all servers which are "duplicates" of servers
464          * that are already marked as "once" (such servers have been
465          * created by the last rehash but are now useless). */
466         for( i = 0; i < MAX_SERVERS; i++ )
467         {
468                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
469                 else
470                 {
471                         /* This structure is in use ... */
472                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
473                         {
474                                 /* Check for duplicates */
475                                 for( n = 0; n < MAX_SERVERS; n++ )
476                                 {
477                                         if( n == i ) continue;
478
479                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id )
480                                         {
481                                                 Init_Server_Struct( &Conf_Server[n] );
482                                                 Log( LOG_DEBUG, "Deleted unused duplicate server %d (kept %d).", n, i );
483                                         }
484                                 }
485                         }
486                         else
487                         {
488                                 /* Mark server as "once" */
489                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
490                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
491                         }
492                 }
493         }
494
495         /* Initialize variables */
496         line = 0;
497         strcpy( section, "" );
498         Init_Server_Struct( &New_Server );
499         New_Server_Idx = NONE;
500
501         /* Read configuration file */
502         while( true )
503         {
504                 if( ! fgets( str, LINE_LEN, fd )) break;
505                 ngt_TrimStr( str );
506                 line++;
507
508                 /* Skip comments and empty lines */
509                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
510
511                 /* Is this the beginning of a new section? */
512                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
513                 {
514                         strlcpy( section, str, sizeof( section ));
515                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
516                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
517                         {
518                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
519                                 else
520                                 {
521                                         /* Initialize new operator structure */
522                                         Conf_Oper[Conf_Oper_Count].name[0] = '\0';
523                                         Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
524                                         if (Conf_Oper[Conf_Oper_Count].mask) {
525                                                 free(Conf_Oper[Conf_Oper_Count].mask );
526                                                 Conf_Oper[Conf_Oper_Count].mask = NULL;
527                                         }
528                                         Conf_Oper_Count++;
529                                 }
530                                 continue;
531                         }
532                         if( strcasecmp( section, "[SERVER]" ) == 0 )
533                         {
534                                 /* Check if there is already a server to add */
535                                 if( New_Server.name[0] )
536                                 {
537                                         /* Copy data to "real" server structure */
538                                         assert( New_Server_Idx > NONE );
539                                         Conf_Server[New_Server_Idx] = New_Server;
540                                 }
541
542                                 /* Re-init structure for new server */
543                                 Init_Server_Struct( &New_Server );
544
545                                 /* Search unused item in server configuration structure */
546                                 for( i = 0; i < MAX_SERVERS; i++ )
547                                 {
548                                         /* Is this item used? */
549                                         if( ! Conf_Server[i].name[0] ) break;
550                                 }
551                                 if( i >= MAX_SERVERS )
552                                 {
553                                         /* Oops, no free item found! */
554                                         Config_Error( LOG_ERR, "Too many servers configured." );
555                                         New_Server_Idx = NONE;
556                                 }
557                                 else New_Server_Idx = i;
558                                 continue;
559                         }
560                         if( strcasecmp( section, "[CHANNEL]" ) == 0 ) {
561                                 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) {
562                                         Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
563                                 } else {
564                                         /* Initialize new channel structure */
565                                         strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
566                                         strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
567                                         array_free(&Conf_Channel[Conf_Channel_Count].topic);
568                                         Conf_Channel_Count++;
569                                 }
570                                 continue;
571                         }
572                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
573                         section[0] = 0x1;
574                 }
575                 if( section[0] == 0x1 ) continue;
576
577                 /* Split line into variable name and parameters */
578                 ptr = strchr( str, '=' );
579                 if( ! ptr )
580                 {
581                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
582                         continue;
583                 }
584                 *ptr = '\0';
585                 var = str; ngt_TrimStr( var );
586                 arg = ptr + 1; ngt_TrimStr( arg );
587
588                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
589                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
590                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
591                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
592                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
593         }
594
595         /* Close configuration file */
596         fclose( fd );
597
598         /* Check if there is still a server to add */
599         if( New_Server.name[0] )
600         {
601                 /* Copy data to "real" server structure */
602                 assert( New_Server_Idx > NONE );
603                 Conf_Server[New_Server_Idx] = New_Server;
604         }
605
606         if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) {
607                 if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) {
608                         Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s",
609                                                         (unsigned int) defaultport, strerror(errno));
610                         exit( 1 );
611                 }
612         }
613 } /* Read_Config */
614
615
616 static bool
617 Check_ArgIsTrue( const char *Arg )
618 {
619         if( strcasecmp( Arg, "yes" ) == 0 ) return true;
620         if( strcasecmp( Arg, "true" ) == 0 ) return true;
621         if( atoi( Arg ) != 0 ) return true;
622
623         return false;
624 } /* Check_ArgIsTrue */
625
626
627 static void
628 Handle_GLOBAL( int Line, char *Var, char *Arg )
629 {
630         struct passwd *pwd;
631         struct group *grp;
632         
633         assert( Line > 0 );
634         assert( Var != NULL );
635         assert( Arg != NULL );
636         
637         if( strcasecmp( Var, "Name" ) == 0 )
638         {
639                 /* Server name */
640                 if( strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName )) >= sizeof( Conf_ServerName ))
641                         Config_Error_TooLong( Line, Var );
642
643                 return;
644         }
645         if( strcasecmp( Var, "Info" ) == 0 )
646         {
647                 /* Info text of server */
648                 if( strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo )) >= sizeof( Conf_ServerInfo ))
649                         Config_Error_TooLong ( Line, Var );
650
651                 return;
652         }
653         if( strcasecmp( Var, "Password" ) == 0 )
654         {
655                 /* Global server password */
656                 if( strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd )) >= sizeof( Conf_ServerPwd ))
657                         Config_Error_TooLong( Line, Var );
658
659                 return;
660         }
661         if( strcasecmp( Var, "AdminInfo1" ) == 0 )
662         {
663                 /* Administrative info #1 */
664                 if( strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 )) >= sizeof( Conf_ServerAdmin1 )) Config_Error_TooLong ( Line, Var );
665                 return;
666         }
667         if( strcasecmp( Var, "AdminInfo2" ) == 0 )
668         {
669                 /* Administrative info #2 */
670                 if( strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 )) >= sizeof( Conf_ServerAdmin2 )) Config_Error_TooLong ( Line, Var );
671                 return;
672         }
673         if( strcasecmp( Var, "AdminEMail" ) == 0 )
674         {
675                 /* Administrative email contact */
676                 if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error_TooLong( Line, Var );
677                 return;
678         }
679
680         if( strcasecmp( Var, "Ports" ) == 0 ) {
681                 ports_parse(&Conf_ListenPorts, Line, Arg);
682                 return;
683         }
684         if( strcasecmp( Var, "MotdFile" ) == 0 )
685         {
686                 /* "Message of the day" (MOTD) file */
687                 if( strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile )) >= sizeof( Conf_MotdFile ))
688                         Config_Error_TooLong( Line, Var );
689
690                 return;
691         }
692         if( strcasecmp( Var, "MotdPhrase" ) == 0 )
693         {
694                 /* "Message of the day" phrase (instead of file) */
695                 if( strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase )) >= sizeof( Conf_MotdPhrase ))
696                         Config_Error_TooLong( Line, Var );
697
698                 return;
699         }
700         if( strcasecmp( Var, "ChrootDir" ) == 0 )
701         {
702                 /* directory for chroot() */
703                 if( strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot )) >= sizeof( Conf_Chroot ))
704                         Config_Error_TooLong( Line, Var );
705
706                 return;
707         }
708
709         if ( strcasecmp( Var, "PidFile" ) == 0 )
710         {
711                 /* name of pidfile */
712                 if( strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile )) >= sizeof( Conf_PidFile ))
713                         Config_Error_TooLong( Line, Var );
714
715                 return;
716         }
717
718         if( strcasecmp( Var, "ServerUID" ) == 0 )
719         {
720                 /* UID the daemon should switch to */
721                 pwd = getpwnam( Arg );
722                 if( pwd ) Conf_UID = pwd->pw_uid;
723                 else
724                 {
725 #ifdef HAVE_ISDIGIT
726                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
727                         else
728 #endif
729                         Conf_UID = (unsigned int)atoi( Arg );
730                 }
731                 return;
732         }
733         if( strcasecmp( Var, "ServerGID" ) == 0 )
734         {
735                 /* GID the daemon should use */
736                 grp = getgrnam( Arg );
737                 if( grp ) Conf_GID = grp->gr_gid;
738                 else
739                 {
740 #ifdef HAVE_ISDIGIT
741                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
742                         else
743 #endif
744                         Conf_GID = (unsigned int)atoi( Arg );
745                 }
746                 return;
747         }
748         if( strcasecmp( Var, "PingTimeout" ) == 0 )
749         {
750                 /* PING timeout */
751                 Conf_PingTimeout = atoi( Arg );
752                 if( Conf_PingTimeout < 5 )
753                 {
754                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
755                         Conf_PingTimeout = 5;
756                 }
757                 return;
758         }
759         if( strcasecmp( Var, "PongTimeout" ) == 0 )
760         {
761                 /* PONG timeout */
762                 Conf_PongTimeout = atoi( Arg );
763                 if( Conf_PongTimeout < 5 )
764                 {
765                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
766                         Conf_PongTimeout = 5;
767                 }
768                 return;
769         }
770         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
771         {
772                 /* Seconds between connection attempts to other servers */
773                 Conf_ConnectRetry = atoi( Arg );
774                 if( Conf_ConnectRetry < 5 )
775                 {
776                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
777                         Conf_ConnectRetry = 5;
778                 }
779                 return;
780         }
781         if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
782         {
783                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
784                 Conf_OperCanMode = Check_ArgIsTrue( Arg );
785                 return;
786         }
787         if( strcasecmp( Var, "OperServerMode" ) == 0 )
788         {
789                 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
790                 Conf_OperServerMode = Check_ArgIsTrue( Arg );
791                 return;
792         }
793         if( strcasecmp( Var, "MaxConnections" ) == 0 )
794         {
795                 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
796 #ifdef HAVE_ISDIGIT
797                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
798                 else
799 #endif
800                 Conf_MaxConnections = atol( Arg );
801                 return;
802         }
803         if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 )
804         {
805                 /* Maximum number of simoultanous connections from one IP. Values <= 0 are equal to "no limit". */
806 #ifdef HAVE_ISDIGIT
807                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
808                 else
809 #endif
810                 Conf_MaxConnectionsIP = atoi( Arg );
811                 return;
812         }
813         if( strcasecmp( Var, "MaxJoins" ) == 0 )
814         {
815                 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
816 #ifdef HAVE_ISDIGIT
817                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
818                 else
819 #endif
820                 Conf_MaxJoins = atoi( Arg );
821                 return;
822         }
823         if( strcasecmp( Var, "Listen" ) == 0 )
824         {
825                 /* IP-Address to bind sockets */
826                 if( strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress )) >= sizeof( Conf_ListenAddress ))
827                 {
828                         Config_Error_TooLong( Line, Var );
829                 }
830                 return;
831         }
832
833         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
834 } /* Handle_GLOBAL */
835
836
837 static void
838 Handle_OPERATOR( int Line, char *Var, char *Arg )
839 {
840         unsigned int opercount;
841         size_t len;
842         assert( Line > 0 );
843         assert( Var != NULL );
844         assert( Arg != NULL );
845         assert( Conf_Oper_Count > 0 );
846
847         if ( Conf_Oper_Count == 0 )
848                 return;
849
850         opercount = Conf_Oper_Count - 1;
851
852         if( strcasecmp( Var, "Name" ) == 0 ) {
853                 /* Name of IRC operator */
854                 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
855                 if (len >= sizeof( Conf_Oper[opercount].name ))
856                                 Config_Error_TooLong( Line, Var );
857
858                 return;
859         }
860         if( strcasecmp( Var, "Password" ) == 0 ) {
861                 /* Password of IRC operator */
862                 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
863                 if (len >= sizeof( Conf_Oper[opercount].pwd ))
864                                 Config_Error_TooLong( Line, Var );
865                 return;
866         }
867         if( strcasecmp( Var, "Mask" ) == 0 ) {
868                 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
869
870                 Conf_Oper[opercount].mask = strdup( Arg );
871                 if (! Conf_Oper[opercount].mask) {
872                         Config_Error( LOG_ERR, "%s, line %d: Cannot allocate memory for operator mask: %s",
873                                                                 NGIRCd_ConfFile, Line, strerror(errno) );
874                         return;
875                 }
876
877                 return;
878         }
879         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
880                                                                 NGIRCd_ConfFile, Line, Var );
881 } /* Handle_OPERATOR */
882
883
884 static void
885 Handle_SERVER( int Line, char *Var, char *Arg )
886 {
887         long port;
888         size_t len;
889         
890         assert( Line > 0 );
891         assert( Var != NULL );
892         assert( Arg != NULL );
893
894         /* Ignore server block if no space is left in server configuration structure */
895         if( New_Server_Idx <= NONE ) return;
896
897         if( strcasecmp( Var, "Host" ) == 0 ) {
898                 /* Hostname of the server */
899                 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
900                 if (len >= sizeof( New_Server.host ))
901                         Config_Error_TooLong ( Line, Var );
902                 return;
903         }
904         if( strcasecmp( Var, "Name" ) == 0 ) {
905                 /* Name of the server ("Nick"/"ID") */
906                 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
907                 if (len >= sizeof( New_Server.name ))
908                         Config_Error_TooLong( Line, Var );
909                 return;
910         }
911         if( strcasecmp( Var, "MyPassword" ) == 0 ) {
912                 /* Password of this server which is sent to the peer */
913                 if (*Arg == ':') {
914                         Config_Error(LOG_ERR,
915                                 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
916                                                                                 NGIRCd_ConfFile, Line);
917                 }
918                 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
919                 if (len >= sizeof( New_Server.pwd_in ))
920                         Config_Error_TooLong( Line, Var );
921                 return;
922         }
923         if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
924                 /* Passwort of the peer which must be received */
925                 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
926                 if (len >= sizeof( New_Server.pwd_out ))
927                         Config_Error_TooLong( Line, Var );
928                 return;
929         }
930         if( strcasecmp( Var, "Port" ) == 0 ) {
931                 /* Port to which this server should connect */
932                 port = atol( Arg );
933                 if( port > 0 && port < 0xFFFF )
934                         New_Server.port = (UINT16)port;
935                 else
936                         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
937                                                                                 NGIRCd_ConfFile, Line, port );
938                 return;
939         }
940         if( strcasecmp( Var, "Group" ) == 0 ) {
941                 /* Server group */
942 #ifdef HAVE_ISDIGIT
943                 if( ! isdigit( (int)*Arg ))
944                         Config_Error_NaN( Line, Var );
945                 else
946 #endif
947                 New_Server.group = atoi( Arg );
948                 return;
949         }
950         
951         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
952                                                                 NGIRCd_ConfFile, Line, Var );
953 } /* Handle_SERVER */
954
955
956 static void
957 Handle_CHANNEL( int Line, char *Var, char *Arg )
958 {
959         assert( Line > 0 );
960         assert( Var != NULL );
961         assert( Arg != NULL );
962
963         if( strcasecmp( Var, "Name" ) == 0 )
964         {
965                 /* Name of the channel */
966                 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].name, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].name )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].name ))
967                         Config_Error_TooLong( Line, Var );
968                 return;
969         }
970         if( strcasecmp( Var, "Modes" ) == 0 )
971         {
972                 /* Initial modes */
973                 if( strlcpy( Conf_Channel[Conf_Channel_Count - 1].modes, Arg, sizeof( Conf_Channel[Conf_Channel_Count - 1].modes )) >= sizeof( Conf_Channel[Conf_Channel_Count - 1].modes ))
974                         Config_Error_TooLong( Line, Var );
975                 return;
976         }
977         if( strcasecmp( Var, "Topic" ) == 0 )
978         {
979                 /* Initial topic */
980                 if (!array_copys( &Conf_Channel[Conf_Channel_Count - 1].topic, Arg))
981                         Config_Error_TooLong( Line, Var );
982  
983                 return;
984         }
985
986         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
987 } /* Handle_CHANNEL */
988
989
990 static void
991 Validate_Config( bool Configtest )
992 {
993         /* Validate configuration settings. */
994
995 #ifdef DEBUG
996         int i, servers, servers_once;
997 #endif
998
999         if( ! Conf_ServerName[0] )
1000         {
1001                 /* No server name configured! */
1002                 Config_Error( LOG_ALERT, "No server name configured in \"%s\" (section 'Global': 'Name')!", NGIRCd_ConfFile );
1003                 if( ! Configtest )
1004                 {
1005                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
1006                         exit( 1 );
1007                 }
1008         }
1009         
1010         if( Conf_ServerName[0] && ! strchr( Conf_ServerName, '.' ))
1011         {
1012                 /* No dot in server name! */
1013                 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!", NGIRCd_ConfFile );
1014                 if( ! Configtest )
1015                 {
1016                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
1017                         exit( 1 );
1018                 }
1019         }
1020
1021 #ifdef STRICT_RFC
1022         if( ! Conf_ServerAdminMail[0] )
1023         {
1024                 /* No administrative contact configured! */
1025                 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
1026                 if( ! Configtest )
1027                 {
1028                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
1029                         exit( 1 );
1030                 }
1031         }
1032 #endif
1033
1034         if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
1035         {
1036                 /* No administrative information configured! */
1037                 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
1038         }
1039 #ifdef DEBUG
1040         servers = servers_once = 0;
1041         for( i = 0; i < MAX_SERVERS; i++ )
1042         {
1043                 if( Conf_Server[i].name[0] )
1044                 {
1045                         servers++;
1046                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
1047                 }
1048         }
1049         Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
1050 #endif
1051 } /* Validate_Config */
1052
1053
1054 static void
1055 Config_Error_TooLong ( const int Line, const char *Item )
1056 {
1057         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1058 }
1059
1060 static void
1061 Config_Error_NaN( const int Line, const char *Item )
1062 {
1063         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!", NGIRCd_ConfFile, Line, Item );
1064 }
1065
1066 #ifdef PROTOTYPES
1067 static void Config_Error( const int Level, const char *Format, ... )
1068 #else
1069 static void Config_Error( Level, Format, va_alist )
1070 const int Level;
1071 const char *Format;
1072 va_dcl
1073 #endif
1074 {
1075         /* Error! Write to console and/or logfile. */
1076
1077         char msg[MAX_LOG_MSG_LEN];
1078         va_list ap;
1079
1080         assert( Format != NULL );
1081
1082 #ifdef PROTOTYPES
1083         va_start( ap, Format );
1084 #else
1085         va_start( ap );
1086 #endif
1087         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1088         va_end( ap );
1089         
1090         /* During "normal operations" the log functions of the daemon should
1091          * be used, but during testing of the configuration file, all messages
1092          * should go directly to the console: */
1093         if( Use_Log ) Log( Level, "%s", msg );
1094         else puts( msg );
1095 } /* Config_Error */
1096
1097
1098 static void
1099 Init_Server_Struct( CONF_SERVER *Server )
1100 {
1101         /* Initialize server configuration structur to default values */
1102
1103         assert( Server != NULL );
1104
1105         memset( Server, 0, sizeof (CONF_SERVER) );
1106
1107         Server->group = NONE;
1108         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1109
1110         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1111
1112         Server->conn_id = NONE;
1113 } /* Init_Server_Struct */
1114
1115
1116 /* -eof- */