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

« back to all changes in this revision

Viewing changes to www/common/makeuser.py

  • Committer: drtomc
  • Date: 2008-02-04 23:18:34 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:405
Version 0 of the file manipulation tools to allow us to get the permissions right.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
# * Unix user account
28
28
 
29
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
30
 
34
31
import os
35
32
import shutil
36
 
import warnings
37
33
 
38
34
import conf
39
35
import db
53
49
    if os.system("useradd -d %s '%s'" % (homedir, username)) != 0:
54
50
        raise Exception("Failed to add Unix user account")
55
51
 
56
 
def make_jail(username, force=True):
 
52
def make_jail(username):
57
53
    """Creates a new user's jail space, in the jail directory as configured in
58
54
    conf.py.
59
55
 
63
59
    template directory, recursively.
64
60
 
65
61
    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
62
    """
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
63
    templatedir = os.path.join(conf.jail_base, 'template')
75
64
    if not os.path.isdir(templatedir):
76
65
        raise Exception("Template jail directory does not exist: " +
77
66
            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
67
    userdir = os.path.join(conf.jail_base, username)
86
 
    homedir = os.path.join(userdir, 'home')
87
 
 
88
68
    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
 
69
        raise Exception("User's jail directory already exists: " +
 
70
            userdir)
 
71
 
 
72
    # Hard-link (copy aliasing) the entire tree over
 
73
    linktree(templatedir, userdir)
 
74
 
 
75
    # Set up the user's home directory
 
76
    homedir = os.path.join(userdir, 'home', username)
 
77
    os.mkdir(homedir)
 
78
    return homedir
131
79
 
132
80
def linktree(src, dst):
133
81
    """Recursively hard-link a directory tree using os.link().
168
116
    if errors:
169
117
        raise Exception, errors
170
118
 
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
 
    """
 
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."""
177
121
    dbconn = db.DB()
178
 
    if force:
179
 
        # Delete user if it exists
180
 
        try:
181
 
            dbconn.delete_user(login)
182
 
        except:
183
 
            pass
184
122
    dbconn.create_user(login, password, nick, fullname, rolenm, studentid)
185
123
    dbconn.close()