/
init.pp
2492 lines (2236 loc) · 83.4 KB
/
init.pp
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# FreeIPA templating module by James
# Copyright (C) 2012-2013+ James Shubin
# Written by James Shubin <james@shubin.ca>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# README: this is a rather complicated module to understand. read the comments!
# NOTE: if you ever see a puppet error where an ipa exec returns with:
# ipa: ERROR: no modifications to be performed
# then please report this as a bug. This puppet module is (supposed to be)
# smart enough to only run exec's when they are actually necessary.
# NOTE: to hack your way into the ipa web ui with ssh port forwarding, when the
# computer you are using is completely isolated from the actual ipa server, you
# could fake the dns entry in your /etc/hosts file by adding/ensuring the line:
# 127.0.0.1 ipa.example.com ipa localhost.localdomain localhost
# exists (replace example.com with your ipa domain of course) and then running:
# sudo ssh root@ipa -L 80:localhost:80 -L 443:localhost:443 # (as root !)
# to force forwarding on priviledged ports, and then point your web browser to:
# https://ipa.example.com/ipa/ui/
# and then accept the certificate. but don't do any of this, it's an evil hack!
# NOTE: this expects mit kerberos: http://web.mit.edu/kerberos/krb5-latest/doc/
# NOTE: useful ipa docs at: https://access.redhat.com/site/documentation/en-US/
# Red_Hat_Enterprise_Linux/6/html-single/Identity_Management_Guide/index.html
# NOTE: if on client reinstall ipa-client-install complains with:
# freeipa LDAP Error: Connect error: TLS error -8054: You are attempting
# to import a cert with the same issuer/serial as an existing cert, but
# that is not the same cert.
# just: 'rm /etc/ipa/ca.crt', bug: https://fedorahosted.org/freeipa/ticket/3537
# NOTE: if you wish to use the $dns option, it must be enabled at first install
# subsequent enabling/disabling is currently not supported. this is because of:
# https://fedorahosted.org/freeipa/ticket/3726
# (ipa-dns-install needs a --uninstall option)
# and also because the DM_PASSWORD might not be available if we gpg encrypt and
# email it out after randomly generating it. This is a security feature! (TODO) <- CHANGE TO (DONE) when finished!
# we could actually support install and uninstall if that bug was resolved, and
# if we either regenerated the password, or were able to circumvent it with our
# root powers somehow. this is actually quite plausible, but not worth the time
# TODO: maybe we could have an exported resource that creates a .k5login in the
# root home dirs of machines to give access to other admins with their tickets?
# TODO: a ...host::dns type or similar needs to be added to manage and host ips
class ipa::vardir { # module vardir snippet
if "${::puppet_vardirtmp}" == '' {
if "${::puppet_vardir}" == '' {
# here, we require that the puppetlabs fact exist!
fail('Fact: $puppet_vardir is missing!')
}
$tmp = sprintf("%s/tmp/", regsubst($::puppet_vardir, '\/$', ''))
# base directory where puppet modules can work and namespace in
file { "${tmp}":
ensure => directory, # make sure this is a directory
recurse => false, # don't recurse into directory
purge => true, # purge all unmanaged files
force => true, # also purge subdirs and links
owner => root,
group => nobody,
mode => 600,
backup => false, # don't backup to filebucket
#before => File["${module_vardir}"], # redundant
#require => Package['puppet'], # no puppet module seen
}
} else {
$tmp = sprintf("%s/", regsubst($::puppet_vardirtmp, '\/$', ''))
}
$module_vardir = sprintf("%s/ipa/", regsubst($tmp, '\/$', ''))
file { "${module_vardir}": # /var/lib/puppet/tmp/ipa/
ensure => directory, # make sure this is a directory
recurse => true, # recursively manage directory
purge => true, # purge all unmanaged files
force => true, # also purge subdirs and links
owner => root, group => nobody, mode => 600, backup => false,
require => File["${tmp}"], # File['/var/lib/puppet/tmp/']
}
}
class ipa::server(
$hostname = $::hostname,
$domain = $::domain,
$realm = '', # defaults to upcase($domain)
# TODO: how can we hide these values ?
# FIXME: maybe we could generate these passwords locally, and then save
# them to a file to be read by the installer and then gpg encrypted and
# emailed to a root email and then deleted from the file! this would
# require an admin's public pgp key which would be a cool thing to have
# TODO: error if these passwords aren't eight chars or more
$dm_password = '', # eight char minimum
$admin_password = '', # eight char minimum
$idstart = '16777216', # TODO: what is sensible? i picked 2^24
$idmax = '',
$email_domain = '', # defaults to domain
$shell = true, # defaults to /bin/sh
$homes = true, # defaults to /home
# packages products to install ?
$ntp = false, # opposite of ipa-server-install default
$dns = false, # must be set at install time to be used
$dogtag = false,
$email = '', # defaults to root@domain, important...
$shorewall = false,
$zone = 'net',
$allow = 'all',
# special
# NOTE: host_excludes is matched with bash regexp matching in: [[ =~ ]]
# if the string regexp passed contains quotes, string matching is done:
# $string='"hostname.example.com"' vs: $regexp='hostname.example.com' !
# obviously, each pattern in the array is tried, and any match will do.
# invalid expressions might cause breakage! use this at your own risk!!
# remember that you are matching against the fqdn's, which have dots...
# a value of true, will automatically add the * character to match all.
$host_excludes = [], # never purge these host excludes...
$service_excludes = [], # never purge these service excludes...
$user_excludes = [], # never purge these user excludes...
$ensure = present # TODO: support uninstall with 'absent'
) {
$FW = '$FW' # make using $FW in shorewall easier...
include ipa::vardir
#$vardir = $::ipa::vardir::module_vardir # with trailing slash
$vardir = regsubst($::ipa::vardir::module_vardir, '\/$', '')
$valid_hostname = "${hostname}" # TODO: validate ?
$valid_domain = downcase($domain) # TODO: validate ?
$valid_realm = $realm ? {
'' => upcase($valid_domain),
default => upcase($realm),
}
$default_email_domain = "${email_domain}" ? {
'' => "${valid_domain}",
default => "${email_domain}",
}
ipa::server::config { 'emaildomain':
value => "${default_email_domain}",
}
$default_shell = type($shell) ? {
'boolean' => $shell ? {
false => false, # unmanaged
default => '/bin/sh', # the default
},
default => "${shell}",
}
# we don't manage if value is false, otherwise it's good to go!
if ! (type($shell) == 'boolean' and (! $shell)) {
ipa::server::config { 'shell':
value => "${default_shell}",
}
}
# TODO: the home stuff seems to not use trailing slashes. can i add it?
$default_homes = type($homes) ? {
'boolean' => $homes ? {
false => false, # unmanaged
default => '/home', # the default
},
default => "${homes}",
}
if ! (type($homes) == 'boolean' and (! $homes)) {
ipa::server::config { 'homes':
value => "${default_homes}", # XXX: remove trailing slash if present ?
}
}
$valid_email = $email ? {
'' => "root@${default_email_domain}",
default => "${email}",
}
if "${valid_hostname}" == '' {
fail('A $hostname value is required.')
}
if "${valid_domain}" == '' {
fail('A $domain value is required.')
}
if $dns {
package { ['bind', 'bind-dyndb-ldap']:
ensure => present,
before => Package['ipa-server'],
}
}
package { 'ipa-server':
ensure => present,
}
package { 'python-argparse': # used by diff.py
ensure => present,
}
file { "${vardir}/diff.py": # used by a few child classes
source => 'puppet:///modules/ipa/diff.py',
owner => root,
group => nobody,
mode => 700, # u=rwx
backup => false, # don't backup to filebucket
ensure => present,
require => [
Package['ipa-server'],
Package['python-argparse'],
File["${vardir}/"],
],
}
# store the passwords in text files instead of having them on cmd line!
# TODO: storing plain text passwords is not good, so what should we do?
file { "${vardir}/dm.password":
content => "${dm_password}\n", # top top secret!
owner => root,
group => nobody,
mode => 600, # u=rw,go=
backup => false,
require => File["${vardir}/"],
ensure => present,
}
file { "${vardir}/admin.password":
content => "${admin_password}\n", # top secret!
owner => root,
group => nobody,
mode => 600, # u=rw,go=
backup => false,
require => File["${vardir}/"],
ensure => present,
}
# these are the arguments to ipa-server-install in the prompted order
$args01 = "--hostname='${valid_hostname}.${valid_domain}'"
$args02 = "--domain='${valid_domain}'"
$args03 = "--realm='${valid_realm}'"
#$args04 = "--ds-password='${dm_password}'" # Directory Manager
$args04 = "--ds-password=`/bin/cat '${vardir}/dm.password'`"
#$args05 = "--admin-password='${admin_password}'" # IPA admin
$args05 = "--admin-password=`/bin/cat '${vardir}/admin.password'`"
# TODO: reconcile these options with the range settings: EXAMPLE.COM_id_range
# if that range is changed, should we watch for it and reset? yes we should if we specified one here...
$args06 = $idstart ? {
'' => '',
default => "--idstart=${idstart}",
}
$args07 = $idmax ? {
'' => '',
default => "--idmax=${idmax}",
}
$args08 = $ntp ? {
true => '', # create ntp server...
default => '--no-ntp',
}
$args09 = $dns ? {
true => '--setup-dns --no-forwarders',
default => '',
}
$args10 = $dns ? {
true => "--zonemgr=${valid_email}",
default => '',
}
$arglist = ["${args01}", "${args02}", "${args03}", "${args04}", "${args05}", "${args06}", "${args07}", "${args08}", "${args09}", "${args10}"]
#$args = inline_template('<%= arglist.delete_if {|x| x.empty? }.join(" ") %>')
$args = join(delete($arglist, ''), ' ')
$unless = "/usr/bin/python -c 'import sys,ipaserver.install.installutils; sys.exit(0 if ipaserver.install.installutils.is_ipa_configured() else 1)'"
exec { "/usr/sbin/ipa-server-install ${args} --unattended":
logoutput => on_failure,
unless => "${unless}", # can't install if already installed...
require => [
Package['ipa-server'],
File["${vardir}/dm.password"],
File["${vardir}/admin.password"],
],
alias => 'ipa-install', # same alias as client to prevent both!
}
# check if we changed the dns state after initial install (unsupported)
# this is needed, because if dns was once setup, but the param is false
# then the host resource won't use --force and we'll get errors... this
# happens because of bug#: https://fedorahosted.org/freeipa/ticket/3726
if ! $dns {
exec { '/bin/false': # fail so that we know about the change
logoutput => on_failure,
# thanks to 'ab' in #freeipa for help with the ipa api!
onlyif => "/usr/bin/python -c 'import sys,ipalib;ipalib.api.bootstrap_with_global_options(context=\"puppet\");ipalib.api.finalize();(ipalib.api.Backend.ldap2.connect(ccache=ipalib.api.Backend.krb.default_ccname()) if ipalib.api.env.in_server else ipalib.api.Backend.xmlclient.connect());sys.exit(0 if ipalib.api.Command.dns_is_enabled().get(\"result\") else 1)'",
require => Package['ipa-server'],
alias => 'ipa-dns-check',
}
}
# TODO: add management of ipa services (ipa, httpd, krb5kdc, kadmin, etc...) run: ipactl status or service ipa status for more info
# TODO: add management (augeas?) of /etc/ipa/default.conf
# since we're on the kdc, we can use our root access to get a ticket...
# < me> kaduk_: [...] is this an evil hack? [...]
# < kaduk_> [...] It's not really a hack, but things running on the KDC
# are always a bit special.
#exec { "/bin/cat '${vardir}/admin.password' | /usr/bin/kinit admin":
# NOTE: i added a lifetime of 1 hour... no sense needing any longer
$rr = "krbtgt/${valid_realm}@${valid_realm}"
$tl = '900' # 60*15 => 15 minutes
exec { "/usr/bin/kinit -k -t KDB: admin -l 1h": # thanks to: kaduk_
logoutput => on_failure,
#unless => "/usr/bin/klist -s", # is there a credential cache
# NOTE: we need to check if the ticket has at least a certain
# amount of time left. if not, it could expire mid execution!
# this should definitely get patched, but in the meantime, we
# check that the current time is greater than the valid start
# time (in seconds) and that we have within $tl seconds left!
unless => "/usr/bin/klist -s && /usr/bin/test \$(( `/bin/date +%s` - `/usr/bin/klist | /bin/grep -F '${rr}' | /bin/awk '{print \$1\" \"\$2}' | /bin/date --file=- +%s` )) -gt 0 && /usr/bin/test \$(( `/usr/bin/klist | /bin/grep -F '${rr}' | /bin/awk '{print \$3\" \"\$4}' | /bin/date --file=- +%s` - `/bin/date +%s` )) -gt ${tl}",
require => [
Exec['ipa-install'],
#File["${vardir}/admin.password"],
],
alias => 'ipa-server-kinit',
}
# FIXME: consider allowing only certain ip's to the ipa server
# TODO: we could open ports per host when added with ipa::server::host
if $shorewall {
if $allow == 'all' or "${allow}" == '' {
$net = "${zone}"
} else {
$net = is_array($allow) ? {
true => sprintf("${zone}:%s", join($allow, ',')),
default => "${zone}:${allow}",
}
}
####################################################################
#ACTION SOURCE DEST PROTO DEST SOURCE ORIGINAL
# PORT PORT(S) DEST
shorewall::rule { 'http': rule => "
HTTP/ACCEPT ${net} $FW
", comment => 'Allow HTTP for webui'}
shorewall::rule { 'https': rule => "
HTTPS/ACCEPT ${net} $FW
", comment => 'Allow HTTPS for webui'}
shorewall::rule { 'ldap': rule => "
LDAP/ACCEPT ${net} $FW
", comment => 'Allow LDAP for 389 server on tcp port 389.'}
shorewall::rule { 'ldaps': rule => "
LDAPS/ACCEPT ${net} $FW
", comment => 'Allow LDAPS for 389 server on tcp port 636.'}
shorewall::rule { 'kerberos': rule => "
Kerberos/ACCEPT ${net} $FW
", comment => 'Allow Kerberos for krb5 server on tcp/udp port 88.'}
# TODO: should i propose this as a shorewall macro ?
shorewall::rule { 'kpasswd': rule => "
ACCEPT ${net} $FW tcp 464
ACCEPT ${net} $FW udp 464
", comment => 'Allow Kerberos for kpasswd on tcp/udp port 464.'}
if $ntp {
shorewall::rule { 'ntp': rule => "
NTP/ACCEPT ${net} $FW
", comment => 'Allow NTP on udp port 123.'}
}
if $dns {
shorewall::rule { 'dns': rule => "
DNS/ACCEPT ${net} $FW
", comment => 'Allow DNS on tcp/udp port 53.'}
}
if $dogtag {
shorewall::rule { 'dogtag': rule => "
ACCEPT ${net} $FW tcp 7389
", comment => 'Allow dogtag certificate system on tcp port 7389.'}
}
}
}
# FIXME: some values have not been filled in yet. some are missing: --arguments
define ipa::server::config(
$value
) {
$key = "${name}"
$etype = "${key}" ? { # expected type
#'?' => '', # FIXME: dn
#'?' => '', # --maxusername
'homes' => 'string',
'shell' => 'string',
#'?' => '', # --defaultgroup
'emaildomain' => 'string',
#'?' => '', # --searchtimelimit
#'?' => '', # --searchrecordslimit
'usersearch' => 'array',
'groupsearch' => 'array',
'migration' => 'boolean',
#'?' => '', # FIXME: ipacertificatesubjectbase
#'?' => '', # --groupobjectclasses
#'?' => '', # --userobjectclasses
#'?' => '', # --pwdexpnotify
#'?' => '', # --ipaconfigstring
#'?' => '', # --ipaselinuxusermaporder
#'?' => '', # --ipaselinuxusermapdefault
#'?' => '', # --pac-type
#'?' => '', # FIXME: cn
#'?' => '', # FIXME: objectclass
default => '', # missing
}
$option = "${key}" ? {
#'?' => 'dn', FIXME
#'?' => '--maxusername=',
'homes' => '--homedirectory=',
'shell' => '--defaultshell=',
#'?' => '--defaultgroup=',
'emaildomain' => '--emaildomain=',
#'?' => '--searchtimelimit=',
#'?' => '--searchrecordslimit=',
'usersearch' => '--usersearch=',
'groupsearch' => '--groupsearch=',
'migration' => '--enable-migration=',
#'?' => 'ipacertificatesubjectbase', FIXME
#'?' => '--groupobjectclasses=',
#'?' => '--userobjectclasses=',
#'?' => '--pwdexpnotify=',
#'?' => '--ipaconfigstring=',
#'?' => '--ipaselinuxusermaporder=',
#'?' => '--ipaselinuxusermapdefault=',
#'?' => '--pac-type=',
#'?' => 'cn', FIXME
#'?' => 'objectclass', FIXME
default => '', # missing
}
$rawkey = "${key}" ? {
#'?' => 'dn',
#'?' => 'ipamaxusernamelength',
'homes' => 'ipahomesrootdir',
'shell' => 'ipadefaultloginshell',
#'?' => 'ipadefaultprimarygroup',
'emaildomain' => 'ipadefaultemaildomain',
#'?' => 'ipasearchtimelimit',
#'?' => 'ipasearchrecordslimit',
'usersearch' => 'ipausersearchfields',
'groupsearch' => 'ipagroupsearchfields',
'migration' => 'ipamigrationenabled',
#'?' => 'ipacertificatesubjectbase',
#'?' => 'ipagroupobjectclasses',
#'?' => 'ipauserobjectclasses',
#'?' => 'ipapwdexpadvnotify',
#'?' => 'ipaconfigstring',
#'?' => 'ipaselinuxusermaporder',
#'?' => 'ipaselinuxusermapdefault',
#'?' => 'ipakrbauthzdata',
#'?' => 'cn',
#'?' => 'objectclass',
default => '', # missing
}
if "${option}" == '' or "${etype}" == '' or "${rawkey}" == '' {
fail("Key '${key}' is invalid.")
}
if type($value) != "${etype}" {
fail("Ipa::Server::Config[${key}] must be type: ${etype}.")
}
# convert to correct type
if "${etype}" == 'string' {
$safe_value = shellquote($value) # TODO: is this right ?
$jchar = '' # pass through the paste binary
} elsif "${etype}" == 'array' {
$jchar = "${key}" ? { # join char
'usersearch' => ',',
'groupsearch' => ',',
default => '',
}
$safe_value = inline_template('<%= value.join(jchar) %>')
} elsif "${etype}" == 'boolean' {
$safe_value = $value ? {
true => 'TRUE',
default => 'FALSE',
}
$jchar = '' # pass through the paste binary
} else {
fail("Unknown type: ${etype}.")
}
$cutlength = inline_template('<%= (rawkey.length+2).to_s %>')
exec { "/usr/bin/ipa config-mod ${option}'${safe_value}'":
unless => "/usr/bin/test \"`/usr/bin/ipa config-show --raw --all | /usr/bin/tr -d ' ' | /bin/grep '^${rawkey}:' | /bin/cut -b ${cutlength}- | /usr/bin/paste -sd '${jchar}'`\" = '${safe_value}'",
logoutput => on_failure,
require => [
Exec['ipa-install'],
Exec['ipa-server-kinit'],
],
}
}
class ipa::server::host::base {
include ipa::server
include ipa::vardir
#$vardir = $::ipa::vardir::module_vardir # with trailing slash
$vardir = regsubst($::ipa::vardir::module_vardir, '\/$', '')
# we don't want to purge the freeipa entry, so we need to exclude it...
$valid_hostname = $ipa::server::valid_hostname
$valid_domain = $ipa::server::valid_domain
$host_always_ignore = ["${valid_hostname}.${valid_domain}"]
$host_excludes = $ipa::server::host_excludes
$valid_host_excludes = type($host_excludes) ? {
'string' => [$host_excludes],
'array' => $host_excludes,
'boolean' => $host_excludes ? {
# TODO: there's probably a better fqdn match expression
# this is an expression to prevent all fqdn deletion...
#true => ['^[a-zA-Z0-9\.\-]*$'],
true => ['^[[:alpha:]]{1}[[:alnum:]-.]*$'],
default => false,
},
default => false, # trigger error...
}
if type($valid_host_excludes) != 'array' {
fail('The $host_excludes must be an array.')
}
# directory of system tags which should exist (as managed by puppet)
file { "${vardir}/hosts/":
ensure => directory, # make sure this is a directory
recurse => true, # recursively manage directory
purge => true, # purge all unmanaged files
force => true, # also purge subdirs and links
owner => root, group => nobody, mode => 600, backup => false,
notify => Exec['ipa-clean-hosts'],
require => File["${vardir}/"],
}
# these are template variables for the clean.sh.erb script
$id_dir = 'hosts'
$ls_cmd = '/usr/bin/ipa host-find --pkey-only --raw | /usr/bin/tr -d " " | /bin/grep "^fqdn:" | /bin/cut -b 6-' # show ipa hosts
# TODO: i don't understand all the implications of the --updatedns arg!
# we should probably change the dns arg based on if dns is on or not...
$rm_cmd = $dns ? { # delete ipa hosts
true => '/usr/bin/ipa host-del --updatedns ',
default => '/usr/bin/ipa host-del ',
}
$fs_chr = ' '
$suffix = '.host'
$regexp = $valid_host_excludes
$ignore = $host_always_ignore
# build the clean script
file { "${vardir}/clean-hosts.sh":
content => template('ipa/clean.sh.erb'),
owner => root,
group => nobody,
mode => 700, # u=rwx
backup => false, # don't backup to filebucket
ensure => present,
require => File["${vardir}/"],
}
# run the cleanup
exec { "${vardir}/clean-hosts.sh":
logoutput => on_failure,
refreshonly => true,
require => [
Exec['ipa-server-kinit'],
File["${vardir}/clean-hosts.sh"],
],
alias => 'ipa-clean-hosts',
}
# NOTE: it doesn't cause a problem that this dir is inside the hosts dir
file { "${vardir}/hosts/passwords/":
ensure => directory, # make sure this is a directory
recurse => true, # recursively manage directory
purge => true, # purge all unmanaged files
force => true, # also purge subdirs and links
owner => root, group => nobody, mode => 600, backup => false,
require => File["${vardir}/hosts/"],
}
file { "${vardir}/hosts/sshpubkeys/":
ensure => directory, # make sure this is a directory
recurse => true, # recursively manage directory
purge => true, # purge all unmanaged files
force => true, # also purge subdirs and links
owner => root, group => nobody, mode => 600, backup => false,
require => File["${vardir}/hosts/"],
}
}
# NOTE: this should only be used by a freeipa client and as an exported resource
define ipa::server::host::pwtag() {
include ipa::vardir
#$vardir = $::ipa::vardir::module_vardir # with trailing slash
$vardir = regsubst($::ipa::vardir::module_vardir, '\/$', '')
# the existence of this file means that an ipa client has exported it,
# and that the ipa server collected it and it means that a provisioned
# ipa client host is notifying the server that a new one time password
# does not need to be generated at this time. to reprovision the host,
# you must erase the exported resource that is sending this file here,
# or rather, in doing so, the ipa server will generate a new password!
file { "${vardir}/hosts/passwords/${name}.pwtag":
content => "# This is a password tag for: ${name}\n",
owner => root,
group => nobody,
mode => 600, # u=rw,go=
backup => false,
before => Exec["ipa-host-verify-password-exists-${name}"],
require => File["${vardir}/hosts/passwords/"],
ensure => present,
}
}
# NOTE: this is called by ipa::client internally and shouldn't be used manually
define ipa::server::host::sshpubkeys( # $name matches ipa::server::host $name
$rsa = '',
$dsa = ''
) {
include ipa::vardir
#$vardir = $::ipa::vardir::module_vardir # with trailing slash
$vardir = regsubst($::ipa::vardir::module_vardir, '\/$', '')
# FIXME: if i really cared, i would just have one argument, an array of
# keys, and i would loop through them creating each file... tempting...
if "${rsa}" != '' {
file { "${vardir}/hosts/sshpubkeys/${name}/rsa.pub":
content => "${rsa}\n",
owner => root,
group => nobody,
mode => 600, # u=rw,go=
backup => false,
# this before is essential, and it implies that it will also go
# before the "ipa-server-host-mod-${name}" exec, because of the
# relationship between those two types. mod might not always be
# present (if $modify is false) so don't directly reference it.
before => Exec["ipa-server-host-add-${name}"],
require => File["${vardir}/hosts/sshpubkeys/${name}/"],
ensure => present,
}
}
if "${dsa}" != '' {
file { "${vardir}/hosts/sshpubkeys/${name}/dsa.pub":
content => "${dsa}\n",
owner => root,
group => nobody,
mode => 600, # u=rw,go=
backup => false,
# this before is essential, and it implies that it will also go
# before the "ipa-server-host-mod-${name}" exec, because of the
# relationship between those two types. mod might not always be
# present (if $modify is false) so don't directly reference it.
before => Exec["ipa-server-host-add-${name}"],
require => File["${vardir}/hosts/sshpubkeys/${name}/"],
ensure => present,
}
}
}
define ipa::server::host(
$domain = $ipa::server::domain, # default to main domain
$server = '', # where the client will find the ipa server...
$macaddress = '', # TODO: this should be a list...
#$ipaddress = '', # NOTE: this is a bad fit here...
$sshpubkeys = true, # leave this at the default to get auto sshkeys
#$certificate = ???, # TODO ?
$password = '', # one time password used for host provisioning!
$random = false, # or set this to true to have us generate it...
# comment parameters...
$locality = '', # host locality (e.g. "Montreal, Canada")
$location = '', # host location (e.g. "Lab 42")
$platform = '', # host hardware platform (e.g. "Lenovo X201")
$osstring = '', # host operating system and version (e.g. "CentOS 6.4")
$comments = '', # host description (e.g. "NFS server")
#$hosts = [], # TODO: add hosts managed by support
# client specific parameters...
$admin = false, # should client get admin tools installed ?
# special parameters...
$watch = true, # manage all changes to this resource, reverting others
$modify = true # modify this resource on puppet changes or not ?
) {
include ipa::server
include ipa::server::host::base
include ipa::vardir
#$vardir = $::ipa::vardir::module_vardir # with trailing slash
$vardir = regsubst($::ipa::vardir::module_vardir, '\/$', '')
$dns = $ipa::server::dns # boolean from main obj
$valid_domain = downcase($domain)
$valid_server = "${server}" ? {
'' => "${::hostname}.${::domain}",
default => "${server}",
}
# NOTE: the valid_fqdn is actually what ipa calls a hostname internally
# if $name has dots, then we assume it's a fqdn, if not, we add $domain
$valid_fqdn = delete("${name}", '.') ? {
"${name}" => "${name}.${valid_domain}", # had no dots present
default => "${name}", # had dots present...
}
$valid_sshpubkeys = type($sshpubkeys) ? {
'string' => "${sshpubkeys}" ? {
# BUG: lol: https://projects.puppetlabs.com/issues/15813
'' => [], # assume managed but empty (rm sshkeys)
default => ["${sshpubkeys}"],
},
'boolean' => $sshpubkeys,
'array' => $sshpubkeys,
default => '', # set an error...
}
if "${valid_sshpubkeys}" == '' {
fail('You must specify a valid type for $sshpubkeys.')
}
if $watch and (! $modify) {
fail('You must be able to $modify to be able to $watch.')
}
# NOTE: this is not a good fit for host-* it is part of the dns system,
# and not the host, and should be managed separately
#if $dns {
# $args00 = "${ipaddress}" ? {
# '' => '',
# default => "--ip-address='${ipaddress}'",
# }
#} else {
# $args00 = ''
# # TODO: allow this silently for now...
# #warning("Host: '${valid_fqdn}' is setting an IP without DNS.")
#}
$args01 = "${macaddress}" ? {
'' => '',
default => "--macaddress='${macaddress}'",
}
# array means: managed, set these keys exactly, and remove when it's []
# boolean false means: unmanaged, don't set or get anything... empty ''
# boolean true means: managed, get the keys automatically (super magic)
$args02 = type($valid_sshpubkeys) ? {
# we always have to at least specify the '--sshpubkey=' if this
# is empty, because otherwise we have no way to remove old keys
'array' => inline_template('<% if valid_sshpubkeys == [] %>--sshpubkey=<% else %><%= valid_sshpubkeys.map {|x| "--sshpubkey=\'"+x+"\'" }.join(" ") %><% end %>'),
default => $valid_sshpubkeys ? { # boolean
false => '', # unmanaged, do nothing
# this large beast loops through all the collected dirs
# and cats the contents of each file into an individual
# --sshpubkey argument. if no keys are found, the empty
# --sshpubkey argument is returned. this is all used to
# build the ipa commands. i hope this doesn't overflow!
default => "`a=(); for i in ${vardir}/hosts/sshpubkeys/${name}/*.pub; do [ -e \"\$i\" ] || break; a+=(\"--sshpubkey='\$(/bin/cat \$i)'\"); done; if [ \"\${a[*]}\" == '' ]; then /bin/echo \"--sshpubkey=\"; else /bin/echo \${a[@]}; fi`",
},
}
$args03 = "${locality}" ? {
'' => '',
default => "--locality='${locality}'",
}
$args04 = "${location}" ? {
'' => '',
default => "--location='${location}'",
}
$args05 = "${platform}" ? {
'' => '',
default => "--platform='${platform}'",
}
$args06 = "${osstring}" ? {
'' => '',
default => "--os='${osstring}'",
}
$args07 = "${comments}" ? {
'' => '',
default => "--desc='${comments}'",
}
$arglist = ["${args01}", "${args02}", "${args03}", "${args04}", "${args05}", "${args06}", "${args07}"]
$args = join(delete($arglist, ''), ' ')
if $random and ("${password}" != '') {
fail('Specify $random or $password, but not both.')
}
$argspass = "${password}" ? {
'' => $random ? {
true => '--random',
default => '', # no password specified
},
#default => "--password='${password}'", # direct mode, (bad)!
default => "--password=`/bin/cat '${vardir}/hosts/passwords/${valid_fqdn}.password'`",
}
$qarglist = ["${argspass}"] # NOTE: add any silent arg changes here
$qargs = join(delete($qarglist, ''), ' ')
# if we're not modifying, we need to add on the qargs stuff to the add!
$xarglist = $modify ? {
false => concat($arglist, $qarglist),
default => $arglist,
}
$xargs = join(delete($xarglist, ''), ' ')
# NOTE: this file is the subscribe destination for the modify exec when
# not using watch mode. it is separate from the qhost file (which is
# used for unwatchable changes), because if we had only one notify
# source, then a configuration transition from watch to unwatched would
# actually trigger a modification. this file is also the official file
# that is used by the clean script for determining which hosts need to
# be erased. please keep in mind that on accidental notification, or on
# system rebuild, the differing changes will be erased.
file { "${vardir}/hosts/${valid_fqdn}.host":
content => "${valid_fqdn}\n${args}\n",
owner => root,
group => nobody,
mode => 600, # u=rw,go=
require => File["${vardir}/hosts/"],
ensure => present,
}
file { "${vardir}/hosts/${valid_fqdn}.qhost":
content => "${valid_fqdn}\n${qargs}\n",
owner => root,
group => nobody,
mode => 600, # u=rw,go=
require => File["${vardir}/hosts/"],
ensure => present,
}
# NOTE: a custom fact, reads from these dirs and collects the passwords
if $random {
file { "${vardir}/hosts/passwords/${valid_fqdn}.password":
# no content! this is a tag, content comes in by echo !
owner => root,
group => nobody,
mode => 600, # u=rw,go=
backup => false,
notify => $modify ? {
false => undef, # can't notify if not modifying
default => Exec["ipa-server-host-qmod-${name}"],
},
require => File["${vardir}/hosts/passwords/"],
ensure => present,
}
} elsif "${password}" != '' {
file { "${vardir}/hosts/passwords/${valid_fqdn}.password":
content => "${password}\n", # top secret (briefly!)
owner => root,
group => nobody,
mode => 600, # u=rw,go=
backup => false,
notify => $modify ? {
false => undef, # can't notify if not modifying
default => Exec["ipa-server-host-qmod-${name}"],
},
before => $modify ? {
false => undef,
default => Exec["ipa-server-host-qmod-${name}"],
},
require => File["${vardir}/hosts/passwords/"],
ensure => present,
}
}
file { "${vardir}/hosts/sshpubkeys/${name}/": # store host ssh keys
ensure => directory, # make sure this is a directory
recurse => true, # recursively manage directory
purge => true, # purge all unmanaged files
force => true, # also purge subdirs and links
owner => root, group => nobody, mode => 600, backup => false,
require => File["${vardir}/hosts/sshpubkeys/"],
}
# collect host specific ssh keys
Ipa::Server::Host::Sshpubkeys <<| tag == "${name}" |>> {
#realname => "${name}",
#basedir => "${vardir}/hosts/sshpubkeys/${name}/",
}
$exists = "/usr/bin/ipa host-show '${valid_fqdn}' > /dev/null 2>&1"
# NOTE: we don't need to set the password in the host-add, because the
# host-mod that deals specifically with password stuff will trigger it
# NOTE: --force is needed when dns is configured for ipa but we're not
# setting an ip address on host-add. this makes ipa sad, and it fails!
# NOTE: we don't seem to need --force for host-mod, as it hasn't erred
$force = "${xargs}" ? { # if args is empty
'' => '--force', # we have no args!
default => "${xargs} --force", # pixel perfect...
}
$fargs = $dns ? { # without the dns,
true => "${force}", # we don't need to
default => "${xargs}", # force everything
}
# NOTE: this runs when no host is present...
#exec { "/usr/bin/ipa host-add '${valid_fqdn}' ${fargs}":
exec { "ipa-server-host-add-${name}": # alias
# this has to be here because the command string gets too long
# for a puppet $name var and strange things start to happen...
command => "/usr/bin/ipa host-add '${valid_fqdn}' ${fargs}",
logoutput => on_failure,
unless => "${exists}",
require => $dns ? {
true => [
Exec['ipa-server-kinit'],
File["${vardir}/hosts/sshpubkeys/${name}/"],
],
default => [
Exec['ipa-dns-check'], # avoid --force errors!
Exec['ipa-server-kinit'],
File["${vardir}/hosts/sshpubkeys/${name}/"],
],
},
#alias => "ipa-server-host-add-${name}",
}
# NOTE: this runs when we detect that the attributes don't match (diff)
if $modify and ("${args}" != '') { # if there are changes to do...
#exec { "/usr/bin/ipa host-mod '${valid_fqdn}' ${args}":
exec { "ipa-server-host-mod-${name}":
command => "/usr/bin/ipa host-mod '${valid_fqdn}' ${args}",
logoutput => on_failure,
refreshonly => $watch ? {
false => true, # when not watching, we
default => undef, # refreshonly to change
},
subscribe => $watch ? {
false => File["${vardir}/hosts/${valid_fqdn}.host"],
default => undef,
},
onlyif => "${exists}",
unless => $watch ? {
false => undef, # don't run the diff checker...
default => "${exists} && ${vardir}/diff.py host '${valid_fqdn}' ${args}",
},
before => "${qargs}" ? { # only if exec exists !
'' => undef,
default => Exec["ipa-server-host-qmod-${name}"],
},
require => [
File["${vardir}/diff.py"],
Exec['ipa-server-kinit'],
Exec["ipa-server-host-add-${name}"],
File["${vardir}/hosts/sshpubkeys/${name}/"],
],
#alias => "ipa-server-host-mod-${name}",
}
}
# NOTE: this runs when there should be an attribute change we can't see
if $modify and ("${qargs}" != '') { # quiet q changes to do
# this is a bonus to double check that a password entry exists!
# once a host is provisioned, it will reset the single use pass
# and this script would normally try and create a new one back,
# however if a pwtag is collected, then it won't run the notify
# this is pretty advanced stuff to understand, but it's useful!
if $random or ("${password}" != '') {
# collect any password tags. note i used $name exactly!
Ipa::Server::Host::Pwtag <<| tag == "${name}" |>> {
}
exec { "ipa-host-verify-password-exists-${name}": # uid
command => '/bin/true', # i'm just here for the notify!
# do not run this if the password tag exists...
# if it dissapears, that means the host is gone
unless => "/usr/bin/test -e '${vardir}/hosts/passwords/${name}.pwtag'",
# only do this if machine is unenrolled, eg see