2013-10-01 13:26:38 -05:00
|
|
|
# Authors:
|
|
|
|
# Nathaniel McCallum <npmccallum@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 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/>.
|
|
|
|
|
2016-04-20 08:41:34 -05:00
|
|
|
from .baseldap import LDAPObject, LDAPAddMember, LDAPRemoveMember
|
|
|
|
from .baseldap import LDAPCreate, LDAPDelete, LDAPUpdate, LDAPSearch, LDAPRetrieve
|
2016-04-28 02:46:03 -05:00
|
|
|
from ipalib import api, Int, Str, Bool, DateTime, Flag, Bytes, IntEnum, StrEnum, _, ngettext
|
2013-10-01 13:26:38 -05:00
|
|
|
from ipalib.plugable import Registry
|
2015-12-16 09:06:03 -06:00
|
|
|
from ipalib.errors import (
|
|
|
|
PasswordMismatch,
|
|
|
|
ConversionError,
|
|
|
|
NotFound,
|
|
|
|
ValidationError)
|
2013-10-01 13:26:38 -05:00
|
|
|
from ipalib.request import context
|
2016-04-13 08:50:52 -05:00
|
|
|
from ipapython.dn import DN
|
2014-06-26 09:09:00 -05:00
|
|
|
|
2013-10-01 13:26:38 -05:00
|
|
|
import base64
|
2018-09-27 00:47:07 -05:00
|
|
|
import urllib
|
2013-10-01 13:26:38 -05:00
|
|
|
import uuid
|
2014-06-19 11:30:23 -05:00
|
|
|
import os
|
2013-10-01 13:26:38 -05:00
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
import six
|
|
|
|
|
|
|
|
if six.PY3:
|
|
|
|
unicode = str
|
|
|
|
|
2013-10-01 13:26:38 -05:00
|
|
|
__doc__ = _("""
|
|
|
|
OTP Tokens
|
2014-06-09 13:10:19 -05:00
|
|
|
""") + _("""
|
2013-10-01 13:26:38 -05:00
|
|
|
Manage OTP tokens.
|
2014-06-09 13:10:19 -05:00
|
|
|
""") + _("""
|
2013-10-01 13:26:38 -05:00
|
|
|
IPA supports the use of OTP tokens for multi-factor authentication. This
|
|
|
|
code enables the management of OTP tokens.
|
2014-06-09 13:10:19 -05:00
|
|
|
""") + _("""
|
2013-10-01 13:26:38 -05:00
|
|
|
EXAMPLES:
|
2014-06-09 13:10:19 -05:00
|
|
|
""") + _("""
|
2013-10-01 13:26:38 -05:00
|
|
|
Add a new token:
|
2014-05-02 12:22:15 -05:00
|
|
|
ipa otptoken-add --type=totp --owner=jdoe --desc="My soft token"
|
2014-06-09 13:10:19 -05:00
|
|
|
""") + _("""
|
2013-10-01 13:26:38 -05:00
|
|
|
Examine the token:
|
2014-05-02 12:22:15 -05:00
|
|
|
ipa otptoken-show a93db710-a31a-4639-8647-f15b2c70b78a
|
2014-06-09 13:10:19 -05:00
|
|
|
""") + _("""
|
2013-10-01 13:26:38 -05:00
|
|
|
Change the vendor:
|
2014-05-02 12:22:15 -05:00
|
|
|
ipa otptoken-mod a93db710-a31a-4639-8647-f15b2c70b78a --vendor="Red Hat"
|
2014-06-09 13:10:19 -05:00
|
|
|
""") + _("""
|
2013-10-01 13:26:38 -05:00
|
|
|
Delete a token:
|
2014-05-02 12:22:15 -05:00
|
|
|
ipa otptoken-del a93db710-a31a-4639-8647-f15b2c70b78a
|
2013-10-01 13:26:38 -05:00
|
|
|
""")
|
|
|
|
|
|
|
|
register = Registry()
|
|
|
|
|
2016-05-30 23:36:55 -05:00
|
|
|
topic = 'otp'
|
2014-12-02 13:43:27 -06:00
|
|
|
|
2014-02-20 12:21:32 -06:00
|
|
|
TOKEN_TYPES = {
|
|
|
|
u'totp': ['ipatokentotpclockoffset', 'ipatokentotptimestep'],
|
|
|
|
u'hotp': ['ipatokenhotpcounter']
|
|
|
|
}
|
2013-10-01 13:26:38 -05:00
|
|
|
|
|
|
|
# NOTE: For maximum compatibility, KEY_LENGTH % 5 == 0
|
2018-02-22 13:04:10 -06:00
|
|
|
KEY_LENGTH = 35
|
2013-10-01 13:26:38 -05:00
|
|
|
|
|
|
|
class OTPTokenKey(Bytes):
|
|
|
|
"""A binary password type specified in base32."""
|
|
|
|
|
|
|
|
password = True
|
|
|
|
|
|
|
|
def _convert_scalar(self, value, index=None):
|
|
|
|
if isinstance(value, (tuple, list)) and len(value) == 2:
|
|
|
|
(p1, p2) = value
|
|
|
|
if p1 != p2:
|
2016-05-23 06:20:27 -05:00
|
|
|
raise PasswordMismatch(name=self.name)
|
2013-10-01 13:26:38 -05:00
|
|
|
value = p1
|
|
|
|
|
|
|
|
if isinstance(value, unicode):
|
|
|
|
try:
|
|
|
|
value = base64.b32decode(value, True)
|
2015-07-30 09:49:29 -05:00
|
|
|
except TypeError as e:
|
2016-05-23 06:20:27 -05:00
|
|
|
raise ConversionError(name=self.name, error=str(e))
|
2013-10-01 13:26:38 -05:00
|
|
|
|
2016-05-23 06:20:27 -05:00
|
|
|
return super(OTPTokenKey, self)._convert_scalar(value)
|
2013-10-01 13:26:38 -05:00
|
|
|
|
|
|
|
def _convert_owner(userobj, entry_attrs, options):
|
|
|
|
if 'ipatokenowner' in entry_attrs and not options.get('raw', False):
|
2015-08-12 05:25:30 -05:00
|
|
|
entry_attrs['ipatokenowner'] = [userobj.get_primary_key_from_dn(o)
|
|
|
|
for o in entry_attrs['ipatokenowner']]
|
2013-10-01 13:26:38 -05:00
|
|
|
|
2018-01-03 05:11:15 -06:00
|
|
|
|
2013-10-01 13:26:38 -05:00
|
|
|
def _normalize_owner(userobj, entry_attrs):
|
|
|
|
owner = entry_attrs.get('ipatokenowner', None)
|
2014-10-24 15:16:50 -05:00
|
|
|
if owner:
|
|
|
|
try:
|
2018-01-03 05:11:15 -06:00
|
|
|
entry_attrs['ipatokenowner'] = userobj._normalize_manager(
|
|
|
|
owner
|
|
|
|
)[0]
|
2014-10-24 15:16:50 -05:00
|
|
|
except NotFound:
|
2018-01-03 05:11:15 -06:00
|
|
|
raise userobj.handle_not_found(owner)
|
|
|
|
|
2013-10-01 13:26:38 -05:00
|
|
|
|
2014-07-29 08:45:21 -05:00
|
|
|
def _check_interval(not_before, not_after):
|
|
|
|
if not_before and not_after:
|
|
|
|
return not_before <= not_after
|
|
|
|
return True
|
|
|
|
|
2018-01-03 05:11:15 -06:00
|
|
|
|
2014-10-15 11:24:56 -05:00
|
|
|
def _set_token_type(entry_attrs, **options):
|
|
|
|
klasses = [x.lower() for x in entry_attrs.get('objectclass', [])]
|
2016-10-11 10:35:01 -05:00
|
|
|
for ttype in TOKEN_TYPES:
|
2014-10-15 11:24:56 -05:00
|
|
|
cls = 'ipatoken' + ttype
|
|
|
|
if cls.lower() in klasses:
|
|
|
|
entry_attrs['type'] = ttype.upper()
|
|
|
|
|
|
|
|
if not options.get('all', False) or options.get('pkey_only', False):
|
|
|
|
entry_attrs.pop('objectclass', None)
|
2013-10-01 13:26:38 -05:00
|
|
|
|
2018-01-03 05:11:15 -06:00
|
|
|
|
2013-10-01 13:26:38 -05:00
|
|
|
@register()
|
|
|
|
class otptoken(LDAPObject):
|
|
|
|
"""
|
|
|
|
OTP Token object.
|
|
|
|
"""
|
|
|
|
container_dn = api.env.container_otp
|
2014-02-10 10:39:53 -06:00
|
|
|
object_name = _('OTP token')
|
2013-10-01 13:26:38 -05:00
|
|
|
object_name_plural = _('OTP tokens')
|
|
|
|
object_class = ['ipatoken']
|
2014-01-28 16:11:04 -06:00
|
|
|
possible_objectclasses = ['ipatokentotp', 'ipatokenhotp']
|
2013-10-01 13:26:38 -05:00
|
|
|
default_attributes = [
|
|
|
|
'ipatokenuniqueid', 'description', 'ipatokenowner',
|
|
|
|
'ipatokendisabled', 'ipatokennotbefore', 'ipatokennotafter',
|
Add support for managedBy to tokens
This also constitutes a rethinking of the token ACIs after the introduction
of SELFDN support.
Admins, as before, have full access to all token permissions.
Normal users have read/search/compare access to all of the non-secret data
for tokens assigned to them, whether managed by them or not. Users can add
tokens if, and only if, they will also manage this token.
Managers can also read/search/compare tokens they manage. Additionally,
they can write non-secret data to their managed tokens and delete them.
When a normal user self-creates a token (the default behavior), then
managedBy is automatically set. When an admin creates a token for another
user (or no owner is assigned at all), then managed by is not set. In this
second case, the token is effectively read-only for the assigned owner.
This behavior enables two important other behaviors. First, an admin can
create a hardware token and assign it to the user as a read-only token.
Second, when the user is deleted, only his self-managed tokens are deleted.
All other (read-only) tokens are instead orphaned. This permits the same
token object to be reasigned to another user without loss of any counter
data.
https://fedorahosted.org/freeipa/ticket/4228
https://fedorahosted.org/freeipa/ticket/4259
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-02 15:44:30 -05:00
|
|
|
'ipatokenvendor', 'ipatokenmodel', 'ipatokenserial', 'managedby'
|
2013-10-01 13:26:38 -05:00
|
|
|
]
|
Add support for managedBy to tokens
This also constitutes a rethinking of the token ACIs after the introduction
of SELFDN support.
Admins, as before, have full access to all token permissions.
Normal users have read/search/compare access to all of the non-secret data
for tokens assigned to them, whether managed by them or not. Users can add
tokens if, and only if, they will also manage this token.
Managers can also read/search/compare tokens they manage. Additionally,
they can write non-secret data to their managed tokens and delete them.
When a normal user self-creates a token (the default behavior), then
managedBy is automatically set. When an admin creates a token for another
user (or no owner is assigned at all), then managed by is not set. In this
second case, the token is effectively read-only for the assigned owner.
This behavior enables two important other behaviors. First, an admin can
create a hardware token and assign it to the user as a read-only token.
Second, when the user is deleted, only his self-managed tokens are deleted.
All other (read-only) tokens are instead orphaned. This permits the same
token object to be reasigned to another user without loss of any counter
data.
https://fedorahosted.org/freeipa/ticket/4228
https://fedorahosted.org/freeipa/ticket/4259
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-02 15:44:30 -05:00
|
|
|
attribute_members = {
|
|
|
|
'managedby': ['user'],
|
|
|
|
}
|
|
|
|
relationships = {
|
|
|
|
'managedby': ('Managed by', 'man_by_', 'not_man_by_'),
|
|
|
|
}
|
2017-03-27 01:18:29 -05:00
|
|
|
allow_rename = True
|
2013-10-01 13:26:38 -05:00
|
|
|
|
2014-02-10 10:39:53 -06:00
|
|
|
label = _('OTP Tokens')
|
|
|
|
label_singular = _('OTP Token')
|
2013-10-01 13:26:38 -05:00
|
|
|
|
|
|
|
takes_params = (
|
|
|
|
Str('ipatokenuniqueid',
|
|
|
|
cli_name='id',
|
|
|
|
label=_('Unique ID'),
|
|
|
|
primary_key=True,
|
|
|
|
flags=('optional_create'),
|
|
|
|
),
|
|
|
|
StrEnum('type?',
|
|
|
|
label=_('Type'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Type of the token'),
|
2014-02-20 12:21:32 -06:00
|
|
|
default=u'totp',
|
|
|
|
autofill=True,
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
values=tuple(list(TOKEN_TYPES) + [x.upper() for x in TOKEN_TYPES]),
|
2013-10-01 13:26:38 -05:00
|
|
|
flags=('virtual_attribute', 'no_update'),
|
|
|
|
),
|
|
|
|
Str('description?',
|
|
|
|
cli_name='desc',
|
|
|
|
label=_('Description'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Token description (informational only)'),
|
2013-10-01 13:26:38 -05:00
|
|
|
),
|
|
|
|
Str('ipatokenowner?',
|
|
|
|
cli_name='owner',
|
|
|
|
label=_('Owner'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Assigned user of the token (default: self)'),
|
2013-10-01 13:26:38 -05:00
|
|
|
),
|
Add support for managedBy to tokens
This also constitutes a rethinking of the token ACIs after the introduction
of SELFDN support.
Admins, as before, have full access to all token permissions.
Normal users have read/search/compare access to all of the non-secret data
for tokens assigned to them, whether managed by them or not. Users can add
tokens if, and only if, they will also manage this token.
Managers can also read/search/compare tokens they manage. Additionally,
they can write non-secret data to their managed tokens and delete them.
When a normal user self-creates a token (the default behavior), then
managedBy is automatically set. When an admin creates a token for another
user (or no owner is assigned at all), then managed by is not set. In this
second case, the token is effectively read-only for the assigned owner.
This behavior enables two important other behaviors. First, an admin can
create a hardware token and assign it to the user as a read-only token.
Second, when the user is deleted, only his self-managed tokens are deleted.
All other (read-only) tokens are instead orphaned. This permits the same
token object to be reasigned to another user without loss of any counter
data.
https://fedorahosted.org/freeipa/ticket/4228
https://fedorahosted.org/freeipa/ticket/4259
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-02 15:44:30 -05:00
|
|
|
Str('managedby_user?',
|
|
|
|
label=_('Manager'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Assigned manager of the token (default: self)'),
|
Add support for managedBy to tokens
This also constitutes a rethinking of the token ACIs after the introduction
of SELFDN support.
Admins, as before, have full access to all token permissions.
Normal users have read/search/compare access to all of the non-secret data
for tokens assigned to them, whether managed by them or not. Users can add
tokens if, and only if, they will also manage this token.
Managers can also read/search/compare tokens they manage. Additionally,
they can write non-secret data to their managed tokens and delete them.
When a normal user self-creates a token (the default behavior), then
managedBy is automatically set. When an admin creates a token for another
user (or no owner is assigned at all), then managed by is not set. In this
second case, the token is effectively read-only for the assigned owner.
This behavior enables two important other behaviors. First, an admin can
create a hardware token and assign it to the user as a read-only token.
Second, when the user is deleted, only his self-managed tokens are deleted.
All other (read-only) tokens are instead orphaned. This permits the same
token object to be reasigned to another user without loss of any counter
data.
https://fedorahosted.org/freeipa/ticket/4228
https://fedorahosted.org/freeipa/ticket/4259
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-02 15:44:30 -05:00
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
2013-10-01 13:26:38 -05:00
|
|
|
Bool('ipatokendisabled?',
|
|
|
|
cli_name='disabled',
|
2014-11-06 14:19:01 -06:00
|
|
|
label=_('Disabled'),
|
|
|
|
doc=_('Mark the token as disabled (default: false)')
|
2013-10-01 13:26:38 -05:00
|
|
|
),
|
2014-01-09 04:29:39 -06:00
|
|
|
DateTime('ipatokennotbefore?',
|
2013-10-01 13:26:38 -05:00
|
|
|
cli_name='not_before',
|
|
|
|
label=_('Validity start'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('First date/time the token can be used'),
|
2013-10-01 13:26:38 -05:00
|
|
|
),
|
2014-01-09 04:29:39 -06:00
|
|
|
DateTime('ipatokennotafter?',
|
2013-10-01 13:26:38 -05:00
|
|
|
cli_name='not_after',
|
|
|
|
label=_('Validity end'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Last date/time the token can be used'),
|
2013-10-01 13:26:38 -05:00
|
|
|
),
|
|
|
|
Str('ipatokenvendor?',
|
|
|
|
cli_name='vendor',
|
|
|
|
label=_('Vendor'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Token vendor name (informational only)'),
|
2013-10-01 13:26:38 -05:00
|
|
|
),
|
|
|
|
Str('ipatokenmodel?',
|
|
|
|
cli_name='model',
|
|
|
|
label=_('Model'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Token model (informational only)'),
|
2013-10-01 13:26:38 -05:00
|
|
|
),
|
|
|
|
Str('ipatokenserial?',
|
|
|
|
cli_name='serial',
|
|
|
|
label=_('Serial'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Token serial (informational only)'),
|
2013-10-01 13:26:38 -05:00
|
|
|
),
|
|
|
|
OTPTokenKey('ipatokenotpkey?',
|
|
|
|
cli_name='key',
|
|
|
|
label=_('Key'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Token secret (Base32; default: random)'),
|
2014-06-19 11:30:23 -05:00
|
|
|
default_from=lambda: os.urandom(KEY_LENGTH),
|
2014-02-20 12:21:32 -06:00
|
|
|
autofill=True,
|
2016-08-25 04:53:39 -05:00
|
|
|
# force server-side conversion
|
|
|
|
normalizer=lambda x: x,
|
2013-10-01 13:26:38 -05:00
|
|
|
flags=('no_display', 'no_update', 'no_search'),
|
|
|
|
),
|
|
|
|
StrEnum('ipatokenotpalgorithm?',
|
|
|
|
cli_name='algo',
|
|
|
|
label=_('Algorithm'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Token hash algorithm'),
|
2014-02-20 12:21:32 -06:00
|
|
|
default=u'sha1',
|
|
|
|
autofill=True,
|
2013-10-01 13:26:38 -05:00
|
|
|
flags=('no_update'),
|
|
|
|
values=(u'sha1', u'sha256', u'sha384', u'sha512'),
|
|
|
|
),
|
|
|
|
IntEnum('ipatokenotpdigits?',
|
|
|
|
cli_name='digits',
|
2014-06-19 11:28:32 -05:00
|
|
|
label=_('Digits'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Number of digits each token code will have'),
|
2013-10-01 13:26:38 -05:00
|
|
|
values=(6, 8),
|
2014-02-20 12:21:32 -06:00
|
|
|
default=6,
|
|
|
|
autofill=True,
|
2013-10-01 13:26:38 -05:00
|
|
|
flags=('no_update'),
|
|
|
|
),
|
|
|
|
Int('ipatokentotpclockoffset?',
|
|
|
|
cli_name='offset',
|
|
|
|
label=_('Clock offset'),
|
2021-01-19 14:35:41 -06:00
|
|
|
doc=_('TOTP token / IPA server time difference'),
|
2014-02-20 12:21:32 -06:00
|
|
|
default=0,
|
|
|
|
autofill=True,
|
2013-10-01 13:26:38 -05:00
|
|
|
flags=('no_update'),
|
|
|
|
),
|
|
|
|
Int('ipatokentotptimestep?',
|
|
|
|
cli_name='interval',
|
|
|
|
label=_('Clock interval'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Length of TOTP token code validity'),
|
2014-02-20 12:21:32 -06:00
|
|
|
default=30,
|
|
|
|
autofill=True,
|
2013-10-01 13:26:38 -05:00
|
|
|
minvalue=5,
|
|
|
|
flags=('no_update'),
|
|
|
|
),
|
2014-01-28 16:11:04 -06:00
|
|
|
Int('ipatokenhotpcounter?',
|
|
|
|
cli_name='counter',
|
|
|
|
label=_('Counter'),
|
2014-11-06 14:19:01 -06:00
|
|
|
doc=_('Initial counter for the HOTP token'),
|
2014-02-20 12:21:32 -06:00
|
|
|
default=0,
|
|
|
|
autofill=True,
|
2014-01-28 16:11:04 -06:00
|
|
|
minvalue=0,
|
|
|
|
flags=('no_update'),
|
|
|
|
),
|
2016-06-29 23:37:16 -05:00
|
|
|
Str('uri?',
|
|
|
|
label=_('URI'),
|
|
|
|
flags={'virtual_attribute', 'no_create', 'no_update', 'no_search'},
|
|
|
|
),
|
2013-10-01 13:26:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@register()
|
|
|
|
class otptoken_add(LDAPCreate):
|
|
|
|
__doc__ = _('Add a new OTP token.')
|
|
|
|
msg_summary = _('Added OTP token "%(value)s"')
|
|
|
|
|
|
|
|
takes_options = LDAPCreate.takes_options + (
|
2014-11-06 14:30:13 -06:00
|
|
|
Flag('qrcode?', label=_('(deprecated)'), flags=('no_option')),
|
|
|
|
Flag('no_qrcode', label=_('Do not display QR code'), default=False),
|
2013-10-01 13:26:38 -05:00
|
|
|
)
|
|
|
|
|
2016-05-19 07:19:07 -05:00
|
|
|
def execute(self, ipatokenuniqueid=None, **options):
|
|
|
|
return super(otptoken_add, self).execute(ipatokenuniqueid, **options)
|
|
|
|
|
2013-10-01 13:26:38 -05:00
|
|
|
def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
|
2014-05-01 15:31:45 -05:00
|
|
|
# Fill in a default UUID when not specified.
|
|
|
|
if entry_attrs.get('ipatokenuniqueid', None) is None:
|
|
|
|
entry_attrs['ipatokenuniqueid'] = str(uuid.uuid4())
|
|
|
|
dn = DN("ipatokenuniqueid=%s" % entry_attrs['ipatokenuniqueid'], dn)
|
|
|
|
|
2014-07-29 08:45:21 -05:00
|
|
|
if not _check_interval(options.get('ipatokennotbefore', None),
|
|
|
|
options.get('ipatokennotafter', None)):
|
|
|
|
raise ValidationError(name='not_after',
|
|
|
|
error='is before the validity start')
|
|
|
|
|
2014-01-28 16:11:04 -06:00
|
|
|
# Set the object class and defaults for specific token types
|
2014-10-15 11:24:56 -05:00
|
|
|
options['type'] = options['type'].lower()
|
2014-02-20 12:21:32 -06:00
|
|
|
entry_attrs['objectclass'] = otptoken.object_class + ['ipatoken' + options['type']]
|
|
|
|
for ttype, tattrs in TOKEN_TYPES.items():
|
|
|
|
if ttype != options['type']:
|
|
|
|
for tattr in tattrs:
|
|
|
|
if tattr in entry_attrs:
|
|
|
|
del entry_attrs[tattr]
|
2013-10-01 13:26:38 -05:00
|
|
|
|
2014-05-05 09:41:20 -05:00
|
|
|
# If owner was not specified, default to the person adding this token.
|
Add support for managedBy to tokens
This also constitutes a rethinking of the token ACIs after the introduction
of SELFDN support.
Admins, as before, have full access to all token permissions.
Normal users have read/search/compare access to all of the non-secret data
for tokens assigned to them, whether managed by them or not. Users can add
tokens if, and only if, they will also manage this token.
Managers can also read/search/compare tokens they manage. Additionally,
they can write non-secret data to their managed tokens and delete them.
When a normal user self-creates a token (the default behavior), then
managedBy is automatically set. When an admin creates a token for another
user (or no owner is assigned at all), then managed by is not set. In this
second case, the token is effectively read-only for the assigned owner.
This behavior enables two important other behaviors. First, an admin can
create a hardware token and assign it to the user as a read-only token.
Second, when the user is deleted, only his self-managed tokens are deleted.
All other (read-only) tokens are instead orphaned. This permits the same
token object to be reasigned to another user without loss of any counter
data.
https://fedorahosted.org/freeipa/ticket/4228
https://fedorahosted.org/freeipa/ticket/4259
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-02 15:44:30 -05:00
|
|
|
# If managedby was not specified, attempt a sensible default.
|
|
|
|
if 'ipatokenowner' not in entry_attrs or 'managedby' not in entry_attrs:
|
2018-03-20 09:40:24 -05:00
|
|
|
cur_dn = DN(self.api.Backend.ldap2.conn.whoami_s()[4:])
|
|
|
|
if cur_dn:
|
|
|
|
cur_uid = cur_dn[0].value
|
Add support for managedBy to tokens
This also constitutes a rethinking of the token ACIs after the introduction
of SELFDN support.
Admins, as before, have full access to all token permissions.
Normal users have read/search/compare access to all of the non-secret data
for tokens assigned to them, whether managed by them or not. Users can add
tokens if, and only if, they will also manage this token.
Managers can also read/search/compare tokens they manage. Additionally,
they can write non-secret data to their managed tokens and delete them.
When a normal user self-creates a token (the default behavior), then
managedBy is automatically set. When an admin creates a token for another
user (or no owner is assigned at all), then managed by is not set. In this
second case, the token is effectively read-only for the assigned owner.
This behavior enables two important other behaviors. First, an admin can
create a hardware token and assign it to the user as a read-only token.
Second, when the user is deleted, only his self-managed tokens are deleted.
All other (read-only) tokens are instead orphaned. This permits the same
token object to be reasigned to another user without loss of any counter
data.
https://fedorahosted.org/freeipa/ticket/4228
https://fedorahosted.org/freeipa/ticket/4259
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-02 15:44:30 -05:00
|
|
|
prev_uid = entry_attrs.setdefault('ipatokenowner', cur_uid)
|
|
|
|
if cur_uid == prev_uid:
|
2018-03-20 09:40:24 -05:00
|
|
|
entry_attrs.setdefault('managedby', cur_dn.ldap_text())
|
2014-05-05 09:41:20 -05:00
|
|
|
|
|
|
|
# Resolve the owner's dn
|
2013-10-01 13:26:38 -05:00
|
|
|
_normalize_owner(self.api.Object.user, entry_attrs)
|
|
|
|
|
|
|
|
# Get the issuer for the URI
|
|
|
|
owner = entry_attrs.get('ipatokenowner', None)
|
|
|
|
issuer = api.env.realm
|
|
|
|
if owner is not None:
|
|
|
|
try:
|
|
|
|
issuer = ldap.get_entry(owner, ['krbprincipalname'])['krbprincipalname'][0]
|
|
|
|
except (NotFound, IndexError):
|
|
|
|
pass
|
|
|
|
|
2016-08-24 06:29:37 -05:00
|
|
|
# Check if key is not empty
|
|
|
|
if entry_attrs['ipatokenotpkey'] is None:
|
|
|
|
raise ValidationError(name='key', error=_(u'cannot be empty'))
|
|
|
|
|
2013-10-01 13:26:38 -05:00
|
|
|
# Build the URI parameters
|
|
|
|
args = {}
|
|
|
|
args['issuer'] = issuer
|
|
|
|
args['secret'] = base64.b32encode(entry_attrs['ipatokenotpkey'])
|
|
|
|
args['digits'] = entry_attrs['ipatokenotpdigits']
|
2015-06-17 09:21:55 -05:00
|
|
|
args['algorithm'] = entry_attrs['ipatokenotpalgorithm'].upper()
|
2014-01-28 16:11:04 -06:00
|
|
|
if options['type'] == 'totp':
|
|
|
|
args['period'] = entry_attrs['ipatokentotptimestep']
|
|
|
|
elif options['type'] == 'hotp':
|
|
|
|
args['counter'] = entry_attrs['ipatokenhotpcounter']
|
2013-10-01 13:26:38 -05:00
|
|
|
|
|
|
|
# Build the URI
|
2015-09-14 05:52:29 -05:00
|
|
|
label = urllib.parse.quote(entry_attrs['ipatokenuniqueid'])
|
|
|
|
parameters = urllib.parse.urlencode(args)
|
2014-01-28 16:11:04 -06:00
|
|
|
uri = u'otpauth://%s/%s:%s?%s' % (options['type'], issuer, label, parameters)
|
2013-10-01 13:26:38 -05:00
|
|
|
setattr(context, 'uri', uri)
|
|
|
|
|
2014-10-15 11:24:56 -05:00
|
|
|
attrs_list.append("objectclass")
|
2013-10-01 13:26:38 -05:00
|
|
|
return dn
|
|
|
|
|
|
|
|
def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
|
|
|
|
entry_attrs['uri'] = getattr(context, 'uri')
|
2014-10-15 11:24:56 -05:00
|
|
|
_set_token_type(entry_attrs, **options)
|
2013-10-01 13:26:38 -05:00
|
|
|
_convert_owner(self.api.Object.user, entry_attrs, options)
|
|
|
|
return super(otptoken_add, self).post_callback(ldap, dn, entry_attrs, *keys, **options)
|
|
|
|
|
|
|
|
|
|
|
|
@register()
|
|
|
|
class otptoken_del(LDAPDelete):
|
|
|
|
__doc__ = _('Delete an OTP token.')
|
|
|
|
msg_summary = _('Deleted OTP token "%(value)s"')
|
|
|
|
|
|
|
|
|
|
|
|
@register()
|
|
|
|
class otptoken_mod(LDAPUpdate):
|
|
|
|
__doc__ = _('Modify a OTP token.')
|
|
|
|
msg_summary = _('Modified OTP token "%(value)s"')
|
|
|
|
|
|
|
|
def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
|
2014-07-29 08:45:21 -05:00
|
|
|
notafter_set = True
|
|
|
|
notbefore = options.get('ipatokennotbefore', None)
|
|
|
|
notafter = options.get('ipatokennotafter', None)
|
|
|
|
# notbefore xor notafter, exactly one of them is not None
|
|
|
|
if bool(notbefore) ^ bool(notafter):
|
|
|
|
result = self.api.Command.otptoken_show(keys[-1])['result']
|
|
|
|
if notbefore is None:
|
|
|
|
notbefore = result.get('ipatokennotbefore', [None])[0]
|
|
|
|
if notafter is None:
|
|
|
|
notafter_set = False
|
|
|
|
notafter = result.get('ipatokennotafter', [None])[0]
|
|
|
|
|
|
|
|
if not _check_interval(notbefore, notafter):
|
|
|
|
if notafter_set:
|
|
|
|
raise ValidationError(name='not_after',
|
|
|
|
error='is before the validity start')
|
|
|
|
else:
|
|
|
|
raise ValidationError(name='not_before',
|
|
|
|
error='is after the validity end')
|
2013-10-01 13:26:38 -05:00
|
|
|
_normalize_owner(self.api.Object.user, entry_attrs)
|
2014-10-15 11:24:56 -05:00
|
|
|
|
2015-01-14 08:57:45 -06:00
|
|
|
# ticket #4681: if the owner of the token is changed and the
|
|
|
|
# user also manages this token, then we should automatically
|
|
|
|
# set the 'managedby' attribute to the new owner
|
|
|
|
if 'ipatokenowner' in entry_attrs and 'managedby' not in entry_attrs:
|
|
|
|
new_owner = entry_attrs.get('ipatokenowner', None)
|
|
|
|
prev_entry = ldap.get_entry(dn, attrs_list=['ipatokenowner',
|
|
|
|
'managedby'])
|
|
|
|
prev_owner = prev_entry.get('ipatokenowner', None)
|
|
|
|
prev_managedby = prev_entry.get('managedby', None)
|
|
|
|
|
|
|
|
if (new_owner != prev_owner) and (prev_owner == prev_managedby):
|
|
|
|
entry_attrs.setdefault('managedby', new_owner)
|
|
|
|
|
2014-10-15 11:24:56 -05:00
|
|
|
attrs_list.append("objectclass")
|
2013-10-01 13:26:38 -05:00
|
|
|
return dn
|
|
|
|
|
|
|
|
def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
|
2014-10-15 11:24:56 -05:00
|
|
|
_set_token_type(entry_attrs, **options)
|
2013-10-01 13:26:38 -05:00
|
|
|
_convert_owner(self.api.Object.user, entry_attrs, options)
|
|
|
|
return super(otptoken_mod, self).post_callback(ldap, dn, entry_attrs, *keys, **options)
|
|
|
|
|
|
|
|
|
|
|
|
@register()
|
|
|
|
class otptoken_find(LDAPSearch):
|
|
|
|
__doc__ = _('Search for OTP token.')
|
Add support for managedBy to tokens
This also constitutes a rethinking of the token ACIs after the introduction
of SELFDN support.
Admins, as before, have full access to all token permissions.
Normal users have read/search/compare access to all of the non-secret data
for tokens assigned to them, whether managed by them or not. Users can add
tokens if, and only if, they will also manage this token.
Managers can also read/search/compare tokens they manage. Additionally,
they can write non-secret data to their managed tokens and delete them.
When a normal user self-creates a token (the default behavior), then
managedBy is automatically set. When an admin creates a token for another
user (or no owner is assigned at all), then managed by is not set. In this
second case, the token is effectively read-only for the assigned owner.
This behavior enables two important other behaviors. First, an admin can
create a hardware token and assign it to the user as a read-only token.
Second, when the user is deleted, only his self-managed tokens are deleted.
All other (read-only) tokens are instead orphaned. This permits the same
token object to be reasigned to another user without loss of any counter
data.
https://fedorahosted.org/freeipa/ticket/4228
https://fedorahosted.org/freeipa/ticket/4259
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-02 15:44:30 -05:00
|
|
|
msg_summary = ngettext('%(count)d OTP token matched', '%(count)d OTP tokens matched', 0)
|
2013-10-01 13:26:38 -05:00
|
|
|
|
2014-10-15 11:24:56 -05:00
|
|
|
def pre_callback(self, ldap, filters, attrs_list, *args, **kwargs):
|
2013-10-01 13:26:38 -05:00
|
|
|
# This is a hack, but there is no other way to
|
|
|
|
# replace the objectClass when searching
|
|
|
|
type = kwargs.get('type', '')
|
|
|
|
if type not in TOKEN_TYPES:
|
|
|
|
type = ''
|
|
|
|
filters = filters.replace("(objectclass=ipatoken)",
|
|
|
|
"(objectclass=ipatoken%s)" % type)
|
|
|
|
|
2014-10-15 11:24:56 -05:00
|
|
|
attrs_list.append("objectclass")
|
|
|
|
return super(otptoken_find, self).pre_callback(ldap, filters, attrs_list, *args, **kwargs)
|
2013-10-01 13:26:38 -05:00
|
|
|
|
|
|
|
def args_options_2_entry(self, *args, **options):
|
|
|
|
entry = super(otptoken_find, self).args_options_2_entry(*args, **options)
|
|
|
|
_normalize_owner(self.api.Object.user, entry)
|
|
|
|
return entry
|
|
|
|
|
|
|
|
def post_callback(self, ldap, entries, truncated, *args, **options):
|
|
|
|
for entry in entries:
|
2014-10-15 11:24:56 -05:00
|
|
|
_set_token_type(entry, **options)
|
2013-10-01 13:26:38 -05:00
|
|
|
_convert_owner(self.api.Object.user, entry, options)
|
|
|
|
return super(otptoken_find, self).post_callback(ldap, entries, truncated, *args, **options)
|
|
|
|
|
|
|
|
|
|
|
|
@register()
|
|
|
|
class otptoken_show(LDAPRetrieve):
|
|
|
|
__doc__ = _('Display information about an OTP token.')
|
|
|
|
|
2014-10-15 11:24:56 -05:00
|
|
|
def pre_callback(self, ldap, dn, attrs_list, *keys, **options):
|
|
|
|
attrs_list.append("objectclass")
|
|
|
|
return super(otptoken_show, self).pre_callback(ldap, dn, attrs_list, *keys, **options)
|
|
|
|
|
2013-10-01 13:26:38 -05:00
|
|
|
def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
|
2014-10-15 11:24:56 -05:00
|
|
|
_set_token_type(entry_attrs, **options)
|
2013-10-01 13:26:38 -05:00
|
|
|
_convert_owner(self.api.Object.user, entry_attrs, options)
|
|
|
|
return super(otptoken_show, self).post_callback(ldap, dn, entry_attrs, *keys, **options)
|
Add support for managedBy to tokens
This also constitutes a rethinking of the token ACIs after the introduction
of SELFDN support.
Admins, as before, have full access to all token permissions.
Normal users have read/search/compare access to all of the non-secret data
for tokens assigned to them, whether managed by them or not. Users can add
tokens if, and only if, they will also manage this token.
Managers can also read/search/compare tokens they manage. Additionally,
they can write non-secret data to their managed tokens and delete them.
When a normal user self-creates a token (the default behavior), then
managedBy is automatically set. When an admin creates a token for another
user (or no owner is assigned at all), then managed by is not set. In this
second case, the token is effectively read-only for the assigned owner.
This behavior enables two important other behaviors. First, an admin can
create a hardware token and assign it to the user as a read-only token.
Second, when the user is deleted, only his self-managed tokens are deleted.
All other (read-only) tokens are instead orphaned. This permits the same
token object to be reasigned to another user without loss of any counter
data.
https://fedorahosted.org/freeipa/ticket/4228
https://fedorahosted.org/freeipa/ticket/4259
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-02 15:44:30 -05:00
|
|
|
|
|
|
|
@register()
|
|
|
|
class otptoken_add_managedby(LDAPAddMember):
|
|
|
|
__doc__ = _('Add users that can manage this token.')
|
|
|
|
|
|
|
|
member_attributes = ['managedby']
|
|
|
|
|
|
|
|
@register()
|
|
|
|
class otptoken_remove_managedby(LDAPRemoveMember):
|
2015-08-05 00:50:07 -05:00
|
|
|
__doc__ = _('Remove users that can manage this token.')
|
Add support for managedBy to tokens
This also constitutes a rethinking of the token ACIs after the introduction
of SELFDN support.
Admins, as before, have full access to all token permissions.
Normal users have read/search/compare access to all of the non-secret data
for tokens assigned to them, whether managed by them or not. Users can add
tokens if, and only if, they will also manage this token.
Managers can also read/search/compare tokens they manage. Additionally,
they can write non-secret data to their managed tokens and delete them.
When a normal user self-creates a token (the default behavior), then
managedBy is automatically set. When an admin creates a token for another
user (or no owner is assigned at all), then managed by is not set. In this
second case, the token is effectively read-only for the assigned owner.
This behavior enables two important other behaviors. First, an admin can
create a hardware token and assign it to the user as a read-only token.
Second, when the user is deleted, only his self-managed tokens are deleted.
All other (read-only) tokens are instead orphaned. This permits the same
token object to be reasigned to another user without loss of any counter
data.
https://fedorahosted.org/freeipa/ticket/4228
https://fedorahosted.org/freeipa/ticket/4259
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-02 15:44:30 -05:00
|
|
|
|
|
|
|
member_attributes = ['managedby']
|