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