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.

186 lines
5.8KB

  1. import glob
  2. import json
  3. import re
  4. import sshpubkeys
  5. from flask import Flask, redirect, url_for, render_template, request
  6. # lyadmin
  7. # scripts and web form for a tilde / PAUS instance
  8. #
  9. # gashapwn
  10. # Nov 2020
  11. #
  12. # https://git.lain.church/gashapwn/lyadmin
  13. # gashapwn@protonmail.com
  14. # or
  15. # gasahwpn on irc.lainchan.org
  16. app=Flask(__name__)
  17. # Paths for conf file,
  18. # user list,
  19. # directory containing
  20. # account request files...
  21. WORKING_DIR = "/home/gashapwn/lyadmin/";
  22. ACCOUNT_DIR = "req/";
  23. FULL_PATH = str(WORKING_DIR) + str(ACCOUNT_DIR)
  24. CONF_PATH = str(WORKING_DIR) + "lyadmin.conf.json"
  25. MAX_PUB_KEY_LEN = 5000
  26. # Account requests are given ID numbers
  27. # the first request will have the below
  28. # id number
  29. INIT_REQ_ID = "00000"
  30. # Slurp the conf file
  31. with open(CONF_PATH) as c: conf_json_str = c.read()
  32. conf_obj = json.loads(conf_json_str)
  33. # The main home page
  34. @app.route("/")
  35. def home():
  36. app.route('/')
  37. # Load the list of tilde users
  38. # to generate links for
  39. u_list = [];
  40. with open("user_list.txt") as u_file:
  41. for line in u_file.readlines():
  42. u_list.append(line.strip());
  43. return render_template("index.html", u_list=u_list, page_name="home")
  44. # The page with rules
  45. def rules():
  46. return render_template("rules.html")
  47. # Generate HTML for a form widget
  48. def widg_fun(widg):
  49. if(widg.w_type == "input"):
  50. # Return HTML for a single line input
  51. return "input id=id_%s name=%s type=text></input"%(widg.w_name, widg.w_name)
  52. elif(widg.w_type == "textarea"):
  53. # Return HTML for a big text input box
  54. return "textarea cols=40 id=id_%s name=%s rows=10 required=\"\""%(widg.w_name, widg.w_name)
  55. elif(widg.w_type == "check"):
  56. # Return HTML for a check box
  57. return "input id=id_%s name=%s type=checkbox required=\"\""%(widg.w_name, widg.w_name)
  58. return widg.w_type;
  59. # Generate HTML for request form
  60. # probably a strange way to do this...
  61. def req():
  62. app.route('/req')
  63. class Widg:
  64. def __init__(self, w_name, w_type, w_opt):
  65. self.w_name = w_name
  66. self.w_type = w_type
  67. self.w_opt = w_opt # only for choice type widg
  68. # Configuration for our request form
  69. rt = {
  70. "username": Widg("username", "input", None),
  71. "email for account lockout / registration confirmation (optional)": Widg("email", "input", None),
  72. "SSH public key": Widg("pub_key", "textarea", None),
  73. "shell of choice": Widg("shell", "choice", map(lambda k : (k, conf_obj["shell"][k]), list(conf_obj["shell"].keys()))),
  74. "have you read the rules?": Widg("rule_read", "check", None)
  75. };
  76. return render_template("req.html", req_tab = rt, widg_fun = widg_fun, page_name="req")
  77. def handle_invalid_data(req):
  78. # print(str(e))
  79. return render_template("signup.html", is_email_user = False)
  80. # Process input from the /req page
  81. def signup():
  82. app.route('/req/signup')
  83. # Get all the params from the POST
  84. # request
  85. username = request.form["username"].strip()
  86. email = request.form["email"].strip()
  87. pub_key = request.form["pub_key"].strip()
  88. shell = request.form["shell"].strip()
  89. rule_read = request.form["rule_read"].strip()
  90. is_email_user = False;
  91. # If a user didnt read the rules
  92. # send them back
  93. # Browser validations should
  94. # prevent this....
  95. if(rule_read != "on"):
  96. return redirect(url_for('req'))
  97. # Set placeholder if user didnt send an email
  98. if(len(email) > 1):
  99. is_email_user = True
  100. else:
  101. email = "NO_EMAIL"
  102. # Validate email
  103. if( not re.search("^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,10}$", email)):
  104. print("failed email validation")
  105. return handle_invalid_data(req)
  106. # Validate the SSH pub key
  107. # Most software only handles up to 4096 bit keys
  108. if(len(pub_key) > MAX_PUB_KEY_LEN):
  109. print("key failed len check")
  110. return handle_invalid_data(req)
  111. # Only printable ascii characters in
  112. # a valid key
  113. if(not re.search("^[ -~]+$", pub_key)):
  114. print("key failed regex")
  115. return handle_invalid_data(req)
  116. # Check the key against a library
  117. key = sshpubkeys.SSHKey(pub_key, strict_mode=False, skip_option_parsing=True)
  118. try:
  119. key.parse()
  120. except Exception as e:
  121. print("key failed lib validation")
  122. return handle_invalid_data(request)
  123. # All users requests have a sequential ID
  124. # this checks how many requests we have
  125. # and gives us a free ID so we can save
  126. # our request
  127. # This sets the ID of the request we're
  128. # abou to save to dsik
  129. if(len(glob.glob(ACCOUNT_DIR + str("[0-9]*ident*"))) == 0):
  130. new_id = int(INIT_REQ_ID)
  131. new_id_str = INIT_REQ_ID
  132. else:
  133. max_id = max(list(map( lambda path : path.split("/")[-1].split(".")[0] , glob.glob(str(ACCOUNT_DIR) + "[0-9]*ident*"))))
  134. # max_id = max(list(map( lambda path : path.split("/")[-1].split(".")[0] , glob.glob("./test/[0-9]*ident*"))))
  135. zpad = len(max_id)
  136. new_id = int(max_id)+1
  137. new_id_str = str(new_id).zfill(zpad)
  138. # write the request to disk
  139. fn1 = str(FULL_PATH) + str(new_id_str) + ".ident"
  140. with open(fn1, "w") as ident_file:
  141. ident_file.write(str(username) + "\n")
  142. ident_file.write(str(email) + "\n")
  143. ident_file.write(str(shell) + "\n")
  144. ident_file.write(str(pub_key) + "\n")
  145. return render_template("signup.html", is_email_user = is_email_user)
  146. @app.context_processor
  147. def get_site_name():
  148. return {"site_name": conf_obj["site_name"]}
  149. if __name__=="__main__":
  150. app.add_url_rule('/rules', 'rules', rules)
  151. app.add_url_rule('/req', 'req', req, methods = ['POST', 'GET'])
  152. app.add_url_rule('/req/signup', 'signup', signup, methods = ['POST'])
  153. app.run(host=conf_obj["listen_ip"],debug=True)