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