From 00577178200a3576412d0424016e5cfe3e06beda Mon Sep 17 00:00:00 2001 From: eleonore12345 Date: Tue, 13 Aug 2024 13:11:09 +0200 Subject: [PATCH] avancees environnement et main --- src/main.cpp | 111 ++++++++++++++++++++++++++++++++++----------- testenv/Dockerfile | 2 + 2 files changed, 86 insertions(+), 27 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3a0e093..1fce722 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,7 +40,7 @@ int createUser(string serviceUsername) { //this method creates a Unix user dedicated to the service //get the User ID from servers.csv - int uidStart=2000; //so that the uids do not overlap with existing uids + int uidStart=stoi(getenv("services_uid_start")); //so that the uids do not overlap with existing uids Services services = Services(); const Service * service = services.FindByUsername(serviceUsername); int uid = (*service).GetUserID()+uidStart; @@ -62,6 +62,33 @@ int createUser(string serviceUsername) return 0; } +int deleteUser(string serviceUsername) +{ + //this method deletes the Unix user dedicated to the service + //get the User ID from servers.csv + int uidStart=stoi(getenv("services_uid_start")); + Services services = Services(); + const Service * service = services.FindByUsername(serviceUsername); + int uid = (*service).GetUserID()+uidStart; + //test if user exists + string cmd = "id -u "+serviceUsername; + string res = BashManager::ExecuteAndReadResult (cmd); + if(res!=to_string(uid)){ + cout << "no user to delete" << endl; + return 0; + } + //delete user + string cmd2 ="deluser --delete-home " + serviceUsername; + string res2 = BashManager::ExecuteAndReadResult(cmd2); + if (res2.find("Done") == std::string::npos){ + cerr << "Error when executing the bash command to delete the user specific to the service." << endl; + cerr << res2 << endl; + return -1; + } + cout << "user deleted" << endl; + return 0; +} + string findCertificate(string serviceUsername) { //this method searches for a specific ssl certificate for the service, either in dns or http directories @@ -71,8 +98,10 @@ string findCertificate(string serviceUsername) //searching is dns_certs_path string dns_certs_path=getenv("dns_certs_path"); //dns_certs_path is an environment variable //finding the serviceUsername* directory - string cmd="ls $dns_certs_path/"+serviceUsername+" | grep \"^"+serviceUsername+"\(-[0-9]\{4\}\)\?$"; + string cmd="ls $dns_certs_path/"+serviceUsername+" | grep '^"+serviceUsername+"\\(-[0-9]\\{4\\}\\)\\?$'"; + cout << "before"<< endl; string name = BashManager::ExecuteAndReadResult(cmd); + cout << "result 1: " << name << endl; if (!name.empty()){ //finding the certificate string cert = dns_certs_path+"/"+name+"/fullchain.pem"; @@ -81,27 +110,24 @@ string findCertificate(string serviceUsername) } else { cout << "No certificate in " << dns_certs_path << endl; } - } else { - cout << "No certificate in " << dns_certs_path << endl; - //searching in http_certs_path - string http_certs_path=getenv(("http_certs_path")); //http_certs_path is an environment variable - //finding the serviceUsername* directory - string cmd="ls $http_certs_path/"+serviceUsername+" | grep \"^"+serviceUsername+"\(-[0-9]\{4\}\)\?$"; - string name = BashManager::ExecuteAndReadResult(cmd); - if (!name.empty()){ - //finding the certificate - string cert = http_certs_path+"/"+name+"/fullchain.pem"; - if (fs::exists(cert)){ - return cert; - } else { - cout << "No certificate in " << http_certs_path << endl; - } - } else { - cout << "Using dummy certificate" << endl; - return getenv("dummy_cert_path"); - } - } + //searching in http_certs_path + string http_certs_path=getenv(("http_certs_path")); //http_certs_path is an environment variable + //finding the serviceUsername* directory + cmd="ls $http_certs_path/"+serviceUsername+" | grep '^"+serviceUsername+"\\(-[0-9]\\{4\\}\\)\\?$'"; + name = BashManager::ExecuteAndReadResult(cmd); + cout << "result 2: " << name << endl; + if (!name.empty()){ + //finding the certificate + string cert = http_certs_path+"/"+name+"/fullchain.pem"; + if (fs::exists(cert)){ + return cert; + } else { + cout << "No certificate in " << http_certs_path << endl; + } + } + cout << "Using dummy certificate" << endl; + return getenv("dummy_cert_path"); } int createEnvService(string serviceUsername) @@ -131,6 +157,7 @@ int createEnvService(string serviceUsername) outfile << "jc_id=" << jc_id << endl; outfile << "net=" << net << endl; outfile << "jc_cert=" << jc_cert << endl; + outfile.close(); //setting the environment variables for all the shell commands called in this C++ programm setenv("http_dir",http_dir.c_str(),1); @@ -156,18 +183,23 @@ int createEnvService(string serviceUsername) string cmd="chown "+ jc_id +":www-data -R "+http_dir; BashManager::Execute(cmd); //secret_dir + fs::create_directories(secret_dir); if (chown(secret_dir.c_str(), (unsigned int)stoi(jc_id),(unsigned int)stoi(jc_id)) != 0) { cerr << "Error changing ownership of" << secret_dir << endl; return -1; } fs::permissions(secret_dir,fs::perms::owner_all|fs::perms::group_read|fs::perms::group_exec|fs::perms::others_exec,fs::perm_options::replace); + cout << "service environment created" << endl; return 0; } int removeEnvService() { - /* - [ -d "$HTTP_DIR" ] && rm -r "$HTTP_DIR"*/ + string http_dir=getenv("http_dir"); + if(fs::exists(http_dir)){ + fs::remove_all(http_dir); + } + cout << "http directory of service deleted" << endl; return 0; } @@ -204,8 +236,7 @@ int createEnv() setenv("dummy_cert_path",dummy_cert_path.c_str(),1); setenv("servicefile",servicefile.c_str(),1); setenv("services_uid_start",services_uid_start.c_str(),1); - - + cout << "global environment created" << endl; return 0; } @@ -241,23 +272,49 @@ int deployAll() cout << "deploying all" <