-
Notifications
You must be signed in to change notification settings - Fork 1
/
securecloudgroupshare.py
319 lines (285 loc) · 12.3 KB
/
securecloudgroupshare.py
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
317
318
319
import glob
import os.path
import cgi
import tempfile
import db_handler
import encrypt_handler
import commands
import send_mail
import cherrypy
from cherrypy.lib.static import serve_file
from auth import AuthController, require, member_of, name_is
groups = 5
header = """<html><title>Secure Cloud Group Share</title>
<head>
<link rel="stylesheet" href="/static/themes/downloaded_style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<h2><a href=/> Welcome to Secure Cloud</h2></a></div>
<body align="center">
<div class="wrapper">"""
footer = """</div></body></html> """
def check_login():
if cherrypy.session['cur_user'] != "":
header = """<html><title>Secure Cloud Group Share</title>
<head>
<link rel="stylesheet" href="/static/themes/downloaded_style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<h2> <a href=/>Welcome to Secure Cloud</a></h2>
<h4>Welcome %s, Your Group : %s<br><a href=/auth/logout>Logout</a> <a href=/remove>Delete Account</a></h4>
<body align="center">
<div class="wrapper">
""" % (str(cherrypy.session['cur_user']),str(cherrypy.session['group']))
else:
header = """<html><title>Secure Cloud Group Share</title>
<head>
<link rel="stylesheet" href="/static/themes/downloaded_style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<h2><a href=/> Welcome to Secure Cloud</a></h2>
<h4>Welcome Guest <br><a href=/auth/login>Login</a> </h4>
<body align="center">
<div class="wrapper">"""
return header
class NamedPart(cherrypy._cpreqbody.Part):
def make_file(self):
return tempfile.NamedTemporaryFile()
cherrypy._cpreqbody.Entity.part_class = NamedPart
class Root:
_cp_config = {
'tools.sessions.on': True,
'tools.auth.on': True
}
auth = AuthController()
@cherrypy.expose
@require()
def upload_view(self):
html = check_login()
html += """
<h2>Upload a file</h2><br>
<form action='/upload_file' method='post' enctype='multipart/form-data'>
File: <input type='file' name='videoFile'/> <br/>
<input type='submit' value='Upload'/>
</form>
<h2>Download a file</h2>
<a href='/'>Go to Downloads</a>
"""
html += footer
return html
@cherrypy.expose
@require()
def index(self, directory="static/download"):
html = check_login()
html += """<h2>Here are the files in the selected directory:</h2>
<h3><a href="/upload_view"> Upload Files </a></h3>
<table><tr><th>Files Available</th></tr>
<a href="/?directory=%s"> Parent Directory [..] </a></br></br>
""" % os.path.dirname(os.path.abspath(directory))
for filename in glob.glob(directory + '/*'):
absPath = os.path.abspath(filename)
if os.path.isdir(absPath):
html += '<tr><td><a href="/?directory=' + absPath + '">' + os.path.basename(filename) + "/[Dir]</a> </tr></td>"
else:
html += '<tr><td><a href="/index_download?filepath=' + absPath + '">' + os.path.basename(filename) + "</a> </tr></td>"
html += """ </table><br></div></body></html>"""
return html
@cherrypy.expose
def register(self):
page = header
page += """
<h2> Register</h2><br>
<form method="post" action="getdata">
Name : <input type="text" name="uname" required="required"/><br>
Password : <input type="password" name="upasswd" required="required"/><br>
Email : <input type="email" name="email" required="required"/><br>
Phone No. : <input type="number" name="phno" required="required"/><br>
Age : <input type="number" name="age" required="required"/><br>
Group : <select name="group"> """
for x in range(1,groups+1):
page += """ <option value=" """ + str(x) + """ " >""" + str(x) + """ </option>"""
page +="""</select><br>
<input type="hidden" value="add" name="action"/>
<input type="submit" value="Register">
</form></div></body></html>"""
return page
@cherrypy.expose
@require()
def remove(self):
page = check_login()
page += """
<h2> Remove</h2><br>
<form method="post" action="getdata">
Name : <input type="text" name="uname"/><br>
Password : <input type="password" value="" name="upasswd"/><br>
Email : <input type="email" name="email" required="required"/><br>
Phone No. : <input type="number" name="phno" required="required"/><br>
Age : <input type="number" name="age" required="required"/><br>
<input type="hidden" value="remove" name="action"/>
<input type="submit" value="Remove">
</form></div></body></html>"""
return page
@cherrypy.expose
def getdata(self,uname,upasswd,action,email,phno,age,group):
if action == "add":
e_pass = encrypt_handler.for_encrypt_pass(upasswd)
adduser = db_handler.add_user(uname,e_pass)
register = db_handler.register(uname,email,phno,age,group)
group_info = db_handler.insert_to_group(uname,group)
html = header
html += """ <h1>Status : %s <br> %s <br> %s</h1> """ % (adduser,register,group_info)
elif action == "remove":
adduser = db_handler.remove_user(uname)
unregister = db_handler.unregister(uname,email,phno,age)
remove_f_group = db_handler.delete_from_group(uname,group)
html = header
html += """ <h1>Status : %s <br> %s <br> %s </h1> """ % (adduser,unregister,remove_f_group)
html += "<h3><a href=/>Click to continue</a></h3></div></body></html>"
return html
@require()
@cherrypy.config(**{'response.timeout': 3600}) # default is 300s
@cherrypy.expose()
def upload_file(self, videoFile):
html = check_login()
assert isinstance(videoFile, cherrypy._cpreqbody.Part)
user = cherrypy.session['cur_user']
file = videoFile.filename
destination = os.path.join('/home/ateeq/Documents/SecureCloud/Secure-Cloud-Share-Python-2.7.x/static/download/', videoFile.filename)
try:
# Note that original link will be deleted by tempfile.NamedTemporaryFile
os.link(videoFile.file.name, destination)
out = db_handler.file_upload(user,file)
except OSError,e:
html += 'error occured : %s ' % (e)
html += footer
return html
html += "ok, got it filename='%s' , %s" % (videoFile.filename, out)
html += footer
return html
@cherrypy.expose
@require()
def index_download(self, filepath):
user = cherrypy.session['cur_user']
group = cherrypy.session['group']
file = os.path.basename(filepath)
html = check_login()
response = db_handler.file_download(user,file,group)
if response == 0:
cherrypy.session['file'] = filepath
html += """<h2> What do you wanna do with the file?</h2> <br>
<h3> %s </h3><br> Response : %s
<table><tr><td><a href=/df_temp?do=%s>Download</a></td>
<tr><td><a href=/delete_file?file=%s>Delete</a></td></tr>
<tr><td><a href=/share?file=%s>Share</a></td><tr>
<br>""" % (file,response,str(2730),filepath,filepath)
elif response == 1:
cherrypy.session['file'] = filepath
html += """ <h2>This file was Shared with You.</h2> <br>
<form method="post" action="ver_download">
<input type="hidden" value=" """ +user+ """ " name="user"/>
<input type="hidden" value=" """ +group+ """ "name="group"/>
Key : <input type="text" name="rstring"><br>
<input type="submit" value="Download">
</form>
"""
elif response == 2:
html += """ <h2> You are not the uploader!. <br> Hence, you are <b>not</b> authorized to do anything with the file! Sorry :( <br> Response = %s """ % response
html += footer
return html
@cherrypy.expose
@require()
def ver_download(self,user,rstring,group):
filepath = cherrypy.session['file']
cherrypy.session['file'] = ""
html = check_login()
file = os.path.basename(filepath)
aggre_key = encrypt_handler.get_d_aggre_key(user,rstring,group)
user = user.replace(' ','')
file = file.replace(' ','')
group = group.replace(' ','')
if db_handler.file_share_download(user,file,aggre_key,group):
#raise cherrypy.HTTPRedirect("/download_file?filepath=%s" % filepath)
#self.download_file(filepath)
return self.download_file(filepath)
else:
html += "<h2> Key not Valid!</h2> "
html += footer
return html
@cherrypy.expose
@require()
def delete_file(self,file):
html = check_login()
user = cherrypy.session['cur_user']
file_name= os.path.basename(file)
if commands.getoutput('rm -rf %s' % file) == '' and not os.path.isfile(file):
if db_handler.file_remove(user,file_name):
html += "<h2> File was deleted and ownership was removed! :)</h2>"
else:
html += "<h2> Something went wrong during DB handling! :(</h2>"
else:
html += "<h2> Something went wrong during Command execution! :(</h2>"
html += "<h3><a href=/>Go Back!</a></h3>"
html += footer
return html
@cherrypy.expose
@require()
def share(self,file):
file_name = os.path.basename(file)
user = cherrypy.session['cur_user']
group = cherrypy.session['group']
html = check_login()
aggre_keys = encrypt_handler.get_aggre_key(group)
html += """ <h2> File : %s </h2>""" % file_name
html += """ Select the Group you wanna share the file with <br>
<form method="post" action=/share_file >
<input type="hidden" value=" """ + file +""" " name="file">
Group : <select name="group">"""
for x in range(1,groups+1):
html += """ <option value=" """ + str(x) + """ " > """ + str(x) + """ </option>"""
html +="""</select><br> <input type=submit value="Share"></form>"""
html += footer
return html
@cherrypy.expose
@require()
def share_file(self, group, file):
html = check_login()
file_name = os.path.basename(file)
user = cherrypy.session['cur_user']
uemail = [db_handler.fetch_member_email(group)]
if uemail == [None]:
html += "No users in Group!"
else:
keys = encrypt_handler.get_aggre_key(group)
for i,j in zip(range(0,len(uemail[0].keys())),range(1,len(keys))):
out = db_handler.file_share(uemail[0].keys()[i],file_name,keys[j],group)
skey = keys[0]
print "rstring share file : %s" % skey
msg = " %s shared %s with you! key = %s " % (user,file_name,skey)
if out != "Share Exists!":
for u in uemail:
send_mail.send_email(u.values(),msg)
html += """ File Shared, \nResult : %s """ % out
else:
html += """ File Sharing Failed, \nResult : %s """ % out
html += footer
return html
@cherrypy.expose
@require()
def df_temp(self,do):
if do == str(2730):
filepath = cherrypy.session['file']
cherrypy.session['file'] = ""
return self.download_file(filepath)
else:
return "Not Authorized!"
@require()
def download_file(self,filepath):
filepath = filepath.strip()
if filepath != "":
return serve_file(filepath, "application/x-download", "attachment")
else:
return "No file Path Found! \nProbably used or Removed!"
tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
if __name__ == '__main__':
root = Root()
cherrypy.quickstart(root, config=tutconf)