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