summaryrefslogtreecommitdiffstats
path: root/ipsilon/helpers/ipa.py
blob: 4a4849aa58006b5139131e16f0ff7d03ba718727 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python
#
# Copyright (C) 2014  Simo Sorce <simo@redhat.com>
#
# 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/>.

import logging
import pwd
import os
import socket
import subprocess
import sys


IPA_CONFIG_FILE = '/etc/ipa/default.conf'
HTTPD_IPA_KEYTAB = '/etc/httpd/conf/ipa.keytab'
IPA_COMMAND = '/usr/bin/ipa'
IPA_GETKEYTAB = '/usr/sbin/ipa-getkeytab'
HTTPD_USER = 'apache'

NO_CREDS_FOR_KEYTAB = """
Valid IPA admin credentials are required to get a keytab.
Please kinit with a pivileged user like 'admin' and retry.
"""

FAILED_TO_GET_KEYTAB = """
A pre-existing keytab was not found and it was not possible to
successfully retrieve a new keytab for the IPA server. Please
manually provide a keytab or resolve the error that cause this
failure (see logs) and retry.
"""


class Installer(object):

    def __init__(self):
        self.name = 'ipa'
        self.ptype = 'helper'
        self.logger = None
        self.realm = None
        self.domain = None
        self.server = None

    def install_args(self, group):
        group.add_argument('--ipa', choices=['yes', 'no', 'auto'],
                           default='auto',
                           help='Helper for IPA joined machines')

    def conf_init(self, opts):
        logger = self.logger
        # Do a simple check to see if machine is ipa joined
        if not os.path.exists(IPA_CONFIG_FILE):
            logger.info('No IPA configuration file. Skipping ipa helper...')
            if opts['ipa'] == 'yes':
                raise Exception('No IPA installation found!')
            return

        # Get config vars from ipa file
        try:
            from ipapython import config as ipaconfig

            ipaconfig.init_config()
            self.realm = ipaconfig.config.get_realm()
            self.domain = ipaconfig.config.get_domain()
            self.server = ipaconfig.config.get_server()

        except Exception, e:  # pylint: disable=broad-except
            logger.info('IPA tools installation found: [%s]', str(e))
            if opts['ipa'] == 'yes':
                raise Exception('No IPA installation found!')
            return

    def get_keytab(self, opts):
        logger = self.logger
        # Check if we have need ipa tools
        if not os.path.exists(IPA_GETKEYTAB):
            logger.info('ipa-getkeytab missing. Will skip keytab creation.')
            if opts['ipa'] == 'yes':
                raise Exception('No IPA tools found!')

        # Check if we already have a keytab for HTTP
        if 'krb_httpd_keytab' in opts:
            msg = "Searching for keytab in: %s" % opts['krb_httpd_keytab']
            print >> sys.stdout, msg,
            if os.path.exists(opts['krb_httpd_keytab']):
                print >> sys.stdout, "... Found!"
                return
            else:
                print >> sys.stdout, "... Not found!"

        msg = "Searching for keytab in: %s" % HTTPD_IPA_KEYTAB
        print >> sys.stdout, msg,
        if os.path.exists(HTTPD_IPA_KEYTAB):
            opts['krb_httpd_keytab'] = HTTPD_IPA_KEYTAB
            print >> sys.stdout, "... Found!"
            return
        else:
            print >> sys.stdout, "... Not found!"

        us = socket.gethostname()
        princ = 'HTTP/%s@%s' % (us, self.realm)

        # Check we have credentials to access server (for keytab)
        from ipapython import ipaldap
        from ipalib import errors as ipaerrors

        for srv in self.server:
            msg = "Testing access to server: %s" % srv
            print >> sys.stdout, msg,
            try:
                server = srv
                c = ipaldap.IPAdmin(host=server)
                c.do_sasl_gssapi_bind()
                del c
                print >> sys.stdout, "... Succeeded!"
                break
            except ipaerrors.ACIError, e:
                # usually this error is returned when we have no
                # good credentials, ask the user to kinit and retry
                print >> sys.stderr, NO_CREDS_FOR_KEYTAB
                logger.error('Invalid credentials: [%s]', repr(e))
                raise Exception('Invalid credentials: [%s]', str(e))
            except Exception, e:  # pylint: disable=broad-except
                # for other exceptions let's try to fail later
                pass

        try:
            subprocess.check_output([IPA_COMMAND, 'service-add', princ],
                                    stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError, e:
            # hopefully this means the service already exists
            # otherwise we'll fail later again
            logger.info('Error trying to create HTTP service:')
            logger.info('Cmd> %s\n%s', e.cmd, e.output)

        try:
            msg = "Trying to fetch keytab[%s] for %s" % (
                  opts['krb_httpd_keytab'], princ)
            print >> sys.stdout, msg,
            subprocess.check_output([IPA_GETKEYTAB,
                                     '-s', server, '-p', princ,
                                     '-k', opts['krb_httpd_keytab']],
                                    stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError, e:
            # unfortunately this one is fatal
            print >> sys.stderr, FAILED_TO_GET_KEYTAB
            logger.info('Error trying to get HTTP keytab:')
            logger.info('Cmd> %s\n%s', e.cmd, e.output)
            raise Exception('Missing keytab: [%s]' % str(e))

        # Fixup permissions so only the ipsilon user can read these files
        pw = pwd.getpwnam(HTTPD_USER)
        os.chown(opts['krb_httpd_keytab'], pw.pw_uid, pw.pw_gid)

    def configure_server(self, opts):
        if opts['ipa'] != 'yes' and opts['ipa'] != 'auto':
            return
        if opts['ipa'] != 'yes' and opts['krb'] == 'no':
            return

        self.logger = logging.getLogger()

        self.conf_init(opts)

        self.get_keytab(opts)

        # Forcibly use krb then pam modules
        if 'lm_order' not in opts:
            opts['lm_order'] = []
        opts['krb'] = 'yes'
        if 'krb' not in opts['lm_order']:
            opts['lm_order'].insert(0, 'krb')
        opts['form'] = 'yes'
        if not any(lm in opts['lm_order'] for lm in ('form', 'pam')):
            opts['lm_order'].append('form')