2020-05-08 12:43:46 +02:00
|
|
|
#include "session_manager.h"
|
|
|
|
#include "msql_acces.h"
|
2020-05-13 00:58:32 +02:00
|
|
|
#include "config_package.h"
|
2020-05-08 12:43:46 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstring>
|
2020-05-26 21:58:33 +02:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/evp.h>
|
2020-05-08 12:43:46 +02:00
|
|
|
|
|
|
|
session_manager::session_manager(int fd)
|
|
|
|
{
|
|
|
|
this->fd=fd;
|
|
|
|
this->data=new msql_acces();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool session_manager::validate_pass(){
|
|
|
|
char* buffer = new char[256];
|
|
|
|
this->read_data(buffer, 256);
|
|
|
|
std::string user=buffer;
|
|
|
|
this->read_data(buffer, 256);
|
2020-05-26 21:58:33 +02:00
|
|
|
// std::string pass=buffer;
|
|
|
|
unsigned char hash[SHA512_DIGEST_LENGTH];
|
|
|
|
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));
|
2020-05-08 12:43:46 +02:00
|
|
|
if(this->data->get_passwd(user)==pass){
|
|
|
|
this->write_data("pass");
|
2020-05-26 18:48:31 +02:00
|
|
|
if(this->data->get_admin(user)){
|
|
|
|
this->write_data("admin");
|
2020-05-27 13:15:06 +02:00
|
|
|
this->admin=true;
|
2020-05-26 18:48:31 +02:00
|
|
|
}else{
|
|
|
|
this->write_data("norma");
|
2020-05-27 13:15:06 +02:00
|
|
|
this->admin=false;
|
2020-05-26 18:48:31 +02:00
|
|
|
}
|
2020-05-15 21:39:23 +02:00
|
|
|
this->user=user;
|
2020-05-08 12:43:46 +02:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
this->write_data("fail");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_manager::start_dialog(){
|
|
|
|
char* buffer = new char[5];
|
|
|
|
while(true){
|
2020-05-21 20:57:18 +02:00
|
|
|
int n_read=this->read_data(buffer,5);
|
2020-05-08 12:43:46 +02:00
|
|
|
if(strcmp(buffer, "exec")==0){
|
|
|
|
this->execute();
|
|
|
|
}else if(strcmp(buffer, "info")==0){
|
|
|
|
this->send_information();
|
2020-05-15 21:39:23 +02:00
|
|
|
}else if(strcmp(buffer, "remv")==0){
|
|
|
|
this->remove();
|
2020-05-27 13:15:06 +02:00
|
|
|
}else if((strcmp(buffer,"uinf")==0)&&this->admin){
|
2020-05-20 00:10:05 +02:00
|
|
|
this->send_user_info();
|
2020-05-27 13:15:06 +02:00
|
|
|
}else if((strcmp(buffer,"cusr")==0)&&this->admin){
|
2020-05-21 20:57:18 +02:00
|
|
|
this->create_user();
|
2020-05-27 13:15:06 +02:00
|
|
|
}else if((strcmp(buffer,"rusr")==0)&&this->admin){
|
2020-05-26 01:37:34 +02:00
|
|
|
this->remove_user();
|
2020-05-21 20:57:18 +02:00
|
|
|
}else if((strcmp(buffer,"exit")==0)||(n_read==0)){
|
2020-05-08 12:43:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_manager::execute(){
|
2020-05-16 20:52:50 +02:00
|
|
|
char* n_package = new char[250];
|
|
|
|
this->read_data(n_package, 5);
|
|
|
|
this->read_data(n_package, 250);
|
2020-05-15 21:39:23 +02:00
|
|
|
char* use_conf=new char[256];
|
|
|
|
this->read_data(use_conf,2);
|
|
|
|
if(strcmp(use_conf,"y")==0){
|
|
|
|
config_package conf = config_package(n_package);
|
|
|
|
this->read_data(use_conf,256);
|
|
|
|
conf.change_uses(use_conf);
|
|
|
|
}else if(strcmp(use_conf,"n")!=0){
|
|
|
|
perror("fail in protocol comunication");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
delete [] (use_conf);
|
|
|
|
std::string result = this->appli_command("--ask", n_package);
|
|
|
|
if(result=="err"){
|
|
|
|
return -1;
|
|
|
|
}else{
|
2020-05-16 20:52:50 +02:00
|
|
|
if(!this->data->get_package_exists(result)){
|
|
|
|
this->data->write_install(result, user);
|
|
|
|
}
|
2020-05-15 21:39:23 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_manager::remove(){
|
|
|
|
char* n_package = new char[256];
|
|
|
|
this->read_data(n_package, 256);
|
|
|
|
std::string result = this->appli_command("--unmerge",n_package);
|
|
|
|
if(result=="err"){
|
|
|
|
return -1;
|
|
|
|
}else{
|
2020-05-16 20:52:50 +02:00
|
|
|
this->data->write_remove(result);
|
2020-05-15 21:39:23 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string session_manager::appli_command(char comand[], char* n_package){
|
2020-05-08 12:43:46 +02:00
|
|
|
this->args=new char*[4];
|
|
|
|
this->args[0]="emerge";
|
2020-05-15 21:39:23 +02:00
|
|
|
this->args[1]=comand;
|
2020-05-08 12:43:46 +02:00
|
|
|
this->args[2]=n_package;
|
|
|
|
this->args[3]=nullptr;
|
|
|
|
int pid = fork();
|
|
|
|
int status=-2;
|
2020-05-15 21:39:23 +02:00
|
|
|
std::string ret;
|
2020-05-08 12:43:46 +02:00
|
|
|
if(pid==0){
|
|
|
|
if(execvp(this->args[0],this->args)==-1){
|
|
|
|
std::cout << "error inesperado" << std::endl;
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}else {
|
|
|
|
waitpid(pid, &status, WCONTINUED);
|
|
|
|
if(status>0){
|
|
|
|
this->write_data("ok");
|
2020-05-15 21:39:23 +02:00
|
|
|
ret = n_package;
|
|
|
|
delete[] (n_package);
|
2020-05-08 12:43:46 +02:00
|
|
|
}else{
|
|
|
|
this->write_data("bad");
|
2020-05-15 21:39:23 +02:00
|
|
|
delete[] (n_package);
|
|
|
|
ret = "err";
|
2020-05-08 12:43:46 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-15 21:39:23 +02:00
|
|
|
return ret;
|
2020-05-08 12:43:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_manager::send_information(){
|
2020-05-13 00:58:32 +02:00
|
|
|
std::list<std::string> lis=this->data->get_pinfo();
|
|
|
|
for(std::string info : lis){
|
|
|
|
this->write_data(info);
|
|
|
|
}
|
2020-05-19 20:36:56 +02:00
|
|
|
this->write_data("end:info");
|
2020-05-08 12:43:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 00:10:05 +02:00
|
|
|
void session_manager::send_user_info(){
|
|
|
|
std::list<std::string> lis=this->data->get_uinfo();
|
|
|
|
for(std::string info : lis){
|
|
|
|
this->write_data(info);
|
|
|
|
}
|
|
|
|
this->write_data("end:info");
|
|
|
|
}
|
|
|
|
|
2020-05-21 20:57:18 +02:00
|
|
|
void session_manager::create_user(){
|
|
|
|
char* user=new char[256];
|
|
|
|
char* pass=new char[256];
|
|
|
|
char* admin=new char[256];
|
2020-05-26 01:37:34 +02:00
|
|
|
//basura
|
|
|
|
this->read_data(user, 256);
|
2020-05-21 20:57:18 +02:00
|
|
|
this->read_data(user, 256);
|
|
|
|
this->read_data(pass, 256);
|
|
|
|
this->read_data(admin, 256);
|
|
|
|
this->data->create_user(std::string(user), std::string(pass), admin[0]=='t');
|
|
|
|
delete [] (user);
|
|
|
|
delete [] (pass);
|
|
|
|
delete [] (admin);
|
|
|
|
}
|
|
|
|
|
2020-05-26 01:37:34 +02:00
|
|
|
void session_manager::remove_user(){
|
|
|
|
char *buffer=new char[256];
|
|
|
|
this->read_data(buffer,256);
|
|
|
|
this->read_data(buffer,256);
|
|
|
|
this->data->remove_user(std::string(buffer));
|
|
|
|
}
|
|
|
|
|
2020-05-08 12:43:46 +02:00
|
|
|
int session_manager::read_data(char* input, int size){
|
|
|
|
return read(this->fd, input, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_manager::write_data(std::string output){
|
|
|
|
return write(this->fd, output.data(), output.size());
|
|
|
|
}
|