radius_schema.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <string>
  8. #include <dhcpsrv/testutils/radius_schema.h>
  9. #include <gtest/gtest.h>
  10. #include <fstream>
  11. #include <sstream>
  12. #include <stdlib.h>
  13. using namespace std;
  14. namespace isc {
  15. namespace dhcp {
  16. namespace test {
  17. const char* RADIUS_AUTHORIZE_FILE = "/tmp/freeradius-authorize";
  18. const char* RADIUS_VALID_TYPE = "type=radius";
  19. const char* VALID_RADIUS_REALM = "name=kea.isc.org";
  20. const char* RADIUS_REALM = "kea.isc.org";
  21. string
  22. validRadiusConnectionString() {
  23. return (connectionString(RADIUS_VALID_TYPE, VALID_RADIUS_REALM, VALID_HOST,
  24. VALID_USER, VALID_PASSWORD));
  25. }
  26. void freeradiusReload() {
  27. std::string line;
  28. ifstream myfile(RADIUS_AUTHORIZE_FILE);
  29. while(std::getline(myfile, line)) {
  30. std::cerr << line << "\n";
  31. }
  32. std::ostringstream cmd;
  33. cmd << "sudo service freeradius stop && sudo service freeradius start";
  34. int retval = ::system(cmd.str().c_str());
  35. ASSERT_EQ(0, retval) << "failed:" << cmd.str();
  36. myfile.close();
  37. }
  38. void destroyRadiusSchema(bool show_err) {
  39. }
  40. void createRadiusSchema(bool show_err) {
  41. std::ostringstream cmd;
  42. cmd << "sudo sh -c 'echo >" << RADIUS_AUTHORIZE_FILE << "'";
  43. int retval = ::system(cmd.str().c_str());
  44. ASSERT_EQ(0, retval) << "failed:" << cmd.str();
  45. freeradiusReload();
  46. }
  47. void freeradiusAdd(const HostPtr& host) {
  48. ofstream myfile;
  49. myfile.open(RADIUS_AUTHORIZE_FILE, std::ios_base::app);
  50. myfile << "# " << host->getIdentifierAsText() << "\n";
  51. if( host->getHWAddress()) {
  52. myfile << host->getHWAddress()->toText(false) ;
  53. if( RADIUS_REALM ) {
  54. myfile << "@" << RADIUS_REALM;
  55. }
  56. } else if( host->getDuid()) {
  57. myfile << host->getDuid()->toText() ;
  58. if( RADIUS_REALM ) {
  59. myfile << "@" << RADIUS_REALM;
  60. }
  61. } else {
  62. std::stringstream tmp;
  63. tmp << std::hex;
  64. bool delim = false;
  65. for (std::vector<uint8_t>::const_iterator it = host->getIdentifier().begin();
  66. it != host->getIdentifier().end(); ++it) {
  67. if (delim) {
  68. tmp << ":";
  69. }
  70. tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*it);
  71. delim = true;
  72. }
  73. myfile << tmp.str();
  74. if( RADIUS_REALM ) {
  75. myfile << "@" << RADIUS_REALM;
  76. }
  77. }
  78. myfile << " Auth-Type := Accept\n";
  79. myfile << " Service-Type = Framed-User";
  80. if(! host->getIPv4Reservation().isV4Zero()) {
  81. myfile << ",\n Framed-IP-Address = " << host->getIPv4Reservation();
  82. }
  83. IPv6ResrvRange resrv = host->getIPv6Reservations();
  84. if (std::distance(resrv.first, resrv.second) > 0) {
  85. int na = 0;
  86. int pd = 0;
  87. for (IPv6ResrvIterator resv = resrv.first; resv != resrv.second;
  88. ++resv) {
  89. if(resv->first == IPv6Resrv::TYPE_NA) {
  90. myfile << ",\n Framed-IPv6-Address:" << na++ << " = " << resv->second.getPrefix();
  91. } else {
  92. myfile << ",\n Delegated-IPv6-Prefix:" << pd++ << " = " << resv->second.toText();
  93. }
  94. }
  95. }
  96. myfile << "\n";
  97. myfile.close();
  98. freeradiusReload();
  99. }
  100. bool freeradiusDel(const SubnetID& subnet_id, const asiolink::IOAddress& addr) {
  101. }
  102. bool freeradiusDel4(const SubnetID& subnet_id,
  103. const Host::IdentifierType& identifier_type,
  104. const uint8_t* identifier_begin, const size_t identifier_len) {
  105. }
  106. bool freeradiusDel6(const SubnetID& subnet_id,
  107. const Host::IdentifierType& identifier_type,
  108. const uint8_t* identifier_begin, const size_t identifier_len) {
  109. }
  110. };
  111. };
  112. };