addded remove in BashModule

This commit is contained in:
eleonore12345 2024-08-09 18:24:56 +02:00
parent b3bd744ef4
commit 549bfe74e3
2 changed files with 28 additions and 22 deletions

View File

@ -19,21 +19,20 @@ BashModule::BashModule()
}
//private methods
int BashModule::executeScript(string serviceUsername)
int BashModule::executeScript(string serviceUsername, string script)
{
//this method is called in deploy(), it executes the script deploy.sh if it exists
string deployScript="./services/"+serviceUsername+"/deploy.sh";
if(!filesystem::exists(deployScript)){
//this method is called in deploy() and remove(), it executes the script if it exists
if(!filesystem::exists(script)){
cout << "No deploy.sh script for this service.";
return 0;
} else {
//check that the file is of type regular
if (filesystem::status(deployScript).type()!=filesystem::file_type::regular){
if (filesystem::status(script).type()!=filesystem::file_type::regular){
cerr << "Error. The file deploy_user.sh is not regular." << endl;
return -1;
} else {
//check if the owner has the execute permission
if ((filesystem::status(deployScript).permissions() & filesystem::perms::owner_exec)==filesystem::perms::none){
if ((filesystem::status(script).permissions() & filesystem::perms::owner_exec)==filesystem::perms::none){
cerr << "Error. The owner of file deploy_user.sh does not have permission to execute it. Please change the permission or remove/rename the file." << endl;
return -1;
}else{
@ -47,14 +46,14 @@ int BashModule::executeScript(string serviceUsername)
int status;
waitpid(-1,&status,0);
if(status==-1){
cerr << "Error when executing " << deployScript << endl;
cerr << "Error when executing " << script << endl;
}
return status;
} else {
//child process
if(execl("/bin/bash", "/bin/bash", "--noediting", "--noprofile", "--norc", deployScript.c_str(), (char *)0)==-1)
if(execl("/bin/bash", "/bin/bash", "--noediting", "--noprofile", "--norc", script.c_str(), (char *)0)==-1)
{
cerr << "Error in the execl call of " << deployScript << endl;
cerr << "Error in the execl call of " << script << endl;
}
}
}
@ -63,22 +62,21 @@ int BashModule::executeScript(string serviceUsername)
}
}
int BashModule::executeScriptAs(string serviceUsername)
int BashModule::executeScriptAs(string serviceUsername, string script)
{
//this method is called in deploy(), it executes the script deploy_user.sh if it exists, as the user associated with the service
string deployAsScript="./services/"+serviceUsername+"/deploy_user.sh";
//this method is called in deploy(), it executes the script if it exists, as the user associated with the service
//check that the file exists
if(!filesystem::exists(deployAsScript)){
if(!filesystem::exists(script)){
cout << "No deploy_user.sh script for this service.";
return 0;
} else {
//check that the file is of type regular
if (filesystem::status(deployAsScript).type()!=filesystem::file_type::regular){
if (filesystem::status(script).type()!=filesystem::file_type::regular){
cerr << "Error. The file deploy_user.sh is not regular." << endl;
return -1;
} else {
//check if the owner has the execute permission
if ((filesystem::status(deployAsScript).permissions() & filesystem::perms::owner_exec)==filesystem::perms::none){
if ((filesystem::status(script).permissions() & filesystem::perms::owner_exec)==filesystem::perms::none){
cerr << "Error. The owner of file deploy_user.sh does not have permission to execute it. Please change the permission or remove/rename the file." << endl;
return -1;
}
@ -99,7 +97,7 @@ int BashModule::executeScriptAs(string serviceUsername)
int status;
waitpid(-1,&status,0);
if(status==-1){
cerr << "Error when executing " << deployAsScript << endl;
cerr << "Error when executing " << script << endl;
}
return status;
} else {
@ -107,9 +105,9 @@ int BashModule::executeScriptAs(string serviceUsername)
//executing as the user corresponding to the service
setgid(p->pw_gid);
setuid(p->pw_uid);
if(execl("/bin/bash", "/bin/bash", "--noediting", "--noprofile", "--norc", deployAsScript.c_str(), serviceUsername, (char *)0)==-1)
if(execl("/bin/bash", "/bin/bash", "--noediting", "--noprofile", "--norc", script.c_str(), serviceUsername, (char *)0)==-1)
{
cerr << "Error in the execl call of " << deployAsScript << endl;
cerr << "Error in the execl call of " << script << endl;
}
}
}
@ -125,11 +123,12 @@ int BashModule::prepare()
int BashModule::deploy (string serviceUsername)
{
cout << "deploy in bash module called" << endl;
if (executeScript(serviceUsername)!=0){
string deployScript="./services/"+serviceUsername+"/deploy.sh"; //to be executed as current user
string deployAsScript="./services/"+serviceUsername+"/deploy_user.sh"; //to be executed as the user corresponding to the service
if (executeScript(serviceUsername,deployScript)!=0){
cerr << "Error in BashModule::deploy." << endl;
return -1;
}else if (executeScriptAs(serviceUsername)!=0){
}else if (executeScriptAs(serviceUsername,deployAsScript)!=0){
cerr << "Error in BashModule::deploy." << endl;
return -1;
}
@ -139,6 +138,11 @@ int BashModule::deploy (string serviceUsername)
int BashModule::remove (string serviceUsername)
{
string removeScript="./services/"+serviceUsername+"/undeploy_user.sh";
if (executeScript(serviceUsername,removeScript)!=0){
cerr << "Error in BashModule::deploy." << endl;
return -1;
}
return 0;
}

View File

@ -17,7 +17,9 @@ class BashModule : public Module
int remove(string serviceUsername);
int clean ();
private:
int executeScript(string serviceUsername);
int executeScript(string serviceUsername,string script);
int executeScriptAs(string serviceUsername,string script);
};
#endif