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