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