/
SFTPSession.cpp
535 lines (464 loc) · 15 KB
/
SFTPSession.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program 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, or (at your option)
* any later version.
*
* This Program 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 XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "SFTPSession.h"
#include <p8-platform/util/timeutils.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sstream>
#include <kodi/General.h>
#define SFTP_TIMEOUT 10
#if !defined(S_ISREG)
#define S_ISREG(m) ((m & _S_IFREG) != 0)
#endif
static std::string CorrectPath(const std::string& path)
{
if (path == "~")
return "./";
else if (path.substr(0, 2) == "~/")
return "./" + path.substr(2);
else
return "/" + path;
}
static const char * SFTPErrorText(int sftp_error)
{
switch(sftp_error)
{
case SSH_FX_OK:
return "No error";
case SSH_FX_EOF:
return "End-of-file encountered";
case SSH_FX_NO_SUCH_FILE:
return "File doesn't exist";
case SSH_FX_PERMISSION_DENIED:
return "Permission denied";
case SSH_FX_BAD_MESSAGE:
return "Garbage received from server";
case SSH_FX_NO_CONNECTION:
return "No connection has been set up";
case SSH_FX_CONNECTION_LOST:
return "There was a connection, but we lost it";
case SSH_FX_OP_UNSUPPORTED:
return "Operation not supported by the server";
case SSH_FX_INVALID_HANDLE:
return "Invalid file handle";
case SSH_FX_NO_SUCH_PATH:
return "No such file or directory path exists";
case SSH_FX_FILE_ALREADY_EXISTS:
return "An attempt to create an already existing file or directory has been made";
case SSH_FX_WRITE_PROTECT:
return "We are trying to write on a write-protected filesystem";
case SSH_FX_NO_MEDIA:
return "No media in remote drive";
case -1:
return "Not a valid error code, probably called on an invalid session";
default:
kodi::Log(ADDON_LOG_ERROR, "SFTPErrorText: Unknown error code: %d", sftp_error);
}
return "Unknown error code";
}
CSFTPSession::CSFTPSession(const VFSURL& url)
{
kodi::Log(ADDON_LOG_INFO, "SFTPSession: Creating new session on host '%s:%d' with user '%s'", url.hostname, url.port, url.username);
P8PLATFORM::CLockObject lock(m_lock);
if (!Connect(url))
Disconnect();
m_LastActive = P8PLATFORM::GetTimeMs();
}
CSFTPSession::~CSFTPSession()
{
P8PLATFORM::CLockObject lock(m_lock);
Disconnect();
}
sftp_file CSFTPSession::CreateFileHande(const std::string& file)
{
if (m_connected)
{
P8PLATFORM::CLockObject lock(m_lock);
m_LastActive = P8PLATFORM::GetTimeMs();
sftp_file handle = sftp_open(m_sftp_session, CorrectPath(file).c_str(), O_RDONLY, 0);
if (handle)
{
sftp_file_set_blocking(handle);
return handle;
}
else
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Was connected but couldn't create filehandle for '%s'", file.c_str());
}
else
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Not connected and can't create file handle for '%s'", file.c_str());
return NULL;
}
void CSFTPSession::CloseFileHandle(sftp_file handle)
{
P8PLATFORM::CLockObject lock(m_lock);
sftp_close(handle);
}
bool CSFTPSession::GetDirectory(const std::string& base, const std::string& folder,
std::vector<kodi::vfs::CDirEntry>& items)
{
int sftp_error = SSH_FX_OK;
if (m_connected)
{
sftp_dir dir = NULL;
P8PLATFORM::CLockObject lock(m_lock);
m_LastActive = P8PLATFORM::GetTimeMs();
dir = sftp_opendir(m_sftp_session, CorrectPath(folder).c_str());
//Doing as little work as possible within the critical section
if (!dir)
sftp_error = sftp_get_error(m_sftp_session);
lock.Unlock();
if (!dir)
{
kodi::Log(ADDON_LOG_ERROR, "%s: %s for '%s'", __FUNCTION__, SFTPErrorText(sftp_error), folder.c_str());
}
else
{
bool read = true;
while (read)
{
sftp_attributes attributes = NULL;
lock.Lock();
read = sftp_dir_eof(dir) == 0;
attributes = sftp_readdir(m_sftp_session, dir);
lock.Unlock();
if (attributes && (attributes->name == NULL || strcmp(attributes->name, "..") == 0 || strcmp(attributes->name, ".") == 0))
{
lock.Lock();
sftp_attributes_free(attributes);
lock.Unlock();
continue;
}
if (attributes)
{
std::string itemName = attributes->name;
std::string localPath = folder;
localPath.append(itemName);
if (attributes->type == SSH_FILEXFER_TYPE_SYMLINK)
{
lock.Lock();
sftp_attributes_free(attributes);
attributes = sftp_stat(m_sftp_session, CorrectPath(localPath).c_str());
lock.Unlock();
if (attributes == NULL)
continue;
}
kodi::vfs::CDirEntry entry;
entry.SetLabel(itemName);
if (itemName[0] == '.')
entry.AddProperty("file:hidden", "true");
entry.SetDateTime(attributes->mtime64);
if (attributes->type & SSH_FILEXFER_TYPE_DIRECTORY)
{
localPath.append("/");
entry.SetFolder(true);
}
else
entry.SetSize(attributes->size);
entry.SetPath(base+localPath);
items.push_back(entry);
lock.Lock();
sftp_attributes_free(attributes);
lock.Unlock();
}
else
read = false;
}
lock.Lock();
sftp_closedir(dir);
lock.Unlock();
return true;
}
}
else
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Not connected, can't list directory '%s'", folder.c_str());
return false;
}
bool CSFTPSession::DirectoryExists(const char *path)
{
bool exists = false;
uint32_t permissions = 0;
exists = GetItemPermissions(path, permissions);
return exists && S_ISDIR(permissions);
}
bool CSFTPSession::FileExists(const char *path)
{
bool exists = false;
uint32_t permissions = 0;
exists = GetItemPermissions(path, permissions);
return exists && S_ISREG(permissions);
}
int CSFTPSession::Stat(const char *path, struct __stat64* buffer)
{
if(m_connected)
{
P8PLATFORM::CLockObject lock(m_lock);
m_LastActive = P8PLATFORM::GetTimeMs();
sftp_attributes attributes = sftp_stat(m_sftp_session, CorrectPath(path).c_str());
if (attributes)
{
memset(buffer, 0, sizeof(struct __stat64));
buffer->st_size = attributes->size;
buffer->st_mtime = attributes->mtime;
buffer->st_atime = attributes->atime;
if S_ISDIR(attributes->permissions)
buffer->st_mode = S_IFDIR;
else if S_ISREG(attributes->permissions)
buffer->st_mode = S_IFREG;
sftp_attributes_free(attributes);
return 0;
}
else
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession::Stat - Failed to get attributes for '%s'", path);
return -1;
}
}
else
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession::Stat - Failed because not connected for '%s'", path);
return -1;
}
}
int CSFTPSession::Seek(sftp_file handle, uint64_t position)
{
P8PLATFORM::CLockObject lock(m_lock);
m_LastActive = P8PLATFORM::GetTimeMs();
int result = sftp_seek64(handle, position);
return result;
}
int CSFTPSession::Read(sftp_file handle, void *buffer, size_t length)
{
P8PLATFORM::CLockObject lock(m_lock);
m_LastActive = P8PLATFORM::GetTimeMs();
int result=sftp_read(handle, buffer, length);
return result;
}
int64_t CSFTPSession::GetPosition(sftp_file handle)
{
P8PLATFORM::CLockObject lock(m_lock);
m_LastActive = P8PLATFORM::GetTimeMs();
int64_t result = sftp_tell64(handle);
return result;
}
bool CSFTPSession::IsIdle()
{
return (P8PLATFORM::GetTimeMs() - m_LastActive) > 90000;
}
bool CSFTPSession::VerifyKnownHost(ssh_session session)
{
switch (ssh_is_server_known(session))
{
case SSH_SERVER_KNOWN_OK:
return true;
case SSH_SERVER_KNOWN_CHANGED:
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Server that was known has changed");
return false;
case SSH_SERVER_FOUND_OTHER:
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: The host key for this server was not found but an other type of key exists. An attacker might change the default server key to confuse your client into thinking the key does not exist");
return false;
case SSH_SERVER_FILE_NOT_FOUND:
kodi::Log(ADDON_LOG_INFO, "SFTPSession: Server file was not found, creating a new one");
case SSH_SERVER_NOT_KNOWN:
kodi::Log(ADDON_LOG_INFO, "SFTPSession: Server unkown, we trust it for now");
if (ssh_write_knownhost(session) < 0)
{
kodi::Log(ADDON_LOG_ERROR, "CSFTPSession: Failed to save host '%s'", strerror(errno));
return false;
}
return true;
case SSH_SERVER_ERROR:
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to verify host '%s'", ssh_get_error(session));
return false;
}
return false;
}
bool CSFTPSession::Connect(const VFSURL& url)
{
int timeout = SFTP_TIMEOUT;
m_connected = false;
m_session = NULL;
m_sftp_session = NULL;
m_session=ssh_new();
if (m_session == NULL)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to initialize session for host '%s'", url.hostname);
return false;
}
if (ssh_options_set(m_session, SSH_OPTIONS_USER, url.username) < 0)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to set username '%s' for session", url.username);
return false;
}
if (ssh_options_set(m_session, SSH_OPTIONS_HOST, url.hostname) < 0)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to set host '%s' for session", url.hostname);
return false;
}
if (ssh_options_set(m_session, SSH_OPTIONS_PORT, &url.port) < 0)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to set port '%d' for session", url.port);
return false;
}
#if defined(TARGET_DARWIN_IOS) || defined(TARGET_WINDOWS_STORE)
std::string sshFolder = kodi::vfs::TranslateSpecialProtocol("special://home/.ssh");
if (ssh_options_set(m_session, SSH_OPTIONS_SSH_DIR, sshFolder.c_str()) < 0)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to set .ssh folder to '%s' for session", sshFolder.c_str());
return false;
}
#endif
ssh_options_set(m_session, SSH_OPTIONS_LOG_VERBOSITY, 0);
ssh_options_set(m_session, SSH_OPTIONS_TIMEOUT, &timeout);
if(ssh_connect(m_session))
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to connect '%s'", ssh_get_error(m_session));
return false;
}
if (!VerifyKnownHost(m_session))
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Host is not known '%s'", ssh_get_error(m_session));
return false;
}
int noAuth = SSH_AUTH_DENIED;
if ((noAuth = ssh_userauth_none(m_session, NULL)) == SSH_AUTH_ERROR)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to authenticate via guest '%s'", ssh_get_error(m_session));
return false;
}
int method = ssh_userauth_list(m_session, NULL);
// Try to authenticate with public key first
int publicKeyAuth = SSH_AUTH_DENIED;
if (method & SSH_AUTH_METHOD_PUBLICKEY && (publicKeyAuth = ssh_userauth_publickey_auto(m_session, NULL, NULL)) == SSH_AUTH_ERROR)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to authenticate via publickey '%s'", ssh_get_error(m_session));
return false;
}
// Try to authenticate with password
int passwordAuth = SSH_AUTH_DENIED;
if (method & SSH_AUTH_METHOD_PASSWORD)
{
if (publicKeyAuth != SSH_AUTH_SUCCESS &&
(passwordAuth = ssh_userauth_password(m_session, url.username, url.password)) == SSH_AUTH_ERROR)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to authenticate via password '%s'", ssh_get_error(m_session));
return false;
}
}
else if (strlen(url.password) > 0)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Password present, but server does not support password authentication");
}
if (noAuth == SSH_AUTH_SUCCESS || publicKeyAuth == SSH_AUTH_SUCCESS || passwordAuth == SSH_AUTH_SUCCESS)
{
m_sftp_session = sftp_new(m_session);
if (m_sftp_session == NULL)
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to initialize channel '%s'", ssh_get_error(m_session));
return false;
}
if (sftp_init(m_sftp_session))
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Failed to initialize sftp '%s'", ssh_get_error(m_session));
return false;
}
m_connected = true;
}
else
{
kodi::Log(ADDON_LOG_ERROR, "SFTPSession: No authentication method successful");
}
return m_connected;
}
void CSFTPSession::Disconnect()
{
if (m_sftp_session)
sftp_free(m_sftp_session);
if (m_session)
{
ssh_disconnect(m_session);
ssh_free(m_session);
}
m_sftp_session = NULL;
m_session = NULL;
}
/*!
\brief Gets POSIX compatible permissions information about the specified file or directory.
\param path Remote SSH path to the file or directory.
\param permissions POSIX compatible permissions information for the file or directory (if it exists). i.e. can use macros S_ISDIR() etc.
\return Returns \e true, if it was possible to get permissions for the file or directory, \e false otherwise.
*/
bool CSFTPSession::GetItemPermissions(const char *path, uint32_t &permissions)
{
bool gotPermissions = false;
P8PLATFORM::CLockObject lock(m_lock);
if(m_connected)
{
sftp_attributes attributes = sftp_stat(m_sftp_session, CorrectPath(path).c_str());
if (attributes)
{
if (attributes->flags & SSH_FILEXFER_ATTR_PERMISSIONS)
{
permissions = attributes->permissions;
gotPermissions = true;
}
sftp_attributes_free(attributes);
}
}
return gotPermissions;
}
CSFTPSessionManager& CSFTPSessionManager::Get()
{
static CSFTPSessionManager instance;
return instance;
}
CSFTPSessionPtr CSFTPSessionManager::CreateSession(const VFSURL& url)
{
// Convert port number to string
std::stringstream itoa;
itoa << (url.port ? url.port : 22);
std::string portstr = itoa.str();
P8PLATFORM::CLockObject lock(m_lock);
std::string key = std::string(url.username) + ":" +
url.password + "@" + url.hostname + ":" + portstr;
CSFTPSessionPtr ptr = sessions[key];
if (ptr == NULL)
{
ptr = CSFTPSessionPtr(new CSFTPSession(url));
sessions[key] = ptr;
}
return ptr;
}
void CSFTPSessionManager::ClearOutIdleSessions()
{
P8PLATFORM::CLockObject lock(m_lock);
for(std::map<std::string, CSFTPSessionPtr>::iterator iter = sessions.begin(); iter != sessions.end();)
{
if (iter->second->IsIdle())
sessions.erase(iter++);
else
iter++;
}
}
void CSFTPSessionManager::DisconnectAllSessions()
{
P8PLATFORM::CLockObject lock(m_lock);
sessions.clear();
}