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