| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2020 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 |
|
| 10 |
import os |
| 11 |
import pytest |
| 12 |
import ldap.sasl |
| 13 |
from lib389.tasks import * |
| 14 |
from lib389.utils import * |
| 15 |
from test389.topologies import topology_m2, topology_st |
| 16 |
from lib389._constants import * |
| 17 |
from lib389.replica import ReplicationManager |
| 18 |
from lib389.config import CertmapLegacy, Config |
| 19 |
from lib389.idm.user import UserAccounts |
| 20 |
from lib389.nss_ssl import NssSsl, USER_ISSUER |
| 21 |
|
| 22 |
pytestmark = [pytest.mark.tier1, |
| 23 |
pytest.mark.skipif(ds_is_older('1.3.5'), reason="Not implemented")] |
| 24 |
|
| 25 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 26 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 27 |
log = logging.getLogger(__name__) |
| 28 |
|
| 29 |
test_user_uid = 1000 |
| 30 |
|
| 31 |
ISSUER = 'cn=CAcert' |
| 32 |
CACERT = 'CAcertificate' |
| 33 |
M1SERVERCERT = 'Server-Cert1' |
| 34 |
M2SERVERCERT = 'Server-Cert2' |
| 35 |
M1LDAPSPORT = '41636' |
| 36 |
M2LDAPSPORT = '42636' |
| 37 |
M1SUBJECT = 'CN=' + os.uname()[1] + ',OU=389 Directory Server' |
| 38 |
M2SUBJECT = 'CN=' + os.uname()[1] + ',OU=390 Directory Server' |
| 39 |
|
| 40 |
|
| 41 |
def add_entry(server, name, rdntmpl, start, num): |
| 42 |
log.info("\n######################### Adding %d entries to %s ######################\n" % (num, name)) |
| 43 |
|
| 44 |
for i in range(num): |
| 45 |
ii = start + i |
| 46 |
dn = '%s%d,%s' % (rdntmpl, ii, DEFAULT_SUFFIX) |
| 47 |
server.add_s(Entry((dn, {'objectclass': 'top person extensibleObject'.split(), |
| 48 |
'uid': '%s%d' % (rdntmpl, ii), |
| 49 |
'cn': '%s user%d' % (name, ii), |
| 50 |
'sn': 'user%d' % (ii)}))) |
| 51 |
|
| 52 |
|
| 53 |
def check_pems(confdir, mycacert, myservercert, myserverkey, notexist): |
| 54 |
log.info("\n######################### Check PEM files (%s, %s, %s)%s in %s ######################\n" |
| 55 |
% (mycacert, myservercert, myserverkey, notexist, confdir)) |
| 56 |
global cacert |
| 57 |
cacert = f"{mycacert}.pem" |
| 58 |
if os.path.isfile(cacert): |
| 59 |
if notexist == "": |
| 60 |
log.info('%s is successfully generated.' % cacert) |
| 61 |
else: |
| 62 |
log.info('%s is incorrecly generated.' % cacert) |
| 63 |
assert False |
| 64 |
else: |
| 65 |
if notexist == "": |
| 66 |
log.fatal('%s is not generated.' % cacert) |
| 67 |
assert False |
| 68 |
else: |
| 69 |
log.info('%s is correctly not generated.' % cacert) |
| 70 |
servercert = f"{myservercert}.pem" |
| 71 |
if os.path.isfile(servercert): |
| 72 |
if notexist == "": |
| 73 |
log.info('%s is successfully generated.' % servercert) |
| 74 |
else: |
| 75 |
log.info('%s is incorrecly generated.' % servercert) |
| 76 |
assert False |
| 77 |
else: |
| 78 |
if notexist == "": |
| 79 |
log.fatal('%s was not generated.' % servercert) |
| 80 |
assert False |
| 81 |
else: |
| 82 |
log.info('%s is correctly not generated.' % servercert) |
| 83 |
serverkey = f"{myserverkey}.pem" |
| 84 |
if os.path.isfile(serverkey): |
| 85 |
if notexist == "": |
| 86 |
log.info('%s is successfully generated.' % serverkey) |
| 87 |
else: |
| 88 |
log.info('%s is incorrectly generated.' % serverkey) |
| 89 |
assert False |
| 90 |
else: |
| 91 |
if notexist == "": |
| 92 |
log.fatal('%s was not generated.' % serverkey) |
| 93 |
assert False |
| 94 |
else: |
| 95 |
log.info('%s is correctly not generated.' % serverkey) |
| 96 |
|
| 97 |
|
| 98 |
def relocate_pem_files(topology_m2): |
| 99 |
log.info("######################### Relocate PEM files on supplier1 ######################") |
| 100 |
certdir_prefix = "/dev/shm" |
| 101 |
mycacert = os.path.join(certdir_prefix, "MyCA") |
| 102 |
topology_m2.ms["supplier1"].encryption.set('CACertExtractFile', mycacert) |
| 103 |
myservercert = os.path.join(certdir_prefix, "MyServerCert1") |
| 104 |
myserverkey = os.path.join(certdir_prefix, "MyServerKey1") |
| 105 |
topology_m2.ms["supplier1"].rsa.apply_mods([(ldap.MOD_REPLACE, 'ServerCertExtractFile', myservercert), |
| 106 |
(ldap.MOD_REPLACE, 'ServerKeyExtractFile', myserverkey)]) |
| 107 |
log.info("##### restart supplier1") |
| 108 |
topology_m2.ms["supplier1"].restart() |
| 109 |
check_pems(certdir_prefix, mycacert, myservercert, myserverkey, "") |
| 110 |
|
| 111 |
def test_openldap_no_nss_crypto(topology_m2): |
| 112 |
"""Check that we allow usage of OpenLDAP libraries |
| 113 |
that don't use NSS for crypto |
| 114 |
|
| 115 |
:id: 0a622f3d-8ba5-4df2-a1de-1fb2237da40a |
| 116 |
:setup: Replication with two suppliers: |
| 117 |
supplier_1 ----- startTLS -----> supplier_2; |
| 118 |
supplier_1 <-- TLS_clientAuth -- supplier_2; |
| 119 |
nsslapd-extract-pemfiles set to 'on' on both suppliers |
| 120 |
without specifying cert names |
| 121 |
:steps: |
| 122 |
1. Add 5 users to supplier 1 and 2 |
| 123 |
2. Check that the users were successfully replicated |
| 124 |
3. Relocate PEM files on supplier 1 |
| 125 |
4. Check PEM files in supplier 1 config directory |
| 126 |
5. Add 5 users more to supplier 1 and 2 |
| 127 |
6. Check that the users were successfully replicated |
| 128 |
7. Export userRoot on supplier 1 |
| 129 |
:expectedresults: |
| 130 |
1. Users should be successfully added |
| 131 |
2. Users should be successfully replicated |
| 132 |
3. Operation should be successful |
| 133 |
4. PEM files should be found |
| 134 |
5. Users should be successfully added |
| 135 |
6. Users should be successfully replicated |
| 136 |
7. Operation should be successful |
| 137 |
""" |
| 138 |
|
| 139 |
log.info("Ticket 47536 - Allow usage of OpenLDAP libraries that don't use NSS for crypto") |
| 140 |
|
| 141 |
m1 = topology_m2.ms["supplier1"] |
| 142 |
m2 = topology_m2.ms["supplier2"] |
| 143 |
[i.enable_tls() for i in topology_m2] |
| 144 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 145 |
repl.test_replication(m1, m2) |
| 146 |
|
| 147 |
add_entry(m1, 'supplier1', 'uid=m1user', 0, 5) |
| 148 |
add_entry(m2, 'supplier2', 'uid=m2user', 0, 5) |
| 149 |
repl.wait_for_replication(m1, m2) |
| 150 |
repl.wait_for_replication(m2, m1) |
| 151 |
|
| 152 |
log.info('##### Searching for entries on supplier1...') |
| 153 |
entries = m1.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)') |
| 154 |
assert 11 == len(entries) |
| 155 |
|
| 156 |
log.info('##### Searching for entries on supplier2...') |
| 157 |
entries = m2.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)') |
| 158 |
assert 11 == len(entries) |
| 159 |
|
| 160 |
relocate_pem_files(topology_m2) |
| 161 |
|
| 162 |
add_entry(m1, 'supplier1', 'uid=m1user', 10, 5) |
| 163 |
add_entry(m2, 'supplier2', 'uid=m2user', 10, 5) |
| 164 |
|
| 165 |
repl.wait_for_replication(m1, m2) |
| 166 |
repl.wait_for_replication(m2, m1) |
| 167 |
|
| 168 |
log.info('##### Searching for entries on supplier1...') |
| 169 |
entries = m1.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)') |
| 170 |
assert 21 == len(entries) |
| 171 |
|
| 172 |
log.info('##### Searching for entries on supplier2...') |
| 173 |
entries = m2.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)') |
| 174 |
assert 21 == len(entries) |
| 175 |
|
| 176 |
output_file = os.path.join(m1.get_ldif_dir(), "supplier1.ldif") |
| 177 |
m1.tasks.exportLDIF(benamebase='userRoot', output_file=output_file, args={'wait': True}) |
| 178 |
|
| 179 |
log.info("Ticket 47536 - PASSED") |
| 180 |
|
| 181 |
|
| 182 |
def create_sasl_user(inst, users, ssca, cn, pw=PW_DM): |
| 183 |
# Create a dict with user data needed for certificate authentication as an user |
| 184 |
|
| 185 |
global test_user_uid |
| 186 |
|
| 187 |
assert cn.startswith('test_') |
| 188 |
subjectDN = USER_ISSUER.format(HOSTNAME=cn) |
| 189 |
properties = { |
| 190 |
'uid': cn, |
| 191 |
'cn': cn, |
| 192 |
'sn': cn, |
| 193 |
'uidNumber': str(test_user_uid), |
| 194 |
'gidNumber': '2000', |
| 195 |
'homeDirectory': '/home/' + cn, |
| 196 |
'userPassword': pw, |
| 197 |
'nsCertSubjectDN': subjectDN, |
| 198 |
} |
| 199 |
user = users.create(properties=properties) |
| 200 |
test_user_uid += 1 |
| 201 |
|
| 202 |
# Create user's certificate |
| 203 |
ssca.create_rsa_user(cn) |
| 204 |
|
| 205 |
# Get the details of where the key and crt are. |
| 206 |
tls_locs = ssca.get_rsa_user(cn) |
| 207 |
user.enroll_certificate(tls_locs['crt_der_path']) |
| 208 |
|
| 209 |
return { 'user': user, |
| 210 |
'subjectDN': subjectDN, |
| 211 |
'ssca_dir': inst.get_ssca_dir(), |
| 212 |
'tls_locs': tls_locs, |
| 213 |
'key': tls_locs['key'], |
| 214 |
'crt': tls_locs['crt'], |
| 215 |
'cn': cn, |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
def remove_test_users(inst): |
| 220 |
if inst.state == DIRSRV_STATE_ONLINE: |
| 221 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 222 |
for user in users.list(): |
| 223 |
if user.rdn.startswith('test_'): |
| 224 |
user.delete() |
| 225 |
|
| 226 |
|
| 227 |
def rebind(inst): |
| 228 |
inst.simple_bind_s(DN_DM, PW_DM, escapehatch='i am sure') |
| 229 |
|
| 230 |
|
| 231 |
def sasl_bind_as_user(inst, user): |
| 232 |
# Bind with user then rebind as Directory Manager |
| 233 |
dn = user["subjectDN"] |
| 234 |
log.info(f'Trying to bind with {dn} certificate') |
| 235 |
try: |
| 236 |
inst.open(saslmethod='EXTERNAL', connOnly=True, certdir=user['ssca_dir'], |
| 237 |
userkey=user['key'], usercert=user['crt']) |
| 238 |
log.info(f'Bind with {dn} certificate is successful.') |
| 239 |
rebind(inst) |
| 240 |
except ldap.LDAPError as e: |
| 241 |
log.error(f'Bind with {dn} certificate failed. Error is: {e}.') |
| 242 |
rebind(inst) |
| 243 |
raise e |
| 244 |
|
| 245 |
|
| 246 |
def test_bind_certifcate_with_unescaped_subject(topology_m2, request): |
| 247 |
"""Test close connection on failed bind with a failed cert mapping |
| 248 |
|
| 249 |
:id: 7545ad30-21dc-11ef-a27e-482ae39447e5 |
| 250 |
:setup: Replication with two suppliers |
| 251 |
(Only one instance is used but it is better to reuse the |
| 252 |
same topology than the other tests in this module) |
| 253 |
:steps: |
| 254 |
1. perform cleanup and initialize security framework |
| 255 |
2. create user1 and its associated certificate |
| 256 |
3. create user2 and its associated certificate. user2 bane include a wildchar |
| 257 |
4. Check that subtring searches are working as expected |
| 258 |
5. configure certmap and set nsslapd-certmap-basedn |
| 259 |
6. check that EXTERNAL is listed in supported mechns. |
| 260 |
7. bind using user1 certificate |
| 261 |
8. bind using user2 certificate |
| 262 |
9. delete user2 but keep its certificates |
| 263 |
10. bind using user2 certificate |
| 264 |
:expectedresults: |
| 265 |
1. success |
| 266 |
2. success |
| 267 |
3. success |
| 268 |
4. success |
| 269 |
5. success |
| 270 |
6. success |
| 271 |
7. success |
| 272 |
8. success |
| 273 |
9. success |
| 274 |
10. should get INVALID_CREDENTIALS exception |
| 275 |
""" |
| 276 |
|
| 277 |
# Perform some cleanup and initialize the security framework |
| 278 |
inst = topology_m2.ms["supplier1"] |
| 279 |
rebind(inst) |
| 280 |
inst.enable_tls() |
| 281 |
ssca = NssSsl(dbpath=inst.get_ssca_dir()) |
| 282 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 283 |
remove_test_users(inst) |
| 284 |
|
| 285 |
# Prepare to update certmap.conf file |
| 286 |
cm = CertmapLegacy(inst) |
| 287 |
certmaps = cm.list() |
| 288 |
previous_certmaps_default = certmaps['default'] |
| 289 |
log.info(f'previous_certmaps_default = {previous_certmaps_default}') |
| 290 |
|
| 291 |
# register cleanup finalizer before actually changing the certmap.conf file |
| 292 |
def fin(): |
| 293 |
if inst.status(): |
| 294 |
rebind(inst) |
| 295 |
# Restore certmaps |
| 296 |
if not DEBUGGING: |
| 297 |
remove_test_users(inst) |
| 298 |
certmaps['default'] = previous_certmaps_default |
| 299 |
cm.set(certmaps) |
| 300 |
|
| 301 |
request.addfinalizer(fin) |
| 302 |
|
| 303 |
# update the certmap.conf file |
| 304 |
certmaps['default'] = { |
| 305 |
**{ key: None for key in previous_certmaps_default.keys() }, |
| 306 |
'VerifyCert': 'off', |
| 307 |
'DNComps': '', |
| 308 |
'CmapLdapAttr': 'nsCertSubjectDN', |
| 309 |
'issuer': 'default' |
| 310 |
} |
| 311 |
log.info(f"certmaps_default = {certmaps['default']}") |
| 312 |
cm.set(certmaps) |
| 313 |
|
| 314 |
# And set nsslapd-certmap-basedn properly |
| 315 |
config = Config(inst) |
| 316 |
config.replace('nsslapd-certmap-basedn', DEFAULT_SUFFIX) |
| 317 |
|
| 318 |
# Check that EXTERNAL is listed in supported mechns. |
| 319 |
assert(inst.rootdse.supports_sasl_external()) |
| 320 |
|
| 321 |
# Restart to allow certmaps to be re-read |
| 322 |
inst.restart() |
| 323 |
|
| 324 |
# create users |
| 325 |
user1 = create_sasl_user(inst, users, ssca, 'test_user_1000') |
| 326 |
user2 = create_sasl_user(inst, users, ssca, 'test_user_1*') |
| 327 |
|
| 328 |
# Check that subtring searches are working as expected |
| 329 |
# ( This step because 'test_u*_1000' hits |
| 330 |
# https://github.com/389ds/389-ds-base/issues/6203 ) |
| 331 |
for cn,result_len in ( (user1['cn'], 1), (user2['cn'], 2), ('*', 2), ): |
| 332 |
filterstr = f'(nsCertSubjectDN={USER_ISSUER.format(HOSTNAME=cn)})' |
| 333 |
ents = inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, filterstr, escapehatch='i am sure') |
| 334 |
log.info(f'Search with filter {filterstr} returned {len(ents)} entries. {result_len} are expected.') |
| 335 |
assert len(ents) == result_len |
| 336 |
|
| 337 |
# bind with user1 cert (Should be successful) |
| 338 |
sasl_bind_as_user(inst, user1) |
| 339 |
|
| 340 |
# bind with user2 cert (Should be successful) |
| 341 |
sasl_bind_as_user(inst, user1) |
| 342 |
|
| 343 |
# Remove user2 entry (but keep its certificate) |
| 344 |
user2['user'].delete() |
| 345 |
|
| 346 |
# bind with user2 certificate (Should fail) |
| 347 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 348 |
sasl_bind_as_user(inst, user2) |
| 349 |
|
| 350 |
|
| 351 |
def test_sasl_bind_does_not_trigger_lockout(topology_st): |
| 352 |
"""Verify that a failed SASL bind does not trigger account lockout on root DSE |
| 353 |
|
| 354 |
:id: adc4b762-8b9b-4356-8150-05a6ad2e41f4 |
| 355 |
:setup: Standalone instance |
| 356 |
:steps: |
| 357 |
1. Enable account lockout with passwordMaxFailure set to 5 |
| 358 |
2. Perform a SASL DIGEST-MD5 bind with non-existent user credentials |
| 359 |
3. Verify the bind fails with INVALID_CREDENTIALS |
| 360 |
4. Check that passwordRetryCount is NOT set on the root DSE entry |
| 361 |
:expectedresults: |
| 362 |
1. Account lockout should be enabled |
| 363 |
2. SASL bind should be attempted |
| 364 |
3. Bind should fail as expected |
| 365 |
4. Root DSE should NOT have passwordRetryCount attribute |
| 366 |
""" |
| 367 |
inst = topology_st.standalone |
| 368 |
|
| 369 |
log.info("Enable account lockout") |
| 370 |
inst.config.set('passwordLockout', 'on') |
| 371 |
inst.config.set('passwordMaxFailure', '5') |
| 372 |
|
| 373 |
log.info("Perform SASL DIGEST-MD5 bind with non-existent user") |
| 374 |
auth_tokens = ldap.sasl.digest_md5("nonexistent_user", "wrong_password") |
| 375 |
with pytest.raises(ldap.INVALID_CREDENTIALS): |
| 376 |
inst.sasl_interactive_bind_s("", auth_tokens) |
| 377 |
|
| 378 |
log.info("Verify root DSE does not have passwordRetryCount") |
| 379 |
assert not inst.rootdse.present('passwordRetryCount') |
| 380 |
|
| 381 |
log.info("Verified that failed SASL bind does not trigger account lockout on root DSE") |
| 382 |
|
| 383 |
|
| 384 |
if __name__ == '__main__': |
| 385 |
# Run isolated |
| 386 |
# -s for DEBUG mode |
| 387 |
CURRENT_FILE = os.path.realpath(__file__) |
| 388 |
pytest.main("-s %s" % CURRENT_FILE) |