Update a bit of the new "adduser" routine quite a bit, adding support for personacrypt init/import options as well (untested).

This commit is contained in:
Ken Moore
2016-07-22 13:20:23 -04:00
parent f586a30d77
commit 6ed9dd4e74
5 changed files with 28 additions and 72 deletions

View File

@@ -20,8 +20,9 @@
#include "library/sysadm-update.h"
#include "library/sysadm-zfs.h"
#include "library/sysadm-pkg.h"
#include "library/sysadm-users.h"
#include <QTemporaryFile>
#define DEBUG 0
//#define SCLISTDELIM QString("::::") //SysCache List Delimiter
@@ -902,76 +903,14 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmPkgRequest(const QJsonValue
// ==== SYSADM USER API ====
RestOutputStruct::ExitCode WebSocket::EvaluateSysadmUserRequest(bool allaccess, QString user, const QJsonValue in_args, QJsonObject *out){
bool ok = false;
QJsonObject obj = in_args.toObject();
//REQUIRED: "action"
QString action =obj.value("action").toString().toLower();
QString action =in_args.toObject().value("action").toString().toLower();
if(action=="usershow"){
QStringList args; args << "usershow";
if(allaccess){ args << "-a"; }
else{ args << user; }
QStringList users = sysadm::General::RunCommand(ok, "pw", args, "",QStringList() << "MM_CHARSET=UTF-8").split("\n");
if(ok){
//Go ahead and parse/list all the users
for(int i=0; i<users.length(); i++){
QStringList info = users[i].split(":");
if(info.length() == 10){
QJsonObject uinfo;
uinfo.insert("name", info[0]);
//uinfo.insert("name", info[1]); //Skip Password field (just a "*" in this viewer anyway)
uinfo.insert("uid", info[2]);
uinfo.insert("gid", info[3]);
uinfo.insert("class", info[4]);
uinfo.insert("change", info[5]);
uinfo.insert("expire", info[6]);
uinfo.insert("comment", info[7]);
uinfo.insert("home_dir", info[8]);
uinfo.insert("shell", info[9]);
out->insert(info[0], uinfo); //use the username as the unique object name
}else if(info.length() == 7){
QJsonObject uinfo;
uinfo.insert("name", info[0]);
//uinfo.insert("name", info[1]); //Skip Password field (just a "*" in this viewer anyway)
uinfo.insert("uid", info[2]);
uinfo.insert("gid", info[3]);
uinfo.insert("comment", info[4]);
uinfo.insert("home_dir", info[5]);
uinfo.insert("shell", info[6]);
out->insert(info[0], uinfo); //use the username as the unique object name
}
}
}else{
//Bad result from "pw" - inputs were just fine (just return nothing)
ok = true;
}
}else if(action=="useradd" && allaccess){
//REQUIRED: ("name" OR "uid") AND "password"
//OPTIONAL: "comment", "home_dir", "expire", "change", "shell", "group", "other_groups", "class"
if(obj.contains("password") && (obj.contains("name") || obj.contains("uid")) ){
QStringList args; args << "useradd";
if(obj.contains("name")){ args << "-n" << obj.value("name").toString(); }
if(obj.contains("uid")){ args << "-u" << obj.value("uid").toString(); }
if(obj.contains("comment")){ args << "-c" << obj.value("comment").toString(); }
if(obj.contains("home_dir")){ args << "-d" << obj.value("home_dir").toString(); }
if(obj.contains("expire")){ args << "-e" << obj.value("expire").toString(); }
if(obj.contains("change")){ args << "-p" << obj.value("change").toString(); }
if(obj.contains("shell")){ args << "-s" << obj.value("shell").toString(); }
if(obj.contains("group")){ args << "-g" << obj.value("group").toString(); }
if(obj.contains("other_groups")){
if(obj.value("other_groups").isString()){ args << "-G" << obj.value("other_groups").toString(); }
else if(obj.value("other_groups").isArray()){ args << "-G" << JsonArrayToStringList(obj.value("other_groups").toArray()).join(","); }
}
if(obj.contains("class")){ args << "-L" << obj.value("class").toString(); }
QTemporaryFile pwfile;
if(pwfile.open()){
qDebug() << "[DEBUG] Opened temporary file to create a user";
pwfile.write( obj.value("password").toString().toUtf8().data() );
pwfile.close(); //closed but still exists - will go out of scope and get removed in a moment
args << "-h" << "0"; //read from std input
ok = (0== system("cat "+pwfile.fileName().toUtf8()+" | pw "+args.join(" ").toUtf8()) );
qDebug() << "[DEBUG] Finished creating user:" << ok;
}else{ qDebug() << "[DEBUG] Could not open temporary file to create a user"; }
}
ok = sysadm::UserManager::listUsers(out, allaccess, user);
}else if(action=="useradd" && allaccess){ //requires all access to create new users
ok = sysadm::UserManager::addUser(out, in_args.toObject());
}
return (ok ? RestOutputStruct::OK : RestOutputStruct::BADREQUEST);
}