diff --git a/src/lib/config/protos/keycode.proto b/src/lib/config/protos/keycode.proto index 93e32133f..3243e1c99 100644 --- a/src/lib/config/protos/keycode.proto +++ b/src/lib/config/protos/keycode.proto @@ -6,6 +6,9 @@ message KeyCode optional uint32 nusers = 10; optional uint64 expiration = 20; optional uint64 generation_time = 30; + + optional string ip = 40; + optional string port = 50; } message EncryptedKeyCode diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index c51f019fe..c7c567a53 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -11,6 +11,7 @@ #include "lib/utils/bts_assert.h" #include "lib/utils/helper_macros.h" #include "lib/config/config.h" +#include "lib/config/protos/Ports.pb.h" #include "lib/config/protos/keycode.pb.h" #include @@ -18,6 +19,12 @@ #include #include +//ip/port stuff +#include +#include +#include +#include + namespace bts { namespace api { namespace auth { @@ -43,9 +50,64 @@ AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() _nusers = kc.nusers(); _expiration = kc.expiration(); + + bts_assert_msg(validate_ip_port(kc), "invalid ip or port in keycode"); } - +bool AuthSessionManager::validate_ip_port(const bts::config::KeyCode& kc) +{ + bool portgood = true; + if(kc.has_port() && kc.port() != "") + { + const auto& ports = config::get(); + portgood = std::to_string(ports.config_port()) == kc.port(); + } + bool ipgood = true; + if(kc.has_ip() && kc.ip() != "") + { + struct ifaddrs* ifAddrStruct = nullptr; + struct ifaddrs* ifa = nullptr; + void * tmpAddrPtr = nullptr; + getifaddrs(&ifAddrStruct); + + for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) + { + if (!ifa->ifa_addr) + continue; + if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4 + { + // is a valid IP4 Address + tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; + char addressBuffer[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); + if(std::string(addressBuffer) == kc.ip()) + { + ipgood = true; + break; + } + else + ipgood = false; + } + else if (ifa->ifa_addr->sa_family == AF_INET6) // check it is IP6 + { + // is a valid IP6 Address + tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; + char addressBuffer[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); + if(std::string(addressBuffer) == kc.ip()) + { + ipgood = true; + break; + } + else + ipgood = false; + } + } + if (ifAddrStruct!=NULL) + freeifaddrs(ifAddrStruct); + } + return portgood && ipgood; +} bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok) { diff --git a/src/lib/utils/AuthSessionManager.h b/src/lib/utils/AuthSessionManager.h index 8da8815ea..d353bef6f 100644 --- a/src/lib/utils/AuthSessionManager.h +++ b/src/lib/utils/AuthSessionManager.h @@ -5,6 +5,7 @@ #include namespace bts { +namespace config { class KeyCode; } namespace api { namespace auth { @@ -21,6 +22,8 @@ public: bool add_or_replace_session(const api::user::TokenPayload& tok); bool check_session(const api::user::TokenPayload& tok); +private: + bool validate_ip_port(const bts::config::KeyCode& kc); private: static const std::string PUBLIC_KEY; typedef std::map SessionMap; diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp index 2a97c6b2f..3c8ce0323 100644 --- a/src/server/sparkd/cmds/KeyCode.cpp +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -29,6 +29,11 @@ #include #include +#include +#include +#include +#include + namespace bts { namespace cmd { @@ -42,13 +47,48 @@ std::string KeyCode::helpstr () { std::ostringstream oss; oss << "\n" - << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18>\n " + << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18> [-ip:] [-port:]\n " << "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n " - << "KEYGEN -keyfile:[keyfile_prefix]\n "; + << "KEYGEN -keyfile:[keyfile_prefix]\n " + << "CHECKIPS \n "; return oss.str(); } +int checkips() +{ + struct ifaddrs * ifAddrStruct=NULL; + struct ifaddrs * ifa=NULL; + void * tmpAddrPtr=NULL; + + getifaddrs(&ifAddrStruct); + + for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) + { + if (!ifa->ifa_addr) + continue; + if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4 + { + // is a valid IP4 Address + tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; + char addressBuffer[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); + printf("%s IP Address => %s\n", ifa->ifa_name, addressBuffer); + } + else if (ifa->ifa_addr->sa_family == AF_INET6) // check it is IP6 + { + // is a valid IP6 Address + tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; + char addressBuffer[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); + printf("%s IP Address => %s\n", ifa->ifa_name, addressBuffer); + } + } + if (ifAddrStruct!=NULL) + freeifaddrs(ifAddrStruct); + return 0; +} + std::string parse_date (std::string const &date) { auto the_date = bts::datetime::parse_iso_date_time_string(date); @@ -57,7 +97,8 @@ std::string parse_date (std::string const &date) void get_cmd(const std::vector& args, std::string& keyfile, std::string& pass, - std::string& users, std::string& expiration, std::string* source = nullptr) + std::string& users, std::string& expiration, std::string* source = nullptr, + std::string* ip = nullptr, std::string* port = nullptr) { for(size_t i = 1; i < args.size(); i++) { @@ -80,6 +121,14 @@ void get_cmd(const std::vector& args, std::string& keyfile, std::st size_t pay_pos = args[i].find("-source:"); if(source && pay_pos != args[i].npos) *source = args[i].substr(pay_pos+8); + + size_t i_pos = args[i].find("-ip:"); + if(ip && i_pos != args[i].npos) + *ip = args[i].substr(i_pos+4); + + size_t p_pos = args[i].find("-port:"); + if(port && p_pos != args[i].npos) + *port = args[i].substr(p_pos+6); } } @@ -134,7 +183,8 @@ int keygen(const std::vector& args) // } // } -void write_keycode(const std::string& users, const std::string& expiration, bts::config::KeyCode& kc) +void write_keycode(const std::string& users, const std::string& expiration, + const std::string& ip, const std::string& port, bts::config::KeyCode& kc) { std::ostringstream out_stream; @@ -146,6 +196,9 @@ void write_keycode(const std::string& users, const std::string& expiration, bts: kc.set_nusers(nusers); kc.set_expiration(expir_ll); kc.set_generation_time(now); + bts_assert_msg(ip != "127.0.0.1", "you can't generate a keycode for loopback, just leave the parameter blank"); + kc.set_ip(ip); + kc.set_port(port); } void eraseAllSubStr(std::string& mainStr, const std::string& toErase) @@ -160,7 +213,8 @@ void eraseAllSubStr(std::string& mainStr, const std::string& toErase) } } -int encrypt(const std::string& keyfile, const std::string& pass, const std::string& users, const std::string& expiration) +int encrypt(const std::string& keyfile, const std::string& pass, const std::string& users, + const std::string& expiration, const std::string& ip, const std::string& port) { std::string protobuf; @@ -177,7 +231,7 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri PrivateKeyPtr key = PrivateKey::read_file(keyfile, &cache); bts::config::KeyCode kc; - write_keycode(users, expiration, kc); + write_keycode(users, expiration, ip, port, kc); if(google::protobuf::TextFormat::PrintToString(kc, &kc_string)) { oss << kc_string; @@ -213,10 +267,10 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri int encode(const std::vector& args) { - std::string keyfile, pass, users, expiration; - get_cmd(args, keyfile, pass, users, expiration); + std::string keyfile, pass, users, expiration, ip, port; + get_cmd(args, keyfile, pass, users, expiration, nullptr, &ip, &port); - return encrypt(keyfile, pass, users, expiration); + return encrypt(keyfile, pass, users, expiration, ip, port); } int decode(const std::vector& args) @@ -273,6 +327,10 @@ int KeyCode::execute(const std::vector& args) { return decode(args); } + if(boost::to_lower_copy(args[0]) == "checkips") + { + return checkips(); + } } std::cout << help() << std::endl; return -1;