1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
/*
* RadiusClass -- An C++-Library for radius authentication
* and accounting.
*
* Copyright (C) 2005 EWE TEL GmbH/Ralf Luebben <ralfluebben@gmx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "RadiusConfig.h"
/** The constructor The constructor initializes all char arrays with 0.
*/
RadiusConfig::RadiusConfig(void)
{
memset(this->nasPortType,0,2);
memset(this->framedProtocol,0,2);
memset(this->serviceType,0,2);
memset(this->nasIdentifier,0,128);
memset(this->nasIpAddress,0,16);
}
/** The constructor initializes all char arrays with 0. After the initialization
* the configfile is parsed and the information which are
* found are copied to the attributes.
* @param configfile The name of the configfile.
*/
RadiusConfig::RadiusConfig(string configfile)
{
memset(this->nasPortType,0,2);
memset(this->framedProtocol,0,2);
memset(this->serviceType,0,2);
memset(this->nasIdentifier,0,128);
memset(this->nasIpAddress,0,16);
this->parseConfigFile(configfile.c_str());
}
/** The destructur clears the serverlist. */
RadiusConfig::~RadiusConfig(void)
{
server.clear();
}
/** The getter method for the radius server list.
* @return The server list.*/
list<RadiusServer> * RadiusConfig::getRadiusServer(void)
{
return (&server);
}
/** The method parse the configfile for attributes and
* radius server, the attributes are copied to the
* member variables.
* @param configfile The name of the configfile.
* @return An integer, 0 if everything is ok
* or PARSING_ERROR or BAD_FILE if something is wrong.*/
int RadiusConfig::parseConfigFile(const char * configfile)
{
string line;
RadiusServer *tmpServer=NULL;
ifstream file;
file.open(configfile, ios::in);
if (file.is_open())
{
while (file.eof()==false)
{
getline(file,line);
this->deletechars(&line);
if (strncmp(line.c_str(),"Framed-Protocol=",16)==0)
{
if((line.size()-16)>2)
{
return BAD_FILE;
}
line.copy(this->framedProtocol,line.size()-16,16);
}
if (strncmp(line.c_str(),"NAS-Port-Type=",14)==0)
{
if((line.size()-14)>1)
{
return BAD_FILE;
}
line.copy(this->nasPortType,line.size()-14,14);
}
if (strncmp(line.c_str(),"Service-Type=",13)==0)
{
if((line.size()-13)>1)
{
return BAD_FILE;
}
line.copy(this->serviceType,line.size()-13,13);
}
if (strncmp(line.c_str(),"NAS-Identifier=",15)==0)
{
if((line.size()-15)>127)
{
return BAD_FILE;
}
line.copy(this->nasIdentifier,line.size()-15,15);
}
if (strncmp(line.c_str(),"NAS-IP-Address=",15)==0)
{
if((line.size()-15)>15)
{
return BAD_FILE;
}
line.copy(this->nasIpAddress,line.size()-15,15);
}
if(strncmp(line.c_str(),"server",6)==0)
{
tmpServer=new RadiusServer;
while((line.find("{")==string::npos) && (file.eof()==false))
{
getline(file,line);
deletechars(&line);
if(line.find_first_not_of("}"))
{
return PARSING_ERROR;
}
}
while (strstr(line.c_str(),"}")==NULL && (file.eof()==false))
{
getline(file,line);
deletechars(&line);
if (strncmp(line.c_str(),"authport=",9)==0)
{
tmpServer->setAuthPort(atoi(line.substr(9,4).c_str()));
}
if (strncmp(line.c_str(),"acctport=",9)==0)
{
tmpServer->setAcctPort(atoi(line.substr(9,4).c_str()));
}
if (strncmp(line.c_str(),"name=",5)==0)
{
tmpServer->setName(line.substr(5));
}
if (strncmp(line.c_str(),"retry=",6)==0)
{
tmpServer->setRetry(atoi(line.substr(6).c_str()));
}
if (strncmp(line.c_str(),"sharedsecret=",13)==0)
{
tmpServer->setSharedSecret(line.substr(13));
}
if (strncmp(line.c_str(),"wait=",5)==0)
{
tmpServer->setWait(atoi(line.substr(5).c_str()));
}
if (strncmp(line.c_str(),"requirema=",10)==0)
{
if (strcmp(line.substr(10).c_str(),"yes") == 0)
tmpServer->setRequireMA(1);
else if (strcmp(line.substr(10).c_str(),"no") == 0)
tmpServer->setRequireMA(0);
else if (strcmp(line.substr(10).c_str(),"auto") == 0)
tmpServer->setRequireMA(-1);
else
return PARSING_ERROR;
}
}
if(strstr(line.c_str(),"}"))
{
this->server.push_back(*tmpServer);
}
//No "}" was found - something in config is wrong
else
{
return PARSING_ERROR;
}
if (tmpServer!=NULL)
{
delete tmpServer;
}
}
}
file.close();
}
else
{
return BAD_FILE;
}
return 0;
}
/** The method deletes chars from a string.
* This is used to delete tabs, spaces, # and '\0'
* from a string.
* @param text The string which should be cleaned.
*/
/** The method deletes chars from a string.
* This is used to delete tabs, spaces, # and '\0'
* from a string.
* @param text The string which should be cleaned.
*/
void RadiusConfig::deletechars(string * line)
{
char const* delims = " \t\r\n\0";
// trim leading whitespace
string::size_type pos = line->find_first_not_of(delims);
if (pos != string::npos) line->erase(0,pos );
// trim trailing whitespace
pos = line->find_last_not_of(delims);
if (pos != string::npos) line->erase(pos+1);
//trim whitespaces in line
pos = line->find_first_of(delims);
while (pos != string::npos)
{
line->erase(pos);
pos = line->find_first_of(delims);
}
// trim comments
pos = line->find_first_of("#");
if (pos != string::npos) line->erase(pos);
}
/**The method finds the part of the string after the
* '=' and puts it in the value.
* @param text The string with the value.
* @param value The value where to put the part of the string. */
void RadiusConfig::getValue(const char * text, char * value)
{
int i=0,j=0;
while (text[i]!='=' && text[i]!='\0')
{
i++;
}
i++;
while (text[i]!='\0')
{
value[j]=text[i];
i++;
j++;
}
value[j]='\0';
}
/** The setter method for the service type.
* @param type A string with the service type.*/
void RadiusConfig::setServiceType(char * type)
{
strncpy(this->serviceType, type, 2);
}
/** The getter method for the service type
* @return A pointer to the service type.*/
char * RadiusConfig::getServiceType(void)
{
return this->serviceType;
}
/** The setter method for the framed protocol.
* @param proto A string with the framed protocol.*/
void RadiusConfig::setFramedProtocol(char * proto)
{
strncpy(this->framedProtocol, proto, 2);
}
/**The getter method for the framed protocol
* @return A pointer to the framed protocol.
*/
char * RadiusConfig::getFramedProtocol(void)
{
return this->framedProtocol;
}
/** The setter method for the nas port type.
* @param type A string with the nas port type.
*/
void RadiusConfig::setNASPortType(char * type)
{
strncpy(this->nasPortType, type, 2);
}
/** The getter method for the nas port type.
* @return A pointer to the nas port type.
*/
char * RadiusConfig::getNASPortType(void)
{
return this->nasPortType;
}
/** The setter method for the nas identifier.
* @param identifier A string with the identifier.
*/
void RadiusConfig::setNASIdentifier(char * identifier)
{
strncpy(this->nasIdentifier,identifier, 128);
}
/** The getter method for the nas identifier.
* @return A pointer to the nas identifier.
*/
char * RadiusConfig::getNASIdentifier(void)
{
return this->nasIdentifier;
}
/** The setter method for the nas ip address.
* @param ip A string with ip address.
*/
void RadiusConfig::setNASIpAddress(char * ip)
{
strncpy(this->nasIpAddress,ip, 16);
}
/** The getter method for the nas ip address.
* @return A pointer to the nas ipaddress.
*/
char * RadiusConfig::getNASIpAddress(void)
{
return this->nasIpAddress;
}
ostream& operator << (ostream& os, RadiusConfig& config)
{
list<RadiusServer> * serverlist;
list<RadiusServer>::iterator server;
os << "RadiusConfig: \n";
os << "\nFramedProtocol: " << config.getFramedProtocol();
os << "\nNASIdentifier: "<< config.getNASIdentifier();
os << "\nNASIpAdress: "<< config.getNASIpAddress();
os << "\nNASPortTyoe: "<< config.getNASPortType();
os << "\nServiceType: " << config.getServiceType();
//get the server list
serverlist=config.getRadiusServer();
//set server to the first server
server=serverlist->begin();
while(server != serverlist->end())
{
cout << *server;
server++;
}
return os;
}
|