forked from oZONo32/EHCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp_server2.py
executable file
·142 lines (105 loc) · 4.04 KB
/
ftp_server2.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
#!/usr/bin/python -u
# -*- coding: utf-8 -*-
"""
Ftp server using python, Coded by Ehcp, All rights reserved
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
Differences from ftp_server.py :
users may be reloaded by a USR1 system signal, no need to restart whole program..
handles pyftpdlib error: https://github.com/giampaolo/pyftpdlib/issues/400
"""
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,signal
prefix="Ehcp ftpserver:"
def log_print(s):
print s
logging.info(s)
def rebuild_users():
log_print(prefix+"Rebuilding/adding users:")
initdb()
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 prefix,"User defined:",i['ftpusername'],homedir
if not os.path.exists(homedir): os.mkdir(homedir)
authorizer.add_user(i['ftpusername'],i['password'],homedir, perm="elradfmw")
del(server.handler.authorizer)
server.handler.authorizer=authorizer
def signal_handler(signal, frame):
print "-"*20
log_print(prefix+"USR1 signal received..")
rebuild_users()
print "-"*20
signal.signal(signal.SIGUSR1, signal_handler)
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 prefix,"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 prefix,"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(app.esc(username),app.esc(password))
say=app.kayitsayisi("ftpaccounts",wh)
#print wh,say
if say==0:
print prefix,"Ehcp: Login failed to ftp server:",username
raise AuthenticationFailed("")
else:
print prefix,"validate_authentication: Login OK... "
def has_perm(self, username, perm, arg):
#print prefix,"has_perm:",username,perm,arg
#return super(Ehcp_Authorizer, self).has_perm(username,perm,arg)
# once user has loged in, it has all perm related to ftp..
return True
def get_msg_quit(self, username):
if not self.user_table.has_key(username):
return "" # because of https://github.com/giampaolo/pyftpdlib/issues/400
else:
return super(Ehcp_Authorizer, self).get_msg_quit(username)
def initdb():
try:
app.conn.execute("select now()")
except:
print prefix,"reconnecting..."
app.connecttodb()
app=Application()
handler = My_Handler
handler.banner = "Welcome to python based EHCP Ftp Server, managed by EHCP (Easy Hosting Control Panel, www.ehcp.net) (Beta)"
handler.authorizer = None
logging.basicConfig(filename='/var/log/ehcp_ftpd.log', level=logging.INFO)
print prefix,"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
rebuild_users()
server.serve_forever()