Files
freeipa/ipaserver/plugins/join.py
T

142 lines
4.3 KiB
Python
Raw Normal View History

2009-02-18 16:45:49 -05:00
# Authors:
# Rob Crittenden <rcritten@redhat.com>
#
# Copyright (C) 2009 Red Hat
# see file 'COPYING' for use and warranty information
#
2010-12-09 13:59:11 +01:00
# 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.
2009-02-18 16:45:49 -05:00
#
# 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
2010-12-09 13:59:11 +01:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2009-02-18 16:45:49 -05:00
2017-05-23 18:35:57 +02:00
import logging
2015-09-11 13:43:28 +02:00
import six
from ipalib import Registry, api
from ipalib import Command, Str
from ipalib import errors
2010-03-05 16:11:21 -05:00
from ipalib import _
2020-09-23 08:46:44 +02:00
from ipalib.constants import FQDN
2009-02-18 16:45:49 -05:00
__doc__ = _("""
Joining an IPA domain
""")
2015-09-11 13:43:28 +02:00
if six.PY3:
unicode = str
2017-05-23 18:35:57 +02:00
logger = logging.getLogger(__name__)
register = Registry()
2013-03-13 14:47:03 +01:00
2009-02-18 16:45:49 -05:00
def validate_host(ugettext, cn):
"""
Require at least one dot in the hostname (to support localhost.localdomain)
"""
dots = len(cn.split('.'))
if dots < 2:
return 'Fully-qualified hostname required'
return None
2013-03-13 14:47:03 +01:00
@register()
2009-02-18 16:45:49 -05:00
class join(Command):
__doc__ = _('Join an IPA domain')
2009-02-18 16:45:49 -05:00
NO_CLI = True
2009-02-18 16:45:49 -05:00
takes_args = (
Str('cn',
validate_host,
cli_name='hostname',
2010-03-05 16:11:21 -05:00
doc=_("The hostname to register as"),
2020-09-23 08:46:44 +02:00
default_from=lambda: FQDN,
autofill=True,
2009-02-18 16:45:49 -05:00
#normalizer=lamda value: value.lower(),
),
)
2013-03-13 14:47:03 +01:00
takes_options = (
2009-02-18 16:45:49 -05:00
Str('realm',
2010-03-05 16:11:21 -05:00
doc=_("The IPA realm"),
2015-07-20 16:04:07 +02:00
default_from=lambda: api.env.realm,
autofill=True,
2009-02-18 16:45:49 -05:00
),
2009-09-14 17:04:08 -04:00
Str('nshardwareplatform?',
cli_name='platform',
2010-03-05 16:11:21 -05:00
doc=_('Hardware platform of the host (e.g. Lenovo T61)'),
2009-09-14 17:04:08 -04:00
),
Str('nsosversion?',
cli_name='os',
2010-03-05 16:11:21 -05:00
doc=_('Operating System and version of the host (e.g. Fedora 9)'),
2009-09-14 17:04:08 -04:00
),
2009-02-18 16:45:49 -05:00
)
has_output = tuple()
use_output_validation = False
2009-02-18 16:45:49 -05:00
def execute(self, hostname, **kw):
"""
Execute the machine join operation.
Returns the entry as it will be created in LDAP.
:param hostname: The name of the host joined
:param kw: Keyword arguments for the other attributes.
"""
assert 'cn' not in kw
2009-09-14 17:04:08 -04:00
ldap = self.api.Backend.ldap2
2009-02-18 16:45:49 -05:00
# realm parameter is not supported by host_{add,mod}
kw.pop('realm', None)
2009-02-18 16:45:49 -05:00
try:
2009-09-14 17:04:08 -04:00
# First see if the host exists
show_kw = {'fqdn': hostname, 'all': True}
attrs_list = api.Command['host_show'](**show_kw)['result']
dn = attrs_list['dn']
2009-09-14 17:04:08 -04:00
2013-03-13 14:47:03 +01:00
# No error raised so far means that host entry exists
2017-05-23 18:35:57 +02:00
logger.info('Host entry for %s already exists, '
'joining may fail on the client side '
'if not forced', hostname)
2013-03-13 14:47:03 +01:00
2009-09-14 17:04:08 -04:00
# If no principal name is set yet we need to try to add
# one.
if 'krbprincipalname' not in attrs_list:
service = "host/%s@%s" % (hostname, api.env.realm)
api.Command['host_mod'](hostname, **kw,
krbprincipalname=service)
2017-05-23 18:35:57 +02:00
logger.info('No principal set, setting to %s', service)
2009-09-14 17:04:08 -04:00
# It exists, can we write the password attributes?
allowed = ldap.can_write(dn, 'krblastpwdchange')
if not allowed:
2013-03-13 14:47:03 +01:00
raise errors.ACIError(info=_("Insufficient 'write' privilege "
"to the 'krbLastPwdChange' attribute of entry '%s'.") % dn)
2009-09-14 17:04:08 -04:00
2013-03-13 14:47:03 +01:00
# Reload the attrs_list and dn so that we return update values
attrs_list = api.Command['host_show'](**show_kw)['result']
dn = attrs_list['dn']
2013-03-13 14:47:03 +01:00
except errors.NotFound:
attrs_list = api.Command['host_add'](hostname, **kw,
2013-03-13 14:47:03 +01:00
force=True)['result']
dn = attrs_list['dn']
2009-02-18 16:45:49 -05:00
config = api.Command['config_show']()['result']
2013-03-13 14:47:03 +01:00
attrs_list['ipacertificatesubjectbase'] =\
config['ipacertificatesubjectbase']
return dn, attrs_list