mirror of
https://github.com/FAUSheppy/jeffrey_miller_flask_ftp
synced 2025-12-06 21:21:37 +01:00
84 lines
2.3 KiB
Python
Executable File
84 lines
2.3 KiB
Python
Executable File
#!/usr/bin/python3
|
|
import flask
|
|
import sys
|
|
import argparse
|
|
import os
|
|
import subprocess
|
|
import crypt
|
|
|
|
from sqlalchemy import Column, Integer, String, Boolean, or_, and_
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.exc import IntegrityError
|
|
from sqlalchemy.sql import func
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
app = flask.Flask("Flask-VSFTP-User-Tool")
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
db = SQLAlchemy(app)
|
|
|
|
HTTP_FORBIDDEN = 401
|
|
HTTP_NOT_FOUND = 404
|
|
HTTP_UNPROCESSABLE = 422
|
|
HTTP_INTERNAL_ERR = 500
|
|
|
|
# unix useradd requires exactly this salt, do not change
|
|
PAM_PASSWD_SALT = "22"
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return flask.render_template("index.html")
|
|
|
|
@app.route('/create-user', methods=["POST"])
|
|
def createUser():
|
|
createUser(flask.request.form)
|
|
return ("Success", 200)
|
|
|
|
@app.route('/delete-user', methods=["POST"])
|
|
def deleteUser():
|
|
deleteUser(user=flask.request.get('user'))
|
|
return ("Success", 200)
|
|
|
|
@app.route('/list-users')
|
|
def listUsers():
|
|
users = db.session.query(FTPUser)
|
|
return flask.render_template("list_users.html", users=users)
|
|
|
|
def createUser(webform):
|
|
|
|
# command line useradd requires a pre-encrypted password
|
|
cryptPass = crypt.crypt(webform['password'], PAM_PASSWD_SALT)
|
|
subprocess.run(["./scripts/create_user.sh", cryptPass, webform['username']])
|
|
|
|
# track added users to prevent deletion of other users and listing #
|
|
db.session.add(FTPUser(webform.username))
|
|
db.session.commit()
|
|
|
|
|
|
def executeScript(scriptName):
|
|
path = os.path.expanduser(scriptName)
|
|
subprocess.Popen(path)
|
|
|
|
class FTPUser(db.Model):
|
|
|
|
__tablename__ = 'users'
|
|
username = Column(String, primary_key=True)
|
|
|
|
@app.before_first_request
|
|
def init():
|
|
|
|
app.config["DB"] = db
|
|
db.create_all()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description="Flask-VSFTP-User-Tool", \
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
|
|
parser.add_argument("-i", "--interface", default="0.0.0.0", help="Interface to listen on")
|
|
parser.add_argument("-p", "--port", default="5000", help="Port to listen on")
|
|
args = parser.parse_args()
|
|
app.run(host=args.interface, port=args.port)
|