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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
|
// Copyright © 2010-20 sss
//
// 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
// of the License, 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "stdafx.h"
std::list<HANDLE> sent_msgs;
struct RecvParams
{
RecvParams(MCONTACT _p1, std::wstring _p2, const char *_p3, DWORD _p4) :
hContact(_p1),
str(_p2),
msg(_p3),
timestamp(_p4)
{}
MCONTACT hContact;
std::wstring str;
std::string msg;
DWORD timestamp;
};
static void RecvMsgSvc_func(RecvParams *param)
{
MCONTACT hContact = param->hContact;
std::string szScreenName(toUTF8(Clist_GetContactDisplayName(hContact)));
// check for gpg related data
wstring::size_type s1 = param->str.find(L"-----BEGIN PGP MESSAGE-----");
wstring::size_type s2 = param->str.find(L"-----END PGP MESSAGE-----");
if (s2 != wstring::npos && s1 != wstring::npos) { //this is generic encrypted data block
if (!isContactSecured(hContact)) {
if (globals.debuglog)
globals.debuglog << "info: received encrypted message from: " + szScreenName + " with turned off encryption";
if (MessageBox(nullptr, TranslateT("We received encrypted message from contact with encryption turned off.\nDo you want to turn on encryption for this contact?"), TranslateT("Warning"), MB_YESNO) == IDYES) {
if (!isContactHaveKey(hContact))
ShowLoadPublicKeyDialog(hContact, true);
else {
g_plugin.setByte(db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact, "GPGEncryption", 1);
setSrmmIcon(hContact);
}
if (isContactHaveKey(hContact)) {
g_plugin.setByte(db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact, "GPGEncryption", 1);
setSrmmIcon(hContact);
}
}
else if (MessageBox(nullptr, TranslateT("Do you want to try to decrypt encrypted message?"), TranslateT("Warning"), MB_YESNO) == IDNO) {
HistoryLog(hContact, param->msg.c_str(), param->timestamp);
delete param;
return;
}
}
else if (globals.debuglog)
globals.debuglog << "info: received encrypted message from: " + szScreenName;
boost::algorithm::erase_all(param->str, "\r");
s2 += mir_wstrlen(L"-----END PGP MESSAGE-----");
ptrW ptszHomePath(g_plugin.getWStringA("szHomePath", L""));
wstring encfile = toUTF16(get_random(10));
wstring decfile = toUTF16(get_random(10));
{
wstring path = wstring(ptszHomePath) + L"\\tmp\\" + encfile;
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(path, e);
}
{
const int timeout = 5000, step = 100;
int count = 0;
fstream f(path.c_str(), std::ios::out);
while (!f.is_open()) {
::Sleep(step);
count += step;
if (count >= timeout) {
g_plugin.setByte(hContact, "GPGEncryption", 0);
setSrmmIcon(hContact);
globals.debuglog << "info: failed to create temporary file for decryption, disabling gpg for contact to avoid deadlock";
delete param;
return;
}
f.open(path.c_str(), std::ios::out);
}
char *tmp = mir_u2a(param->str.substr(s1, s2 - s1).c_str());
f << tmp;
mir_free(tmp);
f.close();
}
gpg_execution_params params;
params.addParam(L"--batch");
{
CMStringA inkeyid = g_plugin.getMStringA(db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact, "InKeyID");
CMStringW pass;
if (!inkeyid.IsEmpty()) {
string dbsetting = "szKey_";
dbsetting += inkeyid;
dbsetting += "_Password";
pass = g_plugin.getMStringW(dbsetting.c_str());
if (!pass.IsEmpty() && globals.debuglog)
globals.debuglog << "info: found password in database for key ID: " + string(inkeyid.c_str()) + ", trying to decrypt message from " + szScreenName + " with password";
}
else {
pass = g_plugin.getMStringW("szKeyPassword");
if (!pass.IsEmpty() && globals.debuglog)
globals.debuglog << "info: found password for all keys in database, trying to decrypt message from " + szScreenName + " with password";
}
if (!pass.IsEmpty()) {
params.addParam(L"--passphrase");
params.addParam(pass.c_str());
}
else if (!globals.wszPassword.IsEmpty()) {
if (globals.debuglog)
globals.debuglog << "info: found password in memory, trying to decrypt message from " + szScreenName + " with password";
params.addParam(L"--passphrase");
params.addParam(globals.wszPassword.c_str());
}
else if (globals.debuglog)
globals.debuglog << "info: passwords not found in database or memory, trying to decrypt message from " + szScreenName + " without password";
}
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(wstring(ptszHomePath) + L"\\tmp\\" + decfile, e);
}
params.addParam(L"--output");
params.addParam(std::wstring(ptszHomePath) + L"\\tmp\\" + decfile);
params.addParam(L"-d");
params.addParam(L"-a");
params.addParam(path);
bool bRes = gpg_launcher(params);
if (!bRes || params.result == pxNotFound) {
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(path, e);
}
SendErrorMessage(hContact);
HistoryLog(hContact, TranslateU("GPG cannot decrypt imcoming message"), param->timestamp);
delete param;
return;
}
// TODO: check gpg output for errors
globals._terminate = false;
string out(params.out);
while (out.find("public key decryption failed: bad passphrase") != string::npos) {
if (globals.debuglog)
globals.debuglog << "info: failed to decrypt messaage from " + szScreenName + " password needed, trying to get one";
if (globals._terminate) {
SendErrorMessage(hContact);
break;
}
{
// save inkey id
string::size_type s = out.find(" encrypted with ");
s = out.find(" ID ", s);
s += mir_strlen(" ID ");
g_plugin.setString(db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact, "InKeyID", out.substr(s, out.find(",", s) - s).c_str());
}
CDlgKeyPasswordMsgBox(hContact).DoModal();
gpg_execution_params params2;
params2.aargv = params.aargv;
if (!globals.wszPassword.IsEmpty()) {
if (globals.debuglog)
globals.debuglog << "info: found password in memory, trying to decrypt message from " + szScreenName;
params2.addParam(L"--passphrase");
params2.addParam(globals.wszPassword.c_str());
}
bRes = gpg_launcher(params2);
if (!bRes || params2.result == pxNotFound) {
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(path, e);
}
HistoryLog(hContact, TranslateU("GPG cannot decrypt imcoming message"), param->timestamp);
SendErrorMessage(hContact);
delete param;
return;
}
}
out.clear();
bRes = gpg_launcher(params);
if (!bRes || params.result == pxNotFound) {
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(path, e);
}
HistoryLog(hContact, TranslateU("GPG cannot decrypt imcoming message"), param->timestamp);
SendErrorMessage(hContact);
delete param;
return;
}
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(wstring(ptszHomePath) + L"\\tmp\\" + encfile, e);
}
if (!boost::filesystem::exists(wstring(ptszHomePath) + L"\\tmp\\" + decfile)) {
if (globals.debuglog)
globals.debuglog << "info: Failed to decrypt GPG encrypted message.";
string str1 = param->msg;
str1.insert(0, "\n");
str1.insert(0, TranslateU("Received unencrypted message:"));
HistoryLog(hContact, str1.c_str(), param->timestamp);
SendErrorMessage(hContact);
delete param;
return;
}
param->str.clear();
wstring tszDecPath = wstring(ptszHomePath) + L"\\tmp\\" + decfile;
fstream f(tszDecPath.c_str(), std::ios::in | std::ios::ate | std::ios::binary);
if (f.is_open()) {
size_t size = f.tellg();
char *tmp = new char[size + 1];
f.seekg(0, std::ios::beg);
f.read(tmp, size);
tmp[size] = '\0';
param->str.append(toUTF16(tmp));
delete[] tmp;
f.close();
if (!globals.debuglog) {
boost::system::error_code ec;
boost::filesystem::remove(tszDecPath, ec);
if (ec) {
//TODO: handle error
}
}
}
if (param->str.empty()) {
if (globals.debuglog)
globals.debuglog << "info: Failed to decrypt GPG encrypted message.";
string szMsg = param->msg;
szMsg.insert(0, TranslateU("Failed to decrypt GPG encrypted message.\nMessage body for manual decryption:\n"));
HistoryLog(hContact, param->msg.c_str(), param->timestamp);
SendErrorMessage(hContact);
delete param;
return;
}
fix_line_term(param->str);
if (g_plugin.bAppendTags) {
param->str.insert(0, globals.wszInopentag);
param->str.append(globals.wszInclosetag);
}
HistoryLog(hContact, toUTF8(param->str).c_str(), param->timestamp);
delete param;
return;
}
}
if (g_plugin.getByte(db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact, "GPGEncryption"))
HistoryLog(hContact, param->msg.c_str(), param->timestamp, DBEF_READ);
else
HistoryLog(hContact, param->msg.c_str(), param->timestamp);
delete param;
}
INT_PTR RecvMsgSvc(WPARAM w, LPARAM l)
{
CCSDATA *ccs = (CCSDATA*)l;
if (!ccs)
return Proto_ChainRecv(w, ccs);
PROTORECVEVENT *pre = (PROTORECVEVENT*)(ccs->lParam);
if (!pre)
return Proto_ChainRecv(w, ccs);
char *msg = pre->szMessage;
if (!msg)
return Proto_ChainRecv(w, ccs);
if (db_mc_isMeta(ccs->hContact)) {
if (!strstr(msg, "-----BEGIN PGP MESSAGE-----"))
return Proto_ChainRecv(w, ccs);
else {
if (globals.debuglog)
globals.debuglog << "info: blocked pgp message to metacontact:" + toUTF8(Clist_GetContactDisplayName(ccs->hContact));
return 0;
}
}
wstring str = toUTF16(msg);
size_t s1, s2;
if (g_plugin.bAutoExchange && (str.find(L"-----PGP KEY RESPONSE-----") != wstring::npos)) {
if (globals.debuglog)
globals.debuglog << "info(autoexchange): parsing key response:" + toUTF8(Clist_GetContactDisplayName(ccs->hContact));
s2 = str.find(L"-----END PGP PUBLIC KEY BLOCK-----");
s1 = str.find(L"-----BEGIN PGP PUBLIC KEY BLOCK-----");
if (s1 != wstring::npos && s2 != wstring::npos) {
if (globals.debuglog)
globals.debuglog << "info(autoexchange): found pubkey block:" + toUTF8(Clist_GetContactDisplayName(ccs->hContact));
s2 += mir_wstrlen(L"-----END PGP PUBLIC KEY BLOCK-----");
g_plugin.setWString(ccs->hContact, "GPGPubKey", str.substr(s1, s2 - s1).c_str());
{
// gpg execute block
CMStringW tmp2(g_plugin.getMStringW("szHomePath"));
tmp2 += L"\\";
tmp2 += get_random(5).c_str();
tmp2 += L".asc";
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(tmp2.c_str(), e);
}
wfstream f(tmp2, std::ios::out);
{
const int timeout = 5000, step = 100;
int count = 0;
while (!f.is_open()) {
::Sleep(step);
count += step;
if (count >= timeout) {
g_plugin.setByte(ccs->hContact, "GPGEncryption", 0);
setSrmmIcon(ccs->hContact);
globals.debuglog << "info: failed to create temporary file for decryption, disabling gpg for contact to avoid deadlock";
return 1;
}
f.open(tmp2, std::ios::out);
}
}
f << g_plugin.getMStringW(ccs->hContact, "GPGPubKey").c_str();
f.close();
gpg_execution_params params;
params.addParam(L"--batch");
params.addParam(L"--import");
params.addParam(tmp2.c_str());
if (!gpg_launcher(params))
return 1;
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(tmp2.c_str(), e);
}
if (params.result == pxNotFound)
return 1;
string output(params.out);
s1 = output.find("gpg: key ") + mir_strlen("gpg: key ");
s2 = output.find(":", s1);
g_plugin.setString(ccs->hContact, "KeyID", output.substr(s1, s2 - s1).c_str());
s2 += 2;
s1 = output.find(RUS_QUOTE, s2);
if (s1 == string::npos) {
s1 = output.find("\"", s2);
s1 += 1;
}
else s1 += sizeof(RUS_QUOTE) - 1;
if ((s2 = output.find("(", s1)) == string::npos)
s2 = output.find("<", s1);
else if (s2 > output.find("<", s1))
s2 = output.find("<", s1);
char *tmp = (char*)mir_alloc(output.substr(s1, s2 - s1 - 1).length() + 1);
mir_strcpy(tmp, output.substr(s1, s2 - s1 - 1).c_str());
mir_utf8decode(tmp, nullptr);
g_plugin.setString(ccs->hContact, "KeyMainName", tmp);
mir_free(tmp);
if ((s1 = output.find(")", s2)) == string::npos)
s1 = output.find(">", s2);
else if (s1 > output.find(">", s2))
s1 = output.find(">", s2);
s2++;
if (output[s1] == ')') {
tmp = (char*)mir_alloc(output.substr(s2, s1 - s2).length() + 1);
mir_strcpy(tmp, output.substr(s2, s1 - s2).c_str());
mir_utf8decode(tmp, nullptr);
g_plugin.setString(ccs->hContact, "KeyComment", tmp);
mir_free(tmp);
s1 += 3;
s2 = output.find(">", s1);
tmp = (char*)mir_alloc(output.substr(s1, s2 - s1).length() + 1);
mir_strcpy(tmp, output.substr(s1, s2 - s1).c_str());
mir_utf8decode(tmp, nullptr);
g_plugin.setString(ccs->hContact, "KeyMainEmail", tmp);
mir_free(tmp);
}
else {
tmp = (char*)mir_alloc(output.substr(s2, s1 - s2).length() + 1);
mir_strcpy(tmp, output.substr(s2, s1 - s2).c_str());
mir_utf8decode(tmp, nullptr);
g_plugin.setString(ccs->hContact, "KeyMainEmail", output.substr(s2, s1 - s2).c_str());
mir_free(tmp);
}
g_plugin.setByte(ccs->hContact, "GPGEncryption", 1);
g_plugin.setByte(ccs->hContact, "bAlwatsTrust", 1);
setSrmmIcon(ccs->hContact);
if (db_mc_isSub(ccs->hContact))
setSrmmIcon(db_mc_getMeta(ccs->hContact));
HistoryLog(ccs->hContact, "PGP Encryption turned on by key autoexchange feature");
}
return 1;
}
}
if (((s2 = str.find(L"-----END PGP PUBLIC KEY BLOCK-----")) == wstring::npos) || ((s1 = str.find(L"-----BEGIN PGP PUBLIC KEY BLOCK-----")) == wstring::npos)) {
s2 = str.find(L"-----END PGP PRIVATE KEY BLOCK-----");
s1 = str.find(L"-----BEGIN PGP PRIVATE KEY BLOCK-----");
}
if ((s2 != wstring::npos) && (s1 != wstring::npos)) { //this is public key
if (globals.debuglog)
globals.debuglog << "info: received key from: " + toUTF8(Clist_GetContactDisplayName(ccs->hContact));
s1 = 0;
while ((s1 = str.find(L"\r", s1)) != wstring::npos)
str.erase(s1, 1);
if (((s2 = str.find(L"-----END PGP PUBLIC KEY BLOCK-----")) != wstring::npos) && ((s1 = str.find(L"-----BEGIN PGP PUBLIC KEY BLOCK-----")) != wstring::npos))
s2 += mir_wstrlen(L"-----END PGP PUBLIC KEY BLOCK-----");
else if (((s2 = str.find(L"-----BEGIN PGP PRIVATE KEY BLOCK-----")) != wstring::npos) && ((s1 = str.find(L"-----END PGP PRIVATE KEY BLOCK-----")) != wstring::npos))
s2 += mir_wstrlen(L"-----END PGP PRIVATE KEY BLOCK-----");
CDlgNewKey *d = new CDlgNewKey(ccs->hContact, str.substr(s1, s2 - s1));
d->Show();
HistoryLog(ccs->hContact, msg);
return 0;
}
if (g_plugin.bAutoExchange && strstr(msg, "-----PGP KEY REQUEST-----") && globals.gpg_valid && globals.gpg_keyexist) {
if (globals.debuglog)
globals.debuglog << "info(autoexchange): received key request from: " + toUTF8(Clist_GetContactDisplayName(ccs->hContact));
CMStringA tmp(g_plugin.getMStringA("GPGPubKey"));
if (!tmp.IsEmpty()) {
int enc_state = g_plugin.getByte(ccs->hContact, "GPGEncryption");
if (enc_state)
g_plugin.setByte(ccs->hContact, "GPGEncryption", 0);
string str1 = "-----PGP KEY RESPONSE-----";
str1.append(tmp);
ProtoChainSend(ccs->hContact, PSS_MESSAGE, 0, (LPARAM)str1.c_str());
if (enc_state)
g_plugin.setByte(ccs->hContact, "GPGEncryption", 1);
}
return 0;
}
else if (!isContactHaveKey(ccs->hContact) && g_plugin.bAutoExchange && globals.gpg_valid && globals.gpg_keyexist) {
char *proto = Proto_GetBaseAccountName(ccs->hContact);
ptrA jid(db_get_utfa(ccs->hContact, proto, "jid", ""));
if (jid[0]) {
for (auto p : globals.Accounts) {
ptrA caps(p->getJabberInterface()->GetResourceFeatures(jid));
if (caps) {
string str1;
for (int i = 0;; i++) {
str1.push_back(caps[i]);
if (caps[i] == '\0')
if (caps[i + 1] == '\0')
break;
}
if (str1.find("GPG_Key_Auto_Exchange:0") != string::npos) {
ProtoChainSend(ccs->hContact, PSS_MESSAGE, 0, (LPARAM)"-----PGP KEY REQUEST-----");
return 0;
}
}
}
}
}
if (!strstr(msg, "-----BEGIN PGP MESSAGE-----"))
return Proto_ChainRecv(w, ccs);
mir_forkThread<RecvParams>(RecvMsgSvc_func, new RecvParams(ccs->hContact, str, msg, pre->timestamp));
return 0;
}
void SendMsgSvc_func(MCONTACT hContact, char *msg, DWORD flags)
{
wstring str = toUTF16(msg);
if (g_plugin.bStripTags && g_plugin.bAppendTags) {
if (globals.debuglog)
globals.debuglog << "info: stripping tags in outgoing message, name: " + toUTF8(Clist_GetContactDisplayName(hContact));
strip_tags(str);
}
LBL_Relaunch:
wstring file = toUTF16(get_random(10));
gpg_execution_params params;
{
CMStringA tmp(g_plugin.getMStringA(hContact, "KeyID"));
if (tmp.IsEmpty()) {
HistoryLog(hContact, "Failed to encrypt message with GPG (not found key for encryption in db", DBEF_SENT);
ProtoChainSend(hContact, PSS_MESSAGE, flags, (LPARAM)msg);
return;
}
if (g_plugin.getByte(hContact, "bAlwaysTrust", 0)) {
params.addParam(L"--trust-model");
params.addParam(L"always");
}
params.addParam(L"--batch");
params.addParam(L"--yes");
params.addParam(L"-eatr");
params.addParam(_A2T(tmp).get());
}
CMStringW path(g_plugin.getMStringW("szHomePath"));
path += L"\\tmp\\";
path += file.c_str();
params.addParam(path.c_str());
const int timeout = 5000, step = 100;
int count = 0;
{
fstream f(path.c_str(), std::ios::out);
while (!f.is_open()) {
::Sleep(step);
count += step;
if (count >= timeout) {
g_plugin.setByte(hContact, "GPGEncryption", 0); //disable encryption
setSrmmIcon(hContact);
globals.debuglog << "info: failed to create temporary file for encryption, disabling encryption to avoid deadlock";
break;
}
f.open(path.c_str(), std::ios::out);
}
if (count < timeout) {
std::string tmp = toUTF8(str);
f.write(tmp.c_str(), tmp.size());
f.close();
}
}
if (!gpg_launcher(params)) {
ProtoChainSend(hContact, PSS_MESSAGE, flags, (LPARAM)msg);
return;
}
if (params.result == pxNotFound) {
ProtoChainSend(hContact, PSS_MESSAGE, flags, (LPARAM)msg);
return;
}
if (params.out.Find("There is no assurance this key belongs to the named user") != -1) {
if (IDYES != MessageBox(nullptr, TranslateT("We're trying to encrypt with untrusted key. Do you want to trust this key permanently?"), TranslateT("Warning"), MB_YESNO))
return;
g_plugin.setByte(hContact, "bAlwaysTrust", 1);
params.aargv.clear();
goto LBL_Relaunch;
}
if (params.out.Find("usage: ") != -1) {
MessageBox(nullptr, TranslateT("Something is wrong, GPG does not understand us, aborting encryption."), TranslateT("Warning"), MB_OK);
//mir_free(msg);
ProtoChainSend(hContact, PSS_MESSAGE, flags, (LPARAM)msg);
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(path.c_str(), e);
}
return;
}
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(path.c_str(), e);
}
path += L".asc";
wfstream f(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary);
count = 0;
while (!f.is_open()) {
::Sleep(step);
f.open(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary);
count += step;
if (count >= timeout) {
g_plugin.setByte(hContact, "GPGEncryption", 0); //disable encryption
setSrmmIcon(hContact);
globals.debuglog << "info: gpg failed to encrypt message, disabling encryption to avoid deadlock";
break;
}
}
str.clear();
if (f.is_open()) {
std::wifstream::pos_type size = f.tellg();
wchar_t *tmp = new wchar_t[(std::ifstream::pos_type)size + (std::ifstream::pos_type)1];
f.seekg(0, std::ios::beg);
f.read(tmp, size);
tmp[size] = '\0';
str.append(tmp);
delete[] tmp;
f.close();
if (!globals.debuglog) {
boost::system::error_code e;
boost::filesystem::remove(path.c_str(), e);
}
}
if (str.empty()) {
HistoryLog(hContact, "Failed to encrypt message with GPG", DBEF_SENT);
if (globals.debuglog)
globals.debuglog << "info: Failed to encrypt message with GPG";
ProtoChainSend(hContact, PSS_MESSAGE, flags, (LPARAM)msg);
return;
}
string str_event = msg;
if (g_plugin.bAppendTags) {
str_event.insert(0, toUTF8(globals.wszOutopentag.c_str()));
str_event.append(toUTF8(globals.wszOutclosetag.c_str()));
}
if (globals.debuglog)
globals.debuglog << "adding event to contact: " + toUTF8(Clist_GetContactDisplayName(hContact)) + " on send message.";
fix_line_term(str);
sent_msgs.push_back((HANDLE)ProtoChainSend(hContact, PSS_MESSAGE, flags, (LPARAM)toUTF8(str).c_str()));
}
INT_PTR SendMsgSvc(WPARAM w, LPARAM l)
{
CCSDATA *ccs = (CCSDATA*)l;
if (!ccs)
return Proto_ChainSend(w, ccs);
if (!ccs->lParam)
return Proto_ChainSend(w, ccs);
std::string szScreenName(toUTF8(Clist_GetContactDisplayName(ccs->hContact)));
char *msg = (char*)ccs->lParam;
if (!msg) {
if (globals.debuglog)
globals.debuglog << "info: failed to get message data, name: " + szScreenName;
return Proto_ChainSend(w, ccs);
}
if (strstr(msg, "-----BEGIN PGP MESSAGE-----")) {
if (globals.debuglog)
globals.debuglog << "info: encrypted message, let it go, name: " + szScreenName;
return Proto_ChainSend(w, ccs);
}
if (globals.debuglog)
globals.debuglog << "info: contact have key, name: " + szScreenName;
if (globals.debuglog && db_mc_isMeta(ccs->hContact))
globals.debuglog << "info: protocol is metacontacts, name: " + szScreenName;
if (!isContactSecured(ccs->hContact) || db_mc_isMeta(ccs->hContact)) {
if (globals.debuglog)
globals.debuglog << "info: contact not secured, name: " + szScreenName;
return Proto_ChainSend(w, ccs);
}
ProtoBroadcastAsync(Proto_GetBaseAccountName(ccs->hContact), ccs->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)777);
return 777;
}
int HookSendMsg(WPARAM w, LPARAM l)
{
if (!l)
return 0;
DBEVENTINFO * dbei = (DBEVENTINFO*)l;
if (dbei->eventType != EVENTTYPE_MESSAGE)
return 0;
MCONTACT hContact = (MCONTACT)w;
std::string szScreenName(toUTF8(Clist_GetContactDisplayName(hContact)));
if (dbei->flags & DBEF_SENT) {
if (isContactSecured(hContact) && strstr((char*)dbei->pBlob, "-----BEGIN PGP MESSAGE-----")) //our service data, can be double added by metacontacts e.w.c.
{
if (globals.debuglog)
globals.debuglog << "info(send handler): block pgp message event, name: " + szScreenName;
return 1;
}
if (g_plugin.bAutoExchange && (strstr((char*)dbei->pBlob, "-----PGP KEY RESPONSE-----") || strstr((char*)dbei->pBlob, "-----PGP KEY REQUEST-----"))) ///do not show service data in history
{
if (globals.debuglog)
globals.debuglog << "info(send handler): block pgp key request/response event, name: " + szScreenName;
return 1;
}
}
if (db_mc_isMeta(hContact))
return 0;
if (!isContactHaveKey(hContact)) {
if (globals.debuglog)
globals.debuglog << "info: contact have not key, name: " + szScreenName;
if (g_plugin.bAutoExchange && !strstr((char*)dbei->pBlob, "-----PGP KEY REQUEST-----") && !strstr((char*)dbei->pBlob, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && globals.gpg_valid) {
if (globals.debuglog)
globals.debuglog << "info: checking for autoexchange possibility, name: " + szScreenName;
LPSTR proto = Proto_GetBaseAccountName(hContact);
ptrA jid(db_get_utfa(hContact, proto, "jid", ""));
if (jid[0]) {
if (globals.debuglog)
globals.debuglog << "info(autoexchange): protocol looks like jabber, name: " + szScreenName;
for (auto p : globals.Accounts) {
ptrA caps(p->getJabberInterface()->GetResourceFeatures(jid));
if (caps) {
string str;
for (int i = 0;; i++) {
str.push_back(caps[i]);
if (caps[i] == '\0')
if (caps[i + 1] == '\0')
break;
}
if (str.find("GPG_Key_Auto_Exchange:0") != string::npos) {
if (globals.debuglog)
globals.debuglog << "info(autoexchange, jabber): autoexchange capability found, sending key request, name: " + szScreenName;
ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)"-----PGP KEY REQUEST-----");
globals.hcontact_data[hContact].msgs_to_send.push_back((char*)dbei->pBlob);
mir_forkthread(send_encrypted_msgs_thread, (void*)hContact);
return 0;
}
}
}
}
}
else return 0;
}
if (isContactSecured(hContact) && (dbei->flags & DBEF_SENT)) //aggressive outgoing events filtering
{
SendMsgSvc_func(hContact, (char*)dbei->pBlob, 0);
//TODO: handle errors somehow ...
if (g_plugin.bAppendTags) {
string str_event = (char*)dbei->pBlob;
//mir_free(dbei->pBlob);
str_event.insert(0, toUTF8(globals.wszOutopentag.c_str()));
str_event.append(toUTF8(globals.wszOutclosetag.c_str()));
dbei->pBlob = (PBYTE)mir_strdup(str_event.c_str());
dbei->cbBlob = (DWORD)str_event.length() + 1;
}
return 0;
}
if (!isContactSecured(hContact)) {
if (globals.debuglog)
globals.debuglog << "event message: \"" + string((char*)dbei->pBlob) + "\" passed event filter, contact " + szScreenName + " is unsecured";
return 0;
}
if (!(dbei->flags & DBEF_SENT) && db_mc_isMeta((MCONTACT)w)) {
char tmp[29];
strncpy(tmp, (char*)dbei->pBlob, 27);
tmp[28] = '\0';
if (strstr(tmp, "-----BEGIN PGP MESSAGE-----")) {
if (globals.debuglog)
globals.debuglog << "info(send handler): block pgp message event, name: " + szScreenName;
return 1;
}
}
return 0;
}
|