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