mirror of
https://github.com/outbackdingo/sysadm.git
synced 2026-01-27 10:20:26 +00:00
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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ HEADERS += $${PWD}/sysadm-global.h \
|
||||
$${PWD}/sysadm-servicemanager.h\
|
||||
$${PWD}/sysadm-systemmanager.h\
|
||||
$${PWD}/sysadm-update.h \
|
||||
$${PWD}/sysadm-usermanager.h \
|
||||
$${PWD}/sysadm-users.h \
|
||||
$${PWD}/sysadm-zfs.h \
|
||||
$${PWD}/sysadm-pkg.h
|
||||
|
||||
@@ -29,7 +29,6 @@ SOURCES += $${PWD}/NetDevice.cpp \
|
||||
$${PWD}/sysadm-servicemanager.cpp \
|
||||
$${PWD}/sysadm-systemmanager.cpp \
|
||||
$${PWD}/sysadm-update.cpp \
|
||||
$${PWD}/sysadm-usermanager.cpp \
|
||||
$${PWD}/sysadm-users.cpp \
|
||||
$${PWD}/sysadm-zfs.cpp \
|
||||
$${PWD}/sysadm-pkg.cpp
|
||||
|
||||
|
||||
@@ -97,6 +97,18 @@ bool General::writeTextFile(QString filepath, QStringList contents, bool overwri
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
//== JsonArrayToStringList() ==
|
||||
QStringList General::JsonArrayToStringList(QJsonArray array){
|
||||
//Note: This assumes that the array is only values, not additional objects
|
||||
QStringList out;
|
||||
for(int i=0; i<array.count(); i++){
|
||||
out << array.at(i).toString();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
//== getConfFileValue() ==
|
||||
QString General::getConfFileValue(QString fileName, QString Key, int occur )
|
||||
{
|
||||
int found = 1;
|
||||
|
||||
@@ -26,6 +26,10 @@ public:
|
||||
static QStringList readTextFile(QString filename);
|
||||
static bool writeTextFile(QString filename, QStringList contents, bool overwrite = true);
|
||||
|
||||
|
||||
//JSON Array to QStringList simplification
|
||||
static QStringList JsonArrayToStringList(QJsonArray);
|
||||
|
||||
/**
|
||||
* @brief getConfFileValue get the value associated with a key in a config file
|
||||
* @param fileName the file to read from
|
||||
|
||||
@@ -14,11 +14,13 @@
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonValue>
|
||||
#include <QProcess>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QTextStream>
|
||||
#include <QTemporaryFile>
|
||||
|
||||
//FreeBSD Includes
|
||||
#include <sys/types.h>
|
||||
|
||||
Reference in New Issue
Block a user