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.
30
# Users must not be called "temp" or "template".
32
# TODO: When creating a new home directory, chown it to its owner
41
def makeuser(username, password, nick, fullname, rolenm, studentid):
42
"""Creates a new user on a pre-installed system.
43
Sets up the following:
44
* User's jail and home directory within the jail.
45
* Subversion repository
46
* Check out Subversion workspace into jail
47
* Database details for user
50
homedir = make_jail(username)
51
make_user_db(username, password, nick, fullname, rolenm, studentid)
52
# TODO: -p password (need to use crypt)
53
if os.system("useradd -d %s '%s'" % (homedir, username)) != 0:
54
raise Exception("Failed to add Unix user account")
56
def make_jail(username, force=True):
57
"""Creates a new user's jail space, in the jail directory as configured in
60
This expects there to be a "template" directory within the jail root which
61
contains all the files for a sample student jail. It creates the student's
62
directory in the jail root, by making a hard-link copy of every file in the
63
template directory, recursively.
65
Returns the path to the user's home directory.
67
force: If false, exception if jail already exists for this user.
68
If true (default), overwrites it, but preserves home directory.
70
# MUST run as root or some of this may fail
72
raise Exception("Must run make_jail as root")
74
templatedir = os.path.join(conf.jail_base, 'template')
75
if not os.path.isdir(templatedir):
76
raise Exception("Template jail directory does not exist: " +
78
# tempdir is for putting backup homes in
79
tempdir = os.path.join(conf.jail_base, 'temp')
80
if not os.path.exists(tempdir):
82
elif not os.path.isdir(tempdir):
85
userdir = os.path.join(conf.jail_base, username)
86
homedir = os.path.join(userdir, 'home')
88
if os.path.exists(userdir):
90
raise Exception("User's jail already exists")
91
# User jail already exists. Blow it away but preserve their home
93
# Ignore warnings about the use of tmpnam
94
warnings.simplefilter('ignore')
95
homebackup = os.tempnam(tempdir)
96
warnings.resetwarnings()
97
# Note: shutil.move does not behave like "mv" - it does not put a file
98
# into a directory if it already exists, just fails. Therefore it is
99
# not susceptible to tmpnam symlink attack.
100
shutil.move(homedir, homebackup)
102
# Any errors that occur after making the backup will be caught and
103
# the backup will be un-made.
104
# XXX This will still leave the user's jail in an unusable state,
105
# but at least they won't lose their files.
106
shutil.rmtree(userdir)
108
# Hard-link (copy aliasing) the entire tree over
109
linktree(templatedir, userdir)
111
# Set up the user's home directory (restore backup)
112
# First make sure the directory is empty and its parent exists
114
shutil.rmtree(homedir)
117
# XXX If this fails the user's directory will be lost (in the temp
118
# directory). But it shouldn't fail as homedir should not exist.
120
shutil.move(homebackup, homedir)
121
return os.path.join(homedir, username)
123
# No user jail exists
124
# Hard-link (copy aliasing) the entire tree over
125
linktree(templatedir, userdir)
127
# Set up the user's home directory
128
userhomedir = os.path.join(homedir, username)
129
os.mkdir(userhomedir)
132
def linktree(src, dst):
133
"""Recursively hard-link a directory tree using os.link().
135
The destination directory must not already exist.
136
If exception(s) occur, an Error is raised with a list of reasons.
138
Symlinks are preserved (in fact, hard links are created which point to the
141
Code heavily based upon shutil.copytree from Python 2.5 library.
143
names = os.listdir(src)
147
srcname = os.path.join(src, name)
148
dstname = os.path.join(dst, name)
150
if os.path.isdir(srcname):
151
linktree(srcname, dstname)
153
os.link(srcname, dstname)
154
# XXX What about devices, sockets etc.?
155
except (IOError, os.error), why:
156
errors.append((srcname, dstname, str(why)))
157
# catch the Error from the recursive copytree so that we can
158
# continue with other files
159
except Exception, err:
160
errors.append(err.args[0])
162
shutil.copystat(src, dst)
164
# can't copy file access times on Windows
167
errors.extend((src, dst, str(why)))
169
raise Exception, errors
171
def make_user_db(login, password, nick, fullname, rolenm, studentid,
173
"""Creates a user's entry in the database, filling in all the fields.
174
If force is False, throws an exception if the user already exists.
175
If True, overwrites the user's entry in the DB.
179
# Delete user if it exists
181
dbconn.delete_user(login)
184
dbconn.create_user(login, password, nick, fullname, rolenm, studentid)