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

« back to all changes in this revision

Viewing changes to ivle/makeuser.py

  • Committer: Nick Chadwick
  • Date: 2009-02-25 16:39:59 UTC
  • mto: (1099.1.227 exercise-ui)
  • mto: This revision was merged to the branch mainline in revision 1162.
  • Revision ID: chadnickbok@gmail.com-20090225163959-3hsedlesu2pjm12e
Added a few extra files. I'm not sure if they're necessary, and I don't
have time to find out.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
# Do not call os.system("chown www-data") - use Python lib
36
36
# and use the web server uid given in conf. (Several places).
37
37
 
38
 
import hashlib
 
38
import md5
39
39
import os
40
40
import stat
41
41
import shutil
79
79
    """
80
80
    users = store.find(ivle.database.User)
81
81
    groups = {}
82
 
    # TODO: Populate groups with per-offering tutors/lecturers/etc.
 
82
    for u in users:
 
83
        role = str(u.role)
 
84
        if role not in groups:
 
85
            groups[role] = []
 
86
        groups[role].append(u.login)
83
87
    f = open(ivle.conf.svn_conf + ".new", "w")
84
88
    f.write("# IVLE SVN Repositories Configuration\n")
85
89
    f.write("# Auto-generated on %s\n" % time.asctime())
124
128
    """Setup svn authentication for the given user.
125
129
       Uses the given DB store object. Does not commit to the db.
126
130
    """
127
 
    passwd = hashlib.md5(uuid.uuid4().bytes).hexdigest()
 
131
    passwd = md5.new(uuid.uuid4().bytes).digest().encode('hex')
128
132
    if os.path.exists(ivle.conf.svn_auth_ivle):
129
133
        create = ""
130
134
    else:
193
197
        raise Exception("Must run make_jail as root")
194
198
    
195
199
    # tempdir is for putting backup homes in
196
 
    tempdir = os.path.join(ivle.conf.jail_src_base, '__temp__')
 
200
    tempdir = os.path.join(ivle.conf.jail_base, '__temp__')
197
201
    if not os.path.exists(tempdir):
198
202
        os.makedirs(tempdir)
199
203
    elif not os.path.isdir(tempdir):
213
217
        warnings.simplefilter('ignore')
214
218
        homebackup = os.tempnam(tempdir)
215
219
        warnings.resetwarnings()
216
 
        # Back up the /home directory, delete the entire jail, recreate the
217
 
        # jail directory tree, then copy the /home back
218
 
        # NOTE that shutil.move changed in Python 2.6, it now moves a
219
 
        # directory INTO the target (like `mv`), which it didn't use to do.
220
 
        # This code works regardless.
 
220
        # Note: shutil.move does not behave like "mv" - it does not put a file
 
221
        # into a directory if it already exists, just fails. Therefore it is
 
222
        # not susceptible to tmpnam symlink attack.
221
223
        shutil.move(homedir, homebackup)
222
224
        shutil.rmtree(userdir)
223
 
        os.makedirs(userdir)
 
225
        os.makedirs(homedir)
224
226
        shutil.move(homebackup, homedir)
225
227
        # Change the ownership of all the files to the right unixid
226
228
        logging.debug("chown %s's home directory files to uid %d"
227
229
            %(user.login, user.unixid))
228
 
        os.spawnvp(os.P_WAIT, 'chown', ['chown', '-R', '%d:%d' % (user.unixid,
229
 
                                        user.unixid), userhomedir])
 
230
        os.chown(userhomedir, user.unixid, user.unixid)
 
231
        for root, dirs, files in os.walk(userhomedir):
 
232
            for fsobj in dirs + files:
 
233
                os.chown(os.path.join(root, fsobj), user.unixid, user.unixid)
230
234
    else:
231
235
        # No user jail exists
232
236
        # Set up the user's home directory
236
240
        # Chmod to rwxr-xr-x (755)
237
241
        os.chmod(userhomedir, 0755)
238
242
 
239
 
    make_ivle_conf(user.login, userdir, user.svn_pass)
 
243
    make_conf_py(user.login, userdir, user.svn_pass)
240
244
    make_etc_passwd(user.login, userdir, ivle.conf.jail_system, user.unixid)
241
245
 
242
246
    return userhomedir
243
247
 
244
 
def make_ivle_conf(username, user_jail_dir, svn_pass):
 
248
def make_conf_py(username, user_jail_dir, svn_pass):
245
249
    """
246
250
    Creates (overwriting any existing file, and creating directories) a
247
 
    file /etc/ivle/ivle.conf in a given user's jail.
 
251
    file ${python_site_packages}/ivle/conf/conf.py in a given user's jail.
248
252
    username: Username.
249
253
    user_jail_dir: User's jail dir, ie. ivle.conf.jail_base + username
250
254
    svn_pass: User's SVN password.
251
255
    """
252
 
    conf_path = os.path.join(user_jail_dir, "etc/ivle/ivle.conf")
 
256
    conf_path = os.path.join(user_jail_dir,
 
257
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
253
258
    os.makedirs(os.path.dirname(conf_path))
254
259
 
255
260
    # In the "in-jail" version of conf, we don't need MOST of the details
256
261
    # (it would be a security risk to have them here).
257
 
    # So we just write root_dir.
258
 
    conf_obj = ivle.config.Config(blank=True)
259
 
    conf_obj.filename = conf_path
260
 
    conf_obj['urls']['root'] = ivle.conf.root_dir
261
 
    conf_obj['urls']['public_host'] = ivle.conf.public_host
262
 
    conf_obj['urls']['svn_addr'] = ivle.conf.svn_addr
263
 
    conf_obj['user_info']['login'] = username
264
 
    conf_obj['user_info']['svn_pass'] = svn_pass
265
 
    conf_obj.write()
 
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.)
 
265
 
 
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.
 
270
    conf_file = open(conf_path, "w")
 
271
    conf_file.write("""# IVLE jail configuration
 
272
 
 
273
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
 
274
# with this).
 
275
# eg. "/" or "/ivle".
 
276
root_dir = %(root_dir)r
 
277
 
 
278
# This value is not relevant inside the jail, but must remain for now. See
 
279
# the XXX in ivle.makeuser.make_conf_py.
 
280
jail_base = '/'
 
281
 
 
282
# The hostname for serving publicly accessible pages
 
283
public_host = %(public_host)r
 
284
 
 
285
# The URL under which the Subversion repositories are located.
 
286
svn_addr = %(svn_addr)r
 
287
 
 
288
# The login name for the owner of the jail
 
289
login = %(username)r
 
290
 
 
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,
 
298
      })
 
299
    conf_file.close()
266
300
 
267
301
    # Make this file world-readable
268
302
    # (chmod 644 conf_path)