/
ssucoreconfig.cpp
178 lines (153 loc) · 4.59 KB
/
ssucoreconfig.cpp
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
169
170
171
172
173
174
175
176
177
178
/**
* @file ssucoreconfig.cpp
* @copyright 2013 Jolla Ltd.
* @author Bernd Wachter <bwachter@lart.info>
* @date 2013
*/
#include <QFile>
#include <QTextStream>
#include <QDBusConnection>
#include <getdef.h>
#include <sys/types.h>
#include <unistd.h>
#include "ssucoreconfig_p.h"
SsuCoreConfig *SsuCoreConfig::ssuCoreConfig = 0;
SsuCoreConfig *SsuCoreConfig::instance()
{
if (!ssuCoreConfig)
ssuCoreConfig = new SsuCoreConfig;
return ssuCoreConfig;
}
QPair<QString, QString> SsuCoreConfig::credentials(const QString &scope)
{
QPair<QString, QString> ret;
beginGroup("credentials-" + scope);
ret.first = value("username").toString();
ret.second = value("password").toString();
endGroup();
return ret;
}
QString SsuCoreConfig::credentialsScope(const QString &repoName, bool rndRepo)
{
Q_UNUSED(repoName)
Q_UNUSED(rndRepo)
if (contains("credentials-scope"))
return value("credentials-scope").toString();
else
return "your-configuration-is-broken-and-does-not-contain-credentials-scope";
}
QString SsuCoreConfig::credentialsUrl(const QString &scope)
{
if (contains("credentials-url-" + scope))
return value("credentials-url-" + scope).toString();
else
return "your-configuration-is-broken-and-does-not-contain-credentials-url-for-" + scope;
}
QString SsuCoreConfig::flavour()
{
if (contains("flavour"))
return value("flavour").toString();
else
return "release";
}
Ssu::DeviceModeFlags SsuCoreConfig::deviceMode()
{
if (!contains("deviceMode"))
return Ssu::ReleaseMode;
else
return Ssu::DeviceModeFlags(value("deviceMode").toInt());
}
QString SsuCoreConfig::domain(bool pretty)
{
if (contains("domain")) {
if (pretty)
return value("domain").toString().replace(":", "-");
else
return value("domain").toString();
} else {
return QString();
}
}
bool SsuCoreConfig::isRegistered()
{
if (!contains("privateKey"))
return false;
if (!contains("certificate"))
return false;
return value("registered").toBool();
}
QDateTime SsuCoreConfig::lastCredentialsUpdate()
{
return value("lastCredentialsUpdate").toDateTime();
}
QString SsuCoreConfig::release(bool rnd)
{
if (rnd)
return value("rndRelease").toString();
else
return value("release").toString();
}
void SsuCoreConfig::setDeviceMode(Ssu::DeviceModeFlags mode, enum Ssu::EditMode editMode)
{
int oldMode = value("deviceMode").toInt();
if ((editMode & Ssu::Add) == Ssu::Add) {
oldMode |= mode;
} else if ((editMode & Ssu::Remove) == Ssu::Remove) {
oldMode &= ~mode;
} else {
oldMode = mode;
}
setValue("deviceMode", oldMode);
sync();
}
void SsuCoreConfig::setFlavour(const QString &flavour)
{
setValue("flavour", flavour);
// flavour is RnD only, so enable RnD mode
setDeviceMode(Ssu::RndMode, Ssu::Add);
sync();
}
void SsuCoreConfig::setRelease(const QString &release, bool rnd)
{
if (rnd) {
setValue("rndRelease", release);
// switch rndMode on/off when setting releases
setDeviceMode(Ssu::RndMode, Ssu::Add);
} else {
setValue("release", release);
setDeviceMode(Ssu::RndMode, Ssu::Remove);
}
sync();
}
void SsuCoreConfig::setDomain(const QString &domain)
{
// - in domain messes with default section autodetection,
// so change it to :
setValue("domain", QString(domain).replace("-", ":"));
sync();
}
bool SsuCoreConfig::useSslVerify()
{
if (contains("ssl-verify"))
return value("ssl-verify").toBool();
else
return true;
}
QDBusConnection SsuCoreConfig::userSessionBus()
{
int uid_min = getdef_num("UID_MIN", -1);
// For calls from valid UID we assume that they are properly logged in users.
// If they are not the call will fail, but it's their fault.
if (getuid() >= static_cast<uid_t>(uid_min)) {
return QDBusConnection::sessionBus();
} else {
// DBus security policy will prevent this beeing used by callers other
// than root at the moment. Still do it generic in case DBus policy will
// be extended later, and just use the usual 'DBus: THOU SHALL NOT PASS!'
// @TODO the uid to be used should be determined using the logind API from
// systemd package to support multiuser systems in the future
QString sessionBusAddress = QString("unix:path=/run/user/%1/dbus/user_bus_socket")
.arg(uid_min);
return QDBusConnection::connectToBus(sessionBusAddress, "userSessionBus");
}
}