-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleldapregistrator.py
More file actions
104 lines (100 loc) · 3.8 KB
/
Simpleldapregistrator.py
File metadata and controls
104 lines (100 loc) · 3.8 KB
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
# -*- coding: utf-8 -*-
import tornado.web
from tornado.ioloop import IOLoop
from tornado import gen
import time
import os
import ldap3
from ldap3 import Server, Connection, ALL, MODIFY_ADD, MODIFY_REPLACE
from ldap3 import Server, Connection, ALL, NTLM
from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups as addUsersInGroups
import random
import json
from random import randint
from string import ascii_letters
import logging
import sys
sys.path.append("lib/")
import logger
import openldap
import activedirectory
@gen.coroutine
def async_sleep(seconds):
yield gen.Task(IOLoop.instance().add_timeout, time.time() + seconds)
class DSHandler(tornado.web.RequestHandler):
@gen.coroutine
def post(self):
login = self.get_argument("login")
password = self.get_argument("password")
f = open("conf/config.json", "r")
conf = f.read()
try:
conf = json.loads(conf)
except ValueError:
logger.error("Decoding config.json has failed! Invalid config!")
conf = False
ldapadd = False
if conf != False:
try:
for i in conf["credentials"]:
host = i["host"]
bind_pass = i["password"]
bind_dn = i["bind_dn"]
base_dn = i["base_dn"]
ds_type = i["ds_type"]
try:
ssl = i["ssl"]
if ssl == "True":
ssl = True
else:
ssl = False
except:
ssl = False
try:
port = i["port"]
port = int(port)
except:
port = None
credentials = True
except KeyError:
logger.error("Reading credentials error! Verify that the credentials block exist in config and contain host, password, bind_dn, base_dn, ds_type!")
credentials = False
ldapadd = False
if credentials != False:
# define the server
try:
if ssl == True:
s = Server(host, port=port, use_ssl = True, get_info=ALL)
elif ssl == False:
s = Server(host, port=port, get_info=ALL) # define an unsecure LDAP server, requesting info on DSE and schema
# define the connection
c = Connection(s, user=bind_dn, password=bind_pass)
c.bind()
connect = True
except:
logger.error("Unable connect to server!")
connect = False
ldapadd = False
if connect != False:
if ds_type == "openldap":
ldapadd = openldap.create(conf,login,password,base_dn,c)
c.unbind()
elif ds_type == "activedirectory":
ldapadd = activedirectory.create(conf,login,password,base_dn,c)
c.unbind()
if ldapadd == True:
self.write("Success")
self.finish()
else:
self.write("Failed")
self.finish()
root = os.path.dirname(__file__)
application = tornado.web.Application([
(r"/registrator", DSHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": root, "default_filename": "index.html"}),
])
logging.getLogger('tornado.access').disabled = True
logger = logger.get_logger()
logger.info('Started')
application.listen(9999)
IOLoop.instance().start()