2018-01-15 06:16:04 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import cgi
|
|
|
|
import html
|
|
|
|
import os
|
2018-04-23 04:49:23 +00:00
|
|
|
import sys
|
2018-01-15 06:16:04 +00:00
|
|
|
import funct
|
|
|
|
import http.cookies
|
2018-04-16 07:01:44 +00:00
|
|
|
import sql
|
2018-04-23 04:49:23 +00:00
|
|
|
import create_db
|
2018-05-02 11:11:22 +00:00
|
|
|
import datetime
|
|
|
|
import uuid
|
|
|
|
from configparser import ConfigParser, ExtendedInterpolation
|
2018-05-05 12:40:41 +00:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
env = Environment(loader=FileSystemLoader('templates/'))
|
|
|
|
template = env.get_template('login.html')
|
|
|
|
form = cgi.FieldStorage()
|
2018-01-15 06:16:04 +00:00
|
|
|
|
|
|
|
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
2018-05-05 12:40:41 +00:00
|
|
|
user_id = cookie.get('uuid')
|
2018-01-15 06:16:04 +00:00
|
|
|
ref = form.getvalue('ref')
|
|
|
|
login = form.getvalue('login')
|
|
|
|
password = form.getvalue('pass')
|
2018-05-05 12:40:41 +00:00
|
|
|
db_create = ""
|
|
|
|
error_log = ""
|
|
|
|
error = ""
|
2018-01-15 06:16:04 +00:00
|
|
|
|
2018-05-02 11:11:22 +00:00
|
|
|
path_config = "haproxy-webintarface.config"
|
|
|
|
config = ConfigParser(interpolation=ExtendedInterpolation())
|
|
|
|
config.read(path_config)
|
|
|
|
|
2018-05-05 12:40:41 +00:00
|
|
|
if ref is None:
|
|
|
|
ref = "/index.html"
|
2018-05-02 11:11:22 +00:00
|
|
|
|
2018-05-05 12:40:41 +00:00
|
|
|
if form.getvalue('error'):
|
|
|
|
error_log = '<div class="alert alert-danger">Somthing wrong :( I\'m sad about this, but try again!</div><br /><br />'
|
|
|
|
|
|
|
|
try:
|
|
|
|
if config.get('main', 'session_ttl'):
|
|
|
|
session_ttl = config.getint('main', 'session_ttl')
|
|
|
|
except:
|
|
|
|
error = '<center><div class="alert alert-danger">Can not find "session_ttl" parametr. Check into config, "main" section</div>'
|
|
|
|
pass
|
2018-01-31 09:22:14 +00:00
|
|
|
|
2018-05-05 12:40:41 +00:00
|
|
|
try:
|
|
|
|
role = sql.get_user_role_by_uuid(user_id.value)
|
|
|
|
user = sql.get_user_name_by_uuid(user_id.value)
|
|
|
|
except:
|
|
|
|
role = ""
|
|
|
|
user = ""
|
|
|
|
pass
|
2018-05-02 11:11:22 +00:00
|
|
|
|
2018-05-05 12:40:41 +00:00
|
|
|
if create_db.check_db():
|
|
|
|
if create_db.create_table():
|
|
|
|
create_db.update_all()
|
|
|
|
db_create = '<div class="alert alert-success">DB was created<br /><br />Now you can login, default: admin/admin</div>'
|
|
|
|
create_db.update_all_silent()
|
|
|
|
|
|
|
|
if form.getvalue('logout'):
|
2018-05-02 11:11:22 +00:00
|
|
|
try:
|
|
|
|
sql.delete_uuid(user_id.value)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
print("Set-cookie: uuid=; expires=Wed May 18 03:33:20 2003; path=/app/; httponly")
|
2018-01-31 09:22:14 +00:00
|
|
|
print("Content-type: text/html\n")
|
2018-05-02 11:11:22 +00:00
|
|
|
print('<meta http-equiv="refresh" content="0; url=/app/login.py">')
|
2018-05-05 12:40:41 +00:00
|
|
|
|
2018-01-15 06:16:04 +00:00
|
|
|
if login is not None and password is not None:
|
2018-05-05 12:40:41 +00:00
|
|
|
|
2018-04-16 07:01:44 +00:00
|
|
|
USERS = sql.select_users()
|
2018-05-02 11:11:22 +00:00
|
|
|
session_ttl = config.getint('main', 'session_ttl')
|
|
|
|
expires = datetime.datetime.utcnow() + datetime.timedelta(days=session_ttl)
|
|
|
|
user_uuid = str(uuid.uuid4())
|
|
|
|
|
2018-04-16 07:01:44 +00:00
|
|
|
for users in USERS:
|
|
|
|
if login in users[1] and password == users[3]:
|
2018-01-31 09:22:14 +00:00
|
|
|
c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
2018-05-02 11:11:22 +00:00
|
|
|
c["uuid"] = user_uuid
|
|
|
|
c["uuid"]["path"] = "/app/"
|
|
|
|
c["uuid"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
|
2018-01-31 09:22:14 +00:00
|
|
|
print(c)
|
2018-05-02 11:11:22 +00:00
|
|
|
sql.write_user_uuid(login, user_uuid)
|
|
|
|
|
|
|
|
print("Content-type: text/html\n")
|
2018-01-15 06:16:04 +00:00
|
|
|
print('<html><head><title>Redirecting</title><meta charset="UTF-8">')
|
|
|
|
print('<link href="/style.css" rel="stylesheet">')
|
|
|
|
print('<meta http-equiv="refresh" content="0; url=%s">' % ref)
|
2018-05-05 12:40:41 +00:00
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
print("Content-type: text/html\n")
|
|
|
|
print('<meta http-equiv="refresh" content="0; url=/app/login.py?error=1">')
|
|
|
|
|
|
|
|
if login is None:
|
|
|
|
print("Content-type: text/html\n")
|
|
|
|
output_from_parsed_template = template.render(h2 = 1, title = "Login page. Enter please",
|
|
|
|
role = role,
|
|
|
|
user = user,
|
|
|
|
error_log = error_log,
|
|
|
|
error = error,
|
|
|
|
ref = ref,
|
|
|
|
db_create = db_create)
|
|
|
|
print(output_from_parsed_template)
|