From 553b14185bf0b80cd15f24206a9eac84c088d73c Mon Sep 17 00:00:00 2001 From: Luke De Mouy Date: Thu, 14 Jan 2016 11:15:14 -0700 Subject: [PATCH 1/2] add ability to set the uid and gid when creating a user back in --- src/library/sysadm-usermanager.cpp | 14 ++++++++++++-- src/library/sysadm-usermanager.h | 8 ++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/library/sysadm-usermanager.cpp b/src/library/sysadm-usermanager.cpp index 9586bf6..929d5e3 100644 --- a/src/library/sysadm-usermanager.cpp +++ b/src/library/sysadm-usermanager.cpp @@ -16,7 +16,7 @@ UserManager::UserManager(QString chroot) loadShells(); } -void UserManager::NewUser(QString fullName, QString userName, QString password, QString shell) +void UserManager::NewUser(QString fullName, QString userName, QString password, QString shell, int uid, int gid) { User user; user.UserName = userName; @@ -44,6 +44,16 @@ void UserManager::NewUser(QString fullName, QString userName, QString password, args << "-m"; //create the user's home directory args << "-s"; //set the user's shell args << shell; //to this + if(gid != -1) + { + args << "-g"; + args << gid; + } + if(uid != -1) + { + args << "-u"; + args << uid; + } args << "-G"; //additionally add the user to args << "operator"; //the operator's group @@ -89,7 +99,7 @@ void UserManager::DeleteUser(User user) if ( ! chroot.isEmpty() ) General::RunCommand("chroot", args); else - General::RunCommand("pw", args); + General::RunCommand("pw", args); loadUsers(); loadGroups(); diff --git a/src/library/sysadm-usermanager.h b/src/library/sysadm-usermanager.h index cc908c8..77ae8b8 100644 --- a/src/library/sysadm-usermanager.h +++ b/src/library/sysadm-usermanager.h @@ -21,6 +21,7 @@ public: HomeFolder = ""; Shell = ""; GroupID = -1; + Encrypted = false; } QString FullName; @@ -29,6 +30,7 @@ public: QString HomeFolder; QString Shell; int GroupID; + bool Encrypted; friend bool operator<(const User lhs, const User rhs){ return std::tie(lhs.ID,lhs.UserName) < std::tie(rhs.ID,rhs.UserName); } @@ -62,7 +64,7 @@ public: class UserManager { -public: +public: UserManager(QString chroot = ""); //#section user actions @@ -72,8 +74,10 @@ public: * @param userName The username of the user * @param password The user's password * @param shell the user's shell, defaults to /bin/tcsh + * @param uid the user id of the user + * @param gid the group id of the user */ - void NewUser(QString fullName, QString userName, QString password, QString shell = "/bin/tcsh"); + void NewUser(QString fullName, QString userName, QString password, QString shell = "/bin/tcsh", int uid = -1, int gid = -1); /** * @brief DeleteUser Deletes a user * @param user the user to delete From 3347669b6e1edfffd8361dfd4fdddd2b488114e1 Mon Sep 17 00:00:00 2001 From: Luke De Mouy Date: Thu, 14 Jan 2016 15:01:04 -0700 Subject: [PATCH 2/2] Copy the PersonaCrypt Functions from the maindlgcode file and wire it up to user creation and password changing --- src/library/sysadm-usermanager.cpp | 182 ++++++++++++++++++++++++++++- src/library/sysadm-usermanager.h | 35 +++++- 2 files changed, 210 insertions(+), 7 deletions(-) diff --git a/src/library/sysadm-usermanager.cpp b/src/library/sysadm-usermanager.cpp index 929d5e3..fa8a0dc 100644 --- a/src/library/sysadm-usermanager.cpp +++ b/src/library/sysadm-usermanager.cpp @@ -16,12 +16,12 @@ UserManager::UserManager(QString chroot) loadShells(); } -void UserManager::NewUser(QString fullName, QString userName, QString password, QString shell, int uid, int gid) +void UserManager::NewUser(QString fullName, QString userName, QString password, QString home, QString shell, int uid, int gid, bool encrypt) { User user; user.UserName = userName; user.FullName = fullName; - user.HomeFolder = "/usr/home/"+userName; + user.HomeFolder = (home.isEmpty())?"/usr/home/"+userName : home; user.Shell = shell; //Add User @@ -42,17 +42,22 @@ void UserManager::NewUser(QString fullName, QString userName, QString password, args << "-c"; //sets the comment field args << "\""+ fullName+"\""; //with the full name of the user args << "-m"; //create the user's home directory + if(!home.isEmpty()) + { + args << "-d"; //set the home directory to + args << home; //this + } args << "-s"; //set the user's shell args << shell; //to this if(gid != -1) { - args << "-g"; - args << gid; + args << "-g"; //set the group id to + args << QString::number(gid); //this } if(uid != -1) { - args << "-u"; - args << uid; + args << "-u"; //set the user id to + args << QString::number(uid); //this } args << "-G"; //additionally add the user to args << "operator"; //the operator's group @@ -74,6 +79,10 @@ void UserManager::NewUser(QString fullName, QString userName, QString password, General::RunCommand("su",args); } + //if we're going to PersonaCrypt the home directory + if(encrypt) + initPCDevice(user,home,password); + //reloads the groups and users so that the internal model is consistent loadUsers(); loadGroups(); @@ -132,6 +141,8 @@ const User UserManager::GetUser(QString userName) void UserManager::ChangeUserPassword(User user, QString newPassword) { + //Don't Change the password of a user with an encrypted Home directory + if( !QFile::exists("/var/db/personacrypt/"+user.UserName+".key") ){ return; } //Create a temporary file to store the password in QTemporaryFile nfile("/tmp/.XXXXXXXX"); @@ -373,3 +384,162 @@ void UserManager::loadShells() // Add /sbin/nologin as well shells.append("/sbin/nologin"); } + + +void UserManager::importPCKey(User user, QString filename){ + //Double check that the key does not exist (button should have been hidden earlier if invalid) + if( QFile::exists("/var/db/personacrypt/"+user.UserName+".key") ){ return; } + + //if the location is empty cancel + if(filename.isEmpty()){ return; } + + //Now run the import command + QStringList args; + args << "import"; + args << "\""+filename + "\""; + if( 0 == General::RunCommand("personacrypt",args) ){ + //Success + qDebug("The key file was imported successfully."); + }else{ + //Failure + qWarning("The key file could not be imported. Please ensure you are using a valid file."); + } +} + +void UserManager::exportPCKey(User user, QString filename){ + //Double check that the key exists (button should have been hidden earlier if invalid) + if( !QFile::exists("/var/db/personacrypt/"+user.UserName+".key") ){ return; } + + if(filename.isEmpty()){ return; } //cancelled + if( !filename.endsWith(".key") ){ filename.append(".key"); } + //Now get/save the key file + QStringList args; + args << "export"; + args << "\"" + user.UserName + "\""; + QString key = General::RunCommand("personacrypt",args); + + QFile file(filename); + if( !file.open(QIODevice::WriteOnly | QIODevice::Truncate) ){ + //Could not open output file + qWarning() <<"Output file could not be opened:\n\n" << filename; + return; + } + QTextStream out(&file); + out << key; + file.close(); + qDebug() << "The PersonaCrypt key has been saved successfully: \n\n" << filename; +} + +void UserManager::disablePCKey(User user){ +//Double check that the key exists (button should have been hidden earlier if invalid) + if( !QFile::exists("/var/db/personacrypt/"+user.UserName+".key") ){ return; } + + if( QFile::remove("/var/db/personacrypt/"+user.UserName+".key") ){ + //Success + qDebug("The PersonaCrypt user key has been disabled." ); + }else{ + //Failure (should almost never happen, since this utility runs as root and just needs to delete a file) + qDebug("The PersonaCrypt user key could not be removed. Do you have the proper permissions?" ); + } +} + +void UserManager::disableAndCopyPCKey(User user, QString password){ + QStringList args; + args << "list"; + QStringList cusers = General::RunCommand("personacrypt",args).split("\n"); + bool available = false; + for(int i=0; i