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