summaryrefslogblamecommitdiffstats
path: root/src/global.cpp
blob: ab2519beacfff92835df12d751463b557d8c125a (plain) (tree)
1
2
3
4
5
6
7
8
9
                   
                     





                           
                      

                             











































                                                                                                                                            



                                  
                                                        























                                                                     



































                                                                




















                                                                                                                             
#include "global.h"
#include "settings.h"
#include <QTimer>
#include <QModelIndex>
#include <QString>
#include <QEventLoop>
#include <QDebug>
#include <QCoreApplication>
#include <QStringList>
#include <QCryptographicHash>
#include <QRegularExpression>

bool Global::m_testMode = false;

QLightDM::Greeter* Global::m_Greeter = nullptr;
QLightDM::PowerInterface* Global::m_Power = nullptr;
QLightDM::SessionsModel* Global::m_Sessions = nullptr;

void Global::initGreeter()
{
	m_Greeter = new QLightDM::Greeter();
	if (!m_Greeter->connectSync()) {
		m_Greeter->deleteLater();
		m_Greeter = nullptr;
	}
}

bool Global::autoLoginGuest()
{
	GreeterCb *cb = new GreeterCb();
	GreeterCb::connect(greeter(), SIGNAL(authenticationComplete()), cb, SLOT(authenticationComplete()));
	GreeterCb::connect(greeter(), SIGNAL(autologinTimerExpired()), cb, SLOT(autologinTimerExpired()));
	GreeterCb::connect(greeter(), SIGNAL(reset()), cb, SLOT(reset()));
	qWarning() << "Trying to auth as guest";
	greeter()->authenticateAsGuest();
	QTimer::singleShot(3000, cb, SLOT(customTimeout()));
	while (!cb->authComplete && !cb->authError && greeter()->inAuthentication()) {
		QCoreApplication::instance()->processEvents(QEventLoop::AllEvents);
	}
	qWarning() << "Complete:" << cb->authComplete << "Error:"
			<< cb->authError << "InAuth:" << greeter()->inAuthentication() << "isAuthenticated" << greeter()->isAuthenticated();
	if (cb->authComplete && greeter()->isAuthenticated()) {
		cb->deleteLater();
		return startSession();
	}
	cb->deleteLater();
	return false;
}

bool Global::startSession()
{
	QModelIndex i = sessions()->index(0, 0);
	QString s = m_Sessions->data(i, QLightDM::SessionsModel::KeyRole).toString();
	return m_Greeter->startSessionSync(s);
}

QImage Global::getConfigGradient()
{
    QImage img;
    const QStringList cols = Settings::gradientColors();
    qWarning() << "Got gradient color list: " << cols;
    if (cols.length() == 4 || cols.length() == 2) {
        bool ok = true;
        uint a, b, c, d;
        if (ok) a = cols.at(0).toUInt(&ok, 16) | 0xff000000;
        if (ok) b = cols.at(1).toUInt(&ok, 16) | 0xff000000;
        if (cols.length() == 4) {
            if (ok) c = cols.at(2).toUInt(&ok, 16) | 0xff000000;
            if (ok) d = cols.at(3).toUInt(&ok, 16) | 0xff000000;
        } else if (ok) {
            c = b;
        }
        if (ok) {
            img = QImage(cols.length() / 2, 2, QImage::Format_RGB32);
            img.setPixel(0, 0, a);
            img.setPixel(0, 1, c);
            if (cols.length() == 4) {
                img.setPixel(1, 0, b);
                img.setPixel(1, 1, d);
            }
        }
    }
    return img;
}

QStringList loadUrlList(const QString &file)
{
	QStringList stringList;
	QFile textFile(file);
	if (!textFile.open(QFile::ReadOnly)) {
		QTextStream(stdout) << "Cannot open URL list\n";
		return QStringList();
	}
	QTextStream textStream(&textFile);
	while (true)
	{
		QString line = textStream.readLine();
		if (line.isNull())
			break;
		else
			stringList.append(line);
	}
	return stringList;
}

QStringList Global::urlBlacklist()
{
    auto path = Settings::urlBlacklistFile();
    if (!QFile::exists(path))
        return QStringList();
    return loadUrlList(path);
}

QStringList Global::urlWhitelist()
{
    auto path = Settings::urlWhitelistFile();
    if (!QFile::exists(path))
        return QStringList();
    return loadUrlList(path);
}

void Global::writeCowToken(const QString &user, const QString &token)
{
    QString userHash = QString::fromLocal8Bit(QCryptographicHash::hash(user.toLocal8Bit(), QCryptographicHash::Md5).toHex());
    QFile file(QLatin1String("/run/openslx/lightdm/") + userHash);
    if (file.open(QFile::WriteOnly | QFile::Truncate)) {
        file.write(token.toLocal8Bit());
        file.close();
        file.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner);
    }
}

bool Global::isValidShibCreds(const QString &ustr, const QString &upass)
{
    static QRegularExpression R_USER("^[a-z_A-Z][a-zA-Z0-9_@.-]{1,32}$");
    static QRegularExpression R_PASS("^[a-z0-9]{1,32}$");

    return ustr.contains('@')
            && R_USER.match(ustr).hasMatch()
            && R_PASS.match(upass).hasMatch();
}