-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathsave.py
More file actions
316 lines (288 loc) · 10.9 KB
/
Copy pathsave.py
File metadata and controls
316 lines (288 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
import sys, os, cgi, cgitb, re, json, base64, time, shutil, string
progpath = os.path.dirname(__file__)
sys.path.append(progpath)
import config
rootcachename = os.path.join(config.cachedir, 'rootcache')
def uri_without_query(uri):
return uri.split('?', 1)[0]
def splituri(uri):
return [p for p in uri_without_query(uri).split('/') if p]
def filename_from_uri(uri):
return '/'.join(splituri(uri)[1:])
# Form fields:
# file="parent/dir/filename.ext" - The destination location.
# data="hello world.\nthis is a file.\n' - Data to write.
# key="888" - Short (insecure) password key to authorize user.
# source="other/dir/filename.ext" - File or directory to copy or move.
# sourcekey - For mv, authorization for the source file.
# mode=
# "a" to append data,
# "mv" to move source,
# "rmtree" to remove dir.
# "setkey" to set key.
def application(env, start_response):
form = cgi.FieldStorage(
fp=env['wsgi.input'], environ=env, keep_blank_values=True)
request_uri = env['REQUEST_URI']
host_name = env['HTTP_HOST']
filename = form.getfirst("file", filename_from_uri(request_uri))
data = form.getfirst("data", None)
callback = form.getfirst("callback", None)
sourcefile = form.getfirst("source", None)
mode = form.getfirst("mode", None)
conditional = form.getfirst("conditional", None)
key = form.getfirst("key", '')
sourcekey = form.getfirst("sourcekey", key)
user = '.'.join(host_name.split('.')[:-2])
domain = '.'.join(host_name.split('.')[-2:])
origfilename = filename
if len(user) is 0:
user = None
try:
os.remove(rootcachename)
except:
pass
def jsonout(dict):
if callback is not None:
return callback + '(' + json.dumps(dict) + ')'
else:
return json.dumps(dict)
class ImmediateReturnException(Exception):
def __init__(self, message, json):
super(ImmediateReturnException, self).__init__(message)
self.json = json
def errorexit(s):
raise ImmediateReturnException(s, exitdata({'error': s}))
def exitdata(d):
start_response('200 OK', [
('Content-Type', 'text/javascript')
])
return jsonout(d)
def filenameuser(fn):
m = re.match(r'^([\w][\w\.\-]*)(?:/.*)?$', filename)
if m is None:
return None
return m.group(1)
def validkey(user, key):
keydir = os.path.join(config.datadir, user, '.key')
if not os.path.isdir(keydir):
return True
keys = os.listdir(keydir)
if len(keys) == 0:
return True
for authorized in keys:
if key.startswith(authorized[1:]):
return True
def setkey(user, oldkey, newkey):
if oldkey == newkey:
return
keydir = os.path.join(config.datadir, user, '.key')
try:
if not os.path.isdir(keydir):
checkreserveduser(user)
os.makedirs(keydir)
if oldkey is not None:
keys = os.listdir(keydir)
for authorized in keys:
if oldkey.startswith(authorized[1:]):
os.remove(os.path.join(keydir, authorized))
if newkey is not None:
keyfile = os.path.join(keydir, 'k' + newkey)
open(keyfile, 'w').close()
except OSError, exc:
errorexit('Could not set key.')
def checkreserveduser(user):
if os.path.isdir(os.path.join(config.datadir, user)):
return
if user.lower() != user:
errorexit('Username should be lowercase.')
normalized = user.lower()
if os.path.isdir(os.path.join(config.datadir, normalized)):
errorexit('Username is reserved.')
normalized = user.lower()
if normalized != user and os.path.isdir(
os.path.join(config.datadir, normalized)):
errorexit('Username is reserved.')
normalizedi = normalized.translate(string.maketrans('013456789', 'oieasbtbg'))
if normalized != normalizedi and os.path.isdir(
os.path.join(config.datadir, normalizedi)):
errorexit('Username is reserved.')
normalizedl = normalized.translate(string.maketrans('013456789', 'oleasbtbg'))
if normalizedl != normalized and os.path.isdir(
os.path.join(config.datadir, normalizedl)):
errorexit('Username is reserved.')
with open(os.path.join(progpath, 'bad-words.txt')) as f:
badwords = f.read().splitlines()
if any(word in badwords for word in [normalized, normalizedi, normalizedl]):
errorexit('Username is reserved.')
with open(os.path.join(progpath, 'bad-substrings.txt')) as f:
badsubstrings = f.read().splitlines()
if any(substring in word
for word in [normalized, normalizedi, normalizedl]
for substring in badsubstrings):
errorexit('Username is reserved.')
return
try:
# Validate params
if data is not None and sourcefile is not None:
errorexit('Cannot supply both data and source.')
if mode is not None and mode not in (
sourcefile and ['mv'] or
data and ['a', 'setkey'] or
(not data and not sourcefile) and ['rmtree', 'setkey']):
errorexit('Illegal mode %s.' % mode + repr(data))
if conditional is not None:
if not re.match(r'^\d+(?:\.\d*)?$', conditional):
errorexit('Illegal conditional %s.' % conditional)
conditional = float(conditional)
# Validate a good username
if user is not None:
if not re.match(r'^[A-Za-z]\w{2,}$', user):
errorexit('Bad username "%s".' % user)
filename = os.path.join(user, filename)
# Validate a good filename: must have a leading dir.
topdir = False
if not re.match(r'^(?:[\w][\w\.\-]*)(?:/[\w][\w\.\-]*)+/?$', filename):
# The rmtree and mv case are allowed to be a bare leading dir, subject
# to additional checks.
if (mode == 'setkey' or (data is None and (
mode in ['rmtree', 'mv'] or (sourcefile is not None))) and
re.match(r'^[\w][\w\\-]*/?$', filename)):
topdir = True
else:
errorexit('Bad filename "%s".' % filename)
absfile = os.path.join(config.datadir, filename)
if not absfile.startswith(config.datadir):
errorexit('Illegal filename "%s".' % filename)
userdir = None
if user:
userdir = os.path.join(config.datadir, user)
# Validate that the user's key matches the supplied key
if not validkey(user, key):
if not key:
return exitdata({'error': 'Password protected.', 'needauth': 'key'})
else:
return exitdata({'error': 'Incorrect password.', 'needauth': 'key'})
# Handle the setkey case
if mode == 'setkey':
if not topdir:
errorexit('Can only set key on a top-level user directory.')
setkey(user, key, data)
return exitdata(data is None and {'keycleared': user} or {'keyset': user});
# Handle the copy/move case
if sourcefile is not None:
if not re.match(r'^(?:[\w][\w\.\-]*)(?:/[\w][\w\.\-]*)*/?$', sourcefile):
errorexit('Bad source filename "%s".' % sourcefile)
sourceuser = filenameuser(sourcefile)
abssourcefile = os.path.join(config.datadir, sourcefile)
if not abssourcefile.startswith(config.datadir):
errorexit('Illegal source filename "%s".' % sourcefile)
if not os.path.exists(abssourcefile):
errorexit('Source file "%s" does not exist.' % sourcefile)
# Validate that only directories can be copied or moved to the top.
if topdir and not os.path.isdir(abssourcefile):
errorexit('Bad filename "%s".' % filename)
# mv requires authorization on source dir
if mode == 'mv':
if not validkey(sourceuser, sourcekey):
if not key:
return exitdata({'error': 'Source password protected.', 'auth': 'key'})
else:
return exitdata({'error': 'Incorrect source password.', 'auth': 'key'})
# create target parent directory
if not os.path.isdir(os.path.dirname(absfile)):
checkreserveduser(user)
try:
os.makedirs(os.path.dirname(absfile))
except OSError, exc:
errorexit('Could not create dir "%s".' % os.path.dirname(filename))
# move case
if mode == 'mv':
if os.path.exists(absfile):
errorexit('Cannot replace existing file "%s".' % filename)
try:
shutil.move(abssourcefile, absfile)
try:
os.removedirs(os.path.dirname(abssourcefile))
except OSError, exc:
pass
# remove .key when moving top dir into deeper dir
if topdir and filename != user:
shutil.rmtree(os.path.join(absfile, '.key'))
except OSError, exc:
errorexit('Could not move "%s" to "%s".' % (sourcefile, filename))
# copy case
else:
try:
if os.path.isdir(abssourcefile):
if os.path.exists(absfile):
errorexit('Cannot overwrite existing directory "%s".' % filename)
shutil.copytree(abssourcefile, absfile, ignore_patterns('.key'))
else:
shutil.copy2(abssourcefile, absfile)
except OSError, exc:
errorexit('Could not copy "%s" to "%s".' % (sourcefile, filename))
try:
os.utime(userdir, None)
except OSError, exc:
pass
return exitdata({'saved' : filename})
# Enforce the conditional request
if conditional:
if os.path.exists(absfile):
mtime = os.stat(absfile).st_mtime
if mtime > conditional:
return exitdata({'error': 'Did not overwrite newer file.', 'newer': mtime})
# Handle the delete case
if not data:
if not form.has_key('data'):
errorexit('Missing data= cgi argument.')
if os.path.exists(absfile):
try:
if os.path.isdir(absfile):
if mode == 'rmtree':
shutil.rmtree(absfile)
else:
os.rmdir(absfile)
else:
os.remove(absfile)
except OSError, exc:
errorexit('Could not remove "' + absfile + '".')
try:
os.removedirs(os.path.dirname(absfile))
except OSError, exc:
pass
try:
if userdir != absfile:
os.utime(userdir, None)
except OSError, exc:
pass
return exitdata({'deleted' : filename})
# Validate data
if len(data) >= 1024 * 1024:
errorexit('Data too large.')
# Handle the create/replace case
if not os.path.isdir(os.path.dirname(absfile)):
checkreserveduser(user)
try:
os.makedirs(os.path.dirname(absfile))
except OSError, exc:
errorexit('Could not create dir "' + os.path.dirname(filename) + '".')
try:
f = file(absfile, (mode == 'a' and 'a' or 'w'))
f.write(data)
f.flush()
os.fsync(f.fileno());
fs = os.fstat(f.fileno())
f.close()
except IOError, err:
errorexit('Error writing file "' + absfile + '".')
try:
os.utime(userdir, None)
except OSError, exc:
pass
return exitdata(
{'saved' : filename, 'mtime': fs.st_mtime, 'size': fs.st_size})
except ImmediateReturnException, e:
return e.json