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