-
Notifications
You must be signed in to change notification settings - Fork 1
/
FTPD.py
278 lines (230 loc) · 9.12 KB
/
FTPD.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
from pyftpdlib.authorizers import *
from pyftpdlib.handlers import *
from pyftpdlib.filesystems import *
from pyftpdlib.ioloop import *
from pyftpdlib.servers import *
import os
class FTP_SERVER(DummyAuthorizer, UnixAuthorizer, FTPServer, FTPHandler, DTPHandler):
__dummy_authorizer = DummyAuthorizer()
__unix_authorizer = UnixAuthorizer(rejected_users=["root"])
__ftp_handler = FTPHandler
__DTP_handler = DTPHandler
__throttledDTP_handler = ThrottledDTPHandler
__TLS_FTP_handler = TLS_FTPHandler
# __TLS_DTP_handler = TLS_DTPHandler
address = "0.0.0.0"
port=2121
bIsAnonymus = False
shandlerType = "FTP"
sAuthorizer = "Unix"
sServerType = "Threaded"
def __init__(self, sAddress="0.0.0.0", sPort="21", sType="Threaded"):
#Default start up
self.SetHandler(self.shandlerType)
self.SetAuthorizer(self.sAuthorizer)
self.IsAnonymousMode(False)
self.SetBanner("ftpd service ready!")
self.port=sPort
self.address=sAddress
self.SetServer(sType)
self.SetMaxConnections(256)
self.SetMaxConnectionsPerIp(5)
self.LogLevel = "INFO"
self.LogFilePath = os.getcwd()+"/"
self.SetLogFile(self.LogFilePath+"pyftpd.log", self.LogLevel)
def GetLogFilePath(self):
return self.LogFilePath
def SetHandler(self, sHandler):
if sHandler == "FTP":
self.__handler = self.__ftp_handler
self.shandlerType="FTP"
elif sHandler == "Throttled":
self.__handler = self.__ftp_handler
self.__handler.dtp_handler = self.__throttledDTP_handler
self.shandlerType="Throttled"
elif sHandler == "DTP":
self.__handler = self.__ftp_handler
self.__handler.dtp_handler = self.__DTP_handler
self.shandlerType="DTP"
elif sHandler == "TLS":
self.__handler = self.__TLS_FTP_handler
self.shandlerType = "TLS"
def GetHandlerType(self):
return self.shandlerType
def SetAuthorizer(self, sAuthorizer):
if sAuthorizer == "Dummy":
self.__handler.authorizer=""
self.__handler.authorizer = self.__dummy_authorizer
self.sAuthorizer="Dummy"
elif sAuthorizer == "Unix":
self.__handler.authorizer=""
self.__handler.authorizer = self.__unix_authorizer
# self.__handler.abstracted_fs = UnixFilesystem
self.sAuthorizer="Unix"
def GetAuthorizer(self):
return self.sAuthorizer
##### DummyAuthorizer Users
def AddUser(self, sUser, sPassword, sHomedir, sPerm, sMsgLogin=None, sMsgQuit=None):
self.__dummy_authorizer.add_user(sUser, sPassword, sHomedir, perm=sPerm, msg_login=sMsgLogin, msg_quit=sMsgQuit)
def IsAnonymousMode(self, b):
self.bIsAnonymus = b
def GetIsAnonymousMode(self):
return self.bIsAnonymus
def RemoveAnonymous(self):
if self.__dummy_authorizer.has_user("anonymous"):
self.__dummy_authorizer.remove_user("anonymous")
def AddAnonyous(self, sHomedir):
self.__dummy_authorizer.add_anonymous(sHomedir)
def RemoveUser(self, sUsername):
self.__dummy_authorizer.remove_user(sUsername)
#####
### TLS_FTP Handler
def SetCertificate(self, sFile):
self.__TLS_FTP_handler.certfile = sFile
def GetCertificate(self):
return self.__TLS_FTP_handler.certfile
def SetKeyFile(self, sFile):
self.__TLS_FTP_handler.keyfile=sFile
def GetKeyFile(self):
return self.__TLS_FTP_handler.keyfile
def IsTLSControlRequired(self, b):
self.__TLS_FTP_handler.tls_control_required=b
def GetIsTLSControlRequired(self):
return self.__TLS_FTP_handler.tls_control_required
def IsTLSDataRequired(self,b):
self.__TLS_FTP_handler.tls_data_required=b
def GetIsTLSDataRequired(self):
return self.__TLS_FTP_handler.tls_data_required
###
### DTP handler
def SetDTPTimeout(self, iSeconds):
self.__DTP_handler.timeout=iSeconds
def GetDTPTimeout(self):
return self.__DTP_handler.timeout
def SetInBufferSize(self, iSize):
self.__DTP_handler.ac_in_buffer_size = iSize
def GetInBufferSize(self):
return self.__DTP_handler.ac_in_buffer_size
def SetOutBufferSize(self, iSize):
self.__DTP_handler.ac_out_buffer_size = iSize
def GetOutBufferSize(self):
return self.__DTP_handler.ac_out_buffer_size
###
### ThrottledDTP handler
def SetReadLimit(self, iLimit):
self.__throttledDTP_handler.read_limit = iLimit
def GetReadLimit(self):
return self.__throttledDTP_handler.read_limit
def SetWriteLimit(self, iLimit):
self.__throttledDTP_handler.write_limit = iLimit
def GetWriteLimit(self):
return self.__throttledDTP_handler.write_limit
###
##### FTPHandler Control connection
def SetFTPimeout(self, iSeconds):
self.__ftp_handler.timeout = iSeconds
def GetFTPTimeout(self):
return self.__ftp_handler.timeout
def SetMaxLogginAtempts(self, iNum):
self.__ftp_handler.max_login_attempts = iNum
def GetMaxLogginAtempts(self):
return self.__ftp_handler.max_login_attempts
def PermitForeignAddresses(self, b):
self.__ftp_handler.permit_foreign_addresses=b
def GetPermitForeignAddresses(self):
return self.__ftp_handler.permit_foreign_addresses
def PermitPrivilegedPorts(self, b):
self.__ftp_handler.permit_privileged_ports=b
def GetPermitPrivilegedPorts(self):
return self.__ftp_handler.permit_privileged_ports
def SetMasqueradeAddress(self, sIp):
self.__ftp_handler.masquerade_address=sIp
def GetMasqueradeAddress(self):
return self.__ftp_handler.masquerade_address
#TODO Implement in PyQT and ui
def SetMasqueradeAddressMap(self, MapDictionary):
self.__ftp_handler.masquerade_address_map=MapDictionary
def GetMasqueradeAddressMap(self):
return self.__ftp_handler.masquerade_address_map
def SetRangePassivePorts(self, iStart, iStop):
self.__ftp_handler.passive_ports = range(iStart, iStop)
def GetRangePassivePorts(self):
return self.__ftp_handler.passive_ports
def UseGMTTimes(self, b):
self.__ftp_handler.use_gmt_times=b
def GetGMTTimes(self):
return self.__ftp_handler.use_gmt_times
def SetTCPNoDelay(self, b):
self.__ftp_handler.tcp_no_delay=b
def GetSetTCPNoDelay(self):
return self.__ftp_handler.tcp_no_delay
def UseSendFile(self,b):
self.__ftp_handler.use_sendfile=b
def GetUseSendFile(self):
return self.__ftp_handler.use_sendfile
def SetAuthFailedTimeout(self, iSeconds):
self.__ftp_handler.auth_failed_timeout=iSeconds
def GetAuthFailedTimeout(self):
return self.__ftp_handler.auth_failed_timeout
def SetBanner(self, sMsg):
self.__ftp_handler.banner = sMsg
def GetBanner(self):
return self.__ftp_handler.banner
###
def SetAddress(self, sIp):
self.address=sIp
def GetAddress(self):
return self.address
def SetPort(self, iPort):
self.port=iPort
def GetPort(self):
return self.port
### Server
def SetServer(self, sServerType=None):
if sServerType == "Normal":
self.sServerType="Normal"
self.server= FTPServer((self.address, self.port), self.__handler)
elif sServerType == "Threaded":
self.sServerType=="Threaded"
self.server = ThreadedFTPServer((self.address, self.port), self.__handler)
elif sServerType == "Multiprocess":
self.sServerType="Multiprocess"
self.server = MultiprocessFTPServer((self.address, self.port), self.__handler)
else: #default Normal
self.sServerType="Normal"
self.server= FTPServer((self.address, self.port), self.__handler)
def SetMaxConnections(self, iNum=512):
self.server.max_cons=iNum
def GetMaxConnections(self):
return self.server.max_cons
def SetMaxConnectionsPerIp(self, iNum=0):
self.server.max_cons_per_ip=iNum
def GetMaxConnectionsPerIp(self):
return self.server.max_cons_per_ip
def StartServer(self):
self.server.serve_forever()
def Close(self):
self.server.close()
def CloseAll(self):
self.server.close_all()
###
def SetLogFile(self, fname, lvl=None):
import logging
logger = logging.getLogger('pyftpdlib')
hdlr = logging.FileHandler(fname)
frm = logging.Formatter("[%(asctime)s];%(name)s;%(levelname)s;%(message)s", "%d-%m-%Y %H:%M:%S")
hdlr.setFormatter(frm)
logger.setLevel(logging.INFO)
if lvl == "INFO":
logger.setLevel(logging.INFO)
elif lvl == "CRITICAL":
logger.setLevel(logging.CRITICAL)
elif lvl == "ERROR":
logger.setLevel(logging.ERROR)
elif lvl == "WARNING":
logger.setLevel(logging.WARNING)
elif lvl == "DEBUG":
logger.setLevel(logging.DEBUG)
else: #Default INFO
logger.setLevel(logging.INFO)
logger.addHandler(hdlr)