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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python

import os
import sys
import logging

import conf
import common.db
import common.chat
import common.makeuser
import common.studpath

# usage:
#   usrmgt-server <port> <magic>

# User management operations:
#   - Create local user
#   - [Re]Create jail for a user
#       - Create a svn repository for a user
#           - create repository
#           - svn config
#           - svn auth
#       - /etc/passwd entry
#   - Disable a user's account
#   - Enable a user's account
#   - Remove a user
#   - Rebuild svn config
#   - Rebuild svn auth file
#   - Rebuild passwd + push to nodes.

def activate_user(props):
    """Create the on-disk stuff for the given user.
       Sets the state of the user in the db from pending to enabled.
       Expected properties:
        login       - the user name for the jail
                      STRING REQUIRED
       Return Value: None
    """

    os.umask(0022) # Bad, but start_server sets it worse.

    login = props['login']

    db = common.db.DB()

    try:

        # FIXME: check we're pending

        details = db.get_user(login)

        # make svn config/auth

        repopath = os.path.join(conf.svn_repo_path, 'users', login)
        logging.debug("Creating user's Subversion repository")
        common.makeuser.make_svn_repo(repopath, throw_on_error=False)

        logging.debug("Rebuilding Subversion authorization config")
        try:
            common.makeuser.rebuild_svn_config()
        except:
            logging.warning('Rebuild of Subversion authorization config failed!')

        logging.debug("Adding Subversion authentication")
        passwd = common.makeuser.make_svn_auth(login, throw_on_error=False)
        logging.debug("passwd: %s" % passwd)

        logging.debug("Creating jail")
        common.makeuser.make_jail(login, details.unixid, svn_pass=passwd)

        logging.info("Enabling user")
        db.update_user(login, state='enabled')

        return {"response": "okay"}

    finally:
        db.close()

actions = {
        'activate_user':activate_user,
    }

def initializer():
    try:
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
        pidfile.write('%d\n' % os.getpid())
        pidfile.close()
    except IOError, (errno, strerror):
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
        sys.exit(1)

def dispatch(props):
    logging.debug(repr(props))
    action = props.keys()[0]
    return actions[action](props[action])

if __name__ == "__main__":
    if len(sys.argv) <3:
        print >>sys.stderr, "Usage: usrmgt-server <port> <magic>"
        sys.exit(1)
    port = int(sys.argv[1])
    magic = sys.argv[2]

    pid = os.getpid()

    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
    logging.info("Starting usrmgt server on port %d (pid = %d)" % (port, pid))

    common.chat.start_server(port, magic, True, dispatch, initializer)