aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/devicesupport/sshsettings.cpp
blob: 9590d501fecd1a713c4e2789359d3d2e29a95273 (plain)
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
179
180
181
182
183
184
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "sshsettings.h"

#include <utils/environment.h>
#include <utils/hostosinfo.h>

#include <QReadWriteLock>
#include <QSettings>

using namespace Utils;

namespace ProjectExplorer {
namespace Internal {

struct SshSettings
{
    bool useConnectionSharing = !HostOsInfo::isWindowsHost();
    int connectionSharingTimeOutInMinutes = 10;
    FilePath sshFilePath;
    FilePath sftpFilePath;
    FilePath askpassFilePath;
    FilePath keygenFilePath;
    ProjectExplorer::SshSettings::SearchPathRetriever searchPathRetriever = [] { return FilePaths(); };
    QReadWriteLock lock;
};

} // namespace Internal

Q_GLOBAL_STATIC(Internal::SshSettings, sshSettings)

class AccessSettingsGroup
{
public:
    AccessSettingsGroup(QSettings *settings) : m_settings(settings)
    {
        settings->beginGroup("SshSettings");
    }
    ~AccessSettingsGroup() { m_settings->endGroup(); }
private:
    QSettings * const m_settings;
};

static QString connectionSharingKey() { return QString("UseConnectionSharing"); }
static QString connectionSharingTimeoutKey() { return QString("ConnectionSharingTimeout"); }
static QString sshFilePathKey() { return QString("SshFilePath"); }
static QString sftpFilePathKey() { return QString("SftpFilePath"); }
static QString askPassFilePathKey() { return QString("AskpassFilePath"); }
static QString keygenFilePathKey() { return QString("KeygenFilePath"); }

void SshSettings::loadSettings(QSettings *settings)
{
    QWriteLocker locker(&sshSettings->lock);
    AccessSettingsGroup g(settings);
    QVariant value = settings->value(connectionSharingKey());
    if (value.isValid() && !HostOsInfo::isWindowsHost())
        sshSettings->useConnectionSharing = value.toBool();
    value = settings->value(connectionSharingTimeoutKey());
    if (value.isValid())
        sshSettings->connectionSharingTimeOutInMinutes = value.toInt();
    sshSettings->sshFilePath = FilePath::fromString(settings->value(sshFilePathKey()).toString());
    sshSettings->sftpFilePath = FilePath::fromString(settings->value(sftpFilePathKey()).toString());
    sshSettings->askpassFilePath = FilePath::fromString(
                settings->value(askPassFilePathKey()).toString());
    sshSettings->keygenFilePath = FilePath::fromString(
                settings->value(keygenFilePathKey()).toString());
}

void SshSettings::storeSettings(QSettings *settings)
{
    QReadLocker locker(&sshSettings->lock);
    AccessSettingsGroup g(settings);
    settings->setValue(connectionSharingKey(), sshSettings->useConnectionSharing);
    settings->setValue(connectionSharingTimeoutKey(),
                       sshSettings->connectionSharingTimeOutInMinutes);
    settings->setValue(sshFilePathKey(), sshSettings->sshFilePath.toString());
    settings->setValue(sftpFilePathKey(), sshSettings->sftpFilePath.toString());
    settings->setValue(askPassFilePathKey(), sshSettings->askpassFilePath.toString());
    settings->setValue(keygenFilePathKey(), sshSettings->keygenFilePath.toString());
}

void SshSettings::setConnectionSharingEnabled(bool share)
{
    QWriteLocker locker(&sshSettings->lock);
    sshSettings->useConnectionSharing = share;
}
bool SshSettings::connectionSharingEnabled()
{
    QReadLocker locker(&sshSettings->lock);
    return sshSettings->useConnectionSharing;
}

void SshSettings::setConnectionSharingTimeout(int timeInMinutes)
{
    QWriteLocker locker(&sshSettings->lock);
    sshSettings->connectionSharingTimeOutInMinutes = timeInMinutes;
}
int SshSettings::connectionSharingTimeout()
{
    QReadLocker locker(&sshSettings->lock);
    return sshSettings->connectionSharingTimeOutInMinutes;
}

// Keep read locker locked while calling this method
static FilePath filePathValue(const FilePath &value, const QStringList &candidateFileNames)
{
    if (!value.isEmpty())
        return value;
    const FilePaths additionalSearchPaths = sshSettings->searchPathRetriever();
    for (const QString &candidate : candidateFileNames) {
        const FilePath filePath = Environment::systemEnvironment()
                .searchInPath(candidate, additionalSearchPaths);
        if (!filePath.isEmpty())
            return filePath;
    }
    return FilePath();
}

// Keep read locker locked while calling this method
static FilePath filePathValue(const FilePath &value, const QString &candidateFileName)
{
    return filePathValue(value, QStringList(candidateFileName));
}

void SshSettings::setSshFilePath(const FilePath &ssh)
{
    QWriteLocker locker(&sshSettings->lock);
    sshSettings->sshFilePath = ssh;
}

FilePath SshSettings::sshFilePath()
{
    QReadLocker locker(&sshSettings->lock);
    return filePathValue(sshSettings->sshFilePath, "ssh");
}

void SshSettings::setSftpFilePath(const FilePath &sftp)
{
    QWriteLocker locker(&sshSettings->lock);
    sshSettings->sftpFilePath = sftp;
}

FilePath SshSettings::sftpFilePath()
{
    QReadLocker locker(&sshSettings->lock);
    return filePathValue(sshSettings->sftpFilePath, "sftp");
}

void SshSettings::setAskpassFilePath(const FilePath &askPass)
{
    QWriteLocker locker(&sshSettings->lock);
    sshSettings->askpassFilePath = askPass;
}

FilePath SshSettings::askpassFilePath()
{
    QReadLocker locker(&sshSettings->lock);
    FilePath candidate;
    candidate = sshSettings->askpassFilePath;
    if (candidate.isEmpty())
        candidate = FilePath::fromString(Environment::systemEnvironment().value("SSH_ASKPASS"));
    return filePathValue(candidate, QStringList{"qtc-askpass", "ssh-askpass"});
}

void SshSettings::setKeygenFilePath(const FilePath &keygen)
{
    QWriteLocker locker(&sshSettings->lock);
    sshSettings->keygenFilePath = keygen;
}

FilePath SshSettings::keygenFilePath()
{
    QReadLocker locker(&sshSettings->lock);
    return filePathValue(sshSettings->keygenFilePath, "ssh-keygen");
}

void SshSettings::setExtraSearchPathRetriever(const SearchPathRetriever &pathRetriever)
{
    QWriteLocker locker(&sshSettings->lock);
    sshSettings->searchPathRetriever = pathRetriever;
}

} // namespace ProjectExplorer