-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
313 lines (284 loc) · 8.97 KB
/
server.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
#ON START SERVER SHOULD BE SUPPLIED WITH CONTROL PORT NUMBER
from socket import *
from threading import Thread
import sys
import os
import shutil
import json
import getpass
import glob
global pam_import
try:
import pam
pam_import = 0
except ImportError:
pam_import = 1
global control_port
class State:
'''
This class contains the following variables
Hidden: self.command, self.dirlist, self.data, self.data_addr,
self.user_name
'''
def __init__(self, connectionSocket, addr, count):
global control_port
self.cwd = os.getcwd()
self.folder = os.path.basename(self.cwd)
self.control = connectionSocket
self.control_addr = addr
self.data_port = control_port + count
self.data_socket = socket(AF_INET, SOCK_STREAM)
self.data_socket.bind(('', self.data_port))
self.glob = False
def ls(state):
d = {}
dirlist = os.scandir()
for entry in dirlist:
if entry.is_dir():
d[entry.name] = "d"
else:
d[entry.name] = "f"
state.control.send(json.dumps(d).encode('ascii'))
def cd(state):
target = state.command[3:]
try:
os.chdir(target)
state.cwd = os.getcwd()
state.folder = target
state.control.send('OK'.encode('ascii'))
except Exception as e:
state.control.send(str(e).encode('ascii'))
def pwd(state):
state.control.send(state.cwd.encode('ascii'))
def data_connection(state):
state.control.send(str(state.data_port).encode('ascii'))
state.data_socket.listen(1)
state.data, state.data_addr = state.data_socket.accept()
def put(state):
target = state.command[4:]
state.control.send('200'.encode('ascii'))
response = state.control.recv(8).decode('ascii')
if(response == '201'):
return
elif(response == 'file'):
put_file(state, target)
elif(response == 'dir'):
put_dir(state, target)
def put_file(state, target):
try:
data_connection(state)
f = open(target, 'wb+')
data = state.data.recv(1024)
while data:
f.write(data)
data = state.data.recv(1024)
except Exception as e:
state.data.send(str(e).encode('ascii'))
finally:
state.data.close()
def put_dir(state, target):
try:
cwd = os.getcwd()
os.mkdir(target)
os.chdir(target)
list_files = state.control.recv(2048).decode('ascii')
l = json.loads(list_files)
if(l == '201'):
os.chdir(cwd)
return
else:
for target in l:
put_file(state, target)
except Exception as e:
print(e)
finally:
os.chdir(cwd)
def get(state):
target = state.command[4:]
if os.path.isfile(target):
state.control.send("file".encode('ascii'))
response = state.control.recv(4).decode('ascii')
get_file(state, target)
elif os.path.isdir(target):
state.control.send("dir".encode('ascii'))
response = state.control.recv(4).decode('ascii')
get_dir(state, target)
def rename(state):
target = state.command.split()
if os.path.isfile(target[1]):
if os.path.isfile(target[2]):
state.control.send("file2exist".encode('ascii'))
else:
os.renames(target[1], target[2])
state.control.send("success".encode('ascii'))
else:
state.control.send("!file1".encode('ascii'))
def get_dir(state, target):
try:
cwd = os.getcwd()
os.chdir(target)
d = {}
dirlist = os.scandir()
for entry in dirlist:
if entry.is_dir():
state.control.send(json.dumps('NESTED').encode('ascii'))
os.chdir(cwd)
return
d[entry.name] = "d"
else:
d[entry.name] = "f"
state.control.send(json.dumps(d).encode('ascii'))
for key, value in d.items():
print('Attempting to transfer ' + key + ' of type ' + value)
if value == 'f':
get_file(state, key)
else:
get_dir(state, key)
except Exception as e:
print(e)
state.control.send(str(e).encode('ascii'))
finally:
os.chdir(cwd)
def get_file(state, target):
try:
data_connection(state)
f = open(target, 'rb+')
l = f.read(1024)
while(l):
state.data.send(l)
l = f.read(1024)
except Exception as e:
state.data.send(str(e).encode('ascii'))
finally:
state.data.close()
def mget(state):
line = state.command[5:]
targets = line.split(" ")
l = []
if(state.glob == False):
for target in targets:
if(os.path.isfile(target)):
l.append(target)
else:
state.control.send('201'.encode('ascii'))
return
else:
for pattern in targets:
list = glob.glob(pattern)
for target in list:
l.append(target)
if(len(l) == 0):
state.control.send('202'.encode('ascii'))
return
state.control.send('200'.encode('ascii'))
response = state.control.recv(1024).decode('ascii')
state.control.send(json.dumps(l).encode('ascii'))
response = state.control.recv(1024).decode('ascii')
for target in l:
get_file(state, target)
def mput(state):
state.control.send('200'.encode('ascii'))
response = state.control.recv(8).decode('ascii')
if(response == '201'):
return
else:
state.control.send('200'.encode('ascii'))
text = state.control.recv(2048).decode('ascii')
l = json.loads(text)
for target in l:
put_file(state, target)
def mkdir(state):
target = state.command[6:]
try:
os.mkdir(target)
state.control.send('OK'.encode('ascii'))
except Exception as e:
state.control.send(str(e).encode('ascii'))
def rm(state):
target = state.command[3:]
try:
if os.path.isfile(target):
os.remove(target)
state.control.send('OK'.encode('ascii'))
else:
shutil.rmtree(target)
state.control.send('OK'.encode('ascii'))
except Exception as e:
state.control.send(str(e).encode('ascii'))
def toggle_glob(state):
if(state.glob == True):
state.glob = False
else:
state.glob = True
def system(state):
state.control.send(str(sys.platform).encode('ascii'))
def user(state):
authenticate_user(state, 1)
def authenticate_user(state, control_code):
state.user_name = getpass.getuser()
if control_code == 'new':
state.control.send(str(state.user_name).encode('ascii'))
user_name = state.control.recv(1024).decode('ascii')
user_pass = state.control.recv(1024).decode('ascii')
result = 'pass'
if pam.authenticate(user_name, user_pass):
state.control.send(str(result).encode('ascii'))
else:
result = 'fail'
state.control.send(str(result).encode('ascii'))
def connection(state):
print("New connection to client {}".format(state.control_addr))
state.control.send(str(pam_import).encode('ascii'))
if pam_import == 0:
authenticate_user(state, 'new')
try:
while True:
state.command = state.control.recv(1024).decode('ascii')
os.chdir(state.cwd)
#SWITCH BASED ON command
if(state.command == "bye"):
break
elif(state.command == "ls"):
ls(state)
elif(state.command[0:3] == "cd "):
cd(state)
elif(state.command == "pwd"):
pwd(state)
elif(state.command[0:4] == "get "):
get(state)
elif(state.command[0:6] == "mkdir "):
mkdir(state)
elif(state.command[0:3] == "rm "):
rm(state)
elif(state.command == "sys"):
system(state)
elif(state.command == "user"):
user(state)
elif(state.command[0:5] == "mget "):
mget(state)
elif(state.command == "glob"):
toggle_glob(state)
elif (state.command[0:7] == "rename "):
rename(state)
elif(state.command[0:4] == "put "):
put(state)
elif(state.command[0:5] == "mput "):
mput(state)
print('Connection closed to client {}'.format(state.control_addr))
state.control.close()
except Exception as e:
print(e)
if __name__ == '__main__':
global control_port
control_port = int(sys.argv[1])
control_socket = socket(AF_INET, SOCK_STREAM)
control_socket.bind(('', control_port))
control_socket.listen(10)
count = 1
while True:
connectionSocket, addr = control_socket.accept()
state = State(connectionSocket, addr, count)
t = Thread(target=connection, args=(state,))
t.start()
count += 1
control_socket.close()