103
103
os.rename(ivle.conf.svn_conf + ".new", ivle.conf.svn_conf)
104
104
chown_to_webserver(ivle.conf.svn_conf)
106
def rebuild_svn_group_config(store):
106
def rebuild_svn_group_config():
107
107
"""Build the complete SVN configuration file for groups
110
groups = conn.get_all('project_group',
111
['groupid', 'groupnm', 'projectsetid'])
109
112
f = open(ivle.conf.svn_group_conf + ".new", "w")
110
113
f.write("# IVLE SVN Group Repositories Configuration\n")
111
114
f.write("# Auto-generated on %s\n" % time.asctime())
113
for group in store.find(ProjectGroup):
114
offering = group.project_set.offering
115
reponame = "_".join([offering.subject.short_name,
116
offering.semester.year,
117
offering.semester.semester,
117
projectsetid = g['projectsetid']
118
offeringinfo = conn.get_offering_info(projectsetid)
119
subj_short_name = offeringinfo['subj_short_name']
120
year = offeringinfo['year']
121
semester = offeringinfo['semester']
122
reponame = "_".join([subj_short_name, year, semester, g['groupnm']])
119
123
f.write("[%s:/]\n"%reponame)
120
for user in group.members:
121
f.write("%s = rw\n" % user.login)
124
users = conn.get_projectgroup_members(g['groupid'])
126
f.write("%s = rw\n"%u['login'])
124
129
os.rename(ivle.conf.svn_group_conf + ".new", ivle.conf.svn_group_conf)
125
130
chown_to_webserver(ivle.conf.svn_group_conf)
127
def make_svn_auth(store, login, throw_on_error=True):
132
def make_svn_auth(login, throw_on_error=True):
128
133
"""Setup svn authentication for the given user.
129
Uses the given DB store object. Does not commit to the db.
134
FIXME: create local.auth entry
131
136
passwd = md5.new(uuid.uuid4().bytes).digest().encode('hex')
132
137
if os.path.exists(ivle.conf.svn_auth_ivle):
226
239
shutil.move(homebackup, homedir)
227
240
# Change the ownership of all the files to the right unixid
228
241
logging.debug("chown %s's home directory files to uid %d"
229
%(user.login, user.unixid))
230
os.chown(userhomedir, user.unixid, user.unixid)
243
os.chown(userhomedir, uid, uid)
231
244
for root, dirs, files in os.walk(userhomedir):
232
245
for fsobj in dirs + files:
233
os.chown(os.path.join(root, fsobj), user.unixid, user.unixid)
246
os.chown(os.path.join(root, fsobj), uid, uid)
235
248
# No user jail exists
236
249
# Set up the user's home directory
237
250
os.makedirs(userhomedir)
238
251
# Chown (and set the GID to the same as the UID).
239
os.chown(userhomedir, user.unixid, user.unixid)
252
os.chown(userhomedir, uid, uid)
240
253
# Chmod to rwxr-xr-x (755)
241
254
os.chmod(userhomedir, 0755)
243
make_conf_py(user.login, userdir, user.svn_pass)
244
make_etc_passwd(user.login, userdir, ivle.conf.jail_system, user.unixid)
256
# There are 2 special files which need to be generated specific to this
257
# user: ${python_site_packages}/lib/conf/conf.py and /etc/passwd.
258
# "__" username "__" users are exempt (special)
259
if not (username.startswith("__") and username.endswith("__")):
260
make_conf_py(username, userdir, ivle.conf.jail_system, svn_pass)
261
make_etc_passwd(username, userdir, ivle.conf.jail_system, uid)
246
263
return userhomedir
248
def make_conf_py(username, user_jail_dir, svn_pass):
265
def make_conf_py(username, user_jail_dir, staging_dir, svn_pass=None):
250
267
Creates (overwriting any existing file, and creating directories) a
251
268
file ${python_site_packages}/ivle/conf/conf.py in a given user's jail.
252
269
username: Username.
253
270
user_jail_dir: User's jail dir, ie. ivle.conf.jail_base + username
254
svn_pass: User's SVN password.
271
staging_dir: The dir with the staging copy of the jail. (With the
272
template conf.py file).
273
svn_pass: As with make_jail. User's SVN password, but if not supplied,
274
will look up in the DB.
276
template_conf_path = os.path.join(staging_dir,
277
ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
256
278
conf_path = os.path.join(user_jail_dir,
257
279
ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
258
280
os.makedirs(os.path.dirname(conf_path))
260
# In the "in-jail" version of conf, we don't need MOST of the details
261
# (it would be a security risk to have them here).
262
# So we just write root_dir, and jail_base is "/".
263
# (jail_base being "/" means "jail-relative" paths are relative to "/"
264
# when inside the jail.)
266
# XXX: jail_base is wrong and shouldn't be here. Unfortunately, jail code
267
# uses ivle.studpath.url_to_{local,jailpaths}, both of which use
268
# jail_base. Note that they don't use the bits of the return value
269
# that depend on jail_base, so it can be any string.
282
# If svn_pass isn't supplied, grab it from the DB
284
dbconn = ivle.db.DB()
285
svn_pass = dbconn.get_user(username).svn_pass
288
# Read the contents of the template conf file
290
template_conf_file = open(template_conf_path, "r")
291
template_conf_data = template_conf_file.read()
292
template_conf_file.close()
294
# Couldn't open template conf.py for some reason
295
# Just treat it as empty file
296
template_conf_data = ("# Warning: Problem building config script.\n"
297
"# Could not find template conf.py file.\n")
270
299
conf_file = open(conf_path, "w")
271
conf_file.write("""# IVLE jail configuration
273
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
275
# eg. "/" or "/ivle".
276
root_dir = %(root_dir)r
278
# This value is not relevant inside the jail, but must remain for now. See
279
# the XXX in ivle.makeuser.make_conf_py.
282
# The hostname for serving publicly accessible pages
283
public_host = %(public_host)r
285
# The URL under which the Subversion repositories are located.
286
svn_addr = %(svn_addr)r
288
# The login name for the owner of the jail
291
# The subversion-only password for the owner of the jail
292
svn_pass = %(svn_pass)r
293
""" % {'root_dir': ivle.conf.root_dir,
294
'public_host': ivle.conf.public_host,
295
'svn_addr': ivle.conf.svn_addr,
296
'username': username,
297
'svn_pass': svn_pass,
300
conf_file.write(template_conf_data)
301
conf_file.write("\n# The login name for the owner of the jail\n")
302
conf_file.write("login = %s\n" % repr(username))
303
conf_file.write("\n")
304
conf_file.write("# The subversion-only password for the owner of "
306
conf_file.write("svn_pass = %s\n" % repr(svn_pass))
299
307
conf_file.close()
301
309
# Make this file world-readable
319
327
% (username, unixid, unixid, username))
320
328
passwd_file.close()
330
def make_user_db(throw_on_error = True, **kwargs):
331
"""Creates a user's entry in the database, filling in all the fields.
332
All arguments must be keyword args. They are the fields in the table.
333
However, instead of supplying a "passhash", you must supply a
334
"password" argument, which will be hashed internally.
335
Also do not supply a state. All users are created in the "no_agreement"
337
Also pulls the user's subjects using the configured subject pulldown
338
module, and adds enrolments to the DB.
339
Throws an exception if the user already exists.
341
dbconn = ivle.db.DB()
342
dbconn.create_user(**kwargs)
345
if kwargs['password']:
346
if os.path.exists(ivle.conf.svn_auth_local):
350
res = os.system("htpasswd -%smb %s %s %s" % (create,
351
ivle.conf.svn_auth_local,
354
if res != 0 and throw_on_error:
355
raise Exception("Unable to create local-auth for %s" % kwargs['login'])
357
# Make sure the file is owned by the web server
359
chown_to_webserver(ivle.conf.svn_auth_local)
361
# Pulldown subjects and add enrolments
362
ivle.pulldown_subj.enrol_user(kwargs['login'])
322
364
def mount_jail(login):
323
365
# This is where we'll mount to...
324
366
destdir = os.path.join(ivle.conf.jail_base, login)