]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
New configuration option "Listen" to bind the server to a specific ip.
[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.60 2003/09/11 12:05:28 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( "  Password = %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         printf( "  Listen = %s\n", Conf_ListenAddress );
124         pwd = getpwuid( Conf_UID );
125         if( pwd ) printf( "  ServerUID = %s\n", pwd->pw_name );
126         else printf( "  ServerUID = %ld\n", (LONG)Conf_UID );
127         grp = getgrgid( Conf_GID );
128         if( grp ) printf( "  ServerGID = %s\n", grp->gr_name );
129         else printf( "  ServerGID = %ld\n", (LONG)Conf_GID );
130         printf( "  PingTimeout = %d\n", Conf_PingTimeout );
131         printf( "  PongTimeout = %d\n", Conf_PongTimeout );
132         printf( "  ConnectRetry = %d\n", Conf_ConnectRetry );
133         printf( "  OperCanUseMode = %s\n", Conf_OperCanMode == TRUE ? "yes" : "no" );
134         if( Conf_MaxConnections > 0 ) printf( "  MaxConnections = %ld\n", Conf_MaxConnections );
135         else printf( "  MaxConnections = -1\n" );
136         if( Conf_MaxJoins > 0 ) printf( "  MaxJoins = %d\n", Conf_MaxJoins );
137         else printf( "  MaxJoins = -1\n" );
138         puts( "" );
139
140         for( i = 0; i < Conf_Oper_Count; i++ )
141         {
142                 if( ! Conf_Oper[i].name[0] ) continue;
143                 
144                 /* Valid "Operator" section */
145                 puts( "[OPERATOR]" );
146                 printf( "  Name = %s\n", Conf_Oper[i].name );
147                 printf( "  Password = %s\n", Conf_Oper[i].pwd );
148                 puts( "" );
149         }
150
151         for( i = 0; i < MAX_SERVERS; i++ )
152         {
153                 if( ! Conf_Server[i].name[0] ) continue;
154                 
155                 /* Valid "Server" section */
156                 puts( "[SERVER]" );
157                 printf( "  Name = %s\n", Conf_Server[i].name );
158                 printf( "  Host = %s\n", Conf_Server[i].host );
159                 printf( "  Port = %d\n", Conf_Server[i].port );
160                 printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
161                 printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
162                 printf( "  Group = %d\n", Conf_Server[i].group );
163                 puts( "" );
164         }
165
166         for( i = 0; i < Conf_Channel_Count; i++ )
167         {
168                 if( ! Conf_Channel[i].name[0] ) continue;
169                 
170                 /* Valid "Channel" section */
171                 puts( "[CHANNEL]" );
172                 printf( "  Name = %s\n", Conf_Channel[i].name );
173                 printf( "  Modes = %s\n", Conf_Channel[i].modes );
174                 printf( "  Topic = %s\n", Conf_Channel[i].topic );
175                 puts( "" );
176         }
177         
178         return 0;
179 } /* Conf_Test */
180
181
182 GLOBAL VOID
183 Conf_UnsetServer( CONN_ID Idx )
184 {
185         /* Set next time for next connection attempt, if this is a server
186          * link that is (still) configured here. If the server is set as
187          * "once", delete it from our configuration.
188          * Non-Server-Connections will be silently ignored. */
189
190         INT i;
191
192         /* Check all our configured servers */
193         for( i = 0; i < MAX_SERVERS; i++ )
194         {
195                 if( Conf_Server[i].conn_id != Idx ) continue;
196
197                 /* Gotcha! Mark server configuration as "unused": */
198                 Conf_Server[i].conn_id = NONE;
199
200                 if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
201                 {
202                         /* Delete configuration here */
203                         Init_Server_Struct( &Conf_Server[i] );
204                 }
205                 else
206                 {
207                         /* Set time for next connect attempt */
208                         if( Conf_Server[i].lasttry <  time( NULL ) - Conf_ConnectRetry )
209                         {
210                                 /* Okay, the connection was established "long enough": */
211                                 Conf_Server[i].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
212                         }
213                 }
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 = 0;
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_NAME, 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         strcpy( Conf_ListenAddress, "" );
345
346         Conf_UID = Conf_GID = 0;
347         
348         Conf_PingTimeout = 120;
349         Conf_PongTimeout = 20;
350
351         Conf_ConnectRetry = 60;
352
353         Conf_Oper_Count = 0;
354         Conf_Channel_Count = 0;
355
356         Conf_OperCanMode = FALSE;
357         
358         Conf_MaxConnections = -1;
359         Conf_MaxJoins = 10;
360
361         /* Initialize server configuration structures */
362         if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
363 } /* Set_Defaults */
364
365
366 LOCAL VOID
367 Read_Config( VOID )
368 {
369         /* Read configuration file. */
370
371         CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
372         INT line, i, n;
373         FILE *fd;
374
375         /* Open configuration file */
376         fd = fopen( NGIRCd_ConfFile, "r" );
377         if( ! fd )
378         {
379                 /* No configuration file found! */
380                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s", NGIRCd_ConfFile, strerror( errno ));
381                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
382                 exit( 1 );
383         }
384
385         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
386
387         /* Clean up server configuration structure: mark all already
388          * configured servers as "once" so that they are deleted
389          * after the next disconnect and delete all unused servers.
390          * And delete all servers which are "duplicates" of servers
391          * that are already marked as "once" (such servers have been
392          * created by the last rehash but are now useless). */
393         for( i = 0; i < MAX_SERVERS; i++ )
394         {
395                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
396                 else
397                 {
398                         /* This structure is in use ... */
399                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE )
400                         {
401                                 /* Check for duplicates */
402                                 for( n = 0; n < MAX_SERVERS; n++ )
403                                 {
404                                         if( n == i ) continue;
405
406                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id )
407                                         {
408                                                 Init_Server_Struct( &Conf_Server[n] );
409                                                 Log( LOG_DEBUG, "Deleted unused duplicate server %d (kept %d).", n, i );
410                                         }
411                                 }
412                         }
413                         else
414                         {
415                                 /* Mark server as "once" */
416                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
417                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
418                         }
419                 }
420         }
421
422         /* Initialize variables */
423         line = 0;
424         strcpy( section, "" );
425         Init_Server_Struct( &New_Server );
426         New_Server_Idx = NONE;
427
428         /* Read configuration file */
429         while( TRUE )
430         {
431                 if( ! fgets( str, LINE_LEN, fd )) break;
432                 ngt_TrimStr( str );
433                 line++;
434
435                 /* Skip comments and empty lines */
436                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
437
438                 /* Is this the beginning of a new section? */
439                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
440                 {
441                         strlcpy( section, str, sizeof( section ));
442                         if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
443                         if( strcasecmp( section, "[OPERATOR]" ) == 0 )
444                         {
445                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Config_Error( LOG_ERR, "Too many operators configured." );
446                                 else
447                                 {
448                                         /* Initialize new operator structure */
449                                         strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
450                                         strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
451                                         Conf_Oper_Count++;
452                                 }
453                                 continue;
454                         }
455                         if( strcasecmp( section, "[SERVER]" ) == 0 )
456                         {
457                                 /* Check if there is already a server to add */
458                                 if( New_Server.name[0] )
459                                 {
460                                         /* Copy data to "real" server structure */
461                                         assert( New_Server_Idx > NONE );
462                                         Conf_Server[New_Server_Idx] = New_Server;
463                                 }
464
465                                 /* Re-init structure for new server */
466                                 Init_Server_Struct( &New_Server );
467
468                                 /* Search unused item in server configuration structure */
469                                 for( i = 0; i < MAX_SERVERS; i++ )
470                                 {
471                                         /* Is this item used? */
472                                         if( ! Conf_Server[i].name[0] ) break;
473                                 }
474                                 if( i >= MAX_SERVERS )
475                                 {
476                                         /* Oops, no free item found! */
477                                         Config_Error( LOG_ERR, "Too many servers configured." );
478                                         New_Server_Idx = NONE;
479                                 }
480                                 else New_Server_Idx = i;
481                                 continue;
482                         }
483                         if( strcasecmp( section, "[CHANNEL]" ) == 0 )
484                         {
485                                 if( Conf_Channel_Count + 1 > MAX_DEFCHANNELS ) Config_Error( LOG_ERR, "Too many pre-defined channels configured." );
486                                 else
487                                 {
488                                         /* Initialize new channel structure */
489                                         strcpy( Conf_Channel[Conf_Channel_Count].name, "" );
490                                         strcpy( Conf_Channel[Conf_Channel_Count].modes, "" );
491                                         strcpy( Conf_Channel[Conf_Channel_Count].topic, "" );
492                                         Conf_Channel_Count++;
493                                 }
494                                 continue;
495                         }
496                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
497                         section[0] = 0x1;
498                 }
499                 if( section[0] == 0x1 ) continue;
500
501                 /* Split line into variable name and parameters */
502                 ptr = strchr( str, '=' );
503                 if( ! ptr )
504                 {
505                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
506                         continue;
507                 }
508                 *ptr = '\0';
509                 var = str; ngt_TrimStr( var );
510                 arg = ptr + 1; ngt_TrimStr( arg );
511
512                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
513                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
514                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
515                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
516                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
517         }
518
519         /* Close configuration file */
520         fclose( fd );
521
522         /* Check if there is still a server to add */
523         if( New_Server.name[0] )
524         {
525                 /* Copy data to "real" server structure */
526                 assert( New_Server_Idx > NONE );
527                 Conf_Server[New_Server_Idx] = New_Server;
528         }
529         
530         /* If there are no ports configured use the default: 6667 */
531         if( Conf_ListenPorts_Count < 1 )
532         {
533                 Conf_ListenPorts_Count = 1;
534                 Conf_ListenPorts[0] = 6667;
535         }
536 } /* Read_Config */
537
538
539 LOCAL VOID
540 Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
541 {
542         struct passwd *pwd;
543         struct group *grp;
544         CHAR *ptr;
545         LONG port;
546         
547         assert( Line > 0 );
548         assert( Var != NULL );
549         assert( Arg != NULL );
550         
551         if( strcasecmp( Var, "Name" ) == 0 )
552         {
553                 /* Server name */
554                 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 );
555                 return;
556         }
557         if( strcasecmp( Var, "Info" ) == 0 )
558         {
559                 /* Info text of server */
560                 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 );
561                 return;
562         }
563         if( strcasecmp( Var, "Password" ) == 0 )
564         {
565                 /* Global server password */
566                 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 );
567                 return;
568         }
569         if( strcasecmp( Var, "AdminInfo1" ) == 0 )
570         {
571                 /* Administrative info #1 */
572                 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 );
573                 return;
574         }
575         if( strcasecmp( Var, "AdminInfo2" ) == 0 )
576         {
577                 /* Administrative info #2 */
578                 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 );
579                 return;
580         }
581         if( strcasecmp( Var, "AdminEMail" ) == 0 )
582         {
583                 /* Administrative email contact */
584                 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 );
585                 return;
586         }
587         if( strcasecmp( Var, "Ports" ) == 0 )
588         {
589                 /* Ports on that the server should listen. More port numbers
590                  * must be separated by "," */
591                 ptr = strtok( Arg, "," );
592                 while( ptr )
593                 {
594                         ngt_TrimStr( ptr );
595                         port = atol( ptr );
596                         if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
597                         else
598                         {
599                                 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT)port;
600                                 else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
601                         }
602                         ptr = strtok( NULL, "," );
603                 }
604                 return;
605         }
606         if( strcasecmp( Var, "MotdFile" ) == 0 )
607         {
608                 /* "Message of the day" (MOTD) file */
609                 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 );
610                 return;
611         }
612         if( strcasecmp( Var, "ServerUID" ) == 0 )
613         {
614                 /* UID the daemon should switch to */
615                 pwd = getpwnam( Arg );
616                 if( pwd ) Conf_UID = pwd->pw_uid;
617                 else
618                 {
619 #ifdef HAVE_ISDIGIT
620                         if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerUID\" is not a number!", NGIRCd_ConfFile, Line );
621                         else
622 #endif
623                         Conf_UID = (UINT)atoi( Arg );
624                 }
625                 return;
626         }
627         if( strcasecmp( Var, "ServerGID" ) == 0 )
628         {
629                 /* GID the daemon should use */
630                 grp = getgrnam( Arg );
631                 if( grp ) Conf_GID = grp->gr_gid;
632                 else
633                 {
634 #ifdef HAVE_ISDIGIT
635                         if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"ServerGID\" is not a number!", NGIRCd_ConfFile, Line );
636                         else
637 #endif
638                         Conf_GID = (UINT)atoi( Arg );
639                 }
640                 return;
641         }
642         if( strcasecmp( Var, "PingTimeout" ) == 0 )
643         {
644                 /* PING timeout */
645                 Conf_PingTimeout = atoi( Arg );
646                 if( Conf_PingTimeout < 5 )
647                 {
648                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!", NGIRCd_ConfFile, Line );
649                         Conf_PingTimeout = 5;
650                 }
651                 return;
652         }
653         if( strcasecmp( Var, "PongTimeout" ) == 0 )
654         {
655                 /* PONG timeout */
656                 Conf_PongTimeout = atoi( Arg );
657                 if( Conf_PongTimeout < 5 )
658                 {
659                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!", NGIRCd_ConfFile, Line );
660                         Conf_PongTimeout = 5;
661                 }
662                 return;
663         }
664         if( strcasecmp( Var, "ConnectRetry" ) == 0 )
665         {
666                 /* Seconds between connection attempts to other servers */
667                 Conf_ConnectRetry = atoi( Arg );
668                 if( Conf_ConnectRetry < 5 )
669                 {
670                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!", NGIRCd_ConfFile, Line );
671                         Conf_ConnectRetry = 5;
672                 }
673                 return;
674         }
675         if( strcasecmp( Var, "OperCanUseMode" ) == 0 )
676         {
677                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
678                 if( strcasecmp( Arg, "yes" ) == 0 ) Conf_OperCanMode = TRUE;
679                 else if( strcasecmp( Arg, "true" ) == 0 ) Conf_OperCanMode = TRUE;
680                 else if( atoi( Arg ) != 0 ) Conf_OperCanMode = TRUE;
681                 else Conf_OperCanMode = FALSE;
682                 return;
683         }
684         if( strcasecmp( Var, "MaxConnections" ) == 0 )
685         {
686                 /* Maximum number of connections. Values <= 0 are equal to "no limit". */
687 #ifdef HAVE_ISDIGIT
688                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxConnections\" is not a number!", NGIRCd_ConfFile, Line );
689                 else
690 #endif
691                 Conf_MaxConnections = atol( Arg );
692                 return;
693         }
694         if( strcasecmp( Var, "MaxJoins" ) == 0 )
695         {
696                 /* Maximum number of channels a user can join. Values <= 0 are equal to "no limit". */
697 #ifdef HAVE_ISDIGIT
698                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"MaxJoins\" is not a number!", NGIRCd_ConfFile, Line );
699                 else
700 #endif
701                 Conf_MaxJoins = atoi( Arg );
702                 return;
703         }
704         if( strcasecmp( Var, "Listen" ) == 0 )
705         {
706                 /* IP-Address to bind sockets */
707                 if( strlcpy( Conf_ListenAddress, Arg, sizeof( Conf_ListenAddress )) >= sizeof( Conf_ListenAddress ))
708                 {
709                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"Listen\" too long!", NGIRCd_ConfFile, Line );
710                         return;
711                 }
712         }
713
714         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
715 } /* Handle_GLOBAL */
716
717
718 LOCAL VOID
719 Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
720 {
721         assert( Line > 0 );
722         assert( Var != NULL );
723         assert( Arg != NULL );
724         assert( Conf_Oper_Count > 0 );
725
726         if( strcasecmp( Var, "Name" ) == 0 )
727         {
728                 /* Name of IRC operator */
729                 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 );
730                 return;
731         }
732         if( strcasecmp( Var, "Password" ) == 0 )
733         {
734                 /* Password of IRC operator */
735                 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 );
736                 return;
737         }
738         
739         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
740 } /* Handle_OPERATOR */
741
742
743 LOCAL VOID
744 Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
745 {
746         LONG port;
747         
748         assert( Line > 0 );
749         assert( Var != NULL );
750         assert( Arg != NULL );
751
752         /* Ignore server block if no space is left in server configuration structure */
753         if( New_Server_Idx <= NONE ) return;
754
755         if( strcasecmp( Var, "Host" ) == 0 )
756         {
757                 /* Hostname of the server */
758                 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 );
759                 return;
760         }
761         if( strcasecmp( Var, "Name" ) == 0 )
762         {
763                 /* Name of the server ("Nick"/"ID") */
764                 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 );
765                 return;
766         }
767         if( strcasecmp( Var, "MyPassword" ) == 0 )
768         {
769                 /* Password of this server which is sent to the peer */
770                 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 );
771                 return;
772         }
773         if( strcasecmp( Var, "PeerPassword" ) == 0 )
774         {
775                 /* Passwort of the peer which must be received */
776                 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 );
777                 return;
778         }
779         if( strcasecmp( Var, "Port" ) == 0 )
780         {
781                 /* Port to which this server should connect */
782                 port = atol( Arg );
783                 if( port > 0 && port < 0xFFFF ) New_Server.port = (INT)port;
784                 else Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port );
785                 return;
786         }
787         if( strcasecmp( Var, "Group" ) == 0 )
788         {
789                 /* Server group */
790 #ifdef HAVE_ISDIGIT
791                 if( ! isdigit( *Arg )) Config_Error( LOG_WARNING, "%s, line %d: Value of \"Group\" is not a number!", NGIRCd_ConfFile, Line );
792                 else
793 #endif
794                 New_Server.group = atoi( Arg );
795                 return;
796         }
797         
798         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
799 } /* Handle_SERVER */
800
801
802 LOCAL VOID
803 Handle_CHANNEL( INT Line, CHAR *Var, CHAR *Arg )
804 {
805         assert( Line > 0 );
806         assert( Var != NULL );
807         assert( Arg != NULL );
808
809         if( strcasecmp( Var, "Name" ) == 0 )
810         {
811                 /* Name of the channel */
812                 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 );
813                 return;
814         }
815         if( strcasecmp( Var, "Modes" ) == 0 )
816         {
817                 /* Initial modes */
818                 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 );
819                 return;
820         }
821         if( strcasecmp( Var, "Topic" ) == 0 )
822         {
823                 /* Initial topic */
824                 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 );
825                 return;
826         }
827
828         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!", NGIRCd_ConfFile, Line, Var );
829 } /* Handle_CHANNEL */
830
831
832 LOCAL VOID
833 Validate_Config( BOOLEAN Configtest )
834 {
835         /* Validate configuration settings. */
836
837 #ifdef DEBUG
838         INT i, servers, servers_once;
839 #endif
840
841         if( ! Conf_ServerName[0] )
842         {
843                 /* No server name configured! */
844                 Config_Error( LOG_ALERT, "No server name configured in \"%s\" ('ServerName')!", NGIRCd_ConfFile );
845                 if( ! Configtest )
846                 {
847                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
848                         exit( 1 );
849                 }
850         }
851         
852         if( ! strchr( Conf_ServerName, '.' ))
853         {
854                 /* No dot in server name! */
855                 Config_Error( LOG_ALERT, "Invalid server name configured in \"%s\" ('ServerName'): Dot missing!", NGIRCd_ConfFile );
856                 if( ! Configtest )
857                 {
858                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
859                         exit( 1 );
860                 }
861         }
862
863 #ifdef STRICT_RFC
864         if( ! Conf_ServerAdminMail[0] )
865         {
866                 /* No administrative contact configured! */
867                 Config_Error( LOG_ALERT, "No administrator email address configured in \"%s\" ('AdminEMail')!", NGIRCd_ConfFile );
868                 if( ! Configtest )
869                 {
870                         Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
871                         exit( 1 );
872                 }
873         }
874 #endif
875
876         if( ! Conf_ServerAdmin1[0] && ! Conf_ServerAdmin2[0] && ! Conf_ServerAdminMail[0] )
877         {
878                 /* No administrative information configured! */
879                 Config_Error( LOG_WARNING, "No administrative information configured but required by RFC!" );
880         }
881 #ifdef FD_SETSIZE       
882         if(( Conf_MaxConnections > (LONG)FD_SETSIZE ) || ( Conf_MaxConnections < 1 ))
883         {
884                 Conf_MaxConnections = (LONG)FD_SETSIZE;
885                 Config_Error( LOG_ERR, "Setting MaxConnections to %ld, select() can't handle more file descriptors!", Conf_MaxConnections );
886         }
887 #else
888         Config_Error( LOG_WARN, "Don't know how many file descriptors select() can handle on this system, don't set MaxConnections too high!" );
889 #endif
890
891 #ifdef DEBUG
892         servers = servers_once = 0;
893         for( i = 0; i < MAX_SERVERS; i++ )
894         {
895                 if( Conf_Server[i].name[0] )
896                 {
897                         servers++;
898                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) servers_once++;
899                 }
900         }
901         Log( LOG_DEBUG, "Configuration: Operators=%d, Servers=%d[%d], Channels=%d", Conf_Oper_Count, servers, servers_once, Conf_Channel_Count );
902 #endif
903 } /* Validate_Config */
904
905
906 #ifdef PROTOTYPES
907 LOCAL VOID Config_Error( CONST INT Level, CONST CHAR *Format, ... )
908 #else
909 LOCAL VOID Config_Error( Level, Format, va_alist )
910 CONST INT Level;
911 CONST CHAR *Format;
912 va_dcl
913 #endif
914 {
915         /* Error! Write to console and/or logfile. */
916
917         CHAR msg[MAX_LOG_MSG_LEN];
918         va_list ap;
919
920         assert( Format != NULL );
921
922 #ifdef PROTOTYPES
923         va_start( ap, Format );
924 #else
925         va_start( ap );
926 #endif
927         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
928         va_end( ap );
929         
930         /* During "normal operations" the log functions of the daemon should
931          * be used, but during testing of the configuration file, all messages
932          * should go directly to the console: */
933         if( Use_Log ) Log( Level, "%s", msg );
934         else puts( msg );
935 } /* Config_Error */
936
937
938 LOCAL VOID
939 Init_Server_Struct( CONF_SERVER *Server )
940 {
941         /* Initialize server configuration structur to default values */
942
943         assert( Server != NULL );
944
945         strcpy( Server->host, "" );
946         strcpy( Server->ip, "" );
947         strcpy( Server->name, "" );
948         strcpy( Server->pwd_in, "" );
949         strcpy( Server->pwd_out, "" );
950         Server->port = 0;
951         Server->group = NONE;
952         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
953         Server->res_stat = NULL;
954         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
955         else Server->flags = 0;
956         Server->conn_id = NONE;
957 } /* Init_Server_Struct */
958
959
960 /* -eof- */