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