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