import Crypto import hashlib import SHA256Custom from DBConn import DBConn from User import User # This is just a setup and test script to create the initial database and to test out some of the features # should create the DB file if missing db_connection = DBConn() create_system_settings_sql = "CREATE TABLE IF NOT EXISTS system_settings(" \ "setting_name TEXT PRIMARY KEY," \ "setting_value TEXT" \ ");" create_user_table_sql = "CREATE TABLE IF NOT EXISTS users (" \ "user_id INTEGER PRIMARY KEY," \ "username TEXT UNIQUE," \ "user_first_name TEXT," \ "user_last_name TEXT," \ "user_password TEXT NOT NULL," \ "user_salt TEXT NOT NULL," \ "user_email TEXT NOT NULL," \ "user_role TEXT NOT NULL," \ "user_date_created INTEGER NOT NULL" \ ");" create_user_login_attempt_table = "CREATE TABLE IF NOT EXISTS login_attempts(" \ "login_attempt_id INTEGER PRIMARY KEY," \ "user_id INTEGER NOT NULL," \ "login_attempt_ip TEXT," \ "login_attempt_status INTEGER NOT NULL," \ "login_attempt_timestamp INTEGER NOT NULL," \ "FOREIGN KEY(user_id) REFERENCES users(user_id)" \ ");" create_session_table = "CREATE TABLE IF NOT EXISTS sessions(" \ "session_id TEST PRIMARY KEY," \ "session_unique_ref TEXT UNIQUE NOT NULL," \ "user_id INTEGER NOT NULL," \ "session_ip TEXT NOT NULL," \ "session_data TEXT," \ "session_state TEXT NOT NULL," \ "session_last_action_date INTEGER NOT NULL," \ "FOREIGN KEY(user_id) REFERENCES users(user_id)" \ ");" create_raw_mail_table_sql = "CREATE TABLE IF NOT EXISTS raw_mail (" \ "raw_id INTEGER PRIMARY KEY," \ "from_id INTEGER NOT NULL," \ "raw_mail TEXT NOT NULL," \ "raw_date_sent INTEGER NOT NULL," \ "FOREIGN KEY(from_id) REFERENCES users(user_id)" \ ");" create_mail_table_sql = "CREATE TABLE IF NOT EXISTS mail_store (" \ "mail_id INTEGER PRIMARY KEY," \ "raw_id INTEGER NOT NULL," \ "from_id INTEGER NOT NULL," \ "to_id INTEGER," \ "mail_subject TEXT NOT NULL," \ "mail_body TEXT NOT NULL," \ "mail_read TEXT DEFAULT 0," \ "mail_date_sent INTEGER NOT NULL," \ "FOREIGN KEY(from_id) REFERENCES users(user_id)," \ "FOREIGN KEY(to_id) REFERENCES users(user_id)" \ ");" create_server_log_sql = "CREATE TABLE IF NOT EXISTS server_logs(" \ "log_id INTEGER PRIMARY KEY," \ "session_ref TEXT," \ "log_action_dir TEXT," \ "log_action TEXT," \ "log_client_ip TEXT," \ "log_date INTEGER" \ ");" # Create the system settings and set the actual system settings needed if db_connection.do_generic(create_system_settings_sql): db_connection.set_system_setting("SERVER_DOMAIN", "server.local") db_connection.set_system_setting("MOTD", "Hello welcome to my SMTP server!") help_main_body = "This is the main help text, i cant think of anything to put here,\n" help_main_body += "so i'll fill it with some lorem ipsums, but you can also look at \n" help_main_body += "sub help files using [USEFUL], [LOREM] or [MAIL]." db_connection.set_system_setting("HELPMAIN", help_main_body) help_lorem_body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sit amet arcu tortor. \n" \ "Maecenas ante lacus, vehicula id volutpat sed, auctor eget nunc. Mauris id consequat \n" \ "augue. Sed molestie diam quis sapien vehicula, non ultricies tellus interdum. \n" \ "Pellentesque vel dui enim. Integer sed magna blandit, consectetur felis id, lacinia nisi. \n" \ "Phasellus luctus imperdiet orci quis hendrerit. Phasellus vel mattis libero. Suspendisse \n" \ "ornare porttitor dolor, eu aliquet quam laoreet at. Integer tincidunt pretium pretium. \n" \ "In auctor sapien a erat suscipit, quis semper ex fermentum. Vestibulum bibendum nulla ut \n" \ "vestibulum fringilla. Fusce est risus, rhoncus feugiat tellus vitae, feugiat finibus \n" \ "lacus. Maecenas gravida ornare imperdiet. Mauris nec est nec mi dapibus dictum viverra \n" \ "at libero. Morbi blandit sed lacus a semper." db_connection.set_system_setting("HELPLOREM", help_lorem_body) help_mail_body = "When you compose a mail message it will take you through 3 sections, the first is to \n" \ "collect the email addresses you wish to send to, \n" \ "leaving the prompt blank will continue to the next section.\n" \ "The next section will ask you to enter a subject for your email, this is straight forward.\n" \ "The last section will allow you to enter the body of the email, enter will start a new \n" \ "paragraph, if you leave the input blank and hit enter the email will attempt to send." db_connection.set_system_setting("HELPMAIL", help_mail_body) help_useful_body = "Test Email account email address list:\n" \ "admin@server.local,\n" \ "testuser@server.local,\n" \ "craig.smith@server.local,\n" \ "g.lemon@server.local,\n" \ "v.king@server.local,\n" \ "j.chong@server.local" \ db_connection.set_system_setting("HELPUSEFUL", help_useful_body) # Create the user table and generate the admin and some fake users if db_connection.do_generic(create_user_table_sql): if db_connection.do_generic(create_user_login_attempt_table) and db_connection.do_generic(create_session_table): # Set up users and do some class tests admin_user = User("admin") if admin_user.create_user("Jeff", "Jones", "admin", "admin@server.local", "Admin"): print("Admin user created.") normal_user = User("testuser") if normal_user.create_user("Adam", "David", "password", "testuser@server.local"): print("Test user created.") user_1 = User("user1") if user_1.create_user("Craig", "Smith", "user", "craig.smith@server.local"): print("Test user created.") user_2 = User("user2") if user_2.create_user("Greg", "Lemon", "user", "g.lemon@server.local"): print("Test user created.") user_3 = User("user3") if user_3.create_user("Vadim", "King", "user", "v.king@server.local"): print("Test user created.") user_4 = User("user4") if user_4.create_user("Jim", "Chong", "user", "j.chong@server.local"): print("Test user created.") print(admin_user.__dict__) print(normal_user.__dict__) user_test = User("admin") for i in range(0, 5): print(user_test.login("nothing", "127.0.0.1")) print(f"Attempt {i+1}, count: {user_test.bad_login_attempt_count()}\n") print(user_test.login("admin", "127.0.0.1")) print(f"Attempt 6, count: {user_test.bad_login_attempt_count()}\n") print("Unlocking the user account\n") user_test.unlock() print(user_test.login("admin", "127.0.0.1")) print(f"Attempt 7, count: {user_test.bad_login_attempt_count()}\n") print(user_test.__dict__) user_test.email_address = "admin@email.com" user_test.save_current_changes() print(user_test.__dict__) user_test.email_address = "admin@server.local" user_test.save_current_changes() # Create the mail store and raw mail tables for holding the emails if db_connection.do_generic(create_raw_mail_table_sql): if db_connection.do_generic(create_mail_table_sql): if db_connection.do_generic(create_server_log_sql): pass # Testing the Encrypting and RSA class crypto_test = Crypto.Receiver() crypto_test.generate_keys() print(f"Public: {crypto_test.get_public_key_pair()}") print(f"Private: {crypto_test.get_private_key_pair()}") decrypto_test = Crypto.Transmitter(crypto_test.get_public_key_pair()) print(f"Public Sent: {crypto_test.get_public_key_pair(False)}") print(f"Public Recv: {decrypto_test.get_current_key_pair()}") print("String in: Hello world!") enc_string = decrypto_test.encrypt_string(b"Hello world!") print(f"Encrypted String: {enc_string}") dec_string = crypto_test.decrypt_string(enc_string.decode()) print(f"Decrypted String: {dec_string}") hash_test_string = "Hello World!" py_hash = hashlib.sha256(hash_test_string.encode()) hash_test_a = py_hash.hexdigest() custom_hash = SHA256Custom.SHA256Custom(hash_test_string.encode()) hash_test_b = custom_hash.hexdigest() print(f"Input string: {hash_test_string}") print(f"Python hash: {hash_test_a}") print(f"Custom hash: {hash_test_b}") if hash_test_a == hash_test_b: print("Matching Hash") else: print("Non matching Hash")