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