| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2022 Red Hat, Inc. |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
import pytest |
| 10 |
import glob |
| 11 |
import base64 |
| 12 |
import re |
| 13 |
from lib389.tasks import * |
| 14 |
from lib389.rewriters import * |
| 15 |
from lib389.idm.user import UserAccounts |
| 16 |
from lib389.utils import * |
| 17 |
from test389.topologies import topology_st |
| 18 |
|
| 19 |
from lib389._constants import DEFAULT_SUFFIX, HOST_STANDALONE, PORT_STANDALONE |
| 20 |
|
| 21 |
samba_missing = False |
| 22 |
try: |
| 23 |
from samba.dcerpc import security |
| 24 |
from samba.ndr import ndr_pack, ndr_unpack |
| 25 |
except: |
| 26 |
samba_missing = True |
| 27 |
pass |
| 28 |
|
| 29 |
log = logging.getLogger(__name__) |
| 30 |
# Skip on versions 1.4.2 and before. Rewriters are expected in 1.4.3 |
| 31 |
pytestmark = [pytest.mark.tier2, |
| 32 |
pytest.mark.skipif(ds_is_older('1.4.3'), reason="Not implemented")] |
| 33 |
|
| 34 |
PW = 'password' |
| 35 |
|
| 36 |
# |
| 37 |
# Necessary because objectcategory relies on cn=xxx RDN |
| 38 |
# while userAccount creates uid=xxx RDN |
| 39 |
# |
| 40 |
def _create_user(inst, schema_container, name, salt): |
| 41 |
dn = 'cn=%s,%s' % (name, schema_container) |
| 42 |
inst.add_s(Entry(( |
| 43 |
dn, { |
| 44 |
'objectClass': 'top person extensibleobject'.split(), |
| 45 |
'cn': name, |
| 46 |
'sn': name, |
| 47 |
'objectcategory': dn, |
| 48 |
"description" : salt, |
| 49 |
'userpassword': PW |
| 50 |
}))) |
| 51 |
|
| 52 |
|
| 53 |
def test_adfilter_objectCategory(topology_st): |
| 54 |
"""Test adfilter objectCategory rewriter function |
| 55 |
|
| 56 |
:id: 9c6493c9-1ee0-4bc2-b5db-7253a04fb6f8 |
| 57 |
:setup: Standalone instance |
| 58 |
:steps: |
| 59 |
1. Add a objectsid rewriter (from librewriters.so) |
| 60 |
2. Add a dummy schema definition of objectsid to prevent nsslapd-verify-filter-schema |
| 61 |
3. Restart the server (to load the rewriter) |
| 62 |
4. Check EQUALITY filter rewrite |
| 63 |
5. Check SUBSTRING search is not replaced |
| 64 |
6. Check PRESENCE search is not replaced so it selects all entries having objectCategory |
| 65 |
:expectedresults: |
| 66 |
1. Add operation should PASS. |
| 67 |
2. Add operations should PASS. |
| 68 |
3. Restart should PASS |
| 69 |
4. Search returns only one entry |
| 70 |
5. Search returns zero entries |
| 71 |
6. Search returns all (20) entries |
| 72 |
""" |
| 73 |
|
| 74 |
librewriters = os.path.join( topology_st.standalone.ds_paths.lib_dir, 'dirsrv/librewriters.so') |
| 75 |
assert librewriters |
| 76 |
|
| 77 |
rewriters = AdRewriters(topology_st.standalone) |
| 78 |
ad_rewriter = rewriters.ensure_state(properties={"cn": "adfilter", "nsslapd-libpath": librewriters}) |
| 79 |
ad_rewriter.add('nsslapd-filterrewriter', "adfilter_rewrite_objectCategory") |
| 80 |
ad_rewriter.create_containers(DEFAULT_SUFFIX) |
| 81 |
schema_container = ad_rewriter.get_schema_dn() |
| 82 |
|
| 83 |
objectcategory_attr = '( NAME \'objectCategory\' DESC \'test of objectCategory\' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )' |
| 84 |
topology_st.standalone.schema.add_schema('attributetypes', [ensure_bytes(objectcategory_attr)]) |
| 85 |
|
| 86 |
topology_st.standalone.restart(60) |
| 87 |
|
| 88 |
# Add a user |
| 89 |
for i in range(0, 20): |
| 90 |
_create_user(topology_st.standalone, schema_container, "user_%d" % i, str(i)) |
| 91 |
|
| 92 |
# Check EQUALITY filter rewrite => it should match only one entry |
| 93 |
for i in range(0, 20): |
| 94 |
ents = topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(objectCategory=user_%d)' % i) |
| 95 |
assert len(ents) == 1 |
| 96 |
|
| 97 |
# Check SUBSTRING search is not replaced |
| 98 |
ents = topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(objectCategory=user_*)') |
| 99 |
assert len(ents) == 0 |
| 100 |
|
| 101 |
# Check PRESENCE search is not replaced so it selects all entries having objectCategory |
| 102 |
ents = topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(objectCategory=*)') |
| 103 |
assert len(ents) == 20 |
| 104 |
|
| 105 |
log.info('Test PASSED') |
| 106 |
|
| 107 |
def sid_to_objectsid(sid): |
| 108 |
return base64.b64encode(ndr_pack(security.dom_sid(sid))).decode('utf-8') |
| 109 |
|
| 110 |
def objectsid_to_sid(objectsid): |
| 111 |
sid = ndr_unpack(security.dom_sid, base64.b64decode(objectsid)) |
| 112 |
return str(sid) |
| 113 |
|
| 114 |
@pytest.mark.skipif(samba_missing, reason="It is missing samba python bindings") |
| 115 |
def test_adfilter_objectSid(topology_st): |
| 116 |
"""Test adfilter objectCategory rewriter function with samba container/users |
| 117 |
|
| 118 |
:id: fc5880ff-4305-47ba-84fb-38429e264e9e |
| 119 |
:setup: Standalone instance |
| 120 |
:steps: |
| 121 |
1. Add a objectsid rewriter (from librewriters.so) |
| 122 |
2. Add a dummy schema definition of objectsid to prevent nsslapd-verify-filter-schema |
| 123 |
3. Restart the server (to load the rewriter) |
| 124 |
4. Add "samba" container/users |
| 125 |
5. Searches using objectsid in string format |
| 126 |
:expectedresults: |
| 127 |
1. Add operation should PASS. |
| 128 |
2. Add operations should PASS. |
| 129 |
3. Restart should PASS |
| 130 |
4. Add "samba" users should PASS |
| 131 |
5. Search returns only one entry |
| 132 |
""" |
| 133 |
librewriters = os.path.join( topology_st.standalone.ds_paths.lib_dir, 'dirsrv/librewriters.so') |
| 134 |
assert librewriters |
| 135 |
|
| 136 |
rewriters = AdRewriters(topology_st.standalone) |
| 137 |
ad_rewriter = rewriters.ensure_state(properties={"cn": "adfilter", "nsslapd-libpath": librewriters}) |
| 138 |
ad_rewriter.add('nsslapd-filterrewriter', "adfilter_rewrite_objectsid") |
| 139 |
ad_rewriter.create_containers(DEFAULT_SUFFIX) |
| 140 |
schema_container = ad_rewriter.get_schema_dn() |
| 141 |
|
| 142 |
# to prevent nsslapd-verify-filter-schema to reject searches with objectsid |
| 143 |
objectcategory_attr = '( NAME \'objectsid\' DESC \'test of objectsid\' SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )' |
| 144 |
topology_st.standalone.schema.add_schema('attributetypes', [ensure_bytes(objectcategory_attr)]) |
| 145 |
|
| 146 |
topology_st.standalone.restart() |
| 147 |
|
| 148 |
# Contains a list of b64encoded SID from https://github.com/SSSD/sssd/blob/supplier/src/tests/intg/data/ad_data.ldif |
| 149 |
SIDs = ["AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EUAQAAA==", |
| 150 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8E9gEAAA==", |
| 151 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EAwIAAA==", |
| 152 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EBAIAAA==", |
| 153 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EBgIAAA==", |
| 154 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EBwIAAA==", |
| 155 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EBQIAAA==", |
| 156 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EAAIAAA==", |
| 157 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EAQIAAA==", |
| 158 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EAgIAAA==", |
| 159 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8ECAIAAA==", |
| 160 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EKQIAAA==", |
| 161 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EOwIAAA==", |
| 162 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EPAIAAA==", |
| 163 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8ECQIAAA==", |
| 164 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8E8gEAAA==", |
| 165 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8ETQQAAA==", |
| 166 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8ETgQAAA==", |
| 167 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EeUMBAA==", |
| 168 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EekMBAA==", |
| 169 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8Ee0MBAA==", |
| 170 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EfEMBAA==", |
| 171 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8ETwQAAA==", |
| 172 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8EUQQAAA==", |
| 173 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8ESUMBAA==", |
| 174 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8ESkMBAA==", |
| 175 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8ES0MBAA==", |
| 176 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8ETEMBAA==", |
| 177 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8E9AEAAA==", |
| 178 |
"AQUAAAAAAAUVAAAADcfLTVzC66zo0l8E9QEAAA=="] |
| 179 |
|
| 180 |
# Add a container and "samba" like users containing objectsid |
| 181 |
users = UserAccounts(topology_st.standalone, schema_container, rdn=None) |
| 182 |
i = 0 |
| 183 |
for sid in SIDs: |
| 184 |
decoded = base64.b64decode(sid) |
| 185 |
user = users.create_test_user(uid=i) |
| 186 |
user.add('objectclass', 'extensibleobject') |
| 187 |
user.replace('objectsid', decoded) |
| 188 |
user.replace('objectSidString', objectsid_to_sid(sid)) |
| 189 |
i = i + 1 |
| 190 |
|
| 191 |
# Check that objectsid rewrite can retrieve the "samba" user |
| 192 |
# using either a string objectsid (i.e. S-1-5...) or a blob objectsid |
| 193 |
for sid_blob in SIDs: |
| 194 |
sid_string = objectsid_to_sid(sid_blob) |
| 195 |
ents_sid_string = topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(objectsid=%s)' % sid_string) |
| 196 |
assert len(ents_sid_string) == 1 |
| 197 |
|