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