forked from VicentGJ/AD-webmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADwebmanager.py
executable file
·140 lines (109 loc) · 4.11 KB
/
ADwebmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python2
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2015 Stéphane Graber
# Author: Stéphane Graber <stgraber@ubuntu.com>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You can find the license on Debian systems in the file
# /usr/share/common-licenses/GPL-2
import argparse
import os
app_prefix = "/opt/samba4-manager-master/"
# Check if running from bzr
for path in ('libs', 'plugins', 'static', 'templates'):
if not os.path.exists(path):
break
else:
app_prefix = "."
parser = argparse.ArgumentParser(description="Samba4 Gestor Web")
args = parser.parse_args()
if not os.path.exists(app_prefix):
raise Exception("Missing app dir: %s" % app_prefix)
# Import the rest of the stuff we need
from flask import Flask, g
import glob
import importlib
# Look at the right place
import sys
sys.path.append(app_prefix)
# Import our modules
from libs.common import ReverseProxied
from libs.common import iri_for as url_for
from settings import Settings
# Prepare the web server
app = Flask(__name__,
static_folder="%s/static" % app_prefix,
template_folder="%s/templates" % app_prefix)
app.config.from_object(Settings)
app.jinja_env.globals['url_for'] = url_for
if 'URL_PREFIX' in app.config:
app.wsgi_app = ReverseProxied(app.wsgi_app, app.config['URL_PREFIX'])
# Check for mandatory configuration
for key in ("LDAP_DOMAIN", "SECRET_KEY", "SEARCH_DN"):
if key not in app.config:
raise KeyError("Missing mandatory %s option in configuration." % key)
# LDAP configuration
if "LDAP_DN" not in app.config:
app.config['LDAP_DN'] = "DC=%s" % ",DC=".join(
app.config['LDAP_DOMAIN'].split("."))
if "LDAP_SERVER" not in app.config:
import dns.resolver
import dns.rdatatype
import operator
record = "_ldap._tcp.%s." % app.config['LDAP_DOMAIN']
answers = []
# Query the DNS
try:
for answer in dns.resolver.query(record, dns.rdatatype.SRV):
address = (answer.target.to_text()[:-1], answer.port)
answers.append((address, answer.priority, answer.weight))
except:
# Ignore exceptions, an empty list will trigger an exception anyway
pass
# Order by priority and weight
servers = [entry[0][0] for entry in sorted(answers,
key=operator.itemgetter(1, 2))]
if not servers:
raise Exception("No LDAP server in domain '%s'." %
app.config['LDAP_DOMAIN'])
if len(servers) == 1:
app.config['LDAP_SERVER'] = servers[0]
else:
app.config['LDAP_SERVER'] = servers
if "SICCIP_AWARE" not in app.config:
app.config['SICCIP_AWARE'] = False
# Load the plugins
for plugin_file in glob.glob("%s/plugins/*.py" % app_prefix):
plugin_name = plugin_file.split('/')[-1].replace('.py', '')
if plugin_name == "__init__":
continue
plugin = importlib.import_module("plugins.%s" % plugin_name)
plugin.init(app)
@app.before_request
def pre_request():
"""
Setup any of the global variables before the request is processed.
"""
g.menu = []
g.menu.append((url_for("core_index"), "Mi Account"))
g.menu.append((url_for("tree_base"), u"Directory"))
g.menu.append((url_for("core_logout"), "Log out"))
# LDAP connection settings
g.ldap = {'domain': app.config['LDAP_DOMAIN'], 'dn': app.config['LDAP_DN'], 'server': app.config['LDAP_SERVER'],
'search_dn': app.config['SEARCH_DN']}
# The various caches
g.ldap_cache = {}
# SICC-IP integrations
g.siccip = app.config['SICCIP_AWARE']
# Extra fields form
g.extra_fields = app.config['EXTRA_FIELDS']
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)