00001 #include <ctype.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004
00005 #include <iostream>
00006 #include <fstream>
00007
00008 #include "config.h"
00009 #include "Debug.h"
00010 #include "Lexer.h"
00011 #include "Config.h"
00012 #include "NServer.h"
00013
00014 using namespace std;
00015
00016
00017
00018
00019 #define STRNCPY(dest,source) \
00020 if(source.length()>=sizeof(dest))\
00021 throw SyntaxError(lex,"argument too long", ERROR_LOCATION);\
00022 strcpy(dest,source.c_str());
00023
00024 #define HANDLESTRGARG(name) \
00025 if(tok==#name) {\
00026 a1=lex.getToken();\
00027 STRNCPY(name,a1);\
00028 }
00029
00030 #define HANDLEINTARG(name) \
00031 if(tok==#name) {\
00032 a1=lex.getToken();\
00033 name=atoi(a1.c_str());\
00034 }
00035
00036
00037 Config::Config()
00038 {
00039 init();
00040 }
00041
00042 Config::Config(const char *fn)
00043 {
00044 init();
00045 read(fn);
00046 }
00047
00048 void Config::init(void)
00049 {
00050
00051 Username[0] = Groupname[0] = '\0';
00052 strcpy(Admin, "notset");
00053
00054 ServerType = standalone;
00055 strcpy(Hostname, getfqdn());
00056 CachePort[0] = '\0';
00057 strcpy (ListenTo, "DEFAULT");
00058
00059 LogDirectory[0] = '\0';
00060 LogStyle = LogName + LogAddr;
00061 strcpy(SpoolDirectory, "/var/cache/newscache");
00062 SpoolSize = 1024 * 1024;
00063 PrefetchFile[0] = PidFile[0] = '\0';
00064
00065 ClientTimeout = 1200;
00066 MaxConnections = 32;
00067
00068 NiceServer = 0;
00069 NiceClient = 10;
00070 NiceClean = 3;
00071
00072 ttl_list = 3600;
00073 ttl_desc = 3600;
00074
00075 clnts.init();
00076 srvrs.init();
00077
00078 }
00079
00080 MPListEntry *Config::server(const char *group)
00081 {
00082 return srvrs.server(group);
00083 }
00084
00085 AccessEntry *Config::client(const char *name, struct in_addr addr)
00086 {
00087 return clnts.client(name, addr);
00088 }
00089
00090 void Config::read(const char *fn)
00091 {
00092 Lexer lex(fn);
00093 string tok, a1, a2, a3;
00094
00095 for (;;) {
00096 tok = lex.getToken();
00097 if (lex.eof())
00098 break;
00099
00100 HANDLESTRGARG(SpoolDirectory)
00101 else
00102 HANDLESTRGARG(LogDirectory)
00103 else
00104 HANDLESTRGARG(PrefetchFile)
00105 else
00106 HANDLESTRGARG(Username)
00107 else
00108 HANDLESTRGARG(Groupname)
00109 else
00110 HANDLESTRGARG(CachePort)
00111 else
00112 HANDLESTRGARG(ListenTo)
00113 else
00114 HANDLESTRGARG(Admin)
00115 else
00116 HANDLESTRGARG(PidFile)
00117 else
00118 HANDLESTRGARG(Hostname)
00119 else
00120 HANDLEINTARG(SpoolSize)
00121 else
00122 HANDLEINTARG(ClientTimeout)
00123 else
00124 HANDLEINTARG(MaxConnections)
00125 else
00126 HANDLEINTARG(NiceServer)
00127 else
00128 HANDLEINTARG(NiceClient)
00129 else
00130 HANDLEINTARG(NiceClean)
00131 else
00132 if (tok == "LogStyle") {
00133 LogStyle = 0;
00134 for (;;) {
00135 a1 = lex.getToken();
00136 if (a1 == "strict-inn")
00137 LogStyle |= LogINN;
00138 else if (a1 == "hostname")
00139 LogStyle |= LogName;
00140 else if (a1 == "ip-address")
00141 LogStyle |= LogAddr;
00142 else
00143 break;
00144 }
00145 lex.putbackToken(a1);
00146 } else if (tok == "ServerType") {
00147 a1 = lex.getToken();
00148 if (a1 == "inetd")
00149 ServerType = inetd;
00150 else if (a1 == "standalone")
00151 ServerType = standalone;
00152 else
00153 throw SyntaxError(lex,
00154 "illegal ServerType",
00155 ERROR_LOCATION);
00156 } else if (tok == "ConfigVersion") {
00157 a1 = lex.getToken();
00158 if (atoi(a1.c_str()) != 5)
00159 throw SyntaxError(lex,
00160 "expecting ConfigVersion 5",
00161 ERROR_LOCATION);
00162 } else if (tok == "NewsServerList") {
00163 srvrs.read(lex);
00164 } else if (tok == "AccessList") {
00165 clnts.read(lex);
00166 } else if (tok == "Timeouts") {
00167 a1 = lex.getToken();
00168 a2 = lex.getToken();
00169 ttl_list = atoi(a1.c_str());
00170 ttl_desc = atoi(a2.c_str());
00171 } else {
00172 throw SyntaxError(lex, "unknown keyword",
00173 ERROR_LOCATION);
00174 }
00175 }
00176 }
00177
00178 void Config::printParameters (ostream *pOut)
00179 {
00180 if (Username[0] != '\0') {
00181 *pOut << "Username " << Username << endl;
00182 }
00183 if (Groupname[0] != '\0') {
00184 *pOut << "Groupname " << Groupname << endl;
00185 }
00186 *pOut << "Admin " << Admin << endl;
00187 *pOut << "ServerType ";
00188 if (ServerType == inetd) {
00189 *pOut << "inetd" << endl;
00190 } else if (ServerType == standalone) {
00191 *pOut << "standalone" << endl;
00192 } else {
00193 *pOut << "UNDEFINED!" << endl;
00194 }
00195 if (CachePort[0] != '\0') {
00196 *pOut << "CachePort " << CachePort << endl;
00197 }
00198 *pOut << "ListenTo " << ListenTo << endl;
00199 if (LogDirectory[0] != '\0') {
00200 *pOut << "LogDirectory " << LogDirectory << endl;
00201 }
00202 *pOut << "SpoolDirectory " << SpoolDirectory << endl;
00203 *pOut << "LogStyle";
00204 if (LogStyle & LogINN) {
00205 *pOut << " strict-inn";
00206 }
00207 if (LogStyle & LogName) {
00208 *pOut << " hostname";
00209 }
00210 if (LogStyle & LogAddr) {
00211 *pOut << " ip-address";
00212 }
00213 *pOut << endl;
00214 *pOut << "SpoolSize " << SpoolSize << endl;
00215 if (PrefetchFile[0] != '\0') {
00216 *pOut << "PrefetchFile " << PrefetchFile << endl;
00217 }
00218 if (PidFile[0] != '\0') {
00219 *pOut << "PidFile " << PidFile << endl;
00220 }
00221 *pOut << "ClientTimeout " << ClientTimeout << endl;
00222 *pOut << "MaxConnections " << MaxConnections << endl;
00223 *pOut << "Timeouts " << ttl_list << " " << ttl_desc << endl;
00224 *pOut << "NiceServer " << NiceServer << endl;
00225 *pOut << "NiceClient " << NiceClient << endl;
00226 *pOut << "NiceClean " << NiceClean << endl;
00227
00228 clnts.printParameters (pOut);
00229 srvrs.printParameters (pOut);
00230 }