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

« back to all changes in this revision

Viewing changes to www/common/makeuser.py

  • Committer: mattgiuca
  • Date: 2008-02-05 01:41:15 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:409
Moved www/conf and www/common to a new directory lib. This separates the "web"
part of IVLE from what is becoming less web oriented (at least from Apache's
standpoint).
Modified setup.py to install this lib directory correctly and write conf in
the right place. Also adds the lib directory to ivle.pth.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# IVLE - Informatics Virtual Learning Environment
2
 
# Copyright (C) 2007-2008 The University of Melbourne
3
 
#
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.
8
 
#
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.
13
 
#
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
17
 
 
18
 
# Module: MakeUser
19
 
# Author: Matt Giuca
20
 
# Date:   1/2/2008
21
 
 
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
27
 
# * Unix user account
28
 
 
29
 
# TODO: Sanitize login name and other fields.
30
 
# Users must not be called "temp" or "template".
31
 
 
32
 
# TODO: When creating a new home directory, chown it to its owner
33
 
 
34
 
import os
35
 
import shutil
36
 
import warnings
37
 
 
38
 
import conf
39
 
import db
40
 
 
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
48
 
    * Unix user account
49
 
    """
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")
55
 
 
56
 
def make_jail(username, force=True):
57
 
    """Creates a new user's jail space, in the jail directory as configured in
58
 
    conf.py.
59
 
 
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.
64
 
 
65
 
    Returns the path to the user's home directory.
66
 
 
67
 
    force: If false, exception if jail already exists for this user.
68
 
    If true (default), overwrites it, but preserves home directory.
69
 
    """
70
 
    # MUST run as root or some of this may fail
71
 
    if os.getuid() != 0:
72
 
        raise Exception("Must run make_jail as root")
73
 
    
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: " +
77
 
            templatedir)
78
 
    # tempdir is for putting backup homes in
79
 
    tempdir = os.path.join(conf.jail_base, 'temp')
80
 
    if not os.path.exists(tempdir):
81
 
        os.makedirs(tempdir)
82
 
    elif not os.path.isdir(tempdir):
83
 
        os.unlink(tempdir)
84
 
        os.mkdir(tempdir)
85
 
    userdir = os.path.join(conf.jail_base, username)
86
 
    homedir = os.path.join(userdir, 'home')
87
 
 
88
 
    if os.path.exists(userdir):
89
 
        if not force:
90
 
            raise Exception("User's jail already exists")
91
 
        # User jail already exists. Blow it away but preserve their home
92
 
        # directory.
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)
101
 
        try:
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)
107
 
 
108
 
            # Hard-link (copy aliasing) the entire tree over
109
 
            linktree(templatedir, userdir)
110
 
        finally:
111
 
            # Set up the user's home directory (restore backup)
112
 
            # First make sure the directory is empty and its parent exists
113
 
            try:
114
 
                shutil.rmtree(homedir)
115
 
            except:
116
 
                pass
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.
119
 
            os.makedirs(homedir)
120
 
            shutil.move(homebackup, homedir)
121
 
        return os.path.join(homedir, username)
122
 
    else:
123
 
        # No user jail exists
124
 
        # Hard-link (copy aliasing) the entire tree over
125
 
        linktree(templatedir, userdir)
126
 
 
127
 
        # Set up the user's home directory
128
 
        userhomedir = os.path.join(homedir, username)
129
 
        os.mkdir(userhomedir)
130
 
        return userhomedir
131
 
 
132
 
def linktree(src, dst):
133
 
    """Recursively hard-link a directory tree using os.link().
134
 
 
135
 
    The destination directory must not already exist.
136
 
    If exception(s) occur, an Error is raised with a list of reasons.
137
 
 
138
 
    Symlinks are preserved (in fact, hard links are created which point to the
139
 
    symlinks).
140
 
 
141
 
    Code heavily based upon shutil.copytree from Python 2.5 library.
142
 
    """
143
 
    names = os.listdir(src)
144
 
    os.makedirs(dst)
145
 
    errors = []
146
 
    for name in names:
147
 
        srcname = os.path.join(src, name)
148
 
        dstname = os.path.join(dst, name)
149
 
        try:
150
 
            if os.path.isdir(srcname):
151
 
                linktree(srcname, dstname)
152
 
            else:
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])
161
 
    try:
162
 
        shutil.copystat(src, dst)
163
 
    except WindowsError:
164
 
        # can't copy file access times on Windows
165
 
        pass
166
 
    except OSError, why:
167
 
        errors.extend((src, dst, str(why)))
168
 
    if errors:
169
 
        raise Exception, errors
170
 
 
171
 
def make_user_db(login, password, nick, fullname, rolenm, studentid,
172
 
    force=True):
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.
176
 
    """
177
 
    dbconn = db.DB()
178
 
    if force:
179
 
        # Delete user if it exists
180
 
        try:
181
 
            dbconn.delete_user(login)
182
 
        except:
183
 
            pass
184
 
    dbconn.create_user(login, password, nick, fullname, rolenm, studentid)
185
 
    dbconn.close()