]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
- #include's fuer stdlib.h ergaenzt.
[ngircd-alex.git] / src / ngircd / conf.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001 by Alexander Barton (alex@barton.de)
4  *
5  * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6  * der GNU General Public License (GPL), wie von der Free Software Foundation
7  * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8  * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10  * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: conf.c,v 1.7 2002/01/01 18:25:44 alex Exp $
13  *
14  * conf.h: Konfiguration des ngircd
15  *
16  * $Log: conf.c,v $
17  * Revision 1.7  2002/01/01 18:25:44  alex
18  * - #include's fuer stdlib.h ergaenzt.
19  *
20  * Revision 1.6  2001/12/31 02:18:51  alex
21  * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
22  * - neuen Header "defines.h" mit (fast) allen Konstanten.
23  * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
24  *
25  * Revision 1.5  2001/12/30 19:26:11  alex
26  * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
27  *
28  * Revision 1.4  2001/12/26 22:48:53  alex
29  * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
30  *
31  * Revision 1.3  2001/12/26 14:45:37  alex
32  * - "Code Cleanups".
33  *
34  * Revision 1.2  2001/12/26 03:19:57  alex
35  * - erste Konfigurations-Variablen definiert: PING/PONG-Timeout.
36  *
37  * Revision 1.1  2001/12/12 17:18:20  alex
38  * - Modul fuer Server-Konfiguration begonnen.
39  */
40
41
42 #include <portab.h>
43 #include "global.h"
44
45 #include <imp.h>
46 #include <assert.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include "client.h"
53 #include "log.h"
54 #include "tool.h"
55
56 #include <exp.h>
57 #include "conf.h"
58
59
60 LOCAL VOID Read_Config( VOID );
61 LOCAL VOID Validate_Config( VOID );
62
63
64 GLOBAL VOID Conf_Init( VOID )
65 {
66         /* Konfigurationsvariablen initialisieren: zunaechst Default-
67          * Werte setzen, dann Konfigurationsdtaei einlesen. */
68
69         strcpy( Conf_File, "/usr/local/etc/ngircd.conf" );
70
71         strcpy( Conf_ServerName, "" );
72         strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
73
74         strcpy( Conf_Oper, "" );
75         strcpy( Conf_OperPwd, "" );
76
77         strcpy( Conf_MotdFile, "/usr/local/etc/ngircd.motd" );
78
79         Conf_ListenPorts_Count = 0;
80         
81         Conf_PingTimeout = 120;
82         Conf_PongTimeout = 10;
83
84         /* Konfigurationsdatei einlesen und validieren */
85         Read_Config( );
86         Validate_Config( );
87 } /* Config_Init */
88
89
90 GLOBAL VOID Conf_Exit( VOID )
91 {
92         /* ... */
93 } /* Config_Exit */
94
95
96 LOCAL VOID Read_Config( VOID )
97 {
98         /* Konfigurationsdatei einlesen. */
99
100         CHAR str[LINE_LEN], *var, *arg, *ptr;
101         BOOLEAN ok;
102         INT32 port;
103         INT line;
104         FILE *fd;
105
106         fd = fopen( Conf_File, "r" );
107         if( ! fd )
108         {
109                 /* Keine Konfigurationsdatei gefunden */
110                 Log( LOG_ALERT, "Can't read configuration \"%s\": %s", Conf_File, strerror( errno ));
111                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
112                 exit( 1 );
113         }
114
115         line = 0;
116         while( TRUE )
117         {
118                 ok = FALSE;
119
120                 if( ! fgets( str, LINE_LEN, fd )) break;
121                 ngt_TrimStr( str );
122                 line++;
123
124                 /* Kommentarzeilen und leere Zeilen ueberspringen */
125                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
126
127                 ptr = strchr( str, '=' );
128                 if( ! ptr )
129                 {
130                         Log( LOG_ERR, "%s, line %d: Syntax error!", Conf_File, line );
131                         continue;
132                 }
133                 *ptr = '\0';
134
135                 var = str; ngt_TrimStr( var );
136                 arg = ptr + 1; ngt_TrimStr( arg );
137                 
138                 if( strcasecmp( str, "ServerName" ) == 0 )
139                 {
140                         /* Der Server-Name */
141                         strncpy( Conf_ServerName, arg, CLIENT_ID_LEN );
142                         Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
143                         ok = TRUE;
144                 }
145                 else if( strcasecmp( str, "ServerInfo" ) == 0 )
146                 {
147                         /* Server-Info-Text */
148                         strncpy( Conf_ServerInfo, arg, CLIENT_INFO_LEN );
149                         Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
150                         ok = TRUE;
151                 }
152                 else if( strcasecmp( str, "Operator" ) == 0 )
153                 {
154                         /* Name des IRC Operator */
155                         strncpy( Conf_Oper, arg, CLIENT_PASS_LEN );
156                         Conf_Oper[CLIENT_PASS_LEN - 1] = '\0';
157                         ok = TRUE;
158                 }
159                 else if( strcasecmp( str, "OperatorPwd" ) == 0 )
160                 {
161                         /* Passwort des IRC Operator */
162                         strncpy( Conf_OperPwd, arg, CLIENT_PASS_LEN );
163                         Conf_OperPwd[CLIENT_PASS_LEN - 1] = '\0';
164                         ok = TRUE;
165                 }
166                 else if( strcasecmp( str, "ListenPorts" ) == 0 )
167                 {
168                         /* Ports, durch "," getrennt, auf denen der Server
169                          * Verbindungen annehmen soll */
170                         ptr = strtok( arg, "," );
171                         while( ptr )
172                         {
173                                 ngt_TrimStr( ptr );
174                                 port = atol( ptr );
175                                 if( Conf_ListenPorts_Count + 1 > LISTEN_PORTS ) Log( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
176                                 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = port;
177                                 else Log( LOG_ERR, "Illegal port number: %ld. Ignored.", port );
178                                 ptr = strtok( NULL, "," );
179                         }
180                         ok = TRUE;
181                 }
182                 else if( strcasecmp( str, "MotdFile" ) == 0 )
183                 {
184                         /* Datei mit der "message of the day" (MOTD) */
185                         strncpy( Conf_MotdFile, arg, FNAME_LEN );
186                         Conf_MotdFile[FNAME_LEN - 1] = '\0';
187                         ok = TRUE;
188                 }
189                 else if( strcasecmp( str, "PingTimeout" ) == 0 )
190                 {
191                         /* PING-Timeout */
192                         Conf_PingTimeout = atoi( arg );
193                         if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
194                         ok = TRUE;
195                 }
196                 else if( strcasecmp( str, "PongTimeout" ) == 0 )
197                 {
198                         /* PONG-Timeout */
199                         Conf_PongTimeout = atoi( arg );
200                         if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
201                         ok = TRUE;
202                 }
203                 
204                 if( ! ok ) Log( LOG_ERR, "%s, line %d: Unknown variable \"%s\"!", Conf_File, line, var );
205         }
206         
207         fclose( fd );
208 } /* Read_Config */
209
210
211 LOCAL VOID Validate_Config( VOID )
212 {
213         /* Konfiguration ueberpruefen */
214         
215         if( ! Conf_ServerName[0] )
216         {
217                 /* Kein Servername konfiguriert */
218                 Log( LOG_ALERT, "No server name configured (use \"ServerName\")!", Conf_File, strerror( errno ));
219                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
220                 exit( 1 );
221         }
222 } /* Validate_Config */
223
224 /* -eof- */