ssu
ssucoreconfig.cpp
Go to the documentation of this file.
1 
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 
18 SsuCoreConfig *SsuCoreConfig::ssuCoreConfig = 0;
19 
20 SsuCoreConfig *SsuCoreConfig::instance()
21 {
22  if (!ssuCoreConfig)
23  ssuCoreConfig = new SsuCoreConfig;
24 
25  return ssuCoreConfig;
26 }
27 
28 QPair<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 
38 QString 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 
49 QString 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 
65 Ssu::DeviceModeFlags SsuCoreConfig::deviceMode()
66 {
67  if (!contains("deviceMode"))
68  return Ssu::ReleaseMode;
69  else
70  return Ssu::DeviceModeFlags(value("deviceMode").toInt());
71 }
72 
73 QString 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 
103 QString SsuCoreConfig::release(bool rnd)
104 {
105  if (rnd)
106  return value("rndRelease").toString();
107  else
108  return value("release").toString();
109 }
110 
111 void SsuCoreConfig::setDeviceMode(Ssu::DeviceModeFlags mode, enum Ssu::EditMode editMode)
112 {
113  int oldMode = value("deviceMode").toInt();
114 
115  if ((editMode & Ssu::Add) == Ssu::Add) {
116  oldMode |= mode;
117  } else if ((editMode & Ssu::Remove) == Ssu::Remove) {
118  oldMode &= ~mode;
119  } else {
120  oldMode = mode;
121  }
122 
123  setValue("deviceMode", oldMode);
124  sync();
125 }
126 
127 void SsuCoreConfig::setFlavour(const QString &flavour)
128 {
129  setValue("flavour", flavour);
130  // flavour is RnD only, so enable RnD mode
132  sync();
133 }
134 
135 void SsuCoreConfig::setRelease(const QString &release, bool rnd)
136 {
137  if (rnd) {
138  setValue("rndRelease", release);
139  // switch rndMode on/off when setting releases
141  } else {
142  setValue("release", release);
144  }
145  sync();
146 }
147 
148 void SsuCoreConfig::setDomain(const QString &domain)
149 {
150  // - in domain messes with default section autodetection,
151  // so change it to :
152  setValue("domain", QString(domain).replace("-", ":"));
153  sync();
154 }
155 
157 {
158  if (contains("ssl-verify"))
159  return value("ssl-verify").toBool();
160  else
161  return true;
162 }
163 
165 {
166  int uid_min = getdef_num("UID_MIN", -1);
167 
168  // For calls from valid UID we assume that they are properly logged in users.
169  // If they are not the call will fail, but it's their fault.
170  if (getuid() >= static_cast<uid_t>(uid_min)) {
171  return QDBusConnection::sessionBus();
172  } else {
173  // DBus security policy will prevent this beeing used by callers other
174  // than root at the moment. Still do it generic in case DBus policy will
175  // be extended later, and just use the usual 'DBus: THOU SHALL NOT PASS!'
176  // @TODO the uid to be used should be determined using the logind API from
177  // systemd package to support multiuser systems in the future
178  QString sessionBusAddress = QString("unix:path=/run/user/%1/dbus/user_bus_socket")
179  .arg(uid_min);
180  return QDBusConnection::connectToBus(sessionBusAddress, "userSessionBus");
181  }
182 }
SsuCoreConfig::setDeviceMode
Q_INVOKABLE void setDeviceMode(Ssu::DeviceModeFlags mode, enum Ssu::EditMode editMode=Ssu::Replace)
Definition: ssucoreconfig.cpp:111
SsuCoreConfig::userSessionBus
static QDBusConnection userSessionBus()
Definition: ssucoreconfig.cpp:164
SsuCoreConfig::credentials
QPair< QString, QString > credentials(const QString &scope)
Definition: ssucoreconfig.cpp:28
Ssu::ReleaseMode
@ ReleaseMode
Enable Release mode.
Definition: ssu.h:69
Ssu::Add
@ Add
Make sure the given value is set in the bitmask.
Definition: ssu.h:92
SsuCoreConfig::domain
Q_INVOKABLE QString domain(bool pretty=false)
Definition: ssucoreconfig.cpp:73
SsuCoreConfig::setRelease
Q_INVOKABLE void setRelease(const QString &release, bool rnd=false)
Definition: ssucoreconfig.cpp:135
SsuCoreConfig::setDomain
Q_INVOKABLE void setDomain(const QString &domain)
Definition: ssucoreconfig.cpp:148
Ssu::Remove
@ Remove
Make sure the given value is not set in the bitmask.
Definition: ssu.h:93
Ssu::RndMode
@ RndMode
Enable RnD mode for device.
Definition: ssu.h:68
SsuCoreConfig::useSslVerify
Q_INVOKABLE bool useSslVerify()
Definition: ssucoreconfig.cpp:156
Ssu::EditMode
EditMode
Definition: ssu.h:90
ssucoreconfig_p.h
SsuCoreConfig::isRegistered
Q_INVOKABLE bool isRegistered()
Definition: ssucoreconfig.cpp:89
SsuCoreConfig
Definition: ssucoreconfig_p.h:27
SsuCoreConfig::brand
Q_INVOKABLE QString brand()
Definition: ssucoreconfig.cpp:85
SsuCoreConfig::setFlavour
Q_INVOKABLE void setFlavour(const QString &flavour)
Definition: ssucoreconfig.cpp:127
SsuCoreConfig::flavour
Q_INVOKABLE QString flavour()
Definition: ssucoreconfig.cpp:57
SsuCoreConfig::credentialsScope
QString credentialsScope(const QString &repoName, bool rndRepo=false)
Definition: ssucoreconfig.cpp:38
SsuCoreConfig::deviceMode
Q_INVOKABLE Ssu::DeviceModeFlags deviceMode()
Definition: ssucoreconfig.cpp:65
SsuCoreConfig::release
Q_INVOKABLE QString release(bool rnd=false)
Definition: ssucoreconfig.cpp:103
SsuCoreConfig::credentialsUrl
QString credentialsUrl(const QString &scope)
Definition: ssucoreconfig.cpp:49
SsuCoreConfig::lastCredentialsUpdate
Q_INVOKABLE QDateTime lastCredentialsUpdate()
Definition: ssucoreconfig.cpp:98