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

« back to all changes in this revision

Viewing changes to ivle/makeuser.py

  • Committer: me at id
  • Date: 2009-01-15 00:37:10 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1141
ivle.database.User: Add an authenticate() method, and a hash_password()
     staticmethod to replace the auth functions in ivle.db.

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
45
45
import filecmp
46
46
import logging
47
47
import ivle.conf
 
48
import ivle.db
48
49
import ivle.pulldown_subj
49
50
 
50
 
from ivle.database import ProjectGroup
51
 
 
52
51
def chown_to_webserver(filename):
53
52
    """
54
53
    Chowns a file so the web server user owns it.
74
73
 
75
74
    chown_to_webserver(path)
76
75
 
77
 
def rebuild_svn_config(store):
 
76
def rebuild_svn_config():
78
77
    """Build the complete SVN configuration file.
79
78
    """
80
 
    users = store.find(ivle.database.User)
 
79
    conn = ivle.db.DB()
 
80
    users = conn.get_users()
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())
99
103
    os.rename(ivle.conf.svn_conf + ".new", ivle.conf.svn_conf)
100
104
    chown_to_webserver(ivle.conf.svn_conf)
101
105
 
102
 
def rebuild_svn_group_config(store):
 
106
def rebuild_svn_group_config():
103
107
    """Build the complete SVN configuration file for groups
104
108
    """
 
109
    conn = ivle.db.DB()
 
110
    groups = conn.get_all('project_group',
 
111
        ['groupid', 'groupnm', 'projectsetid'])
105
112
    f = open(ivle.conf.svn_group_conf + ".new", "w")
106
113
    f.write("# IVLE SVN Group Repositories Configuration\n")
107
114
    f.write("# Auto-generated on %s\n" % time.asctime())
108
115
    f.write("\n")
109
 
    for group in store.find(ProjectGroup):
110
 
        offering = group.project_set.offering
111
 
        reponame = "_".join([offering.subject.short_name,
112
 
                             offering.semester.year,
113
 
                             offering.semester.semester,
114
 
                             group.name])
 
116
    for g in groups:
 
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']])
115
123
        f.write("[%s:/]\n"%reponame)
116
 
        for user in group.members:
117
 
            f.write("%s = rw\n" % user.login)
 
124
        users = conn.get_projectgroup_members(g['groupid'])
 
125
        for u in users:
 
126
            f.write("%s = rw\n"%u['login'])
118
127
        f.write("\n")
119
128
    f.close()
120
129
    os.rename(ivle.conf.svn_group_conf + ".new", ivle.conf.svn_group_conf)
123
132
def make_svn_auth(store, login, throw_on_error=True):
124
133
    """Setup svn authentication for the given user.
125
134
       Uses the given DB store object. Does not commit to the db.
 
135
       FIXME: create local.auth entry
126
136
    """
127
 
    passwd = hashlib.md5(uuid.uuid4().bytes).hexdigest()
 
137
    passwd = md5.new(uuid.uuid4().bytes).digest().encode('hex')
128
138
    if os.path.exists(ivle.conf.svn_auth_ivle):
129
139
        create = ""
130
140
    else:
174
184
    return (to_add, to_remove)
175
185
 
176
186
 
177
 
def make_jail(user, force=True):
 
187
def make_jail(username, uid, force=True, svn_pass=None):
178
188
    """Creates a new user's jail space, in the jail directory as configured in
179
189
    conf.py.
180
190
 
185
195
 
186
196
    Chowns the user's directory within the jail to the given UID.
187
197
 
 
198
    Note: This takes separate username and uid arguments. The UID need not
 
199
    *necessarily* correspond to a Unix username at all, if all you are
 
200
    planning to do is setuid to it. This allows the caller the freedom of
 
201
    deciding the binding between username and uid, if any.
 
202
 
188
203
    force: If false, exception if jail already exists for this user.
189
204
    If true (default), overwrites it, but preserves home directory.
 
205
 
 
206
    svn_pass: If provided this will be a string, the randomly-generated
 
207
    Subversion password for this user (if you happen to already have it).
 
208
    If not provided, it will be read from the database.
190
209
    """
191
210
    # MUST run as root or some of this may fail
192
211
    if os.getuid() != 0:
193
212
        raise Exception("Must run make_jail as root")
194
213
    
195
214
    # tempdir is for putting backup homes in
196
 
    tempdir = os.path.join(ivle.conf.jail_src_base, '__temp__')
 
215
    tempdir = os.path.join(ivle.conf.jail_base, '__temp__')
197
216
    if not os.path.exists(tempdir):
198
217
        os.makedirs(tempdir)
199
218
    elif not os.path.isdir(tempdir):
200
219
        os.unlink(tempdir)
201
220
        os.mkdir(tempdir)
202
 
    userdir = os.path.join(ivle.conf.jail_src_base, user.login)
 
221
    userdir = os.path.join(ivle.conf.jail_src_base, username)
203
222
    homedir = os.path.join(userdir, 'home')
204
 
    userhomedir = os.path.join(homedir, user.login)   # Return value
 
223
    userhomedir = os.path.join(homedir, username)   # Return value
205
224
 
206
225
    if os.path.exists(userdir):
207
226
        if not force:
213
232
        warnings.simplefilter('ignore')
214
233
        homebackup = os.tempnam(tempdir)
215
234
        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.
 
235
        # Note: shutil.move does not behave like "mv" - it does not put a file
 
236
        # into a directory if it already exists, just fails. Therefore it is
 
237
        # not susceptible to tmpnam symlink attack.
221
238
        shutil.move(homedir, homebackup)
222
239
        shutil.rmtree(userdir)
223
 
        os.makedirs(userdir)
 
240
        os.makedirs(homedir)
224
241
        shutil.move(homebackup, homedir)
225
242
        # Change the ownership of all the files to the right unixid
226
243
        logging.debug("chown %s's home directory files to uid %d"
227
 
            %(user.login, user.unixid))
228
 
        os.spawnvp(os.P_WAIT, 'chown', ['chown', '-R', '%d:%d' % (user.unixid,
229
 
                                        user.unixid), userhomedir])
 
244
            %(username, uid))
 
245
        os.chown(userhomedir, uid, uid)
 
246
        for root, dirs, files in os.walk(userhomedir):
 
247
            for fsobj in dirs + files:
 
248
                os.chown(os.path.join(root, fsobj), uid, uid)
230
249
    else:
231
250
        # No user jail exists
232
251
        # Set up the user's home directory
233
252
        os.makedirs(userhomedir)
234
253
        # Chown (and set the GID to the same as the UID).
235
 
        os.chown(userhomedir, user.unixid, user.unixid)
 
254
        os.chown(userhomedir, uid, uid)
236
255
        # Chmod to rwxr-xr-x (755)
237
256
        os.chmod(userhomedir, 0755)
238
257
 
239
 
    make_ivle_conf(user.login, userdir, user.svn_pass)
240
 
    make_etc_passwd(user.login, userdir, ivle.conf.jail_system, user.unixid)
 
258
    # There are 2 special files which need to be generated specific to this
 
259
    # user: ${python_site_packages}/lib/conf/conf.py and /etc/passwd.
 
260
    # "__" username "__" users are exempt (special)
 
261
    if not (username.startswith("__") and username.endswith("__")):
 
262
        make_conf_py(username, userdir, ivle.conf.jail_system, svn_pass)
 
263
        make_etc_passwd(username, userdir, ivle.conf.jail_system, uid)
241
264
 
242
265
    return userhomedir
243
266
 
244
 
def make_ivle_conf(username, user_jail_dir, svn_pass):
 
267
def make_conf_py(username, user_jail_dir, staging_dir, svn_pass=None):
245
268
    """
246
269
    Creates (overwriting any existing file, and creating directories) a
247
 
    file /etc/ivle/ivle.conf in a given user's jail.
 
270
    file ${python_site_packages}/ivle/conf/conf.py in a given user's jail.
248
271
    username: Username.
249
272
    user_jail_dir: User's jail dir, ie. ivle.conf.jail_base + username
250
 
    svn_pass: User's SVN password.
 
273
    staging_dir: The dir with the staging copy of the jail. (With the
 
274
        template conf.py file).
 
275
    svn_pass: As with make_jail. User's SVN password, but if not supplied,
 
276
        will look up in the DB.
251
277
    """
252
 
    conf_path = os.path.join(user_jail_dir, "etc/ivle/ivle.conf")
 
278
    template_conf_path = os.path.join(staging_dir,
 
279
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
 
280
    conf_path = os.path.join(user_jail_dir,
 
281
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
253
282
    os.makedirs(os.path.dirname(conf_path))
254
283
 
255
 
    # In the "in-jail" version of conf, we don't need MOST of the details
256
 
    # (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()
 
284
    # If svn_pass isn't supplied, grab it from the DB
 
285
    if svn_pass is None:
 
286
        dbconn = ivle.db.DB()
 
287
        svn_pass = dbconn.get_user(username).svn_pass
 
288
        dbconn.close()
 
289
 
 
290
    # Read the contents of the template conf file
 
291
    try:
 
292
        template_conf_file = open(template_conf_path, "r")
 
293
        template_conf_data = template_conf_file.read()
 
294
        template_conf_file.close()
 
295
    except:
 
296
        # Couldn't open template conf.py for some reason
 
297
        # Just treat it as empty file
 
298
        template_conf_data = ("# Warning: Problem building config script.\n"
 
299
                              "# Could not find template conf.py file.\n")
 
300
 
 
301
    conf_file = open(conf_path, "w")
 
302
    conf_file.write(template_conf_data)
 
303
    conf_file.write("\n# The login name for the owner of the jail\n")
 
304
    conf_file.write("login = %s\n" % repr(username))
 
305
    conf_file.write("\n")
 
306
    conf_file.write("# The subversion-only password for the owner of "
 
307
        "the jail\n")
 
308
    conf_file.write("svn_pass = %s\n" % repr(svn_pass))
 
309
    conf_file.close()
266
310
 
267
311
    # Make this file world-readable
268
312
    # (chmod 644 conf_path)
285
329
                      % (username, unixid, unixid, username))
286
330
    passwd_file.close()
287
331
 
 
332
def make_user_db(throw_on_error = True, **kwargs):
 
333
    """Creates a user's entry in the database, filling in all the fields.
 
334
    All arguments must be keyword args. They are the fields in the table.
 
335
    However, instead of supplying a "passhash", you must supply a
 
336
    "password" argument, which will be hashed internally.
 
337
    Also do not supply a state. All users are created in the "no_agreement"
 
338
    state.
 
339
    Also pulls the user's subjects using the configured subject pulldown
 
340
    module, and adds enrolments to the DB.
 
341
    Throws an exception if the user already exists.
 
342
    """
 
343
    dbconn = ivle.db.DB()
 
344
    dbconn.create_user(**kwargs)
 
345
    dbconn.close()
 
346
 
 
347
    if kwargs['password']:
 
348
        if os.path.exists(ivle.conf.svn_auth_local):
 
349
            create = ""
 
350
        else:
 
351
            create = "c"
 
352
        res = os.system("htpasswd -%smb %s %s %s" % (create,
 
353
                                                     ivle.conf.svn_auth_local,
 
354
                                                     kwargs['login'],
 
355
                                                     kwargs['password']))
 
356
        if res != 0 and throw_on_error:
 
357
            raise Exception("Unable to create local-auth for %s" % kwargs['login'])
 
358
 
 
359
    # Make sure the file is owned by the web server
 
360
    if create == "c":
 
361
        chown_to_webserver(ivle.conf.svn_auth_local)
 
362
 
 
363
    # Pulldown subjects and add enrolments
 
364
    ivle.pulldown_subj.enrol_user(kwargs['login'])
 
365
 
288
366
def mount_jail(login):
289
367
    # This is where we'll mount to...
290
368
    destdir = os.path.join(ivle.conf.jail_base, login)