forked from aaPanel/BaoTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp.py
137 lines (125 loc) · 6.45 KB
/
ftp.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
#coding: utf-8
# + -------------------------------------------------------------------
# | 宝塔Linux面板
# + -------------------------------------------------------------------
# | Copyright (c) 2015-2016 宝塔软件(http:#bt.cn) All rights reserved.
# + -------------------------------------------------------------------
# | Author: hwliang <[email protected]>
# + -------------------------------------------------------------------
import public,db,re,os,firewalls
try:
from BTPanel import session
except: pass
class ftp:
__runPath = None
def __init__(self):
self.__runPath = '/www/server/pure-ftpd/bin'
#添加FTP
def AddUser(self,get):
try:
if not os.path.exists('/www/server/pure-ftpd/sbin/pure-ftpd'): return public.returnMsg(False,'请先到软件商店安装Pure-FTPd服务')
import files,time
fileObj=files.files()
if get['ftp_username'].strip().find(' ') != -1: return public.returnMsg(False,'FTP用户名中不能包含空格')
if re.search("\W+",get['ftp_username']): return {'status':False,'code':501,'msg':public.getMsg('FTP_USERNAME_ERR_T')}
if len(get['ftp_username']) < 3: return {'status':False,'code':501,'msg':public.getMsg('FTP_USERNAME_ERR_LEN')}
if not fileObj.CheckDir(get['path']): return {'status':False,'code':501,'msg':public.getMsg('FTP_USERNAME_ERR_DIR')}
if public.M('ftps').where('name=?',(get.ftp_username.strip(),)).count(): return public.returnMsg(False,'FTP_USERNAME_ERR_EXISTS',(get.ftp_username,))
username = get['ftp_username'].strip()
password = get['ftp_password'].strip()
if len(password) < 6: return public.returnMsg(False,'FTP密码长度不能少于6位!')
get.path = get['path'].replace(' ','')
get.path = get.path.replace("\\", "/")
fileObj.CreateDir(get)
public.ExecShell('chown www.www ' + get.path)
public.ExecShell(self.__runPath + '/pure-pw useradd ' + username + ' -u www -d ' + get.path + '<<EOF \n' + password + '\n' + password + '\nEOF')
self.FtpReload()
ps = public.xssencode2(get['ps'])
if get['ps']=='': ps= public.getMsg('INPUT_PS');
addtime=time.strftime('%Y-%m-%d %X',time.localtime())
pid = 0
if hasattr(get,'pid'): pid = get.pid
public.M('ftps').add('pid,name,password,path,status,ps,addtime',(pid,username,password,get.path,1,ps,addtime))
public.WriteLog('TYPE_FTP', 'FTP_ADD_SUCCESS',(username,))
return public.returnMsg(True,'ADD_SUCCESS')
except Exception as ex:
public.WriteLog('TYPE_FTP', 'FTP_ADD_ERR',(username,str(ex)))
return public.returnMsg(False,'ADD_ERROR')
#删除用户
def DeleteUser(self,get):
try:
username = get['username']
id = get['id']
public.ExecShell(self.__runPath + '/pure-pw userdel ' + username)
self.FtpReload()
public.M('ftps').where("id=?",(id,)).delete()
public.WriteLog('TYPE_FTP', 'FTP_DEL_SUCCESS',(username,))
return public.returnMsg(True, "DEL_SUCCESS")
except Exception as ex:
public.WriteLog('TYPE_FTP', 'FTP_DEL_ERR',(username,str(ex)))
return public.returnMsg(False,'DEL_ERROR')
#修改用户密码
def SetUserPassword(self,get):
try:
id = get['id']
username = get['ftp_username'].strip()
password = get['new_password'].strip()
if len(password) < 6: return public.returnMsg(False,'FTP密码长度不能少于6位!')
public.ExecShell(self.__runPath + '/pure-pw passwd ' + username + '<<EOF \n' + password + '\n' + password + '\nEOF')
self.FtpReload()
public.M('ftps').where("id=?",(id,)).setField('password',password)
public.WriteLog('TYPE_FTP', 'FTP_PASS_SUCCESS',(username,))
return public.returnMsg(True,'EDIT_SUCCESS')
except Exception as ex:
public.WriteLog('TYPE_FTP', 'FTP_PASS_ERR',(username,str(ex)))
return public.returnMsg(False,'EDIT_ERROR')
#设置用户状态
def SetStatus(self,get):
msg = public.getMsg('OFF')
if get.status != '0': msg = public.getMsg('ON')
try:
id = get['id']
username = get['username']
status = get['status']
if int(status)==0:
public.ExecShell(self.__runPath + '/pure-pw usermod ' + username + ' -r 1')
else:
public.ExecShell(self.__runPath + '/pure-pw usermod ' + username + " -r ''")
self.FtpReload()
public.M('ftps').where("id=?",(id,)).setField('status',status)
public.WriteLog('TYPE_FTP','FTP_STATUS', (msg,username))
return public.returnMsg(True, 'SUCCESS')
except Exception as ex:
public.WriteLog('TYPE_FTP','FTP_STATUS_ERR', (msg,username,str(ex)))
return public.returnMsg(False,'FTP_STATUS_ERR',(msg,))
'''
* 设置FTP端口
* @param Int _GET['port'] 端口号
* @return bool
'''
def setPort(self,get):
try:
port = get['port'].strip()
if not port: return public.returnMsg(False,'FTP端口不能为空')
if int(port) < 1 or int(port) > 65535: return public.returnMsg(False,'PORT_CHECK_RANGE')
file = '/www/server/pure-ftpd/etc/pure-ftpd.conf'
conf = public.readFile(file)
rep = u"\n#?\s*Bind\s+[0-9]+\.[0-9]+\.[0-9]+\.+[0-9]+,([0-9]+)"
#preg_match(rep,conf,tmp)
conf = re.sub(rep,"\nBind 0.0.0.0," + port,conf)
public.writeFile(file,conf)
public.ExecShell('/etc/init.d/pure-ftpd restart')
public.WriteLog('TYPE_FTP', "FTP_PORT",(port,))
#添加防火墙
#data = ftpinfo(port=port,ps = 'FTP端口')
get.port=port
get.ps = public.getMsg('FTP_PORT_PS');
firewalls.firewalls().AddAcceptPort(get)
session['port']=port
return public.returnMsg(True, 'EDIT_SUCCESS')
except Exception as ex:
public.WriteLog('TYPE_FTP', 'FTP_PORT_ERR',(str(ex),))
return public.returnMsg(False,'EDIT_ERROR')
#重载配置
def FtpReload(self):
public.ExecShell(self.__runPath + '/pure-pw mkdb /www/server/pure-ftpd/etc/pureftpd.pdb')