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