codekingpro/portable-devtools
114k
1"""2"""3 4# Created on 2015.03.275#6# Author: Giovanni Cannata7#8# Copyright 2015 - 2020 Giovanni Cannata9#10# This file is part of ldap3.11#12# ldap3 is free software: you can redistribute it and/or modify13# it under the terms of the GNU Lesser General Public License as published14# by the Free Software Foundation, either version 3 of the License, or15# (at your option) any later version.16#17# ldap3 is distributed in the hope that it will be useful,18# but WITHOUT ANY WARRANTY; without even the implied warranty of19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20# GNU Lesser General Public License for more details.21#22# You should have received a copy of the GNU Lesser General Public License23# along with ldap3 in the COPYING and COPYING.LESSER files.24# If not, see <http://www.gnu.org/licenses/>.25 26import ctypes27 28from pyasn1.type.namedtype import NamedTypes, NamedType29from pyasn1.type.tag import Tag, tagClassApplication, tagFormatConstructed30from pyasn1.type.univ import Sequence, OctetString, Integer31from .rfc4511 import ResultCode, LDAPString32from .controls import build_control33 34 35class SicilyBindResponse(Sequence):36 # SicilyBindResponse ::= [APPLICATION 1] SEQUENCE {37 #38 # resultCode ENUMERATED {39 # success (0),40 # protocolError (2),41 # adminLimitExceeded (11),42 # inappropriateAuthentication (48),43 # invalidCredentials (49),44 # busy (51),45 # unavailable (52),46 # unwillingToPerform (53),47 # other (80) },48 #49 # serverCreds OCTET STRING,50 # errorMessage LDAPString }51 # BindResponse ::= [APPLICATION 1] SEQUENCE {52 # COMPONENTS OF LDAPResult,53 # serverSaslCreds [7] OCTET STRING OPTIONAL }54 tagSet = Sequence.tagSet.tagImplicitly(Tag(tagClassApplication, tagFormatConstructed, 1))55 componentType = NamedTypes(NamedType('resultCode', ResultCode()),56 NamedType('serverCreds', OctetString()),57 NamedType('errorMessage', LDAPString())58 )59 60 61class DirSyncControlRequestValue(Sequence):62 # DirSyncRequestValue ::= SEQUENCE {63 # Flags integer64 # MaxBytes integer65 # Cookie OCTET STRING }66 componentType = NamedTypes(NamedType('Flags', Integer()),67 NamedType('MaxBytes', Integer()),68 NamedType('Cookie', OctetString())69 )70 71 72class DirSyncControlResponseValue(Sequence):73 # DirSyncResponseValue ::= SEQUENCE {74 # MoreResults INTEGER75 # unused INTEGER76 # CookieServer OCTET STRING77 # }78 componentType = NamedTypes(NamedType('MoreResults', Integer()),79 NamedType('unused', Integer()),80 NamedType('CookieServer', OctetString())81 )82 83 84class SdFlags(Sequence):85 # SDFlagsRequestValue ::= SEQUENCE {86 # Flags INTEGER87 # }88 componentType = NamedTypes(NamedType('Flags', Integer())89 )90 91 92class ExtendedDN(Sequence):93 # A flag value 0 specifies that the GUID and SID values be returned in hexadecimal string94 # A flag value of 1 will return the GUID and SID values in standard string format95 componentType = NamedTypes(NamedType('option', Integer())96 )97 98 99def dir_sync_control(criticality, object_security, ancestors_first, public_data_only, incremental_values, max_length, cookie):100 control_value = DirSyncControlRequestValue()101 flags = 0x0102 if object_security:103 flags |= 0x00000001104 105 if ancestors_first:106 flags |= 0x00000800107 108 if public_data_only:109 flags |= 0x00002000110 111 if incremental_values:112 flags |= 0x80000000113 # converts flags to signed 32 bit (AD expects a 4 bytes long unsigned integer, but ASN.1 Integer type is signed114 # so the BER encoder gives back a 5 bytes long signed integer115 flags = ctypes.c_long(flags & 0xFFFFFFFF).value116 117 control_value.setComponentByName('Flags', flags)118 control_value.setComponentByName('MaxBytes', max_length)119 if cookie:120 control_value.setComponentByName('Cookie', cookie)121 else:122 control_value.setComponentByName('Cookie', OctetString(''))123 return build_control('1.2.840.113556.1.4.841', criticality, control_value)124 125 126def extended_dn_control(criticality=False, hex_format=False):127 control_value = ExtendedDN()128 control_value.setComponentByName('option', Integer(not hex_format))129 return build_control('1.2.840.113556.1.4.529', criticality, control_value)130 131 132def show_deleted_control(criticality=False):133 return build_control('1.2.840.113556.1.4.417', criticality, value=None)134 135 136def security_descriptor_control(criticality=False, sdflags=0x0F):137 sdcontrol = SdFlags()138 sdcontrol.setComponentByName('Flags', sdflags)139 return [build_control('1.2.840.113556.1.4.801', criticality, sdcontrol)]140 141def persistent_search_control(criticality=False):142 return build_control('1.2.840.113556.1.4.528', criticality, value=None)