-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassManager.py
More file actions
1349 lines (1110 loc) · 45 KB
/
PassManager.py
File metadata and controls
1349 lines (1110 loc) · 45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
print("\nLoading packages\n")
print("Loading os")
import os
print("Loading csv")
import csv
print("Loading sys")
import sys
print("Loading shutil")
import shutil
# print("Loading smtplib")
# import smtplib
print("Loading pandas")
import pandas as pd
print("Loading FTP")
from ftplib import FTP
print("Loading time")
from time import sleep
print("Loading random")
from random import sample
print("Loading getpass")
from getpass import getpass
print("Loading platform")
from platform import system
print("Loading hashlib")
from hashlib import sha3_512
print("Loading datetime")
from datetime import datetime
print("Loading checker")
from checker import check_h, warning_msg
print("Loading bcrypt")
from bcrypt import gensalt, checkpw, hashpw
print("Loading pyAesCrypt")
from pyAesCrypt import encryptFile, decryptFile
print("Loading complete")
def double_check(first_promnt="Key: ", confirm_promnt="Confirm key: ", dont_match_promt="Keys don't match\n"
, attempts_limit=3):
y = 0
key1 = getpass(first_promnt)
key2 = getpass(confirm_promnt)
while key1 != key2:
y += 1
if y == attempts_limit:
exit("Too many attempts")
print(dont_match_promt)
key1 = getpass(first_promnt)
key2 = getpass(confirm_promnt)
else:
return key1
def encrypt_file(decrypted_in, encrypted_out, key):
encryptFile(decrypted_in, encrypted_out, key, 1024*64)
def decrypt_file(encrypted_in, decrypted_out, key):
decryptFile(encrypted_in, decrypted_out, key, 1024*64)
'''
def send_mail_copy(saved_logins, token, key):
print("Currently this option only works with gmail, and you have to turn on 'Allow less secure apps' ", end="\n\n")
en = input("Would you like to send the files encrypted? (Recommended option is Y) Y/n")
if en.lower() == "y":
message = "Attached here you have encrypted copies of saved_logins and token files" \
"\n\nSent by PasswordManager."
token_file = "x"
logins_file = "y"
elif en.lower() == "n":
message = "Attached here you have encrypted copies of saved_logins and token files" \
"\n\nSent by PasswordManager."
else:
print("Didn't recognize that answer, preceding sending encrypted files")
message = "Attached here you have encrypted copies of saved_logins and token files" \
"\n\nSent by PasswordManager."
token_file = "x"
logins_file = "y"
sender_mail = input("Email to send the files: ")
sender_pass = getpass("Password of mail: ")
receiver_mail = input("Email to receive files: ")
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(sender_mail, sender_pass)
server.sendmail(
sender_mail,
receiver_mail,
message)
server.quit()
'''
def create_logins_file(key):
with open("unenc", "w") as new_logins:
new_logins.write("Website,Mail,User,Password,Notes\n")
encrypt_file("unenc", "saved_logins", key)
os.remove("unenc")
return open("saved_logins", "r")
def new_token():
key = getpass("\nPlease enter your new key: ")
while True:
if len(key) < 8:
print("Its recommended to use a password with atleast 8 characters")
key_conf = getpass("Enter a new key or type the last key again to use it: ")
if key == key_conf:
break
else:
key = key_conf
else:
key_conf = getpass("Confirm your key: ")
if key == key_conf:
break
else:
print("Key's dont match")
return key
def sc_files(): # Check password files and key_hash present and returns their values
if os.path.exists("saved_logins") and os.path.exists("token"):
logins = open("saved_logins", "r")
token = open("token", "r")
key = ""
elif not os.path.exists("saved_logins"):
print("saved_logins file is missing, creating new logins and token file.")
key = new_token()
token = open("token", "w")
token.write(str(hashpw(key.encode("utf8"), gensalt(11))))
token.close()
token = open("token", "r")
logins = create_logins_file(key)
elif not os.path.exists("token"):
option_ = input("token file is missing, would you like to format all data? Y/n: ")
if option_.lower() == "y":
key = new_token()
token = open("token", "w")
token.write(str(hashpw(key.encode("utf8"), gensalt(11))))
logins = create_logins_file(key)
token.close()
else:
print("Cannot continue without the token file")
exit(0)
# noinspection PyUnboundLocalVariable
return token, logins, key
def hash256(key): # Hashes a given key with sha512
key_to_hash = sha3_512(key.encode(encoding="UTF-8")).hexdigest()
return key_to_hash
def check_key(): # Compares the stored hash with the user hash
token_f = open("token", "rb")
stored_hash = token_f.read()[2:-1] # File not readable after creating new token file
# print(stored_hash.__len__())
if stored_hash.__len__() == 128:
print("Old hash found, checking...")
user_key = input("Please enter your key: ")
hashed_key = hash256(user_key)
xyz = 1
while hashed_key != stored_hash:
if xyz == 3:
exit("Too many tries.")
else:
user_key = getpass("Your key didn't match the saved key. Please try again: ")
hashed_key = hash256(user_key)
xyz += 1
print("Saving new hash")
tokenf = open("token", "w+")
tokenf.write(str(hashpw(user_key.encode("utf8"), gensalt(11))))
tokenf.close()
elif stored_hash.__len__() != 60:
print("No compatible key stored. New key needed. (This will format any stored password's)\n")
token_f.close()
while True:
key = getpass(u"Please insert the key you would like to use to lock/unlock the list: ")
key_c = getpass(u"Confirm your key: ")
if key != key_c:
print("Key's didn't match\n")
else:
break
print("Saving new key")
open("saved_logins", "w+")
tokenf = open("token", "w+")
tokenf.write(str(hashpw(key.encode("utf8"), gensalt(11))))
user_key = key
else:
user_key = getpass("Please enter your key: ")
xyz = 0
no_match = True
while no_match:
xyz += 1
if xyz == 3:
exit("Too many tries.")
else:
if checkpw(user_key.encode("utf8"), stored_hash):
break
else:
user_key = getpass("Wrong key, please try again: ")
key = user_key
return key
def read_csv(csv_file):
df = pd.read_csv(csv_file)
pd.options.display.max_columns = len(df.columns)
pd.set_option('display.expand_frame_repr', False)
print(df)
def readall_passwords(logins_f, key):
encrypted_logins = logins_f
temp_fn = "temp"
decrypt_file(encrypted_logins, temp_fn, key)
read_csv(temp_fn)
os.remove(temp_fn)
def csv_value_change(x_, y, value, file_name):
f = open(file_name, "r")
csv_read = csv.reader(f)
lines = list(csv_read)
lines[x_ + 1][y] = value
new_csv_ = open(f.name, "w", newline="")
writer = csv.writer(new_csv_)
writer.writerows(lines)
def generate_random_pass(length):
chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ!@#$%^&*()/-_.:?+"
special_chars = "!@#$%^&*()/-_.:?+"
nums = "01234567890"
caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ran_pass = ""
x_ = 0
y = 0
z = 0
while x_ == 0 or y == 0 or z == 0: # Makes sure at least one number and one special character is present
x_ = 0
y = 0
z = 0
ran_pass = "".join(sample(chars, length))
for char in ran_pass:
if char in special_chars:
x_ += 1
elif char in nums:
y += 1
elif char in caps:
z += 1
return ran_pass
def add_new_record(logins_f, key, columns_):
values = []
for field in columns_:
if field.lower() != "password":
values.append(input("%s: " % field.capitalize()))
else:
random_pass_opt = input("Would you like a random generated password? Y/n: ")
if random_pass_opt.lower() == "y":
length = input("How long would you like the password to be? Leave blank for default length: ")
if length == "":
length = 14
password = generate_random_pass(int(length))
print("Your new password is:\n%s" % password)
elif random_pass_opt.lower() == "n":
password = getpass("Password: ")
else:
print("Not an option. Continuing with manual password\n")
password = getpass("Password: ")
values.append(password)
for value in values:
while "," in value:
pos = values.index(value)
print("\n',' may not be entered into any field")
value = input("New value for %s: " % (columns_[pos]))
values[pos] = value
write_str = ""
for a in values:
write_str += a + ","
write_str = write_str[:-1]
write_str += "\n"
decrypt_file(logins_f.name, "unenc", key)
new_file = open("unenc", "a")
new_file.write(write_str)
new_file.close()
encrypt_file(new_file.name, logins_f.name, key)
os.remove(new_file.name)
def edit_record(logins_f, key, columns_):
pos = 0
unenc = "unenc"
encrypted_logins = logins_f.name
decrypt_file(encrypted_logins, unenc, key)
read_csv(unenc)
record_to_change = input("No of record to edit: ")
while not record_to_change.isdigit():
print("\nShould be a number")
record_to_change = input("No of record to edit: ")
record_to_change = int(record_to_change)
field_to_change = input("Name of the column to edit: ")
try:
pos = columns_.index(field_to_change.lower())
found = True
except ValueError:
print("Column not found")
try:
pos = int(input("Enter the position of the column (1-%s): " % len(columns_))) - 1
found = True
except IndexError:
print("Column not found")
found = False
if found:
if columns_[pos].lower != "password":
new_value = input("New value for '%s': " % columns_[pos])
else:
option_ = input("Would you like a new random password? Y/n: ")
if option_.lower() == "n":
new_value = getpass("New value for the Password: ")
else:
length = input("How long would you like the password to be? Leave blank for default length: ")
if length == "":
length = 12
else:
length = int(length)
new_value = generate_random_pass(length)
while "," in new_value:
print("\nValues cannot contain ','")
new_value = input("New value for '%s': " % columns_[pos])
csv_value_change(record_to_change, pos, new_value, unenc)
encrypt_file(unenc, "saved_logins", key)
os.remove(unenc)
def delete_record(logins_f, key):
readall_passwords(logins_f.name, key)
record_to_delete = int(input("Which record do you want to delete? "))
confirm = input("Are you sure you want to delete row number %s ? Y/n: " % record_to_delete)
record_to_delete += 1
if confirm.lower() == "y":
row_c = 0
temp_file = open("temp", "a")
decrypt_file(logins_file.name, "unenc", key)
unenc = open("unenc", "r")
for row in unenc:
if row_c != record_to_delete:
temp_file.write(row)
row_c += 1
unenc.close()
temp_file.close()
os.remove(unenc.name)
encrypt_file(temp_file.name, logins_file.name, key)
os.remove(temp_file.name)
def change_key(key):
print("After changing the key, you will have to re-open the program")
old_key = getpass("Please enter your old key: ")
counter = 0
while old_key != key:
counter += 1
print("Incorrect key")
old_key = getpass("\nPlease enter your old key: ")
if counter == 3:
exit("Too many attempts")
new_key = new_token()
with open("token", "w+") as f:
f.write(str(hashpw(new_key.encode("utf8"), gensalt(11))))
decrypt_file("saved_logins", "temp", key)
encrypt_file("temp", "saved_logins", new_key)
os.remove("temp")
return new_key
def delete_files(token_f, logins_f, key):
print("FTP Files will not be deleted, please do so manually or with option 'J' on the main menu")
pass_confirm = getpass("All data will be deleted, type your key to continue or 'x' to cancel: ")
if pass_confirm == key:
token_f.close()
logins_f.close()
os.remove(logins_f.name)
os.remove(token_f.name)
if os.path.exists("ftp.conf"):
os.remove("ftp.conf")
exit(0)
elif option.lower() == "x":
pass
else:
print("Keys don't match")
sleep(1.5)
def move_record(logins_f, key):
logins_fname = logins_f.name
readall_passwords(logins_fname, key)
record_to_move = int(input("Which record would you like to move? ")) + 1
new_position = int(input("Where would you like to move this record? ")) + 1
row_c = 0
decrypt_file(logins_fname, "unenc", key)
unenc = open("unenc", "r")
row_to_move = ""
for row in unenc: # First loop to find the row we want to move
if row_c == record_to_move:
row_to_move = row
break
row_c += 1
unenc.close()
if row_to_move != "":
row_c = 0
temp_file = open("temp", "w", newline="")
decrypt_file(logins_fname, "unenc", key)
unenc = open("unenc", "r")
for row in unenc:
if row_c == new_position:
if row_to_move.endswith("\n"): # Checks if there is already \n to avoid extra blank spaces
temp_file.write(row_to_move)
else:
temp_file.write(row_to_move + "\n")
temp_file.write(row)
elif row_c != record_to_move and row != "":
temp_file.write(row)
row_c += 1
unenc.close()
temp_file.close()
encrypt_file(temp_file.name, logins_file.name, key)
os.remove(unenc.name)
os.remove(temp_file.name)
else:
print("Couldn't find the selected record")
sleep(2)
def local_backup(logins_f, token_f, key):
global os_sys
backup_name = ""
token_back_name = ""
home_path = os.path.expanduser("~")
if os_sys in ["windows"]:
path = home_path + "\\Documents\\PasswordManager_Backup\\"
else:
path = home_path + "/Documents/PasswordManager_Backup/"
enc = input("Would you like to save the backup (E)ncrypted or (D)ecrypted: ")
custom_name_opt = input("Would you like to save files with (A)utomatically generated names or (C)ustom names: ")
if custom_name_opt.lower() == "c":
backup_name = input("Please enter name for saved_logins file: ")
if enc.lower() == "e":
token_back_name = input("Please enter name for taken file: ")
elif custom_name_opt.lower() == "a":
datetime_ = (str(datetime.now()).replace(" ", "_").replace(":", "-"))[:-7]
backup_name = logins_f.name + datetime_
token_back_name = token_f.name + datetime_
else:
enc = "a"
try:
os.makedirs(path)
except FileExistsError:
pass
if enc.lower() == "e":
print("Copying token file to " + path)
shutil.copy(logins_f.name, path + backup_name)
shutil.copy(token_f.name, path + token_back_name)
elif enc.lower() == "d":
print("Decrypting files")
w = 0
key_conf = getpass("Please type your key: ")
while key_conf != key:
w += 1
if w == 3:
exit("To many attempts")
print("Keys dont match")
key_conf = getpass("Please confirm your key: ")
decrypt_file(logins_f.name, "temp", key)
print("Copying decrypted files to " + path + backup_name)
shutil.copy("temp", path + backup_name)
os.remove("temp")
input("Press enter to continue")
else:
print("Unexpected error, please try again")
input("\nPress enter to continue")
def import_backup(key, token_f, saved_logins_f):
global os_sys
confirm = input("This will delete any data present. Are you sure you want to continue? Y/n: ")
if confirm.lower() == "y":
w = 0
key_conf = getpass("Please confirm your key: ")
while key_conf != key:
w += 1
if w == 3:
print("Too many tries")
exit()
print("Keys dont match")
key_conf = getpass("Please confirm your key: ")
home_path = os.path.expanduser("~")
if os_sys in ["windows"]:
path = home_path + "\\Documents\\PasswordManager_Backup\\"
# if os_sys in ["macosx darwin linux"]:
else:
path = home_path + "/Documents/PasswordManager_Backup/"
saved_l_name = input("Please type the name of the saved_logins backup file: ")
if os.path.exists(path + saved_l_name):
print("saved_logins file found")
saved_logins_path = path + saved_l_name
saved_logins_file = open(saved_logins_path, "r")
else:
print("Could not find file")
if os_sys in ["windows"]:
path = input("Could not find file, type the location of the file C:\\Path\\To\\Folder\\")
# if os_sys in ["macosx darwin linux"]:
else:
path = input("Could not find file, type the location of the file /Path/To/Folder/")
if os.path.exists(path):
print("saved_logins file found")
saved_logins_path = path + saved_l_name
saved_logins_file = open(saved_logins_path, "r")
else:
exit("Couldn't find path to file")
saved_logins_file = ""
decrypted = True
try:
for line in saved_logins_file:
del line
break
except UnicodeDecodeError:
decrypted = False
if decrypted:
print("File detected as decrypted, new key required to continue")
key = double_check("New key: ", "Confirm new key: ", "Keys don't match\n", -1)
encrypt_file(saved_logins_file.name, "saved_logins", key)
delete_old = input("Do you want to delete the backup file? Y/n: ")
if delete_old.lower() == "y":
os.remove(saved_logins_file.name)
with open("token", "w") as token:
token.write(str(hashpw(key.encode("utf8"), gensalt(11))))
print("Import successful")
elif not decrypted:
print("File detected as encrypted")
token_path = path + "token" + saved_l_name[-19:]
if not os.path.exists(token_path):
token_name = input("Please type the name of the token file: ")
token_path = path + token_name
if not os.path.exists(token_path):
if os_sys in ["windows"]:
path = input("Could not find file, type the location of the file C:\\Path\\To\\Folder\\")
# if os_sys in ["macosx darwin linux"]:
else:
path = input("Could not find file, type the location of the file /Path/To/Folder/")
if not os.path.exists(path + token_name):
exit("Could not find file")
backup_token = open(token_path, "r").read()
print("Token file found")
abc = 0
backup_key = getpass("Please input the key used in this backup: ")
while checkpw(backup_token, hashpw(backup_key.encode("utf8"), gensalt(11))):
abc += 1
backup_key = getpass("Keys don't match, please try again:")
# print(backup_token, hashpw(backup_key.encode("utf8"), gensalt(11)))
if abc == 3:
print("If you have forgotten the key type leave to exit the backup import,"
" or type another key to try again.")
exit_ = getpass(": ")
if exit_.lower() != "leave":
backup_key = exit_
else:
break
else:
print("Keys match")
os.remove(token_f.name)
os.remove(saved_logins_f.name)
shutil.copy(token_path, "token")
shutil.copy(saved_logins_path, "saved_logins")
remove_old = input("Would you like to delete the backup from the old directory? Y/n: ")
if remove_old.lower() == "y":
os.remove(token_path)
os.remove(saved_logins_path)
print("Import successful")
input("Press enter to continue")
def add_new_column(saved_logins_f, key):
column_name = input("Name for the new column: ")
while not column_name or column_name.isspace():
print("Column name cannot be left blank")
print("To exit type _exit_")
column_name = input("Name for the new column: ")
if column_name == "_exit_":
break
else:
decrypt_file(saved_logins_f.name, "unenc", key)
unenc_f = open("unenc", "r")
with open("temp", "w") as f:
x_ = 0
for row in unenc_f:
n = "\n"
if x_ == 0:
x_ += 1
new_row = (row[:-1] + ",%s" % column_name + n)
f.write(new_row)
else:
new_row = row[:-1] + "," + n
f.write(new_row)
del x_
f.flush()
encrypt_file("temp", saved_logins_f.name, key)
os.remove("temp")
os.remove("unenc")
def delete_column(saved_logins_f, key, header_list):
for item in header_list:
print(item.capitalize(), end=" ")
else:
print("")
column_name = input("\nName of the column you want to remove: ").lower()
try:
index2remove = header_list.index(column_name)
decrypt_file(saved_logins_f.name, "temp", key)
with open("temp", "r") as source:
os.remove("temp")
rdr = csv.reader(source)
with open("result", "w") as result:
wtr = csv.writer(result)
for r in rdr:
write = r[:index2remove] + r[(index2remove + 1):]
wtr.writerow(write)
encrypt_file("result", "saved_logins", key)
os.remove("result")
except ValueError:
print("That wasn't a valid column")
input("Press enter to continue")
def get_columns(saved_logins_f, key):
decrypt_file(saved_logins_f.name, "temp", key)
with open("temp", "r") as file:
for row in file:
header = row[:-1].split(",")
break
os.remove("temp")
return [i.lower() for i in header]
'''
def check_appropiate_data(input, data_type, message="That data wasn't appropriate, please type it again",
possible_values = ""):
accepted_types = ("number", "text")
if data_type in accepted_types:
if data_type == "number":
def x(x): return isinstance(x, int)
elif data_type == "text":
pass
'''
def search(saved_logins_f, columns_, key):
try:
search_mode = int(input("Would you like to search in a column (1) or in all fields (2): "))
except ValueError:
while True:
try:
search_mode = int(input("Please select to search in a column (1) or in all fields (2): "))
break
except ValueError:
pass
search_term = input("\nTerm to search: ")
num_col = len(columns_)
if search_mode == 2:
decrypt_file(saved_logins_f.name, "temp", key)
with open("temp", "r") as temp:
os.remove("temp")
print()
x = -1
for line in temp:
if x != -1:
if search_term in line:
splitted = line.split(",")
print("\nMatch found at line %s:" % x)
for i in range(num_col):
print("{}: {} ".format(columns[i].capitalize(), splitted[i]), end="")
x += 1
elif search_mode == 1:
print("\n ", end="")
for i in columns_:
print(i.capitalize(), end=" ")
print("")
column2search = input("\nName of the column to search: ").lower()
try:
pos = columns_.index(column2search.lower())
except ValueError:
print("Column not found")
while True:
try:
pos = int(input("Enter the position of the column (1-%s): " % len(columns_))) - 1
break
except IndexError:
print("Column not found")
decrypt_file(saved_logins_f.name, "temp", key)
with open("temp", "r") as temp:
os.remove("temp")
print()
x = -1
for line in temp:
if x != -1:
splitted = line.split(",")
if search_term in splitted[pos]:
print("\nMatch found at line %s:" % x)
for i in range(num_col):
print("{}: {} ".format(columns[i].capitalize(), splitted[i]), end="")
x += 1
else:
print("Couldnt find the specified column")
input("\nPress enter to continue")
def configure_ftp(key):
configure = True
if os.path.exists("ftp.conf"):
override = input("This will override your old ftp configuration file, do you want to continue (Y/n): ")
if override.lower() == "n":
configure = False
if configure:
config = []
server = input("Enter IP or domain of the ftp server: ")
port = input("Enter the port of the ftp server (default = 21): ")
if port == "":
port = "21"
user = input("Enter the username of the ftp server: ")
passw = getpass("Enter password for the ftp server: ")
auto_sync = input("Would you like to automatically sync with the FTP Server (May impact performance)"
" (Y/n): ").lower()
if auto_sync == "y":
auto_sync = True
else:
auto_sync = False
config.append('server=%s\n' % server)
config.append('port=%s\n' % port)
config.append('user=%s\n' % user)
config.append('passw=%s\n' % passw)
config.append('auto_sync=%s\n' % auto_sync)
with open("conf", "w+") as conf:
for line in range(len(config)):
conf.write(config[line])
encrypt_file("conf", "ftp.conf", key)
os.remove("conf")
def pull_ftp(server, port, user, passw):
try:
x_ = True
cancel = False
print("\nConnecting to server\n")
ftp = FTP()
print(ftp.connect(server, int(port)))
print("\nLogging in\n")
print(ftp.login(user, passw))
print("\nChecking token")
with open("token_f", "wb") as f:
ftp.retrbinary("RETR " + "token", f.write)
token = open("token", "r")
token_f = open("token_f", "r")
if token.read() != token_f.read():
yn = input("Seems the key stored on the FTP Server isn't the same as the one locally,"
" do you want to continue (Y/n): ").lower()
if yn == "y":
cancel = True
os.remove("token_f")
if x_:
with open("token", "wb") as f:
ftp.retrbinary("RETR " + "token", f.write)
with open("saved_logins", "wb") as f:
ftp.retrbinary("RETR " + "saved_logins", f.write)
ftp.quit()
print("Files retrieved successfully")
sleep(1)
except:
print("\nUnexpected error: ", sys.exc_info()[0])
opt = input("Press enter to continue or 'X' to get error details: ")
if opt.lower() == "x":
print("\n -------- Error Info -------- \n")
raise
if cancel:
exit("Its necessary to restart the program")
return cancel
def push_ftp(server, port, user, passw, token_name="token", saved_logins_name = "saved_logins"):
try:
print("Connecting to server")
ftp = FTP()
ftp.connect(server, int(port))
print("Logging in")
ftp.login(user, passw)
print("Uploading files")
with open('token', 'rb') as f:
ftp.storbinary('STOR %s' % token_name, f)
with open('saved_logins', 'rb') as f:
ftp.storbinary('STOR %s' % saved_logins_name, f)
ftp.quit()
print("Files sent succesfully")
sleep(1)
except:
print("\nUnexpected error: ", sys.exc_info()[0])
opt = input("Press enter to continue or 'X' to get error details: ")
if opt.lower() == "x":
print("\n -------- Error Info -------- \n")
raise
def silent_push(server, port, user, passw):
try:
ftp = FTP()
ftp.connect(server, int(port))
ftp.login(user, passw)
with open('token', 'rb') as f:
ftp.storbinary('STOR %s' % 'token', f)
with open('saved_logins', 'rb') as f:
ftp.storbinary('STOR %s' % 'saved_logins', f)
ftp.quit()
except:
pass
def check_ftp(server, port, user, passw):
try:
print("Connecting to server\n")
ftp = FTP()
print(ftp.connect(server, int(port)))
print("\nLogging in\n")
print(ftp.login(user, passw))
ftp.quit()
input("\nPress enter to continue")
except:
print("\nUnexpected error: ", sys.exc_info()[0])
opt = input("Press enter to continue or 'X' to get error details: ")
if opt.lower() == "x":
print("\n -------- Error Info -------- \n")
raise
def load_ftp_config(key):
if os.path.exists("ftp.conf"):
print("FTP Configuration detected")
print("Loading FTP Configuration")
decrypt_file("ftp.conf", "ftp_conf.py", key)
# New method for getting the data
with open("ftp_conf.py", "r") as file:
for line in file:
contents = line.split("=")
contents[1] = contents[1][:-1]
if "server" in contents:
server = contents[1]
elif "port" in contents:
port = contents[1]
elif "user" in contents:
user = contents[1]
elif "passw" in contents:
passw = contents[1]
elif "auto_sync" in contents:
auto = contents[1]
if auto == "True":
auto_sync = True
else:
auto_sync = False
# Old method
# import ftp_conf
# server = ftp_conf.server
# port = ftp_conf.port
# user = ftp_conf.user
# passw = ftp_conf.passw
# auto_sync = ftp_conf.auto_sync
# del ftp_conf
os.remove("ftp_conf.py")
else:
print("No FTP Configuration file found")
return server, port, user, passw, auto_sync
def show_ftp_options(server_, port_, user_, passw_, auto_sync_):
print("Server: {serverx}\nPort: {portx}\nUser: {userx}\nPassword: {passwd} \nAuto Sync: {a_s}".
format(serverx=server_, portx=port_, userx=user_, a_s=auto_sync_,
passwd="*" * len(passw_)), end="\n\n")
input("Press enter to continue ")
def delete_ftp_files(server_, port_, user_, passw_):
print("This will not delete any backup files you may have saved there.")
opt = input("Would you like to remove the FTP Configuration? Y/n: ").lower()
if opt != "y" and opt != "n":
print("Not an acceptable option, continuing without deleting configuration")