/usr/lib/python2.7/dist-packages/ipaserver/plugins/certmap.py is in python-ipaserver 4.7.0~pre1+git20180411-2ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | # Authors:
# Florence Blanc-Renaud <flo@redhat.com>
#
# Copyright (C) 2017 Red Hat
# see file 'COPYING' for use and warranty information
#
# 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 logging
import dbus
import six
from ipalib import api, errors, x509
from ipalib.crud import Search
from ipalib.frontend import Object
from ipalib.parameters import Bool, DNSNameParam, Flag, Int, Str, Certificate
from ipalib.plugable import Registry
from .baseldap import (
LDAPCreate,
LDAPDelete,
LDAPObject,
LDAPQuery,
LDAPRetrieve,
LDAPSearch,
LDAPUpdate,
pkey_to_value)
from ipalib import _, ngettext
from ipalib import output
if six.PY3:
unicode = str
logger = logging.getLogger(__name__)
__doc__ = _("""
Certificate Identity Mapping
""") + _("""
Manage Certificate Identity Mapping configuration and rules.
""") + _("""
IPA supports the use of certificates for authentication. Certificates can
either be stored in the user entry (full certificate in the usercertificate
attribute), or simply linked to the user entry through a mapping.
This code enables the management of the rules allowing to link a
certificate to a user entry.
""") + _("""
EXAMPLES:
""") + _("""
Display the Certificate Identity Mapping global configuration:
ipa certmapconfig-show
""") + _("""
Modify Certificate Identity Mapping global configuration:
ipa certmapconfig-mod --promptusername=TRUE
""") + _("""
Create a new Certificate Identity Mapping Rule:
ipa certmaprule-add rule1 --desc="Link certificate with subject and issuer"
""") + _("""
Modify a Certificate Identity Mapping Rule:
ipa certmaprule-mod rule1 --maprule="<ALT-SEC-ID-I-S:altSecurityIdentities>"
""") + _("""
Disable a Certificate Identity Mapping Rule:
ipa certmaprule-disable rule1
""") + _("""
Enable a Certificate Identity Mapping Rule:
ipa certmaprule-enable rule1
""") + _("""
Display information about a Certificate Identity Mapping Rule:
ipa certmaprule-show rule1
""") + _("""
Find all Certificate Identity Mapping Rules with the specified domain:
ipa certmaprule-find --domain example.com
""") + _("""
Delete a Certificate Identity Mapping Rule:
ipa certmaprule-del rule1
""")
register = Registry()
def check_associateddomain_is_trusted(api_inst, options):
"""
Check that the associateddomain in options are either IPA domain or
a trusted domain.
:param api_inst: API instance
:param associateddomain: domains to be checked
:raises: ValidationError if the domain is neither IPA domain nor trusted
"""
domains = options.get('associateddomain')
if domains:
trusted_domains = api_inst.Object.config.gather_trusted_domains()
trust_suffix_namespace = {dom_name.lower() for dom_name in
trusted_domains}
trust_suffix_namespace.add(api_inst.env.domain.lower())
for dom in domains:
if not str(dom).lower() in trust_suffix_namespace:
raise errors.ValidationError(
name=_('domain'),
error=_('The domain %s is neither IPA domain nor a trusted'
'domain.') % dom
)
@register()
class certmapconfig(LDAPObject):
"""
Certificate Identity Mapping configuration object
"""
object_name = _('Certificate Identity Mapping configuration options')
default_attributes = ['ipacertmappromptusername']
container_dn = api.env.container_certmap
label = _('Certificate Identity Mapping Global Configuration')
label_singular = _('Certificate Identity Mapping Global Configuration')
takes_params = (
Bool(
'ipacertmappromptusername',
cli_name='promptusername',
label=_('Prompt for the username'),
doc=_('Prompt for the username when multiple identities'
' are mapped to a certificate'),
),
)
permission_filter_objectclasses = ['ipacertmapconfigobject']
managed_permissions = {
'System: Read Certmap Configuration': {
'replaces_global_anonymous_aci': True,
'ipapermbindruletype': 'all',
'ipapermright': {'read', 'search', 'compare'},
'ipapermdefaultattr': {
'ipacertmappromptusername',
'cn',
},
},
'System: Modify Certmap Configuration': {
'replaces_global_anonymous_aci': True,
'ipapermright': {'write'},
'ipapermdefaultattr': {
'ipacertmappromptusername',
},
'default_privileges': {
'Certificate Identity Mapping Administrators'},
},
}
@register()
class certmapconfig_mod(LDAPUpdate):
__doc__ = _('Modify Certificate Identity Mapping configuration.')
@register()
class certmapconfig_show(LDAPRetrieve):
__doc__ = _('Show the current Certificate Identity Mapping configuration.')
@register()
class certmaprule(LDAPObject):
"""
Certificate Identity Mapping Rules
"""
label = _('Certificate Identity Mapping Rules')
label_singular = _('Certificate Identity Mapping Rule')
object_name = _('Certificate Identity Mapping Rule')
object_name_plural = _('Certificate Identity Mapping Rules')
object_class = ['ipacertmaprule']
container_dn = api.env.container_certmaprules
default_attributes = [
'cn', 'description',
'ipacertmapmaprule',
'ipacertmapmatchrule',
'associateddomain',
'ipacertmappriority',
'ipaenabledflag'
]
search_attributes = [
'cn', 'description',
'ipacertmapmaprule',
'ipacertmapmatchrule',
'associateddomain',
'ipacertmappriority',
'ipaenabledflag'
]
takes_params = (
Str(
'cn',
cli_name='rulename',
primary_key=True,
label=_('Rule name'),
doc=_('Certificate Identity Mapping Rule name'),
),
Str(
'description?',
cli_name='desc',
label=_('Description'),
doc=_('Certificate Identity Mapping Rule description'),
),
Str(
'ipacertmapmaprule?',
cli_name='maprule',
label=_('Mapping rule'),
doc=_('Rule used to map the certificate with a user entry'),
),
Str(
'ipacertmapmatchrule?',
cli_name='matchrule',
label=_('Matching rule'),
doc=_('Rule used to check if a certificate can be used for'
' authentication'),
),
DNSNameParam(
'associateddomain*',
cli_name='domain',
label=_('Domain name'),
doc=_('Domain where the user entry will be searched'),
),
Int(
'ipacertmappriority?',
cli_name='priority',
label=_('Priority'),
doc=_('Priority of the rule (higher number means lower priority'),
minvalue=0,
),
Flag(
'ipaenabledflag?',
label=_('Enabled'),
flags=['no_option'],
default=True
),
)
permission_filter_objectclasses = ['ipacertmaprule']
managed_permissions = {
'System: Add Certmap Rules': {
'replaces_global_anonymous_aci': True,
'ipapermright': {'add'},
'default_privileges': {
'Certificate Identity Mapping Administrators'},
},
'System: Read Certmap Rules': {
'replaces_global_anonymous_aci': True,
'ipapermbindruletype': 'all',
'ipapermright': {'read', 'search', 'compare'},
'ipapermdefaultattr': {
'objectclass', 'cn', 'description',
'ipacertmapmaprule', 'ipacertmapmatchrule', 'associateddomain',
'ipacertmappriority', 'ipaenabledflag',
},
},
'System: Delete Certmap Rules': {
'replaces_global_anonymous_aci': True,
'ipapermright': {'delete'},
'default_privileges': {
'Certificate Identity Mapping Administrators'},
},
'System: Modify Certmap Rules': {
'replaces_global_anonymous_aci': True,
'ipapermright': {'write'},
'ipapermdefaultattr': {
'objectclass', 'cn', 'description',
'ipacertmapmaprule', 'ipacertmapmatchrule', 'associateddomain',
'ipacertmappriority', 'ipaenabledflag',
},
'default_privileges': {
'Certificate Identity Mapping Administrators'},
},
}
@register()
class certmaprule_add(LDAPCreate):
__doc__ = _('Create a new Certificate Identity Mapping Rule.')
msg_summary = _('Added Certificate Identity Mapping Rule "%(value)s"')
def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys,
**options):
check_associateddomain_is_trusted(self.api, options)
return dn
@register()
class certmaprule_mod(LDAPUpdate):
__doc__ = _('Modify a Certificate Identity Mapping Rule.')
msg_summary = _('Modified Certificate Identity Mapping Rule "%(value)s"')
def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys,
**options):
check_associateddomain_is_trusted(self.api, options)
return dn
@register()
class certmaprule_find(LDAPSearch):
__doc__ = _('Search for Certificate Identity Mapping Rules.')
msg_summary = ngettext(
'%(count)d Certificate Identity Mapping Rule matched',
'%(count)d Certificate Identity Mapping Rules matched', 0
)
@register()
class certmaprule_show(LDAPRetrieve):
__doc__ = _('Display information about a Certificate Identity Mapping'
' Rule.')
@register()
class certmaprule_del(LDAPDelete):
__doc__ = _('Delete a Certificate Identity Mapping Rule.')
msg_summary = _('Deleted Certificate Identity Mapping Rule "%(value)s"')
@register()
class certmaprule_enable(LDAPQuery):
__doc__ = _('Enable a Certificate Identity Mapping Rule.')
msg_summary = _('Enabled Certificate Identity Mapping Rule "%(value)s"')
has_output = output.standard_value
def execute(self, cn, **options):
ldap = self.obj.backend
dn = self.obj.get_dn(cn)
try:
entry_attrs = ldap.get_entry(dn, ['ipaenabledflag'])
except errors.NotFound:
raise self.obj.handle_not_found(cn)
entry_attrs['ipaenabledflag'] = ['TRUE']
try:
ldap.update_entry(entry_attrs)
except errors.EmptyModlist:
pass
return dict(
result=True,
value=pkey_to_value(cn, options),
)
@register()
class certmaprule_disable(LDAPQuery):
__doc__ = _('Disable a Certificate Identity Mapping Rule.')
msg_summary = _('Disabled Certificate Identity Mapping Rule "%(value)s"')
has_output = output.standard_value
def execute(self, cn, **options):
ldap = self.obj.backend
dn = self.obj.get_dn(cn)
try:
entry_attrs = ldap.get_entry(dn, ['ipaenabledflag'])
except errors.NotFound:
raise self.obj.handle_not_found(cn)
entry_attrs['ipaenabledflag'] = ['FALSE']
try:
ldap.update_entry(entry_attrs)
except errors.EmptyModlist:
pass
return dict(
result=True,
value=pkey_to_value(cn, options),
)
DBUS_SSSD_NAME = 'org.freedesktop.sssd.infopipe'
DBUS_PROPERTY_IF = 'org.freedesktop.DBus.Properties'
DBUS_SSSD_USERS_PATH = '/org/freedesktop/sssd/infopipe/Users'
DBUS_SSSD_USERS_IF = 'org.freedesktop.sssd.infopipe.Users'
DBUS_SSSD_USER_IF = 'org.freedesktop.sssd.infopipe.Users.User'
class _sssd(object):
"""
Auxiliary class for SSSD infopipe DBus.
"""
def __init__(self):
"""
Initialize the Users object and interface.
:raise RemoteRetrieveError: if DBus error occurs
"""
try:
self._bus = dbus.SystemBus()
self._users_obj = self._bus.get_object(
DBUS_SSSD_NAME, DBUS_SSSD_USERS_PATH)
self._users_iface = dbus.Interface(
self._users_obj, DBUS_SSSD_USERS_IF)
except dbus.DBusException as e:
logger.error(
'Failed to initialize DBus interface %s. DBus '
'exception is %s.', DBUS_SSSD_USERS_IF, e
)
raise errors.RemoteRetrieveError(
reason=_('Failed to connect to sssd over SystemBus. '
'See details in the error_log'))
def list_users_by_cert(self, cert):
"""
Look for users matching the cert.
Call Users.ListByCertificate interface and return a dict
with key = domain, value = list of uids
corresponding to the users matching the provided cert
:param cert: DER cert
:raise RemoteRetrieveError: if DBus error occurs
"""
try:
cert_obj = x509.load_der_x509_certificate(cert)
# bug 3306 in sssd returns 0 entry when max_entries = 0
# Temp workaround is to use a non-null value, not too high
# to avoid reserving unneeded memory
max_entries = dbus.UInt32(100)
user_paths = self._users_iface.ListByCertificate(
cert_obj.public_bytes(x509.Encoding.PEM),
max_entries)
users = dict()
for user_path in user_paths:
user_obj = self._bus.get_object(DBUS_SSSD_NAME, user_path)
user_iface = dbus.Interface(user_obj, DBUS_PROPERTY_IF)
user_login = user_iface.Get(DBUS_SSSD_USER_IF, 'name')
# Extract name@domain
items = user_login.split('@')
domain = api.env.realm if len(items) < 2 else items[1]
name = items[0]
# Retrieve the list of users for the given domain,
# or initialize to an empty list
# and add the name
users_for_dom = users.setdefault(domain, list())
users_for_dom.append(name)
return users
except dbus.DBusException as e:
err_name = e.get_dbus_name()
# If there is no matching user, do not consider this as an
# exception and return an empty list
if err_name == 'org.freedesktop.sssd.Error.NotFound':
return dict()
logger.error(
'Failed to use interface %s. DBus '
'exception is %s.', DBUS_SSSD_USERS_IF, e)
raise errors.RemoteRetrieveError(
reason=_('Failed to find users over SystemBus. '
' See details in the error_log'))
@register()
class certmap(Object):
"""
virtual object for certmatch_map API
"""
takes_params = (
DNSNameParam(
'domain',
label=_('Domain'),
flags={'no_search'},
),
Str(
'uid*',
label=_('User logins'),
flags={'no_search'},
),
)
@register()
class certmap_match(Search):
__doc__ = _("""
Search for users matching the provided certificate.
This command relies on SSSD to retrieve the list of matching users and
may return cached data. For more information on purging SSSD cache,
please refer to sss_cache documentation.
""")
msg_summary = ngettext('%(count)s user matched',
'%(count)s users matched', 0)
def get_summary_default(self, output):
"""
Need to sum the numbre of matching users for each domain.
"""
count = sum(len(entry['uid']) for entry in output['result'])
return self.msg_summary % dict(count=count)
def get_args(self):
for arg in super(certmap_match, self).get_args():
if arg.name == 'criteria':
continue
yield arg
yield Certificate(
'certificate',
cli_name='certificate',
label=_('Certificate'),
doc=_('Base-64 encoded user certificate'),
flags=['virtual_attribute']
)
def execute(self, *args, **options):
"""
Search for users matching the provided certificate.
The search is performed using SSSD's DBus interface
Users.ListByCertificate.
SSSD does the lookup based on certificate mapping rules, using
FreeIPA domain and trusted domains.
:raise RemoteRetrieveError: if DBus returns an exception
"""
sssd = _sssd()
cert = args[0]
users = sssd.list_users_by_cert(cert)
result = [{'domain': domain, 'uid': userlist}
for (domain, userlist) in users.items()]
count = len(result)
return dict(
result=result,
count=count,
truncated=False,
)
|