-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmboxzilla.hpp
1140 lines (961 loc) · 42.5 KB
/
mboxzilla.hpp
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BSD 2-Clause License
Copyright (c) 2017-2023, Noël Martinon
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <fstream>
#include <iterator>
#include <vector>
#include <iostream>
#include <iomanip> // std::setw
#include <limits> // numeric_limits<int>::max()
#include <algorithm> // find_if
#include <functional> // std::not1
#include <time.h>
#include <zlib.h>
#include <curl/curl.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>
#include <openssl/sha.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
#include "common.hpp"
#include "mbox_parser.hpp"
#include "json.hpp"
#include "cxxopts.hpp"
#include "SimpleIni.h"
//#define ELPP_NO_DEFAULT_LOG_FILE -> specified on command line with gcc -D option
#include "easylogging++.h"
INITIALIZE_EASYLOGGINGPP
using json = nlohmann::json;
using namespace std;
json json_remotelist;
std::vector<unsigned char> aes_iv_token;
std::string sToken, ciphertext_token;
string ciphertext;
int nbUploadSuccess = 0, nbUploadError = 0;
string aes_key;
string host_url; // eg: "https://www.domain.net/backup";
int maxlogfiles = 5;
int timeout = 600;
long long speedlimit = 0;
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
/**
* sha256()
* Generate a 256 SHA hash from string
*/
std::string sha256(string inputstr) {
unsigned char hash[SHA256_DIGEST_LENGTH];
std::stringstream stream;
#if OPENSSL_VERSION_NUMBER >= 0x030000000
EVP_Digest((const unsigned char *)(inputstr.c_str()), inputstr.length(), hash, NULL, EVP_sha256(), NULL);
#else
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, inputstr.c_str(), inputstr.length());
SHA256_Final(hash, &sha256);
#endif
int i = 0;
for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
stream << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(hash[i]);
}
return stream.str();
}
//---------------------------------------------------------------------------------------------
/**
* AES_NormalizeKey()
* Make a key of exactly 32 bytes using a hash SHA-256
*/
std::string AES_NormalizeKey(std::string inputstr) {
std::string retVal;
retVal = sha256(inputstr);
retVal.resize(32);
return retVal;
}
//---------------------------------------------------------------------------------------------
/**
* url_encode()
* Encode a string to be used in a query part of a URL
*/
string url_encode(const std::string &value) {
ostringstream escaped;
escaped.fill('0');
escaped << hex;
for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << uppercase;
escaped << '%' << setw(2) << int((unsigned char) c);
escaped << nouppercase;
}
return escaped.str();
}
//---------------------------------------------------------------------------------------------
/**
* AES_Encrypt()
* Encrypt vector of char arrays to a vector of char arrays using AES_256_CBC encryption mode
*/
bool AES_Encrypt(string key, std::vector<unsigned char>& iv, std::vector<char>& ptext, std::string& ctext) {
try {
if (key.length()!=32)
throw std::runtime_error("AES-256-CBC key must be 256 bits");
// Create initialization vector
std::vector<unsigned char> randbytes(16);
RAND_bytes(&randbytes[0], 16);
iv = randbytes;
EVP_CIPHER_CTX *ctx;
if(!(ctx = EVP_CIPHER_CTX_new())) throw std::runtime_error("EVP_CIPHER_CTX_new failed");
int rc = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)key.c_str(), reinterpret_cast<unsigned char*> (&iv[0]));
if (rc != 1)
throw std::runtime_error("EVP_EncryptInit_ex failed");
// Recovered text expands upto BLOCK_SIZE
ctext.resize(ptext.size()+AES_BLOCK_SIZE);
int out_len1 = (int)ctext.size();
rc = EVP_EncryptUpdate(ctx, (unsigned char*)&ctext[0], &out_len1, (const unsigned char*)&ptext[0], (int)ptext.size());
if (rc != 1)
throw std::runtime_error("EVP_EncryptUpdate failed");
int out_len2 = (int)ctext.size() - out_len1;
rc = EVP_EncryptFinal_ex(ctx, (unsigned char*)&ctext[0]+out_len1, &out_len2);
if (rc != 1)
throw std::runtime_error("EVP_EncryptFinal_ex failed");
// Set cipher text size now that we know it
ctext.resize(out_len1 + out_len2);
return true;
}
catch (std::runtime_error &e) {
throw std::runtime_error(e.what ());
return false;
}
}
//---------------------------------------------------------------------------------------------
/**
* AES_Encrypt()
* Encrypt vector of char arrays to a vector of char arrays using AES_256_CBC encryption mode
*/
bool AES_Encrypt(string key, std::vector<unsigned char>& iv, std::vector<char>& ptext, std::vector<char>& ctext) {
try {
if (key.length()!=32)
throw std::runtime_error("AES-256-CBC key must be 256 bits");
// Create initialization vector
std::vector<unsigned char> randbytes(16);
RAND_bytes(&randbytes[0], 16);
iv = randbytes;
EVP_CIPHER_CTX *ctx;
if(!(ctx = EVP_CIPHER_CTX_new())) throw std::runtime_error("EVP_CIPHER_CTX_new failed");
int rc = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)key.c_str(), reinterpret_cast<unsigned char*> (&iv[0]));
if (rc != 1)
throw std::runtime_error("EVP_EncryptInit_ex failed");
// Recovered text expands upto BLOCK_SIZE
ctext.resize(ptext.size()+AES_BLOCK_SIZE);
int out_len1 = (int)ctext.size();
rc = EVP_EncryptUpdate(ctx, (unsigned char*)&ctext[0], &out_len1, (const unsigned char*)&ptext[0], (int)ptext.size());
if (rc != 1)
throw std::runtime_error("EVP_EncryptUpdate failed");
int out_len2 = (int)ctext.size() - out_len1;
rc = EVP_EncryptFinal_ex(ctx, (unsigned char*)&ctext[0]+out_len1, &out_len2);
if (rc != 1)
throw std::runtime_error("EVP_EncryptFinal_ex failed");
// Set cipher text size now that we know it
ctext.resize(out_len1 + out_len2);
return true;
}
catch (std::runtime_error &e) {
throw std::runtime_error(e.what ());
return false;
}
}
//---------------------------------------------------------------------------------------------
/**
* AES_Decrypt()
* Decrypt string to a STL string using AES_256_CBC encryption mode
*/
void AES_Decrypt(string key, std::vector<unsigned char>& iv, const std::string& ctext, std::string& rtext) {
EVP_CIPHER_CTX *ctx;
if(!(ctx = EVP_CIPHER_CTX_new())) throw std::runtime_error("EVP_CIPHER_CTX_new failed");
int rc = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)key.c_str(), reinterpret_cast<unsigned char*> (&iv[0]));
if (rc != 1)
throw std::runtime_error("EVP_DecryptInit_ex failed");
// Recovered text contracts upto BLOCK_SIZE
rtext.resize(ctext.size());
int out_len1 = (int)rtext.size();
rc = EVP_DecryptUpdate(ctx, (unsigned char*)&rtext[0], &out_len1, (const unsigned char*)&ctext[0], (int)ctext.size());
if (rc != 1)
throw std::runtime_error("EVP_DecryptUpdate failed");
int out_len2 = (int)rtext.size() - out_len1;
rc = EVP_DecryptFinal_ex(ctx, (unsigned char*)&rtext[0]+out_len1, &out_len2);
if (rc != 1)
throw std::runtime_error("EVP_DecryptFinal_ex failed");
// Set recovered text size now that we know it
rtext.resize(out_len1 + out_len2);
}
//---------------------------------------------------------------------------------------------
/**
* AES_Decrypt()
* Decrypt string to a vector of char arrays using AES_256_CBC encryption mode
*/
void AES_Decrypt(string key, std::vector<unsigned char>& iv, std::string& ctext, std::vector<char>& rtext) {
EVP_CIPHER_CTX *ctx;
if(!(ctx = EVP_CIPHER_CTX_new())) throw std::runtime_error("EVP_CIPHER_CTX_new failed");
int rc = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)key.c_str(), reinterpret_cast<unsigned char*> (&iv[0]));
if (rc != 1)
throw std::runtime_error("EVP_DecryptInit_ex failed");
// Recovered text contracts upto BLOCK_SIZE
rtext.resize(ctext.size());
int out_len1 = (int)rtext.size();
rc = EVP_DecryptUpdate(ctx, (unsigned char*)&rtext[0], &out_len1, (const unsigned char*)&ctext[0], (int)ctext.size());
if (rc != 1)
throw std::runtime_error("EVP_DecryptUpdate failed");
int out_len2 = (int)rtext.size() - out_len1;
rc = EVP_DecryptFinal_ex(ctx, (unsigned char*)&rtext[0]+out_len1, &out_len2);
if (rc != 1)
throw std::runtime_error("EVP_DecryptFinal_ex failed");
// Set recovered text size now that we know it
rtext.resize(out_len1 + out_len2);
}
//---------------------------------------------------------------------------------------------
/**
* base64Encode()
* Encodes the given data with base64
*/
std::string base64Encode(const std::string &data, size_t len=-1) {
BIO *bmem, *b64, *bcontainer;
BUF_MEM *bptr;
std::string encoded_data;
if (len == (size_t)-1) len = data.size();
b64 = BIO_new(BIO_f_base64());
// we don't want newlines
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new(BIO_s_mem());
bcontainer = BIO_push(b64, bmem);
BIO_write(bcontainer, (void*)data.c_str(), len);
(void) BIO_flush(bcontainer);
BIO_get_mem_ptr(bcontainer, &bptr);
char *buf = new char[bptr->length];
memcpy(buf, bptr->data, bptr->length);
encoded_data.assign(buf, bptr->length);
delete [] buf;
BIO_free_all(bcontainer);
return encoded_data;
}
//---------------------------------------------------------------------------------------------
/**
* base64Decode()
* Decodes data encoded with MIME base64
*/
std::string base64Decode(const std::string &data) {
BIO *bmem, *b64, *bcontainer;
char *buf = new char[data.size()];
int decoded_len = 0;
std::string decoded_data;
// create a memory buffer containing base64 encoded data
bmem = BIO_new_mem_buf((void*)data.c_str(), data.size());
//create a base64 filter
b64 = BIO_new(BIO_f_base64());
// we don't want newlines
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
// push a Base64 filter so that reading from buffer decodes it
bcontainer = BIO_push(b64, bmem);
decoded_len = BIO_read(bcontainer, (void*)buf, data.size());
BIO_free_all(bcontainer);
decoded_data.assign(buf, decoded_len);
delete [] buf;
return decoded_data;
}
//---------------------------------------------------------------------------------------------
/**
* Parse_remote_log()
* Parse the response of remote server to logging process
*/
void Parse_remote_log(string logmsg) {
if (logmsg.empty()) return;
if (logmsg.find("INFO#") == 0){ LOG(INFO) << logmsg.substr(5); }
else if (logmsg.find("WARNING#") == 0){ LOG(WARNING) << logmsg.substr(8); }
else if (logmsg.find("ERROR#") == 0){ throw std::runtime_error(logmsg.substr(6)); }
else if (logmsg.find("VERBOSE1#") == 0){ VLOG(1) << logmsg.substr(9); }
else if (logmsg.find("VERBOSE2#") == 0){ VLOG(2) << logmsg.substr(9); }
else if (logmsg.find("VERBOSE3#") == 0){ VLOG(3) << logmsg.substr(9); }
//else LOG(INFO) << logmsg;
}
//---------------------------------------------------------------------------------------------
/**
* WriteCallback_toBuffer()
* CURL callback for writing received data to buffer defined in CURLOPT_WRITEDATA option
*/
size_t WriteCallback_toBuffer(void *buffer, size_t size, size_t nmemb, void *userp) {
((std::string*)userp)->append((char*)buffer, size * nmemb);
return size * nmemb;
}
//---------------------------------------------------------------------------------------------
/**
* WriteCallback()
* CURL callback for logging
*/
size_t WriteCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
std::vector<std::string> vSplit;
string sBuf((char*)buffer);
sBuf.resize(size*nmemb);
regex e("[\r\n]+|[^\r\n]+");
//[.,;-]|[^.,;-]
sregex_iterator rit(sBuf.begin(), sBuf.end(), e), rend;
while(rit != rend)
{
if (!(rit->str()).empty() && rit->str() !="\r" && rit->str() !="\n") {
vSplit.push_back(rit->str().c_str());
Parse_remote_log(rit->str());
}
++rit;
}
if (vSplit.size()==0){
Parse_remote_log(sBuf);
}
return size * nmemb;
}
//---------------------------------------------------------------------------------------------
/**
* Remote_IsAvailable()
* Verify that remote host accept requests to upload eml files
*/
bool Remote_IsAvailable() {
CURL *curl;
CURLcode res;
bool ret = false;
std::string readBuffer;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
// initialize token
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::stringstream token;
token << std::put_time(&tm, "%Y%m%d_%H%M%S");
sToken = token.str();
std::vector<char> vToken(sToken.begin(), sToken.end());
if (!AES_Encrypt(aes_key, aes_iv_token, vToken, ciphertext_token))
return false;
std::string aes_iv_token_str = base64Encode(std::string(aes_iv_token.begin(), aes_iv_token.end()),16);
string ciphertext_token_b64 = base64Encode(ciphertext_token, ciphertext_token.size());
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
// initialize custom header list (stating that Expect: 100-continue is not wanted
headerlist = curl_slist_append(headerlist, buf);
if (curl) {
curl_mime *multipart = curl_mime_init(curl);
curl_mimepart *part = curl_mime_addpart(multipart);
curl_mime_name(part, "token");
curl_mime_data(part, ciphertext_token_b64.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(multipart);
curl_mime_name(part, "token_iv");
curl_mime_data(part, aes_iv_token_str.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(multipart);
curl_mime_name(part, "check");
curl_mime_data(part, "HELLO", CURL_ZERO_TERMINATED);
curl_easy_setopt(curl, CURLOPT_URL, host_url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, multipart);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback_toBuffer); // Disable standard output
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
res = curl_easy_perform(curl);
/* Check for errors */
if (res == CURLE_OK) {
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK && readBuffer == "READY")
{
ret = true;
}
else
{
ret = false;
}
}
curl_easy_cleanup(curl);
curl_mime_free(multipart);
curl_slist_free_all(headerlist);
}
if (!curl) throw std::runtime_error("curl_easy_init() failed\n");
if (res!=0) throw std::runtime_error(curl_easy_strerror(res));
return ret;
}
//---------------------------------------------------------------------------------------------
/**
* Remote_GetList()
* Get files stored in remote directory. Use to find out if a file must be uploaded.
*/
bool Remote_GetList(json &j, const std::string outputdir) {
CURL *curl;
CURLcode res;
bool ret = false;
j.clear();
std::string readBuffer;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
// initialize token
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::stringstream token;
token << std::put_time(&tm, "%Y%m%d_%H%M%S");
sToken = token.str();
std::vector<char> vToken(sToken.begin(), sToken.end());
if (!AES_Encrypt(aes_key, aes_iv_token, vToken, ciphertext_token))
return false;
std::string aes_iv_token_str = base64Encode(std::string(aes_iv_token.begin(), aes_iv_token.end()),16);
string ciphertext_token_b64 = base64Encode(ciphertext_token, ciphertext_token.size());
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
// initialize custom header list (stating that Expect: 100-continue is not wanted
headerlist = curl_slist_append(headerlist, buf);
if (curl) {
curl_mime *multipart = curl_mime_init(curl);
curl_mimepart *part = curl_mime_addpart(multipart);
curl_mime_name(part, "token");
curl_mime_data(part, ciphertext_token_b64.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(multipart);
curl_mime_name(part, "token_iv");
curl_mime_data(part, aes_iv_token_str.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(multipart);
curl_mime_name(part, "get_filelist");
curl_mime_data(part, outputdir.c_str(), CURL_ZERO_TERMINATED);
curl_easy_setopt(curl, CURLOPT_URL, host_url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, multipart);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback_toBuffer); // Disable standard output
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
res = curl_easy_perform(curl);
/* Check for errors */
if (res == CURLE_OK) {
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK)
{
j = json::parse(decompress_gzip(readBuffer));
ret = true;
}
else
{
ret = false;
}
}
curl_easy_cleanup(curl);
curl_mime_free(multipart);
curl_slist_free_all(headerlist);
}
if (!curl) throw std::runtime_error("curl_easy_init() failed\n");
if (res!=0) throw std::runtime_error(curl_easy_strerror(res));
return ret;
}
//---------------------------------------------------------------------------------------------
/**
* Remote_SendEml()
* Send eml or eml.gz to remote host
*/
bool Remote_SendEml(string fname, std::vector<char> eml) {
CURL *curl;
CURLcode res;
double speed_upload, total_time;
bool ret = false;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
// initialize ciphertext (eml) and iv
std::vector<unsigned char> aes_iv;
if (!AES_Encrypt(aes_key, aes_iv, eml, ciphertext))
return false;
std::string aes_iv_str = base64Encode(std::string(aes_iv.begin(), aes_iv.end()));
// initialize token
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::stringstream token;
token << std::put_time(&tm, "%Y%m%d_%H%M%S");
std::string sToken = token.str();
std::vector<char> vToken(sToken.begin(), sToken.end());
if (!AES_Encrypt(aes_key, aes_iv_token, vToken, ciphertext_token))
return false;
VLOG(3) << "Uploading to " << fname << " (" << bytes_convert(eml.size()) << ")";
std::string aes_iv_token_str = base64Encode(std::string(aes_iv_token.begin(), aes_iv_token.end()));
string ciphertext_token_b64 = base64Encode(ciphertext_token, ciphertext_token.size());
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
// initialize custom header list (stating that Expect: 100-continue is not wanted
headerlist = curl_slist_append(headerlist, buf);
if (curl) {
curl_mime *multipart = curl_mime_init(curl);
curl_mimepart *part = curl_mime_addpart(multipart);
curl_mime_name(part, "token");
curl_mime_data(part, ciphertext_token_b64.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(multipart);
curl_mime_name(part, "token_iv");
curl_mime_data(part, aes_iv_token_str.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(multipart);
curl_mime_name(part, "iv");
curl_mime_data(part, aes_iv_str.c_str(), CURL_ZERO_TERMINATED);
fname = base64Encode(fname); // b64 encoded because COPYNAME strip slash
part = curl_mime_addpart(multipart);
curl_mime_name(part, "fileToUpload");
curl_mime_data(part, ciphertext.data(), (curl_off_t) ciphertext.size());
curl_mime_filename(part, fname.c_str());
struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "content-type: multipart/form-data");
curl_mime_headers(part, headers, false);
curl_easy_setopt(curl, CURLOPT_URL, host_url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, multipart);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); // Disable standard output
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE, speedlimit);
res = curl_easy_perform(curl);
/* Check for errors */
if (res == CURLE_OK) {
/* now extract transfer info */
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD_T, &speed_upload);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
VLOG(3) << "Speed was " << bytes_convert(speed_upload) << "/s during " << floor(total_time*100)/100 << " seconds";
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK)
{
//Succeeded
ret = true;
}
else
{
//Failed
ret = false;
}
}
curl_easy_cleanup(curl);
curl_mime_free(multipart);
curl_slist_free_all(headerlist);
}
if (!curl) throw std::runtime_error("curl_easy_init() failed\n");
if (res!=0) throw std::runtime_error(curl_easy_strerror(res));
return ret;
}
//---------------------------------------------------------------------------------------------
/**
* Remote_SendSyncList()
* Send list to server in order to keep only emails or directories listed
*/
bool Remote_SendSyncList(const std::string ListName, std::string SyncDir, std::vector<std::string> vList) {
json j_sync(vList);
string sSync = j_sync.dump();
sSync = compress_gzip(sSync);
sSync = base64Encode(sSync,sSync.length());
CURL *curl;
CURLcode res;
bool ret = false;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
// initialize token
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::stringstream token;
token << std::put_time(&tm, "%Y%m%d_%H%M%S");
std::string sToken = token.str();
std::vector<char> vToken(sToken.begin(), sToken.end());
if (!AES_Encrypt(aes_key, aes_iv_token, vToken, ciphertext_token))
return false;
std::string aes_iv_token_str = base64Encode(std::string(aes_iv_token.begin(), aes_iv_token.end()));
string ciphertext_token_b64 = base64Encode(ciphertext_token, ciphertext_token.size());
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
// initialize custom header list (stating that Expect: 100-continue is not wanted
headerlist = curl_slist_append(headerlist, buf);
if (curl) {
curl_mime *multipart = curl_mime_init(curl);
curl_mimepart *part = curl_mime_addpart(multipart);
curl_mime_name(part, "token");
curl_mime_data(part, ciphertext_token_b64.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(multipart);
curl_mime_name(part, "token_iv");
curl_mime_data(part, aes_iv_token_str.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(multipart);
curl_mime_name(part, ListName.c_str());
curl_mime_data(part, sSync.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(multipart);
curl_mime_name(part, "sync_directory");
curl_mime_data(part, SyncDir.c_str(), CURL_ZERO_TERMINATED);
struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "content-type: multipart/form-data");
curl_mime_headers(part, headers, false);
curl_easy_setopt(curl, CURLOPT_URL, host_url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, multipart);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); // Disable standard output
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
res = curl_easy_perform(curl);
/* Check for errors */
if (res == CURLE_OK) {
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK)
{
ret = true;
}
else
{
ret = false;
}
}
curl_easy_cleanup(curl);
curl_mime_free(multipart);
curl_slist_free_all(headers);
curl_slist_free_all(headerlist);
}
if (!curl) throw std::runtime_error("curl_easy_init() failed\n");
if (res!=0) throw std::runtime_error(curl_easy_strerror(res));
return ret;
}
//---------------------------------------------------------------------------------------------
/**
* callbackEMLvalid()
* Callback function to start callbackEML() when current file is not
* in the remote directory content
*/
bool callbackEMLvalid(string dirname, string filename) {
if (std::find(std::begin(json_remotelist), std::end(json_remotelist),
filename) == std::end(json_remotelist))
return true;
return false;
}
//---------------------------------------------------------------------------------------------
/**
** callbackEML()
** Callback function to start Remote_SendEml()
*/
void callbackEML(string dirname, string filename, std::vector<char> eml) {
string fullpathfile = dirname + filename;
if (Remote_SendEml(fullpathfile, eml)) nbUploadSuccess++;
else nbUploadError++;
}
//---------------------------------------------------------------------------------------------
/**
** callbackLOG()
** Callback function for logging
*/
void callbackLOG(string logtype, string logmsg) {
if (logtype=="INFO") {LOG(INFO) << logmsg.data();}
else if (logtype=="ERROR") {LOG(ERROR) << logmsg.data();}
else if (logtype=="WARNING") {LOG(WARNING) << logmsg.data();}
else if (logtype=="VERBOSE1") {VLOG(1) << logmsg.data();}
else if (logtype=="VERBOSE2") {VLOG(2) << logmsg.data();}
else if (logtype=="VERBOSE3") {VLOG(3) << logmsg.data();}
}
//---------------------------------------------------------------------------------------------
/**
** rolloutHandler()
** Callback function used to rotate log files
*/
void rolloutHandler(const char* filename, std::size_t size) {
std::stringstream ssSrc, ssDst;
string fileName = filename;
size_t pos = fileName.find(".");
string extractName = (string::npos == pos)? fileName : fileName.substr(0, pos);
for (int i = maxlogfiles-1; i>=1; i--) {
ssSrc << extractName << ".log." << i-1;
ssDst << extractName << ".log." << i;
if (FileExists(ssSrc.str())) {
if (FileExists(ssDst.str())) std::remove( ssDst.str().c_str() );
rename(ssSrc.str().c_str(), ssDst.str().c_str());
}
ssSrc.str(std::string()); ssSrc.clear();
ssDst.str(std::string()); ssDst.clear();
}
if (maxlogfiles>1) {
ssDst << extractName << ".log.1";
rename(filename, ssDst.str().c_str());
}
}
//---------------------------------------------------------------------------------------------
/**
* IsMboxFile()
* Used to list thunderbird mbox
* Returns true if source file is mbox type
*/
bool IsMboxFile(std::string filename) {
char buffer[6] = "0";
std::ifstream mboxfile( filename, std::ios::binary | std::ios::ate);
if (mboxfile.fail() || mboxfile.tellg()<5) return false;
mboxfile.seekg (0, mboxfile.beg);
mboxfile.read(buffer, 5);
mboxfile.close();
if (!strcmp(buffer, "From ")) return true;
return false;
}
//---------------------------------------------------------------------------------------------
/**
* GetRegexVector()
* Search a regex into string list and extract substring
* Returns index of the line where the element has been found
* else return -1 if not found
*/
int GetRegexVector (std::vector<std::string>& vString, const std::string& sRegex, std::vector<std::string>& vResult, int iVectorIndex=0) {
std::regex rgx(sRegex);
std::smatch match;
int index=-1;
vResult.clear();
for(auto it:vString) {
if(++index >= iVectorIndex && std::regex_search(it, match, rgx) && match.size() > 1) {
for (auto iter: match) {
vResult.push_back(iter);
}
return index;
}
}
return -1;
}
//---------------------------------------------------------------------------------------------
/**
* ListThunderbirdMbox()
* List the mbox files found in all subfolders of a specified directory
*/
void ListThunderbirdMbox(std::map<std::string, std::vector<std::string>>& map, std::string destination_path, std::string directory, vector<string>& vMboxExcluded) {
std::vector<string> vListDirectories;
vListDirectories.push_back(directory);
ListAllSubDirectories(vListDirectories, directory);
for (auto itDir:vListDirectories) {
std::vector<string> vList;
ListDirectoryContents(vList, itDir, true, false);
for (auto itFile:vList) {
string mbx = itDir+"/"+itFile;
if (IsMboxFile(mbx)) {
// Extract mbox name only (without account dir) and test if excluded
mbx = mbx.data()+directory.length()+1;
bool bIgnore = false;
if (vMboxExcluded.size()) {
for (auto it:vMboxExcluded) {
std::regex rgx(it, regex_constants::icase);
if (std::regex_match(mbx, rgx)) {
LOG(WARNING) << "-> Ignore Mbox file \""+itDir+"/"+itFile+"\"";
bIgnore = true;
break;
}
}
}
if (!bIgnore)
map[destination_path+"|"+directory].push_back(itDir+"/"+itFile);
}
}
}
sort(map[destination_path+"|"+directory].begin(),map[destination_path+"|"+directory].end(),NoCaseLess);
}
//---------------------------------------------------------------------------------------------
/**
* GetThunderbirdMbox()
* Automaticaly retrieve all mbox files of Mozilla Thunderbird email program
* for the current user or username if specified.
* The optional "email_domain" argument is used to filter email accounts that are found.
* Mbox files can be excluded by setting a regex list separated by comma.
* Returns the list of mbox files
*/
std::map<std::string, std::vector<std::string>> GetThunderbirdMbox(bool b_getlocalfolders=false, std::string username="", std::string email_domain="", std::string source_exclude="") {
std::string userpath, tbpath, user="";
// Search profile path
#if defined(__linux__) || defined(__APPLE__)
userpath = path_dusting(getenv("HOME"));
#elif _WIN32
userpath = path_dusting(getenv("USERPROFILE"));
#endif
if (!DirectoryExists(userpath))
return std::map<std::string, std::vector<std::string>>();
// Get the user name and the global users directory path
size_t pos = userpath.find_last_of("/");
if (pos != string::npos) {
if (!username.empty())
userpath = userpath.substr(0, pos+1) + username;
user = userpath.substr(pos+1);
}
else return std::map<std::string, std::vector<std::string>>();
// Search thunderbird path
#if defined(__linux__) || defined(__APPLE__)
tbpath = userpath+"/.thunderbird";
#elif _WIN32
// Path for Windows >= 7
tbpath = userpath+"/AppData/Roaming/Thunderbird";
if (!DirectoryExists(tbpath)) {
// Path for Windows < 7
tbpath = userpath+"/Application Data/Thunderbird";
}
#endif
if (!DirectoryExists(tbpath)) return std::map<std::string, std::vector<std::string>>();
// Initialize mbox excluded regex list
vector<string> vMboxExcluded;
if (!source_exclude.empty()) {
split(source_exclude , ',', vMboxExcluded, false);
// If no comma found then excluded list is the full string
if (vMboxExcluded.empty())
vMboxExcluded.push_back(source_exclude);
}
// Search thunderbird profiles
CSimpleIniA ini;
std::vector<string> vProfiles;
string profile = tbpath+"/profiles.ini";
ini.SetUnicode();
ini.Reset();
SI_Error rc = ini.LoadFile(profile.c_str());
if (rc < 0) {
LOG(ERROR) << "-> No Mozilla Thunderbird profiles.ini file found";
return std::map<std::string, std::vector<std::string>>();
}
else {
CSimpleIniA::TNamesDepend sections, keys;
ini.GetAllSections(sections);
CSimpleIniA::TNamesDepend::const_iterator i;
for (i = sections.begin(); i != sections.end(); ++i) {
std::regex rgx("^Profile.*", regex_constants::icase);
if (std::regex_match(i->pItem, rgx)) {
const char * pszValue = ini.GetValue(i->pItem, "path", NULL);
if (pszValue) {
vProfiles.push_back(pszValue);
}
}
}
}
if (vProfiles.size()==0) {
LOG(ERROR) << "-> No Mozilla Thunderbird profiles found";