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

« back to all changes in this revision

Viewing changes to ivle/makeuser.py

  • Committer: William Grant
  • Date: 2009-01-20 01:44:20 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: grantw@unimelb.edu.au-20090120014420-t40ofmnvysps6ii9
ivle.database.ProjectSet: Add projects and project_groups referencesets.
www/apps/userservice: Don't use get_groups_by_projectset; construct the list
    of dicts manually.
ivle.db.get_groups_by_projectset: Remove. Unused. 

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
74
74
 
75
75
    chown_to_webserver(path)
76
76
 
77
 
def rebuild_svn_config(store, config):
 
77
def rebuild_svn_config(store):
78
78
    """Build the complete SVN configuration file.
79
 
    @param config: An ivle.config.Config object.
80
79
    """
81
80
    users = store.find(ivle.database.User)
82
81
    groups = {}
83
 
    # TODO: Populate groups with per-offering tutors/lecturers/etc.
84
 
    conf_name = config['paths']['svn']['conf']
85
 
    temp_name = conf_name + ".new"
86
 
    f = open(temp_name, "w")
 
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)
 
87
    f = open(ivle.conf.svn_conf + ".new", "w")
87
88
    f.write("# IVLE SVN Repositories Configuration\n")
88
89
    f.write("# Auto-generated on %s\n" % time.asctime())
89
90
    f.write("\n")
99
100
        #f.write("@admin = rw\n")
100
101
        f.write("\n")
101
102
    f.close()
102
 
    os.rename(temp_name, conf_name)
103
 
    chown_to_webserver(conf_name)
 
103
    os.rename(ivle.conf.svn_conf + ".new", ivle.conf.svn_conf)
 
104
    chown_to_webserver(ivle.conf.svn_conf)
104
105
 
105
 
def rebuild_svn_group_config(store, config):
 
106
def rebuild_svn_group_config(store):
106
107
    """Build the complete SVN configuration file for groups
107
 
    @param config: An ivle.config.Config object.
108
108
    """
109
 
    conf_name = config['paths']['svn']['group_conf']
110
 
    temp_name = conf_name + ".new"
111
 
    f = open(temp_name, "w")
 
109
    f = open(ivle.conf.svn_group_conf + ".new", "w")
112
110
    f.write("# IVLE SVN Group Repositories Configuration\n")
113
111
    f.write("# Auto-generated on %s\n" % time.asctime())
114
112
    f.write("\n")
123
121
            f.write("%s = rw\n" % user.login)
124
122
        f.write("\n")
125
123
    f.close()
126
 
    os.rename(temp_name, conf_name)
127
 
    chown_to_webserver(conf_name)
 
124
    os.rename(ivle.conf.svn_group_conf + ".new", ivle.conf.svn_group_conf)
 
125
    chown_to_webserver(ivle.conf.svn_group_conf)
128
126
 
129
 
def make_svn_auth(store, login, config, throw_on_error=True):
 
127
def make_svn_auth(store, login, throw_on_error=True):
130
128
    """Setup svn authentication for the given user.
131
129
       Uses the given DB store object. Does not commit to the db.
132
130
    """
133
 
    # filename is, eg, /var/lib/ivle/svn/ivle.auth
134
 
    filename = config['paths']['svn']['auth_ivle']
135
 
    passwd = hashlib.md5(uuid.uuid4().bytes).hexdigest()
136
 
    if os.path.exists(filename):
 
131
    passwd = md5.new(uuid.uuid4().bytes).digest().encode('hex')
 
132
    if os.path.exists(ivle.conf.svn_auth_ivle):
137
133
        create = ""
138
134
    else:
139
135
        create = "c"
141
137
    user = ivle.database.User.get_by_login(store, login)
142
138
    user.svn_pass = unicode(passwd)
143
139
 
144
 
    res = os.system("htpasswd -%smb %s %s %s" % (create, filename,
 
140
    res = os.system("htpasswd -%smb %s %s %s" % (create,
 
141
                                              ivle.conf.svn_auth_ivle,
145
142
                                              login, passwd))
146
143
    if res != 0 and throw_on_error:
147
144
        raise Exception("Unable to create ivle-auth for %s" % login)
148
145
 
149
146
    # Make sure the file is owned by the web server
150
147
    if create == "c":
151
 
        chown_to_webserver(filename)
 
148
        chown_to_webserver(ivle.conf.svn_auth_ivle)
152
149
 
153
150
    return passwd
154
151
 
200
197
        raise Exception("Must run make_jail as root")
201
198
    
202
199
    # tempdir is for putting backup homes in
203
 
    tempdir = os.path.join(ivle.conf.jail_src_base, '__temp__')
 
200
    tempdir = os.path.join(ivle.conf.jail_base, '__temp__')
204
201
    if not os.path.exists(tempdir):
205
202
        os.makedirs(tempdir)
206
203
    elif not os.path.isdir(tempdir):
220
217
        warnings.simplefilter('ignore')
221
218
        homebackup = os.tempnam(tempdir)
222
219
        warnings.resetwarnings()
223
 
        # Back up the /home directory, delete the entire jail, recreate the
224
 
        # jail directory tree, then copy the /home back
225
 
        # NOTE that shutil.move changed in Python 2.6, it now moves a
226
 
        # directory INTO the target (like `mv`), which it didn't use to do.
227
 
        # 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.
228
223
        shutil.move(homedir, homebackup)
229
224
        shutil.rmtree(userdir)
230
 
        os.makedirs(userdir)
 
225
        os.makedirs(homedir)
231
226
        shutil.move(homebackup, homedir)
232
227
        # Change the ownership of all the files to the right unixid
233
228
        logging.debug("chown %s's home directory files to uid %d"
234
229
            %(user.login, user.unixid))
235
 
        os.spawnvp(os.P_WAIT, 'chown', ['chown', '-R', '%d:%d' % (user.unixid,
236
 
                                        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)
237
234
    else:
238
235
        # No user jail exists
239
236
        # Set up the user's home directory
243
240
        # Chmod to rwxr-xr-x (755)
244
241
        os.chmod(userhomedir, 0755)
245
242
 
246
 
    make_ivle_conf(user.login, userdir, user.svn_pass)
 
243
    make_conf_py(user.login, userdir, ivle.conf.jail_system, user.svn_pass)
247
244
    make_etc_passwd(user.login, userdir, ivle.conf.jail_system, user.unixid)
248
245
 
249
246
    return userhomedir
250
247
 
251
 
def make_ivle_conf(username, user_jail_dir, svn_pass):
 
248
def make_conf_py(username, user_jail_dir, staging_dir, svn_pass):
252
249
    """
253
250
    Creates (overwriting any existing file, and creating directories) a
254
 
    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.
255
252
    username: Username.
256
253
    user_jail_dir: User's jail dir, ie. ivle.conf.jail_base + username
257
 
    svn_pass: User's SVN password.
 
254
    staging_dir: The dir with the staging copy of the jail. (With the
 
255
        template conf.py file).
 
256
    svn_pass: As with make_jail. User's SVN password, but if not supplied,
 
257
        will look up in the DB.
258
258
    """
259
 
    conf_path = os.path.join(user_jail_dir, "etc/ivle/ivle.conf")
 
259
    template_conf_path = os.path.join(staging_dir,
 
260
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
 
261
    conf_path = os.path.join(user_jail_dir,
 
262
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
260
263
    os.makedirs(os.path.dirname(conf_path))
261
264
 
262
 
    # In the "in-jail" version of conf, we don't need MOST of the details
263
 
    # (it would be a security risk to have them here).
264
 
    # So we just write root_dir.
265
 
    conf_obj = ivle.config.Config(blank=True)
266
 
    conf_obj.filename = conf_path
267
 
    conf_obj['urls']['root'] = ivle.conf.root_dir
268
 
    conf_obj['urls']['public_host'] = ivle.conf.public_host
269
 
    conf_obj['urls']['svn_addr'] = ivle.conf.svn_addr
270
 
    conf_obj['user_info']['login'] = username
271
 
    conf_obj['user_info']['svn_pass'] = svn_pass
272
 
    conf_obj.write()
 
265
    # Read the contents of the template conf file
 
266
    try:
 
267
        template_conf_file = open(template_conf_path, "r")
 
268
        template_conf_data = template_conf_file.read()
 
269
        template_conf_file.close()
 
270
    except:
 
271
        # Couldn't open template conf.py for some reason
 
272
        # Just treat it as empty file
 
273
        template_conf_data = ("# Warning: Problem building config script.\n"
 
274
                              "# Could not find template conf.py file.\n")
 
275
 
 
276
    conf_file = open(conf_path, "w")
 
277
    conf_file.write(template_conf_data)
 
278
    conf_file.write("\n# The login name for the owner of the jail\n")
 
279
    conf_file.write("login = %s\n" % repr(username))
 
280
    conf_file.write("\n")
 
281
    conf_file.write("# The subversion-only password for the owner of "
 
282
        "the jail\n")
 
283
    conf_file.write("svn_pass = %s\n" % repr(svn_pass))
 
284
    conf_file.close()
273
285
 
274
286
    # Make this file world-readable
275
287
    # (chmod 644 conf_path)