summaryrefslogtreecommitdiffstats
path: root/install/tools/ipa-adtrust-install
blob: 2af2df92f341a70a6650e14ccc6e4593eac11b76 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#! /usr/bin/python
#
# Authors: Sumit Bose <sbose@redhat.com>
# Based on ipa-server-install by Karl MacMillan <kmacmillan@mentalrootkit.com>
# and ipa-dns-install by Martin Nagy
#
# Copyright (C) 2011  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, 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/>.
#

from ipaserver.install import adtrustinstance
from ipaserver.install.installutils import *
from ipaserver.install import service
from ipapython import version
from ipapython import ipautil, sysrestore
from ipalib import api, errors, util
from ipapython.config import IPAOptionParser
import krbV
from ipapython.ipa_log_manager import *
from ipapython.dn import DN

log_file_name = "/var/log/ipaserver-install.log"

def parse_options():
    parser = IPAOptionParser(version=version.VERSION)
    parser.add_option("-d", "--debug", dest="debug", action="store_true",
                      default=False, help="print debugging information")
    parser.add_option("--ip-address", dest="ip_address",
                      type="ip", ip_local=True, help="Master Server IP Address")
    parser.add_option("--netbios-name", dest="netbios_name",
                      help="NetBIOS name of the IPA domain")
    parser.add_option("--no-msdcs", dest="no_msdcs", action="store_true",
                      default=False, help="Do not create DNS service records " \
                                          "for Windows in managed DNS server")
    parser.add_option("--rid-base", dest="rid_base", type=int, default=1000,
                      help="Start value for mapping UIDs and GIDs to RIDs")
    parser.add_option("--secondary-rid-base", dest="secondary_rid_base",
                      type=int, default=100000000,
                      help="Start value of the secondary range for mapping " \
                           "UIDs and GIDs to RIDs")
    parser.add_option("-U", "--unattended", dest="unattended", action="store_true",
                      default=False, help="unattended installation never prompts the user")
    parser.add_option("-a", "--admin-password",
                      sensitive=True, dest="admin_password",
                      help="admin user kerberos password")
    parser.add_option("-A", "--admin-name",
                      sensitive=True, dest="admin_name", default='admin',
                      help="admin user principal")
    parser.add_option("--add-sids", dest="add_sids", action="store_true",
                      default=False, help="Add SIDs for existing users and" \
                                          " groups as the final step")
    parser.add_option("--enable-compat",
                      dest="enable_compat", default=False, action="store_true",
                      help="Enable support for trusted domains for old clients")

    options, args = parser.parse_args()
    safe_options = parser.get_safe_opts(options)

    return safe_options, options

def netbios_name_error(name):
    print "\nIllegal NetBIOS name [%s].\n" % name
    print "Up to 15 characters and only uppercase ASCII letter and digits are allowed."

def read_netbios_name(netbios_default):
    netbios_name = ""

    print "Enter the NetBIOS name for the IPA domain."
    print "Only up to 15 uppercase ASCII letters and digits are allowed."
    print "Example: EXAMPLE."
    print ""
    print ""
    if not netbios_default:
        netbios_default = "EXAMPLE"
    while True:
        netbios_name = ipautil.user_input("NetBIOS domain name", netbios_default, allow_empty = False)
        print ""
        if adtrustinstance.check_netbios_name(netbios_name):
            break

        netbios_name_error(netbios_name)

    return netbios_name

def read_admin_password(admin_name):
    print "Configuring cross-realm trusts for IPA server requires password for user '%s'." % (admin_name)
    print "This user is a regular system account used for IPA server administration."
    print ""
    admin_password = read_password(admin_name, confirm=False, validate=None)
    return admin_password

def set_and_check_netbios_name(netbios_name, unattended):
    """
    Depending if trust in already configured or not a given NetBIOS domain
    name must be handled differently.

    If trust is not configured the given NetBIOS is used or the NetBIOS is
    generated if none was given on the command line.

    If trust is  already configured the given NetBIOS name is used to reset
    the stored NetBIOS name it it differs from the current one.
    """

    flat_name_attr = 'ipantflatname'
    cur_netbios_name = None
    gen_netbios_name = None
    reset_netbios_name = False
    dom_dn = None

    try:
        (dom_dn, entry) = api.Backend.ldap2.get_entry(DN(('cn', api.env.domain),
                                    api.env.container_cifsdomains,
                                    ipautil.realm_to_suffix(api.env.realm)),
                                    [flat_name_attr])
    except errors.NotFound:
        # trust not configured
        pass
    else:
        cur_netbios_name = entry.get(flat_name_attr)[0]

    if cur_netbios_name and not netbios_name:
        # keep the current NetBIOS name
        netbios_name = cur_netbios_name
        reset_netbios_name = False
    elif cur_netbios_name and cur_netbios_name != netbios_name:
        # change the NetBIOS name
        print "Current NetBIOS domain name is %s, new name is %s.\n" % \
              (cur_netbios_name, netbios_name)
        print "Please note that changing the NetBIOS name might " \
              "break existing trust relationships."
        if unattended:
            reset_netbios_name = True
            print "NetBIOS domain name will be changed to %s.\n" % \
                  netbios_name
        else:
            print "Say 'yes' if the NetBIOS shall be changed and " \
                  "'no' if the old one shall be kept."
            reset_netbios_name = ipautil.user_input(
                            'Do you want to reset the NetBIOS domain name?',
                            default = False, allow_empty = False)
        if not reset_netbios_name:
            netbios_name = cur_netbios_name
    elif cur_netbios_name and cur_netbios_name == netbios_name:
        # keep the current NetBIOS name
        reset_netbios_name = False
    elif not cur_netbios_name:
        if not netbios_name:
            gen_netbios_name = adtrustinstance.make_netbios_name(api.env.domain)

        if dom_dn:
            # Fix existing trust configuration
            print "Trust is configured but no NetBIOS domain name found, " \
                  "setting it now."
            reset_netbios_name = True
        else:
            # initial trust configuration
            reset_netbios_name = False
    else:
        # all possible cases should be covered above
        raise Exception('Unexpected state while checking NetBIOS domain name')

    if not adtrustinstance.check_netbios_name(netbios_name):
        if unattended and not gen_netbios_name:
            netbios_name_error(netbios_name)
            sys.exit("Aborting installation.")
        else:
            if netbios_name:
                netbios_name_error(netbios_name)
                netbios_name = None

    if not unattended and not netbios_name:
        netbios_name = read_netbios_name(gen_netbios_name)

    if unattended and not netbios_name and gen_netbios_name:
        netbios_name = gen_netbios_name

    return (netbios_name, reset_netbios_name)

def ensure_admin_kinit(admin_name, admin_password):
    try:
        ipautil.run(['kinit', admin_name], stdin=admin_password+'\n')
    except ipautil.CalledProcessError, e:
        print "There was error to automatically re-kinit your admin user ticket."
        return False
    return True

def enable_compat_tree():
    print "Do you want to enable support for trusted domains in Schema Compatibility plugin?"
    print "This will allow clients older than SSSD 1.9 and non-Linux clients to work with trusted users."
    print ""
    enable_compat = ipautil.user_input("Enable trusted domains support in slapi-nis?", default = False, allow_empty = False)
    print ""
    return enable_compat


def main():
    safe_options, options = parse_options()

    if os.getegid() != 0:
        sys.exit("Must be root to setup AD trusts on server")

    standard_logging_setup(log_file_name, debug=options.debug, filemode='a')
    print "\nThe log file for this installation can be found in %s" % log_file_name

    root_logger.debug('%s was invoked with options: %s' % (sys.argv[0], safe_options))
    root_logger.debug("missing options might be asked for interactively later\n")

    check_server_configuration()

    global fstore
    fstore = sysrestore.FileStore('/var/lib/ipa/sysrestore')

    print "=============================================================================="
    print "This program will setup components needed to establish trust to AD domains for"
    print "the FreeIPA Server."
    print ""
    print "This includes:"
    print "  * Configure Samba"
    print "  * Add trust related objects to FreeIPA LDAP server"
    #TODO:
    #print "  * Add a SID to all users and Posix groups"
    print ""
    print "To accept the default shown in brackets, press the Enter key."
    print ""

    # Check if samba packages are installed
    if not adtrustinstance.check_inst():
        sys.exit("Aborting installation.")

    # Initialize the ipalib api
    cfg = dict(
        in_server=True,
        debug=options.debug,
    )
    api.bootstrap(**cfg)
    api.finalize()

    # If domain name and realm does not match, IPA server will not be able
    # to estabilish trust with Active Directory. Print big fat warning.

    realm_not_matching_domain = (api.env.domain.upper() != api.env.realm)

    if realm_not_matching_domain:
        print("WARNING: Realm name does not match the domain name.\n"
              "You will not be able to estabilish trusts with Active "
              "Directory unless\nthe realm name of the IPA server matches its "
              "domain name.\n\n")
        if not options.unattended:
            if not ipautil.user_input("Do you wish to continue?",
                                      default = False,
                                      allow_empty = False):
                sys.exit("Aborting installation.")

    if adtrustinstance.ipa_smb_conf_exists():
        if not options.unattended:
            while True:
                print "IPA generated smb.conf detected."
                if not ipautil.user_input("Overwrite smb.conf?", default = False, allow_empty = False):
                    sys.exit("Aborting installation.")
                break

    if not options.unattended and not options.enable_compat:
        options.enable_compat = enable_compat_tree()

    # Check we have a public IP that is associated with the hostname
    ip = None
    try:
        hostaddr = resolve_host(api.env.host)
        if len(hostaddr) > 1:
            print >> sys.stderr, "The server hostname resolves to more than one address:"
            for addr in hostaddr:
                print >> sys.stderr, "  %s" % addr

            if options.ip_address:
                if str(options.ip_address) not in hostaddr:
                    print >> sys.stderr, "Address passed in --ip-address did not match any resolved"
                    print >> sys.stderr, "address!"
                    sys.exit(1)
                print "Selected IP address:", str(options.ip_address)
                ip = options.ip_address
            else:
                if options.unattended:
                    print >> sys.stderr, "Please use --ip-address option to specify the address"
                    sys.exit(1)
                else:
                    ip = read_ip_address(api.env.host, fstore)
        else:
                ip = hostaddr and ipautil.CheckedIPAddress(hostaddr[0], match_local=True)
    except Exception, e:
        print "Error: Invalid IP Address %s: %s" % (ip, e)
        print "Aborting installation"
        sys.exit(1)

    ip_address = str(ip)
    root_logger.debug("will use ip_address: %s\n", ip_address)

    admin_password = options.admin_password
    if not (options.unattended or admin_password):
        admin_password = read_admin_password(options.admin_name)

    admin_kinited = None
    if admin_password:
        admin_kinited = ensure_admin_kinit(options.admin_name, admin_password)
        if not admin_kinited:
            print "Proceeding with credentials that existed before"

    try:
        ctx = krbV.default_context()
        ccache = ctx.default_ccache()
        principal = ccache.principal()
    except krbV.Krb5Error, e:
        sys.exit("Must have Kerberos credentials to setup AD trusts on server")

    try:
        api.Backend.ldap2.connect(ccache)
    except errors.ACIError, e:
        sys.exit("Outdated Kerberos credentials. Use kdestroy and kinit to update your ticket")
    except errors.DatabaseError, e:
        sys.exit("Cannot connect to the LDAP database. Please check if IPA is running")

    try:
        user = api.Command.user_show(unicode(principal[0]))['result']
        group = api.Command.group_show(u'admins')['result']
        if not (user['uid'][0] in group['member_user'] and
                group['cn'][0] in user['memberof_group']):
            raise errors.RequirementError(name='admins group membership')
    except errors.RequirementError, e:
        sys.exit("Must have administrative privileges to setup AD trusts on server")
    except Exception, e:
        sys.exit("Unrecognized error during check of admin rights: %s" % (str(e)))

    (netbios_name, reset_netbios_name) = \
                                set_and_check_netbios_name(options.netbios_name,
                                options.unattended)

    if not options.add_sids:
        # The filter corresponds to ipa_sidgen_task.c LDAP search filter
        filter = '(&(objectclass=ipaobject)(!(objectclass=mepmanagedentry))' \
                 '(|(objectclass=posixaccount)(objectclass=posixgroup)' \
                 '(objectclass=ipaidobject))(!(ipantsecurityidentifier=*)))'
        base_dn = api.env.basedn
        try:
            root_logger.debug("Searching for objects with missing SID with "
                "filter=%s, base_dn=%s", filter, base_dn)
            (entries, truncated) = api.Backend.ldap2.find_entries(filter=filter,
                base_dn=base_dn, attrs_list=[''])
        except errors.NotFound:
            # All objects have SIDs assigned
            pass
        except (errors.DatabaseError, errors.NetworkError), e:
            print "Could not retrieve a list of objects that need a SID identifier assigned:"
            print unicode(e)
        else:
            object_count = len(entries)
            if object_count > 0:
                print ""
                print "WARNING: %d existing users or groups do not have a SID identifier assigned." \
                    % len(entries)
                print "Installer can run a task to have ipa-sidgen Directory Server plugin generate"
                print "the SID identifier for all these users. Please note, the in case of a high"
                print "number of users and groups, the operation might lead to high replication"
                print "traffic and performance degradation. Refer to ipa-adtrust-install(1) man page"
                print "for details."
                print ""
                if options.unattended:
                    print "Unattended mode was selected, installer will NOT run ipa-sidgen task!"
                else:
                    if ipautil.user_input("Do you want to run the ipa-sidgen task?", default=False,
                        allow_empty=False):
                        options.add_sids = True

    if not options.unattended:
        print ""
        print "The following operations may take some minutes to complete."
        print "Please wait until the prompt is returned."
        print ""

    smb = adtrustinstance.ADTRUSTInstance(fstore)
    smb.realm = api.env.realm
    smb.autobind = service.ENABLED
    smb.setup(api.env.host, ip_address, api.env.realm, api.env.domain,
              netbios_name, reset_netbios_name,
              options.rid_base, options.secondary_rid_base,
              options.no_msdcs, options.add_sids,
              enable_compat = options.enable_compat)
    smb.find_local_id_range()
    smb.create_instance()

    print """
=============================================================================
Setup complete

You must make sure these network ports are open:
\tTCP Ports:
\t  * 138: netbios-dgm
\t  * 139: netbios-ssn
\t  * 445: microsoft-ds
\tUDP Ports:
\t  * 138: netbios-dgm
\t  * 139: netbios-ssn
\t  * 389: (C)LDAP
\t  * 445: microsoft-ds

Additionally you have to make sure the FreeIPA LDAP server is not reachable
by any domain controller in the Active Directory domain by closing down
the following ports for these servers:
\tTCP Ports:
\t  * 389, 636: LDAP/LDAPS

You may want to choose to REJECT the network packets instead of DROPing
them to avoid timeouts on the AD domain controllers.

=============================================================================
"""
    if admin_password:
        admin_kinited = ensure_admin_kinit(options.admin_name, admin_password)

    if not admin_kinited:
        print """
WARNING: you MUST re-kinit admin user before using 'ipa trust-*' commands
family in order to re-generate Kerberos tickets to include AD-specific
information"""

    return 0

if __name__ == '__main__':
    run_script(main, log_file_name=log_file_name,
            operation_name='ipa-adtrust-install')