ssu
Loading...
Searching...
No Matches
ssucoreconfig.cpp
Go to the documentation of this file.
1
7
8#include <QFile>
9#include <QTextStream>
10#include <QDBusConnection>
11
12#include <getdef.h>
13#include <sys/types.h>
14#include <unistd.h>
15
16#include "ssucoreconfig_p.h"
17
18SsuCoreConfig *SsuCoreConfig::ssuCoreConfig = 0;
19
20SsuCoreConfig *SsuCoreConfig::instance()
21{
22 if (!ssuCoreConfig)
23 ssuCoreConfig = new SsuCoreConfig;
24
25 return ssuCoreConfig;
26}
27
28QPair<QString, QString> SsuCoreConfig::credentials(const QString &scope)
29{
30 QPair<QString, QString> ret;
31 beginGroup("credentials-" + scope);
32 ret.first = value("username").toString();
33 ret.second = value("password").toString();
34 endGroup();
35 return ret;
36}
37
38QString SsuCoreConfig::credentialsScope(const QString &repoName, bool rndRepo)
39{
40 Q_UNUSED(repoName)
41 Q_UNUSED(rndRepo)
42
43 if (contains("credentials-scope"))
44 return value("credentials-scope").toString();
45 else
46 return "your-configuration-is-broken-and-does-not-contain-credentials-scope";
47}
48
49QString SsuCoreConfig::credentialsUrl(const QString &scope)
50{
51 if (contains("credentials-url-" + scope))
52 return value("credentials-url-" + scope).toString();
53 else
54 return "your-configuration-is-broken-and-does-not-contain-credentials-url-for-" + scope;
55}
56
58{
59 if (contains("flavour"))
60 return value("flavour").toString();
61 else
62 return "release";
63}
64
65Ssu::DeviceModeFlags SsuCoreConfig::deviceMode()
66{
67 if (!contains("deviceMode"))
68 return Ssu::ReleaseMode;
69 else
70 return Ssu::DeviceModeFlags(value("deviceMode").toInt());
71}
72
73QString SsuCoreConfig::domain(bool pretty)
74{
75 if (contains("domain")) {
76 if (pretty)
77 return value("domain").toString().replace(":", "-");
78 else
79 return value("domain").toString();
80 } else {
81 return QString();
82 }
83}
84
86 return value("brand").toString();
87}
88
90{
91 if (!contains("privateKey"))
92 return false;
93 if (!contains("certificate"))
94 return false;
95 return value("registered").toBool();
96}
97
99{
100 return value("lastCredentialsUpdate").toDateTime();
101}
102
103QString SsuCoreConfig::release(bool rnd)
104{
105 Q_UNUSED(rnd)
106 return value("release").toString();
107}
108
109void SsuCoreConfig::setDeviceMode(Ssu::DeviceModeFlags mode, enum Ssu::EditMode editMode)
110{
111 int oldMode = value("deviceMode").toInt();
112
113 if ((editMode & Ssu::Add) == Ssu::Add) {
114 oldMode |= mode;
115 } else if ((editMode & Ssu::Remove) == Ssu::Remove) {
116 oldMode &= ~mode;
117 } else {
118 oldMode = mode;
119 }
120
121 setValue("deviceMode", oldMode);
122 sync();
123}
124
126{
127 setValue("flavour", flavour);
128 // flavour is RnD only, so enable RnD mode
130 sync();
131}
132
133void SsuCoreConfig::setRelease(const QString &release, bool rnd)
134{
135 // switch rndMode on/off when setting releases
137 setValue("release", release);
138 sync();
139}
140
142{
143 // - in domain messes with default section autodetection,
144 // so change it to :
145 setValue("domain", QString(domain).replace("-", ":"));
146 sync();
147}
148
150{
151 if (contains("ssl-verify"))
152 return value("ssl-verify").toBool();
153 else
154 return true;
155}
156
158{
159 int uid_min = getdef_num("UID_MIN", -1);
160
161 // For calls from valid UID we assume that they are properly logged in users.
162 // If they are not the call will fail, but it's their fault.
163 if (getuid() >= static_cast<uid_t>(uid_min)) {
164 return QDBusConnection::sessionBus();
165 } else {
166 // DBus security policy will prevent this beeing used by callers other
167 // than root at the moment. Still do it generic in case DBus policy will
168 // be extended later, and just use the usual 'DBus: THOU SHALL NOT PASS!'
169 // @TODO the uid to be used should be determined using the logind API from
170 // systemd package to support multiuser systems in the future
171 QString sessionBusAddress = QString("unix:path=/run/user/%1/dbus/user_bus_socket")
172 .arg(uid_min);
173 return QDBusConnection::connectToBus(sessionBusAddress, "userSessionBus");
174 }
175}
Q_INVOKABLE QString domain(bool pretty=false)
QString credentialsScope(const QString &repoName, bool rndRepo=false)
Q_INVOKABLE void setFlavour(const QString &flavour)
Q_INVOKABLE bool useSslVerify()
Q_INVOKABLE QDateTime lastCredentialsUpdate()
Q_INVOKABLE Ssu::DeviceModeFlags deviceMode()
Q_INVOKABLE void setDeviceMode(Ssu::DeviceModeFlags mode, enum Ssu::EditMode editMode=Ssu::Replace)
Q_INVOKABLE bool isRegistered()
Q_INVOKABLE void setDomain(const QString &domain)
QString credentialsUrl(const QString &scope)
static QDBusConnection userSessionBus()
Q_INVOKABLE QString release(bool rnd=false)
Q_INVOKABLE QString brand()
Q_INVOKABLE void setRelease(const QString &release, bool rnd=false)
Q_INVOKABLE QString flavour()
QPair< QString, QString > credentials(const QString &scope)
@ ReleaseMode
Enable Release mode.
Definition ssu.h:70
@ RndMode
Enable RnD mode for device.
Definition ssu.h:69
EditMode
Definition ssu.h:91
@ Add
Make sure the given value is set in the bitmask.
Definition ssu.h:93
@ Remove
Make sure the given value is not set in the bitmask.
Definition ssu.h:94