-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshellextthread.cpp
More file actions
368 lines (338 loc) · 14.2 KB
/
shellextthread.cpp
File metadata and controls
368 lines (338 loc) · 14.2 KB
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
#include "shellextthread.h"
#include "pcloudapp.h"
#ifdef Q_OS_WIN
#include <Shlobj.h>
#include "QMessageBox"
#define PIPE_NAME L"\\\\.\\pipe\\shellextnpipe2"
#define BUFSIZE 8192 + MAX_PATH + 10 //crypto
ShellExtThread::ShellExtThread(PCloudApp *a)
{
app = a;
PSID pEveryoneSID = NULL;
PACL pACL = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
EXPLICIT_ACCESS ea[1];
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = {SECURITY_WORLD_SID_AUTHORITY};
SECURITY_ATTRIBUTES sa;
if(!AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID))
{
goto cleanup;
}
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
ea[0].grfAccessPermissions = SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL;
ea[0].grfAccessMode = SET_ACCESS;
ea[0].grfInheritance= NO_INHERITANCE;
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID;
if (SetEntriesInAcl(1, ea, NULL, &pACL))
{
goto cleanup;
}
pSD = (PSECURITY_DESCRIPTOR) malloc(SECURITY_DESCRIPTOR_MIN_LENGTH);
if(!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
{
goto cleanup;
}
if (!SetSecurityDescriptorDacl(pSD, TRUE, pACL, FALSE))
{
goto cleanup;
}
sa.nLength = sizeof (SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = pSD;
sa.bInheritHandle = FALSE;
qDebug() << "Qt: creating pipe";
hPipe = CreateNamedPipe(PIPE_NAME, PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1, BUFSIZE,BUFSIZE, 0, &sa);
qDebug()<< "Qt: pipe created successfully";
if (hPipe != INVALID_HANDLE_VALUE)
return;
cleanup:
if (pEveryoneSID) FreeSid(pEveryoneSID);
if (pACL) free(pACL);
if (pSD) free(pSD);
}
void ShellExtThread::run()
{
qDebug()<< " Qt: RUN thread pipe started";
while(true)
{
qDebug()<< "Qt WHILE: pipe started ";
if (hPipe != INVALID_HANDLE_VALUE)
{
qDebug()<< "Qt: pipe is VALID handle, before connect";
if (!ConnectNamedPipe(hPipe, NULL) && (GetLastError() != ERROR_PIPE_CONNECTED))
{
qDebug()<< "Qt: Connect pipe err: "<< GetLastError();
break;
}
else
qDebug() << "Qt: pipe sucessfully connected";
}
else
{
qDebug()<<"qt CreateNamedPipe failed pipe is invalid handle, GLE=%d.\n" << GetLastError();
return;
}
char buffer[BUFSIZE];
DWORD numBytesRead = 0;
bool resread = ReadFile(hPipe, buffer, BUFSIZE, &numBytesRead, NULL); // in cycle while read for long msgs
qDebug() << "Qt: after read";
if (resread)
{
QString bufferStr = buffer;
qDebug()<<"Qt: read file not zero, received buffer ="<<bufferStr;
//NOT LOGGED
if (bufferStr.startsWith("login"))
app->showLoginPublic();
//SyncLIST
else if (bufferStr.startsWith("synclist"))
{
qDebug() << "Qt: read pipe: request the synclist case" << bufferStr;
//send synclist to the client
bool isCrypto = false, canShare = true, multiselect = false;
char msgCopyFldrs[BUFSIZE];
if (app->isLogedIn())
{
QString remoteFldr("");
if(bufferStr[8] != '\0') // clicked in drive
{
localFldrsFlag = false;
if(bufferStr[8] == '*') //check crypto
{
if(bufferStr[9] == '*')
{
multiselect = true;
remoteFldr = bufferStr.replace("\\","/").remove(0,12);
}
else
remoteFldr = bufferStr.replace("\\","/").remove(0,11);
if(remoteFldr.startsWith("pCloud/pCloud Drive",Qt::CaseInsensitive)) //remove network preffix
{
remoteFldr.remove(0,19);
bufferStr.remove(0,19);
}
//check is selected in crypto subfodler
pentry_t* pfldr = psync_stat_path(remoteFldr.toUtf8());
if (pfldr != NULL && pfldr->folder.isencrypted)
isCrypto = true;
else
{
int count = remoteFldr.count("/")-1;
while (count)
{
remoteFldr = remoteFldr.section("/", 0, -2);
pentry_t* pfldr = psync_stat_path(remoteFldr.toUtf8());
if (pfldr != NULL && pfldr->folder.isencrypted)
{
isCrypto = true;
break;
}
count--;
}
}
}
}
else
localFldrsFlag = true;
strcpy(msgCopyFldrs, "synclist");
if (isCrypto && (bufferStr == remoteFldr) && !multiselect) //crypto folder is selected
{
canShare = false;
strcat(msgCopyFldrs,"0"); //canshare
strcat(msgCopyFldrs,":");
if(psync_crypto_isstarted())
strcat(msgCopyFldrs,"0"); //folder is unlocked
else
strcat(msgCopyFldrs,"1");
strcat(msgCopyFldrs,"?"); //separator for crypto locked status
}
else if (isCrypto && (bufferStr != remoteFldr)) //selection in crypto folder
{
canShare = false;
strcat(msgCopyFldrs,">");
}
else // no crypto selection
{
psync_folder_list_t *fldrsList = psync_get_sync_list();
if (fldrsList != NULL && fldrsList->foldercnt)
{
if(localFldrsFlag)
{
char msgDwnldOnlyFldrs[260*fldrsList->foldercnt];
strcpy(msgDwnldOnlyFldrs,"");
for (uint i = 0; i< fldrsList->foldercnt; i++)
{
if(fldrsList->folders[i].synctype != PSYNC_DOWNLOAD_ONLY)
{
strcat(msgCopyFldrs,fldrsList->folders[i].localpath);
strcat(msgCopyFldrs,"|");
}
else
{
strcat(msgDwnldOnlyFldrs,fldrsList->folders[i].localpath);
strcat(msgDwnldOnlyFldrs,"*");
}
}
strcat(msgCopyFldrs, msgDwnldOnlyFldrs); //concatenate folders lists
qDebug() << "Qt: whole sync list for send locals :" << msgCopyFldrs;
}
else
{
for (uint i = 0; i< fldrsList->foldercnt; i++)
{
QString remotePathStr = fldrsList->folders[i].remotepath;
qDebug()<<"Qt: read pipe: add remote folder to send" <<remotePathStr.toUtf8().constData();
strcat(msgCopyFldrs, remotePathStr.toUtf8().constData());
strcat(msgCopyFldrs,"|");
}
}
}
free(fldrsList);
}
qDebug() << "Qt: whole sync list for send remote :" << msgCopyFldrs;
}
else
strcpy(msgCopyFldrs,"notlogged");
DWORD numBytesWritten = 0;
wchar_t wtext[strlen(msgCopyFldrs)];
mbstowcs(wtext, msgCopyFldrs,strlen(msgCopyFldrs)+1);//Plus null
//send both lists, after every copy folders there is '|', after every donwload only folder there is '*'
bool result = WriteFile(
hPipe, // handle to our outbound pipe
msgCopyFldrs, // data to send
strlen(msgCopyFldrs)*sizeof(char),// length of data to send (bytes)
&numBytesWritten, // will store actual amount of data sent
NULL); // not using overlapped IO
if (result)
qDebug()<<"Qt: write in pipe(send synclist) successful, synclist = "<<msgCopyFldrs;
else
qDebug()<<"Qt: write in pipe(send synclist) NOT successful";
}
//ADD NEW SYNC
else if(bufferStr.startsWith("addsync")) // local fldrs
{
qDebug() << "Qt:read pipe addnew sync case" <<bufferStr <<localFldrsFlag;
bufferStr.remove(0,7); // removes "addsync"
if (app->isLogedIn())
{
QStringList fldrToSyncLst = bufferStr.split("|", QString::SkipEmptyParts);
app->setsyncSuggstLst(fldrToSyncLst);
app->addNewSyncLstPublic(localFldrsFlag);
}
else
app->showLoginPublic();
}
//REMOVE FROM SYNC
else if(bufferStr.startsWith("remove ")) //local fldrs
{
bufferStr.remove(0,7);
qDebug()<< "Qt: remove from sync folders:"<< bufferStr;
if(app->isLogedIn())
{
QStringList fldrsToRemoveLst = bufferStr.split("|",QString::SkipEmptyParts);
psync_folder_list_t *fldrsList = psync_get_sync_list();
if (localFldrsFlag)
{
for (int i = 0; i < fldrsToRemoveLst.size(); i++)
{
QString fldr = fldrsToRemoveLst.at(i);
for(int j = 0; j < fldrsList->foldercnt; j++) //search syncid of the synced folder
{
if(fldr == fldrsList->folders[j].localpath)
{
psync_syncid_t id = fldrsList->folders[j].syncid;
psync_delete_sync(id);
break;
}
}
}
}
else
{
for (int i = 0; i < fldrsToRemoveLst.size(); i++)
{
QString fldr = fldrsToRemoveLst.at(i);
for(int j = 0; j < fldrsList->foldercnt; j++) //search syncid of the synced folder
{
if(fldr == fldrsList->folders[j].remotepath)
{
psync_syncid_t id = fldrsList->folders[j].syncid;
psync_delete_sync(id);
break;
}
}
}
}
free(fldrsList);
app->refreshSyncUIitemsPublic(); //refresh synced folders list in syncmenu
}
else
app->showLoginPublic();
}
//SHARE FOLDER
else if(bufferStr.startsWith("sharefr"))
{
qDebug()<<"Qt: share folder case"<< bufferStr;
app->addNewSharePublic(bufferStr.remove(0,9).replace("\\","/")); // removes p: also
}
//MANAGE CRYPTO FLDR
else if(bufferStr.startsWith("managecryptofldr"))
{
if(psync_crypto_isstarted())
app->lockCryptoFldrPublic();
else
app->unlockCryptoFldrPublic();
}
}
else
{
qDebug()<< "qt read failed gle=" <<GetLastError();
if (numBytesRead == 0 && (GetLastError() == ERROR_BROKEN_PIPE))
{
qDebug()<<"qt InstanceThread: server disconnected.\n " << GetLastError();
}
else
{
qDebug() << "qt InstanceThread ReadFile failed, GLE=%d.\n "<<GetLastError();
}
}
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
}
if (hPipe != INVALID_HANDLE_VALUE){
qDebug()<< "qt after while: flush and disconnect handle";
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
}
}
ShellExtThread::~ShellExtThread()
{
qDebug()<< "qt destructing pipe";
if (hPipe != INVALID_HANDLE_VALUE)
{
closePipe();
CloseHandle(hPipe);
hPipe = INVALID_HANDLE_VALUE;
}
}
bool ShellExtThread::closePipe()
{
if (hPipe != INVALID_HANDLE_VALUE)
{
if (FlushFileBuffers(hPipe) && DisconnectNamedPipe(hPipe))
return true;
}
return false;
}
void ShellExtThread::killPipe()
{
if(closePipe() && hPipe != INVALID_HANDLE_VALUE)
{
CloseHandle(hPipe);
qDebug()<<"killpipe: handle closed";
hPipe = INVALID_HANDLE_VALUE;
}
}
#endif