Esempio n. 1
0
def getGC():
    cont = adsi.ADsOpenObject("GC:", options.user, options.password, 0, adsi.IID_IADsContainer)
    enum = adsi.ADsBuildEnumerator(cont)
    # Only 1 child of the global catalog.
    for e in enum:
        gc = e.QueryInterface(adsi.IID_IDirectorySearch)
        return gc
    return None
Esempio n. 2
0
 def from_string(cls,
                 moniker,
                 username=None,
                 password=None,
                 interface=adsi.IID_IADs):
     return cls.from_object(
         adsi.ADsOpenObject(
             moniker, username, password, adsicon.ADS_SECURE_AUTHENTICATION
             | adsicon.ADS_SERVER_BIND | adsicon.ADS_FAST_BIND, interface))
Esempio n. 3
0
def ldap_moniker(root=None, server=None, username=None, password=None):
    #
    # FIXME: Need to allow for GC/WinNT monikers
    #
    if root is None:
        root = adsi.ADsOpenObject(
            ldap_moniker("rootDSE", server), username, password,
            adsicon.ADS_SECURE_AUTHENTICATION | adsicon.ADS_SERVER_BIND
            | adsicon.ADS_FAST_BIND, adsi.IID_IADs).Get("defaultNamingContext")
    prefix, rest = re.match("(\w+://)?(.*)", root).groups()
    if not prefix:
        prefix = "LDAP://"
    if server:
        return "%s%s/%s" % (prefix, server, root)
    else:
        return "%s%s" % (prefix, root)
Esempio n. 4
0
def _search(filter, root=None, server=None, username=None, password=None):
    def get_column_value(hSearch, column):
        #
        # FIXME: Need a more general-purpose way of determining which
        # fields are indeed lists. Either a factory function or a
        # peek at the schema.
        #
        CONVERT_TO_LIST = set(['memberOf', "member"])
        try:
            column_name, column_type, column_values = directory_search.GetColumn(
                hSearch, column)
            if column_name in CONVERT_TO_LIST:
                return list(value for value, type in column_values)
            else:
                for value, type in column_values:
                    return value
        except adsi.error:
            details = sys.exc_info()[1]
            if details[0] == adsicon.E_ADS_COLUMN_NOT_SET:
                return None
            else:
                raise

    pythoncom.CoInitialize()
    try:
        directory_search = adsi.ADsOpenObject(
            ldap_moniker(root, server, username, password), username, password,
            adsicon.ADS_SECURE_AUTHENTICATION | adsicon.ADS_SERVER_BIND
            | adsicon.ADS_FAST_BIND, adsi.IID_IDirectorySearch)
        directory_search.SetSearchPreference([
            (k, (v, )) for k, v in SEARCH_PREFERENCES.items()
        ])

        hSearch = directory_search.ExecuteSearch(filter, columns)
        try:
            hResult = directory_search.GetFirstRow(hSearch)
            while hResult == 0:
                yield Result((column, get_column_value(hSearch, column))
                             for column in columns)
                hResult = directory_search.GetNextRow(hSearch)
        finally:
            directory_search.AbandonSearch(hSearch)
            directory_search.CloseSearchHandle(hSearch)

    finally:
        pythoncom.CoUninitialize()
Esempio n. 5
0
                hSearch, column)
            if column_name in CONVERT_TO_LIST:
                return list(value for value, type in column_values)
            else:
                for value, type in column_values:
                    return value
        except adsi.error, details:
            if details[0] == adsicon.E_ADS_COLUMN_NOT_SET:
                return None
            else:
                raise

    pythoncom.CoInitialize()
    try:
        directory_search = adsi.ADsOpenObject(
            ldap_moniker(root, server, username, password), username, password,
            adsicon.ADS_SECURE_AUTHENTICATION | adsicon.ADS_SERVER_BIND
            | adsicon.ADS_FAST_BIND, adsi.IID_IDirectorySearch)
        directory_search.SetSearchPreference([
            (k, (v, )) for k, v in SEARCH_PREFERENCES.items()
        ])

        hSearch = directory_search.ExecuteSearch(filter, columns)
        try:
            hResult = directory_search.GetFirstRow(hSearch)
            while hResult == 0:
                yield Result((column, get_column_value(hSearch, column))
                             for column in columns)
                hResult = directory_search.GetNextRow(hSearch)
        finally:
            directory_search.AbandonSearch(hSearch)
            directory_search.CloseSearchHandle(hSearch)
 def open_object(self, path, use_password=True):
     return adsi.ADsOpenObject(path, self.username,
                               self.password if use_password else None,
                               adsicon.ADS_SECURE_AUTHENTICATION,
                               adsi.IID_IADs)