summaryrefslogtreecommitdiff
path: root/python/samba/domain/models/value_type.py
blob: a111ce15489fa8243fbd43a3f1a32c1992e31f42 (plain)
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
# Unix SMB/CIFS implementation.
#
# Claim value type model.
#
# Copyright (C) Catalyst.Net Ltd. 2023
#
# Written by Rob van der Linde <rob@catalyst.net.nz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from .exceptions import NotFound
from .fields import BooleanField, DnField, IntegerField, StringField
from .model import Model

# LDAP Syntax to Value Type CN lookup table.
# These are the lookups used by known AD attributes, add new ones as required.
SYNTAX_TO_VALUE_TYPE_CN = {
    "2.5.5.1": "MS-DS-Text",     # Object(DS-DN)
    "2.5.5.2": "MS-DS-Text",     # String(Object-Identifier)
    "2.5.5.8": "MS-DS-YesNo",    # Boolean
    "2.5.5.9": "MS-DS-Number",   # Integer
    "2.5.5.12": "MS-DS-Text",    # String(Unicode)
    "2.5.5.15": "MS-DS-Text",    # String(NT-Sec-Desc)
    "2.5.5.16": "MS-DS-Number",  # LargeInteger
}


class ValueType(Model):
    description = StringField("description")
    display_name = StringField("displayName")
    claim_is_single_valued = BooleanField("msDS-ClaimIsSingleValued")
    claim_is_value_space_restricted = BooleanField(
        "msDS-ClaimIsValueSpaceRestricted")
    claim_value_type = IntegerField("msDS-ClaimValueType")
    is_possible_values_present = BooleanField("msDS-IsPossibleValuesPresent")
    show_in_advanced_view_only = BooleanField("showInAdvancedViewOnly")

    # Backlinks
    value_type_reference_bl = DnField(
        "msDS-ValueTypeReferenceBL", readonly=True)

    @staticmethod
    def get_base_dn(samdb):
        """Return the base DN for the ValueType model.

        :param samdb: SamDB connection
        :return: Dn object of container
        """
        base_dn = samdb.get_config_basedn()
        base_dn.add_child("CN=Value Types,CN=Claims Configuration,CN=Services")
        return base_dn

    @staticmethod
    def get_object_class():
        return "msDS-ValueType"

    @classmethod
    def find(cls, samdb, attribute):
        """Helper function to get ValueType by attribute or raise NotFound.

        :param samdb: SamDB connection
        :param attribute: AttributeSchema object
        :raises: NotFound if not found
        :raises: ValueError for unknown attribute syntax
        """
        # If attribute is None.
        if not attribute:
            raise ValueError("Attribute is required for value type lookup.")

        # Unknown attribute syntax as it isn't in the lookup table.
        syntax = attribute.attribute_syntax
        cn = SYNTAX_TO_VALUE_TYPE_CN.get(syntax)
        if not cn:
            raise ValueError(f"Unable to process attribute syntax {syntax}")

        # This should always return something but should still be handled.
        value_type = cls.get(samdb, cn=cn)
        if value_type is None:
            raise NotFound(f"Could not find claim value type for {attribute}.")

        return value_type