2017-09-30 12:51:23 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-09-30 15:02:06 +00:00
|
|
|
|
|
|
|
# Author: Bandie Canis
|
|
|
|
# License: 2-Clause BSD License
|
|
|
|
|
2018-02-01 13:09:35 +00:00
|
|
|
import sys, ssl, socket, os
|
2017-09-30 12:51:23 +00:00
|
|
|
import configparser
|
|
|
|
|
|
|
|
|
2017-12-27 12:40:17 +00:00
|
|
|
global exitcode
|
|
|
|
exitcode = 1
|
|
|
|
|
2017-09-30 12:51:23 +00:00
|
|
|
def readConfig():
|
2018-02-01 13:09:35 +00:00
|
|
|
|
|
|
|
if(os.name == 'nt'):
|
|
|
|
CONFIG = "ding_client.win.cfg"
|
|
|
|
else:
|
|
|
|
CONFIG = "ding_client.cfg"
|
|
|
|
|
2017-09-30 12:51:23 +00:00
|
|
|
cfg = configparser.SafeConfigParser()
|
|
|
|
try:
|
|
|
|
cfg.read(CONFIG)
|
|
|
|
|
|
|
|
global host, port, cafile, certfile, keyfile
|
|
|
|
|
|
|
|
host = cfg.get("Client", "host")
|
|
|
|
port = int(cfg.get("Client", "port"))
|
|
|
|
|
|
|
|
cafile = cfg.get("Client", "cafile")
|
|
|
|
certfile = cfg.get("Client", "certfile")
|
|
|
|
keyfile = cfg.get("Client", "keyfile")
|
|
|
|
except configparser.NoSectionError:
|
|
|
|
print("No suitable config found. Expecting some config in", CONFIG)
|
|
|
|
quit(3)
|
|
|
|
|
|
|
|
|
|
|
|
def send(conn, cmd):
|
|
|
|
|
|
|
|
conn.connect((host, port))
|
|
|
|
buf = conn.recv(1024)
|
|
|
|
if(buf == b"OK 1337\n"):
|
|
|
|
conn.sendall(cmd)
|
|
|
|
buf = conn.recv(1024)
|
|
|
|
if(buf == b"OK CMD"):
|
|
|
|
exitcode = 0
|
|
|
|
elif(buf == b"ERR NO_CMD"):
|
2018-02-01 13:09:35 +00:00
|
|
|
print("Error. Server said: The command isn't set on the server.", file=sys.stderr)
|
2017-09-30 12:51:23 +00:00
|
|
|
exitcode = 1
|
|
|
|
elif(buf == b"ERR CMD_ERR"):
|
|
|
|
print("Error. Server said: The command doesn't work because the file doesn't exist on the server.")
|
|
|
|
exitcode = 2
|
2018-02-01 00:36:25 +00:00
|
|
|
elif(buf == b"ERR PW"):
|
|
|
|
print("Error. Password required. The password was wrong.")
|
|
|
|
exitcode = 4
|
|
|
|
elif(b"OK PW" in buf):
|
|
|
|
bufr=str(buf.decode('utf-8'))
|
|
|
|
print("Password accepted. Timeout:", bufr.replace("OK PW ",""), "seconds.")
|
2017-09-30 12:51:23 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
conn.sendall(b"NO.")
|
|
|
|
print("The server seems to be crazy. Nothing sent.", file=sys.stderr)
|
|
|
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
|
|
|
|
context.verify_mode = ssl.CERT_REQUIRED
|
|
|
|
context.load_verify_locations(cafile)
|
|
|
|
context.load_cert_chain(certfile=certfile, keyfile=keyfile)
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
print(e)
|
|
|
|
print("Please check your paths in the config file. (Have you forgotten to generate the Certificates?)", sep="", file=sys.stderr)
|
|
|
|
quit(2)
|
|
|
|
|
2017-10-02 22:05:23 +00:00
|
|
|
if(":" in host):
|
|
|
|
conn = context.wrap_socket(socket.socket(family=socket.AF_INET6))
|
|
|
|
else:
|
|
|
|
conn = context.wrap_socket(socket.socket(family=socket.AF_INET))
|
2017-09-30 12:51:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
send(conn, bytes(sys.argv[1], sys.stdin.encoding))
|
|
|
|
except IndexError:
|
|
|
|
print(sys.argv[0], ": Missing argument.\nSyntax: ", sys.argv[0], " <COMMAND>", file=sys.stderr)
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
print("Connection refused.", file=sys.stderr)
|
|
|
|
quit(1)
|
|
|
|
except ssl.SSLError:
|
|
|
|
print("Wrong certificate.", file=sys.stderr);
|
|
|
|
quit(3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(__name__ == "__main__"):
|
|
|
|
readConfig()
|
|
|
|
main()
|
|
|
|
quit(exitcode)
|
|
|
|
|