summaryrefslogtreecommitdiffstats
path: root/src/global.cpp
blob: 91704f3a0a39589bf128f82019098268f5df4ce4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "global.h"
#include "settings.h"
#include <QTimer>
#include <QModelIndex>
#include <QString>
#include <QEventLoop>
#include <QDebug>
#include <QCoreApplication>
#include <QStringList>
#include <QCryptographicHash>
#include <QRegularExpression>
#include <QSet>

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();
}

QString Global::getCombinedIdpWhitelist()
{
	QDir configDir(QLatin1String("/opt/openslx/pam/shibboleth/whitelist"));
	QFileInfoList fileInfoList = configDir.entryInfoList(QStringList() << "*.idp", QDir::Files);

	QSet<QString> list;
	for (QFileInfo fileInfo : fileInfoList) {
		QString filePath = fileInfo.absoluteFilePath();
		QFile f(filePath);
		if (!f.open(QFile::ReadOnly))
			continue;
		while (f.canReadLine()) {
			list << QString::fromUtf8(f.readLine());
		}
		f.close();
	}
	QString retval;
	for (const QString &s : list) {
		if (!retval.isEmpty()) {
			retval.append(QLatin1Char(' '));
		}
		retval += s;
	}
	return retval;
}