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