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

« back to all changes in this revision

Viewing changes to usrmgt/usrmgt-server

  • Committer: drtomc
  • Date: 2008-02-06 03:21:36 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:432
usrmgt: more work on this. Still some work to go.
console: refactored to use the ivle-chat protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
 
3
import sys
 
4
 
 
5
import common.chat
 
6
import common.makeuser
 
7
 
3
8
# usage:
4
9
#   usrmgt-server <port> <magic>
5
10
 
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 = ''
 
11
def create_user(props):
 
12
 
 
13
    if 'uid' not in props:
 
14
        uid = invent-uid
 
15
        props['uid'] = uid
 
16
 
 
17
    common.makeuser.make_user_db(props['username'], props['uid'],
 
18
                                 props['password'], props['nick'],
 
19
                                 props['fullname'], props['rolenm'],
 
20
                                 props['studentid'])
 
21
 
 
22
    common.makeuser.make_jail(props['username'], props['uid'])
 
23
 
 
24
    # eventually, we're going to want to grant shell access, in which
 
25
    # case we'll need to add the uid to the password file. Magic to do
 
26
    # that should go here.
 
27
 
 
28
    return props['uid']
27
29
 
28
30
if __name__ == "__main__":
29
31
    port = int(sys.argv[1])
30
32
    magic = sys.argv[2]
31
33
 
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()
 
34
    common.chat.start_server(port, magic, False, create_user)