memory leaks fixing

This commit is contained in:
Guillermo Roche 2020-05-31 19:24:35 +02:00
parent b8659268cc
commit 3387aa7ca1
11 changed files with 87 additions and 34 deletions

View File

@ -7,7 +7,6 @@ SOURCES += \
main.cpp \ main.cpp \
conexion.cpp \ conexion.cpp \
conexion_ssl.cpp \ conexion_ssl.cpp \
thread_selector.cpp \
config_reader.cpp \ config_reader.cpp \
tests.cpp \ tests.cpp \
data_acces.cpp \ data_acces.cpp \
@ -19,7 +18,6 @@ SOURCES += \
HEADERS += \ HEADERS += \
conexion.h \ conexion.h \
conexion_ssl.h \ conexion_ssl.h \
thread_selector.h \
config_reader.h \ config_reader.h \
tests.h \ tests.h \
data_acces.h \ data_acces.h \

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.8.2, 2020-05-27T13:14:37. --> <!-- Written by QtCreator 4.8.2, 2020-05-31T02:21:12. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -56,7 +56,6 @@ void conexion::start_server(){
int client = accept(sock, (struct sockaddr*)&addr, &len); int client = accept(sock, (struct sockaddr*)&addr, &len);
std::thread t_client(conexion_client , client); std::thread t_client(conexion_client , client);
t_client.detach(); t_client.detach();
//cont++;
} }
close(sock); close(sock);
} }
@ -70,8 +69,13 @@ void conexion_client(int client){
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}else{ }else{
session_manager* session = new session_manager(client); session_manager* session = new session_manager(client);
while(!session->validate_pass()); bool enter=session->validate_pass();
session->start_dialog(); while(!enter){
enter=session->validate_pass();
}
if(enter){
session->start_dialog();
}
delete (session); delete (session);
close(client); close(client);
} }

View File

@ -29,7 +29,6 @@ void conexion_ssl::start_server(){
perror("bad port in config file"); perror("bad port in config file");
} }
sock = this->create_socket(atoi(port.data())); sock = this->create_socket(atoi(port.data()));
/* Handle connections */
SSL_CTX *ctx; SSL_CTX *ctx;
init_openssl(); init_openssl();
@ -37,8 +36,6 @@ void conexion_ssl::start_server(){
this->configure_context(ctx); this->configure_context(ctx);
//std::thread *hilos=new thread[50];
//int cont=0;
while(1) { while(1) {
struct sockaddr_in addr; struct sockaddr_in addr;
uint len = sizeof(addr); uint len = sizeof(addr);
@ -46,7 +43,6 @@ void conexion_ssl::start_server(){
int client = accept(sock, (struct sockaddr*)&addr, &len); int client = accept(sock, (struct sockaddr*)&addr, &len);
std::thread t_client(conexion_client,ctx , client); std::thread t_client(conexion_client,ctx , client);
t_client.detach(); t_client.detach();
//cont++;
} }
close(sock); close(sock);
SSL_CTX_free(ctx); SSL_CTX_free(ctx);
@ -107,8 +103,6 @@ void conexion_ssl::configure_context(SSL_CTX *ctx)
} }
void conexion_client(SSL_CTX *ctx,int client){ void conexion_client(SSL_CTX *ctx,int client){
char buf [256];
if (client < 0) { if (client < 0) {
perror("Unable to accept"); perror("Unable to accept");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -122,9 +116,13 @@ void conexion_client(SSL_CTX *ctx,int client){
} }
else { else {
session_manager* session = new session_manager_ssl(ssl); session_manager* session = new session_manager_ssl(ssl);
while(!session->validate_pass()); bool enter=session->validate_pass();
session->start_dialog(); while(!enter){
//SSL_write(ssl,std::to_string(la->execute()).data() , sizeof (int)); enter=session->validate_pass();
}
if(enter){
session->start_dialog();
}
delete (session); delete (session);
} }
SSL_shutdown(ssl); SSL_shutdown(ssl);

View File

@ -1,6 +1,18 @@
#include "data_acces.h" #include "data_acces.h"
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <string.h>
data_acces::data_acces() data_acces::data_acces()
{ {
} }
char* data_acces::get_hash(char *data){
unsigned char hash[SHA512_DIGEST_LENGTH];
SHA512(reinterpret_cast<unsigned const char*>(data), strlen(data), hash);
unsigned char* encodedData= new unsigned char[200];
EVP_EncodeBlock(encodedData, hash, sizeof (hash));
return reinterpret_cast<char*>(encodedData);
}

View File

@ -17,6 +17,6 @@ public:
virtual bool get_package_exists(std::string package)=0; virtual bool get_package_exists(std::string package)=0;
virtual void create_user(std::string user, std::string pass, bool admin)=0; virtual void create_user(std::string user, std::string pass, bool admin)=0;
virtual void remove_user(std::string user)=0; virtual void remove_user(std::string user)=0;
static char* get_hash(char *data);
}; };
#endif // DATA_ACCES_H #endif // DATA_ACCES_H

View File

@ -5,11 +5,15 @@
#include "conexion_ssl.h" #include "conexion_ssl.h"
#include "config_reader.h" #include "config_reader.h"
#include "tests.h" #include "tests.h"
#include "msql_acces.h"
#define file_config "config" #define file_config "config"
CPPUNIT_TEST_SUITE_REGISTRATION( test_basic ); CPPUNIT_TEST_SUITE_REGISTRATION( test_basic );
int main() int main()
{ {
//msql_acces data;
//data.create_user("otro", "otro", true);
//inicio de los tests //inicio de los tests
CPPUNIT_NS::TestResult testresult; CPPUNIT_NS::TestResult testresult;
CPPUNIT_NS::TestResultCollector collectedresults; CPPUNIT_NS::TestResultCollector collectedresults;

View File

@ -20,6 +20,7 @@ std::string msql_acces::get_passwd(std::string username){
while(res->next()) while(res->next())
ret = res->getString("passwd"); ret = res->getString("passwd");
delete res; delete res;
delete pstmt;
return ret; return ret;
} }
@ -31,6 +32,7 @@ bool msql_acces::get_admin(std::string username){
while(res->next()) while(res->next())
ret = res->getBoolean("admin"); ret = res->getBoolean("admin");
delete res; delete res;
delete pstmt;
return ret; return ret;
} }
@ -55,6 +57,7 @@ std::list<std::string> msql_acces::get_pinfo(){
ret.push_back(aux); ret.push_back(aux);
} }
delete res; delete res;
delete pstmt;
return ret; return ret;
} }
@ -76,6 +79,7 @@ std::list<std::string> msql_acces::get_uinfo(){
ret.push_back(aux); ret.push_back(aux);
} }
delete res; delete res;
delete pstmt;
return ret; return ret;
} }
@ -85,6 +89,7 @@ void msql_acces::write_install(std::string package, std::string user){
pstmt->setString(1,package); pstmt->setString(1,package);
pstmt->setString(2,user); pstmt->setString(2,user);
pstmt->executeUpdate(); pstmt->executeUpdate();
delete pstmt;
} }
void msql_acces::write_remove(std::string package){ void msql_acces::write_remove(std::string package){
@ -92,6 +97,7 @@ void msql_acces::write_remove(std::string package){
con->prepareStatement("delete from packages where name=?"); con->prepareStatement("delete from packages where name=?");
pstmt->setString(1,package); pstmt->setString(1,package);
pstmt->executeUpdate(); pstmt->executeUpdate();
delete pstmt;
} }
bool msql_acces::get_package_exists(std::string package){ bool msql_acces::get_package_exists(std::string package){
@ -99,22 +105,20 @@ bool msql_acces::get_package_exists(std::string package){
pstmt->setString(1,package); pstmt->setString(1,package);
sql::ResultSet *res = pstmt->executeQuery(); sql::ResultSet *res = pstmt->executeQuery();
int ret=0; int ret=0;
while(res->next()) while(res->next()){
ret = res->getInt(1); ret = res->getInt(1);
}
delete pstmt;
return ret>0; return ret>0;
} }
void msql_acces::create_user(std::string user, std::string pass, bool admin){ void msql_acces::create_user(std::string user, std::string pass, bool admin){
sql::PreparedStatement *pstmt = con->prepareStatement("insert into users(username, passwd, admin) values(?, ?, ?)"); sql::PreparedStatement *pstmt = con->prepareStatement("insert into users(username, passwd, admin) values(?, ?, ?)");
unsigned char hash[SHA512_DIGEST_LENGTH];
SHA512(reinterpret_cast<unsigned const char*>(pass.data()), strlen(pass.data()), hash);
unsigned char encodedData[200];
EVP_EncodeBlock(encodedData, hash, sizeof (hash));
pstmt->setString(1,user); pstmt->setString(1,user);
pstmt->setString(2,std::string(reinterpret_cast<char*>(encodedData))); pstmt->setString(2,std::string(data_acces::get_hash(&pass[0])));
pstmt->setBoolean(3,admin); pstmt->setBoolean(3,admin);
pstmt->executeQuery(); pstmt->executeQuery();
delete pstmt;
} }
void msql_acces::remove_user(std::string user){ void msql_acces::remove_user(std::string user){
@ -122,4 +126,5 @@ void msql_acces::remove_user(std::string user){
con->prepareStatement("delete from users where username=?"); con->prepareStatement("delete from users where username=?");
pstmt->setString(1,user); pstmt->setString(1,user);
pstmt->executeUpdate(); pstmt->executeUpdate();
delete pstmt;
} }

View File

@ -19,12 +19,8 @@ bool session_manager::validate_pass(){
this->read_data(buffer, 256); this->read_data(buffer, 256);
std::string user=buffer; std::string user=buffer;
this->read_data(buffer, 256); this->read_data(buffer, 256);
// std::string pass=buffer; std::string pass=std::string(data_acces::get_hash(buffer));
unsigned char hash[SHA512_DIGEST_LENGTH]; delete[] (buffer);
SHA512(reinterpret_cast<unsigned const char*>(buffer), strlen(buffer), hash);
unsigned char encodedData[200];
EVP_EncodeBlock(encodedData, hash, sizeof (hash));
std::string pass=std::string(reinterpret_cast<char*>(encodedData));
if(this->data->get_passwd(user)==pass){ if(this->data->get_passwd(user)==pass){
this->write_data("pass"); this->write_data("pass");
if(this->data->get_admin(user)){ if(this->data->get_admin(user)){
@ -62,6 +58,7 @@ void session_manager::start_dialog(){
break; break;
} }
} }
delete[] (buffer);
} }
int session_manager::execute(){ int session_manager::execute(){
@ -75,11 +72,13 @@ int session_manager::execute(){
this->read_data(use_conf,256); this->read_data(use_conf,256);
conf.change_uses(use_conf); conf.change_uses(use_conf);
}else if(strcmp(use_conf,"n")!=0){ }else if(strcmp(use_conf,"n")!=0){
delete[] (n_package);
perror("fail in protocol comunication"); perror("fail in protocol comunication");
return -1; return -1;
} }
delete [] (use_conf); delete [] (use_conf);
std::string result = this->appli_command("--ask", n_package); std::string result = this->appli_command("--ask", n_package);
delete[] (n_package);
if(result=="err"){ if(result=="err"){
return -1; return -1;
}else{ }else{
@ -94,6 +93,7 @@ int session_manager::remove(){
char* n_package = new char[256]; char* n_package = new char[256];
this->read_data(n_package, 256); this->read_data(n_package, 256);
std::string result = this->appli_command("--unmerge",n_package); std::string result = this->appli_command("--unmerge",n_package);
delete[] (n_package);
if(result=="err"){ if(result=="err"){
return -1; return -1;
}else{ }else{
@ -121,10 +121,8 @@ std::string session_manager::appli_command(char comand[], char* n_package){
if(status>0){ if(status>0){
this->write_data("ok"); this->write_data("ok");
ret = n_package; ret = n_package;
delete[] (n_package);
}else{ }else{
this->write_data("bad"); this->write_data("bad");
delete[] (n_package);
ret = "err"; ret = "err";
} }
} }
@ -167,6 +165,7 @@ void session_manager::remove_user(){
this->read_data(buffer,256); this->read_data(buffer,256);
this->read_data(buffer,256); this->read_data(buffer,256);
this->data->remove_user(std::string(buffer)); this->data->remove_user(std::string(buffer));
delete[] (buffer);
} }
int session_manager::read_data(char* input, int size){ int session_manager::read_data(char* input, int size){

View File

@ -1,10 +1,15 @@
#include "tests.h" #include "tests.h"
#include "msql_acces.h"
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <string.h>
void test_basic::setUp(){ void test_basic::setUp(){
this->conf=new config_reader("config"); this->conf=new config_reader("config");
this->data=new msql_acces();
} }
void test_basic::testInitial(){ void test_basic::test_initial(){
std::string res; std::string res;
CPPUNIT_ASSERT(true==this->conf->get_param("port",res)); CPPUNIT_ASSERT(true==this->conf->get_param("port",res));
CPPUNIT_ASSERT("4433"==res); CPPUNIT_ASSERT("4433"==res);
@ -20,3 +25,23 @@ void test_basic::pass_tests(){
testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ()); testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
testrunner.run(testresult); testrunner.run(testresult);
} }
void test_basic::hash_test(){
CPPUNIT_ASSERT(strcmp(data_acces::get_hash("test1")
,data_acces::get_hash("test1"))==0);
CPPUNIT_ASSERT(strcmp(data_acces::get_hash("test1")
,data_acces::get_hash("test2"))!=0);
}
void test_basic::msql_loggin_test(){
std::string pass = this->data->get_passwd("test");
CPPUNIT_ASSERT(pass==std::string(data_acces::get_hash("ok")));
CPPUNIT_ASSERT(pass!=std::string(data_acces::get_hash("fail")));
}
void test_basic::msql_package_test(){
data->write_install("p1","test");
CPPUNIT_ASSERT(data->get_package_exists("p1"));
data->write_remove("p1");
CPPUNIT_ASSERT(!data->get_package_exists("p1"));
}

12
tests.h
View File

@ -12,17 +12,25 @@
#include <cppunit/CompilerOutputter.h> #include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h> #include <cppunit/XmlOutputter.h>
#include "config_reader.h" #include "config_reader.h"
#include "data_acces.h"
using namespace CppUnit; using namespace CppUnit;
class test_basic : public CppUnit::TestFixture{ class test_basic : public CppUnit::TestFixture{
CPPUNIT_TEST_SUITE(test_basic); CPPUNIT_TEST_SUITE(test_basic);
CPPUNIT_TEST(testInitial); CPPUNIT_TEST(test_initial);
CPPUNIT_TEST(hash_test);
CPPUNIT_TEST(msql_loggin_test);
CPPUNIT_TEST(msql_package_test);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
public: public:
void setUp(void); void setUp(void);
void pass_tests(); void pass_tests();
private: private:
void testInitial(void); void test_initial();
void hash_test();
void msql_loggin_test();
void msql_package_test();
config_reader *conf; config_reader *conf;
data_acces *data;
}; };
#endif // TESTS_H #endif // TESTS_H