mirror of
https://gitlab.com/Syping/mayu
synced 2024-11-22 12:00:23 +01:00
drop/regain privileges at file ops
This commit is contained in:
parent
201f20999b
commit
15e7a40fa6
3 changed files with 50 additions and 13 deletions
1
main.cpp
1
main.cpp
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
QCoreApplication::setSetuidAllowed(true);
|
||||||
QCoreApplication a(argc, argv);
|
QCoreApplication a(argc, argv);
|
||||||
a.setApplicationName("mayu");
|
a.setApplicationName("mayu");
|
||||||
|
|
||||||
|
|
59
mayu.cpp
59
mayu.cpp
|
@ -143,12 +143,12 @@ double mayu::ping(const QString &host, int tries, double timeout)
|
||||||
bool pingSuccess = false;
|
bool pingSuccess = false;
|
||||||
for (pingIter = ping_iterator_get(pingObj); pingIter != NULL; pingIter =
|
for (pingIter = ping_iterator_get(pingObj); pingIter != NULL; pingIter =
|
||||||
ping_iterator_next(pingIter)) {
|
ping_iterator_next(pingIter)) {
|
||||||
char hostname[100];
|
|
||||||
size_t len;
|
size_t len;
|
||||||
len = sizeof(double);
|
len = sizeof(double);
|
||||||
ping_iterator_get_info(pingIter, PING_INFO_LATENCY, &latency, &len);
|
ping_iterator_get_info(pingIter, PING_INFO_LATENCY, &latency, &len);
|
||||||
pingSuccess = !(latency < 0);
|
pingSuccess = !(latency < 0);
|
||||||
#ifdef E_DEBUG
|
#ifdef E_DEBUG
|
||||||
|
char hostname[100];
|
||||||
len = 100;
|
len = 100;
|
||||||
ping_iterator_get_info(pingIter, PING_INFO_HOSTNAME, hostname, &len);
|
ping_iterator_get_info(pingIter, PING_INFO_HOSTNAME, hostname, &len);
|
||||||
qDebug() << hostname << latency << pingSuccess;
|
qDebug() << hostname << latency << pingSuccess;
|
||||||
|
@ -168,9 +168,10 @@ double mayu::ping(const QString &host, int tries, double timeout)
|
||||||
void mayu::parse_hosts()
|
void mayu::parse_hosts()
|
||||||
{
|
{
|
||||||
p_hostsList.clear();
|
p_hostsList.clear();
|
||||||
/**
|
if (!dropPrivileges()) {
|
||||||
Drop here
|
p_return = 2;
|
||||||
**/
|
return;
|
||||||
|
}
|
||||||
QFile hostsFile(p_hostsFile);
|
QFile hostsFile(p_hostsFile);
|
||||||
if (hostsFile.open(QFile::ReadOnly)) {
|
if (hostsFile.open(QFile::ReadOnly)) {
|
||||||
const QList<QByteArray> hostsArray = hostsFile.readAll().split('\n');
|
const QList<QByteArray> hostsArray = hostsFile.readAll().split('\n');
|
||||||
|
@ -197,9 +198,10 @@ void mayu::parse_hosts()
|
||||||
{
|
{
|
||||||
cerr << "Failed read hosts from " << p_hostsFile.toStdString().c_str();
|
cerr << "Failed read hosts from " << p_hostsFile.toStdString().c_str();
|
||||||
}
|
}
|
||||||
/**
|
if (!regainPrivileges()) {
|
||||||
Regain here
|
p_return = 3;
|
||||||
**/
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mayu::work()
|
void mayu::work()
|
||||||
|
@ -215,9 +217,10 @@ void mayu::work()
|
||||||
QJsonDocument jsonDocument;
|
QJsonDocument jsonDocument;
|
||||||
jsonDocument.setObject(jsonObject);
|
jsonDocument.setObject(jsonObject);
|
||||||
QByteArray jsonArray = jsonDocument.toJson();
|
QByteArray jsonArray = jsonDocument.toJson();
|
||||||
/**
|
if (!dropPrivileges()) {
|
||||||
Drop here
|
p_return = 2;
|
||||||
**/
|
return;
|
||||||
|
}
|
||||||
QSaveFile jsonFile(p_jsonFile);
|
QSaveFile jsonFile(p_jsonFile);
|
||||||
if (jsonFile.open(QSaveFile::WriteOnly)) {
|
if (jsonFile.open(QSaveFile::WriteOnly)) {
|
||||||
jsonFile.write(jsonArray);
|
jsonFile.write(jsonArray);
|
||||||
|
@ -226,8 +229,38 @@ void mayu::work()
|
||||||
p_return = 1;
|
p_return = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
if (!regainPrivileges()) {
|
||||||
Regain here
|
p_return = 3;
|
||||||
**/
|
return;
|
||||||
|
}
|
||||||
p_return = 0;
|
p_return = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mayu::dropPrivileges()
|
||||||
|
{
|
||||||
|
#if _POSIX_SAVED_IDS
|
||||||
|
p_uid = geteuid();
|
||||||
|
int status = seteuid(getuid());
|
||||||
|
if (status != 0) {
|
||||||
|
cerr << "Dropping of privileges has failed!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mayu::regainPrivileges()
|
||||||
|
{
|
||||||
|
#if _POSIX_SAVED_IDS
|
||||||
|
int status = seteuid(p_uid);
|
||||||
|
if (status != 0) {
|
||||||
|
cerr << "Regaining of privileges has failed!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
3
mayu.h
3
mayu.h
|
@ -42,12 +42,15 @@ public slots:
|
||||||
void work();
|
void work();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
bool dropPrivileges();
|
||||||
|
bool regainPrivileges();
|
||||||
QStringList p_hostsList;
|
QStringList p_hostsList;
|
||||||
QString p_hostsFile;
|
QString p_hostsFile;
|
||||||
QString p_jsonFile;
|
QString p_jsonFile;
|
||||||
bool p_hostsParsed;
|
bool p_hostsParsed;
|
||||||
int p_return;
|
int p_return;
|
||||||
int p_tries;
|
int p_tries;
|
||||||
|
uid_t p_uid;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MAYU_H
|
#endif // MAYU_H
|
||||||
|
|
Loading…
Reference in a new issue