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