diff --git a/asrc-server.py b/asrc-server.py index 7ec77d5..6a29962 100755 --- a/asrc-server.py +++ b/asrc-server.py @@ -25,8 +25,8 @@ # aSRC (Aliased Server Remote Control) # - SERVER - # -# program version: 0.0.0.20130426 -# protocol version: 0.2.20130423 +# program version: 0.0.0.20130617 +# protocol version: 0.2.20130617 # # @@ -53,12 +53,16 @@ class ThreadedRequestHandler(socketserver.StreamRequestHandler): # send motd self.request.sendall(bytes((comm.motd(MOTD) + "\n"), ENCODING)) - # Receive data - self.data = str(self.rfile.readline().strip(), ENCODING) + repeat = True - # content handler - self.request.sendall(bytes( - comm.command(str(self.client_address), self.data), ENCODING)) + while repeat: + # Receive data + self.data = str(self.rfile.readline().strip(), ENCODING) + + if self.data == "exit": repeat = False + # content handler + self.request.sendall(bytes( + comm.command(str(self.client_address), self.data), ENCODING)) class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): @@ -67,10 +71,10 @@ class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): def main(): # import global settings - global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING + global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING, PASSWORD - comm.init(ServerVersion, ProtocolVersion, VERBOSITY, aliases) + comm.init(ServerVersion, ProtocolVersion, VERBOSITY, aliases, PASSWORD) # parse arguments parser = argparser @@ -170,6 +174,9 @@ if __name__ == '__main__': # Encoding to be used when communicating with a client ENCODING = 'utf-8' + # SHA512-encoded Password for authentication with the server + PASSWORD = 'a52fb4e552326fd8216f52a96f3d037309ef25acb22e5ead60bf258d2ea6652ab9fd5ab1117eb4bafe7476224d081ad7737132c4c096e9e8287a3c3f9d7d14f6' + # Dictionary of aliases. Use python syntax. You can use # the variable send for text to send to the client. # diff --git a/doc/protocol_reference.odt b/doc/protocol_reference.odt index fa01699..b98d685 100644 Binary files a/doc/protocol_reference.odt and b/doc/protocol_reference.odt differ diff --git a/include/__init__.py b/include/__init__.py index c891f96..02d3aa8 100644 --- a/include/__init__.py +++ b/include/__init__.py @@ -1,3 +1,4 @@ from .argparser import argparser +from .auth import auth from .comm import comm from .statuscodes import statuscodes diff --git a/include/auth.py b/include/auth.py new file mode 100644 index 0000000..73712ed --- /dev/null +++ b/include/auth.py @@ -0,0 +1,23 @@ +# includes/auth.py +# +# module version: 0.0.20130617 +# for protocol version 0.2.20130617 +# + +class auth: + passwd = "" + + def init(password): + #from Crypto.Hash import Hash + import hashlib + + global passwd + passwd = password + + + def check_passwd(incpass): + """ + checks a given password + """ + if hashlib.sha512(bytearray(incpass)).hexdigest() == passwd: return True + else: return False diff --git a/include/comm.py b/include/comm.py index 52cc7ad..20a4754 100644 --- a/include/comm.py +++ b/include/comm.py @@ -1,11 +1,12 @@ # includes/comm.py # -# module version: 0.0.20130426 -# for protocol versions 0.2.20130423 and later +# module version: 0.0.20130617 +# for protocol version 0.2.20130617 # from .statuscodes import statuscodes +from .auth import auth class comm: @@ -16,16 +17,22 @@ class comm: protocol_version = "" verbosity = 0 + # initializes global settings - def init(ServerVersion, ProtocolVersion, Verbosity, Aliases): + def init(ServerVersion, ProtocolVersion, Verbosity, Aliases, Password): """ - makes settings aviable in this class + makes settings aviable in this class and initialize auth """ global aliases, server_version, protocol_version, verbosity aliases = Aliases server_version = ServerVersion protocol_version = ProtocolVersion verbosity = Verbosity + + auth.init(Password) + + def authenticate(): + pass # builds an header def header(CodeList, AdditionalHeaderLines = ""): @@ -39,10 +46,10 @@ class comm: for i in range(0, len(CodeList)): ret += CodeList[int(i)] + " " +\ statuscodes.description['s' + CodeList[int(i)]] + "\n" - ret += "ServerVersion: " + server_version + "\n" +\ - AdditionalHeaderLines + "\n\n" + ret += AdditionalHeaderLines + "\n\n" return ret + # formats a massage def encode_message(text): """ @@ -50,6 +57,7 @@ class comm: """ return text.replace('{', '\{').replace('}', '\}') + # "deformats" a message def decode_message(text): """ @@ -57,12 +65,14 @@ class comm: """ return text.replace('\{', '{').replace('\}', '}') + # returns the motd def motd(motd): """ builds and returns a motd package """ - return comm.header(['202', '003']) + comm.encode_message(motd) + return comm.header(['202', '003']) + comm.encode_message(motd) + "\n{END}" + # handles the content def command(client_address, data): @@ -80,12 +90,18 @@ class comm: if verbosity >= 2: print("Got valid service command from" + str(client_address) + ": ", data) - ret = ret +\ - "202 Valid Service Command\n"\ - "002 Version\n"\ - "ServerVersion:" + server_version + "\n"\ + hdr = comm.header(['202','002']) + ret = "ServerVersion:" + server_version + "\n"\ "ProtocolVersion:" + protocol_version + "\n" + # if it's 'exit', then send .. nothing! + elif data == 'exit': + if verbosity >= 2: print("Got valid service command from" + + str(client_address) + ": ", data) + + hdr = comm.header(['202','004']) + ret = "closing connection..." + # if it's 'help', give a little help elif data == 'help': @@ -93,10 +109,10 @@ class comm: + str(client_address) + ": ", data) # send status code - ret = ret + "202 Valid Service Command\n\n" + hdr = comm.header(['202']) # send the list of aliases - ret = ret + "Aviable aliases:\n" + ret = "Aviable aliases:\n" for i in aliases.keys(): ret = ret + str(i) + "\n" @@ -104,9 +120,9 @@ class comm: elif data in aliases: # send status code - ret = ret + "201 Valid Command\n\n" + hdr = comm.header(['201']) - # ohmagawd! a debug message!!1! + # ohmagawd! a debug message!!1! (sry...) if verbosity >= 2: print("Got valid command from" + str(client_address) + ": ", data) @@ -125,11 +141,11 @@ class comm: else: # send status code - ret = ret + "203 Invalid Command\n" + hdr = comm.header(['203']) if verbosity >= 2: print("Got invalid command from", str(client_address), ": ", data) - ret = ret + "{END}\n" + ret = comm.encode_message(ret) + "{END}\n\n" - return ret \ No newline at end of file + return hdr + ret diff --git a/include/statuscodes.py b/include/statuscodes.py index 378ceb9..d3a9ad8 100644 --- a/include/statuscodes.py +++ b/include/statuscodes.py @@ -1,7 +1,7 @@ # include/statuscodes.py # -# module version: 1.0.20130426 -# for protocol versions 0.2.20130423 and later +# module version: 1.0.20130617 +# for protocol version 0.2.20130617 # @@ -12,6 +12,7 @@ class statuscodes: s001 = "OK", s002 = "Version", s003 = "MOTD", + s004 = "Exit", # 100 authentication and maintenance s101 = "Challenge", s102 = "Success",