1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
# Allows creation of users. This sets up the following:
23
# * User's jail and home directory within the jail.
24
# * Subversion repository (TODO)
25
# * Check out Subversion workspace into jail (TODO)
26
# * Database details for user
29
# TODO: Sanitize login name and other fields.
37
def makeuser(username, password, nick, fullname, rolenm, studentid):
38
"""Creates a new user on a pre-installed system.
39
Sets up the following:
40
* User's jail and home directory within the jail.
41
* Subversion repository
42
* Check out Subversion workspace into jail
43
* Database details for user
46
homedir = make_jail(username)
47
make_user_db(username, password, nick, fullname, rolenm, studentid)
48
# TODO: -p password (need to use crypt)
49
if os.system("useradd -d %s '%s'" % (homedir, username)) != 0:
50
raise Exception("Failed to add Unix user account")
52
def make_jail(username):
53
"""Creates a new user's jail space, in the jail directory as configured in
56
This expects there to be a "template" directory within the jail root which
57
contains all the files for a sample student jail. It creates the student's
58
directory in the jail root, by making a hard-link copy of every file in the
59
template directory, recursively.
61
Returns the path to the user's home directory.
63
templatedir = os.path.join(conf.jail_base, 'template')
64
if not os.path.isdir(templatedir):
65
raise Exception("Template jail directory does not exist: " +
67
userdir = os.path.join(conf.jail_base, username)
68
if os.path.exists(userdir):
69
raise Exception("User's jail directory already exists: " +
72
# Hard-link (copy aliasing) the entire tree over
73
linktree(templatedir, userdir)
75
# Set up the user's home directory
76
homedir = os.path.join(userdir, 'home', username)
80
def linktree(src, dst):
81
"""Recursively hard-link a directory tree using os.link().
83
The destination directory must not already exist.
84
If exception(s) occur, an Error is raised with a list of reasons.
86
Symlinks are preserved (in fact, hard links are created which point to the
89
Code heavily based upon shutil.copytree from Python 2.5 library.
91
names = os.listdir(src)
95
srcname = os.path.join(src, name)
96
dstname = os.path.join(dst, name)
98
if os.path.isdir(srcname):
99
linktree(srcname, dstname)
101
os.link(srcname, dstname)
102
# XXX What about devices, sockets etc.?
103
except (IOError, os.error), why:
104
errors.append((srcname, dstname, str(why)))
105
# catch the Error from the recursive copytree so that we can
106
# continue with other files
107
except Exception, err:
108
errors.append(err.args[0])
110
shutil.copystat(src, dst)
112
# can't copy file access times on Windows
115
errors.extend((src, dst, str(why)))
117
raise Exception, errors
119
def make_user_db(login, password, nick, fullname, rolenm, studentid):
120
"""Creates a user's entry in the database, filling in all the fields."""
122
dbconn.create_user(login, password, nick, fullname, rolenm, studentid)