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