summaryrefslogtreecommitdiffstats
path: root/ipaserver/plugins/join.py
blob: b63000d89e92a3aa7a5be20dc780461602e4af66 (plain)
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
# 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

"""
Joining an IPA domain
"""

from ipalib import api, util
from ipalib import Command, Str, Int
from ipalib import errors
import krbV
import os, subprocess
from ipapython import ipautil
import tempfile
import sha
import stat
import shutil

def get_realm():
    krbctx = krbV.default_context()

    return unicode(krbctx.default_realm)

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"""

    requires_root = True

    takes_args = (
        Str('cn',
            validate_host,
            cli_name='hostname',
            doc="The hostname to register as",
            create_default=lambda **kw: unicode(util.get_fqdn()),
            autofill=True,
            #normalizer=lamda value: value.lower(),
        ),
    )
    takes_options= (
        Str('realm',
            doc="The IPA realm",
            create_default=lambda **kw: get_realm(),
            autofill=True,
        ),
        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)',
        ),
    )

    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
        ldap = self.api.Backend.ldap2

        host = None
        try:
            # First see if the host exists
            kw = {'fqdn': hostname, 'all': True}
            (dn, attrs_list) = api.Command['host_show'](**kw)

            # 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)
                (d, a) = api.Command['host_mod'](hostname, krbprincipalname=service)

            # It exists, can we write the password attributes?
            allowed = ldap.can_write(dn, 'krblastpwdchange')
            if not allowed:
                raise errors.ACIError(info="Insufficient 'write' privilege to the 'krbLastPwdChange' attribute of entry '%s'." % dn)

            kw = {'fqdn': hostname, 'all': True}
            (dn, attrs_list) = api.Command['host_show'](**kw)
        except errors.NotFound:
            (dn, attrs_list) = api.Command['host_add'](hostname)

        return (dn, attrs_list)

    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'))

api.register(join)