59
63
template directory, recursively.
61
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")
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: " +
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):
67
85
userdir = os.path.join(conf.jail_base, username)
86
homedir = os.path.join(userdir, 'home')
68
88
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)
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)
80
132
def linktree(src, dst):
81
133
"""Recursively hard-link a directory tree using os.link().