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