diff --git a/src/BashManager.cpp b/src/BashManager.cpp index c02f6af..1c10ea8 100644 --- a/src/BashManager.cpp +++ b/src/BashManager.cpp @@ -7,6 +7,7 @@ int BashManager::execute(string command) { + cout << "command is " << command << endl; FILE * p = popen(command.c_str(),"r"); if( p == NULL) { @@ -28,6 +29,10 @@ string BashManager::executeAndReadResult(string command) } while(fgets(lineBuffer,sizeof(lineBuffer),p)){ result += lineBuffer; + //removing newline character + if (!result.empty() && result[result.length()-1] == '\n') { + result.erase(result.length()-1); + } } pclose(p); return result; diff --git a/src/Services.cpp b/src/Services.cpp index 6528df1..74750b8 100644 --- a/src/Services.cpp +++ b/src/Services.cpp @@ -70,13 +70,16 @@ vector Services::readServicesFromCSV (string CSV) const cout << "Invalid services.csv file." << endl; }else{ string line; - string tmpUserID; //used before converting to int + string tmpUserID=""; //used before converting to int int userID; - string username=""; - string server=""; + string username; + string server; list servers; while(getline(streamServices,line)){ servers.clear(); + tmpUserID=""; + username=""; + server=""; if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account continue; } diff --git a/src/main.cpp b/src/main.cpp index 8d1d981..21c6834 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,8 +34,27 @@ int isServiceOnServer(string serviceUsername) } int createUser(string serviceUsername){ - //TO DO - cout << "create user called" << endl; + //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 + Services services = Services("./services/services.csv"); + const Service * service = services.findByUsername(serviceUsername); + int uid = (*service).getUserID()+uidStart; + //test if user already exists + string cmd = "id -u "+serviceUsername; + string res = BashManager::executeAndReadResult (cmd); + if(res==to_string(uid)){ + cout << "user already existed" << endl; + return 0; + } + //create user + string cmd2 ="useradd -u " + to_string(uid) + " " + serviceUsername; + cout << cmd2 << endl; + int res2 = BashManager::execute(cmd2); + if (res2 != 0){ + cerr << "Error when executing the bash command to create a user specific to the service." << endl; + return -1; + } return 0; } diff --git a/test/ServicesTest/test b/test/ServicesTest/test index 2da754c..cf573b0 100755 Binary files a/test/ServicesTest/test and b/test/ServicesTest/test differ diff --git a/testenv/services/services.csv b/testenv/services/services.csv index eca2d8c..c9c122f 100644 --- a/testenv/services/services.csv +++ b/testenv/services/services.csv @@ -1,4 +1,4 @@ -ID,Name,Server +#ID,Name,Server 1,test.sh8s.sh,ordinosaure 2,truc.sh8s.sh,shlag_cluster 3,other.sh8s.sh,