2009-05-27 08:55:49 -05:00
|
|
|
# Authors:
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
|
|
|
# Pavel Zuna <pzuna@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 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; version 2 only
|
|
|
|
#
|
|
|
|
# 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, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
"""
|
|
|
|
Hosts/Machines (Identity)
|
|
|
|
"""
|
|
|
|
|
|
|
|
from ipalib import api, crud, errors, util
|
|
|
|
from ipalib import Object
|
2009-09-14 16:04:08 -05:00
|
|
|
from ipalib import Str, Flag, List
|
2009-07-02 03:04:28 -05:00
|
|
|
from ipalib.plugins.service import split_principal
|
2009-08-10 15:24:10 -05:00
|
|
|
from ipalib import uuid
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
_container_dn = api.env.container_host
|
|
|
|
_default_attributes = [
|
|
|
|
'fqdn', 'description', 'localityname', 'nshostlocation',
|
|
|
|
'nshardwareplatform', 'nsosversion'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def get_host(ldap, hostname):
|
|
|
|
"""
|
|
|
|
Try to get the hostname as fully-qualified first, then fall back to
|
|
|
|
just a host name search.
|
|
|
|
"""
|
|
|
|
if hostname.endswith('.'):
|
|
|
|
hostname = hostname[:-1]
|
|
|
|
try:
|
|
|
|
dn = ldap.find_entry_by_attr('fqdn', hostname, 'ipaHost')[0]
|
|
|
|
except errors.NotFound:
|
|
|
|
dn = ldap.find_entry_by_attr('serverhostname', hostname, 'ipaHost')[0]
|
|
|
|
return dn
|
|
|
|
|
|
|
|
def validate_host(ugettext, fqdn):
|
|
|
|
"""
|
|
|
|
Require at least one dot in the hostname (to support localhost.localdomain)
|
|
|
|
"""
|
|
|
|
if fqdn.index('.') == -1:
|
|
|
|
return 'Fully-qualified hostname required'
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class host(Object):
|
2009-05-27 08:55:49 -05:00
|
|
|
"""
|
|
|
|
Host object.
|
|
|
|
"""
|
|
|
|
takes_params = (
|
|
|
|
# FIXME: All Object params get cloned with query=True in the new
|
|
|
|
# CRUD base classes, so there's no validation going on
|
|
|
|
Str('fqdn', validate_host,
|
|
|
|
cli_name='hostname',
|
|
|
|
doc='Hostname',
|
|
|
|
primary_key=True,
|
|
|
|
normalizer=lambda value: value.lower(),
|
|
|
|
),
|
|
|
|
Str('description?',
|
|
|
|
doc='Description of the host',
|
|
|
|
),
|
|
|
|
Str('localityname?',
|
|
|
|
cli_name='locality',
|
|
|
|
doc='Locality of the host (Baltimore, MD)',
|
|
|
|
),
|
|
|
|
Str('nshostlocation?',
|
|
|
|
cli_name='location',
|
|
|
|
doc='Location of the host (e.g. Lab 2)',
|
|
|
|
),
|
|
|
|
Str('nshardwareplatform?',
|
|
|
|
cli_name='platform',
|
|
|
|
doc='Hardware platform of the host (e.g. Lenovo T61)',
|
|
|
|
),
|
|
|
|
Str('nsosversion?',
|
|
|
|
cli_name='os',
|
|
|
|
doc='Operating System and version of the host (e.g. Fedora 9)',
|
|
|
|
),
|
|
|
|
Str('userpassword?',
|
|
|
|
cli_name='password',
|
|
|
|
doc='Password used in bulk enrollment',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(host)
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
class host_add(crud.Create):
|
2009-05-27 08:55:49 -05:00
|
|
|
"""
|
|
|
|
Create new host.
|
|
|
|
"""
|
|
|
|
def execute(self, hostname, **kw):
|
|
|
|
"""
|
|
|
|
Execute the host-add operation.
|
|
|
|
|
|
|
|
The dn should not be passed as a keyword argument as it is constructed
|
|
|
|
by this method.
|
|
|
|
|
|
|
|
If password is set then this is considered a 'bulk' host so we
|
|
|
|
do not create a kerberos service principal.
|
|
|
|
|
|
|
|
Returns the entry as it will be created in LDAP.
|
|
|
|
|
|
|
|
:param hostname: The name of the host being added.
|
|
|
|
:param kw: Keyword arguments for the other LDAP attributes.
|
|
|
|
"""
|
|
|
|
assert 'fqdn' not in kw
|
|
|
|
assert 'cn' not in kw
|
|
|
|
assert 'dn' not in kw
|
|
|
|
assert 'krbprincipalname' not in kw
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
|
|
|
|
entry_attrs = self.args_options_2_entry(hostname, **kw)
|
|
|
|
entry_attrs['cn'] = hostname
|
|
|
|
entry_attrs['serverhostname'] = hostname.split('.', 1)[0]
|
|
|
|
|
|
|
|
dn = ldap.make_dn(entry_attrs, 'fqdn', _container_dn)
|
|
|
|
|
|
|
|
# FIXME: do a DNS lookup to ensure host exists
|
|
|
|
|
|
|
|
# FIXME: add this attribute to cn=ipaconfig
|
|
|
|
# config = ldap.get_ipa_config()[1]
|
|
|
|
# kw['objectclass'] = config.get('ipahostobjectclasses')
|
2009-08-10 15:24:10 -05:00
|
|
|
entry_attrs['objectclass'] = ['ipaobject', 'nshost', 'ipahost', 'pkiuser']
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
if 'userpassword' not in entry_attrs:
|
|
|
|
entry_attrs['krbprincipalname'] = 'host/%s@%s' % (
|
|
|
|
hostname, self.api.env.realm
|
|
|
|
)
|
|
|
|
if 'krbprincipalaux' not in entry_attrs['objectclass']:
|
|
|
|
entry_attrs['objectclass'].append('krbprincipalaux')
|
|
|
|
entry_attrs['objectclass'].append('krbprincipal')
|
|
|
|
elif 'krbprincipalaux' in entry_attrs['objectclass']:
|
|
|
|
entry_attrs['objectclass'].remove('krbprincipalaux')
|
|
|
|
|
2009-08-10 15:24:10 -05:00
|
|
|
entry_attrs['ipauniqueid'] = str(uuid.uuid1())
|
|
|
|
|
2009-05-27 08:55:49 -05:00
|
|
|
ldap.add_entry(dn, entry_attrs)
|
|
|
|
|
|
|
|
return ldap.get_entry(dn, entry_attrs.keys())
|
|
|
|
|
|
|
|
def output_for_cli(self, textui, result, hostname, **options):
|
|
|
|
"""
|
|
|
|
Output result of this command to command line interface.
|
|
|
|
"""
|
|
|
|
(dn, entry_attrs) = result
|
|
|
|
|
|
|
|
textui.print_name(self.name)
|
|
|
|
textui.print_attribute('dn', dn)
|
|
|
|
textui.print_entry(entry_attrs)
|
|
|
|
textui.print_dashed('Created host "%s".' % hostname)
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(host_add)
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
class host_del(crud.Delete):
|
2009-05-27 08:55:49 -05:00
|
|
|
"""
|
|
|
|
Delete host.
|
|
|
|
"""
|
|
|
|
def execute(self, hostname, **kw):
|
|
|
|
"""
|
|
|
|
Delete a host.
|
|
|
|
|
|
|
|
hostname is the name of the host to delete
|
|
|
|
|
|
|
|
:param hostname: The name of the host being removed.
|
|
|
|
:param kw: Not used.
|
|
|
|
"""
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
dn = get_host(ldap, hostname)
|
2009-07-02 03:04:28 -05:00
|
|
|
hostname = hostname.lower()
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
# Remove all service records for this host
|
2009-06-24 08:18:34 -05:00
|
|
|
(services, truncated) = api.Command['service_find'](hostname)
|
2009-07-02 03:04:28 -05:00
|
|
|
for (dn_, entry_attrs) in services:
|
|
|
|
principal = entry_attrs['krbprincipalname'][0]
|
|
|
|
(service, hostname_, realm) = split_principal(principal)
|
|
|
|
if hostname_.lower() == hostname:
|
|
|
|
api.Command['service_del'](principal)
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
ldap.delete_entry(dn)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def output_for_cli(self, textui, result, hostname, **options):
|
|
|
|
"""
|
|
|
|
Output result of this command to command line interface.
|
|
|
|
"""
|
|
|
|
textui.print_name(self.name)
|
|
|
|
textui.print_dashed('Deleted host "%s".' % hostname)
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(host_del)
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class host_mod(crud.Update):
|
2009-05-27 08:55:49 -05:00
|
|
|
"""
|
|
|
|
Modify host.
|
|
|
|
"""
|
2009-09-14 16:04:08 -05:00
|
|
|
|
|
|
|
takes_options = (
|
|
|
|
Str('krbprincipalname?',
|
|
|
|
cli_name='principalname',
|
|
|
|
doc='Kerberos principal name for this host',
|
|
|
|
attribute=True
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2009-05-27 08:55:49 -05:00
|
|
|
def execute(self, hostname, **kw):
|
|
|
|
"""
|
|
|
|
Execute the host-mod operation.
|
|
|
|
|
|
|
|
The dn should not be passed as a keyword argument as it is constructed
|
|
|
|
by this method.
|
|
|
|
|
|
|
|
Returns the entry
|
|
|
|
|
|
|
|
:param hostname: The name of the host to retrieve.
|
|
|
|
:param kw: Keyword arguments for the other LDAP attributes.
|
|
|
|
"""
|
|
|
|
assert 'fqdn' not in kw
|
|
|
|
assert 'dn' not in kw
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
dn = get_host(ldap, hostname)
|
|
|
|
|
|
|
|
entry_attrs = self.args_options_2_entry(**kw)
|
|
|
|
|
2009-09-14 16:04:08 -05:00
|
|
|
# Once a principal name is set it cannot be changed
|
|
|
|
if 'krbprincipalname' in entry_attrs:
|
|
|
|
(d, e) = api.Command['host_show'](hostname, all=True)
|
|
|
|
if 'krbprincipalname' in e:
|
|
|
|
raise errors.ACIError(info='Principal name already set, it is unchangeable.')
|
|
|
|
entry_attrs['objectclass'] = e['objectclass']
|
|
|
|
entry_attrs['objectclass'].append('krbprincipalaux')
|
|
|
|
|
2009-05-27 08:55:49 -05:00
|
|
|
try:
|
|
|
|
ldap.update_entry(dn, entry_attrs)
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return ldap.get_entry(dn, entry_attrs.keys())
|
|
|
|
|
|
|
|
def output_for_cli(self, textui, result, hostname, **options):
|
|
|
|
"""
|
|
|
|
Output result of this command to command line interface.
|
|
|
|
"""
|
|
|
|
(dn, entry_attrs) = result
|
|
|
|
|
|
|
|
textui.print_name(self.name)
|
|
|
|
textui.print_attribute('dn', dn)
|
|
|
|
textui.print_entry(entry_attrs)
|
|
|
|
textui.print_dashed('Modified host "%s".' % hostname)
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(host_mod)
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class host_find(crud.Search):
|
2009-05-27 08:55:49 -05:00
|
|
|
"""
|
|
|
|
Search for hosts.
|
|
|
|
"""
|
|
|
|
|
|
|
|
takes_options = (
|
|
|
|
Flag('all',
|
|
|
|
doc='Retrieve all attributes'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def execute(self, term, **kw):
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
|
|
|
|
search_kw = self.args_options_2_entry(**kw)
|
|
|
|
search_kw['objectclass'] = 'ipaHost'
|
|
|
|
filter = ldap.make_filter(search_kw, rules=ldap.MATCH_ALL)
|
|
|
|
|
|
|
|
search_kw = {}
|
|
|
|
for a in _default_attributes:
|
|
|
|
search_kw[a] = term
|
|
|
|
term_filter = ldap.make_filter(search_kw, exact=False)
|
|
|
|
|
2009-06-24 08:18:34 -05:00
|
|
|
filter = ldap.combine_filters(
|
|
|
|
(filter, term_filter), rules=ldap.MATCH_ALL
|
|
|
|
)
|
|
|
|
|
2009-05-27 08:55:49 -05:00
|
|
|
if kw['all']:
|
|
|
|
attrs_list = ['*']
|
|
|
|
else:
|
|
|
|
attrs_list = _default_attributes
|
|
|
|
|
|
|
|
try:
|
2009-06-15 08:59:20 -05:00
|
|
|
(entries, truncated) = ldap.find_entries(
|
|
|
|
filter, attrs_list, _container_dn
|
|
|
|
)
|
2009-05-27 08:55:49 -05:00
|
|
|
except errors.NotFound:
|
2009-06-15 08:59:20 -05:00
|
|
|
(entries, truncated) = (tuple(), False)
|
2009-05-27 08:55:49 -05:00
|
|
|
|
2009-06-24 08:18:34 -05:00
|
|
|
return (entries, truncated)
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
def output_for_cli(self, textui, result, term, **options):
|
2009-06-15 08:59:20 -05:00
|
|
|
(entries, truncated) = result
|
|
|
|
|
2009-05-27 08:55:49 -05:00
|
|
|
textui.print_name(self.name)
|
2009-06-15 08:59:20 -05:00
|
|
|
for (dn, entry_attrs) in entries:
|
2009-05-27 08:55:49 -05:00
|
|
|
textui.print_attribute('dn', dn)
|
|
|
|
textui.print_entry(entry_attrs)
|
|
|
|
textui.print_plain('')
|
|
|
|
textui.print_count(
|
2009-07-02 08:19:54 -05:00
|
|
|
len(entries), '%i host matched.', '%i hosts matched.'
|
2009-05-27 08:55:49 -05:00
|
|
|
)
|
2009-06-15 08:59:20 -05:00
|
|
|
if truncated:
|
|
|
|
textui.print_dashed('These results are truncated.', below=False)
|
|
|
|
textui.print_dashed(
|
|
|
|
'Please refine your search and try again.', above=False
|
|
|
|
)
|
2009-05-27 08:55:49 -05:00
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(host_find)
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class host_show(crud.Retrieve):
|
2009-05-27 08:55:49 -05:00
|
|
|
"""
|
|
|
|
Display host.
|
|
|
|
"""
|
|
|
|
takes_options = (
|
|
|
|
Flag('all',
|
2009-09-14 16:04:08 -05:00
|
|
|
cli_short_name='a',
|
2009-05-27 08:55:49 -05:00
|
|
|
doc='Retrieve all attributes'
|
|
|
|
),
|
2009-09-14 16:04:08 -05:00
|
|
|
List('attrs?',
|
|
|
|
doc='comma-separated list of attributes to display'
|
|
|
|
),
|
2009-05-27 08:55:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
def execute(self, hostname, **kw):
|
|
|
|
"""
|
|
|
|
Execute the host-show operation.
|
|
|
|
|
|
|
|
The dn should not be passed as a keyword argument as it is constructed
|
|
|
|
by this method.
|
|
|
|
|
|
|
|
Returns the entry
|
|
|
|
|
|
|
|
:param hostname: The login name of the host to retrieve.
|
|
|
|
:param kw: "all" set to True = return all attributes
|
|
|
|
"""
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
dn = get_host(ldap, hostname)
|
|
|
|
|
|
|
|
if kw['all']:
|
|
|
|
attrs_list = ['*']
|
|
|
|
else:
|
2009-09-14 16:04:08 -05:00
|
|
|
if 'attrs' in kw:
|
|
|
|
attrs_list = kw['attrs']
|
|
|
|
else:
|
|
|
|
attrs_list = _default_attributes
|
2009-05-27 08:55:49 -05:00
|
|
|
|
|
|
|
return ldap.get_entry(dn, attrs_list)
|
|
|
|
|
|
|
|
def output_for_cli(self, textui, result, *args, **options):
|
|
|
|
(dn, entry_attrs) = result
|
|
|
|
|
|
|
|
textui.print_name(self.name)
|
|
|
|
textui.print_attribute('dn', dn)
|
|
|
|
textui.print_entry(entry_attrs)
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(host_show)
|