forked from oZONo32/EHCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp_server.py
executable file
·101 lines (76 loc) · 2.73 KB
/
ftp_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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Ftp server using python,
Authenticate users through ehcp database in mysql.
For use instead of vstfpd or other ftp server in ehcp.
Being tested now.
pip install pyftpdlib
apt-get install clamav
freshclam
"""
import logging
from classapp import Application
from pyftpdlib.authorizers import DummyAuthorizer # On error: pip install pyftpdlib
from pyftpdlib.authorizers import AuthenticationFailed
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import os,pwd,grp,time
try:
ftpuid = pwd.getpwnam("vsftpd").pw_uid
ftpgid = grp.getgrnam("www-data").gr_gid
except Exception, e:
print "Ftp kullanıcı ve grup tanımları yok.. "
raise
infected_dir="/var/www/new/infected"
"""
if not os.path.exists(infected_dir):
os.mkdir(infected_dir)
"""
virus_scan=False # clamav şuanda, en küçük dosyada bile 15 sn civarında sürüyor, bu haliyle kullanılması zor.
class My_Handler(FTPHandler):
def on_file_received(self, file):
print "file received:",file
os.chown(file, ftpuid, ftpgid)
if virus_scan: os.system("clamscan --move={} {}".format(infected_dir,file))
def on_incomplete_file_received(self, file):
print "incomplete file received:",file
os.chown(file, ftpuid, ftpgid)
if virus_scan: os.system("clamscan --move={} {}".format(infected_dir,file))
class Ehcp_Authorizer(DummyAuthorizer):
def validate_authentication(self, username, password, handler):
initdb()
wh="ftpusername='{}' and password('{}')=`password`".format(username,password)
say=app.kayitsayisi("ftpaccounts",wh)
#print wh,say
if say==0:
raise AuthenticationFailed("")
else:
print "validate_authentication: Login OK... "
def initdb():
try:
app.conn.execute("select now()")
except:
print "reconnecting..."
app.connecttodb()
app=Application()
authorizer = Ehcp_Authorizer()
q="select * from ftpaccounts"
for i in app.query(q):
homedir=i['homedir']
if homedir in [None,'']:
homedir="/var/www/vhosts/"+i['ftpusername']
print "User defined:",i['ftpusername'],homedir
if not os.path.exists(homedir): os.mkdir(homedir)
authorizer.add_user(i['ftpusername'],i['password'],homedir, perm="elradfmw")
#print authorizer.user_table
handler = My_Handler
handler.authorizer = authorizer
handler.banner = "Welcome to python based EHCP Ftp Server, managed by EHCP (Easy Hosting Control Panel, www.ehcp.net) (Beta)"
logging.basicConfig(filename='/var/log/ehcp_ftpd.log', level=logging.INFO)
print "Ftp server starting in 2 sec.. "
time.sleep(2)
server = FTPServer(("0.0.0.0", 2121), handler)
server.max_cons = 256
server.max_cons_per_ip = 5
server.serve_forever()