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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
|
/*
* 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 "RadiusAttribute.h"
#include <stdlib.h>
#include "error.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define NEED_LIBGCRYPT_VERSION "1.2.0"
GCRY_THREAD_OPTION_PTHREAD_IMPL;
/** The constructor sets the type to 0 and the value to NULL.*/
RadiusAttribute::RadiusAttribute(void)
{
this->type=0;
this->length=0;
this->value=NULL;
}
/** The constructor creates an attribute.
* The type and the value can be set.
* @param Octet type : The type of the attribute.
* @param char *value : A pointer to a value for the attribut.
*/
RadiusAttribute::RadiusAttribute(Octet ty, const char *value)
{
this->type=ty;
this->value=NULL;
//Only set the value if there is something in.
if (value!=NULL)
{
this->setValue(value);
}
}
/**The construcotr sets the type. The value is set to NULL.
* @param Octet typ : The type of the attribute.*/
RadiusAttribute::RadiusAttribute(Octet typ)
{
this->type=typ;
this->length=0;
this->value=NULL;
}
/**The constructor sets the type and the value.
* @param Octet typ : The type of the attribute.
* @param string str : The value as a string.
*/
RadiusAttribute::RadiusAttribute(Octet typ, string str)
{
this->type=typ;
this->value=NULL;
this->setValue(str);
}
/** The constructor sets the type and the value. The type must
* have the datatype integer as it is defined in the RFC of the
* radius protocol.
* @param Octet type : The type of the packet.
* @param int value : The value as an integer.
*/
RadiusAttribute::RadiusAttribute(Octet typ, uint32_t value)
{
this->type=typ;
this->value=NULL;
this->setValue(value);
}
/** The destructor of the class.
* It frees the allocated memory for the value, if the pointer is not NULL.
*/
RadiusAttribute::~RadiusAttribute(void)
{
if (this->value)
{
delete [] this->value;
}
}
/** Creates a dump of an attribute.
*/
void RadiusAttribute::dumpRadiusAttrib(void)
{
int i;
fprintf(stdout,"\ttype\t\t:\t%d\t|",this->type);
fprintf(stdout,"\tlength\t:\t%d\t|",this->getLength());
fprintf(stdout,"\tvalue\t:\t'");
for(i=0;i<((this->getLength())-2);i++)
fputc(this->value[i],stdout);
fprintf(stdout,"'\n");
}
/** The getter method for the length of the attribute
* @return The length as an integer.
*/
int RadiusAttribute::getLength(void)
{
return (this->length);
}
Octet * RadiusAttribute::getLength_Octet(void)
{
return (&this->length);
}
/** The setter method for the length of the attribut.
* Normally it calculated automatically.
* @param len The length as datatype unsigned char (=Octet).
*/
void RadiusAttribute::setLength(Octet len)
{
this->length=len;
}
/** Creates a password buffer with MD5/xOR hashing for the
* ATTRIB_User_Password. The password filed must be have a
* length of 16 Octets or a multiple of 16 Octets.
* If the password is longer than 16 characters, the XOR-hash is
* build of the first 16 chars, than a new XOR-hash is build
* over the first XOR-hash and the shared secret and so on.
* It if defined in the radius RFC.
* @param password The User password.
* @param hpassword A char array for the hashed password. It must have the
* same length as the password filed (=this->length-2).
* @param sharedSecret The sharedsecret of the server.
* @param authenticator String of the authenticator field.
* @return A pointer to the hpassword array, so the function can
* be used directly in a function/method.
*/
char * RadiusAttribute::makePasswordHash(const char *password,char * hpassword, const char *sharedSecret,const char *authenticator)
{
unsigned char digest[MD5_DIGEST_LENGTH]; //The digest.
gcry_md_hd_t context; //the hash context
int i,k,j,l, //Some counters.
passwordlen; //The password length.
memset(digest,0,MD5_DIGEST_LENGTH);
//build the hash
if (!gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
{ /* No other library has already initialized libgcrypt. */
gcry_control(GCRYCTL_SET_THREAD_CBS,&gcry_threads_pthread);
if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
{
cerr << "libgcrypt is too old (need " << NEED_LIBGCRYPT_VERSION << ", have " << gcry_check_version (NULL) << ")\n";
}
/* Disable secure memory. */
gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
}
gcry_md_open(&context, GCRY_MD_MD5, 0);
gcry_md_write(context, sharedSecret, strlen(sharedSecret));
gcry_md_write(context, authenticator, MD5_DIGEST_LENGTH);
memcpy(digest, gcry_md_read(context, GCRY_MD_MD5), MD5_DIGEST_LENGTH);
if (this->length<MD5_DIGEST_LENGTH)
{
//XOR the password and the digest
for(i=0;i<MD5_DIGEST_LENGTH;i++) hpassword[i]=password[i]^digest[i];
}
else
{
passwordlen=this->length-2; //get the length of the passwordfield
//XOR the password and the digest
//build the first xOR-hash
for(i=0;i<MD5_DIGEST_LENGTH;i++)
{
hpassword[i]=password[i]^digest[i];
}
passwordlen=passwordlen-MD5_DIGEST_LENGTH; //the next 16 charakters
k=0; //the first loop
while (passwordlen>0)
{
//build the next hash
memset(digest,0,MD5_DIGEST_LENGTH);
//put the hash of the last XOR in the digest
//build the hash
if (!gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
{ /* No other library has already initialized libgcrypt. */
gcry_control(GCRYCTL_SET_THREAD_CBS,&gcry_threads_pthread);
if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
{
cerr << "libgcrypt is too old (need " << NEED_LIBGCRYPT_VERSION << ", have " << gcry_check_version (NULL) << ")\n";
}
/* Disable secure memory. */
gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
}
gcry_md_open (&context, GCRY_MD_MD5, 0);
gcry_md_write(context, sharedSecret, strlen(sharedSecret));
gcry_md_write(context, hpassword+(k*MD5_DIGEST_LENGTH), MD5_DIGEST_LENGTH);
memcpy(digest, gcry_md_read(context, GCRY_MD_MD5), MD5_DIGEST_LENGTH);
j=0;
l=i+MD5_DIGEST_LENGTH;
for(;i<l;i++)
{
hpassword[i]=password[i]^digest[j];
j++;
}
passwordlen=passwordlen-MD5_DIGEST_LENGTH; //and the next 16 characters
k++; //and the next loop
}
}
gcry_md_close(context);
return hpassword;
}
/** The getter method for the type of the attribute.
* @return An integer with the type.
*/
int RadiusAttribute::getType(void)
{
return (this->type);
}
Octet * RadiusAttribute::getType_Octet(void)
{
return (&this->type);
}
/** The setter method for the type of the attribute.
* @param type The type as Octet.
*/
void RadiusAttribute::setType(Octet type)
{
this->type=type;
}
/** The getter method for the value.
* @return The value as an Octet.*/
Octet * RadiusAttribute::getValue(void)
{
return (this->value);
}
/**Set the value of the attribute. The representation of the value
* is changed, so it is ready to send over the network.
* The changes depend on the datatype
* IPADRESS, INTEGER, String. The datatype enum can be treated as an integer.
* A special attribut is the User password,
* the length must be 16 octets or a multipe of 16 Octets.
* Here it is only copied to the
* value. The datatypes ipv6addr,ifid, ipv6prefix, ipv6addr are treated as
* strings.
* @param value : A pointer to the value.
* @return An integer which indicates errors, 0 if everthing is ok,
* else a number defined in the error.h
*/
int RadiusAttribute::setValue(char *value)
{
char tmpStr[20]; //An array to convert the datatype.
int i,j,q, //Some counter.
passwordlen; //The passwordlength.
//If the attribute has already an value, clear it.
if (this->value!=NULL)
{
delete [] this->value;
}
switch(this->type)
{
//for data type IPADDRESS
case ATTRIB_NAS_IP_Address:
case ATTRIB_Framed_IP_Address:
case ATTRIB_Framed_IP_Netmask:
case ATTRIB_Login_IP_Host:
//allocate memory
if(!(this->value=new Octet[4]))
{
return ALLOC_ERROR;
}
//transform the number parted by the "." in network byte order
i=0;j=0;
while(value[i]!='.' && i<3)
tmpStr[j++]=value[i++];
tmpStr[j]=0;
if (value[i]!='.')
{
return BAD_IP;
}
this->value[0]=(unsigned char)atoi(tmpStr);
j=0;
i++;
while(value[i]!='.' && i<7)
tmpStr[j++]=value[i++];
tmpStr[j]=0;
if (value[i]!='.')
{
return BAD_IP;
}
this->value[1]=(unsigned char)atoi(tmpStr);
j=0;i++;
while(value[i]!='.' && i<11)
tmpStr[j++]=value[i++];
tmpStr[j]=0;
if (value[i]!='.')
{
return BAD_IP;
}
this->value[2]=(unsigned char)atoi(tmpStr);
j=0;i++;
while(value[i] && i<15)
tmpStr[j++]=value[i++];
tmpStr[j]=0;
this->value[3]=(unsigned char)atoi(tmpStr);
this->length=4;
break;
// User-Password
case ATTRIB_User_Password:
//the minimum length is 16 Octets
if (strlen(value)<16)
{
if(!(this->value=new Octet [16]))
{
return ALLOC_ERROR;
}
memset(this->value,0,16);
memcpy(this->value, value, strlen(value));
this->length=(Octet)16;
}
//find a multiple of 16 Octets where the password fits
else
{
passwordlen=((strlen(value)-(strlen(value)%16))/16);
//if it doesn't fit, get the next bigger array.
if ((strlen(value)%16)!=0)
{
passwordlen++;
}
if(!(this->value=new Octet [passwordlen*16]))
{
return ALLOC_ERROR;
}
memset(this->value,0,(passwordlen*16));
memcpy(this->value, value, strlen(value));
this->length=(Octet)(passwordlen*16);
}
break;
case ATTRIB_Message_Authenticator:
if(!(this->value=new Octet [16]))
{
return ALLOC_ERROR;
}
memcpy(this->value, value, 16);
this->length=(Octet)16;
break;
//for datatype integer/enum
case ATTRIB_NAS_Port:
case ATTRIB_Framed_MTU:
case ATTRIB_Login_TCP_Port:
case ATTRIB_Framed_IPX_Network:
case ATTRIB_Session_Timeout:
case ATTRIB_Framed_AppleTalk_Link:
case ATTRIB_Framed_AppleTalk_Network:
case ATTRIB_Acct_Delay:
case ATTRIB_Acct_Input_Octets:
case ATTRIB_Acct_Output_Octets:
case ATTRIB_Acct_Session_Time:
case ATTRIB_Acct_Input_Packets:
case ATTRIB_Acct_Output_Packets:
case ATTRIB_Acct_Link_Count:
case ATTRIB_Port_Limit:
case ATTRIB_Service_Type:
case ATTRIB_Framed_Protocol:
case ATTRIB_Framed_Routing:
case ATTRIB_Framed_Compression:
case ATTRIB_Login_Service:
case ATTRIB_Idle_Timeout:
case ATTRIB_Termination_Action:
case ATTRIB_Acct_Status_Type:
case ATTRIB_Acct_Authentic:
case ATTRIB_Acct_Terminate_Cause:
case ATTRIB_NAS_Port_Type:
case ATTRIB_Login_LAT_Port:
case ATTRIB_ARAP_Zone_Access:
case ATTRIB_ARAP_Security:
case ATTRIB_Password_Retry:
case ATTRIB_Prompt:
case ATTRIB_Acct_Interim_Interval:
case ATTRIB_Acct_Input_Gigawords:
case ATTRIB_Acct_Output_Gigawords:
case ATTRIB_Event_Timestamp:
if(!(this->value=new Octet [4]))
{
return ALLOC_ERROR;
}
//transform the integer in the right network byte order
q=htonl(strtoul(value,NULL,10));
memcpy(this->value,&q,4);
this->length=4;
break;
//Special case vender specific, at the moment it is treated as a string.
case ATTRIB_Vendor_Specific:
if(!(this->value=new Octet [int(value[5])+4]))
{
return ALLOC_ERROR;
}
memcpy(this->value, value, int(value[5])+4);
this->length=int(value[5])+4;
break;
//String: They need only copied in a new char array.
default:
if(!(this->value=new Octet [strlen(value)]))
{
return ALLOC_ERROR;
}
memcpy(this->value, value, strlen(value));
this->length=strlen(value);
}
this->length+=sizeof(Octet)+sizeof(Octet);
return 0;
}
/** Extract the value of an received attribute in the buffer to
* the value field of an attribute. The order is still in network
* order. If you want to get it maybe as a char or integer you
* must to know which type it is. So you know the datatype and you
* can convert it. But this done in another method.
* @param value A pointer to the value which is copied to the value of the attribute.
* @return An integer which indicates errors, 0 if everthing is ok,
* else a number defined in the error.h.
*/
int RadiusAttribute::setRecvValue(char *value)
{
if(!(this->value=new Octet[this->length-2]))
{
return ALLOC_ERROR;
}
memcpy(this->value, value, (this->length-2));
return 0;
}
/** Transform a attribute value to an integer, this makes only sense
* if the datatype is an integer. This dependents on the definition
* in the radius RFC or can be locked up in the file radius.h of this
* source code.
* @return The transformed integer.
*/
int RadiusAttribute::intFromBuf(void)
{
return (ntohl(*(int*)this->value));
}
/**The overloading of the assignment operator.*/
RadiusAttribute & RadiusAttribute::operator=(const RadiusAttribute &ra)
{
this->value=new Octet[ra.length-2];
this->type=ra.type;
this->length=ra.length;
memcpy(this->value,ra.value,ra.length-2);
return *this;
}
/**The copy constructor.*/
RadiusAttribute::RadiusAttribute(const RadiusAttribute &ra)
{
this->value=new Octet[ra.length-2];
this->type=ra.type;
this->length=ra.length;
memcpy(this->value,ra.value,ra.length-2);
}
/**The method sets the value. Internal it converts the string
* into a char array and calls setValue(char *).
* @param s The value as a string.
* @return An integer. 0 if everything is ok, else !=0.
*/
int RadiusAttribute::setValue(string s)
{
int ret;
char * value=new char[s.size()+1];
memset (value,0,s.size()+1);
strncpy(value,s.c_str(),s.size());
ret=setValue(value);
delete [] value;
return ret;
}
/** The method sets the value for an integer. The method
* writes the integer in a string and calls the method
* setValue(char *).
* @param value The value as an integer.
* @return An integer, 0 if everything is ok, else !=0.
*/
int RadiusAttribute::setValue(uint32_t value)
{
char num[11];
memset(num,0,11);
sprintf(num,"%u",value);
return setValue(num);
}
/** The method converts the value into an ip.
* The attribute must have the right datatype IPADDRESS.
* @return The ip address as a string.
*/
string RadiusAttribute::ipFromBuf(void)
{
int num,i;
char ip2[4],ip3[16];
memset(ip3,0,16);
for (i=0;i<(this->length-2);i++)
{
num=(int)this->value[i];
if(i==0)
{
sprintf(ip3,"%i",num);
strcat(ip3,".");
}
else if (i<3)
{
sprintf(ip2,"%i",num);
strcat(ip3,ip2);
strcat(ip3,".");
}
else
{
sprintf(ip2,"%i",num);
strcat(ip3,ip2);
}
}
return string(ip3);
}
/** The method converts the value into an IPv6.
* The attribute must have the right datatype IPADDRESS6.
* @return The ip address as a string.
*/
string RadiusAttribute::ip6FromBuf(void)
{
int num,i,len;
char ip2[3],ip3[40];
memset(ip3,0,40);
len=(this->length-2);
if(len>16)
len=16;
for (i=0;i<len;i++)
{
num=(int)this->value[i];
sprintf(ip2,"%02x",num);
strcat(ip3,ip2);
if((i%2)==1 && i<len-1)
strcat(ip3,":");
}
return string(ip3);
}
|