scripts and tools to administer the lingy.in public unix / tilde
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 line
3.7KB

  1. import glob
  2. import json
  3. from flask import Flask, redirect, url_for, render_template, request
  4. app=Flask(__name__)
  5. WORKING_DIR = "/home/gashapwn/lyadmin/";
  6. ACCOUNT_DIR = "test/";
  7. FULL_PATH = str(WORKING_DIR) + str(ACCOUNT_DIR)
  8. CONF_PATH = str(WORKING_DIR) + "lyadmin.conf.json"
  9. # Account requests are given ID numbers
  10. # the first request will have the below
  11. # id number
  12. INIT_REQ_ID = "00000"
  13. with open(CONF_PATH) as c: conf_json_str = c.read()
  14. conf_obj = json.loads(conf_json_str)
  15. @app.route("/")
  16. def home():
  17. app.route('/')
  18. u_list = [];
  19. with open("user_list.txt") as u_file:
  20. for line in u_file.readlines():
  21. u_list.append(line.strip());
  22. return render_template("index.html", u_list=u_list, page_name="home")
  23. def rules():
  24. return render_template("rules.html")
  25. # this is a weird way to do this
  26. # right?
  27. def widg_fun(widg):
  28. if(widg.w_type == "input"):
  29. return "input id=id_%s name=%s type=text></input"%(widg.w_name, widg.w_name)
  30. elif(widg.w_type == "textarea"):
  31. return "textarea cols=40 id=id_%s name=%s rows=10 required=\"\""%(widg.w_name, widg.w_name)
  32. elif(widg.w_type == "check"):
  33. return "input id=id_%s name=%s type=checkbox required=\"\""%(widg.w_name, widg.w_name)
  34. return widg.w_type;
  35. def req():
  36. app.route('/req')
  37. class Widg:
  38. def __init__(self, w_name, w_type, w_opt):
  39. self.w_name = w_name
  40. self.w_type = w_type
  41. self.w_opt = w_opt
  42. rt = {
  43. "username": Widg("username", "input", None),
  44. "email for account lockout / registration confirmation (optional)": Widg("email", "input", None),
  45. "SSH public key": Widg("pub_key", "textarea", None),
  46. "shell of choice": Widg("shell", "choice", map(lambda k : (k, conf_obj["shell"][k]), list(conf_obj["shell"].keys()))),
  47. "have you read the rules?": Widg("rule_read", "check", None)
  48. };
  49. # uhhh is this how you're supposed to do this?
  50. return render_template("req.html", req_tab = rt, widg_fun = widg_fun, page_name="req")
  51. def signup():
  52. app.route('/req/signup')
  53. username = request.form["username"]
  54. email = request.form["email"]
  55. shell = request.form["shell"]
  56. rule_read = request.form["rule_read"]
  57. is_email_user = False;
  58. if(rule_read != "on"):
  59. return redirect(url_for('req'))
  60. if(len(email) > 1):
  61. is_email_user = True
  62. else:
  63. email = "NO_EMAIL"
  64. if(len(glob.glob("./test/[0-9]*ident*")) == 0):
  65. new_id = int(INIT_REQ_ID)
  66. new_id_str = INIT_REQ_ID
  67. else:
  68. max_id = max(list(map( lambda path : path.split("/")[-1].split(".")[0] , glob.glob("./test/[0-9]*ident*"))))
  69. zpad = len(max_id)
  70. new_id = int(max_id)+1
  71. new_id_str = str(new_id).zfill(zpad)
  72. fn1 = str(FULL_PATH) + str(new_id_str) + ".ident"
  73. with open(fn1, "w") as ident_file:
  74. ident_file.write(str(username) + "\n")
  75. ident_file.write(str(email) + "\n")
  76. ident_file.write(str(shell) + "\n")
  77. ident_file.write(str(rule_read) + "\n")
  78. print(username + " " + email + " " + shell + " " + rule_read)
  79. return render_template("signup.html", is_email_user = is_email_user)
  80. def login():
  81. if request.method == "POST":
  82. user = request.form["nm"]
  83. return redirect(url_for('success', name = user))
  84. else:
  85. return redirect(url_for('home'))
  86. if __name__=="__main__":
  87. app.add_url_rule('/rules', 'rules', rules)
  88. app.add_url_rule('/login', 'login', login, methods = ['POST', 'GET'])
  89. app.add_url_rule('/req', 'req', req, methods = ['POST', 'GET'])
  90. app.add_url_rule('/req/signup', 'signup', signup, methods = ['POST'])
  91. # app.run(host="104.248.118.130",debug=True)
  92. app.run(host=conf_obj["listen_ip"],debug=True)