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