ukui-control-center/ 0000755 0001750 0001750 00000000000 14777174564 013435 5 ustar feng feng ukui-control-center/group-manager-server/ 0000755 0001750 0001750 00000000000 14604741160 017462 5 ustar feng feng ukui-control-center/group-manager-server/group_manager_server.h 0000664 0001750 0001750 00000004723 14604741160 024057 0 ustar feng feng /*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see .
*
**/
#include
#include
#include
#include
#include
#include
#include
#include
#include "custom_struct.h"
class group_manager_server : public QObject,QDBusContext
{
Q_OBJECT
//定义Interface名称
Q_CLASSINFO("D-Bus Interface", "org.ukui.groupmanager.interface")
Q_PROPERTY(bool NoPwdLoginStatus READ NoPwdLoginStatus)
public:
explicit group_manager_server();
bool authoriyEdit();
void notifyPropertyChanged(const QString& interface, const QString &propertyName);
bool NoPwdLoginStatus() const;
private:
bool _addUserToGroup(QString groupName, QString userName);
bool _delUserFromGroup(QString groupName, QString userName);
bool _changeOtherUserPasswd(QString username, QString pwd);
public slots:
QVariantList getGroup();
QVariantList getPasswd();
bool add(QString groupName, QString groupId);
bool set(QString groupName, QString groupId);
bool del(QString groupName);
bool addUserToGroup(QString groupName, QStringList userNameList);
bool delUserFromGroup(QString groupName, QStringList userNameList);
bool changeOtherUserPasswd(QString username, QString pwd);
bool createUser(QString name, QString fullname, int accounttype, QString faceicon, QString pwd);
bool setNoPwdLoginStatus(bool status,QString username = nullptr);
QString getNoPwdLoginStatus();
bool setAutomaticLogin(QString objpath, bool enabled);
bool setIconFile(QString username, QString objpath, QString filename);
bool setAccountType(QString objpath, int accountType);
bool deleteUser(qint64 id, bool removeFiles);
private:
QList value;
qint64 _id = 0;
bool authoriy = true;
bool NoPwdLogin;
signals:
void message();
};
ukui-control-center/group-manager-server/conf/ 0000755 0001750 0001750 00000000000 14552105260 020404 5 ustar feng feng ukui-control-center/group-manager-server/conf/ukui-group-manager.service 0000644 0001750 0001750 00000000213 14552105260 025501 0 ustar feng feng [Unit]
Description=group-manager-server
[Service]
User=root
ExecStart=/usr/bin/group-manager-server
[Install]
WantedBy=multi-user.target
ukui-control-center/group-manager-server/conf/org.ukui.groupmanager.service 0000644 0001750 0001750 00000000130 14552105260 026211 0 ustar feng feng [D-BUS Service]
Name=org.ukui.groupmanager
Exec=/usr/bin/group-manager-server
User=root
ukui-control-center/group-manager-server/conf/org.ukui.groupmanager.conf 0000644 0001750 0001750 00000001323 14552105260 025503 0 ustar feng feng
ukui-control-center/group-manager-server/conf/org.ukui.groupmanager.policy 0000644 0001750 0001750 00000002155 14552105260 026061 0 ustar feng feng
KYLINOS
http://www.kylinos.cn
ues
group tools
用户组
སྤྱད་མཁན་ཚོ་
Authorization required to modify user data.
需要授权以修改用户数据
སྤྱོད་མཁན་གྱི་གཞི་གྲངས་ལ་བརྟེན་ནས་བཟོ་བཅོས་བྱེད་དབང་བསྐུར་དགོས་།
auth_admin_keep
auth_admin_keep
auth_admin_keep
ukui-control-center/group-manager-server/group-manager-server.pro 0000644 0001750 0001750 00000003040 14557626106 024262 0 ustar feng feng TEMPLATE = app
TARGET = group-manager-server
INCLUDEPATH += .
QT += dbus core
# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# Input
DBUS_ADAPTORS += org.ukui.groupmanager.xml
DBUS_INTERFACES += org.ukui.groupmanager.xml
CONFIG += console c++11 link_pkgconfig
PKGCONFIG += gio-2.0 \
gio-unix-2.0 \
LIBS += -L$$[QT_INSTALL_LIBS] -lpolkit-qt5-core-1
HEADERS += \
custom_struct.h \
group_manager_server.h
SOURCES += \
group_manager_server.cpp \
main.cpp
target.path = /usr/bin/
!isEmpty(target.path): INSTALLS += target
dbus_conf.path = /usr/share/dbus-1/system.d
dbus_conf.files += conf/org.ukui.groupmanager.conf
INSTALLS += dbus_conf
systemd_service.path = /usr/share/dbus-1/system-services/
systemd_service.files += conf/org.ukui.groupmanager.service
INSTALLS += systemd_service
polkit.path = /usr/share/polkit-1/actions/
polkit.files += conf/org.ukui.groupmanager.policy
INSTALLS += polkit
ukui-control-center/group-manager-server/custom_struct.h 0000644 0001750 0001750 00000003014 14557626106 022560 0 ustar feng feng /*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see .
*
**/
#include
#include
#ifndef CUSTOM_STRUCT
#define CUSTOM_STRUCT
struct custom_struct
{
QString groupname;
QString passphrase;
QString groupid;
QString usergroup;
friend QDBusArgument &operator<<(QDBusArgument &argument, const custom_struct&mystruct)
{
argument.beginStructure();
argument << mystruct.groupname << mystruct.passphrase << mystruct.groupid << mystruct.usergroup;
argument.endStructure();
return argument;
}
friend const QDBusArgument &operator>>(const QDBusArgument &argument, custom_struct&mystruct)
{
argument.beginStructure();
argument >> mystruct.groupname >> mystruct.passphrase >> mystruct.groupid >> mystruct.usergroup;
argument.endStructure();
return argument;
}
};
Q_DECLARE_METATYPE(custom_struct)
#endif
ukui-control-center/group-manager-server/group_manager_server.cpp 0000664 0001750 0001750 00000036716 14604741160 024421 0 ustar feng feng /*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see .
*
**/
#include "group_manager_server.h"
#include "custom_struct.h"
#include
#include
#include
#include
#include
#include
#include
group_manager_server::group_manager_server()
: QDBusContext()
{
}
// 解析组文件
QVariantList group_manager_server::getGroup()
{
const QString fileName = "/etc/group";
QFile groupFile(fileName);
QVariantList value;
QVariant cnt;
int lineCount = 1;
if(!groupFile.exists()){
printf("/etc/group file not exist \n");
}
if(!groupFile.open(QIODevice::ReadOnly | QIODevice::Text)){
printf("open /etc/group fail \n");
}
QTextStream in(&groupFile);
QString line = in.readLine();
struct custom_struct demo[200];
while(!line.isNull()){
QStringList lineList = line.split(":");
line = in.readLine();
demo[lineCount].groupname = lineList.at(0);
demo[lineCount].passphrase = lineList.at(1);
demo[lineCount].groupid = lineList.at(2);
demo[lineCount].usergroup = lineList.at(3);
cnt = QVariant::fromValue(demo[lineCount]);
value << cnt;
lineCount ++;
}
return value;
}
// 解析passwd文件
QVariantList group_manager_server::getPasswd()
{
const QString fileName = "/etc/passwd";
QFile passwdFile(fileName);
QVariantList value;
QVariant cnt;
int lineCount = 1;
if(!passwdFile.exists()){
printf("/etc/passwd file not exist \n");
}
if(!passwdFile.open(QIODevice::ReadOnly | QIODevice::Text)){
printf("open /etc/passwd fail \n");
}
QTextStream in(&passwdFile);
QString line = in.readLine();
struct custom_struct demo[200];
while(!line.isNull()){
QStringList lineList = line.split(":");
line = in.readLine();
demo[lineCount].groupname = lineList.at(0);
demo[lineCount].passphrase = lineList.at(1);
demo[lineCount].groupid = lineList.at(3);
cnt = QVariant::fromValue(demo[lineCount]);
value << cnt;
lineCount ++;
}
return value;
}
// 添加组
bool group_manager_server::add(QString groupName, QString groupId)
{
//密码校验
if (!authoriyEdit()){
return false;
}
QString groupadd = "/usr/sbin/groupadd";
QString addgroup = "/usr/sbin/addgroup";
QString command;
QFile groupaddFile("/usr/sbin/addgroup");
QFile addgroupFile("/usr/sbin/groupadd");
QProcess p(0);
QStringList args;
if(!addgroupFile.exists()){
printf("/usr/sbin/addgroup file not exist \n");
if(!groupaddFile.exists()){
return false;
}
command = groupadd;
args.append("-g");
args.append(groupId);
args.append(groupName);
}else{
command = addgroup;
args.append("-gid");
args.append(groupId);
args.append(groupName);
}
p.execute(command,args);//command是要执行的命令,args是参数
p.waitForFinished(-1);
// qDebug()<servicePid(msg.service()).value();
if (_id == 0)
return false;
PolkitQt1::Authority::Result result;
result = PolkitQt1::Authority::instance()->checkAuthorizationSync(
"org.ukui.groupmanager.action.edit",
PolkitQt1::UnixProcessSubject(_id),
PolkitQt1::Authority::AllowUserInteraction);
if (result == PolkitQt1::Authority::Yes){
_id = 0;
return true;
} else {
_id = 0;
return false;
}
}
bool group_manager_server::_delUserFromGroup(QString groupName, QString userName)
{
QString gpasswd = "/usr/bin/gpasswd";
QString command;
QFile gpasswdFile(gpasswd);
QProcess p(0);
QStringList args;
if(!gpasswdFile.exists()){
printf("/usr/sbin/gpasswd file not exist \n");
return false;
}
command = gpasswd;
args.append("-d");
args.append(userName);
args.append(groupName);
p.execute(command,args);//command是要执行的命令,args是参数
p.waitForFinished(-1);
// qDebug() << QString::fromLocal8Bit(p.readAllStandardError());
return true;
}
// 删除用户从组
bool group_manager_server::delUserFromGroup(QString groupName, QStringList userNameList)
{
//密码校验
if (!authoriyEdit()){
return false;
}
foreach (QString userName,userNameList) {
bool ret = _delUserFromGroup(groupName, userName);
if (!ret) {
qDebug() << "deluser " << userName << " to " << groupName << " failed!";
}
}
return true;
}
bool group_manager_server::_changeOtherUserPasswd(QString username, QString pwd)
{
std::string str1 = username.toStdString();
const char * user_name = str1.c_str();
QString output;
QString newPwd = pwd;
int i = 0;
for (i = 0; i < newPwd.count(); i++){
if (!(int(newPwd.at(i).toLatin1() >= 48 && int(newPwd.at(i).toLatin1()) <= 57) ||
int(newPwd.at(i).toLatin1() >= 65 && int(newPwd.at(i).toLatin1()) <= 90) ||
int(newPwd.at(i).toLatin1() >= 97 && int(newPwd.at(i).toLatin1()) <= 122))){
newPwd = newPwd.insert(i, QString("\\"));
i++;
}
}
std::string str2 = newPwd.toStdString();
const char * passwd = str2.c_str();
char * cmd = g_strdup_printf("/usr/bin/changeotheruserpwd '%s' %s", user_name, passwd);
FILE *stream;
char buf[256];
if ((stream = popen(cmd, "r" )) == NULL){
return false;
}
while(fgets(buf, 256, stream) != NULL){
output = QString(buf).simplified();
}
pclose(stream);
return true;
}
// 修改其他用户密码
bool group_manager_server::changeOtherUserPasswd(QString username, QString pwd)
{
//密码校验
if (!authoriyEdit()){
return false;
}
bool _changeRet = _changeOtherUserPasswd(username, pwd);
return _changeRet;
}
// 创建新用户
bool group_manager_server::createUser(QString name, QString fullname, int accounttype, QString faceicon, QString pwd)
{
//密码校验
if (!authoriyEdit()){
return false;
}
QDBusInterface iface("org.freedesktop.Accounts",
"/org/freedesktop/Accounts",
"org.freedesktop.Accounts",
QDBusConnection::systemBus());
QDBusReply reply = iface.call("CreateUser", name, fullname, accounttype);
if (reply.isValid()){
QString op = reply.value().path();
if (!op.isEmpty()){
QDBusInterface ifaceUser("org.freedesktop.Accounts",
op,
"org.freedesktop.Accounts.User",
QDBusConnection::systemBus());
// 设置头像
ifaceUser.call("SetIconFile", faceicon);
// 设置密码
_changeOtherUserPasswd(name, pwd);
}
}
return true;
}
//获取免密登录状态
QString group_manager_server::getNoPwdLoginStatus()
{
QByteArray ba;
FILE * fp = NULL;
char cmd[128];
char buf[1024];
snprintf(cmd, 128, "cat /etc/group |grep nopasswdlogin");
if ((fp = popen(cmd, "r")) != NULL){
rewind(fp);
fgets(buf, sizeof (buf), fp);
ba.append(buf);
pclose(fp);
fp = NULL;
}else{
qDebug()<<"popen文件打开失败"<