]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
c50f8f9ff222bc8156047398aa77bc04d0164d02
[ngircd-alex.git] / src / ngircd / conf.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2009 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 #include "imp.h"
18 #include <assert.h>
19 #include <errno.h>
20 #ifdef PROTOTYPES
21 #       include <stdarg.h>
22 #else
23 #       include <varargs.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <unistd.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 #ifdef HAVE_CTYPE_H
36 # include <ctype.h>
37 #endif
38
39 #include "array.h"
40 #include "ngircd.h"
41 #include "conn.h"
42 #include "client.h"
43 #include "channel.h"
44 #include "defines.h"
45 #include "log.h"
46 #include "match.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 static size_t Conf_Channel_Count;
59 static void Set_Defaults PARAMS(( bool InitServers ));
60 static bool Read_Config PARAMS(( bool ngircd_starting ));
61 static bool 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 #ifdef WANT_IPV6
76 #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
77 #else
78 #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
79 #endif
80
81 #ifdef SSL_SUPPORT
82 struct SSLOptions Conf_SSLOptions;
83
84 static void
85 ConfSSL_Init(void)
86 {
87         free(Conf_SSLOptions.KeyFile);
88         Conf_SSLOptions.KeyFile = NULL;
89
90         free(Conf_SSLOptions.CertFile);
91         Conf_SSLOptions.CertFile = NULL;
92
93         free(Conf_SSLOptions.DHFile);
94         Conf_SSLOptions.DHFile = NULL;
95         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
96 }
97
98 static bool
99 can_open(const char *name, const char *file)
100 {
101         FILE *fp = fopen(file, "r");
102         if (fp)
103                 fclose(fp);
104         else
105                 fprintf(stderr, "ERROR: %s \"%s\": %s\n",
106                         name, file, strerror(errno));
107         return fp != NULL;
108 }
109
110 static bool
111 ConfSSL_Puts(void)
112 {
113         bool ret = true;
114
115         if (Conf_SSLOptions.KeyFile) {
116                 printf( "  SSLKeyFile = %s\n", Conf_SSLOptions.KeyFile);
117                 ret = can_open("SSLKeyFile", Conf_SSLOptions.KeyFile);
118         }
119         if (Conf_SSLOptions.CertFile) {
120                 printf( "  SSLCertFile = %s\n", Conf_SSLOptions.CertFile);
121                 if (!can_open("SSLCertFile", Conf_SSLOptions.CertFile))
122                         ret = false;
123         }
124         if (Conf_SSLOptions.DHFile) {
125                 printf( "  SSLDHFile = %s\n", Conf_SSLOptions.DHFile);
126                 if (!can_open("SSLDHFile", Conf_SSLOptions.DHFile))
127                         ret = false;
128         }
129         if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
130                 puts("  SSLKeyFilePassword = <secret>"  );
131         array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
132
133         return ret;
134 }
135 #endif
136
137 static char *
138 strdup_warn(const char *str)
139 {
140         char *ptr = strdup(str);
141         if (!ptr)
142                 Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
143         return ptr;
144 }
145
146
147 static void
148 ports_puts(array *a)
149 {
150         size_t len;
151         UINT16 *ports;
152         len = array_length(a, sizeof(UINT16));
153         if (len--) {
154                 ports = (UINT16*) array_start(a);
155                 printf("%u", (unsigned int) *ports);
156                 while (len--) {
157                         ports++;
158                         printf(", %u", (unsigned int) *ports);
159                 }
160         }
161         putc('\n', stdout);
162 }
163
164
165 static void
166 ports_parse(array *a, int Line, char *Arg)
167 {
168         char *ptr;
169         int port;
170         UINT16 port16;
171
172         array_trunc(a);
173
174         /* Ports on that the server should listen. More port numbers
175          * must be separated by "," */
176         ptr = strtok( Arg, "," );
177         while (ptr) {
178                 ngt_TrimStr(ptr);
179                 port = atoi(ptr);
180                 if (port > 0 && port < 0xFFFF) {
181                         port16 = (UINT16) port;
182                         if (!array_catb(a, (char*)&port16, sizeof port16))
183                                 Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
184                                                         NGIRCd_ConfFile, Line, port, strerror(errno));
185                 } else {
186                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
187                                                                         NGIRCd_ConfFile, Line, port );
188                 }
189
190                 ptr = strtok( NULL, "," );
191         }
192 }
193
194
195 GLOBAL void
196 Conf_Init( void )
197 {
198         Read_Config( true );
199         Validate_Config(false, false);
200 } /* Config_Init */
201
202
203 GLOBAL bool
204 Conf_Rehash( void )
205 {
206         if (!Read_Config(false))
207                 return false;
208         Validate_Config(false, true);
209
210         /* Update CLIENT structure of local server */
211         Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
212         return true;
213 } /* Config_Rehash */
214
215
216 static const char*
217 yesno_to_str(int boolean_value)
218 {
219         if (boolean_value)
220                 return "yes";
221         return "no";
222 }
223
224
225 GLOBAL int
226 Conf_Test( void )
227 {
228         /* Read configuration, validate and output it. */
229
230         struct passwd *pwd;
231         struct group *grp;
232         unsigned int i;
233         bool config_valid;
234         size_t predef_channel_count;
235         struct Conf_Channel *predef_chan;
236
237         Use_Log = false;
238
239         if (! Read_Config(true))
240                 return 1;
241
242         config_valid = Validate_Config(true, false);
243
244         /* If stdin and stdout ("you can read our nice message and we can
245          * read in your keypress") are valid tty's, wait for a key: */
246         if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
247                 puts( "OK, press enter to see a dump of your service configuration ..." );
248                 getchar( );
249         } else {
250                 puts( "Ok, dump of your server configuration follows:\n" );
251         }
252
253         puts( "[GLOBAL]" );
254         printf( "  Name = %s\n", Conf_ServerName );
255         printf( "  Info = %s\n", Conf_ServerInfo );
256         printf( "  Password = %s\n", Conf_ServerPwd );
257         printf( "  AdminInfo1 = %s\n", Conf_ServerAdmin1 );
258         printf( "  AdminInfo2 = %s\n", Conf_ServerAdmin2 );
259         printf( "  AdminEMail = %s\n", Conf_ServerAdminMail );
260         printf( "  MotdFile = %s\n", Conf_MotdFile );
261         printf( "  MotdPhrase = %s\n", Conf_MotdPhrase );
262         printf( "  ChrootDir = %s\n", Conf_Chroot );
263         printf( "  PidFile = %s\n", Conf_PidFile);
264         printf("  Listen = %s\n", Conf_ListenAddress);
265         fputs("  Ports = ", stdout);
266
267         ports_puts(&Conf_ListenPorts);
268 #ifdef SSL_SUPPORT
269         fputs("  SSLPorts = ", stdout);
270         ports_puts(&Conf_SSLOptions.ListenPorts);
271         if (!ConfSSL_Puts())
272                 config_valid = false;
273 #endif
274
275         pwd = getpwuid( Conf_UID );
276         if( pwd ) printf( "  ServerUID = %s\n", pwd->pw_name );
277         else printf( "  ServerUID = %ld\n", (long)Conf_UID );
278         grp = getgrgid( Conf_GID );
279         if( grp ) printf( "  ServerGID = %s\n", grp->gr_name );
280         else printf( "  ServerGID = %ld\n", (long)Conf_GID );
281         printf( "  PingTimeout = %d\n", Conf_PingTimeout );
282         printf( "  PongTimeout = %d\n", Conf_PongTimeout );
283         printf( "  ConnectRetry = %d\n", Conf_ConnectRetry );
284         printf( "  OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
285         printf( "  OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
286         printf( "  PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
287         printf( "  NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
288         printf( "  NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
289
290 #ifdef WANT_IPV6
291         printf("  ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
292         printf("  ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
293 #endif
294         printf( "  MaxConnections = %ld\n", Conf_MaxConnections);
295         printf( "  MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
296         printf( "  MaxJoins = %d\n", Conf_MaxJoins>0 ? Conf_MaxJoins : -1);
297         printf( "  MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
298
299         for( i = 0; i < Conf_Oper_Count; i++ ) {
300                 if( ! Conf_Oper[i].name[0] ) continue;
301
302                 /* Valid "Operator" section */
303                 puts( "[OPERATOR]" );
304                 printf( "  Name = %s\n", Conf_Oper[i].name );
305                 printf( "  Password = %s\n", Conf_Oper[i].pwd );
306                 if ( Conf_Oper[i].mask ) printf( "  Mask = %s\n", Conf_Oper[i].mask );
307                 puts( "" );
308         }
309
310         for( i = 0; i < MAX_SERVERS; i++ ) {
311                 if( ! Conf_Server[i].name[0] ) continue;
312
313                 /* Valid "Server" section */
314                 puts( "[SERVER]" );
315                 printf( "  Name = %s\n", Conf_Server[i].name );
316                 printf( "  Host = %s\n", Conf_Server[i].host );
317                 printf( "  Port = %u\n", (unsigned int)Conf_Server[i].port );
318 #ifdef SSL_SUPPORT
319                 printf( "  SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
320 #endif
321                 printf( "  MyPassword = %s\n", Conf_Server[i].pwd_in );
322                 printf( "  PeerPassword = %s\n", Conf_Server[i].pwd_out );
323                 printf( "  ServiceMask = %s\n", Conf_Server[i].svs_mask);
324                 printf( "  Group = %d\n", Conf_Server[i].group );
325                 printf( "  Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
326         }
327
328         predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
329         predef_chan = array_start(&Conf_Channels);
330
331         for (i = 0; i < predef_channel_count; i++, predef_chan++) {
332                 if (!predef_chan->name[0])
333                         continue;
334
335                 /* Valid "Channel" section */
336                 puts( "[CHANNEL]" );
337                 printf("  Name = %s\n", predef_chan->name);
338                 printf("  Modes = %s\n", predef_chan->modes);
339                 printf("  Key = %s\n", predef_chan->key);
340                 printf("  MaxUsers = %lu\n", predef_chan->maxusers);
341                 printf("  Topic = %s\n", predef_chan->topic);
342                 printf("  KeyFile = %s\n\n", predef_chan->keyfile);
343         }
344
345         return (config_valid ? 0 : 1);
346 } /* Conf_Test */
347
348
349 GLOBAL void
350 Conf_UnsetServer( CONN_ID Idx )
351 {
352         /* Set next time for next connection attempt, if this is a server
353          * link that is (still) configured here. If the server is set as
354          * "once", delete it from our configuration.
355          * Non-Server-Connections will be silently ignored. */
356
357         int i;
358         time_t t;
359
360         /* Check all our configured servers */
361         for( i = 0; i < MAX_SERVERS; i++ ) {
362                 if( Conf_Server[i].conn_id != Idx ) continue;
363
364                 /* Gotcha! Mark server configuration as "unused": */
365                 Conf_Server[i].conn_id = NONE;
366
367                 if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
368                         /* Delete configuration here */
369                         Init_Server_Struct( &Conf_Server[i] );
370                 } else {
371                         /* Set time for next connect attempt */
372                         t = time(NULL);
373                         if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
374                                 /* The connection has been "long", so we don't
375                                  * require the next attempt to be delayed. */
376                                 Conf_Server[i].lasttry =
377                                         t - Conf_ConnectRetry + RECONNECT_DELAY;
378                         } else
379                                 Conf_Server[i].lasttry = t;
380                 }
381         }
382 } /* Conf_UnsetServer */
383
384
385 GLOBAL void
386 Conf_SetServer( int ConfServer, CONN_ID Idx )
387 {
388         /* Set connection for specified configured server */
389
390         assert( ConfServer > NONE );
391         assert( Idx > NONE );
392
393         Conf_Server[ConfServer].conn_id = Idx;
394 } /* Conf_SetServer */
395
396
397 GLOBAL int
398 Conf_GetServer( CONN_ID Idx )
399 {
400         /* Get index of server in configuration structure */
401
402         int i = 0;
403
404         assert( Idx > NONE );
405
406         for( i = 0; i < MAX_SERVERS; i++ ) {
407                 if( Conf_Server[i].conn_id == Idx ) return i;
408         }
409         return NONE;
410 } /* Conf_GetServer */
411
412
413 GLOBAL bool
414 Conf_EnableServer( const char *Name, UINT16 Port )
415 {
416         /* Enable specified server and adjust port */
417
418         int i;
419
420         assert( Name != NULL );
421
422         for( i = 0; i < MAX_SERVERS; i++ ) {
423                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
424                         /* Gotcha! Set port and enable server: */
425                         Conf_Server[i].port = Port;
426                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
427                         return (Conf_Server[i].port && Conf_Server[i].host[0]);
428                 }
429         }
430         return false;
431 } /* Conf_EnableServer */
432
433
434 GLOBAL bool
435 Conf_EnablePassiveServer(const char *Name)
436 {
437         /* Enable specified server */
438         int i;
439
440         assert( Name != NULL );
441         for (i = 0; i < MAX_SERVERS; i++) {
442                 if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
443                         /* BINGO! Enable server */
444                         Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
445                         return true;
446                 }
447         }
448         return false;
449 } /* Conf_EnablePassiveServer */
450
451
452 GLOBAL bool
453 Conf_DisableServer( const char *Name )
454 {
455         /* Enable specified server and adjust port */
456
457         int i;
458
459         assert( Name != NULL );
460
461         for( i = 0; i < MAX_SERVERS; i++ ) {
462                 if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
463                         /* Gotcha! Disable and disconnect server: */
464                         Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
465                         if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
466                         return true;
467                 }
468         }
469         return false;
470 } /* Conf_DisableServer */
471
472
473 GLOBAL bool
474 Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
475 {
476         /* Add new server to configuration */
477
478         int i;
479
480         assert( Name != NULL );
481         assert( Host != NULL );
482         assert( MyPwd != NULL );
483         assert( PeerPwd != NULL );
484
485         /* Search unused item in server configuration structure */
486         for( i = 0; i < MAX_SERVERS; i++ ) {
487                 /* Is this item used? */
488                 if( ! Conf_Server[i].name[0] ) break;
489         }
490         if( i >= MAX_SERVERS ) return false;
491
492         Init_Server_Struct( &Conf_Server[i] );
493         strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
494         strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
495         strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
496         strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
497         Conf_Server[i].port = Port;
498         Conf_Server[i].flags = CONF_SFLAG_ONCE;
499
500         return true;
501 } /* Conf_AddServer */
502
503
504 /**
505  * Check if the given nick name is an service
506  */
507 GLOBAL bool
508 Conf_IsService(int ConfServer, const char *Nick)
509 {
510         return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
511 } /* Conf_IsService */
512
513
514 static void
515 Set_Defaults( bool InitServers )
516 {
517         /* Initialize configuration variables with default values. */
518
519         int i;
520
521         strcpy( Conf_ServerName, "" );
522         snprintf( Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s", PACKAGE_NAME, PACKAGE_VERSION );
523         strcpy( Conf_ServerPwd, "" );
524
525         strcpy( Conf_ServerAdmin1, "" );
526         strcpy( Conf_ServerAdmin2, "" );
527         strcpy( Conf_ServerAdminMail, "" );
528
529         strlcpy( Conf_MotdFile, SYSCONFDIR, sizeof( Conf_MotdFile ));
530         strlcat( Conf_MotdFile, MOTD_FILE, sizeof( Conf_MotdFile ));
531
532         strlcpy( Conf_MotdPhrase, MOTD_PHRASE, sizeof( Conf_MotdPhrase ));
533
534         strlcpy( Conf_Chroot, CHROOT_DIR, sizeof( Conf_Chroot ));
535
536         strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile ));
537
538         free(Conf_ListenAddress);
539         Conf_ListenAddress = NULL;
540         Conf_UID = Conf_GID = 0;
541
542         Conf_PingTimeout = 120;
543         Conf_PongTimeout = 20;
544
545         Conf_ConnectRetry = 60;
546
547         Conf_Oper_Count = 0;
548         Conf_Channel_Count = 0;
549
550         Conf_OperCanMode = false;
551         Conf_NoDNS = false;
552         Conf_NoIdent = false;
553         Conf_PredefChannelsOnly = false;
554         Conf_OperServerMode = false;
555
556         Conf_ConnectIPv4 = true;
557         Conf_ConnectIPv6 = true;
558
559         Conf_MaxConnections = 0;
560         Conf_MaxConnectionsIP = 5;
561         Conf_MaxJoins = 10;
562         Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
563
564         /* Initialize server configuration structures */
565         if( InitServers ) for( i = 0; i < MAX_SERVERS; Init_Server_Struct( &Conf_Server[i++] ));
566 } /* Set_Defaults */
567
568
569 static bool
570 no_listenports(void)
571 {
572         size_t cnt = array_bytes(&Conf_ListenPorts);
573 #ifdef SSL_SUPPORT
574         cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
575 #endif
576         return cnt == 0;
577 }
578
579 static bool
580 Read_Config( bool ngircd_starting )
581 {
582         /* Read configuration file. */
583
584         char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
585         const UINT16 defaultport = 6667;
586         int line, i, n;
587         FILE *fd;
588
589         /* Open configuration file */
590         fd = fopen( NGIRCd_ConfFile, "r" );
591         if( ! fd ) {
592                 /* No configuration file found! */
593                 Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
594                                         NGIRCd_ConfFile, strerror( errno ));
595                 if (!ngircd_starting)
596                         return false;
597                 Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
598                 exit( 1 );
599         }
600
601         Set_Defaults( ngircd_starting );
602
603         Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
604
605         /* Clean up server configuration structure: mark all already
606          * configured servers as "once" so that they are deleted
607          * after the next disconnect and delete all unused servers.
608          * And delete all servers which are "duplicates" of servers
609          * that are already marked as "once" (such servers have been
610          * created by the last rehash but are now useless). */
611         for( i = 0; i < MAX_SERVERS; i++ ) {
612                 if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
613                 else {
614                         /* This structure is in use ... */
615                         if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
616                                 /* Check for duplicates */
617                                 for( n = 0; n < MAX_SERVERS; n++ ) {
618                                         if( n == i ) continue;
619
620                                         if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
621                                                 Init_Server_Struct( &Conf_Server[n] );
622 #ifdef DEBUG
623                                                 Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
624                                                                                                 n, i );
625 #endif
626                                         }
627                                 }
628                         } else {
629                                 /* Mark server as "once" */
630                                 Conf_Server[i].flags |= CONF_SFLAG_ONCE;
631                                 Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
632                         }
633                 }
634         }
635
636         /* Initialize variables */
637         line = 0;
638         strcpy( section, "" );
639         Init_Server_Struct( &New_Server );
640         New_Server_Idx = NONE;
641 #ifdef SSL_SUPPORT
642         ConfSSL_Init();
643 #endif
644         /* Read configuration file */
645         while( true ) {
646                 if( ! fgets( str, LINE_LEN, fd )) break;
647                 ngt_TrimStr( str );
648                 line++;
649
650                 /* Skip comments and empty lines */
651                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
652
653                 /* Is this the beginning of a new section? */
654                 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
655                         strlcpy( section, str, sizeof( section ));
656                         if( strcasecmp( section, "[GLOBAL]" ) == 0 )
657                                 continue;
658
659                         if( strcasecmp( section, "[OPERATOR]" ) == 0 ) {
660                                 if( Conf_Oper_Count + 1 > MAX_OPERATORS )
661                                         Config_Error( LOG_ERR, "Too many operators configured.");
662                                 else {
663                                         /* Initialize new operator structure */
664                                         Conf_Oper[Conf_Oper_Count].name[0] = '\0';
665                                         Conf_Oper[Conf_Oper_Count].pwd[0] = '\0';
666                                         if (Conf_Oper[Conf_Oper_Count].mask) {
667                                                 free(Conf_Oper[Conf_Oper_Count].mask );
668                                                 Conf_Oper[Conf_Oper_Count].mask = NULL;
669                                         }
670                                         Conf_Oper_Count++;
671                                 }
672                                 continue;
673                         }
674                         if( strcasecmp( section, "[SERVER]" ) == 0 ) {
675                                 /* Check if there is already a server to add */
676                                 if( New_Server.name[0] ) {
677                                         /* Copy data to "real" server structure */
678                                         assert( New_Server_Idx > NONE );
679                                         Conf_Server[New_Server_Idx] = New_Server;
680                                 }
681
682                                 /* Re-init structure for new server */
683                                 Init_Server_Struct( &New_Server );
684
685                                 /* Search unused item in server configuration structure */
686                                 for( i = 0; i < MAX_SERVERS; i++ ) {
687                                         /* Is this item used? */
688                                         if( ! Conf_Server[i].name[0] ) break;
689                                 }
690                                 if( i >= MAX_SERVERS ) {
691                                         /* Oops, no free item found! */
692                                         Config_Error( LOG_ERR, "Too many servers configured." );
693                                         New_Server_Idx = NONE;
694                                 }
695                                 else New_Server_Idx = i;
696                                 continue;
697                         }
698                         if (strcasecmp(section, "[CHANNEL]") == 0) {
699                                 Conf_Channel_Count++;
700                                 continue;
701                         }
702
703                         Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
704                         section[0] = 0x1;
705                 }
706                 if( section[0] == 0x1 ) continue;
707
708                 /* Split line into variable name and parameters */
709                 ptr = strchr( str, '=' );
710                 if( ! ptr ) {
711                         Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
712                         continue;
713                 }
714                 *ptr = '\0';
715                 var = str; ngt_TrimStr( var );
716                 arg = ptr + 1; ngt_TrimStr( arg );
717
718                 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
719                 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
720                 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
721                 else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
722                 else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
723         }
724
725         /* Close configuration file */
726         fclose( fd );
727
728         /* Check if there is still a server to add */
729         if( New_Server.name[0] ) {
730                 /* Copy data to "real" server structure */
731                 assert( New_Server_Idx > NONE );
732                 Conf_Server[New_Server_Idx] = New_Server;
733         }
734
735         /* not a single listening port? Add default. */
736         if (no_listenports() &&
737                 !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
738         {
739                 Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
740                                         (unsigned int) defaultport, strerror(errno));
741
742                 exit(1);
743         }
744
745         if (!Conf_ListenAddress)
746                 Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
747
748         if (!Conf_ListenAddress) {
749                 Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
750                 exit(1);
751         }
752         return true;
753 } /* Read_Config */
754
755
756 static bool
757 Check_ArgIsTrue( const char *Arg )
758 {
759         if( strcasecmp( Arg, "yes" ) == 0 ) return true;
760         if( strcasecmp( Arg, "true" ) == 0 ) return true;
761         if( atoi( Arg ) != 0 ) return true;
762
763         return false;
764 } /* Check_ArgIsTrue */
765
766
767 static unsigned int Handle_MaxNickLength(int Line, const char *Arg)
768 {
769         unsigned new;
770
771         new = (unsigned) atoi(Arg) + 1;
772         if (new > CLIENT_NICK_LEN) {
773                 Config_Error(LOG_WARNING,
774                              "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
775                              NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
776                 return CLIENT_NICK_LEN;
777         }
778         if (new < 2) {
779                 Config_Error(LOG_WARNING,
780                              "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
781                              NGIRCd_ConfFile, Line);
782                 return 2;
783         }
784         return new;
785 } /* Handle_MaxNickLength */
786
787
788 static void
789 Handle_GLOBAL( int Line, char *Var, char *Arg )
790 {
791         struct passwd *pwd;
792         struct group *grp;
793         size_t len;
794         
795         assert( Line > 0 );
796         assert( Var != NULL );
797         assert( Arg != NULL );
798         
799         if( strcasecmp( Var, "Name" ) == 0 ) {
800                 /* Server name */
801                 len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
802                 if (len >= sizeof( Conf_ServerName ))
803                         Config_Error_TooLong( Line, Var );
804                 return;
805         }
806         if( strcasecmp( Var, "Info" ) == 0 ) {
807                 /* Info text of server */
808                 len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
809                 if (len >= sizeof( Conf_ServerInfo ))
810                         Config_Error_TooLong ( Line, Var );
811                 return;
812         }
813         if( strcasecmp( Var, "Password" ) == 0 ) {
814                 /* Global server password */
815                 len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
816                 if (len >= sizeof( Conf_ServerPwd ))
817                         Config_Error_TooLong( Line, Var );
818                 return;
819         }
820         if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
821                 /* Administrative info #1 */
822                 len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
823                 if (len >= sizeof( Conf_ServerAdmin1 ))
824                         Config_Error_TooLong ( Line, Var );
825                 return;
826         }
827         if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
828                 /* Administrative info #2 */
829                 len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
830                 if (len >= sizeof( Conf_ServerAdmin2 ))
831                         Config_Error_TooLong ( Line, Var );
832                 return;
833         }
834         if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
835                 /* Administrative email contact */
836                 len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
837                 if (len >= sizeof( Conf_ServerAdminMail ))
838                         Config_Error_TooLong( Line, Var );
839                 return;
840         }
841
842         if( strcasecmp( Var, "Ports" ) == 0 ) {
843                 ports_parse(&Conf_ListenPorts, Line, Arg);
844                 return;
845         }
846         if( strcasecmp( Var, "MotdFile" ) == 0 ) {
847                 /* "Message of the day" (MOTD) file */
848                 len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
849                 if (len >= sizeof( Conf_MotdFile ))
850                         Config_Error_TooLong( Line, Var );
851                 return;
852         }
853         if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
854                 /* "Message of the day" phrase (instead of file) */
855                 len = strlcpy( Conf_MotdPhrase, Arg, sizeof( Conf_MotdPhrase ));
856                 if (len >= sizeof( Conf_MotdPhrase ))
857                         Config_Error_TooLong( Line, Var );
858                 return;
859         }
860         if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
861                 /* directory for chroot() */
862                 len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
863                 if (len >= sizeof( Conf_Chroot ))
864                         Config_Error_TooLong( Line, Var );
865                 return;
866         }
867         if ( strcasecmp( Var, "PidFile" ) == 0 ) {
868                 /* name of pidfile */
869                 len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
870                 if (len >= sizeof( Conf_PidFile ))
871                         Config_Error_TooLong( Line, Var );
872                 return;
873         }
874         if( strcasecmp( Var, "ServerUID" ) == 0 ) {
875                 /* UID the daemon should switch to */
876                 pwd = getpwnam( Arg );
877                 if( pwd ) Conf_UID = pwd->pw_uid;
878                 else {
879 #ifdef HAVE_ISDIGIT
880                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
881                         else
882 #endif
883                         Conf_UID = (unsigned int)atoi( Arg );
884                 }
885                 return;
886         }
887         if( strcasecmp( Var, "ServerGID" ) == 0 ) {
888                 /* GID the daemon should use */
889                 grp = getgrnam( Arg );
890                 if( grp ) Conf_GID = grp->gr_gid;
891                 else {
892 #ifdef HAVE_ISDIGIT
893                         if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
894                         else
895 #endif
896                         Conf_GID = (unsigned int)atoi( Arg );
897                 }
898                 return;
899         }
900         if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
901                 /* PING timeout */
902                 Conf_PingTimeout = atoi( Arg );
903                 if( Conf_PingTimeout < 5 ) {
904                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
905                                                                         NGIRCd_ConfFile, Line );
906                         Conf_PingTimeout = 5;
907                 }
908                 return;
909         }
910         if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
911                 /* PONG timeout */
912                 Conf_PongTimeout = atoi( Arg );
913                 if( Conf_PongTimeout < 5 ) {
914                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
915                                                                         NGIRCd_ConfFile, Line );
916                         Conf_PongTimeout = 5;
917                 }
918                 return;
919         }
920         if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
921                 /* Seconds between connection attempts to other servers */
922                 Conf_ConnectRetry = atoi( Arg );
923                 if( Conf_ConnectRetry < 5 ) {
924                         Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
925                                                                         NGIRCd_ConfFile, Line );
926                         Conf_ConnectRetry = 5;
927                 }
928                 return;
929         }
930         if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
931                 /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
932                 Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
933                 return;
934         }
935         if( strcasecmp( Var, "NoDNS" ) == 0 ) {
936                 /* don't do reverse dns lookups when clients connect? */
937                 Conf_NoDNS = Check_ArgIsTrue( Arg );
938                 return;
939         }
940         if (strcasecmp(Var, "NoIdent") == 0) {
941                 /* don't do IDENT lookups when clients connect? */
942                 Conf_NoIdent = Check_ArgIsTrue(Arg);
943 #ifndef IDENTAUTH
944                 if (!Conf_NoIdent) {
945                         /* user has enabled ident lookups explicitly, but ... */
946                         Config_Error(LOG_WARNING,
947                                 "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
948                                 NGIRCd_ConfFile, Line);
949                 }
950 #endif
951                 return;
952         }
953 #ifdef WANT_IPV6
954         /* the default setting for all the WANT_IPV6 special options is 'true' */
955         if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
956                 /* connect to other hosts using ipv6, if they have an AAAA record? */
957                 Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
958                 return;
959         }
960         if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
961                 /* connect to other hosts using ipv4.
962                  * again, this can be used for ipv6-only setups */
963                 Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
964                 return;
965         }
966 #endif
967         if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
968                 /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
969                 Conf_OperCanMode = Check_ArgIsTrue( Arg );
970                 return;
971         }
972         if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
973                 /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
974                 Conf_OperServerMode = Check_ArgIsTrue( Arg );
975                 return;
976         }
977         if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
978                 /* Maximum number of connections. 0 -> "no limit". */
979 #ifdef HAVE_ISDIGIT
980                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
981                 else
982 #endif
983                 Conf_MaxConnections = atol( Arg );
984                 return;
985         }
986         if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
987                 /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
988 #ifdef HAVE_ISDIGIT
989                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
990                 else
991 #endif
992                 Conf_MaxConnectionsIP = atoi( Arg );
993                 return;
994         }
995         if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
996                 /* Maximum number of channels a user can join. 0 -> "no limit". */
997 #ifdef HAVE_ISDIGIT
998                 if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
999                 else
1000 #endif
1001                 Conf_MaxJoins = atoi( Arg );
1002                 return;
1003         }
1004         if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
1005                 /* Maximum length of a nick name; must be same on all servers
1006                  * within the IRC network! */
1007                 Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
1008                 return;
1009         }
1010
1011         if( strcasecmp( Var, "Listen" ) == 0 ) {
1012                 /* IP-Address to bind sockets */
1013                 if (Conf_ListenAddress) {
1014                         Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
1015                         return;
1016                 }
1017                 Conf_ListenAddress = strdup_warn(Arg);
1018                 /*
1019                  * if allocation fails, we're in trouble:
1020                  * we cannot ignore the error -- otherwise ngircd
1021                  * would listen on all interfaces.
1022                  */
1023                 if (!Conf_ListenAddress) {
1024                         Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
1025                         exit(1);
1026                 }
1027                 return;
1028         }
1029
1030 #ifdef SSL_SUPPORT
1031         if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
1032                 ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
1033                 return;
1034         }
1035
1036         if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
1037                 assert(Conf_SSLOptions.KeyFile == NULL );
1038                 Conf_SSLOptions.KeyFile = strdup_warn(Arg);
1039                 return;
1040         }
1041         if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
1042                 assert(Conf_SSLOptions.CertFile == NULL );
1043                 Conf_SSLOptions.CertFile = strdup_warn(Arg);
1044                 return;
1045         }
1046
1047         if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
1048                 assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
1049                 if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
1050                         Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
1051                                                                 NGIRCd_ConfFile, Line, Var, strerror(errno));
1052                 return;
1053         }
1054         if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
1055                 assert(Conf_SSLOptions.DHFile == NULL);
1056                 Conf_SSLOptions.DHFile = strdup_warn( Arg );
1057                 return;
1058         }
1059 #endif
1060         Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
1061                                                                 NGIRCd_ConfFile, Line, Var);
1062 } /* Handle_GLOBAL */
1063
1064
1065 static void
1066 Handle_OPERATOR( int Line, char *Var, char *Arg )
1067 {
1068         unsigned int opercount;
1069         size_t len;
1070         assert( Line > 0 );
1071         assert( Var != NULL );
1072         assert( Arg != NULL );
1073         assert( Conf_Oper_Count > 0 );
1074
1075         if ( Conf_Oper_Count == 0 )
1076                 return;
1077
1078         opercount = Conf_Oper_Count - 1;
1079
1080         if( strcasecmp( Var, "Name" ) == 0 ) {
1081                 /* Name of IRC operator */
1082                 len = strlcpy( Conf_Oper[opercount].name, Arg, sizeof( Conf_Oper[opercount].name ));
1083                 if (len >= sizeof( Conf_Oper[opercount].name ))
1084                                 Config_Error_TooLong( Line, Var );
1085                 return;
1086         }
1087         if( strcasecmp( Var, "Password" ) == 0 ) {
1088                 /* Password of IRC operator */
1089                 len = strlcpy( Conf_Oper[opercount].pwd, Arg, sizeof( Conf_Oper[opercount].pwd ));
1090                 if (len >= sizeof( Conf_Oper[opercount].pwd ))
1091                                 Config_Error_TooLong( Line, Var );
1092                 return;
1093         }
1094         if( strcasecmp( Var, "Mask" ) == 0 ) {
1095                 if (Conf_Oper[opercount].mask) return; /* Hostname already configured */
1096
1097                 Conf_Oper[opercount].mask = strdup_warn( Arg );
1098                 return;
1099         }
1100         Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
1101                                                                 NGIRCd_ConfFile, Line, Var );
1102 } /* Handle_OPERATOR */
1103
1104
1105 static void
1106 Handle_SERVER( int Line, char *Var, char *Arg )
1107 {
1108         long port;
1109         size_t len;
1110         
1111         assert( Line > 0 );
1112         assert( Var != NULL );
1113         assert( Arg != NULL );
1114
1115         /* Ignore server block if no space is left in server configuration structure */
1116         if( New_Server_Idx <= NONE ) return;
1117
1118         if( strcasecmp( Var, "Host" ) == 0 ) {
1119                 /* Hostname of the server */
1120                 len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
1121                 if (len >= sizeof( New_Server.host ))
1122                         Config_Error_TooLong ( Line, Var );
1123                 return;
1124         }
1125         if( strcasecmp( Var, "Name" ) == 0 ) {
1126                 /* Name of the server ("Nick"/"ID") */
1127                 len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
1128                 if (len >= sizeof( New_Server.name ))
1129                         Config_Error_TooLong( Line, Var );
1130                 return;
1131         }
1132         if (strcasecmp(Var, "Bind") == 0) {
1133                 if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
1134                         return;
1135
1136                 Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
1137                                 NGIRCd_ConfFile, Line, Arg);
1138                 return;
1139         }
1140         if( strcasecmp( Var, "MyPassword" ) == 0 ) {
1141                 /* Password of this server which is sent to the peer */
1142                 if (*Arg == ':') {
1143                         Config_Error(LOG_ERR,
1144                                 "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
1145                                                                                 NGIRCd_ConfFile, Line);
1146                 }
1147                 len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
1148                 if (len >= sizeof( New_Server.pwd_in ))
1149                         Config_Error_TooLong( Line, Var );
1150                 return;
1151         }
1152         if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
1153                 /* Passwort of the peer which must be received */
1154                 len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
1155                 if (len >= sizeof( New_Server.pwd_out ))
1156                         Config_Error_TooLong( Line, Var );
1157                 return;
1158         }
1159         if( strcasecmp( Var, "Port" ) == 0 ) {
1160                 /* Port to which this server should connect */
1161                 port = atol( Arg );
1162                 if( port > 0 && port < 0xFFFF )
1163                         New_Server.port = (UINT16)port;
1164                 else
1165                         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
1166                                                                                 NGIRCd_ConfFile, Line, port );
1167                 return;
1168         }
1169 #ifdef SSL_SUPPORT
1170         if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
1171                 New_Server.SSLConnect = Check_ArgIsTrue(Arg);
1172                 return;
1173         }
1174 #endif
1175         if( strcasecmp( Var, "Group" ) == 0 ) {
1176                 /* Server group */
1177 #ifdef HAVE_ISDIGIT
1178                 if( ! isdigit( (int)*Arg ))
1179                         Config_Error_NaN( Line, Var );
1180                 else
1181 #endif
1182                 New_Server.group = atoi( Arg );
1183                 return;
1184         }
1185         if( strcasecmp( Var, "Passive" ) == 0 ) {
1186                 if (Check_ArgIsTrue(Arg))
1187                         New_Server.flags |= CONF_SFLAG_DISABLED;
1188                 return;
1189         }
1190         if (strcasecmp(Var, "ServiceMask") == 0) {
1191                 len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
1192                               sizeof(New_Server.svs_mask));
1193                 if (len >= sizeof(New_Server.svs_mask))
1194                         Config_Error_TooLong(Line, Var);
1195                 return;
1196         }
1197
1198         Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
1199                                                                 NGIRCd_ConfFile, Line, Var );
1200 } /* Handle_SERVER */
1201
1202
1203 static bool
1204 Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
1205 {
1206         size_t size = sizeof(new_chan->name);
1207         char *dest = new_chan->name;
1208
1209         if (!Channel_IsValidName(name)) {
1210                 /*
1211                  * maybe user forgot to add a '#'.
1212                  * This is only here for user convenience.
1213                  */
1214                 *dest = '#';
1215                 --size;
1216                 ++dest;
1217         }
1218         return size > strlcpy(dest, name, size);
1219 }
1220
1221
1222 static void
1223 Handle_CHANNEL(int Line, char *Var, char *Arg)
1224 {
1225         size_t len;
1226         size_t chancount;
1227         struct Conf_Channel *chan;
1228
1229         assert( Line > 0 );
1230         assert( Var != NULL );
1231         assert( Arg != NULL );
1232         assert(Conf_Channel_Count > 0);
1233
1234         chancount = Conf_Channel_Count - 1;
1235
1236         chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
1237         if (!chan) {
1238                 Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
1239                 return;
1240         }
1241         if (strcasecmp(Var, "Name") == 0) {
1242                 if (!Handle_Channelname(chan, Arg))
1243                         Config_Error_TooLong(Line, Var);
1244                 return;
1245         }
1246         if (strcasecmp(Var, "Modes") == 0) {
1247                 /* Initial modes */
1248                 len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
1249                 if (len >= sizeof(chan->modes))
1250                         Config_Error_TooLong( Line, Var );
1251                 return;
1252         }
1253         if( strcasecmp( Var, "Topic" ) == 0 ) {
1254                 /* Initial topic */
1255                 len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
1256                 if (len >= sizeof(chan->topic))
1257                         Config_Error_TooLong( Line, Var );
1258                 return;
1259         }
1260         if( strcasecmp( Var, "Key" ) == 0 ) {
1261                 /* Initial Channel Key (mode k) */
1262                 len = strlcpy(chan->key, Arg, sizeof(chan->key));
1263                 if (len >= sizeof(chan->key))
1264                         Config_Error_TooLong(Line, Var);
1265                 return;
1266         }
1267         if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
1268                 /* maximum user limit, mode l */
1269                 chan->maxusers = (unsigned long) atol(Arg);
1270                 if (chan->maxusers == 0)
1271                         Config_Error_NaN(Line, Var);
1272                 return;
1273         }
1274         if (strcasecmp(Var, "KeyFile") == 0) {
1275                 /* channel keys */
1276                 len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
1277                 if (len >= sizeof(chan->keyfile))
1278                         Config_Error_TooLong(Line, Var);
1279                 return;
1280         }
1281
1282         Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
1283                                                                 NGIRCd_ConfFile, Line, Var );
1284 } /* Handle_CHANNEL */
1285
1286
1287 static bool
1288 Validate_Config(bool Configtest, bool Rehash)
1289 {
1290         /* Validate configuration settings. */
1291
1292 #ifdef DEBUG
1293         int i, servers, servers_once;
1294 #endif
1295         bool config_valid = true;
1296         char *ptr;
1297
1298         /* Validate configured server name, see RFC 2812 section 2.3.1 */
1299         ptr = Conf_ServerName;
1300         do {
1301                 if (*ptr >= 'a' && *ptr <= 'z') continue;
1302                 if (*ptr >= 'A' && *ptr <= 'Z') continue;
1303                 if (*ptr >= '0' && *ptr <= '9') continue;
1304                 if (ptr > Conf_ServerName) {
1305                         if (*ptr == '.' || *ptr == '-')
1306                                 continue;
1307                 }
1308                 Conf_ServerName[0] = '\0';
1309                 break;
1310         } while (*(++ptr));
1311
1312         if (!Conf_ServerName[0]) {
1313                 /* No server name configured! */
1314                 config_valid = false;
1315                 Config_Error(LOG_ALERT,
1316                              "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
1317                              NGIRCd_ConfFile);
1318                 if (!Configtest && !Rehash) {
1319                         Config_Error(LOG_ALERT,
1320                                      "%s exiting due to fatal errors!",
1321                                      PACKAGE_NAME);
1322                         exit(1);
1323                 }
1324         }
1325
1326         if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
1327                 /* No dot in server name! */
1328                 config_valid = false;
1329                 Config_Error(LOG_ALERT,
1330                              "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
1331                              NGIRCd_ConfFile);
1332                 if (!Configtest) {
1333                         Config_Error(LOG_ALERT,
1334                                      "%s exiting due to fatal errors!",
1335                                      PACKAGE_NAME);
1336                         exit(1);
1337                 }
1338         }
1339
1340 #ifdef STRICT_RFC
1341         if (!Conf_ServerAdminMail[0]) {
1342                 /* No administrative contact configured! */
1343                 config_valid = false;
1344                 Config_Error(LOG_ALERT,
1345                              "No administrator email address configured in \"%s\" ('AdminEMail')!",
1346                              NGIRCd_ConfFile);
1347                 if (!Configtest) {
1348                         Config_Error(LOG_ALERT,
1349                                      "%s exiting due to fatal errors!",
1350                                      PACKAGE_NAME);
1351                         exit(1);
1352                 }
1353         }
1354 #endif
1355
1356         if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
1357             && !Conf_ServerAdminMail[0]) {
1358                 /* No administrative information configured! */
1359                 Config_Error(LOG_WARNING,
1360                              "No administrative information configured but required by RFC!");
1361         }
1362
1363 #ifdef DEBUG
1364         servers = servers_once = 0;
1365         for (i = 0; i < MAX_SERVERS; i++) {
1366                 if (Conf_Server[i].name[0]) {
1367                         servers++;
1368                         if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
1369                                 servers_once++;
1370                 }
1371         }
1372         Log(LOG_DEBUG,
1373             "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
1374             Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
1375 #endif
1376
1377         return config_valid;
1378 } /* Validate_Config */
1379
1380
1381 static void
1382 Config_Error_TooLong ( const int Line, const char *Item )
1383 {
1384         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
1385 }
1386
1387
1388 static void
1389 Config_Error_NaN( const int Line, const char *Item )
1390 {
1391         Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
1392                                                 NGIRCd_ConfFile, Line, Item );
1393 }
1394
1395
1396 #ifdef PROTOTYPES
1397 static void Config_Error( const int Level, const char *Format, ... )
1398 #else
1399 static void Config_Error( Level, Format, va_alist )
1400 const int Level;
1401 const char *Format;
1402 va_dcl
1403 #endif
1404 {
1405         /* Error! Write to console and/or logfile. */
1406
1407         char msg[MAX_LOG_MSG_LEN];
1408         va_list ap;
1409
1410         assert( Format != NULL );
1411
1412 #ifdef PROTOTYPES
1413         va_start( ap, Format );
1414 #else
1415         va_start( ap );
1416 #endif
1417         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
1418         va_end( ap );
1419         
1420         /* During "normal operations" the log functions of the daemon should
1421          * be used, but during testing of the configuration file, all messages
1422          * should go directly to the console: */
1423         if (Use_Log) Log( Level, "%s", msg );
1424         else puts( msg );
1425 } /* Config_Error */
1426
1427
1428 static void
1429 Init_Server_Struct( CONF_SERVER *Server )
1430 {
1431         /* Initialize server configuration structur to default values */
1432
1433         assert( Server != NULL );
1434
1435         memset( Server, 0, sizeof (CONF_SERVER) );
1436
1437         Server->group = NONE;
1438         Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
1439
1440         if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
1441
1442         Resolve_Init(&Server->res_stat);
1443         Server->conn_id = NONE;
1444         memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
1445 } /* Init_Server_Struct */
1446
1447
1448 /* -eof- */