63
59
template directory, recursively.
65
61
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
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: " +
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
67
userdir = os.path.join(conf.jail_base, username)
86
homedir = os.path.join(userdir, 'home')
88
68
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)
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)
132
80
def linktree(src, dst):
133
81
"""Recursively hard-link a directory tree using os.link().
169
117
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.
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."""
179
# Delete user if it exists
181
dbconn.delete_user(login)
184
122
dbconn.create_user(login, password, nick, fullname, rolenm, studentid)