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
|
// Copyright © 2010-2012 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"
#pragma comment(lib, "shlwapi.lib")
void FirstRun()
{
if (!db_get_b(NULL, szGPGModuleName, "FirstRun", 1))
return;
CDlgGpgBinOpts *d = new CDlgGpgBinOpts;
d->Show();
}
void InitCheck()
{
{
// parse gpg output
wchar_t *current_home = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", L"");
db_set_ws(NULL, szGPGModuleName, "szHomePath", L""); //we do not need home for gpg binary validation
globals.gpg_valid = isGPGValid();
db_set_ws(NULL, szGPGModuleName, "szHomePath", current_home); //return current home dir back
mir_free(current_home);
bool home_dir_access = false, temp_access = false;
wchar_t *home_dir = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", L"");
std::wstring test_path = home_dir;
mir_free(home_dir);
test_path += L"/";
test_path += toUTF16(get_random(13));
wfstream test_file;
test_file.open(test_path, std::ios::trunc | std::ios::out);
if (test_file.is_open() && test_file.good()) {
test_file << L"access_test\n";
if (test_file.good())
home_dir_access = true;
test_file.close();
boost::filesystem::remove(test_path);
}
home_dir = _tgetenv(L"TEMP");
test_path = home_dir;
test_path += L"/";
test_path += toUTF16(get_random(13));
test_file.open(test_path, std::ios::trunc | std::ios::out);
if (test_file.is_open() && test_file.good()) {
test_file << L"access_test\n";
if (test_file.good())
temp_access = true;
test_file.close();
boost::filesystem::remove(test_path);
}
if (!home_dir_access || !temp_access || !globals.gpg_valid) {
wchar_t buf[4096];
wcsncpy(buf, globals.gpg_valid ? TranslateT("GPG binary is set and valid (this is good).\n") : TranslateT("GPG binary unset or invalid (plugin will not work).\n"), _countof(buf));
mir_wstrncat(buf, home_dir_access ? TranslateT("Home dir write access granted (this is good).\n") : TranslateT("Home dir has no write access (plugin most probably will not work).\n"), _countof(buf) - mir_wstrlen(buf));
mir_wstrncat(buf, temp_access ? TranslateT("Temp dir write access granted (this is good).\n") : TranslateT("Temp dir has no write access (plugin should work, but may have some problems, file transfers will not work)."), _countof(buf) - mir_wstrlen(buf));
if (!globals.gpg_valid)
mir_wstrncat(buf, TranslateT("\nGPG will be disabled until you solve these problems"), _countof(buf) - mir_wstrlen(buf));
MessageBox(nullptr, buf, TranslateT("GPG plugin problems"), MB_OK);
}
if (!globals.gpg_valid)
return;
globals.gpg_keyexist = isGPGKeyExist();
string out;
DWORD code;
pxResult result;
wstring::size_type p = 0, p2 = 0;
{
std::vector<wstring> cmd;
cmd.push_back(L"--batch");
cmd.push_back(L"--list-secret-keys");
gpg_execution_params params(cmd);
params.out = &out;
params.code = &code;
params.result = &result;
if (!gpg_launcher(params))
return;
if (result == pxNotFound)
return;
}
home_dir = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", L"");
wstring tmp_dir = home_dir;
mir_free(home_dir);
tmp_dir += L"\\tmp";
_wmkdir(tmp_dir.c_str());
int count = 0;
PROTOACCOUNT **accounts;
Proto_EnumAccounts(&count, &accounts);
string question;
char *keyid = nullptr;
for (int i = 0; i < count; i++) {
if (StriStr(accounts[i]->szModuleName, "metacontacts"))
continue;
if (StriStr(accounts[i]->szModuleName, "weather"))
continue;
std::string acc = toUTF8(accounts[i]->tszAccountName);
acc += "(";
acc += accounts[i]->szModuleName;
acc += ")";
acc += "_KeyID";
keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, acc.c_str(), "");
if (keyid[0]) {
question = Translate("Your secret key with ID: ");
mir_free(keyid);
keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
if ((p = out.find(keyid)) == string::npos) {
question += keyid;
question += Translate(" for account ");
question += toUTF8(accounts[i]->tszAccountName);
question += Translate(" deleted from GPG secret keyring.\nDo you want to set another key?");
if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
{
CDlgFirstRun *d = new CDlgFirstRun;
d->DoModal();
}
}
p2 = p;
p = out.find("[", p);
p2 = out.find("\n", p2);
if ((p != std::string::npos) && (p < p2)) {
p = out.find("expires:", p);
p += mir_strlen("expires:");
p++;
p2 = out.find("]", p);
wchar_t *expire_date = mir_wstrdup(toUTF16(out.substr(p, p2 - p)).c_str());
bool expired = false;
{
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
wchar_t buf[5];
wcsncpy_s(buf, expire_date, _TRUNCATE);
int year = _wtoi(buf);
if (year < now.date().year())
expired = true;
else if (year == now.date().year()) {
wcsncpy_s(buf, (expire_date + 5), _TRUNCATE);
int month = _wtoi(buf);
if (month < now.date().month())
expired = true;
else if (month == now.date().month()) {
wcsncpy_s(buf, (expire_date + 8), _TRUNCATE);
unsigned day = _wtoi(buf);
if (day <= now.date().day_number())
expired = true;
}
}
}
if (expired) {
question += keyid;
question += Translate(" for account ");
question += toUTF8(accounts[i]->tszAccountName);
question += Translate(" expired and will not work.\nDo you want to set another key?");
if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
{
CDlgFirstRun *d = new CDlgFirstRun;
d->DoModal();
}
}
mir_free(expire_date);
}
}
if (keyid) {
mir_free(keyid);
keyid = nullptr;
}
}
question = Translate("Your secret key with ID: ");
keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
char *key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", "");
if (!db_get_b(NULL, szGPGModuleName, "FirstRun", 1) && (!keyid[0] || !key[0])) {
question = Translate("You didn't set a private key.\nWould you like to set it now?");
if (MessageBoxA(nullptr, question.c_str(), Translate("Own private key warning"), MB_YESNO) == IDYES)
{
CDlgFirstRun *d = new CDlgFirstRun;
d->DoModal();
}
}
if ((p = out.find(keyid)) == string::npos) {
question += keyid;
question += Translate(" deleted from GPG secret keyring.\nDo you want to set another key?");
if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
{
CDlgFirstRun *d = new CDlgFirstRun;
d->DoModal();
}
}
p2 = p;
p = out.find("[", p);
p2 = out.find("\n", p2);
if ((p != std::string::npos) && (p < p2)) {
p = out.find("expires:", p);
p += mir_strlen("expires:");
p++;
p2 = out.find("]", p);
wchar_t *expire_date = mir_wstrdup(toUTF16(out.substr(p, p2 - p)).c_str());
bool expired = false;
{
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
wchar_t buf[5];
wcsncpy_s(buf, expire_date, _TRUNCATE);
int year = _wtoi(buf);
if (year < now.date().year())
expired = true;
else if (year == now.date().year()) {
wcsncpy_s(buf, (expire_date + 5), _TRUNCATE);
int month = _wtoi(buf);
if (month < now.date().month())
expired = true;
else if (month == now.date().month()) {
wcsncpy_s(buf, (expire_date + 8), _TRUNCATE);
unsigned day = _wtoi(buf);
if (day <= now.date().day_number())
expired = true;
}
}
}
if (expired) {
question += keyid;
question += Translate(" expired and will not work.\nDo you want to set another key?");
if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
{
CDlgFirstRun *d = new CDlgFirstRun;
d->DoModal();
}
}
mir_free(expire_date);
}
// TODO: check for expired key
mir_free(keyid);
mir_free(key);
}
{
wchar_t *path = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", L"");
DWORD dwFileAttr = GetFileAttributes(path);
if (dwFileAttr != INVALID_FILE_ATTRIBUTES) {
dwFileAttr &= ~FILE_ATTRIBUTE_READONLY;
SetFileAttributes(path, dwFileAttr);
}
mir_free(path);
}
if (globals.bAutoExchange) {
int count = 0;
PROTOACCOUNT **accounts;
Proto_EnumAccounts(&count, &accounts);
ICQ_CUSTOMCAP cap;
cap.cbSize = sizeof(ICQ_CUSTOMCAP);
cap.hIcon = nullptr;
strncpy(cap.name, "GPG Key AutoExchange", MAX_CAPNAME);
strncpy(cap.caps, "GPGAutoExchange", sizeof(cap.caps));
for (int i = 0; i < count; i++)
if (ProtoServiceExists(accounts[i]->szProtoName, PS_ICQ_ADDCAPABILITY))
CallProtoService(accounts[i]->szProtoName, PS_ICQ_ADDCAPABILITY, 0, (LPARAM)&cap);
}
if (globals.bFileTransfers) {
int count = 0;
PROTOACCOUNT **accounts;
Proto_EnumAccounts(&count, &accounts);
ICQ_CUSTOMCAP cap;
cap.cbSize = sizeof(ICQ_CUSTOMCAP);
cap.hIcon = nullptr;
strncpy(cap.name, "GPG Encrypted FileTransfers", MAX_CAPNAME);
strncpy(cap.caps, "GPGFileTransfer", sizeof(cap.caps));
for (int i = 0; i < count; i++)
if (ProtoServiceExists(accounts[i]->szProtoName, PS_ICQ_ADDCAPABILITY))
CallProtoService(accounts[i]->szProtoName, PS_ICQ_ADDCAPABILITY, 0, (LPARAM)&cap);
}
}
void ImportKey(MCONTACT hContact, std::wstring new_key)
{
bool for_all_sub = false;
if (db_mc_isMeta(hContact)) {
if (MessageBox(nullptr, TranslateT("Do you want to load key for all subcontacts?"), TranslateT("Metacontact detected"), MB_YESNO) == IDYES)
for_all_sub = true;
if (for_all_sub) {
int count = db_mc_getSubCount(hContact);
for (int i = 0; i < count; i++) {
MCONTACT hcnt = db_mc_getSub(hContact, i);
if (hcnt)
db_set_ws(hcnt, szGPGModuleName, "GPGPubKey", new_key.c_str());
}
}
else db_set_ws(metaGetMostOnline(hContact), szGPGModuleName, "GPGPubKey", new_key.c_str());
}
else db_set_ws(hContact, szGPGModuleName, "GPGPubKey", new_key.c_str());
// gpg execute block
std::vector<wstring> cmd;
wchar_t tmp2[MAX_PATH] = { 0 };
{
wcsncpy(tmp2, ptrW(UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", L"")), MAX_PATH - 1);
mir_wstrncat(tmp2, L"\\", _countof(tmp2) - mir_wstrlen(tmp2));
mir_wstrncat(tmp2, L"temporary_exported.asc", _countof(tmp2) - mir_wstrlen(tmp2));
boost::filesystem::remove(tmp2);
ptrW ptmp;
if (db_mc_isMeta(hContact))
ptmp = UniGetContactSettingUtf(metaGetMostOnline(hContact), szGPGModuleName, "GPGPubKey", L"");
else
ptmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", L"");
wfstream f(tmp2, std::ios::out);
f << ptmp.get();
f.close();
cmd.push_back(L"--batch");
cmd.push_back(L"--import");
cmd.push_back(tmp2);
}
gpg_execution_params params(cmd);
string output;
DWORD exitcode;
pxResult result;
params.out = &output;
params.code = &exitcode;
params.result = &result;
if (!gpg_launcher(params))
return;
if (result == pxNotFound)
return;
if (db_mc_isMeta(hContact)) {
if (for_all_sub) {
int count = db_mc_getSubCount(hContact);
for (int i = 0; i < count; i++) {
MCONTACT hcnt = db_mc_getSub(hContact, i);
if (hcnt) {
char *tmp = nullptr;
string::size_type s = output.find("gpg: key ") + mir_strlen("gpg: key ");
string::size_type s2 = output.find(":", s);
db_set_s(hcnt, szGPGModuleName, "KeyID", output.substr(s, s2 - s).c_str());
s = output.find("“", s2);
if (s == string::npos) {
s = output.find("\"", s2);
s += 1;
}
else s += 3;
bool uncommon = false;
if ((s2 = output.find("(", s)) == string::npos) {
if ((s2 = output.find("<", s)) == string::npos) {
s2 = output.find("”", s);
uncommon = true;
}
}
else if (s2 > output.find("<", s))
s2 = output.find("<", s);
if (s != string::npos && s2 != string::npos) {
tmp = (char*)mir_alloc(sizeof(char)*(output.substr(s, s2 - s - (uncommon ? 1 : 0)).length() + 1));
mir_strcpy(tmp, output.substr(s, s2 - s - (uncommon ? 1 : 0)).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(hcnt, szGPGModuleName, "KeyMainName", tmp);
mir_free(tmp);
}
if ((s = output.find(")", s2)) == string::npos)
s = output.find(">", s2);
else if (s > output.find(">", s2))
s = output.find(">", s2);
s2++;
if (s != string::npos && s2 != string::npos) {
if (output[s] == ')') {
tmp = (char*)mir_alloc(sizeof(char)* (output.substr(s2, s - s2).length() + 1));
mir_strcpy(tmp, output.substr(s2, s - s2).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(hcnt, szGPGModuleName, "KeyComment", tmp);
mir_free(tmp);
s += 3;
s2 = output.find(">", s);
if (s != string::npos && s2 != string::npos) {
tmp = (char*)mir_alloc(sizeof(char)*(output.substr(s, s2 - s).length() + 1));
mir_strcpy(tmp, output.substr(s, s2 - s).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(hcnt, szGPGModuleName, "KeyMainEmail", tmp);
mir_free(tmp);
}
}
else {
tmp = (char*)mir_alloc(sizeof(char)* (output.substr(s2, s - s2).length() + 1));
mir_strcpy(tmp, output.substr(s2, s - s2).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(hcnt, szGPGModuleName, "KeyMainEmail", output.substr(s2, s - s2).c_str());
mir_free(tmp);
}
}
db_unset(hcnt, szGPGModuleName, "bAlwatsTrust");
}
}
}
else {
char *tmp = nullptr;
string::size_type s = output.find("gpg: key ") + mir_strlen("gpg: key ");
string::size_type s2 = output.find(":", s);
db_set_s(metaGetMostOnline(hContact), szGPGModuleName, "KeyID", output.substr(s, s2 - s).c_str());
s = output.find("“", s2);
if (s == string::npos) {
s = output.find("\"", s2);
s += 1;
}
else
s += 3;
bool uncommon = false;
if ((s2 = output.find("(", s)) == string::npos) {
if ((s2 = output.find("<", s)) == string::npos) {
s2 = output.find("”", s);
uncommon = true;
}
}
else if (s2 > output.find("<", s))
s2 = output.find("<", s);
if (s != string::npos && s2 != string::npos) {
tmp = (char*)mir_alloc(sizeof(char)*(output.substr(s, s2 - s - (uncommon ? 1 : 0)).length() + 1));
mir_strcpy(tmp, output.substr(s, s2 - s - (uncommon ? 1 : 0)).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(metaGetMostOnline(hContact), szGPGModuleName, "KeyMainName", tmp);
mir_free(tmp);
}
if ((s = output.find(")", s2)) == string::npos)
s = output.find(">", s2);
else if (s > output.find(">", s2))
s = output.find(">", s2);
s2++;
if (s != string::npos && s2 != string::npos) {
if (output[s] == ')') {
tmp = (char*)mir_alloc(sizeof(char)* (output.substr(s2, s - s2).length() + 1));
mir_strcpy(tmp, output.substr(s2, s - s2).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(metaGetMostOnline(hContact), szGPGModuleName, "KeyComment", tmp);
mir_free(tmp);
s += 3;
s2 = output.find(">", s);
if (s != string::npos && s2 != string::npos) {
tmp = (char*)mir_alloc(sizeof(char)*(output.substr(s, s2 - s).length() + 1));
mir_strcpy(tmp, output.substr(s, s2 - s).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(metaGetMostOnline(hContact), szGPGModuleName, "KeyMainEmail", tmp);
mir_free(tmp);
}
}
else {
tmp = (char*)mir_alloc(sizeof(char)* (output.substr(s2, s - s2).length() + 1));
mir_strcpy(tmp, output.substr(s2, s - s2).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(metaGetMostOnline(hContact), szGPGModuleName, "KeyMainEmail", output.substr(s2, s - s2).c_str());
mir_free(tmp);
}
}
db_unset(metaGetMostOnline(hContact), szGPGModuleName, "bAlwatsTrust");
}
}
else {
char *tmp = nullptr;
string::size_type s = output.find("gpg: key ") + mir_strlen("gpg: key ");
string::size_type s2 = output.find(":", s);
db_set_s(hContact, szGPGModuleName, "KeyID", output.substr(s, s2 - s).c_str());
s = output.find("“", s2);
if (s == string::npos) {
s = output.find("\"", s2);
s += 1;
}
else
s += 3;
bool uncommon = false;
if ((s2 = output.find("(", s)) == string::npos) {
if ((s2 = output.find("<", s)) == string::npos) {
s2 = output.find("”", s);
uncommon = true;
}
}
else if (s2 > output.find("<", s))
s2 = output.find("<", s);
if (s != string::npos && s2 != string::npos) {
tmp = (char*)mir_alloc(sizeof(char)*(output.substr(s, s2 - s - (uncommon ? 1 : 0)).length() + 1));
mir_strcpy(tmp, output.substr(s, s2 - s - (uncommon ? 1 : 0)).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(hContact, szGPGModuleName, "KeyMainName", tmp);
mir_free(tmp);
}
if ((s = output.find(")", s2)) == string::npos)
s = output.find(">", s2);
else if (s > output.find(">", s2))
s = output.find(">", s2);
s2++;
if (s != string::npos && s2 != string::npos) {
if (output[s] == ')') {
tmp = (char*)mir_alloc(sizeof(char)* (output.substr(s2, s - s2).length() + 1));
mir_strcpy(tmp, output.substr(s2, s - s2).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(hContact, szGPGModuleName, "KeyComment", tmp);
mir_free(tmp);
s += 3;
s2 = output.find(">", s);
if (s != string::npos && s2 != string::npos) {
tmp = (char*)mir_alloc(sizeof(char)*(output.substr(s, s2 - s).length() + 1));
mir_strcpy(tmp, output.substr(s, s2 - s).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(hContact, szGPGModuleName, "KeyMainEmail", tmp);
mir_free(tmp);
}
}
else {
tmp = (char*)mir_alloc(sizeof(char)* (output.substr(s2, s - s2).length() + 1));
mir_strcpy(tmp, output.substr(s2, s - s2).c_str());
mir_utf8decode(tmp, nullptr);
db_set_s(hContact, szGPGModuleName, "KeyMainEmail", output.substr(s2, s - s2).c_str());
mir_free(tmp);
}
}
db_unset(hContact, szGPGModuleName, "bAlwatsTrust");
}
MessageBox(nullptr, toUTF16(output).c_str(), L"", MB_OK);
boost::filesystem::remove(tmp2);
}
|