summaryrefslogtreecommitdiffstats
path: root/src/tests/intg/test_pysss_nss_idmap.py
blob: fd714203f8e5a6140447b39d1350da1f51e1c0f3 (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
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
#
# LDAP integration test
#
# Copyright (c) 2017 Red Hat, Inc.
# Author: Lukas Slebodnik <lslebodn@redhat.com>
#
# 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/>.
#
import os
import stat
import pwd
import grp
import signal
import subprocess
import time
import pytest
import ldb
import pysss_nss_idmap

import config
import ds_openldap

from .util import unindent

LDAP_BASE_DN = "dc=example,dc=com"


@pytest.fixture(scope="module")
def ad_inst(request):
    """Fake AD server instance fixture"""
    instance = ds_openldap.FakeAD(
        config.PREFIX, 10389, LDAP_BASE_DN,
        "cn=admin", "Secret123"
    )

    try:
        instance.setup()
    except Exception:
        instance.teardown()
        raise
    request.addfinalizer(instance.teardown)
    return instance


@pytest.fixture(scope="module")
def ldap_conn(request, ad_inst):
    """LDAP server connection fixture"""
    ldap_conn = ad_inst.bind()
    ldap_conn.ad_inst = ad_inst
    request.addfinalizer(ldap_conn.unbind_s)
    return ldap_conn


def format_basic_conf(ldap_conn, ignore_unreadable_refs):
    """Format a basic SSSD configuration"""

    ignore_unreadable_refs_conf = "false"
    if ignore_unreadable_refs:
        ignore_unreadable_refs_conf = "true"

    return unindent("""\
        [sssd]
        domains = FakeAD
        services = nss

        [nss]

        [pam]

        [domain/FakeAD]
        ldap_search_base = {ldap_conn.ad_inst.base_dn}
        ldap_referrals = false

        id_provider = ldap
        auth_provider = ldap
        chpass_provider = ldap
        access_provider = ldap

        ldap_uri = {ldap_conn.ad_inst.ldap_url}
        ldap_default_bind_dn = {ldap_conn.ad_inst.admin_dn}
        ldap_default_authtok_type = password
        ldap_default_authtok = {ldap_conn.ad_inst.admin_pw}

        ldap_schema = ad
        ldap_id_mapping = true
        ldap_idmap_default_domain_sid = S-1-5-21-1305200397-2901131868-73388776
        case_sensitive = False

        ldap_ignore_unreadable_references = {ignore_unreadable_refs_conf}
    """).format(**locals())


def create_conf_file(contents):
    """Create sssd.conf with specified contents"""
    conf = open(config.CONF_PATH, "w")
    conf.write(contents)
    conf.close()
    os.chmod(config.CONF_PATH, stat.S_IRUSR | stat.S_IWUSR)


def create_conf_fixture(request, contents):
    """
    Create sssd.conf with specified contents and add teardown for removing it
    """
    create_conf_file(contents)

    def cleanup_conf_file():
        """Remove sssd.conf, if it exists"""
        if os.path.lexists(config.CONF_PATH):
            os.unlink(config.CONF_PATH)

    request.addfinalizer(cleanup_conf_file)


def create_sssd_process():
    """Start the SSSD process"""
    if subprocess.call(["sssd", "-D", "--logger=files"]) != 0:
        raise Exception("sssd start failed")


def cleanup_sssd_process():
    """Stop the SSSD process and remove its state"""
    try:
        with open(config.PIDFILE_PATH, "r") as pid_file:
            pid = int(pid_file.read())
        os.kill(pid, signal.SIGTERM)
        while True:
            try:
                os.kill(pid, signal.SIGCONT)
            except OSError:
                break
            time.sleep(1)
    except OSError:
        pass
    for path in os.listdir(config.DB_PATH):
        os.unlink(config.DB_PATH + "/" + path)
    for path in os.listdir(config.MCACHE_PATH):
        os.unlink(config.MCACHE_PATH + "/" + path)


def create_sssd_fixture(request):
    """Start SSSD and add teardown for stopping it and removing its state"""
    create_sssd_process()
    request.addfinalizer(cleanup_sssd_process)


def sysdb_sed_domainid(domain_name, domain_id):
    sssd_cache = "{0}/cache_{1}.ldb".format(config.DB_PATH, domain_name)
    domain_ldb = ldb.Ldb(sssd_cache)

    msg = ldb.Message()
    msg.dn = ldb.Dn(domain_ldb, "cn=sysdb")
    msg["cn"] = "sysdb"
    msg["description"] = "base object"
    msg["version"] = "0.17"
    domain_ldb.add(msg)

    # Set domainID for fake AD domain
    msg = ldb.Message()
    msg.dn = ldb.Dn(domain_ldb, "cn={0},cn=sysdb".format(domain_name))
    msg["cn"] = domain_name
    msg["domainID"] = domain_id
    msg["distinguishedName"] = "cn={0},cn=sysdb".format(domain_name)
    domain_ldb.add(msg)

    msg = ldb.Message()
    msg.dn = ldb.Dn(domain_ldb, "@ATTRIBUTES")
    msg["distinguishedName"] = "@ATTRIBUTES"
    for attr in ['cn', 'dc', 'dn', 'objectclass', 'originalDN',
                 'userPrincipalName']:
        msg[attr] = "CASE_INSENSITIVE"
    domain_ldb.add(msg)

    msg = ldb.Message()
    msg.dn = ldb.Dn(domain_ldb, "@INDEXLIST")
    msg["distinguishedName"] = "@INDEXLIST"
    msg["@IDXONE"] = "1"
    for attr in ['cn', 'objectclass', 'member', 'memberof', 'name',
                 'uidNumber', 'gidNumber', 'lastUpdate', 'dataExpireTimestamp',
                 'originalDN', 'nameAlias', 'servicePort', 'serviceProtocol',
                 'sudoUser', 'sshKnownHostsExpire', 'objectSIDString']:
        msg["@IDXATTR"] = attr
    domain_ldb.add(msg)

    msg = ldb.Message()
    msg.dn = ldb.Dn(domain_ldb, "@MODULES")
    msg["distinguishedName"] = "@MODULES"
    msg["@LIST"] = "asq,memberof"
    domain_ldb.add(msg)


@pytest.fixture
def simple_ad(request, ldap_conn):
    conf = format_basic_conf(ldap_conn, ignore_unreadable_refs=False)
    sysdb_sed_domainid("FakeAD", "S-1-5-21-1305200397-2901131868-73388776")

    create_conf_fixture(request, conf)
    create_sssd_fixture(request)
    return None


def test_user_operations(ldap_conn, simple_ad):
    user = 'user1_dom1-19661'
    user_id = pwd.getpwnam(user).pw_uid
    user_sid = 'S-1-5-21-1305200397-2901131868-73388776-82809'

    output = pysss_nss_idmap.getsidbyname(user)[user]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
    assert output[pysss_nss_idmap.SID_KEY] == user_sid

    output = pysss_nss_idmap.getsidbyusername(user)[user]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
    assert output[pysss_nss_idmap.SID_KEY] == user_sid

    output = pysss_nss_idmap.getsidbygroupname(user)
    assert len(output) == 0

    output = pysss_nss_idmap.getsidbyid(user_id)[user_id]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
    assert output[pysss_nss_idmap.SID_KEY] == user_sid

    output = pysss_nss_idmap.getsidbyuid(user_id)[user_id]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
    assert output[pysss_nss_idmap.SID_KEY] == user_sid

    output = pysss_nss_idmap.getsidbygid(user_id)
    assert len(output) == 0

    output = pysss_nss_idmap.getidbysid(user_sid)[user_sid]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
    assert output[pysss_nss_idmap.ID_KEY] == user_id

    output = pysss_nss_idmap.getnamebysid(user_sid)[user_sid]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_USER
    assert output[pysss_nss_idmap.NAME_KEY] == user


def test_group_operations(ldap_conn, simple_ad):
    group = 'group1_dom1-19661'
    group_id = grp.getgrnam(group).gr_gid
    group_sid = 'S-1-5-21-1305200397-2901131868-73388776-82810'

    output = pysss_nss_idmap.getsidbyname(group)[group]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbygroupname(group)[group]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbyusername(group)
    assert len(output) == 0

    output = pysss_nss_idmap.getsidbyid(group_id)[group_id]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbygid(group_id)[group_id]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbyuid(group_id)
    assert len(output) == 0

    output = pysss_nss_idmap.getidbysid(group_sid)[group_sid]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.ID_KEY] == group_id

    output = pysss_nss_idmap.getnamebysid(group_sid)[group_sid]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.NAME_KEY] == group


def test_case_insensitive(ldap_conn, simple_ad):
    # resolve group and also member of this group
    group = 'Domain Users'
    group_id = grp.getgrnam(group).gr_gid
    group_sid = 'S-1-5-21-1305200397-2901131868-73388776-513'

    output = pysss_nss_idmap.getsidbyname(group)[group]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbyid(group_id)[group_id]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbygid(group_id)[group_id]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbyuid(group_id)
    assert len(output) == 0

    output = pysss_nss_idmap.getidbysid(group_sid)[group_sid]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.ID_KEY] == group_id

    output = pysss_nss_idmap.getnamebysid(group_sid)[group_sid]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.NAME_KEY] == group.lower()


@pytest.fixture
def simple_ad_ignore_unrdbl_refs(request, ldap_conn):
    conf = format_basic_conf(ldap_conn, ignore_unreadable_refs=True)
    sysdb_sed_domainid("FakeAD", "S-1-5-21-1305200397-2901131868-73388776")

    create_conf_fixture(request, conf)
    create_sssd_fixture(request)
    return None


def test_ignore_unreadable_references(ldap_conn, simple_ad_ignore_unrdbl_refs):
    group = 'group3_dom1-17775'
    group_id = grp.getgrnam(group).gr_gid
    group_sid = 'S-1-5-21-1305200397-2901131868-73388776-82764'

    output = pysss_nss_idmap.getsidbyname(group)[group]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbyid(group_id)[group_id]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbygid(group_id)[group_id]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.SID_KEY] == group_sid

    output = pysss_nss_idmap.getsidbyuid(group_id)
    assert len(output) == 0

    output = pysss_nss_idmap.getidbysid(group_sid)[group_sid]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.ID_KEY] == group_id

    output = pysss_nss_idmap.getnamebysid(group_sid)[group_sid]
    assert output[pysss_nss_idmap.TYPE_KEY] == pysss_nss_idmap.ID_GROUP
    assert output[pysss_nss_idmap.NAME_KEY] == group


def test_no_ignore_unreadable_references(ldap_conn, simple_ad):
    group = 'group3_dom1-17775'

    # This group has a member attribute referencing to a user in other
    # domain
    with pytest.raises(KeyError):
        grp.getgrnam(group)