diff options
author | Miklos Vajna <vmiklos@frugalware.org> | 2008-01-12 02:36:54 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@frugalware.org> | 2008-01-12 02:36:54 +0100 |
commit | 5245e9d785fced3ef1232e47f1ee5a7fbef396b4 (patch) | |
tree | 3aba201db8c5c2ac134f63e14388711f77383e67 | |
parent | a0b206b4506ec4f1c30d82fab6a054e162bf7a14 (diff) |
skyped: add authentication support
this is not yet ssl, but better than nothing
-rw-r--r-- | skype/skyped.conf.dist | 4 | ||||
-rw-r--r-- | skype/skyped.py | 81 |
2 files changed, 65 insertions, 20 deletions
diff --git a/skype/skyped.conf.dist b/skype/skyped.conf.dist new file mode 100644 index 00000000..43faa0c6 --- /dev/null +++ b/skype/skyped.conf.dist @@ -0,0 +1,4 @@ +[skyped] +username = john +# use `echo -n foo|sha1sum` to generate this hash for your password +password = 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 diff --git a/skype/skyped.py b/skype/skyped.py index 007d867c..c2b102a3 100644 --- a/skype/skyped.py +++ b/skype/skyped.py @@ -34,21 +34,25 @@ import socket import getopt import Skype4Py import threading +import sha +from ConfigParser import ConfigParser __version__ = "0.1.1" SKYPE_SERVICE = 'com.Skype.API' CLIENT_NAME = 'SkypeApiPythonShell' -# well, this is a bit hackish. we store the socket of the last connected client -# here and notify it. maybe later notify all connected clients? -conn = None - def input_handler(fd, io_condition): - input = fd.recv(1024) - for i in input.split("\n"): - skype.send(i.strip()) - return True + global options + if options.buf: + for i in options.buf: + skype.send(i.strip()) + options.buf = None + else: + input = fd.recv(1024) + for i in input.split("\n"): + skype.send(i.strip()) + return True def idle_handler(skype): try: @@ -69,11 +73,28 @@ def server(host, port): gobject.io_add_watch(sock, gobject.IO_IN, listener) def listener(sock, *args): - global conn - conn, addr = sock.accept() - fileno = conn.fileno() - gobject.io_add_watch(conn, gobject.IO_IN, input_handler) - return True + global options + options.conn, addr = sock.accept() + lines = options.conn.recv(512).split('\n') + ret = 0 + nlines = [] + for i in lines: + if i.startswith("USERNAME") and i.split(' ')[1].strip() == options.config.username: + ret += 1 + elif i.startswith("PASSWORD") and sha.sha(i.split(' ')[1].strip()).hexdigest() == options.config.password: + ret += 1 + else: + nlines.append(i) + del lines + if ret == 2: + dprint("Username and password OK.") + options.buf = nlines + input_handler(None, None) + gobject.io_add_watch(options.conn, gobject.IO_IN, input_handler) + return True + else: + dprint("Username and/or password WRONG.") + return False def dprint(msg): global options @@ -88,7 +109,7 @@ class SkypeApi(): self.skype.Attach() def recv(self, msg_text): - global conn + global options if msg_text == "PONG": return if "\n" in msg_text: @@ -109,9 +130,9 @@ class SkypeApi(): # everybody will be happy e = i.encode('UTF-8') dprint('<< ' + e) - if conn: + if options.conn: try: - conn.send(e + "\n") + options.conn.send(e + "\n") except IOError, s: dprint("Warning, sending '%s' failed (%s)." % (e, s)) @@ -131,12 +152,19 @@ class SkypeApi(): class Options: def __init__(self): + self.cfgpath = "/etc/skyped.conf" self.daemon = True self.debug = False self.help = False self.host = "0.0.0.0" self.port = 2727 self.version = False + # well, this is a bit hackish. we store the socket of the last connected client + # here and notify it. maybe later notify all connected clients? + self.conn = None + # this will be read first by the input handler + self.buf = None + def usage(self, ret): print """Usage: skyped [OPTION]... @@ -144,22 +172,25 @@ class Options: skyped is a daemon that acts as a tcp server on top of a Skype instance. Options: + -c --config path to configuration file (default: %s) -d --debug enable debug messages -h --help this help -H --host set the tcp host (default: %s) -n --nofork don't run as daemon in the background -p --port set the tcp port (default: %d) - -v --version display version information""" % (self.host, self.port) + -v --version display version information""" % (self.cfgpath, self.host, self.port) sys.exit(ret) if __name__=='__main__': options = Options() try: - opts, args = getopt.getopt(sys.argv[1:], "dhH:np:v", ["daemon", "help", "host=", "nofork", "port=", "version"]) + opts, args = getopt.getopt(sys.argv[1:], "c:dhH:np:v", ["config=", "daemon", "help", "host=", "nofork", "port=", "version"]) except getopt.GetoptError: options.usage(1) for opt, arg in opts: - if opt in ("-d", "--debug"): + if opt in ("-c", "--config"): + options.cfgpath = arg + elif opt in ("-d", "--debug"): options.debug = True elif opt in ("-h", "--help"): options.help = True @@ -176,7 +207,17 @@ if __name__=='__main__': elif options.version: print "skyped %s" % __version__ sys.exit(0) - elif options.daemon: + # parse our config + if not os.path.exists(options.cfgpath): + print "Can't find configuration file at '%s'." % options.cfgpath + print "Use the -c option to specify an alternate one." + sys.exit(1) + options.config = ConfigParser() + options.config.read(options.cfgpath) + options.config.username = options.config.get('skyped', 'username').split('#')[0] + options.config.password = options.config.get('skyped', 'password').split('#')[0] + dprint("Parsing config file '%s' done, username is '%s'." % (options.cfgpath, options.config.username)) + if options.daemon: pid = os.fork() if pid == 0: nullin = file('/dev/null', 'r') |