This file is indexed.

/usr/lib/python2.7/dist-packages/twext/who/ldap/_util.py is in python-twext 0.1.b2.dev15059-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- test-case-name: twext.who.ldap.test.test_util -*-
##
# Copyright (c) 2013-2015 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

from ..idirectory import QueryNotSupportedError, FieldName
from ..expression import (
    CompoundExpression, ExistsExpression, MatchExpression,
    MatchFlags, Operand, BooleanExpression
)
from ._constants import LDAPOperand, LDAPMatchType, LDAPMatchFlags



def ldapQueryStringFromQueryStrings(operand, queryStrings):
    """
    Combines LDAP query strings into a single query string.

    @param operand: An LDAP operand (C{u"&"} or C{u"|"}).
    @type operand: L{unicode}

    @param queryStrings: LDAP query strings.
    @type queryStrings: sequence of L{unicode}
    """
    if len(queryStrings) == 1:
        return queryStrings[0]

    elif len(queryStrings) > 1:
        queryTokens = []
        queryTokens.append(u"(")
        queryTokens.append(operand)
        queryTokens.extend(queryStrings)
        queryTokens.append(u")")
        return u"".join(queryTokens)

    else:
        return u""



def ldapQueryStringFromMatchExpression(
    expression, fieldNameToAttributesMap, recordTypeSchemas
):
    """
    Generates an LDAP query string from a match expression.

    @param expression: A match expression.
    @type expression: L{MatchExpression}

    @param fieldNameToAttributesMap: A mapping from field names to native LDAP
        attribute names.
    @type fieldNameToAttributesMap: L{dict} with L{FieldName} keys and sequence
        of L{unicode} values.

    @param recordTypeSchemas: Schema information for record types.
    @type recordTypeSchemas: mapping from L{NamedConstant} to
        L{RecordTypeSchema}

    @return: An LDAP query string.
    @rtype: L{unicode}

    @raises QueryNotSupportedError: If the expression's match type is unknown,
        or if the expresion references an unknown field name (meaning a field
        name not in C{fieldNameToAttributeMap}).
    """

    matchType = LDAPMatchType.fromMatchType(expression.matchType)
    if matchType is None:
        raise QueryNotSupportedError(
            "Unknown match type: {0}".format(matchType)
        )

    flags = tuple(expression.flags)

    if MatchFlags.NOT in flags:
        notOp = LDAPMatchFlags.NOT.value
        operand = LDAPOperand.AND.value
    else:
        notOp = LDAPMatchFlags.none.value
        operand = LDAPOperand.OR.value

    # FIXME: It doesn't look like LDAP queries can be case sensitive.
    # This would mean that it's up to the callers to filter out the false
    # positives...
    #
    # if MatchFlags.caseInsensitive not in flags:
    #     raise NotImplementedError("Need to handle case sensitive")

    fieldName = expression.fieldName

    if fieldName is FieldName.recordType:
        if matchType is not LDAPMatchType.equals:
            raise QueryNotSupportedError(
                "Match type for record type must be equals, not {0!r}"
                .format(expression.matchType)
            )

        try:
            schema = recordTypeSchemas[expression.fieldValue]
        except KeyError:
            raise QueryNotSupportedError(
                "Unmapped record type: {0}".format(expression.fieldValue)
            )

        if notOp:
            valueOperand = LDAPOperand.OR.value
        else:
            valueOperand = LDAPOperand.AND.value

        queryStrings = [
            matchType.queryString.format(
                notOp=notOp, attribute=attribute, value=value,
            )
            for attribute, value in schema.attributes
        ]

        return ldapQueryStringFromQueryStrings(valueOperand, queryStrings)

    else:
        try:
            attributes = fieldNameToAttributesMap[fieldName]
        except KeyError:
            raise QueryNotSupportedError(
                "Unmapped field name: {0}".format(expression.fieldName)
            )

        # Covert to unicode and escape special LDAP query characters
        value = unicode(expression.fieldValue).translate(LDAP_QUOTING_TABLE)

        # Compose an query using each of the LDAP attributes cooresponding to
        # the target field name.

        queryStrings = [
            matchType.queryString.format(
                notOp=notOp, attribute=attribute, value=value
            )
            for attribute in attributes
        ]

        return ldapQueryStringFromQueryStrings(operand, queryStrings)

    raise AssertionError("We shouldn't be here.")



def ldapQueryStringFromExistsExpression(
    expression, fieldNameToAttributesMap, recordTypeSchemas
):
    """
    Generates an LDAP query string from an exists expression.

    @param expression: An exists expression.
    @type expression: L{ExistsExpression}

    @param fieldNameToAttributesMap: A mapping from field names to native LDAP
        attribute names.
    @type fieldNameToAttributesMap: L{dict} with L{FieldName} keys and sequence
        of L{unicode} values.

    @param recordTypeSchemas: Schema information for record types.
    @type recordTypeSchemas: mapping from L{NamedConstant} to
        L{RecordTypeSchema}

    @return: An LDAP query string.
    @rtype: L{unicode}

    @raises QueryNotSupportedError: if the expresion references an unknown
        field name (meaning a field name not in C{fieldNameToAttributeMap}).
    """

    fieldName = expression.fieldName

    try:
        attributes = fieldNameToAttributesMap[fieldName]
    except KeyError:
        raise QueryNotSupportedError(
            "Unmapped field name: {0}".format(expression.fieldName)
        )

    queryStrings = [
        u"({attribute}=*)".format(attribute=attribute)
        for attribute in attributes
    ]

    operand = LDAPOperand.OR.value
    return ldapQueryStringFromQueryStrings(operand, queryStrings)



def ldapQueryStringFromBooleanExpression(
    expression, fieldNameToAttributesMap, recordTypeSchemas
):
    """
    Generates an LDAP query string from a boolean expression.

    @param expression: An boolean expression.
    @type expression: L{BooleanExpression}

    @param fieldNameToAttributesMap: A mapping from field names to native LDAP
        attribute names.
    @type fieldNameToAttributesMap: L{dict} with L{FieldName} keys and sequence
        of L{unicode} values.

    @param recordTypeSchemas: Schema information for record types.
    @type recordTypeSchemas: mapping from L{NamedConstant} to
        L{RecordTypeSchema}

    @return: An LDAP query string.
    @rtype: L{unicode}

    @raises QueryNotSupportedError: if the expresion references an unknown
        field name (meaning a field name not in C{fieldNameToAttributeMap}).
    """

    fieldName = expression.fieldName

    try:
        attributes = fieldNameToAttributesMap[fieldName]
    except KeyError:
        raise QueryNotSupportedError(
            "Unmapped field name: {0}".format(expression.fieldName)
        )

    queryStrings = []
    for attribute in attributes:
        if ":" in attribute:
            attribute, trueValue = attribute.split(":")
        else:
            trueValue = "true"

        queryStrings.append(
            u"({attribute}={trueValue})".format(
                attribute=attribute,
                trueValue=trueValue
            )
        )


    operand = LDAPOperand.OR.value
    return ldapQueryStringFromQueryStrings(operand, queryStrings)



def ldapQueryStringFromCompoundExpression(
    expression, fieldNameToAttributesMap, recordTypeSchemas
):
    """
    Generates an LDAP query string from a compound expression.

    @param expression: A compound expression.
    @type expression: L{MatchExpression}

    @param fieldNameToAttributesMap: A mapping from field names to native LDAP
        attribute names.
    @type fieldNameToAttributesMap: L{dict} with L{FieldName} keys and sequence
        of L{unicode} values.

    @param recordTypeSchemas: Schema information for record types.
    @type recordTypeSchemas: mapping from L{NamedConstant} to
        L{RecordTypeSchema}

    @return: An LDAP query string.
    @rtype: L{unicode}

    @raises QueryNotSupportedError: If any sub-expression cannot be converted
        to an LDAP query.
    """
    if expression.operand is Operand.AND:
        operand = LDAPOperand.AND.value

    elif expression.operand is Operand.OR:
        operand = LDAPOperand.OR.value

    queryStrings = [
        ldapQueryStringFromExpression(
            subExpression,
            fieldNameToAttributesMap, recordTypeSchemas
        )
        for subExpression in expression.expressions
    ]

    return ldapQueryStringFromQueryStrings(operand, queryStrings)



def ldapQueryStringFromExpression(
    expression, fieldNameToAttributesMap, recordTypeSchemas
):
    """
    Converts an expression into an LDAP query string.

    @param expression: An expression.
    @type expression: L{MatchExpression} or L{CompoundExpression}

    @param fieldNameToAttributesMap: A mapping from field names to native LDAP
        attribute names.
    @type fieldNameToAttributesMap: L{dict} with L{FieldName} keys and sequence
        of L{unicode} values.

    @param recordTypeSchemas: Schema information for record types.
    @type recordTypeSchemas: mapping from L{NamedConstant} to
        L{RecordTypeSchema}

    @return: An LDAP query string.
    @rtype: L{unicode}

    @raises QueryNotSupportedError: If the expression cannot be converted to an
        LDAP query.
    """

    if isinstance(expression, MatchExpression):
        return ldapQueryStringFromMatchExpression(
            expression, fieldNameToAttributesMap, recordTypeSchemas
        )

    if isinstance(expression, BooleanExpression):
        return ldapQueryStringFromBooleanExpression(
            expression, fieldNameToAttributesMap, recordTypeSchemas
        )

    if isinstance(expression, ExistsExpression):
        return ldapQueryStringFromExistsExpression(
            expression, fieldNameToAttributesMap, recordTypeSchemas
        )

    if isinstance(expression, CompoundExpression):
        return ldapQueryStringFromCompoundExpression(
            expression, fieldNameToAttributesMap, recordTypeSchemas
        )

    raise QueryNotSupportedError(
        "Unknown expression type: {0!r}".format(expression)
    )


LDAP_QUOTING_TABLE = {
    ord(u"\\"): u"\\5C",
    ord(u"/"): u"\\2F",

    ord(u"("): u"\\28",
    ord(u")"): u"\\29",
    ord(u"*"): u"\\2A",

    ord(u"<"): u"\\3C",
    ord(u"="): u"\\3D",
    ord(u">"): u"\\3E",
    ord(u"~"): u"\\7E",

    ord(u"&"): u"\\26",
    ord(u"|"): u"\\7C",

    ord(u"\0"): u"\\00",
}