-
Notifications
You must be signed in to change notification settings - Fork 0
/
vacdepo-cgi
executable file
·221 lines (187 loc) · 6.69 KB
/
vacdepo-cgi
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
#!/usr/bin/python
#
# vacdepo-cgi - HTTPS depository service
#
# Andrew McNab, University of Manchester.
# Copyright (c) 2013-9. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# o Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
# o Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# This software is part of the Vac project:
# https://www.gridpp.ac.uk/vac/
#
import os
import re
import sys
import time
import tempfile
if '/../' in os.environ['REQUEST_URI']:
# Don't even go there...
print 'Status: 404 Not Found'
print
print 'Not Found'
sys.exit(0)
try:
firstDir = os.environ['REQUEST_URI'].split('/')[1]
filePath = os.environ['REQUEST_URI'].replace('//','/').split('?')[0]
except:
print 'Status: 404 Not Found'
print
print 'Not Found'
sys.exit(0)
try:
# There must be a vacdepo-readers list of X.509 DNs for read control
# to be enabled
readList = open('/etc/vacdepo-readers/' + firstDir, 'r').read().splitlines()
except:
pass
else:
foundDN = False
if 'SSL_CLIENT_S_DN' in os.environ and os.environ['SSL_CLIENT_S_DN']:
# Look for DN or DN+'/CN=...' (ie proxy) in access list
for readListDN in readList:
if os.environ['SSL_CLIENT_S_DN'] == readListDN or os.environ['SSL_CLIENT_S_DN'].startswith(readListDN + '/CN='):
foundDN = True
break
if not foundDN:
# Unless we found a matching DN then refuse
print 'Status: 403 Forbidden'
print
print 'Forbidden - read access not allowed'
sys.exit(0)
if os.environ['REQUEST_URI'] == '/':
# No browsing of the first level directory names
print 'Status: 403 Directory listing forbidden'
print
print 'Directory listing forbidden'
sys.exit(0)
if os.environ['REQUEST_METHOD'] == 'GET' or os.environ['REQUEST_METHOD'] == 'HEAD':
absoluteFilePath = os.environ['DOCUMENT_ROOT'] + '/' + filePath
if os.path.isfile(absoluteFilePath):
print 'Status: 200 OK'
stat = os.stat(absoluteFilePath)
print 'Content-Length: ' + str(stat.st_size)
print 'Content-Type: text/plain'
print 'Last-Modified: ' + time.strftime("%a, %d %b %Y %T GMT", time.gmtime(stat.st_mtime))
print
if os.environ['REQUEST_METHOD'] == 'GET':
try:
print open(absoluteFilePath, 'r').read()
except:
pass
elif os.path.isdir(absoluteFilePath) and not os.environ['REQUEST_URI'].endswith('/'):
print 'Status: 301 Moved Permanently'
print 'Location: https://' + \
os.environ['SERVER_NAME'] + \
('' if os.environ['SERVER_PORT'] == '443' else (':' + os.environ['SERVER_PORT'])) + \
os.environ['REQUEST_URI'] + '/'
print
elif os.path.isdir(absoluteFilePath):
print 'Status: 200 OK'
print 'Content-Type: text/html'
print
print '<html><head><title>Directory ' + os.environ['REQUEST_URI'] + '</title></head>'
print '<body><h1>Directory ' + os.environ['REQUEST_URI'] + '</h1>'
try:
os.chdir(absoluteFilePath)
dirList = sorted(os.listdir('.'), key=os.path.getmtime, reverse=True)
except:
pass
else:
print '<pre>'
for fileName in dirList:
print time.asctime(time.gmtime(os.path.getmtime(fileName))) \
+ ' <a href="' + fileName + '">' + fileName + '</a>'
print '</pre>'
print '</body></html>'
else:
print 'Status: 404 Not Found'
print
print 'Not Found'
sys.exit(0)
# Writing methods have extra restrictions
if 'SSL_CLIENT_S_DN' not in os.environ or not os.environ['SSL_CLIENT_S_DN']:
print 'Status: 403 Forbidden - no client X.509 authentication'
print
print 'Forbidden - no client X.509 authentication'
sys.exit(0)
try:
# There must be a vacdepo-writers list of X.509 DNs for this first level
# directory for writing to be allowed in it
writeList = open('/etc/vacdepo-writers/' + firstDir, 'r').read().splitlines()
except:
print 'Status: 403 Forbidden - write access not enabled'
print
print 'Forbidden - write access not enabled'
sys.exit(0)
# Look for DN or DN+'/CN=...' (ie proxy) in access list
foundDN = False
for writeListDN in writeList:
if os.environ['SSL_CLIENT_S_DN'] == writeListDN or os.environ['SSL_CLIENT_S_DN'].startswith(writeListDN + '/CN='):
foundDN = True
break
if not foundDN:
# X.509 DN must be on the vacdepo-writers list for this first level directory
# for writing here to be allowed
print 'Status: 403 Forbidden - write access not allowed'
print
print 'Forbidden - write access not allowed'
sys.exit(0)
if os.environ['REQUEST_METHOD'] == 'PUT':
absoluteFilePath = os.environ['DOCUMENT_ROOT'] + filePath
try:
os.makedirs('/'.join(absoluteFilePath.split('/')[:-1]))
except:
pass
tempFileName = None
try:
# Try atomic update of absoluteFilePath using a temporary file and a rename
fd, tempFileName = tempfile.mkstemp(prefix = '/'.join(absoluteFilePath.split('/')[:-1]) + '/.vacdepo.')
os.write(fd, sys.stdin.read())
os.close(fd)
os.rename(tempFileName, absoluteFilePath)
except:
try:
os.remove(tempFileName)
except:
pass
print 'Status: 500 Internal Server Error writing file'
print
print 'Internal Server Error writing file'
else:
# All parent directories get time stamp updated too
recursePath = os.environ['DOCUMENT_ROOT']
for dir in filePath.split('/')[1:-1]:
recursePath += '/' + dir
os.utime(recursePath, None)
print 'Status: 200 OK'
print
sys.exit(0)
print 'Status: 405 Method not allowed'
print
print 'Method not allowed'
sys.exit(0)