|
|
|
@ -5,12 +5,6 @@
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
|
|
// platform-dependent:
|
|
|
|
|
#include <cstdlib> // for getenv()
|
|
|
|
|
#include <sys/types.h> // for creat()
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h> // for unlink()
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
@ -33,17 +27,22 @@ namespace
|
|
|
|
|
std::generate_n( std::back_inserter(ret), right_len, [&](){ return token_alphabet[dis(gen)]; } );
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace js = json_spirit;
|
|
|
|
|
|
|
|
|
|
// platform dependent:
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
|
|
#error "Please implement get_token_filename() for Win32"
|
|
|
|
|
#error "Please implement get_token_filename() and write_security_file() for Win32!"
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
// version for POSIX-compliant systems:
|
|
|
|
|
|
|
|
|
|
#include <cstdlib> // for getenv()
|
|
|
|
|
#include <sys/types.h> // for creat()
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h> // for unlink()
|
|
|
|
|
|
|
|
|
|
std::string get_token_filename()
|
|
|
|
|
{
|
|
|
|
|
const char* const temp_dir = getenv("TEMP");
|
|
|
|
@ -53,10 +52,8 @@ std::string get_token_filename()
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // ! _WIN32
|
|
|
|
|
|
|
|
|
|
// creates a file with restrictive access rights that contains a security token.
|
|
|
|
|
std::string create_security_token(const std::string& server_address, unsigned port_nr, const std::string& path)
|
|
|
|
|
void write_security_file(const std::string& content)
|
|
|
|
|
{
|
|
|
|
|
const std::string filename = get_token_filename();
|
|
|
|
|
int fd = creat( filename.c_str(), S_IRUSR | S_IWUSR );
|
|
|
|
@ -64,7 +61,35 @@ std::string create_security_token(const std::string& server_address, unsigned po
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("Cannot create security token file \"" + filename + "\": " + std::to_string(errno) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ssize_t ss = write(fd, content.data(), content.size());
|
|
|
|
|
if(ss<0 || uint64_t(ss)!=content.size())
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("Cannot write into security token file \"" + filename + "\": " + std::to_string(errno));
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
void remove_token_file()
|
|
|
|
|
{
|
|
|
|
|
const std::string filename = get_token_filename();
|
|
|
|
|
unlink(filename.c_str());
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endif // ! _WIN32
|
|
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace js = json_spirit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// creates a file with restrictive access rights that contains a security token.
|
|
|
|
|
std::string create_security_token(const std::string& server_address, unsigned port_nr, const std::string& path)
|
|
|
|
|
{
|
|
|
|
|
const std::string sec_token = create_random_token();
|
|
|
|
|
|
|
|
|
|
js::Object o;
|
|
|
|
@ -74,19 +99,9 @@ std::string create_security_token(const std::string& server_address, unsigned po
|
|
|
|
|
o.emplace_back("security_token", sec_token );
|
|
|
|
|
|
|
|
|
|
const std::string content = js::write( o, js::pretty_print | js::raw_utf8 ) + '\n';
|
|
|
|
|
const ssize_t ss = write(fd, content.data(), content.size());
|
|
|
|
|
if(ss<0 || uint64_t(ss)!=content.size())
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("Cannot write into security token file \"" + filename + "\": " + std::to_string(errno));
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
write_security_file( content );
|
|
|
|
|
|
|
|
|
|
return sec_token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void remove_token_file()
|
|
|
|
|
{
|
|
|
|
|
const std::string filename = get_token_filename();
|
|
|
|
|
unlink(filename.c_str());
|
|
|
|
|
}
|
|
|
|
|