2009-02-18 15:45:49 -06:00
|
|
|
# Authors:
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 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
|
|
|
|
|
|
|
|
"""
|
2009-06-16 09:51:44 -05:00
|
|
|
Joining an IPA domain
|
2009-02-18 15:45:49 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
from ipalib import api, util
|
|
|
|
from ipalib import Command, Str, Int
|
2009-04-23 07:51:59 -05:00
|
|
|
from ipalib import errors
|
2009-02-18 15:45:49 -06:00
|
|
|
import krbV
|
|
|
|
import os, subprocess
|
2009-04-22 13:35:43 -05:00
|
|
|
from ipapython import ipautil
|
|
|
|
import tempfile
|
|
|
|
import sha
|
|
|
|
import stat
|
|
|
|
import shutil
|
2009-02-18 15:45:49 -06:00
|
|
|
|
|
|
|
def get_realm():
|
|
|
|
krbctx = krbV.default_context()
|
|
|
|
|
2009-04-22 13:36:29 -05:00
|
|
|
return unicode(krbctx.default_realm)
|
2009-02-18 15:45:49 -06: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
|
|
|
|
|
|
|
|
class join(Command):
|
|
|
|
"""Join an IPA domain"""
|
|
|
|
|
2009-04-22 13:36:29 -05:00
|
|
|
requires_root = True
|
|
|
|
|
2009-02-18 15:45:49 -06:00
|
|
|
takes_args = (
|
|
|
|
Str('cn',
|
|
|
|
validate_host,
|
|
|
|
cli_name='hostname',
|
|
|
|
doc="The hostname to register as",
|
2009-04-22 13:36:29 -05:00
|
|
|
create_default=lambda **kw: unicode(util.get_fqdn()),
|
|
|
|
autofill=True,
|
2009-02-18 15:45:49 -06:00
|
|
|
#normalizer=lamda value: value.lower(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
takes_options= (
|
|
|
|
Str('realm',
|
|
|
|
doc="The IPA realm",
|
2009-04-22 13:36:29 -05:00
|
|
|
create_default=lambda **kw: get_realm(),
|
|
|
|
autofill=True,
|
2009-02-18 15:45:49 -06: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
|
|
|
|
|
|
|
|
try:
|
|
|
|
host = api.Command['host_show'](hostname)
|
2009-04-23 07:51:59 -05:00
|
|
|
except errors.NotFound:
|
2009-02-18 15:45:49 -06:00
|
|
|
pass
|
|
|
|
else:
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.DuplicateEntry
|
2009-02-18 15:45:49 -06:00
|
|
|
|
|
|
|
return api.Command['host_add'](hostname)
|
|
|
|
|
2009-04-22 13:36:29 -05:00
|
|
|
def output_for_cli(self, textui, result, args, **options):
|
|
|
|
textui.print_plain("Welcome to the %s realm" % options['realm'])
|
|
|
|
textui.print_plain("Your keytab is in %s" % result.get('keytab'))
|
2009-02-18 15:45:49 -06:00
|
|
|
|
|
|
|
def run(self, *args, **options):
|
|
|
|
"""
|
|
|
|
Dispatch to forward() and execute() to do work locally and on the
|
|
|
|
server.
|
|
|
|
"""
|
2009-04-22 13:36:29 -05:00
|
|
|
if self.env.in_server:
|
2009-02-18 15:45:49 -06:00
|
|
|
return self.execute(*args, **options)
|
|
|
|
|
2009-04-22 13:36:29 -05:00
|
|
|
# This forward will call the server-side portion of join
|
|
|
|
result = self.forward(*args, **options)
|
|
|
|
|
|
|
|
self._get_keytab(result['krbprincipalname'])
|
2009-05-05 16:25:35 -05:00
|
|
|
result['keytab'] = '/etc/krb5.keytab'
|
2009-04-22 13:36:29 -05:00
|
|
|
return result
|
2009-02-18 15:45:49 -06:00
|
|
|
|
2009-04-22 13:36:29 -05:00
|
|
|
def _get_keytab(self, principal, stdin=None):
|
2009-05-05 16:25:35 -05:00
|
|
|
args = ["/usr/sbin/ipa-getkeytab", "-s", self.env.host, "-p", principal,"-k", "/etc/krb5.keytab"]
|
2009-04-22 13:35:43 -05:00
|
|
|
return ipautil.run(args, stdin)
|
2009-02-18 15:45:49 -06:00
|
|
|
|
|
|
|
api.register(join)
|