~azzar1/unity/add-show-desktop-key

404 by drtomc
Version 0 of the usrmgt server.
1
#!/usr/bin/python
2
3
# usage:
4
#   usrmgt-server <port> <magic>
5
6
import cjson
7
import cStringIO
8
import md5
9
import os
10
import Queue
11
import signal
12
import socket
13
import sys
14
15
def daemonize():
16
    if os.fork():   # launch child and...
17
        os._exit(0) # kill off parent
18
    os.setsid()
19
    if os.fork():   # launch child and...
20
        os._exit(0) # kill off parent again.
21
    os.umask(077)
22
23
# The global 'magic' is the secret that the client and server share
24
# which is used to create and md5 digest to authenticate requests.
25
# It is assigned a real value at startup.
26
magic = ''
27
28
if __name__ == "__main__":
29
    port = int(sys.argv[1])
30
    magic = sys.argv[2]
31
32
    # Attempt to open the socket.
33
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
34
    s.bind(('', port))
35
    s.listen(1)
36
37
    # Excellent! It worked. Let's turn ourself into a daemon,
38
    # then get on with the job of being a python interpreter.
39
    daemonize()
40
41
    while True:
42
        (conn, addr) = s.accept()
43
        try:
44
            # Grab the input
45
            buf = cStringIO.StringIO()
46
            blk = conn.recv(1024)
47
            while blk:
48
                buf.write(blk)
49
                try:
50
                    blk = conn.recv(1024, socket.MSG_DONTWAIT)
51
                except:
52
                    # Exception thrown if it WOULD block (but we
53
                    # told it not to wait) - ie. we are done
54
                    blk = None
55
            inp = buf.getvalue()
56
            msg = cjson.decode(inp)
57
            
58
            # Check that the message is 
59
            digest = md5.new(msg['login'] + magic).digest().encode('hex')
60
            if msg['digest'] != digest:
61
                conn.close()
62
                continue
63
64
            os.system("useradd '%s'" % msg['login'])
65
            for node in [4,5]:
66
                os.system("scp /etc/passwd informatics%s:/etc/passwd" % str(n))
67
68
            conn.sendall(cjson.encode(True))
69
            conn.close()
70
        except Exception, e:
71
            conn.close()