def main(): # Creating Connection to Database conn = create_connection() # Binding Server sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print("Starting AuthClient on port %s", str(constants.KDC_CLIENT_VERIFICATION[1])) sock.bind(constants.KDC_CLIENT_VERIFICATION) sock.listen() while (True): print("Waiting for a connection....") connection, client_addr = sock.accept() # Receiving Input input_stream = connection.recv(4096) request = crypto.unserial(input_stream) user_id = request[constants.USER_ID] password = request[constants.PASSWORD] # Verifies Client Details with Database client_secret_key = authenticate(user_id, password, conn) request[constants.USER_ID] = user_id if client_secret_key == 0: request[constants.STATUS] = constants.FAILURE else: request[constants.STATUS] = constants.ACCOUNT_PRESENT # Sending Response data_stream = crypto.serial(request) connection.send(data_stream) connection.close()
def create_server_response(ss_key, user_key): # Response for sending Session key to Client data = {} data[constants.SESSION_SERVICE_KEY] = ss_key # Encrpyting with Users_Key return crypto.encrypt(crypto.serial(data), user_key)
def exposed_cp(self, filename, new_path): # Copy File print("cp") try: newpath = new_path copyfile(self.create_path() + '/' + filename, newpath) return crypto.encrypt(crypto.serial("ACK"), self.session_key) except: return None
def exposed_make_dir(self, foldername): # Make New Directory old_path = self.create_path() path = old_path + '/' + foldername path = path[2:] # print(path) if not os.path.exists(path): os.makedirs(path) return crypto.encrypt(crypto.serial(self.current_dir), self.session_key)
def exposed_cdbackward(self): # Move one step backward print("cd ..") if (len(self.current_dir) == 2): return None self.current_dir = self.current_dir[:len(self.current_dir) - 1] return crypto.encrypt(crypto.serial(self.current_dir), self.session_key)
def create_service_ticket(userid, serviceid, ss_key, service_key, user_ip): # Generating Service Ticket for file server data = {} data[constants.USER_ID] = userid data[constants.SERVICE_ID] = serviceid data[constants.SESSION_SERVICE_KEY] = ss_key data[constants.USER_IP] = user_ip # Encrypt Service ticket with file_server.py's Key return crypto.encrypt(crypto.serial(data), service_key)
def exposed_cdforward(self, foldername): # move to a folder print("cd") old_path = self.create_path() new_path = old_path + '/' + foldername if not os.path.exists(new_path): return None self.current_dir.append(foldername) return crypto.encrypt(crypto.serial(self.current_dir), self.session_key)
def exposed_cat(self, filename): # Return file content print("cat") newpath = self.create_path() + '/' + filename try: file_content = [] with open(newpath) as myfile: for line in myfile: file_content.append(line) return crypto.encrypt(crypto.serial(file_content), self.session_key) except: return None
def exposed_create_file(self, filename): # Create New File print("nano") path = self.create_path() + '/' + filename path = path[2:] # print(path) # Creating File with open(path, 'w') as myfile: pass # Opening File for edit os.system('gedit ' + path) # print("File created at " + path + "(You Can Edit)") return crypto.encrypt(crypto.serial(self.current_dir), self.session_key)
def handle_connection_request(user_request, connection, client_addr): # Handles Specific Request to obtain session key for a particular server user_id = user_request[constants.USER_ID] # Check in database if user id exists conn = database_init() crs = conn.cursor() c = crs.execute('Select * from CLIENT_DATA where USER_ID =?', (user_id, )) res = c.fetchone() if (res == None): print("USER ID doesn't exist") data = {} data[constants.STATUS] = False conn.close() else: service_id = user_request[constants.SERVICE_ID] crs = conn.cursor() c = crs.execute('Select * from SERVICE_DATA where SERVICE_ID =?', (service_id, )) res1 = c.fetchone() if (res1 == None): data = {} data[constants.STATUS] = False conn.close() else: data = {} data[constants.STATUS] = True conn.close() # Getting Keys client_key = res[2] session_key = crypto.get_key() service_key = res1[1] # Creating Response server_response = create_server_response(session_key, client_key) service_ticket = create_service_ticket(user_id, service_id, session_key, service_key, client_addr) data[constants.SERVER_RESPONSE] = server_response data[constants.SERVICE_TICKET] = service_ticket # Sending Response data_stream = crypto.serial(data) connection.send(data_stream) print("Responded to ", user_id)
def exposed_ls(self): # Print all files and Folder print("ls") path = self.create_path()[2:] directories = [ name for name in os.listdir(path) if os.path.isdir(join(path, name)) ] files = [f for f in listdir(path) if isfile(join(path, f))] # Sending result as tuple where x[1] =1 for directory and 0 for file all_combined = [] for x in directories: all_combined.append((x, 1)) for x in files: all_combined.append((x, 0)) all_combined.sort() return crypto.encrypt(crypto.serial(all_combined), self.session_key)
def get_session_key(file_server_id, file_server_port, user_id, client_key): # Provides Session Key for accessing a file server client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # client.bind((hostname, port)) client.connect(constants.KDC) data = {} data[constants.USER_ID] = user_id data[constants.SERVICE_ID] = file_server_id data[constants.REQUEST_TYPE] = constants.SPECIFIC data_stream = crypto.serial(data) client.send(data_stream) print("Request sent to the Authentication server for Session Key") print("Establishing Session Key Using Needham Schroder Protocol") response_stream = client.recv(4096) response = crypto.unserial(response_stream) if not response[constants.STATUS]: print("Bad Request") exit(1) user_response_encoded = response[constants.SERVER_RESPONSE] service_ticket_encoded = response[constants.SERVICE_TICKET] user_response = crypto.unserial( crypto.decrypt(user_response_encoded, client_key)) session_key = user_response[constants.SESSION_SERVICE_KEY] client.close() client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(('localhost', file_server_port)) client.send(service_ticket_encoded) response_service_stream = client.recv(4096) response_service = crypto.unserial( crypto.decrypt(response_service_stream, session_key)) if response_service[constants.SERVICE_ID] != file_server_id: print("Could Not Establish Communication with File server") return False, None rpc_port = response_service[constants.RPC_PORT] return True, session_key, rpc_port
def get_access_init(user_id): # Returns List of all active File servers client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # client.bind((hostname, port)) client.connect(constants.KDC) # Authentication server print("Connected to Authentication Server") data = {} data[constants.USER_ID] = user_id data[constants.REQUEST_TYPE] = constants.INIT data_stream = crypto.serial(data) client.send(data_stream) print("Request sent to the Authentication server for Initialization") response_stream = client.recv(4096) response= crypto.unserial(response_stream) if response[constants.STATUS]==False: print("Could Not Verify with Server") exit(1) active_file_servers = response[constants.ACTIVE_SERVERS] client.close() return active_file_servers
def verify_and_update(connection, client_addr, server_id, server_key, rpc_port): # Handles Client Authorization Request print("Connection from ", client_addr) data = connection.recv(4096) ticket = get_service_ticket(data, server_key) if (ticket[constants.SERVICE_ID] != server_id): raise Exception("Wrong Message Received") update_my_database(ticket[constants.USER_ID], ticket[constants.SESSION_SERVICE_KEY], server_id) # Acknowledgement response = {} response[constants.SERVICE_ID] = ticket[constants.SERVICE_ID] response[constants.RPC_PORT] = rpc_port connection.send( crypto.encrypt(crypto.serial(response), ticket[constants.SESSION_SERVICE_KEY])) connection.close() return True
def handle_init_request(user_request, connection): # Handles Active File Server List Request user_id = user_request[constants.USER_ID] # Check in database if user id exists conn = database_init() crs = conn.cursor() c = crs.execute('Select * from CLIENT_DATA where USER_ID =?', (user_id, )) res = c.fetchone() conn.close() if (res == None): print("USER ID doesn't exist") data = {} data[constants.STATUS] = False else: print("User Verified") data = {} data[constants.STATUS] = True data[constants.ACTIVE_SERVERS] = get_active_file_servers() # Sending Response data_stream = crypto.serial(data) connection.send(data_stream) print("Responded to ", user_id)
def exposed_pwd(self): # Return Present Working Directory of the Client at the file server print("pwd") return crypto.encrypt(crypto.serial(self.current_dir), self.session_key)