-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcredstorage.cpp
305 lines (261 loc) · 10.8 KB
/
credstorage.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* This file is part of gsshvnc.
*
* gsshvnc is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* gsshvnc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gsshvnc. If not, see <http://www.gnu.org/licenses/>.
*/
#include "credstorage.h"
#include <iostream>
#if defined(USE_LIBSECRET)
#include <libsecret/secret.h>
static void _password_stored(GObject *, GAsyncResult *result, gpointer)
{
GError *error = nullptr;
secret_password_store_finish(result, &error);
if (error) {
// Don't bother the UI
std::cerr << "Error saving password: " << error->message << std::endl;
g_error_free(error);
}
}
static void _password_cleared(GObject *, GAsyncResult *result, gpointer)
{
GError *error = nullptr;
secret_password_clear_finish(result, &error);
if (error) {
// Don't bother the UI
std::cerr << "Error clearing password: " << error->message << std::endl;
g_error_free(error);
}
}
CredentialStorage::CredentialStorage()
: m_cancel(Gio::Cancellable::create())
{
}
CredentialStorage::~CredentialStorage()
{
m_cancel->cancel();
}
static const SecretSchema *_ssh_password_schema()
{
static SecretSchema schema = {};
if (!schema.name) {
schema.name = "net.zrax.gsshvnc.SshPassword";
schema.flags = SECRET_SCHEMA_NONE;
schema.attributes[0] = { "app_type", SECRET_SCHEMA_ATTRIBUTE_STRING };
schema.attributes[1] = { "user_host", SECRET_SCHEMA_ATTRIBUTE_STRING };
}
return &schema;
}
void CredentialStorage::remember_ssh_password(const Glib::ustring &ssh_user_host,
const Glib::ustring &password)
{
secret_password_store(_ssh_password_schema(), SECRET_COLLECTION_DEFAULT,
"gsshvnc SSH password", password.c_str(), nullptr,
&_password_stored, nullptr,
"app_type", "gsshvnc-ssh",
"user_host", ssh_user_host.c_str(),
nullptr);
}
void CredentialStorage::forget_ssh_password(const Glib::ustring &ssh_user_host)
{
secret_password_clear(_ssh_password_schema(), nullptr,
&_password_cleared, nullptr,
"app_type", "gsshvnc-ssh",
"user_host", ssh_user_host.c_str(),
nullptr);
}
static void _ssh_password_found(GObject *, GAsyncResult *result, gpointer user)
{
CredentialStorage *self = reinterpret_cast<CredentialStorage *>(user);
GError *error = nullptr;
gchar *password = secret_password_lookup_finish(result, &error);
if (error) {
// Don't bother the UI
std::cerr << "Error retrieving saved password: " << error->message << std::endl;
g_error_free(error);
} else if (password) {
Glib::ustring result(password);
secret_password_free(password);
self->got_ssh_password().emit(result);
}
}
void CredentialStorage::fetch_ssh_password(const Glib::ustring &ssh_user_host)
{
m_cancel->reset();
secret_password_lookup(_ssh_password_schema(), m_cancel->gobj(),
&_ssh_password_found, this,
"app_type", "gsshvnc-ssh",
"user_host", ssh_user_host.c_str(),
nullptr);
}
static const SecretSchema *_vnc_password_schema()
{
static SecretSchema schema = {};
if (!schema.name) {
schema.name = "net.zrax.gsshvnc.VncPassword";
schema.flags = SECRET_SCHEMA_NONE;
schema.attributes[0] = { "app_type", SECRET_SCHEMA_ATTRIBUTE_STRING };
schema.attributes[1] = { "ssh_host", SECRET_SCHEMA_ATTRIBUTE_STRING };
schema.attributes[2] = { "vnc_host", SECRET_SCHEMA_ATTRIBUTE_STRING };
}
return &schema;
}
void CredentialStorage::remember_vnc_password(const Glib::ustring &ssh_host,
const Glib::ustring &vnc_host,
const Glib::ustring &vnc_user,
const Glib::ustring &password)
{
auto storage = Glib::ustring::compose("%1@%2", vnc_user, password);
secret_password_store(_vnc_password_schema(), SECRET_COLLECTION_DEFAULT,
"gsshvnc VNC password", storage.c_str(), nullptr,
&_password_stored, nullptr,
"app_type", "gsshvnc-vnc",
"ssh_host", ssh_host.c_str(),
"vnc_host", vnc_host.c_str(),
nullptr);
}
void CredentialStorage::forget_vnc_password(const Glib::ustring &ssh_host,
const Glib::ustring &vnc_host)
{
secret_password_clear(_vnc_password_schema(), nullptr,
&_password_cleared, nullptr,
"app_type", "gsshvnc-vnc",
"ssh_host", ssh_host.c_str(),
"vnc_host", vnc_host.c_str(),
nullptr);
}
static void _vnc_password_found(GObject *, GAsyncResult *result, gpointer user)
{
CredentialStorage *self = reinterpret_cast<CredentialStorage *>(user);
GError *error = nullptr;
gchar *storage = secret_password_lookup_finish(result, &error);
if (error) {
// Don't bother the UI
std::cerr << "Error retrieving saved credentials: " << error->message << std::endl;
g_error_free(error);
} else if (storage) {
Glib::ustring username(storage);
Glib::ustring password;
secret_password_free(storage);
auto upos = username.find('@');
if (upos == std::string::npos) {
std::cerr << "Ignoring invalid credential storage format" << std::endl;
} else {
password = username.substr(upos + 1);
username.resize(upos);
self->got_vnc_password().emit(username, password);
}
}
}
void CredentialStorage::fetch_vnc_user_password(const Glib::ustring &ssh_host,
const Glib::ustring &vnc_host)
{
m_cancel->reset();
secret_password_lookup(_vnc_password_schema(), m_cancel->gobj(),
&_vnc_password_found, this,
"app_type", "gsshvnc-vnc",
"ssh_host", ssh_host.c_str(),
"vnc_host", vnc_host.c_str(),
nullptr);
}
#elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <wincred.h>
#include <locale>
#include <codecvt>
CredentialStorage::CredentialStorage() { }
CredentialStorage::~CredentialStorage() { }
static inline std::wstring _to_wstring(const Glib::ustring &ustr)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
return conv.from_bytes(ustr);
}
static inline Glib::ustring _to_ustring(const std::wstring &wstr)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
return conv.to_bytes(wstr);
}
void CredentialStorage::remember_ssh_password(const Glib::ustring &ssh_user_host,
const Glib::ustring &password)
{
auto target = _to_wstring(Glib::ustring::compose("gsshvnc-ssh/%1", ssh_user_host));
CREDENTIALW credential = {};
credential.Type = CRED_TYPE_GENERIC;
credential.TargetName = const_cast<LPWSTR>(target.c_str());
credential.CredentialBlobSize = password.size();
credential.CredentialBlob = reinterpret_cast<LPBYTE>(const_cast<char *>(password.c_str()));
credential.Persist = CRED_PERSIST_LOCAL_MACHINE;
CredWriteW(&credential, 0);
}
void CredentialStorage::forget_ssh_password(const Glib::ustring &ssh_user_host)
{
auto target = _to_wstring(Glib::ustring::compose("gsshvnc-ssh/%1", ssh_user_host));
CredDeleteW(target.c_str(), CRED_TYPE_GENERIC, 0);
}
void CredentialStorage::fetch_ssh_password(const Glib::ustring &ssh_user_host)
{
auto target = _to_wstring(Glib::ustring::compose("gsshvnc-ssh/%1", ssh_user_host));
CREDENTIALW *credential = nullptr;
if (CredReadW(target.c_str(), CRED_TYPE_GENERIC, 0, &credential)) {
Glib::ustring password(reinterpret_cast<const char *>(credential->CredentialBlob),
credential->CredentialBlobSize);
m_got_ssh_password.emit(password);
CredFree(credential);
}
}
void CredentialStorage::remember_vnc_password(const Glib::ustring &ssh_host,
const Glib::ustring &vnc_host,
const Glib::ustring &vnc_user,
const Glib::ustring &password)
{
auto target = _to_wstring(Glib::ustring::compose("gsshvnc-vnc/%1/%2", ssh_host, vnc_host));
auto storage = Glib::ustring::compose("%1@%2", vnc_user, password);
CREDENTIALW credential = {};
credential.Type = CRED_TYPE_GENERIC;
credential.TargetName = const_cast<LPWSTR>(target.c_str());
credential.CredentialBlobSize = storage.size();
credential.CredentialBlob = reinterpret_cast<LPBYTE>(const_cast<char *>(storage.c_str()));
credential.Persist = CRED_PERSIST_LOCAL_MACHINE;
CredWriteW(&credential, 0);
}
void CredentialStorage::forget_vnc_password(const Glib::ustring &ssh_host,
const Glib::ustring &vnc_host)
{
auto target = _to_wstring(Glib::ustring::compose("gsshvnc-vnc/%1/%2", ssh_host, vnc_host));
CredDeleteW(target.c_str(), CRED_TYPE_GENERIC, 0);
}
void CredentialStorage::fetch_vnc_user_password(const Glib::ustring &ssh_host,
const Glib::ustring &vnc_host)
{
auto target = _to_wstring(Glib::ustring::compose("gsshvnc-vnc/%1/%2", ssh_host, vnc_host));
CREDENTIALW *credential = nullptr;
if (CredReadW(target.c_str(), CRED_TYPE_GENERIC, 0, &credential)) {
Glib::ustring storage(reinterpret_cast<const char *>(credential->CredentialBlob),
credential->CredentialBlobSize);
Glib::ustring username(storage);
Glib::ustring password;
auto upos = username.find('@');
if (upos == std::string::npos) {
std::cerr << "Ignoring invalid credential storage format" << std::endl;
} else {
password = username.substr(upos + 1);
username.resize(upos);
m_got_vnc_password.emit(username, password);
}
CredFree(credential);
}
}
#else
#error This platform is not yet supported
#endif