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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#!/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()
def create_group_repository(props):
"""Creates on disk repository for the given group
Expected properties:
subj_short_name, year, semester, groupnm
Return value:
response (okay, failure)
"""
subj_short_name = props['subj_short_name']
year = props['year']
semester = props['semester']
groupnm = props['groupnm']
namespace = "_".join([subj_short_name, year, semester, groupnm])
repopath = os.path.join(conf.svn_repo_path, 'groups', namespace)
logging.debug("Creating Subversion repository %s"%repopath)
try:
common.makeuser.make_svn_repo(repopath)
except Exception, e:
logging.error("Failed to create Subversion repository %s: %s"%
(repopath,repr(e)))
return {'response': 'failure', 'msg': repr(e)}
return {'response': 'okay'}
actions = {
'activate_user':activate_user,
'create_group_repository':create_group_repository,
}
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__":
pid = os.getpid()
logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
logging.info("Starting usrmgt server on port %d (pid = %d)" %
(conf.usrmgt_port, pid))
common.chat.start_server(conf.usrmgt_port, conf.usrmgt_magic, True, dispatch, initializer)
|