def get(self, request): """ Query the FTP server configuration """ ftpconfig = {"readonly_config": {}, "config": {}, "result": {}} config_file = '/etc/vsftpd/vsftpd.conf' try: config = ConfigObj(config_file) except: ftpconfig['result']['type'] = 1 ftpconfig['result']['message'] = "Can't find vsftpd.conf" return Response(ftpconfig, status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = config.keys() for i in range(0, 10): if FTP_Config_Items[i] in key: ftpconfig['readonly_config'][FTP_Config_Items[i]] = config[FTP_Config_Items[i]] else: config[FTP_Config_Items[i]] = FTP_Config_INIT[FTP_Config_Items[i]] config.write() ftpconfig['readonly_config'][FTP_Config_Items[i]] = FTP_Config_INIT[FTP_Config_Items[i]] for i in range(10, len(FTP_Config_Items)): if FTP_Config_Items[i] in key: ftpconfig['config'][FTP_Config_Items[i]] = config[FTP_Config_Items[i]] else: ftpconfig['config'][FTP_Config_Items[i]] = FTP_Config_INIT[FTP_Config_Items[i]] ftpconfig['result']['type'] = 0 return Response(ftpconfig, status=status.HTTP_200_OK)
def put(self, request): """ Modify the FTP server configuration """ result = {"type": 0, "message": ""} # ftpconfig = request.data.get('config') ftpconfig = request.data if not ftpconfig: result['type'] = 1 result['message'] = "Invalid parameter." return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) cmd = "rm -rf /etc/vsftpd/vsftpd.conf.bak" commands.getstatusoutput(cmd) cmd = "cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak" (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: result['type'] = 2 result['message'] = "Backup old configuration failed." return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) config_file = '/etc/vsftpd/vsftpd.conf' try: config = ConfigObj(config_file) except: result['type'] = 3 result['message'] = "Can't find vsftpd.conf" return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) boolitems = [] for i in range(10, 18): boolitems.append(FTP_Config_Items[i]) for i in ftpconfig: if i in boolitems: if (ftpconfig[i] == "YES") or (ftpconfig[i] == "NO"): config[i] = ftpconfig[i] config.write() cmd = "service vsftpd restart" (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: result['type'] = 3 result['message'] = result_MSG cmd = "cp /etc/vsftpd/vsftpd.conf.bak /etc/vsftpd/vsftpd.conf ; rm -rf /etc/vsftpd/sftpd.conf.bak ; service vsftpd restart" commands.getstatusoutput(cmd) return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) cmd = "rm -rf /etc/vsftpd/vsftpd.conf.bak" commands.getstatusoutput(cmd) result['type'] = 0 return Response(result, status=status.HTTP_200_OK)
def get(self, request, username): """ Query the user's configuration information """ # username = request.data.get('name') # userinfo = request.data userconfig = {"config": {}, "result": {}} result = {"type": 0, "message": ""} check = CheckFile() if (not username): result['type'] = 1 result['message'] = "Invalid user name parameter." result['name'] = username return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # username = userinfo['name'] code, result_MSG = check.check_file() if code != 0: result['type'] = 2 result['message'] = result_MSG return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) cmd = "cp /etc/vsftpd/vsftpd_login/" + username + " /etc/vsftpd/vsftpd_login/" + username + ".conf" commands.getstatusoutput(cmd) config_file = '/etc/vsftpd/vsftpd_login/' + username + ".conf" try: config = ConfigObj(config_file) except: result['type'] = 3 result['message'] = "Can't find the corresponding configuration file." return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = config.keys() if 'local_root' in key: userconfig['homedir'] = config['local_root'] else: userconfig['homedir'] = "Unknown" for i in range(1, len(User_Config_Items)-1): if User_Config_Items[i] in key: userconfig['config'][User_Config_Items[i]] = config[User_Config_Items[i]] else: userconfig['config'][User_Config_Items[i]] = User_Config_INIT[User_Config_Items[i]] userconfig['result']['type'] = 0 userconfig['result']['message'] = "" cmd = "rm -rf /etc/vsftpd/vsftpd_login/" + username + ".conf" commands.getstatusoutput(cmd) return Response(userconfig, status=status.HTTP_200_OK)
def get(self, request): userlist = [] i = 0 if not os.path.exists("/etc/vsftpd/vsftpd_user"): (code, resultTouch) = commands.getstatusoutput('touch /etc/vsftpd/vsftpd_user') return Response(userlist, status=status.HTTP_200_OK) fileobj = open('/etc/vsftpd/vsftpd_user') for data in fileobj: tmpdict = {} i = i + 1 if i % 2: name = data.strip().split('\n') tmpdict['name'] = name[0] cmd = "cp /etc/vsftpd/vsftpd_login/" + tmpdict['name'] + " /etc/vsftpd/vsftpd_login/" + tmpdict['name'] + ".conf" (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: tmpdict['result'] = 1 tmpdict['message'] = result_MSG userlist.append(tmpdict) continue config_file = '/etc/vsftpd/vsftpd_login/' + tmpdict['name'] + ".conf" try: config = ConfigObj(config_file) except: tmpdict['result'] = 2 tmpdict['message'] = "Can't find the configuration file" userlist.append(tmpdict) continue key = config.keys() if 'local_root' in key: tmpdict['homedir'] = config['local_root'] else: tmpdict['homedir'] = "Unknown" tmpdict['result'] = 0 userlist.append(tmpdict) cmd = "rm -rf /etc/vsftpd/vsftpd_login/"+tmpdict['name']+".conf" commands.getstatusoutput(cmd) fileobj.close() return Response(userlist, status=status.HTTP_200_OK)
def reset_default(self): if not os.path.exists("/etc/vsftpd/vsftpd_login"): cmd = "mkdir /etc/vsftpd/vsftpd_login" (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: result['type'] = 1 result['message'] = result_MSG return 1, result_MSG else: cmd = "rm -rf /etc/vsftpd/vsftpd_login/*" commands.getstatusoutput(cmd) cmd = "rm -rf /etc/vsftpd/vsftpd_user" commands.getstatusoutput(cmd) cmd = "rm -rf /etc/vsftpd/vsftpd_login.db" commands.getstatusoutput(cmd) cmd = "touch /etc/vsftpd/vsftpd_user" (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: return 2, result_MSG cmd = "db_load -T -t hash -f /etc/vsftpd/vsftpd_user /etc/vsftpd/vsftpd_login.db" (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: return 3, result_MSG config = ConfigObj() config.filename = "/etc/vsftpd/vsftpd.conf" for i in FTP_Config_Items: config[i] = FTP_Config_Items[i] config.write() cmd = "chmod 600 /etc/vsftpd/vsftpd.conf" commands.getstatusoutput(cmd) cmd = "service vsftpd restart" (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: return 4, result_MSG cmd = "cat /etc/pam.d/vsftpd |grep vsftpd_login" (code, result_MSG) = commands.getstatusoutput(cmd) if code == 0: data = result_MSG.split('\n') if len(data) == 2: if (data[0][0] != '#') and (data[1][0] != '#'): return 0, " " cmd = " " cmd_1 = " " x86_64_file = "/lib64/security/pam_userdb.so" x86_file = "/lib/security/pam_userdb.so" file_name = "" if os.path.exists(x86_64_file): file_name = x86_64_file else: if os.path.exists(x86_file): file_name = x86_file if file_name != " ": cmd = "echo \"#%PAM-1.0\" >/etc/pam.d/vsftpd ; echo \"auth sufficient " + file_name + " db=/etc/vsftpd/vsftpd_login\" >>/etc/pam.d/vsftpd" cmd_1 = "echo \"account sufficient " + file_name + " db=/etc/vsftpd/vsftpd_login\" >>/etc/pam.d/vsftpd" if (cmd == " ") or (cmd_1 == " "): return 5, "pam_userdb.so not exist." else: (code, result_MSG) = commands.getstatusoutput(cmd) (code_1, result_MSG_1) = commands.getstatusoutput(cmd_1) if (code != 0) or (code_1 != 0): return 6, "/etc/pam.d/vsftpd file error." return 0, " "
def put(self, request): """ Modify the user configuration information """ # userconfig = {"name":"","config":{},"new_passwd":""} result = {"type": 0, "message": ""} userconfig = request.data username = request.data.get('name') check = CheckFile() if (not username) and (not userconfig): result['type'] = 1 result['message'] = "Invalid parameter." return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) code, result_MSG = check.check_file() if code != 0: result['type'] = 2 result['message'] = result_MSG return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) cmd = "cp /etc/vsftpd/vsftpd_login/" + username + " /etc/vsftpd/vsftpd_login/" + username + ".conf" commands.getstatusoutput(cmd) config_file = '/etc/vsftpd/vsftpd_login/' + username + ".conf" try: config = ConfigObj(config_file) except: result['type'] = 3 result['message'] = "Can't find the corresponding configuration file." return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = config.keys() if 'new_passwd' in userconfig: cmd = "rm -rf /etc/vsftpd/vsftpd_user.bak;rm -rf /etc/vsftpd/vsftpd_login.db.bak" commands.getstatusoutput(cmd) cmd = "cp /etc/vsftpd/vsftpd_user /etc/vsftpd/vsftpd_user.bak; cp /etc/vsftpd/vsftpd_login.db /etc/vsftpd/vsftpd_login.db.bak" commands.getstatusoutput(cmd) cmd = "sed -i \"/^" + username + "$/{n;d}\" /etc/vsftpd/vsftpd_user" cmd_1 = "sed -i \"/^" + username + "$/a\\" + userconfig['new_passwd'] + "\" /etc/vsftpd/vsftpd_user" (code, result_MSG) = commands.getstatusoutput(cmd) (code_1, result_MSG_1) = commands.getstatusoutput(cmd_1) if (code != 0) or (code_1 != 0): result['type'] = 7 result['message'] = "Update password failure." cmd = "mv -f /etc/vsftpd/vdftpd_user.bak /etc/vsftpd/vsftpd_user; mv -f /etc/vsftpd/vsftpd_login.db.bak /etc/vsftpd/vsftpd_login.db" commands.getstatusoutput(cmd) return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) cmd = "db_load -T -t hash -f /etc/vsftpd/vsftpd_user /etc/vsftpd/vsftpd_login.db" (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: result['type'] = 8 result['message'] = result_MSG cmd = "mv -f /etc/vsftpd/vdftpd_user.bak /etc/vsftpd/vsftpd_user; mv -f /etc/vsftpd/vsftpd_login.db.bak /etc/vsftpd/vsftpd_login.db" commands.getstatusoutput(cmd) return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) cmd = "rm -rf /etc/vsftpd/vsftpd_user.bak;rm -rf /etc/vsftpd/vsftpd_login.db.bak" commands.getstatusoutput(cmd) boolitems = [] valueitems = [] for i in range(1, 7): boolitems.append(User_Config_Items[i]) for i in range(7, 11): valueitems.append(User_Config_Items[i]) for i in userconfig: if i in boolitems: if (userconfig[i] == "YES") or (userconfig[i] == "NO"): config[i] = userconfig[i] else: if i in valueitems: config[i] = userconfig[i] config.write() result['type'] = 0 result['message'] = "" cmd = "mv -f /etc/vsftpd/vsftpd_login/" + username + ".conf /etc/vsftpd/vsftpd_login/" + username commands.getstatusoutput(cmd) return Response(result, status=status.HTTP_200_OK)
def post(self, request): """ add a user """ # userinfo = {"name":"","config":{},"passwd":"","homedir":""} userinfo = request.data result = {"type": 0, "message": ""} check = CheckFile() if not userinfo: result['type'] = 1 result['message'] = "The user information cannot be empty." return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if ('name' not in userinfo) or ('passwd' not in userinfo) or ('homedir' not in userinfo): result['type'] = 2 result['message'] = "Add a user must specify a user name,passwd,shared directories and configuration items." return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) (code, result_MSG) = check.check_file() if code != 0: result['type'] = 3 result['message'] = result_MSG return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) cmd = "cat /etc/vsftpd/vsftpd_user |grep \"^" + userinfo['name'] + "$\"" (code, result_MSG) = commands.getstatusoutput(cmd) if code == 0: result['type'] = 4 result['message'] = "User name already exists." return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if not re.match('/var/ftp', userinfo['homedir']): result['type'] = 5 result['message'] = "Shared directory must be under /var/ftp." return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if not os.path.exists(userinfo['homedir']): cmd = "mkdir -p " + userinfo['homedir'] (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: result['type'] = 7 result['message'] = result_MSG return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) cmd = "chmod " + "700 " + userinfo['homedir'] (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: result['type'] = 9 result['message'] = result_MSG cmd = "rm -rf " + userinfo['homedir'] commands.getstatusoutput(cmd) return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) cmd = "chown -R ftp:ftp " + userinfo['homedir'] (code, result_MSG) = commands.getstatusoutput(cmd) if code != 0: result['type'] = 11 result['message'] = result_MSG cmd = "rm -rf " + userinfo['homedir'] commands.getstatusoutput(cmd) return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) config_file = "/etc/vsftpd/vsftpd_login/" + userinfo['name'] cmd = "rm -rf " + config_file commands.getstatusoutput(cmd) config = ConfigObj() config.filename = config_file config['local_root'] = userinfo['homedir'] # for i in User_Config_INIT: # config[i] = User_Config_INIT[i] # config.write() cmd = "cp -f /etc/vsftpd/vsftpd_user /etc/vsftpd/vsftpd_user.bak" cmd_1 = "echo " + userinfo['name'] + " >> /etc/vsftpd/vsftpd_user ; echo " + userinfo['passwd'] + " >> /etc/vsftpd/vsftpd_user" cmd_2 = "mv -f /etc/vsftpd/vsftpd_login.db /etc/vsftpd/vsftpd_login.db.bak" cmd_3 = "db_load -T -t hash -f /etc/vsftpd/vsftpd_user /etc/vsftpd/vsftpd_login.db" (code, result_MSG) = commands.getstatusoutput(cmd) (code_1, result_MSG_1) = commands.getstatusoutput(cmd_1) (code_2, result_MSG_2) = commands.getstatusoutput(cmd_2) (code_3, result_MSG_3) = commands.getstatusoutput(cmd_3) if (code != 0) or (code_1 != 0) or (code_2 != 0) or (code_3 != 0): result['type'] = 12 result['message'] = "Not support the user name or password." cmd = "mv -f /etc/vsftpd/vdftpd_user.bak /etc/vsftpd/vsftpd_user; mv -f /etc/vsftpd/vsftpd_login.db.bak /etc/vsftpd/vsftpd_login.db" commands.getstatusoutput(cmd) return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # if userconfig.has_key('config'): boolitems = [] valueitems = [] for i in range(1, 7): boolitems.append(User_Config_Items[i]) for i in range(7, 11): valueitems.append(User_Config_Items[i]) for i in userinfo: if i in boolitems: if (userinfo[i] == "YES") or (userinfo[i] == "NO"): config[i] = userinfo[i] else: if i in valueitems: config[i] = userinfo[i] config['guest_enable'] = "YES" config.write() result['type'] = 0 cmd = "rm -rf /etc/vsftpd/vdftpd_user.bak; rm -rf /etc/vsftpd/vsftpd_login.db.bak " commands.getstatusoutput(cmd) return Response(result, status=status.HTTP_200_OK)