]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
011dffec691d3f3ce287305f7d4a1bd157c09a47
[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.53 2002/12/31 16:12:50 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                 break;
214         }
215 } /* Conf_UnsetServer */
216
217
218 GLOBAL VOID
219 Conf_SetServer( INT ConfServer, CONN_ID Idx )
220 {
221         /* Set connection for specified configured server */
222
223         assert( ConfServer > NONE );
224         assert( Idx > NONE );
225
226         Conf_Server[ConfServer].conn_id = Idx;
227 } /* Conf_SetServer */
228
229
230 GLOBAL INT
231 Conf_GetServer( CONN_ID Idx )
232 {
233         /* Get index of server in configuration structure */
234
235         INT i;
236
237         assert( Idx > NONE );
238
239         for( i = 0; i < MAX_SERVERS; i++ )
240         {
241                 if( Conf_Server[i].conn_id == Idx ) return i;
242         }
243         return NONE;
244 } /* Conf_GetServer */
245
246
247 GLOBAL BOOLEAN
248 Conf_EnableServer( CHAR *Name, INT Port )
249 {
250         /* Enable specified server and adjust port */
251
252         INT i;
253
254         assert( Name != NULL );
255
256         for( i = 0; i < MAX_SERVERS; i++ )
257         {
258                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
259                 {
260                         /* Gotcha! Set port and enable server: */
261                         Conf_Server[i].port = Port;
262                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
263                         return TRUE;
264                 }
265         }
266         return FALSE;
267 } /* Conf_EnableServer */
268
269
270 GLOBAL BOOLEAN
271 Conf_DisableServer( CHAR *Name )
272 {
273         /* Enable specified server and adjust port */
274
275         INT i;
276
277         assert( Name != NULL );
278
279         for( i = 0; i < MAX_SERVERS; i++ )
280         {
281                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 )
282                 {
283                         /* Gotcha! Disable and disconnect server: */
284                         Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
285                         if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", TRUE );
286                         return TRUE;
287                 }
288         }
289         return FALSE;
290 } /* Conf_DisableServer */
291
292
293 GLOBAL BOOLEAN
294 Conf_AddServer( CHAR *Name, INT Port, CHAR *Host, CHAR *MyPwd, CHAR *PeerPwd )
295 {
296         /* Add new server to configuration */
297
298         INT i;
299
300         assert( Name != NULL );
301         assert( Host != NULL );
302         assert( MyPwd != NULL );
303         assert( PeerPwd != NULL );
304
305         /* Search unused item in server configuration structure */
306         for( i = 0; i < MAX_SERVERS; i++ )
307         {
308                 /* Is this item used? */
309                 if( ! Conf_Server[i].name[0] ) break;
310         }
311         if( i >= MAX_SERVERS ) return FALSE;
312
313         Init_Server_Struct( &Conf_Server[i] );
314         strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
315         strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
316         strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
317         strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
318         Conf_Server[i].port = Port;
319         Conf_Server[i].flags = CONF_SFLAG_ONCE;
320         
321         return TRUE;
322 } /* Conf_AddServer */
323
324
325 LOCAL VOID
326 Set_Defaults( BOOLEAN InitServers )
327 {
328         /* Initialize configuration variables with default values. */
329
330         INT i;
331
332         strcpy( Conf_ServerName, "" );
333         sprintf( Conf_ServerInfo, "%s %s", PACKAGE, VERSION );
334         strcpy( Conf_ServerPwd, "" );
335
336         strcpy( Conf_ServerAdmin1, "" );
337         strcpy( Conf_ServerAdmin2, "" );
338         strcpy( Conf_ServerAdminMail, "" );
339
340         strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
341         strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
342
343         Conf_ListenPorts_Count = 0;
344
345         Conf_UID = Conf_GID = 0;
346         
347         Conf_PingTimeout = 120;
348         Conf_PongTimeout = 20;
349
350         Conf_ConnectRetry = 60;
351
352         Conf_Oper_Count = 0;
353         Conf_Channel_Count = 0;
354
355         Conf_OperCanMode = FALSE;
356         
357         Conf_MaxConnections = -1;
358         Conf_MaxJoins = 10;
359
360         /* Initialize server configuration structures */
361         if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
362 } /* Set_Defaults */
363
364
365 LOCAL VOID
366 Read_Config( VOID )
367 {
368         /* Read configuration file. */
369
370         CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
371         INT line, i;
372         FILE *fd;
373
374         /* Open configuration file */
375         fd = fopen( NGIRCd_ConfFile, "r" );
376         if( ! fd )
377         {
378                 /* No configuration file found! */
379                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
380                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
381                 exit( 1 );
382         }
383
384         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
385
386         /* Clean up server configuration structure: mark all already
387          * configured servers as "once" so that they are deleted
388          * after the next disconnect and delete all unused servers. */
389         for( i = 0; i < MAX_SERVERS; i++ )
390         {
391                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
392                 else Conf_Server[i].flags |= CONF_SFLAG_ONCE;
393         }
394
395         /* Initialize variables */
396         line = 0;
397         strcpy( section, "" );
398         Init_Server_Struct( &New_Server );
399         New_Server_Idx = NONE;
400
401         /* Read configuration file */
402         while( TRUE )
403         {
404                 if( ! fgets( str, LINE_LEN, fd )) break;
405                 ngt_TrimStr( str );
406                 line++;
407
408                 /* Skip comments and empty lines */
409                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
410
411                 /* Is this the beginning of a new section? */
412                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
413                 {
414                         strlcpy( section, str, sizeof( section ));
415                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
416                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
417                         {
418                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
419                                 else
420                                 {
421                                         /* Initialize new operator structure */
422                                         strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
423                                         strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
424                                         Conf_Oper_Count++;
425                                 }
426                                 continue;
427                         }
428                         if( strcasecmp( section, "[SERVER]" ) == 0 )
429                         {
430                                 /* Check if there is already a server to add */
431                                 if( New_Server.name[0] )
432                                 {
433                                         /* Copy data to "real" server structure */
434                                         assert( New_Server_Idx > NONE );
435                                         Conf_Server[New_Server_Idx] = New_Server;
436                                 }
437
438                                 /* Re-init structure for new server */
439                                 Init_Server_Struct( &New_Server );
440
441                                 /* Search unused item in server configuration structure */
442                                 for( i = 0; i < MAX_SERVERS; i++ )
443                                 {
444                                         /* Is this item used? */
445                                         if( ! Conf_Server[i].name[0] ) break;
446                                 }
447                                 if( i >= MAX_SERVERS )
448                                 {
449                                         /* Oops, no free item found! */
450                                         Config_Error( LOG_ERR, "Too many servers configured." );
451                                         New_Server_Idx = NONE;
452                                 }
453                                 else New_Server_Idx = i;
454                                 continue;
455                         }
456                         if( strcasecmp( section, "[CHANNEL]" ) == 0 )
457                         {
458                                 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
459                                 else
460                                 {
461                                         /* Initialize new channel structure */
462                                         strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
463                                         strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
464                                         strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
465                                         Conf_Channel_Count++;
466                                 }
467                                 continue;
468                         }
469                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
470                         section[0] = 0x1;
471                 }
472                 if( section[0] == 0x1 ) continue;
473
474                 /* Split line into variable name and parameters */
475                 ptr = strchr( str, '=' );
476                 if( ! ptr )
477                 {
478                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
479                         continue;
480                 }
481                 *ptr = '\0';
482                 var = str; ngt_TrimStr( var );
483                 arg = ptr + 1; ngt_TrimStr( arg );
484
485                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
486                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
487                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
488                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
489                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
490         }
491
492         /* Close configuration file */
493         fclose( fd );
494
495         /* Check if there is still a server to add */
496         if( New_Server.name[0] )
497         {
498                 /* Copy data to "real" server structure */
499                 assert( New_Server_Idx > NONE );
500                 Conf_Server[New_Server_Idx] = New_Server;
501         }
502         
503         /* If there are no ports configured use the default: 6667 */
504         if( Conf_ListenPorts_Count < 1 )
505         {
506                 Conf_ListenPorts_Count = 1;
507                 Conf_ListenPorts[0] = 6667;
508         }
509 } /* Read_Config */
510
511
512 LOCAL VOID
513 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
514 {
515         struct passwd *pwd;
516         struct group *grp;
517         CHAR *ptr;
518         LONG port;
519         
520         assert( Line > 0 );
521         assert( Var != NULL );
522         assert( Arg != NULL );
523         
524         if( strcasecmp( Var, "Name" ) == 0 )
525         {
526                 /* Server name */
527                 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 );
528                 return;
529         }
530         if( strcasecmp( Var, "Info" ) == 0 )
531         {
532                 /* Info text of server */
533                 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 );
534                 return;
535         }
536         if( strcasecmp( Var, "Password" ) == 0 )
537         {
538                 /* Global server password */
539                 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 );
540                 return;
541         }
542         if( strcasecmp( Var, "AdminInfo1" ) == 0 )
543         {
544                 /* Administrative info #1 */
545                 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 );
546                 return;
547         }
548         if( strcasecmp( Var, "AdminInfo2" ) == 0 )
549         {
550                 /* Administrative info #2 */
551                 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 );
552                 return;
553         }
554         if( strcasecmp( Var, "AdminEMail" ) == 0 )
555         {
556                 /* Administrative email contact */
557                 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 );
558                 return;
559         }
560         if( strcasecmp( Var, "Ports" ) == 0 )
561         {
562                 /* Ports on that the server should listen. More port numbers
563                  * must be separated by "," */
564                 ptr = strtok( Arg, "," );
565                 while( ptr )
566                 {
567                         ngt_TrimStr( ptr );
568                         port = atol( ptr );
569                         if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
570                         else
571                         {
572                                 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
573                                 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
574                         }
575                         ptr = strtok( NULL, "," );
576                 }
577                 return;
578         }
579         if( strcasecmp( Var, "MotdFile" ) == 0 )
580         {
581                 /* "Message of the day" (MOTD) file */
582                 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 );
583                 return;
584         }
585         if( strcasecmp( Var, "ServerUID" ) == 0 )
586         {
587                 /* UID the daemon should switch to */
588                 pwd = getpwnam( Arg );
589                 if( pwd ) Conf_UID = pwd->pw_uid;
590                 else
591                 {
592 #ifdef HAVE_ISDIGIT
593                         if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerUID\" is not a number!", NGIRCd_ConfFile, Line );
594                         else
595 #endif
596                         Conf_UID = (UINT)atoi( Arg );
597                 }
598                 return;
599         }
600         if( strcasecmp( Var, "ServerGID" ) == 0 )
601         {
602                 /* GID the daemon should use */
603                 grp = getgrnam( Arg );
604                 if( grp ) Conf_GID = grp->gr_gid;
605                 else
606                 {
607 #ifdef HAVE_ISDIGIT
608                         if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerGID\" is not a number!", NGIRCd_ConfFile, Line );
609                         else
610 #endif
611                         Conf_GID = (UINT)atoi( Arg );
612                 }
613                 return;
614         }
615         if( strcasecmp( Var, "PingTimeout" ) == 0 )
616         {
617                 /* PING timeout */
618                 Conf_PingTimeout = atoi( Arg );
619                 if( Conf_PingTimeout < 5 )
620                 {
621                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
622                         Conf_PingTimeout = 5;
623                 }
624                 return;
625         }
626         if( strcasecmp( Var, "PongTimeout" ) == 0 )
627         {
628                 /* PONG timeout */
629                 Conf_PongTimeout = atoi( Arg );
630                 if( Conf_PongTimeout < 5 )
631                 {
632                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
633                         Conf_PongTimeout = 5;
634                 }
635                 return;
636         }
637         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
638         {
639                 /* Seconds between connection attempts to other servers */
640                 Conf_ConnectRetry = atoi( Arg );
641                 if( Conf_ConnectRetry < 5 )
642                 {
643                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
644                         Conf_ConnectRetry = 5;
645                 }
646                 return;
647         }
648         if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
649         {
650                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
651                 if( strcasecmp( Arg, "yes" ) == 0 ) Conf_OperCanMode = TRUE;
652                 else if( strcasecmp( Arg, "true" ) == 0 ) Conf_OperCanMode = TRUE;
653                 else if( atoi( Arg ) != 0 ) Conf_OperCanMode = TRUE;
654                 else Conf_OperCanMode = FALSE;
655                 return;
656         }
657         if( strcasecmp( Var, "MaxConnections" ) == 0 )
658         {
659                 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
660 #ifdef HAVE_ISDIGIT
661                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxConnections\" is not a number!", NGIRCd_ConfFile, Line );
662                 else
663 #endif
664                 Conf_MaxConnections = atol( Arg );
665                 return;
666         }
667         if( strcasecmp( Var, "MaxJoins" ) == 0 )
668         {
669                 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
670 #ifdef HAVE_ISDIGIT
671                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxJoins\" is not a number!", NGIRCd_ConfFile, Line );
672                 else
673 #endif
674                 Conf_MaxJoins = atoi( Arg );
675                 return;
676         }
677
678         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
679 } /* Handle_GLOBAL */
680
681
682 LOCAL VOID
683 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
684 {
685         assert( Line > 0 );
686         assert( Var != NULL );
687         assert( Arg != NULL );
688         assert( Conf_Oper_Count > 0 );
689
690         if( strcasecmp( Var, "Name" ) == 0 )
691         {
692                 /* Name of IRC operator */
693                 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 );
694                 return;
695         }
696         if( strcasecmp( Var, "Password" ) == 0 )
697         {
698                 /* Password of IRC operator */
699                 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 );
700                 return;
701         }
702         
703         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
704 } /* Handle_OPERATOR */
705
706
707 LOCAL VOID
708 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
709 {
710         LONG port;
711         
712         assert( Line > 0 );
713         assert( Var != NULL );
714         assert( Arg != NULL );
715
716         /* Ignore server block if no space is left in server configuration structure */
717         if( New_Server_Idx <= NONE ) return;
718
719         if( strcasecmp( Var, "Host" ) == 0 )
720         {
721                 /* Hostname of the server */
722                 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 );
723                 return;
724         }
725         if( strcasecmp( Var, "Name" ) == 0 )
726         {
727                 /* Name of the server ("Nick"/"ID") */
728                 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 );
729                 return;
730         }
731         if( strcasecmp( Var, "MyPassword" ) == 0 )
732         {
733                 /* Password of this server which is sent to the peer */
734                 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 );
735                 return;
736         }
737         if( strcasecmp( Var, "PeerPassword" ) == 0 )
738         {
739                 /* Passwort of the peer which must be received */
740                 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 );
741                 return;
742         }
743         if( strcasecmp( Var, "Port" ) == 0 )
744         {
745                 /* Port to which this server should connect */
746                 port = atol( Arg );
747                 if( port > 0 && port < 0xFFFF ) New_Server.port = (INT)port;
748                 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
749                 return;
750         }
751         if( strcasecmp( Var, "Group" ) == 0 )
752         {
753                 /* Server group */
754 #ifdef HAVE_ISDIGIT
755                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Group\" is not a number!", NGIRCd_ConfFile, Line );
756                 else
757 #endif
758                 New_Server.group = atoi( Arg );
759                 return;
760         }
761         
762         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
763 } /* Handle_SERVER */
764
765
766 LOCAL VOID
767 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
768 {
769         assert( Line > 0 );
770         assert( Var != NULL );
771         assert( Arg != NULL );
772
773         if( strcasecmp( Var, "Name" ) == 0 )
774         {
775                 /* Name of the channel */
776                 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 );
777                 return;
778         }
779         if( strcasecmp( Var, "Modes" ) == 0 )
780         {
781                 /* Initial modes */
782                 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 );
783                 return;
784         }
785         if( strcasecmp( Var, "Topic" ) == 0 )
786         {
787                 /* Initial topic */
788                 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 );
789                 return;
790         }
791
792         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
793 } /* Handle_CHANNEL */
794
795
796 LOCAL VOID
797 Validate_Config( BOOLEAN Configtest )
798 {
799         /* Validate configuration settings. */
800
801 #ifdef DEBUG
802         INT i, servers, servers_once;
803 #endif
804
805         if( ! Conf_ServerName[0] )
806         {
807                 /* No server name configured! */
808                 Config_Error( LOG_ALERT, "No server name configured in \"%s\" ('ServerName')!", NGIRCd_ConfFile );
809                 if( ! Configtest )
810                 {
811                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
812                         exit( 1 );
813                 }
814         }
815
816 #ifdef STRICT_RFC
817         if( ! Conf_ServerAdminMail[0] )
818         {
819                 /* No administrative contact configured! */
820                 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
821                 if( ! Configtest )
822                 {
823                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
824                         exit( 1 );
825                 }
826         }
827 #endif
828
829         if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
830         {
831                 /* No administrative information configured! */
832                 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
833         }
834 #ifdef FD_SETSIZE       
835         if(( Conf_MaxConnections > (LONG)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
836         {
837                 Conf_MaxConnections = (LONG)FD_SETSIZE;
838                 Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
839         }
840 #else
841         Config_Error( LOG_WARN, "Don't know how many file descriptors select() can handle on this system, don't set MaxConnections too high!" );
842 #endif
843
844 #ifdef DEBUG
845         servers = servers_once = 0;
846         for( i = 0; i < MAX_SERVERS; i++ )
847         {
848                 if( Conf_Server[i].name[0] )
849                 {
850                         servers++;
851                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
852                 }
853         }
854         Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
855 #endif
856 } /* Validate_Config */
857
858
859 #ifdef PROTOTYPES
860 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
861 #else
862 LOCAL VOID Config_Error( Level, Format, va_alist )
863 CONST INT Level;
864 CONST CHAR *Format;
865 va_dcl
866 #endif
867 {
868         /* Error! Write to console and/or logfile. */
869
870         CHAR msg[MAX_LOG_MSG_LEN];
871         va_list ap;
872
873         assert( Format != NULL );
874
875 #ifdef PROTOTYPES
876         va_start( ap, Format );
877 #else
878         va_start( ap );
879 #endif
880         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
881         va_end( ap );
882         
883         /* During "normal operations" the log functions of the daemon should
884          * be used, but during testing of the configuration file, all messages
885          * should go directly to the console: */
886         if( Use_Log ) Log( Level, "%s", msg );
887         else puts( msg );
888 } /* Config_Error */
889
890
891 LOCAL VOID
892 Init_Server_Struct( CONF_SERVER *Server )
893 {
894         /* Initialize server configuration structur to default values */
895
896         assert( Server != NULL );
897
898         strcpy( Server->host, "" );
899         strcpy( Server->ip, "" );
900         strcpy( Server->name, "" );
901         strcpy( Server->pwd_in, "" );
902         strcpy( Server->pwd_out, "" );
903         Server->port = 0;
904         Server->group = NONE;
905         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
906         Server->res_stat = NULL;
907         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
908         else Server->flags = 0;
909         Server->conn_id = NONE;
910 } /* Init_Server_Struct */
911
912
913 /* -eof- */