-
Notifications
You must be signed in to change notification settings - Fork 1
/
imap_sec.py
1259 lines (1081 loc) · 48 KB
/
imap_sec.py
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
import imaplib, time
import re
from mailbox import Message
import json
import sys
import traceback
import threading
import hashlib
import random
import pickle
import scrypt # sudo -H pip install scrypt
from crypticle import Crypticle
import imap_scheduler
class IMAP4(imaplib.IMAP4):
pass
# override/add here
def __init__(self):
raise NotImplementedError("Do not use this class")
CRYPTOBLOBS = "CRYPTOBLOBS"
INDEX = "INDEX"
INBOX = "INBOX"
ENCRYPTED = "ENCRYPTED"
SEQUENCE_NUMBER = "SEQUENCE_NUMBER"
FAKE_MESSAGES = "FAKE_MESSAGES"
SMTorP = "SMTorP"
NEXT_UID = "NEXT_UID"
LOCK = "LOCK"
GMAIL_ALL_MAIL = "[Gmail]/All Mail"
OK = "OK"
parent = imaplib.IMAP4_SSL
CRLF = imaplib.CRLF
IMAP4_SSL_PORT = imaplib.IMAP4_SSL_PORT
TOTAL_LOCK_RECOVERY_TIME = 60.0 # this may need to be dependent on mix num
LOCK_RETRY_DELAY = 2
MIX_NUMBER = 5
# Some flags that are helpful for debugging
DEBUG_IMAP_FROM_GMAIL = False
DEBUG_IMAP_FROM_SMTORP = False
DEBUG_SCHEDULER = False
DEBUG_EXTRAS = False
DEBUG_METHODS_ARGS_RETURNS = True
PRINT_INDEX = False
DEBUG_LOCK = False
DEBUG_RESPONSES = False
DEBUG_TURN_OFF_INDEX_REFETCH = True
NOOP_ENCRYPTION = False
def hash_of_message_as_string(message_as_string):
if DEBUG_SCHEDULER or DEBUG_IMAP_FROM_GMAIL:
print "in hash_of_message_as_string"
hashed = hashlib.md5(message_as_string)
if DEBUG_SCHEDULER or DEBUG_IMAP_FROM_GMAIL:
print "hashed", hashed
hexed = hashed.hexdigest()
if DEBUG_SCHEDULER or DEBUG_IMAP_FROM_GMAIL:
print "hexed", hexed
return hexed
def get_new_unique_subject():
return '%032x' % random.randrange(16**32)
def fake_message_with_random_contents_as_string():
message = Message()
message['Subject'] = "garbage"
message['From'] = "[email protected]"
message['To'] = "[email protected]"
message.set_payload(str(random.randint(0,10000000000)))
return str(message)
def replace_item_in_tuple(d, replacement, x, y):
if DEBUG_METHODS_ARGS_RETURNS:
print "replace_item_in_tuple"
ld = list(d)
ldd = list(d[x])
ldd[y] = replacement
ld[x] = tuple(ldd)
ldt = tuple(ld)
if DEBUG_METHODS_ARGS_RETURNS:
print "replaced item_in_tuple"
return ldt
def delete_item_from_tuple(d, x, y):
ld = list(d)
ldd = list(d[x])
ldd.pop(y)
ld[x] = tuple(ldd)
ldt = tuple(ld)
return ldt
# we need to keep track of imap state
class IMAP4_SSL(imaplib.IMAP4_SSL):
pass
# if local store file isn't specified, we can't guarantee that the
# index hasn't been rolled back
# if local send/delete queue file isn't specified and we're shut down before
# all messages that came in from SMTorP are sent, we can't resend/delete
# when the next session starts.
# please specify a passphrase. leaving this to the caller.
def __init__(self, passphrase, host='',port=imaplib.IMAP4_SSL_PORT,
keyfile=None, certfile=None, timer_tick=120,
local_store_file=None, local_send_queue_file=None,
local_delete_queue_file=None):
if DEBUG_SCHEDULER or DEBUG_IMAP_FROM_GMAIL \
or DEBUG_IMAP_FROM_SMTORP or DEBUG_EXTRAS:
print "initializing IMAP4_SSL(imap_sec)", host, port, passphrase
print "local_store_file", local_store_file
print "local_send_queue_file", local_send_queue_file
print "local_delete_queue_file", local_delete_queue_file
imaplib.IMAP4_SSL.__init__(self, host, port, keyfile, certfile)
self.mapping = None # this will be loaded in load_cryptoblobs
self.selected_mailbox = INBOX
# load locally stored index number
self.local_store_file = local_store_file
self.load_last_index_number_from_disk()
self.rollback_detected = False
self._scheduler = imap_scheduler.ImapScheduler()
self.passphrase = passphrase
# we can't derive the key until we've pulled down the index
# to get the salt
self.random_salt = None
self.index_subject = None
self.encryption_key = None
self.have_index_lock = False
self.local_send_queue_file = local_send_queue_file
self.load_send_queue_from_disk()
self.local_delete_queue_file = local_delete_queue_file
self.load_delete_queue_from_disk()
self.timer_tick = timer_tick
if DEBUG_SCHEDULER or DEBUG_EXTRAS:
print "made it through initialization of IMAP4_SSL", self
# lazily construct this, because we need to have called down
# the index or created a new one to get the salt
def key_derived_from_passphrase(self):
if DEBUG_EXTRAS:
print "key_derived_from_passphrase", self.passphrase, self.random_salt
assert self.random_salt
if not self.encryption_key:
if DEBUG_EXTRAS:
print "creating self.encryption_key"
self.encryption_key = scrypt.hash(self.passphrase, self.random_salt)
return self.encryption_key
def uid_for_constructed_message(self, message_as_string, folder):
if folder == FAKE_MESSAGES:
a = hash_of_message_as_string(message_as_string)
s = '11111111111111111111111111111111'
return str(int(int(a,16)&int(s,2)))
if folder == SMTorP:
# self.fetch_and_load_index() # theoretically, we should call
# this. but in this implementation, it's extra.
# actually, it would be bad to call this twice because
# the double index call would be a signal that this is a
# real message
if not NEXT_UID in self.mapping:
self.mapping[NEXT_UID] = {}
if not folder in self.mapping[NEXT_UID]:
self.mapping[NEXT_UID][folder] = 1
out = self.mapping[NEXT_UID][folder]
self.mapping[NEXT_UID][folder] += 1
return str(out)
# it shouldn't be anything else
def load_last_index_number_from_disk(self):
if self.local_store_file:
try:
self.last_index_number = pickle.load(open(self.local_store_file, "rb"))
if DEBUG_EXTRAS:
print "loaded index number", self.last_index_number
except:
self.last_index_number = 0
else:
self.last_index_number = 0
def load_send_queue_from_disk(self):
if self.local_send_queue_file:
try:
self.send_queue = pickle.load(open(self.local_send_queue_file, "rb"))
except:
self.send_queue = []
else:
self.send_queue = []
def load_delete_queue_from_disk(self):
if self.local_delete_queue_file:
try:
self.delete_queue = pickle.load(open(self.local_delete_queue_file, "rb"))
except:
self.delete_queue = []
else:
self.delete_queue = []
def save_send_queue_to_disk(self):
if self.local_send_queue_file != None:
if DEBUG_SCHEDULER:
print "trying to save send queue to", self.local_send_queue_file
pickle.dump(self.send_queue, open(self.local_send_queue_file, "wb"))
def save_delete_queue_to_disk(self):
if self.local_delete_queue_file != None:
if DEBUG_SCHEDULER:
print "trying to save delete queue to", self.local_delete_queue_file
pickle.dump(self.delete_queue, open(self.local_delete_queue_file, "wb"))
def quote(self, item):
return '"'+item+'"'
def find_index_uid_in_folder(self, folder):
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(folder))
if DEBUG_SCHEDULER or DEBUG_EXTRAS:
print "find_index_uid_in_folder folder", folder
typ, data = imaplib.IMAP4_SSL.uid(self, 'SEARCH', None, "SUBJECT", self.quote(INDEX))
if DEBUG_SCHEDULER or DEBUG_EXTRAS:
print "find_index_uid_in_folder", typ, data
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(self.selected_mailbox))
if DEBUG_IMAP_FROM_GMAIL or DEBUG_EXTRAS:
print "found index id", data
return data[0].split()[0]
def get_list_of_uids_given_subject_line(self, subject_line, folder):
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(folder))
typ, data = imaplib.IMAP4_SSL.uid(self, 'SEARCH', None, "SUBJECT", self.quote(subject_line))
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(self.selected_mailbox))
return data[0].split()
def get_message_uid_given_hash_in_subject(self, hash_of_message):
if DEBUG_METHODS_ARGS_RETURNS:
print "get_message_uid_given_hash_in_subject", hash_of_message
return self.get_list_of_uids_given_subject_line(hash_of_message, CRYPTOBLOBS)[0]
def delete_message_from_actual_folder_uid(self, index_id, folder):
if DEBUG_EXTRAS:
print "delete_message_from_actual_folder_uid", index_id, folder
a, b = imaplib.IMAP4_SSL.select(self, mailbox=self.quote(folder))
if DEBUG_IMAP_FROM_GMAIL:
print "selected folder", a, b
typ, data = imaplib.IMAP4_SSL.uid(self, "STORE", str(index_id), "+FLAGS", r'(\Deleted)')
if DEBUG_IMAP_FROM_GMAIL:
print "delete response", typ, data
typ, response = imaplib.IMAP4_SSL.expunge(self)
if DEBUG_IMAP_FROM_GMAIL:
print "expunge response", typ, response
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(self.selected_mailbox))
return typ, response
def delete_index(self):
index_id = self.find_index_uid_in_folder(CRYPTOBLOBS)
self.delete_message_from_actual_folder_uid(index_id, CRYPTOBLOBS)
self.delete_index_from_all_mail()
# this method checks and sets the rollback_detected flag
# if rollback is detected, it returns the current index
def unpack_index_contents(self, decrypted_index_body_text):
table = json.loads(decrypted_index_body_text)
if table[SEQUENCE_NUMBER] < self.last_index_number:
if DEBUG_IMAP_FROM_GMAIL:
print "rollback_detected"
self.rollback_detected = True
return self.mapping
return table
def decrypt_string(self, data):
# decrypt string, check that it decrypted correctly
if NOOP_ENCRYPTION:
return data
else:
crypt = Crypticle(self.key_derived_from_passphrase())
if DEBUG_EXTRAS:
print "crypt created perfectly well", crypt
out = crypt.loads(data)
return out
# returns None if it failed
def encrypt_string(self, data):
# encrypt string, add auth
if NOOP_ENCRYPTION:
return data
else:
crypt = Crypticle(self.key_derived_from_passphrase())
return crypt.dumps(data)
def extract_salt_from_subject(self, subject):
return subject.split()[2]
def save_salt_and_related_information(self, random_salt):
if DEBUG_EXTRAS:
print "save_salt_and_related_information", random_salt
self.random_salt = random_salt
self.index_subject = INDEX + " " + self.random_salt
if DEBUG_EXTRAS:
print "saved salt and related information."
def fetch_and_load_index(self, refetch_index=True):
# if DEBUG_EXTRAS or DEBUG_METHODS_ARGS_RETURNS:
# print "fetch_and_load_index"
if DEBUG_TURN_OFF_INDEX_REFETCH or not refetch_index:
if self.mapping:
if PRINT_INDEX:
print "Index:"
print self.mapping
return self.mapping
# 1. fetch index and index subject
index_id = self.find_index_uid_in_folder(CRYPTOBLOBS)
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(CRYPTOBLOBS))
# fetches the text of message with id index_id
typ, data = imaplib.IMAP4_SSL.uid(self, "FETCH", index_id,\
'(BODY[TEXT])')
# fetches the subject of message with id index_id
ok, subject_data = imaplib.IMAP4_SSL.uid(self, "FETCH", index_id,\
'(BODY[HEADER.FIELDS (SUBJECT)])')
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(self.selected_mailbox))
# 1.5. save salt and related information
if DEBUG_EXTRAS:
print "subject_data", subject_data
subject_line = subject_data[0][1].strip()
if DEBUG_EXTRAS:
print "stripped", subject_line
salt = self.extract_salt_from_subject(subject_line)
self.save_salt_and_related_information(salt)
# 2. decrypt or return None if it fails to decrypt
unloaded_data = data[0][1].strip()
decrypted = self.decrypt_string(unloaded_data)
if not decrypted:
if DEBUG_EXTRAS:
print "decryption failed"
return
if DEBUG_EXTRAS:
print "decryption succeeded"
# 3. unjsonify, check for rollback, and verify
unpacked_table = self.unpack_index_contents(decrypted)
if not unpacked_table:
return
# 4. replace current index and sequence number in memory
self.mapping = unpacked_table
self.last_index_number = unpacked_table[SEQUENCE_NUMBER]
if PRINT_INDEX:
print "Index:"
print self.mapping
# 5. save new sequence number to disk
self.save_index_sequence_number_to_disk()
def encrypt_and_append_message(self, message_contents, unique_subject):
encrypted_message = self.encrypt_string(message_contents)
self.append_encrypted_message(encrypted_message, unique_subject)
def append_encrypted_message(self, encrypted_message_body, hashed):
message = Message()
message['Subject'] = ENCRYPTED+" "+hashed
message['From'] = "[email protected]"
message['To'] = "[email protected]"
message.set_payload(str(encrypted_message_body))
typ, data = parent.append(self, CRYPTOBLOBS, None, None, str(message))
def decrypt_and_unpack_message(self, message_contents):
if DEBUG_METHODS_ARGS_RETURNS:
print "decrypt_and_unpack_message"
messageified = Message(message_contents)
old_message_str = messageified.get_payload()
a = self.decrypt_string(old_message_str)
if DEBUG_METHODS_ARGS_RETURNS:
print "decrypted and_unpacked message"
return a
# pushes up the index to the cryptoblobs folder
# doesn't matter which folder is selected
def append_index(self, table):
if DEBUG_EXTRAS:
print "append_index", self.index_subject
message = Message()
message['Subject'] = self.index_subject
message['From'] = "[email protected]"
message['To'] = "[email protected]"
encrypted_contents = self.encrypt_string(json.dumps(table))
message.set_payload(encrypted_contents)
str_message = str(message)
typ, data = imaplib.IMAP4_SSL.append(self, CRYPTOBLOBS, None, \
None, str_message)
if DEBUG_IMAP_FROM_GMAIL or DEBUG_EXTRAS:
print "append_index", typ, data
def append_lock(self):
message = Message()
message['Subject'] = LOCK
message['From'] = "[email protected]"
message['To'] = "[email protected]"
str_message = str(message)
typ, data = imaplib.IMAP4_SSL.append(self, CRYPTOBLOBS, None, \
None, str_message)
def save_index_sequence_number_to_disk(self):
if DEBUG_SCHEDULER:
print "save_index_sequence_number_to_disk"
if self.local_store_file != None:
if DEBUG_SCHEDULER:
print "trying to save index sequence number to", self.local_store_file
pickle_file = open(self.local_store_file, "wb")
if DEBUG_EXTRAS:
print "opened file", pickle_file
pickle.dump(self.mapping[SEQUENCE_NUMBER], pickle_file)
else:
if DEBUG_SCHEDULER:
print "no local store file"
def find_lock_uid_in_folder(self, folder):
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(folder))
if DEBUG_SCHEDULER or DEBUG_EXTRAS:
print "find_lock_uid_in_folder folder", folder
typ, data = imaplib.IMAP4_SSL.uid(self, 'SEARCH', None, "SUBJECT", self.quote(LOCK))
if DEBUG_SCHEDULER or DEBUG_EXTRAS:
print "find_lock_uid_in_folder", typ, data
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(self.selected_mailbox))
if DEBUG_IMAP_FROM_GMAIL or DEBUG_EXTRAS:
print "found lock id", data
if data[0] == '':
return None
return data[0].split()[0]
def acquire_index_lock(self):
if DEBUG_METHODS_ARGS_RETURNS:
print "acquire_index_lock"
if self.have_index_lock:
if DEBUG_EXTRAS:
print "already have index lock"
return
try_time = 0
timeout = TOTAL_LOCK_RECOVERY_TIME
while True:
if DEBUG_LOCK:
print "try_time", try_time, "out of", timeout
if DEBUG_EXTRAS:
print "trying to acquire index lock"
# find lock id
lock_id = self.find_lock_uid_in_folder(CRYPTOBLOBS)
if lock_id == None:
if DEBUG_SCHEDULER or DEBUG_LOCK:
print 'lock not in there'
try_time += LOCK_RETRY_DELAY
# if it's been too long
if try_time >= timeout:
# is the index there?
try:
prev_sequence_num = self.mapping[SEQUENCE_NUMBER]
self.fetch_and_load_index() # TODO: ensure that this excepts
# the index is there.
if DEBUG_LOCK:
print "the index is there"
# if the index sequence number hasn't changed
if prev_sequence_num == self.mapping[SEQUENCE_NUMBER]:
# just get the lock
if DEBUG_LOCK:
print "index number hasn't changed"
# make sure it's not in all mail
try:
self.delete_lock_from_all_mail()
except:
pass
self.have_index_lock = True
return
# if it has
else:
# double timeout (exponential backoff)
timeout = 2 * timeout
except:
# index isn't there. our turn!
# make sure index is also gone from all mail
try:
self.delete_index_from_all_mail()
except:
pass
# make sure it's not in all mail
try:
self.delete_lock_from_all_mail()
except:
pass
# put up the index
self.save_index_without_deleting()
# we have the lock now
self.have_index_lock = True
return
time.sleep(LOCK_RETRY_DELAY)
continue
# try to delete
# if we fail, we haven't acquired the lock
typ, data = self.delete_message_from_actual_folder_uid(lock_id, CRYPTOBLOBS)
if data[0] == None: # otherwise it's ['NUM_MESSAGES_BEFORE_DELETION']
if DEBUG_SCHEDULER:
print "failed to delete"
try_time = 0 # someone else just touched it, so reset
time.sleep(LOCK_RETRY_DELAY)
continue
# we successfully found the uid and deleted the message
self.have_index_lock = True
# so also take it out of all mail
self.delete_lock_from_all_mail()
break
def release_index_lock(self, release_lock):
if not release_lock:
return
if DEBUG_METHODS_ARGS_RETURNS:
print "release_index_lock"
if self.have_index_lock:
# put up a message with subject "LOCK"
self.have_index_lock = False
self.append_lock()
def save_index_without_deleting(self):
# increment the sequence number
self.mapping[SEQUENCE_NUMBER] = self.mapping[SEQUENCE_NUMBER] + 1
# save the new sequence number to disk
self.save_index_sequence_number_to_disk()
if DEBUG_SCHEDULER:
print "successfully saved index seq number to disk or didn't"
# append new index
if DEBUG_SCHEDULER:
print "new index about to be uploaded", self.mapping
self.append_index(self.mapping)
# assumes cryptoblobs and index already exist
# this method only pushes up. we should have already updated from the
# new one by pulling down.
def save_index(self):
if DEBUG_METHODS_ARGS_RETURNS:
print "saving index"
if DEBUG_IMAP_FROM_GMAIL:
print "old index id"
if DEBUG_SCHEDULER:
print "old index", self.mapping
# delete original index
self.delete_index()
self.save_index_without_deleting()
def create_cryptoblobs_or_load_index(self):
if DEBUG_EXTRAS or DEBUG_METHODS_ARGS_RETURNS:
print "create_cryptoblobs_or_load_index"
if self.mapping != None:
if DEBUG_SCHEDULER:
print "self.mapping exists"
return
typ, data = imaplib.IMAP4_SSL.list(self)
names = [line.split()[-1].strip('"') for line in data]
if DEBUG_SCHEDULER:
print "names", names
# create cryptoblobs folder if one does not yet exist
if not CRYPTOBLOBS in names:
if DEBUG_IMAP_FROM_GMAIL or DEBUG_SCHEDULER or DEBUG_EXTRAS:
print "creating cryptoblobs"
imaplib.IMAP4_SSL.create(self, CRYPTOBLOBS)
random_salt = str(random.randint(0,10000000000))
self.save_salt_and_related_information(random_salt)
self.append_index({SEQUENCE_NUMBER: 1})
self.append_lock()
# unload the index
self.fetch_and_load_index()
# this makes it time out. ah well.
# for i in range(0, MIX_NUMBER):
# print "push up next fake"
# self.push_up_next_message(force_fake=True)
else:
# just unload the index
self.fetch_and_load_index()
if DEBUG_IMAP_FROM_GMAIL or DEBUG_SCHEDULER:
print "loaded index", self.mapping
def login(self, user, password):
if DEBUG_EXTRAS:
print "login"
a = imaplib.IMAP4_SSL.login(self, user, password)
self.create_cryptoblobs_or_load_index()
return a
# implicitly called by the constructor
def open(self, host = '', port = IMAP4_SSL_PORT):
a = imaplib.IMAP4_SSL.open(self, host, port)
# print "opening mailbox", host, port()
return a
# ********************************************************** #
# the following methods are called by mailpile
# ********************************************************** #
def list(self, directory='""', pattern='*'):
if DEBUG_METHODS_ARGS_RETURNS:
print "list", directory, pattern
typ, data = imaplib.IMAP4_SSL.list(self, directory, pattern)
if typ == OK:
# find the index of the one to delete
found = False
for i in range(0, len(data)):
if CRYPTOBLOBS in data[i]:
delete_index = i
found = True
break
# delete it
if found:
string_format = data.pop(delete_index)
# if directory is the top
if directory == '""' or directory == "":
# append things from the index, if they're not
# already in there as having no children
# start = string_format.index(CRYPTOBLOBS)
# before = string_format[:start]
# after = string_format[start+len(CRYPTOBLOBS):]
before = '(\\HasNoChildren) "/" "'
after = '"'
self.fetch_and_load_index()
to_append = []
for folder in self.mapping.keys():
# search through lines in data
if folder == SEQUENCE_NUMBER or folder == FAKE_MESSAGES\
or folder == NEXT_UID:
continue
found = False
for line in data:
if folder in line:
found = True
break
if not found:
to_append.append(folder)
for folder_name in to_append:
string = before + folder_name + after
data.append(string)
if DEBUG_IMAP_FROM_GMAIL or DEBUG_METHODS_ARGS_RETURNS:
print "list data", typ, data
return typ, data
def noop(self):
if DEBUG_SCHEDULER:
print "noop", self
typ, data = imaplib.IMAP4_SSL.noop(self)
# print "noop", data
return typ, data
# switches which mailbox we're prodding for updates in
def select(self, mailbox='INBOX', readonly=False):
if DEBUG_METHODS_ARGS_RETURNS:
print "select", mailbox
# don't let it ask for cryptoblobs
mb = mailbox
folder = mb.strip('"')
if folder in [GMAIL_ALL_MAIL, CRYPTOBLOBS, FAKE_MESSAGES, SEQUENCE_NUMBER, NEXT_UID]:
self.selected_mailbox = folder
return OK, ['0']
# add the number from the index
typ, data = imaplib.IMAP4_SSL.select(self, mb, readonly)
if typ == OK or (typ == "NO" and folder in self.mapping):
self.selected_mailbox = folder
self.fetch_and_load_index()
if typ == "NO":
data = ['0']
typ = OK
if folder in self.mapping:
count = len(self.mapping[folder])
the_sum = count + int(data[0])
data[0] = str(the_sum)
if DEBUG_METHODS_ARGS_RETURNS:
print "select data", mailbox, typ, data
return typ, data
nonsense = "ASDFKASFKACXMCMCKWDMFOEWEKR"
if mb.strip('"') == CRYPTOBLOBS:
mb = nonsense
typ, data = imaplib.IMAP4_SSL.select(self, mb, readonly)
if typ == OK:
self.selected_mailbox = mailbox.strip('"')
if mb == nonsense:
# replace CRYPTOBLOBS
string_format = data[0]
if nonsense in string_format:
start = string_format.index(nonsense)
before = string_format[:start]
after = string_format[start+len(nonsense):]
new_string = before + CRYPTOBLOBS + after
data[0] = new_string
if DEBUG_IMAP_FROM_GMAIL:
print "select", mailbox, mb, data
return typ, data
def timed_imap_exchange(self):
if DEBUG_SCHEDULER or DEBUG_METHODS_ARGS_RETURNS:
print "imap timer tick"
if self._scheduler.next_time_point_action_is_push():
# push up message
if DEBUG_SCHEDULER or DEBUG_METHODS_ARGS_RETURNS:
print "up"
self.push_up_next_message()
else:
# pull down (delete) message from imap
if DEBUG_SCHEDULER or DEBUG_METHODS_ARGS_RETURNS:
print "down"
self.pull_down_next_message()
def push_up_next_message(self, force_fake=False):
message_contents = ""
folder = ""
uid = ""
generate = False
if len(self.send_queue) >= 1 and not force_fake:
if DEBUG_IMAP_FROM_SMTORP or DEBUG_SCHEDULER:
print "pushing up next message"
(message_contents, folder, uid, generate_uid) = self.send_queue[0]
generate = generate_uid
self.send_queue = self.send_queue[1:]
self.save_send_queue_to_disk()
else:
# push up a fake message
if DEBUG_SCHEDULER:
print "push up a fake message"
# construct fake message
folder = FAKE_MESSAGES
# just put into a FAKE_MESSAGES pseudo-folder in cryptoblobs!
message_contents = fake_message_with_random_contents_as_string()
if DEBUG_SCHEDULER:
print "fake message:\n", message_contents
uid = ""
generate = True
# append onto index's list of fake messages
if DEBUG_SCHEDULER:
print "uid", uid
if DEBUG_SCHEDULER:
print "folder, uid", folder, uid
self.add_message_to_folder_internal(message_contents,
folder, uid, False, generate_uid=generate, mix=(not force_fake))
self._scheduler.message_was_pushed()
def pull_down_next_message(self):
# check messages to delete and pull down one of those.
# otherwise, pull down a fake message. also save delete queue to
# disk.
if len(self.delete_queue) >= 1:
raise NotImplementedError("No code should call this")
else:
# pull down a fake message
# choose which fake message to pull down
self.acquire_index_lock()
self.fetch_and_load_index()
# this will sync the index to make sure we know about all
# fake messages
if FAKE_MESSAGES not in self.mapping \
or len(self.mapping[FAKE_MESSAGES]) == 0:
if DEBUG_SCHEDULER:
print "no fake messages to pull down"
return
delete_uid = random.choice(self.mapping[FAKE_MESSAGES].keys())
if DEBUG_SCHEDULER:
print "pulling down fake message", delete_uid
hash_of_message = self.mapping[FAKE_MESSAGES][delete_uid]
self.delete_message_from_pseudo_folder(delete_uid, FAKE_MESSAGES)
self._scheduler.message_was_pulled()
# message_contents is the message as a string. get by calling str(message)
# where message is a Message object.
def add_message_to_folder(self, message_contents, folder):
if DEBUG_METHODS_ARGS_RETURNS:
print "add_message_to_folder", folder
# we've called this from an external class, so it can't be the index.
self.add_message_to_folder_internal(message_contents, folder,
"", True, generate_uid=True)
if DEBUG_METHODS_ARGS_RETURNS:
print "added message to folder", folder
def choose_k_messages_to_fetch_then_delete(self, k, extra=None):
# combine uids from real folders
combined = []
for folder in self.mapping:
if folder in [NEXT_UID, SEQUENCE_NUMBER]:
continue
uids = self.mapping[folder].keys()
combined.extend([(folder, x) for x in uids])
# choose one more than you should
num = min(k+1, len(combined))
sample = random.sample(combined, num)
# if extra is not none and in there, delete extra
if extra and extra in sample:
sample.remove(extra)
# else, delete the last one
else:
sample = sample[:-1]
return sample
# <schedule_upload> should be True for messages coming from a
# metadata-secure source. it should probably be False if we've
# just pulled the message down from IMAP and are reuploading it.
# TODO: check to make sure this actually works, and only delete from
# cache after. because it doesn't, there's no fault tolerance
# going on...
# <folder> is a pseudo-folder.
def add_message_to_folder_internal(self, message_contents, folder,
uid, schedule_upload, generate_uid=False, mix=True, \
release_lock=True, refetch_index=True):
uid = str(uid)
if DEBUG_IMAP_FROM_SMTORP or DEBUG_SCHEDULER:
print "add_message_to_folder", self, "schedule for later", schedule_upload
if schedule_upload:
# place it on the queue, deal with it later
# if messages aren't sent, they'll be pickled and sent next time
self.send_queue.append((message_contents, folder, uid, generate_uid))
self.save_send_queue_to_disk()
return
# update it in the index
if DEBUG_IMAP_FROM_SMTORP or DEBUG_SCHEDULER or DEBUG_IMAP_FROM_GMAIL:
# print "message_contents", message_contents
print "folder", folder
print "uid", uid
print "host", self.host
print "port", self.port
# these calls are atomic
self.acquire_index_lock()
self.fetch_and_load_index(refetch_index=refetch_index)
selected_cache = self.selected_mailbox
if not folder in self.mapping:
self.mapping[folder] = {}
if generate_uid:
uid = self.uid_for_constructed_message(message_contents, folder)
saved_messages_and_data = [(uid, folder, message_contents)]
if mix:
self.choose_fetch_delete_and_save_k_messages(MIX_NUMBER, saved_messages_and_data)
# now we have k+1 tuples in saved_messages_and_data, none of which
# are in the index or in actual folder
for uid, folder, message_contents in saved_messages_and_data:
unique_subject = get_new_unique_subject()
self.mapping[folder][uid] = unique_subject
# reupload message to the cryptoblobs folder
self.encrypt_and_append_message(message_contents, unique_subject)
self.selected_mailbox = selected_cache
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(self.selected_mailbox))
# save the index
if DEBUG_IMAP_FROM_GMAIL:
print "about to try saving index"
self.save_index()
self.release_index_lock(release_lock)
def choose_fetch_delete_and_save_k_messages(self, k, saved_messages_and_data, \
extra=None):
# choose k messages to fetch then delete
chosen_folder_uid_pairs = self.choose_k_messages_to_fetch_then_delete(k, extra=extra)
if extra:
# insert it into chosen_folder_uid_pairs
position = random.randint(0, len(chosen_folder_uid_pairs))
chosen_folder_uid_pairs.insert(position, extra)
# fetch and delete each of those messages
for folder, uid in chosen_folder_uid_pairs:
self.select(folder)
message_contents = self.uid("FETCH",uid,"(BODY[TEXT])")[1][0][1].strip()
if (folder, uid) != extra:
saved_messages_and_data.append((uid, folder, message_contents))
self.delete_message_from_pseudo_folder(uid, folder, mix=False, release_lock=False)
def delete_message_from_pseudo_folder(self, delete_uid, folder, \
mix=True, release_lock=True, refetch_index=True):
delete_uid = str(delete_uid)
if DEBUG_EXTRAS:
print "delete_message_from_pseudo_folder", delete_uid, folder
# these calls are atomic
self.acquire_index_lock()
self.fetch_and_load_index(refetch_index=refetch_index) # this should be redundant
if DEBUG_SCHEDULER:
print "delete folder in self.mapping", folder in self.mapping
print "uid in there where we want it", delete_uid in self.mapping[folder]
selected_cache = self.selected_mailbox
saved_messages_and_data = []
if mix:
self.choose_fetch_delete_and_save_k_messages(MIX_NUMBER, \
saved_messages_and_data, extra=(folder,delete_uid))
# now we have k tuples in saved_messages_and_data, none of which
# are in the index or in actual folder
for uid, folder, message_contents in saved_messages_and_data:
unique_subject = get_new_unique_subject()
self.mapping[folder][uid] = unique_subject
# reupload message to the cryptoblobs folder
self.encrypt_and_append_message(message_contents, unique_subject)
if not mix:
# delete_uid is the old uid that's not meaningful to us
# we want the hash of the message, because it'll be in the
# subject string, and we can use that to find the actual
# uid to delete.
hash_of_message = self.mapping[folder][delete_uid]
actual_uid = self.get_message_uid_given_hash_in_subject(hash_of_message)
del self.mapping[folder][delete_uid]
# reupload message to the cryptoblobs folder
self.delete_message_from_actual_folder_uid(actual_uid, CRYPTOBLOBS)
self.delete_from_all_mail_by_subject(hash_of_message)
# clean up
self.selected_mailbox = selected_cache
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(self.selected_mailbox))
# save the index
self.save_index()
self.release_index_lock(release_lock)
# assumes only one of that subject in that folder exists
def delete_from_all_mail_by_subject(self, subject_substring):
uid = self.get_list_of_uids_given_subject_line(subject_substring, \
GMAIL_ALL_MAIL)[0]
self.delete_message_from_actual_folder_uid(uid, GMAIL_ALL_MAIL)
def delete_index_from_all_mail(self):
self.delete_from_all_mail_by_subject(INDEX)
def delete_lock_from_all_mail(self):
self.delete_from_all_mail_by_subject(LOCK)
def delete_message_from_all_other_folders_given_subject(self, subject):
folder = GMAIL_ALL_MAIL
uids = self.get_list_of_uids_given_subject_line(subject, folder)
new_uid = uids[0]
# delete that uid from all mail
self.delete_message_from_actual_folder_uid(new_uid, folder)
def delete_message_from_all_other_folders(self, message_contents):
# TODO this only works for this exact setup, with gmail. if
# you want to use it for something else, have fun! if you're
# doing gmail, delete from all mail last.
folder = GMAIL_ALL_MAIL
# find the uid of the message in gmail by searching for its subject
messageified = Message(message_contents)
subject = messageified['Subject'].strip().strip('"')
uids = self.get_list_of_uids_given_subject_line(subject, folder)
new_uid = None
# look through the results to find which one is the right one
for potential_uid in uids:
# fetch that message
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(folder))
typ, data = imaplib.IMAP4_SSL.uid(self, 'FETCH', potential_uid, '(BODY[])')
imaplib.IMAP4_SSL.select(self, mailbox=self.quote(self.selected_mailbox))
new_contents = data[0][1].strip()
if new_contents == message_contents:
# use that uid
new_uid = potential_uid
break
if not new_uid:
return