from activedirectory import Client, Creds, Locator, activate

domain = 'freeadi.org'
user = '******'
password = '******'

creds = Creds(domain)
creds.acquire(user, password)
activate(creds)

locator = Locator()
pdc = locator.locate(domain, role='pdc')

client = Client(domain)
users = client.search('(objectClass=user)', server=pdc)
for dn,attrs in users:
    name = attrs['sAMAccountName'][0]
    print '-> %s' % name
Example #2
0
    def init(cls, tracking_mode=False):
        """

        :param tracking_mode:
        :type tracking_mode: bool
        :return:
        :rtype: flask.Flask
        """
        from flask_socketio import SocketIO
        from flask import Flask
        from Config import Environment
        cls._app = Flask(
            Environment.SERVER_DATA['APP_NAME'],
            static_url_path="/file",
            static_folder=Environment.SERVER_DATA['STATIC_PATH'],
            template_folder=Environment.SERVER_DATA['TEMPLATE_PATH'])
        if 'CORS' in Environment.Logins:
            from flask_cors import CORS
            cls._app.config["CORS_ALLOW_HEADERS"] = Environment.SERVER_DATA[
                'CORS']['ALLOW_HEADERS']
            cls._app.config["CORS_ALWAYS_SEND"] = Environment.SERVER_DATA[
                'CORS']['ALWAYS_SEND']
            cls._app.config[
                "CORS_AUTOMATIC_OPTIONS"] = Environment.SERVER_DATA['CORS'][
                    'AUTOMATIC_OPTIONS']
            cls._app.config["CORS_EXPOSE_HEADERS"] = Environment.SERVER_DATA[
                'CORS']['EXPOSE_HEADERS']
            cls._app.config[
                "CORS_INTERCEPT_EXCEPTIONS"] = Environment.SERVER_DATA['CORS'][
                    'INTERCEPT_EXCEPTIONS']
            cls._app.config["CORS_MAX_AGE"] = Environment.SERVER_DATA['CORS'][
                'MAX_AGE']
            cls._app.config["CORS_METHODS"] = Environment.SERVER_DATA['CORS'][
                'METHODS']
            cls._app.config["CORS_ORIGINS"] = Environment.SERVER_DATA['CORS'][
                'ORIGINS']
            cls._app.config["CORS_RESOURCES"] = r"/*"
            cls._app.config["CORS_SEND_WILDCARD"] = Environment.SERVER_DATA[
                'CORS']['SEND_WILDCARD']
            cls._app.config[
                "CORS_SUPPORTS_CREDENTIALS"] = Environment.SERVER_DATA['CORS'][
                    'SUPPORTS_CREDENTIALS']
            cls._app.config["CORS_VARY_HEADER"] = Environment.SERVER_DATA[
                'CORS']['VARY_HEADER']
            cors = CORS(cls._app,
                        origins=Environment.SERVER_DATA['CORS']['ORIGINS'])
        if 'APP_KEY' in Environment.SERVER_DATA:
            from flask_wtf.csrf import CSRFProtect
            cls._session = Session()
            #cls._app.config['TESTING'] = True
            #cls._app.config['TEMPLATES_AUTO_RELOAD'] = True
            cls._app.config['SECRET_KEY'] = Environment.SERVER_DATA['APP_KEY']
            cls._app.config['SESSION_TYPE'] = Environment.SERVER_DATA[
                'SESSION']
            if Environment.SERVER_DATA['SESSION'] == 'filesystem':
                cls._app.config['SESSION_FILE_DIR'] = Environment.Services[
                    Environment.SERVER_DATA['SESSION']]['PATH']
            if Environment.SERVER_DATA['SESSION'] == 'memcached':
                import pymemcache
                cls._app.config['SESSION_MEMCACHED'] = pymemcache.Client(
                    (Environment.Services[Environment.SERVER_DATA['SESSION']]
                     ['HOST'], Environment.Services[
                         Environment.SERVER_DATA['SESSION']]['PORT']))
            if Environment.SERVER_DATA['SESSION'] == 'redis':
                import redis
                cls._app.config['SESSION_REDIS'] = redis.from_url(
                    "%s://%s:%d/redis" %
                    (Environment.SERVER_DATA['SESSION'], Environment.Services[
                        Environment.SERVER_DATA['SESSION']]['HOST'],
                     Environment.Services[
                         Environment.SERVER_DATA['SESSION']]['PORT']))
            if Environment.SERVER_DATA['SESSION'] == 'sqlalchemy':
                from Database import Database
                cls._app = Database.setup_sessions(cls._app)
            if Environment.SERVER_DATA['SESSION'] == 'mongodb':
                from pymongo import MongoClient
                db_conf = Environment.Databases[
                    Environment.SERVER_DATA['SESSION']]
                cls._app.config['SESSION_MONGODB'] = MongoClient(
                    "%s://%s:%s@%s:%d" %
                    (db_conf['driver'], db_conf['user'], db_conf['password'],
                     db_conf['address'], db_conf['port']))
                cls._app.config['SESSION_MONGODB_DB'] = db_conf['database']
                cls._app.config['SESSION_MONGODB_COLLECT'] = db_conf[
                    'collection']
            cls._session.init_app(cls._app)
            cls._csrf = CSRFProtect()
            cls._csrf.init_app(cls._app)
            if 'SSO' in Environment.Logins:
                from flask_sso import SSO
                cls.sso = SSO()
                cls._app.config['SSO_LOGIN_URL'] = Environment.Logins['SSO'][
                    'LOGIN_URL']
                cls._app.config['SSO_LOGIN_ENDPOINT'] = Environment.Logins[
                    'SSO']['LOGIN_ENDPOINT']
                cls._app.config['SSO_ATTRIBUTE_MAP'] = {
                    item: (value['value'], value['attr'])
                    for item, value in Environment.Logins['SSO']
                    ['ATTRIBUTE_MAP'].items()
                }
                cls.sso.init_app(cls._app)
            if 'OpenID' in Environment.Logins:
                from Utils.Auth.openid import OpenIDConnect
                from flask_openid import OpenID
                cls.openid = OpenIDConnect()
                for key, value in Environment.Logins['OpenID'].items():
                    cls._app.config[key] = value
                cls.openid.init_app(cls._app)
            if 'LDAP' in Environment.Logins:
                if 'LDAP_HOST' not in Environment.Logins[
                        'LDAP'] and 'LDAP_DOMAIN' in Environment.Logins['LDAP']:
                    from activedirectory import Locator
                    ldap = Locator()
                    Environment.Logins['LDAP']['LDAP_HOST'] = ldap.locate_many(
                        Environment.Logins['LDAP']['LDAP_DOMAIN'])[0]
                if 'LDAP_REQUIRED_GROUP' not in Environment.Logins['LDAP']:
                    Environment.Logins['LDAP']['LDAP_REQUIRED_GROUP'] = None
                from Utils.Auth.ldap import LDAP
                for key, val in Environment.Logins['LDAP'].items():
                    cls._app.config[key] = val
                cls.ldap = LDAP(cls._app)
        cls._socket = SocketIO()
        cls._socket.init_app(cls._app)
        return cls._app
from __future__ import print_function
from activedirectory import Client, Creds, Locator, activate

domain = 'freeadi.org'
user = '******'
password = '******'

levels = {
    '0': 'windows 2000',
    '1': 'windows 2003 interim',
    '2': 'windows 2003'
}

creds = Creds(domain)
creds.acquire(user, password)
activate(creds)

locator = Locator()
server = locator.locate(domain)

client = Client(domain)
result = client.search(base='', scope='base', server=server)
assert len(result) == 1
dn, attrs = result[0]
level = attrs['forestFunctionality'][0]
level = levels.get(level, 'unknown')
print('Forest functionality level: %s' % level)
from activedirectory import Client, Creds, Locator, activate

domain = 'freeadi.org'
user = '******'
password = '******'

levels = \
{
    '0': 'windows 2000',
    '1': 'windows 2003 interim',
    '2': 'windows 2003'
}

creds = Creds(domain)
creds.acquire(user, password)
activate(creds)

locator = Locator()
server = locator.locate(domain)

client = Client(domain)
result = client.search(base='', scope='base', server=server)
assert len(result) == 1
dn, attrs = result[0]
level = attrs['forestFunctionality'][0]
level = levels.get(level, 'unknown')
print 'Forest functionality level: %s' % level