forked from ivanenko/cloud_storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_utils.cpp
351 lines (292 loc) · 10.9 KB
/
plugin_utils.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
/*
Wfx plugin for working with cloud storage services
Copyright (C) 2019 Ivanenko Danil ([email protected])
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string>
#include <ctime>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
#include <cstring>
#include <codecvt>
#include <locale>
#include <iomanip>
#include "library.h"
#include "plugin_utils.h"
#include "service_clients/service_client.h"
FILETIME get_now_time()
{
time_t t2 = time(0);
int64_t ft = (int64_t) t2 * 10000000 + 116444736000000000;
FILETIME file_time;
file_time.dwLowDateTime = ft & 0xffff;
file_time.dwHighDateTime = ft >> 32;
return file_time;
}
FILETIME parse_iso_time(std::string time){
struct tm t, tz;
strptime(time.c_str(), "%Y-%m-%dT%H:%M:%S%z", &t);
//long int gmtoff = t.tm_gmtoff;
time_t t2 = mktime(&t);// + gmtoff; //TODO gmtoff doesnt correct here
int64_t ft = (int64_t)t2 * 10000000 + 116444736000000000;
FILETIME file_time;
file_time.dwLowDateTime = ft & 0xffff;
file_time.dwHighDateTime = ft >> 32;
return file_time;
}
std::string UTF16toUTF8(const WCHAR *p)
{
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> convert2;
return convert2.to_bytes(std::u16string((char16_t*) p));
}
wcharstring UTF8toUTF16(const std::string &str)
{
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> convert2;
std::u16string utf16 = convert2.from_bytes(str);
return wcharstring((WCHAR*)utf16.data());
}
BOOL file_exists(const std::string& filename)
{
struct stat buf;
return stat(filename.c_str(), &buf) == 0;
}
pResources prepare_connections(const nlohmann::json &connections)
{
pResources pRes = new tResources;
pRes->nCount = 0;
if(connections.is_array() && connections.size()>0){
pRes->resource_array.resize(connections.size());
int i=0;
for(auto obj: connections){
wcharstring wName = UTF8toUTF16(obj["name"].get<std::string>());
size_t str_size = (MAX_PATH > wName.size()+1)? (wName.size()+1): MAX_PATH;
memcpy(pRes->resource_array[i].cFileName, wName.data(), sizeof(WCHAR) * str_size);
pRes->resource_array[i].dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
pRes->resource_array[i].nFileSizeLow = 0;
pRes->resource_array[i].nFileSizeHigh = 0;
pRes->resource_array[i].ftCreationTime = get_now_time();
pRes->resource_array[i].ftLastWriteTime = get_now_time();
pRes->resource_array[i].ftLastAccessTime = get_now_time();
i++;
}
}
return pRes;
}
BOOL isConnectionExists(json connections, std::string connection_name)
{
for(auto obj: connections){
if(obj["name"].get<std::string>() == connection_name){
return true;
}
}
return false;
}
void splitPath(wcharstring wPath, std::string& strConnection, std::string& strServicePath)
{
size_t nPos = wPath.find((WCHAR*)u"/", 1);
wcharstring wConnection, wServicePath;
if(nPos == std::string::npos){
wConnection = wPath.substr(1);
wServicePath = (WCHAR*)u"/";
} else {
wConnection = wPath.substr(1, nPos-1);
wServicePath = wPath.substr(nPos);
}
strConnection = UTF16toUTF8(wConnection.data());
strServicePath = UTF16toUTF8(wServicePath.data());
}
json& get_connection_config(json& globalConf, std::string& strConnection)
{
// get connection config
for(nlohmann::json& o: globalConf["connections"]){
if(o["name"].get<std::string>() == strConnection){
return o;
//return o.get_ref<json&>();
}
}
nlohmann::json j_null(nlohmann::json::value_t::null);
return j_null;
}
json::object_t* get_connection_ptr(json& globalConf, std::string& strConnection)
{
for(json& o: globalConf["connections"]){
if(o["name"].get<std::string>() == strConnection){
return o.get_ptr<json::object_t*>();
}
}
throw std::runtime_error("Connection not found in config");
}
json::iterator get_connection_iter(json& globalConf, std::string& strConnection)
{
for(json::iterator it = globalConf.begin(); it!=globalConf.end(); ++it){
if((*it)["name"].get<std::string>() == strConnection){
return it;
}
}
throw std::runtime_error("Connection not found in config");
}
std::string get_oauth_token(json& jsConfig, ServiceClient* client, int pluginNumber, tRequestProcW requestProc, int cryptoNumber,
tCryptProcW cryptProc)
{
std::string oauth_token;
if(jsConfig["get_token_method"].get<std::string>() == "oauth"){
if(jsConfig["save_type"].get<std::string>() == "dont_save"){
oauth_token = client->get_oauth_token();
return oauth_token;
}
if(jsConfig["save_type"].get<std::string>() == "config"){
if(jsConfig["oauth_token"].is_string() && !jsConfig["oauth_token"].get<std::string>().empty()) {
oauth_token = jsConfig["oauth_token"].get<std::string>();
} else {
oauth_token = client->get_oauth_token();
jsConfig["oauth_token"] = oauth_token;
}
return oauth_token;
}
if(jsConfig["save_type"].get<std::string>() == "password_manager"){
WCHAR psw[MAX_PATH];
wcharstring wName = UTF8toUTF16(jsConfig["name"].get<std::string>());
int res = cryptProc(pluginNumber, cryptoNumber, FS_CRYPT_LOAD_PASSWORD, (WCHAR*)wName.data(), psw, MAX_PATH);
if(res != FS_FILE_OK){
oauth_token = client->get_oauth_token();
wcharstring psw2 = UTF8toUTF16(oauth_token);
cryptProc(pluginNumber, cryptoNumber, FS_CRYPT_SAVE_PASSWORD, (WCHAR*)wName.data(), (WCHAR*)psw2.c_str(), psw2.size()+1);
} else {
oauth_token = UTF16toUTF8(psw);
}
return oauth_token;
}
}
if(jsConfig["get_token_method"].get<std::string>() == "manual"){
if(jsConfig["save_type"].get<std::string>() == "dont_save"){
WCHAR psw[MAX_PATH];
psw[0] = 0;
BOOL res = requestProc(pluginNumber, RT_Other, (WCHAR*)u"Enter oauth token", (WCHAR*)u"Enter oauth token", psw, MAX_PATH);
if(res > 0)
oauth_token = UTF16toUTF8(psw);
return oauth_token;
}
if(jsConfig["save_type"].get<std::string>() == "config"){
if(jsConfig["oauth_token"].is_string() && !jsConfig["oauth_token"].get<std::string>().empty()) {
oauth_token = jsConfig["oauth_token"].get<std::string>();
} else {
WCHAR psw[MAX_PATH];
psw[0] = 0;
BOOL res = requestProc(pluginNumber, RT_Other, (WCHAR*)u"Enter oauth token", (WCHAR*)u"Enter oauth token", psw, MAX_PATH);
if(res != 0){
oauth_token = UTF16toUTF8(psw);
jsConfig["oauth_token"] = oauth_token;
}
}
return oauth_token;
}
if(jsConfig["save_type"].get<std::string>() == "password_manager"){
wcharstring wName = UTF8toUTF16(jsConfig["name"].get<std::string>());
WCHAR psw[MAX_PATH];
int res = cryptProc(pluginNumber, cryptoNumber, FS_CRYPT_LOAD_PASSWORD, (WCHAR*)wName.data(), psw, MAX_PATH);
if(res != FS_FILE_OK){
WCHAR psw2[MAX_PATH];
psw2[0] = 0;
BOOL res2 = requestProc(pluginNumber, RT_Other, (WCHAR*)u"Enter oauth token", (WCHAR*)u"Enter oauth token", psw2, MAX_PATH);
if(res2 != 0){
oauth_token = UTF16toUTF8(psw2);
cryptProc(pluginNumber, cryptoNumber, FS_CRYPT_SAVE_PASSWORD, (WCHAR*)wName.data(), psw2, MAX_PATH);
}
} else {
oauth_token = UTF16toUTF8(psw);
}
return oauth_token;
}
}
return oauth_token;
}
void removeOldToken(json &jsConfig, std::string &strConnectionName, int pluginNumber, int cryptoNumber, tCryptProcW cryptProc)
{
json::object_t* obj = get_connection_ptr(jsConfig, strConnectionName);
std::string save_type = obj->at("save_type").get<std::string>();
if(save_type == "config")
obj->at("oauth_token") = "";
if(save_type == "password_manager"){
wcharstring wName = UTF8toUTF16(strConnectionName);
cryptProc(pluginNumber, cryptoNumber, FS_CRYPT_DELETE_PASSWORD, (WCHAR*)wName.data(), NULL, MAX_PATH);
}
}
void save_config(const std::string &path, const json &jsonConfig)
{
std::ofstream o(path);
o << std::setw(4) << jsonConfig << std::endl;
}
size_t strlen16(register const WCHAR* string) {
if (!string) return 0;
register size_t len = 0;
while(string[len++]);
return len;
}
std::string narrow(const std::wstring & wstr)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
return convert.to_bytes(wstr);
}
std::wstring widen(const std::string & str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
return convert.from_bytes(str);
}
std::wstring widen(const WCHAR* p)
{
#ifdef _WIN32
//sizeof(wchar_t) == sizeof(WCHAR) == 2 byte
return std::wstring((wchar_t*)p);
#else
//sizeof(wchar_t) > sizeof(WCHAR) in Linux
int nCount = 0, i=0;
WCHAR* tmp = (WCHAR*) p;
while(*tmp++) nCount++;
std::wstring result(nCount+1, '\0');
while(*p){
result[i++] = (wchar_t) *(p++);
}
result[i] = '\0';
return result;
#endif //_WIN32
}
// Check if we are in the plugin root
// if we have more than one '/' (ex. /connection1/folder1) - we are not in the root
BOOL isConnectionName(WCHAR *Path)
{
int nCount = 0;
WCHAR *p = Path;
while(*p != 0){
if(*p == u'/')
nCount++;
if(nCount>1)
return false;
p++;
}
return true;
}
std::vector<wcharstring> split(const wcharstring s, WCHAR separator)
{
std::vector<std::basic_string<WCHAR> > result;
int prev_pos = 0, pos = 0;
while(pos <= s.size()){
if(s[pos] == separator){
result.push_back(s.substr(prev_pos, pos-prev_pos));
prev_pos = ++pos;
}
pos++;
}
result.push_back(s.substr(prev_pos, pos-prev_pos));
return result;
}