ssucoreconfig.cpp 4.46 KB
Newer Older
1 2 3 4 5 6 7 8 9
/**
 * @file ssucoreconfig.cpp
 * @copyright 2013 Jolla Ltd.
 * @author Bernd Wachter <bwachter@lart.info>
 * @date 2013
 */

#include <QFile>
#include <QTextStream>
10 11 12
#include <QDBusConnection>

#include <getdef.h>
13 14
#include <sys/types.h>
#include <unistd.h>
15

16
#include "ssucoreconfig_p.h"
17 18 19

SsuCoreConfig *SsuCoreConfig::ssuCoreConfig = 0;

20 21 22 23
SsuCoreConfig *SsuCoreConfig::instance()
{
    if (!ssuCoreConfig)
        ssuCoreConfig = new SsuCoreConfig();
24

25
    return ssuCoreConfig;
26 27
}

28 29 30 31 32 33 34 35
QPair<QString, QString> SsuCoreConfig::credentials(QString scope)
{
    QPair<QString, QString> ret;
    beginGroup("credentials-" + scope);
    ret.first = value("username").toString();
    ret.second = value("password").toString();
    endGroup();
    return ret;
36 37
}

38 39 40 41 42 43
QString SsuCoreConfig::credentialsScope(QString repoName, bool rndRepo)
{
    if (contains("credentials-scope"))
        return value("credentials-scope").toString();
    else
        return "your-configuration-is-broken-and-does-not-contain-credentials-scope";
44 45
}

46 47 48 49 50 51
QString SsuCoreConfig::credentialsUrl(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;
52 53
}

54 55 56 57 58 59
QString SsuCoreConfig::flavour()
{
    if (contains("flavour"))
        return value("flavour").toString();
    else
        return "release";
60 61
}

62 63 64 65
Ssu::DeviceModeFlags SsuCoreConfig::deviceMode()
{
    if (!contains("deviceMode"))
        return Ssu::ReleaseMode;
66
    else
67
        return Ssu::DeviceModeFlags(value("deviceMode").toInt());
68 69
}

70 71 72 73 74 75 76 77 78
QString SsuCoreConfig::domain(bool pretty)
{
    if (contains("domain")) {
        if (pretty)
            return value("domain").toString().replace(":", "-");
        else
            return value("domain").toString();
    } else
        return "";
79 80
}

81 82 83 84 85 86 87
bool SsuCoreConfig::isRegistered()
{
    if (!contains("privateKey"))
        return false;
    if (!contains("certificate"))
        return false;
    return value("registered").toBool();
88 89
}

90 91 92
QDateTime SsuCoreConfig::lastCredentialsUpdate()
{
    return value("lastCredentialsUpdate").toDateTime();
93 94
}

95 96 97 98 99 100 101
QString SsuCoreConfig::release(bool rnd)
{
    if (rnd)
        return value("rndRelease").toString();
    else
        return value("release").toString();
}
102

103 104 105
void SsuCoreConfig::setDeviceMode(Ssu::DeviceModeFlags mode, enum Ssu::EditMode editMode)
{
    int oldMode = value("deviceMode").toInt();
106

107 108 109 110 111 112
    if ((editMode & Ssu::Add) == Ssu::Add) {
        oldMode |= mode;
    } else if ((editMode & Ssu::Remove) == Ssu::Remove) {
        oldMode &= ~mode;
    } else
        oldMode = mode;
113

114 115
    setValue("deviceMode", oldMode);
    sync();
116 117
}

118 119 120 121
void SsuCoreConfig::setFlavour(QString flavour)
{
    setValue("flavour", flavour);
    // flavour is RnD only, so enable RnD mode
122
    setDeviceMode(Ssu::RndMode, Ssu::Add);
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
    sync();
}

void SsuCoreConfig::setRelease(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(QString domain)
{
    // - in domain messes with default section autodetection,
    // so change it to :
    setValue("domain", 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() >= 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");
    }
173
}