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