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