182
182
return (to_add, to_remove)
185
def make_jail(username, uid, force=True, svn_pass=None):
185
def make_jail(user, force=True):
186
186
"""Creates a new user's jail space, in the jail directory as configured in
194
194
Chowns the user's directory within the jail to the given UID.
196
Note: This takes separate username and uid arguments. The UID need not
197
*necessarily* correspond to a Unix username at all, if all you are
198
planning to do is setuid to it. This allows the caller the freedom of
199
deciding the binding between username and uid, if any.
201
196
force: If false, exception if jail already exists for this user.
202
197
If true (default), overwrites it, but preserves home directory.
204
svn_pass: If provided this will be a string, the randomly-generated
205
Subversion password for this user (if you happen to already have it).
206
If not provided, it will be read from the database.
208
199
# MUST run as root or some of this may fail
209
200
if os.getuid() != 0:
216
207
elif not os.path.isdir(tempdir):
217
208
os.unlink(tempdir)
218
209
os.mkdir(tempdir)
219
userdir = os.path.join(ivle.conf.jail_src_base, username)
210
userdir = os.path.join(ivle.conf.jail_src_base, user.login)
220
211
homedir = os.path.join(userdir, 'home')
221
userhomedir = os.path.join(homedir, username) # Return value
212
userhomedir = os.path.join(homedir, user.login) # Return value
223
214
if os.path.exists(userdir):
239
230
shutil.move(homebackup, homedir)
240
231
# Change the ownership of all the files to the right unixid
241
232
logging.debug("chown %s's home directory files to uid %d"
243
os.chown(userhomedir, uid, uid)
233
%(user.login, user.unixid))
234
os.chown(userhomedir, user.unixid, user.unixid)
244
235
for root, dirs, files in os.walk(userhomedir):
245
236
for fsobj in dirs + files:
246
os.chown(os.path.join(root, fsobj), uid, uid)
237
os.chown(os.path.join(root, fsobj), user.unixid, user.unixid)
248
239
# No user jail exists
249
240
# Set up the user's home directory
250
241
os.makedirs(userhomedir)
251
242
# Chown (and set the GID to the same as the UID).
252
os.chown(userhomedir, uid, uid)
243
os.chown(userhomedir, user.unixid, user.unixid)
253
244
# Chmod to rwxr-xr-x (755)
254
245
os.chmod(userhomedir, 0755)
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)
247
make_conf_py(user.login, userdir, ivle.conf.jail_system, user.svn_pass)
248
make_etc_passwd(user.login, userdir, ivle.conf.jail_system, user.unixid)
263
250
return userhomedir
265
def make_conf_py(username, user_jail_dir, staging_dir, svn_pass=None):
252
def make_conf_py(username, user_jail_dir, staging_dir, svn_pass):
267
254
Creates (overwriting any existing file, and creating directories) a
268
255
file ${python_site_packages}/ivle/conf/conf.py in a given user's jail.
279
266
ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
280
267
os.makedirs(os.path.dirname(conf_path))
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
269
# Read the contents of the template conf file
290
271
template_conf_file = open(template_conf_path, "r")