~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-04 23:46:36 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:406
makeuser.py: make_jail now allows jails to already exist. In this case it will
    back up the user's home, vape the jail, rebuild it, then restore the
    user's home.

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
30
33
 
31
34
import os
32
35
import shutil
 
36
import warnings
33
37
 
34
38
import conf
35
39
import db
49
53
    if os.system("useradd -d %s '%s'" % (homedir, username)) != 0:
50
54
        raise Exception("Failed to add Unix user account")
51
55
 
52
 
def make_jail(username):
 
56
def make_jail(username, force=True):
53
57
    """Creates a new user's jail space, in the jail directory as configured in
54
58
    conf.py.
55
59
 
59
63
    template directory, recursively.
60
64
 
61
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.
62
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
    
63
74
    templatedir = os.path.join(conf.jail_base, 'template')
64
75
    if not os.path.isdir(templatedir):
65
76
        raise Exception("Template jail directory does not exist: " +
66
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)
67
85
    userdir = os.path.join(conf.jail_base, username)
 
86
    homedir = os.path.join(userdir, 'home')
 
87
 
68
88
    if os.path.exists(userdir):
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
 
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
79
131
 
80
132
def linktree(src, dst):
81
133
    """Recursively hard-link a directory tree using os.link().