| 1 | import os |
| 2 | import struct |
| 3 | import sys |
| 4 | |
| 5 | import pytest |
| 6 | from conftest import get_config, test_ace |
| 7 | |
| 8 | from bonsai import LDAPClient |
| 9 | from bonsai.active_directory.acl import ACE, ACL, ACLRevision |
| 10 | from bonsai.active_directory.sid import SID |
| 11 | from bonsai.active_directory import SecurityDescriptor |
| 12 | |
| 13 | |
| 14 | @pytest.fixture |
| 15 | def client(): |
| 16 | """Create a client with authentication settings.""" |
| 17 | cfg = get_config() |
| 18 | url = f"ldap://{cfg['SERVER']['hostname']}:{cfg['SERVER']['port']}" |
| 19 | client = LDAPClient(url) |
| 20 | client.set_credentials( |
| 21 | "SIMPLE", user=cfg["SIMPLEAUTH"]["user"], password=cfg["SIMPLEAUTH"]["password"] |
| 22 | ) |
| 23 | return client |
| 24 | |
| 25 | |
| 26 | @pytest.fixture |
| 27 | def test_sd(): |
| 28 | return SecurityDescriptor( |
| 29 | { |
| 30 | "self_relative": False, |
| 31 | "rm_control_valid": False, |
| 32 | "sacl_protected": False, |
| 33 | "dacl_protected": False, |
| 34 | "sacl_auto_inherited": False, |
| 35 | "dacl_auto_inherited": False, |
| 36 | "sacl_computed_inheritance_required": False, |
| 37 | "dacl_computed_inheritance_required": False, |
| 38 | "server_security": False, |
| 39 | "dacl_trusted": True, |
| 40 | "sacl_defaulted": False, |
| 41 | "sacl_present": False, |
| 42 | "dacl_defaulted": False, |
| 43 | "dacl_present": False, |
| 44 | "group_defaulted": True, |
| 45 | "owner_defaulted": True, |
| 46 | }, |
| 47 | SID("S-1-1-0"), |
| 48 | SID("S-1-1-0"), |
| 49 | None, |
| 50 | None, |
| 51 | ) |
| 52 | |
| 53 | |
| 54 | def test_from_binary(): |
| 55 | """Test from_binary method.""" |
| 56 | with pytest.raises(TypeError): |
| 57 | _ = SecurityDescriptor.from_binary(0) |
| 58 | with pytest.raises(TypeError): |
| 59 | _ = SecurityDescriptor.from_binary("INVALID") |
| 60 | with pytest.raises(ValueError): |
| 61 | _ = SecurityDescriptor.from_binary(b"\x05\nH\x00\x07\x00") |
| 62 | curdir = os.path.abspath(os.path.dirname(__file__)) |
| 63 | with open(os.path.join(curdir, "testenv", "sd-sample0.bin"), "rb") as data: |
| 64 | input_data = data.read() |
| 65 | sec_desc = SecurityDescriptor.from_binary(input_data) |
| 66 | assert sec_desc.revision == 1 |
| 67 | assert sec_desc.group_sid == "S-1-5-21-3526669579-2242266465-3136906013-512" |
| 68 | assert sec_desc.owner_sid == "S-1-5-21-3526669579-2242266465-3136906013-512" |
| 69 | assert sec_desc.sbz1 == 0 |
| 70 | assert sec_desc.control["dacl_present"] |
| 71 | assert len(sec_desc.dacl.aces) == 24 |
| 72 | assert sec_desc.dacl.aces[0].type == 5 |
| 73 | assert str(sec_desc.dacl.aces[0].trustee_sid) == "S-1-5-32-554" |
| 74 | assert not sec_desc.control["sacl_present"] |
| 75 | assert sec_desc.sacl is None |
| 76 | with open(os.path.join(curdir, "testenv", "sd-sample1.bin"), "rb") as data: |
| 77 | input_data = data.read() |
| 78 | sec_desc = SecurityDescriptor.from_binary(input_data) |
| 79 | assert sec_desc.revision == 1 |
| 80 | assert sec_desc.group_sid == "S-1-5-21-3526669579-2242266465-3136906013-512" |
| 81 | assert sec_desc.owner_sid == "S-1-5-21-3526669579-2242266465-3136906013-512" |
| 82 | assert sec_desc.sbz1 == 0 |
| 83 | assert sec_desc.control["dacl_present"] |
| 84 | assert len(sec_desc.dacl.aces) == 24 |
| 85 | assert sec_desc.dacl.aces[0].type == 5 |
| 86 | assert sec_desc.dacl.aces[0].trustee_sid == "S-1-5-32-554" |
| 87 | assert sec_desc.control["sacl_present"] |
| 88 | assert len(sec_desc.sacl.aces) == 3 |
| 89 | assert sec_desc.sacl.aces[0].type == 2 |
| 90 | assert sec_desc.sacl.aces[0].trustee_sid == "S-1-1-0" |
| 91 | |
| 92 | |
| 93 | @pytest.mark.parametrize( |
| 94 | "file", |
| 95 | ["sd-sample0.bin", "sd-sample1.bin"], |
| 96 | ) |
| 97 | def test_to_binary(file): |
| 98 | """Test to_binary method.""" |
| 99 | curdir = os.path.abspath(os.path.dirname(__file__)) |
| 100 | with open(os.path.join(curdir, "testenv", file), "rb") as data: |
| 101 | expected_data = data.read() |
| 102 | test_sec_desc = SecurityDescriptor.from_binary(expected_data) |
| 103 | test_data = test_sec_desc.to_binary() |
| 104 | ( |
| 105 | expected_rev, |
| 106 | expected_sbz1, |
| 107 | expected_ctrl, |
| 108 | expected_offset_owner, |
| 109 | expected_offset_group, |
| 110 | expected_offset_sacl, |
| 111 | expected_offset_dacl, |
| 112 | ) = struct.unpack("<BBHIIII", expected_data[:20]) |
| 113 | ( |
| 114 | test_rev, |
| 115 | test_sbz1, |
| 116 | test_ctrl, |
| 117 | test_offset_owner, |
| 118 | test_offset_group, |
| 119 | test_offset_sacl, |
| 120 | test_offset_dacl, |
| 121 | ) = struct.unpack("<BBHIIII", test_data[:20]) |
| 122 | assert len(test_data) == len(expected_data) |
| 123 | assert test_rev == expected_rev |
| 124 | assert test_sbz1 == expected_sbz1 |
| 125 | assert test_ctrl == expected_ctrl |
| 126 | if expected_offset_owner: |
| 127 | assert ( |
| 128 | test_data[ |
| 129 | test_offset_owner : test_offset_owner + test_sec_desc.owner_sid.size |
| 130 | ] |
| 131 | == expected_data[ |
| 132 | expected_offset_owner : expected_offset_owner |
| 133 | + test_sec_desc.owner_sid.size |
| 134 | ] |
| 135 | ) |
| 136 | if expected_offset_group: |
| 137 | assert ( |
| 138 | test_data[ |
| 139 | test_offset_group : test_offset_group + test_sec_desc.group_sid.size |
| 140 | ] |
| 141 | == expected_data[ |
| 142 | expected_offset_group : expected_offset_group |
| 143 | + test_sec_desc.group_sid.size |
| 144 | ] |
| 145 | ) |
| 146 | if expected_offset_sacl: |
| 147 | assert ( |
| 148 | test_data[test_offset_sacl : test_offset_sacl + test_sec_desc.sacl.size] |
| 149 | == expected_data[ |
| 150 | expected_offset_sacl : expected_offset_sacl |
| 151 | + test_sec_desc.sacl.size |
| 152 | ] |
| 153 | ) |
| 154 | if expected_offset_dacl: |
| 155 | assert ( |
| 156 | test_data[test_offset_dacl : test_offset_dacl + test_sec_desc.dacl.size] |
| 157 | == expected_data[ |
| 158 | expected_offset_dacl : expected_offset_dacl |
| 159 | + test_sec_desc.dacl.size |
| 160 | ] |
| 161 | ) |
| 162 | assert SecurityDescriptor.from_binary(test_data).to_binary() == test_data |
| 163 | |
| 164 | |
| 165 | @pytest.mark.skipif( |
| 166 | not sys.platform.startswith("win"), |
| 167 | reason="Cannot query SecurityDescriptor from OpenLDAP", |
| 168 | ) |
| 169 | @pytest.mark.parametrize( |
| 170 | "sd_flags, owner_sid, group_sid, dacl, sacl", |
| 171 | [ |
| 172 | (1, True, False, False, False), |
| 173 | (2, False, True, False, False), |
| 174 | (3, True, True, False, False), |
| 175 | (4, False, False, True, False), |
| 176 | (8, False, False, False, True), |
| 177 | (15, True, True, True, True), |
| 178 | ], |
| 179 | ids=["only-owner", "only-group", "owner-group", "only-dacl", "only-sacl", "all"], |
| 180 | ) |
| 181 | def test_sd_flags(client, sd_flags, owner_sid, group_sid, dacl, sacl): |
| 182 | """Test LDAP_SERVER_SD_FLAGS_OID control""" |
| 183 | client.sd_flags = sd_flags |
| 184 | with client.connect() as conn: |
| 185 | res = conn.search( |
| 186 | "cn=chuck,ou=nerdherd,dc=bonsai,dc=test", |
| 187 | 0, |
| 188 | attrlist=["nTSecurityDescriptor"], |
| 189 | )[0] |
| 190 | sec_desc = SecurityDescriptor.from_binary(res["nTSecurityDescriptor"][0]) |
| 191 | assert sec_desc.revision == 1 |
| 192 | if owner_sid: |
| 193 | assert sec_desc.owner_sid is not None |
| 194 | assert isinstance(sec_desc.owner_sid, SID) |
| 195 | else: |
| 196 | assert sec_desc.owner_sid is None |
| 197 | if group_sid: |
| 198 | assert sec_desc.group_sid is not None |
| 199 | assert isinstance(sec_desc.group_sid, SID) |
| 200 | else: |
| 201 | assert sec_desc.group_sid is None |
| 202 | assert sec_desc.control["dacl_present"] is dacl |
| 203 | if dacl: |
| 204 | assert isinstance(sec_desc.dacl, ACL) |
| 205 | else: |
| 206 | assert sec_desc.dacl is None |
| 207 | assert sec_desc.control["sacl_present"] is sacl |
| 208 | if sacl: |
| 209 | assert isinstance(sec_desc.sacl, ACL) |
| 210 | else: |
| 211 | assert sec_desc.sacl is None |
| 212 | |
| 213 | |
| 214 | def test_sd_set_control(test_sd: SecurityDescriptor): |
| 215 | """Test setting controls for SecurityDescriptor.""" |
| 216 | with pytest.raises(TypeError): |
| 217 | test_sd.set_control("string") |
| 218 | with pytest.raises(TypeError): |
| 219 | test_sd.set_control({"a": "b", "c": True}) |
| 220 | with pytest.raises(ValueError): |
| 221 | test_sd.set_control(-2) |
| 222 | test_sd.set_control(0) |
| 223 | assert all(val is False for val in test_sd.control.values()) |
| 224 | test_sd.control = {"sacl_protected": True} |
| 225 | assert test_sd.control["sacl_protected"] |
| 226 | assert all( |
| 227 | val is False for key, val in test_sd.control.items() if key != "sacl_protected" |
| 228 | ) |
| 229 | test_sd.control = 0x8 + 0x20 |
| 230 | assert test_sd.control["dacl_defaulted"] |
| 231 | assert test_sd.control["sacl_defaulted"] |
| 232 | assert all( |
| 233 | val is False |
| 234 | for key, val in test_sd.control.items() |
| 235 | if key not in ("dacl_defaulted", "sacl_defaulted") |
| 236 | ) |
| 237 | |
| 238 | |
| 239 | def test_sd_set_owner_sid(test_sd: SecurityDescriptor): |
| 240 | """Test setting owner SID for SecurityDescriptor.""" |
| 241 | assert test_sd.owner_sid == SID("S-1-1-0") |
| 242 | with pytest.raises(TypeError): |
| 243 | test_sd.set_owner_sid("string") |
| 244 | with pytest.raises(TypeError): |
| 245 | test_sd.set_owner_sid(0) |
| 246 | with pytest.raises(TypeError): |
| 247 | test_sd.owner_sid = True |
| 248 | test_sd.set_owner_sid(SID("S-1-5-10")) |
| 249 | assert test_sd.owner_sid == SID("S-1-5-10") |
| 250 | test_sd.owner_sid = SID("S-1-5-20") |
| 251 | assert test_sd.owner_sid == SID("S-1-5-20") |
| 252 | |
| 253 | |
| 254 | def test_sd_set_group_sid(test_sd: SecurityDescriptor): |
| 255 | """Test setting group SID for SecurityDescriptor.""" |
| 256 | assert test_sd.group_sid == SID("S-1-1-0") |
| 257 | with pytest.raises(TypeError): |
| 258 | test_sd.set_group_sid("string") |
| 259 | with pytest.raises(TypeError): |
| 260 | test_sd.set_group_sid(0) |
| 261 | with pytest.raises(TypeError): |
| 262 | test_sd.group_sid = True |
| 263 | test_sd.set_group_sid(SID("S-1-5-10")) |
| 264 | assert test_sd.group_sid == SID("S-1-5-10") |
| 265 | test_sd.group_sid = SID("S-1-5-20") |
| 266 | assert test_sd.group_sid == SID("S-1-5-20") |
| 267 | |
| 268 | |
| 269 | def test_sd_set_dacl(test_sd: SecurityDescriptor, test_ace: ACE): |
| 270 | """Test setting dacl for SecurityDescriptor.""" |
| 271 | assert test_sd.dacl is None |
| 272 | with pytest.raises(TypeError): |
| 273 | test_sd.set_dacl("string") |
| 274 | with pytest.raises(TypeError): |
| 275 | test_sd.set_dacl(0) |
| 276 | with pytest.raises(TypeError): |
| 277 | test_sd.dacl = True |
| 278 | with pytest.raises(TypeError): |
| 279 | test_sd.set_dacl(["A", "b"]) |
| 280 | test_sd.set_dacl(ACL(ACLRevision.ACL_REVISION, [])) |
| 281 | assert test_sd.dacl is not None |
| 282 | assert test_sd.control["dacl_present"] |
| 283 | test_sd.dacl = ACL(ACLRevision.ACL_REVISION, [test_ace]) |
| 284 | assert test_sd.dacl is not None |
| 285 | assert test_sd.dacl.aces[0] == test_ace |
| 286 | test_sd.dacl = None |
| 287 | assert test_sd.dacl is None |
| 288 | assert test_sd.control["dacl_present"] is False |
| 289 | |
| 290 | |
| 291 | def test_sd_set_sacl(test_sd: SecurityDescriptor, test_ace: ACE): |
| 292 | """Test setting sacl for SecurityDescriptor.""" |
| 293 | assert test_sd.sacl is None |
| 294 | with pytest.raises(TypeError): |
| 295 | test_sd.set_sacl("string") |
| 296 | with pytest.raises(TypeError): |
| 297 | test_sd.set_sacl(0) |
| 298 | with pytest.raises(TypeError): |
| 299 | test_sd.sacl = True |
| 300 | with pytest.raises(TypeError): |
| 301 | test_sd.set_sacl(["A", "b"]) |
| 302 | test_sd.set_sacl(ACL(ACLRevision.ACL_REVISION, [])) |
| 303 | assert test_sd.sacl is not None |
| 304 | assert test_sd.control["sacl_present"] |
| 305 | test_sd.sacl = ACL(ACLRevision.ACL_REVISION, [test_ace]) |
| 306 | assert test_sd.sacl is not None |
| 307 | assert test_sd.sacl.aces[0] == test_ace |
| 308 | test_sd.sacl = None |
| 309 | assert test_sd.sacl is None |
| 310 | assert test_sd.control["sacl_present"] is False |