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