-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathezjail-admin
executable file
·1715 lines (1417 loc) · 80.8 KB
/
ezjail-admin
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
#!/bin/sh
# $Id$
# ugly: this variable is set during port install time
ezjail_prefix=EZJAIL_PREFIX
ezjail_admin=`basename -- $0`
ezjail_etc="${ezjail_prefix}/etc"
ezjail_share="${ezjail_prefix}/share/ezjail"
ezjail_examples="${ezjail_prefix}/share/examples/ezjail"
ezjail_jailcfgs="${ezjail_etc}/ezjail"
ezjail_snap_date_format="%Y%m%d%H%M"
# read user config
[ -f "${ezjail_etc}/ezjail.conf" ] && . "${ezjail_etc}/ezjail.conf"
# set defaults
: ${ezjail_jaildir="/usr/jails"}
: ${ezjail_jailtemplate="${ezjail_jaildir}/newjail"}
: ${ezjail_jailbase="${ezjail_jaildir}/basejail"}
: ${ezjail_jailfull="${ezjail_jaildir}/fulljail"}
: ${ezjail_jailtemp="${ezjail_jaildir}/ezjailtemp"}
: ${ezjail_flavours_dir="${ezjail_jaildir}/flavours"}
: ${ezjail_archivedir="${ezjail_jaildir}/ezjail_archives"}
: ${ezjail_sourcetree="/usr/src"}
: ${ezjail_uglyperlhack="YES"}
: ${ezjail_default_execute="/usr/bin/login -f root"}
: ${ezjail_mount_enable="YES"}
: ${ezjail_devfs_enable="YES"}
: ${ezjail_devfs_ruleset="devfsrules_jail"}
: ${ezjail_procfs_enable="YES"}
: ${ezjail_fdescfs_enable="YES"}
: ${ezjail_exec_start="/bin/sh /etc/rc"}
: ${ezjail_use_zfs="NO"}
ezjail_dirlist="bin boot lib libexec rescue sbin usr/bin usr/include usr/lib usr/libdata usr/libexec usr/sbin usr/src usr/share"
ezjail_basesystem="base"
# amd64 needs some extra libs
case `uname -p` in amd64) ezjail_dirlist="${ezjail_dirlist} usr/lib32"; ezjail_basesystem="${ezjail_basesystem} lib32";; esac
# Synopsis messages
ezjail_usage_ezjailadmin="${ezjail_admin} v3.4.2\nUsage: ${ezjail_admin} [archive|config|console|create|delete|freeze|install|list|restore|snapshot|troubleshoot|update] {params}"
ezjail_usage_install="Usage: ${ezjail_admin} install [-mMpPsS] [-h host] [-r release]"
ezjail_usage_create="Usage: ${ezjail_admin} create [-xbi] [-f flavour] [-r jailroot] [-s size] [-c bde|eli|zfs] [-C args] [-a archive] [-z parentzfs] jailname jailip"
ezjail_usage_delete="Usage: ${ezjail_admin} delete [-wf] jailname"
ezjail_usage_update="Usage: ${ezjail_admin} update [-s sourcetree|sourceosversion] [-p] (-b|-i|-u|-U|-P)"
ezjail_usage_config="Usage: ${ezjail_admin} config [-r run|norun] [-n newname] [-c cpuset] [-z zfs-datasets] [-f fib] [-i attach|detach|fsck] jailname"
ezjail_usage_console="Usage: ${ezjail_admin} console [-f] [-e command] jailname"
ezjail_usage_archive="Usage: ${ezjail_admin} archive [-Af] [-a archive] [-d archivedir] jailname [jailname...]"
ezjail_usage_restore="Usage: ${ezjail_admin} restore [-f] [-d archivedir] (archive|jailname)..."
ezjail_usage_freeze="Usage: ${ezjail_admin} freeze jailname newflavour"
ezjail_usage_troubleshoot="Usage: ${ezjail_admin} troubleshoot [jailname...]"
ezjail_usage_list="Usage: ${ezjail_admin} list"
################################
# End of variable initialization
#
# define our bail out shortcut
exerr () { echo -e "$*" >&2 ; exit 1; }
# generic attach routine for image jails
attach_images () {
# Create a memory disc from jail image
ezjail_imagedevice=`mdconfig -a -t vnode -f ${ezjail_image}` || exerr "Error: Could not attach memory disc."
# If this is a crypto jail, try to mount it, remind user, which jail
# this is. In this case, the device to mount is
case ${ezjail_imagetype} in
crypto|bde)
echo Attaching bde device for image jail ${ezjail}...
echo gbde attach "/dev/${ezjail_imagedevice}" ${ezjail_attachparams} | /bin/sh
[ $? -eq 0 ] || detach_images keep || exerr "Error: Attaching bde device failed."
# Device to mount is not md anymore
ezjail_device="${ezjail_imagedevice}.bde"
;;
eli)
echo "Attaching eli device for image jail ${ezjail}..."
echo geli attach ${ezjail_attachparams} "/dev/${ezjail_imagedevice}" | /bin/sh
[ $? -eq 0 ] || detach_images keep || exerr "Error: Attaching eli device failed."
# Device to mount is not md anymore
ezjail_device="${ezjail_imagedevice}.eli"
;;
simple)
ezjail_device=${ezjail_imagedevice}
;;
esac
}
# generic mount routine for image jails
mount_images () {
rm -f "${ezjail_devicelink}"
# Attach images by type
attach_images
# Clean image
fsck -t ffs -p "/dev/${ezjail_device}"
mount "/dev/${ezjail_device}" "${ezjail_rootdir}" || detach_images keep || exerr "Error: Could not mount /dev/${ezjail_device} to ${ezjail_rootdir}."
# relink image device
ln -s "/dev/${ezjail_device}" "${ezjail_devicelink}"
}
# generic detach routine for image jails
detach_images () {
# Avoid ending up inside mount point
cd /
# unmount and detach memory disc
if [ "${ezjail_imagedevice}" ]; then
umount "${ezjail_rootdir}" > /dev/null 2> /dev/null
case ${ezjail_imagetype} in
bde) gbde detach "/dev/${ezjail_imagedevice}" > /dev/null;;
eli) geli detach "/dev/${ezjail_imagedevice}" > /dev/null;;
esac
mdconfig -d -u "${ezjail_imagedevice}" > /dev/null
[ "$1" = "keep" ] || rm -f "${ezjail_image}"
fi
# Remove soft link (which acts as a lock)
[ -e "/dev/${ezjail_imagedevice}" ] || rm -f "${ezjail_devicelink}"
# This function is being called in case of error. Keep $? bad
return 1
}
# Find and execute our rc script
start_stop_jail_by_script () {
ezjail_action=$1
case ${ezjail_action} in *start*) ezjail_success_check="-n";; *) ezjail_success_check="-z";; esac
# Try to locate and run ezjails rc.d script
if [ -x "${ezjail_prefix}/etc/rc.d/ezjail" ]; then
"${ezjail_prefix}/etc/rc.d/ezjail" $@
elif [ -x "${ezjail_prefix}/etc/rc.d/ezjail.sh" ]; then
"${ezjail_prefix}/etc/rc.d/ezjail.sh" $@
else
exerr "Error: Could not find ezjail's rc.d script in ${ezjail_prefix}/etc/rc.d/.\n You need to ${ezjail_action} ${ezjail_name} by hand."
fi
# Check for success of our operation
shift
for ezjail; do
fetchjailinfo ${ezjail}
case ${ezjail_config} in *.norun) continue;; esac
[ ${ezjail_success_check} "${ezjail_id}" ] || exerr "Error: Could not ${ezjail_action} $@.\n You need to ${ezjail_action} it by hand."
done
}
# write everything we know about an ezjail to config
writejailinfo () {
ezjail_destconf=$1
ezjail_sourceconf=$2
(
if [ "${ezjail_sourceconf}" ]; then
grep -E ^\# ${ezjail_sourceconf}; echo
else
echo -e "# To specify the start up order of your ezjails, use these lines to\n# create a Jail dependency tree. See rcorder(8) for more details."
echo -e "#\n# PROVIDE: standard_ezjail\n# REQUIRE: \n# BEFORE: \n#\n"
fi
echo export jail_${ezjail_safename}_hostname=\"${ezjail_hostname}\"
echo export jail_${ezjail_safename}_ip=\"${ezjail_ips}\"
echo export jail_${ezjail_safename}_rootdir=\"${ezjail_rootdir}\"
echo export jail_${ezjail_safename}_exec_start=\"${ezjail_exec_start}\"
echo export jail_${ezjail_safename}_exec_stop=\"${ezjail_exec_stop}\"
echo export jail_${ezjail_safename}_mount_enable=\"${ezjail_mount_enable}\"
echo export jail_${ezjail_safename}_devfs_enable=\"${ezjail_devfs_enable}\"
echo export jail_${ezjail_safename}_devfs_ruleset=\"${ezjail_devfs_ruleset}\"
echo export jail_${ezjail_safename}_procfs_enable=\"${ezjail_procfs_enable}\"
echo export jail_${ezjail_safename}_fdescfs_enable=\"${ezjail_fdescfs_enable}\"
echo export jail_${ezjail_safename}_image=\"${ezjail_image}\"
echo export jail_${ezjail_safename}_imagetype=\"${ezjail_imagetype}\"
echo export jail_${ezjail_safename}_attachparams=\"${ezjail_attachparams}\"
echo export jail_${ezjail_safename}_attachblocking=\"${ezjail_attachblocking}\"
echo export jail_${ezjail_safename}_forceblocking=\"${ezjail_forceblocking}\"
echo export jail_${ezjail_safename}_zfs_datasets=\"${ezjail_zfs_datasets}\"
echo export jail_${ezjail_safename}_cpuset=\"${ezjail_cpuset}\"
echo export jail_${ezjail_safename}_fib=\"${ezjail_fib}\"
echo export jail_${ezjail_safename}_parentzfs=\"${ezjail_parentzfs}\"
echo export jail_${ezjail_safename}_parameters=\"${ezjail_parameters}\"
echo export jail_${ezjail_safename}_post_start_script=\"${ezjail_post_start_script}\"
echo export jail_${ezjail_safename}_retention_policy=\"${ezjail_retention_policy}\"
) > "${ezjail_destconf}"
}
# fetch everything we need to know about an ezjail from config
fetchjailinfo () {
ezjail_name=$1
# Clean variables, prevent pollution
unset ezjail_config ezjail_running ezjail_hostname ezjail_rootdir ezjail_image ezjail_imagetype ezjail_imagedevice ezjail_devicelink ezjail_ips ezjail_id ezjail_attached ezjail_device ezjail_device_geom ezjail_exec_start ezjail_exec_stop ezjail_mount_enable ezjail_devfs_enable ezjail_devfs_ruleset ezjail_procfs_enable ezjail_fdescfs_enable
ezjail_safename=`echo -n "${ezjail_name}" | tr -c '[:alnum:]' _`
if [ -z "$2" ]; then
[ -e "${ezjail_jailcfgs}/${ezjail_safename}" ] && ezjail_config="${ezjail_jailcfgs}/${ezjail_safename}"
[ -e "${ezjail_jailcfgs}/${ezjail_safename}.norun" ] && ezjail_config="${ezjail_jailcfgs}/${ezjail_safename}.norun"
else
ezjail_config=$2
fi
[ "${ezjail_config}" ] || return 0
. "${ezjail_config}"
eval ezjail_hostname=\"\$jail_${ezjail_safename}_hostname\"
eval ezjail_ips=\"\$jail_${ezjail_safename}_ip\"
eval ezjail_rootdir=\"\$jail_${ezjail_safename}_rootdir\"
eval ezjail_exec_start=\"\$jail_${ezjail_safename}_exec_start\"
eval ezjail_exec_stop=\"\$jail_${ezjail_safename}_exec_stop\"
# fix backward compatibility issue
eval ezjail_exec=\"\$jail_${ezjail_safename}_exec\"
[ "${ezjail_exec}" -a -z "${ezjail_exec_start}" ] && ezjail_exec_start=${ezjail_exec}
eval ezjail_mount_enable=\"\$jail_${ezjail_safename}_mount_enable\"
eval ezjail_devfs_enable=\"\$jail_${ezjail_safename}_devfs_enable\"
eval ezjail_devfs_ruleset=\"\$jail_${ezjail_safename}_devfs_ruleset\"
eval ezjail_procfs_enable=\"\$jail_${ezjail_safename}_procfs_enable\"
eval ezjail_fdescfs_enable=\"\$jail_${ezjail_safename}_fdescfs_enable\"
eval ezjail_image=\"\$jail_${ezjail_safename}_image\"
eval ezjail_imagetype=\"\$jail_${ezjail_safename}_imagetype\"
eval ezjail_attachparams=\"\$jail_${ezjail_safename}_attachparams\"
eval ezjail_attachblocking=\"\$jail_${ezjail_safename}_attachblocking\"
eval ezjail_forceblocking=\"\$jail_${ezjail_safename}_forceblocking\"
eval ezjail_zfs_datasets=\"\$jail_${ezjail_safename}_zfs_datasets\"
eval ezjail_cpuset=\"\$jail_${ezjail_safename}_cpuset\"
eval ezjail_fib=\"\$jail_${ezjail_safename}_fib\"
eval ezjail_parentzfs=\"\$jail_${ezjail_safename}_parentzfs\"
eval ezjail_parameters=\"\$jail_${ezjail_safename}_parameters\"
eval ezjail_post_start_script=\"\$jail_${ezjail_safename}_post_start_script\"
eval ezjail_retention_policy=\"\$jail_${ezjail_safename}_retention_policy\"
# Pre ezjail-3.3-jails do not have this set
: ${ezjail_parentzfs:=${ezjail_jailzfs}}
ezjail_softlink=${ezjail_jaildir}/`basename -- "${ezjail_rootdir}"`
ezjail_devicelink="${ezjail_rootdir}.device"
if [ "${ezjail_image}" -a -L "${ezjail_devicelink}" ]; then
# Fetch destination of soft link
ezjail_device=`stat -f "%Y" ${ezjail_devicelink}`
ezjail_device_geom=${ezjail_device#/dev/}
# Add this device to the list of devices to be unmounted
case ${ezjail_imagetype} in
crypto|bde) ezjail_imagedevice=${ezjail_device_geom%.bde} ;;
eli) ezjail_imagedevice=${ezjail_device_geom%.eli} ;;
zfs) ezjail_imagedevice='' ;;
*) ezjail_imagedevice=${ezjail_device_geom} ;;
esac
mount -p -v | grep -q -E "^${ezjail_devicelink}[[:space:]]+${ezjail_rootdir}" && ezjail_attached="YES"
mount -p -v | grep -q -E "^${ezjail_device}[[:space:]]+${ezjail_rootdir}" && ezjail_attached="YES"
# Stale device link detected. Remove and clean.
[ -z "${ezjail_attached}" ] && unset ezjail_device && rm -f "${ezjail_devicelink}"
fi
[ -f "/var/run/jail_${ezjail_safename}.id" ] && ezjail_id=`cat /var/run/jail_${ezjail_safename}.id` || return
jls | grep -q -E "^ *${ezjail_id} " || unset ezjail_id
}
# fill the base jail - this function is used by install and update
ezjail_splitworld() {
# Fill basejail from installed world
cd "${ezjail_jailfull}" || exerr "Error: Cant access temporary Jail directory."
if [ "${ezjail_use_zfs}" = "YES" ]; then
ensure_jailzfs
/sbin/zfs create ${ezjail_jailzfs}/basejail
/sbin/zfs snapshot ${ezjail_jailzfs}/basejail@`date +"%C%y%m%d_%H:%M:%S"`
fi
# Remove schg flags from old basejail binaries. The flags
# will be re-added in the cpio for the new basejail
[ -d "${ezjail_jailbase}" ] && chflags -R noschg "${ezjail_jailbase}"
# This mkdir is important, since cpio will create intermediate
# directories with permission 0700 which is bad
mkdir -p "${ezjail_jailbase}/usr"
for dir in ${ezjail_dirlist}; do
find ${dir} | cpio -d -p -v "${ezjail_jailbase}" || exerr "Error: Installation of ${dir} failed."
chflags -R noschg ${dir}; rm -r ${dir}; ln -s /basejail/${dir} ${dir}
done
mkdir basejail
# Try to remove the old template jail
if [ "${ezjail_use_zfs}" = "YES" ]; then
[ -d "${ezjail_jailtemplate}" ] && zfs destroy -R ${ezjail_jailzfs}/newjail && rm -rf "${ezjail_jailtemplate}"
cd ${ezjail_jaildir}
zfs rename ${ezjail_jailzfs}/fulljail ${ezjail_jailzfs}/newjail
else
[ -d "${ezjail_jailtemplate}" ] && chflags -R noschg "${ezjail_jailtemplate}" && rm -rf "${ezjail_jailtemplate}"
mv "${ezjail_jailfull}" "${ezjail_jailtemplate}"
fi
# If the default flavour example has not yet been copied, do it now
[ -d "${ezjail_flavours_dir}/example" ] || mkdir -p "${ezjail_flavours_dir}" && cp -p -R "${ezjail_examples}/example" "${ezjail_flavours_dir}"
# no /usr/ports? link to /basejail/usr/ports
[ -e "${ezjail_jailtemplate}/usr/ports" ] || ln -s /basejail/usr/ports "${ezjail_jailtemplate}/usr/ports"
# A ports collection inside jails is hardly useful w/o an appropriate
# /etc/make.conf.
if [ -f "${ezjail_examples}/example/etc/make.conf" -a ! -f "${ezjail_jailtemplate}/etc/make.conf" ]; then
cp -p "${ezjail_examples}/example/etc/make.conf" "${ezjail_jailtemplate}/etc/"
mkdir -p ${ezjail_jailtemplate}/var/ports/packages ${ezjail_jailtemplate}/var/ports/distfiles
echo "Note: a non-standard /etc/make.conf was copied to the template jail in order to get the ports collection running inside jails."
fi
# The ugly perl hack[tm]. Note: we wont do such things for any given
# port :(
[ "${ezjail_uglyperlhack}" -a ! -L "${ezjail_jailbase}/usr/bin/perl" ] && ln -s /usr/local/bin/perl "${ezjail_jailbase}/usr/bin/perl"
}
# The user may want to have a ports tree in basejail
# This section has been modified to remove the use of portsnap for HBSD
ezjail_updateports () {
#local _portsnap_fetch="fetch"
[ -z "$TERM" -o "$TERM" = "dumb" ] && _portsnap_fetch="cron"
#portsnap not used on HBSD
#portsnap ${_portsnap_fetch}
git --git-dir="${ezjail_jailbase}/usr/ports"/.git --work-tree="${ezjail_jailbase}/usr/ports" pull origin master
[ -d "${ezjail_jailbase}/usr/ports" ] && ezjail_portsnapaction="update"
#portsnap not used on HBSD
#portsnap -p "${ezjail_jailbase}/usr/ports" ${ezjail_portsnapaction:-"extract"} || exerr "Error: Updating ports failed."
git clone https://github.com/HardenedBSD/hardenedbsd-ports.git "${ezjail_jailbase}/usr/ports"
}
# Try to fetch the list of releases the server provides
ezjail_queryftpserver () {
unset _ret
echo -n "Querying your ftp-server... "
TIFS=${IFS}; IFS=
#Modding for HBSD use (adding HBSD tree)
#for ezjail_path in pub/FreeBSD/releases pub/FreeBSD/snapshot pub/FreeBSD releases snapshots pub/FreeBSD-Archive/old-releases NO; do
for ezjail_path in pub/HardenedBSD/releases; do
if [ ${ezjail_path} = "NO" ]; then
echo "Warning: I am having problems querying the ftp server you specified (${ezjail_disturi})."
_ret=1; break
fi
ezjail_ftpresponse=`echo ls | ftp "${ezjail_disturi}:${ezjail_path}/${ezjail_installarch}/" 2> /dev/null` 2> /dev/null
if [ $? -eq 0 ]; then
echo -e "The ftp server you specified (${ezjail_disturi}) seems to provide the following builds:\n${ezjail_ftpresponse}"
_ret=0; break
fi
done
IFS=${TIFS}
ezjail_ftpserverqueried="YES"
return ${_ret}
}
# Make a path absolute, if it isn't already
ezjail_makeabsolute ( ) {
eval [ \"\${$1%%[!/]*}\" -o \"\${$1}\" = \"-\" ] && return
[ "${2%%[!/]*}" ] && path=${2} || path=`pwd -P`/${2}
eval export ${1}="${path}/\${$1}"
}
parse_geli_attach_args () {
# create geli(8) attach arguments from geli(8) init arguments:
# -P becomes -p if present, -K newkeyfile becomes -k newkeyfile if present,
# everything else is discarded
# exit values: 0->NO PASSWORD SET, 1->PASSWORD SET
_exit=0
while getopts :bPva:i:K:l:s: arg; do
case ${arg} in
b|v|a|i|l|s);; # ignore these
P) echo -n "-p "; _exit=1 ;;
K) echo -n "-k '$OPTARG' ";;
?) exerr "Error: Processing of attach params failed.";;
esac
done
return ${_exit}
}
parse_gbde_attach_args () {
# create gbde(8) attach arguments from gbde(8) init arguments:
# -L lockfile becomes -l lockfile if present
# -K keyfile becomes -k keyfile if present
# -P passphrase becomes -p passphrase if present
# everything else is discarded
# exit values: 0->NO PASSWORD SET, 1->PASSWORD SET
_exit=0
while getopts :iK:f:L:P: arg; do
case ${arg} in
i|f);; # ignore these
P) echo -n "-p '$OPTARG' "; _exit=1;;
K) echo -n "-k '$OPTARG' ";;
L) echo -n "-l '$OPTARG' ";;
?) exerr "Error: Processing of attach params failed.";;
esac
done
return ${_exit}
}
check_for_zfs () {
# check if the ezjail zfs has been specified
[ -n "${ezjail_jailzfs}" ] || exerr "Error: The variable ezjail_jailzfs needs to point a zfs ezjail can work in.\n Set it in your ezjail.conf."
# check the ZFS version
_zfs_version=`sysctl -nq vfs.zfs.version.spa`
[ -z "${_zfs_version}" -o "${_zfs_version}" -lt 13 ] && exerr "Error: ZFS is not loaded or your ZFS version is not supported."
# check if ZFS is enabled when managing basejail/newjail in ZFS
. /etc/rc.subr
load_rc_config_var zfs zfs_enable
checkyesno ezjail_use_zfs && ! checkyesno zfs_enable && echo "Warning: You should enable ZFS in /etc/rc.conf"
}
check_for_zpool () {
# check for the ZFS zpool to be online
check_for_zfs
_zpoolstatus=`/sbin/zpool list -H -o health ${ezjail_jailzfs%%/*} 2> /dev/null`
[ "${_zpoolstatus}" = "DEGRADED" ] && echo "Warning: Your zpool is in a DEGRADED state."
[ "${_zpoolstatus}" = "ONLINE" -o "${_zpoolstatus}" = "DEGRADED" ] || exerr "Error: Your zpool does not exist or is not online."
}
check_for_zfs_exist () {
# check if the zfs we want to use already exists or not, return 0 if it does and 1 if not
_exit=1
_to_check=$1
_zfs_status=`/sbin/zfs list -H -o name ${_to_check} 2> /dev/null`
[ "${_zfs_status}" = "${_to_check}" ] && _exit=0
return ${_exit}
}
ensure_jailzfs() {
# make sure we do have a zfs to work with, create it if necessary or exit with an error
# if a plain non-empty directory in the place
# ensure that the system has a working zfs
check_for_zpool
# if a zfs already exists, we assume it to be the one we need
_zfs_status=`/sbin/zfs list -H -o name ${ezjail_jailzfs} 2> /dev/null`
[ "${_zfs_status}" = "${ezjail_jailzfs}" ] && return
# if a directory already exists in that place, make sure it is empty, else bump user
[ -d "${ezjail_jaildir}" -a -n "`ls -A ${ezjail_jaildir}`" ] && exerr "Error: Can not create zfs at ${ezjail_jaildir}.\n There is a non-empty directory in the way."
# create all parent file systems if necessary
case "${ezjail_jailzfs}" in */*/*) /sbin/zfs create -p ${ezjail_jailzfs%/*};; esac
# create the ezjail root zfs
# Note, we can not use -p here, because the -o options are ignored
/sbin/zfs create -o mountpoint=${ezjail_jaildir} ${ezjail_zfs_properties} ${ezjail_jailzfs}
}
# implement snapshot retentions
filteroldsnapshots() {
local win repeat bottom in_window snap_id snap_del snap_first first_round_done max_diff
bottom=`date +%s`
unset snap_del first_round_done
snap_id=0
for win in ${ezjail_retention_policy}; do
# split repeat count from window chunk
case ${win} in *x*) repeat=${win%x*}; win=${win#*x};; *) repeat=1;; KEEP) return;; esac
# check for correct value
case ${win} in [0-9]);; [0-9]*[0-9mhdwy]);; *) echo "Unknown window length declaration ${win}"; return ;; esac
# values default to minutes
case ${win} in *h) m=60;; *d) m=1440;; *w) m=10080;; *y) m=525600;; *) m=1;; esac
win=$((${win%[mhdwy]}*m*60))
# innerloop $repeats over windows
while [ $(( repeat-=1 )) -ge 0 ]; do
# Shift bottom of window
bottom=$(( ${bottom} - ${win} ))
unset snap_first
# now loop over parameters
in_window=YES; while [ "${in_window}" ]; do
# When snap_id is required, pop one if possible
if [ "${snap_id}" -eq 0 -a $# -gt 0 ]; then
snap_id=`date -j -f ${ezjail_snap_date_format} $1 +%s`
shift
fi
# is next snapshot before this window? test next window
if [ ${snap_id} -lt ${bottom} ]; then
[ "${first_round_done}" ] || echo /sbin/zfs snapshot -r ${ezjail_zfs}@ez-autosnap-`date +${ezjail_snap_date_format}`
[ "${first_round_done}" ] || /sbin/zfs snapshot -r ${ezjail_zfs}@ez-autosnap-`date +${ezjail_snap_date_format}`
# we remembered the first snapshot in window
# only keep it, if it is newer than the oldest by more than half the window size
if [ "${snap_first}" -a "${snap_del}" -a $(( snap_del - snap_first )) -lt $(( win / 2 )) ]; then
echo /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_first} +${ezjail_snap_date_format}`
/sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_first} +${ezjail_snap_date_format}`
fi
# Zero marks end of snaps list
[ "${snap_id}" -eq 0 ] && return
unset snap_del in_window
else
if [ "${snap_del}" ]; then
if [ "${snap_first}" ]; then
echo /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}`
/sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}`
else
snap_first="${snap_del}"
fi
fi
snap_del="${snap_id}"
snap_id=0
fi
first_round_done="YES"
done
done
done
# if out of windows, default to delete the remainder of snaps
for snap_del in ${snap_id} $*; do
echo /sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}`
/sbin/zfs destroy -r ${ezjail_zfs}@ez-autosnap-`date -j -f %s ${snap_del} +${ezjail_snap_date_format}`
done
}
#############################
# End of function definitions
# "
# check for command
[ $# -gt 0 ] || exerr ${ezjail_usage_ezjailadmin}
case "$1" in
######################## ezjail-admin CREATE ########################
create)
# Clean variables, prevent pollution
unset ezjail_rootdir ezjail_flavours ezjail_softlink ezjail_image ezjail_imagetype ezjail_imageparams ezjail_imagesize ezjail_parentzfs ezjail_device ezjail_devicelink ezjail_config ezjail_attachparams ezjail_exists ezjail_attachblocking ezjail_forceblocking ezjail_sourcedevice ezjail_rootdirempty ezjail_fromarchive ezjail_fromarchive_config
shift; while getopts :f:r:s:xbkic:C:a:A:z: arg; do case ${arg} in
x) ezjail_exists="YES";;
r) ezjail_rootdir=${OPTARG};;
f) ezjail_flavours=${OPTARG};;
a) ezjail_fromarchive=${OPTARG};;
A) ezjail_fromarchive_config=${OPTARG};;
c) ezjail_imagetype=${OPTARG};;
C) ezjail_imageparams=${OPTARG};;
b) ezjail_forceblocking="YES";;
i) : ${ezjail_imagetype="simple"};;
s) ezjail_imagesize=${OPTARG};;
z) ezjail_imagetype="zfs";
ezjail_parentzfs=${OPTARG};;
k) ezjail_keepsnapshot="YES";; # undocumented
?) exerr ${ezjail_usage_create};;
esac; done; shift $(( ${OPTIND} - 1 ))
ezjail_name=$1; ezjail_ips=$2
# we need at least a name and an ip for new jail
[ "${ezjail_name}" -a "${ezjail_ips}" -a $# -eq 2 ] || exerr ${ezjail_usage_create}
# check for sanity of settings concerning the image feature
if [ "${ezjail_imagetype}" != "zfs" ]; then
[ -z "${ezjail_imagetype}" -o "${ezjail_exists}" -o "${ezjail_imagesize}" ] || exerr "Error: Image jails need an image size."
fi
# If user wants jails to be in zfs by default, and did not override it on
# the command line, make the jail a zfs one
[ "${ezjail_use_zfs_for_jails}" = "YES" -a ! "${ezjail_imagetype}" ] && ezjail_imagetype=zfs
# check for an active ZFS zpool
[ "${ezjail_imagetype}" = "zfs" ] && check_for_zpool
# check for a sane image type
case ${ezjail_imagetype} in ""|simple|bde|eli|zfs) ;; *) exerr ${ezjail_usage_create};; esac
# check for a sane image size and split it up in blocks
if [ "${ezjail_imagesize}" ]; then
_val=`echo "${ezjail_imagesize}"|tr GMKBWX gmkbwx|sed -Ees:g:km:g -es:m:kk:g -es:k:"*2b":g -es:b:"*128w":g -es:w:"*4 ":g -e"s:(^|[^0-9])0x:\1\0X:g" -ey:x:"*":|bc`
[ $? -eq 0 -a ${_val} -gt 0 ] || exerr "Error: The image size you specified is somehow incomprehensible (you specified ${ezjail_imagesize})."
ezjail_imageblockcount=`echo ${_val} / 1048576 | bc`
ezjail_imagerestbytes=`echo ${_val} % 1048576 | bc`
fi
# check, whether ezjail has been set up correctly. existence of
# ezjail_jailbase is our indicator
[ -d "${ezjail_jailbase}" ] || exerr "Error: base jail does not exist.\n Please run '${ezjail_admin} install' or '${ezjail_admin} update' first."
# relative paths don't make sense in rc.scripts
[ "${ezjail_jaildir%%[!/]*}" ] || exerr "Error: Need an absolute path in ezjail_jaildir.\n It is currently set to: ${ezjail_jaildir}."
# jail names must not irritate file systems, excluding dots from this list
# was done intentionally to permit foo.com style directory names, however,
# the jail name will be foo_com in most scripts
ezjail_hostname=`echo -n "${ezjail_name}" | tr '/~' '__'`
ezjail_safename=`echo -n "${ezjail_name}" | tr -c '[:alnum:]' _`
: ${ezjail_rootdir="${ezjail_jaildir}/${ezjail_hostname}"}
ezjail_config="${ezjail_jailcfgs}/${ezjail_safename}"
# This scenario really will only lead to real troubles in the 'fulljail'
# case, but I should still explain this to the user and not claim that
# "an ezjail would already exist"
case ${ezjail_hostname} in basejail|newjail|fulljail|flavours|ezjailtemp|ezjail_archives) exerr "Error: Cannot name the jail ${ezjail_hostname}.\n ezjail needs the ${ezjail_hostname} directory for its own administrative purposes.\n Please rename the ezjail.";; esac
# jail names may lead to identical configs, eg. foo.bar.com == foo-bar.com
# so check, whether we might be running into problems
[ -e "${ezjail_config}" -o -e "${ezjail_config}.norun" ] && exerr "Error: An ezjail config already exists at ${ezjail_config}.\n This can happen because ezjail converts non alphanumeric characters in jail names to '_'.\n Please rename the ezjail."
# if jail root specified on command line is not absolute, make it absolute
# inside our jail directory
ezjail_makeabsolute ezjail_rootdir ${ezjail_jaildir}
# if a directory at the specified jail root already exists, refuse to
# install. Empty root dirs are considered okay, sometimes they are
# mount points to be filled by ezjail.
[ -d "${ezjail_rootdir}" ] && [ -z "`ls -I ${ezjail_rootdir}`" ] && ezjail_rootdirempty="YES"
[ -e "${ezjail_rootdir}" -a -z "${ezjail_rootdirempty}" -a -z "${ezjail_exists}" ] && exerr "Error: A file or a non empty directory already exists at the specified jail root ${ezjail_rootdir}.\n Maybe you want to '${ezjail_admin} create -x' an existing jail?\n Please specify another jail root with the -r switch."
# if jail root specified on command line does not lie within our jail
# directory, we need to create a softlink
if [ "${ezjail_rootdir##${ezjail_jaildir}}" = "${ezjail_rootdir}" ]; then
ezjail_softlink=${ezjail_jaildir}/`basename -- "${ezjail_rootdir}"`
[ -e "${ezjail_softlink}" ] && ezjail_softlink=`mktemp -u "${ezjail_softlink}.XXXXXX"`
fi
# if no flavour specified on command line, use default flavour
[ "${ezjail_fromarchive}" -o "${ezjail_exists}" ] || : ${ezjail_flavours=${ezjail_default_flavour}}
# do some sanity checks on the selected flavour (if any)
for ezjail_flavour in ${ezjail_flavours}; do
[ -d "${ezjail_flavours_dir}/${ezjail_flavour}" ] || exerr "Error: Flavour config directory ${ezjail_flavours_dir}/${ezjail_flavour} not found.\n Refer to ${ezjail_admin}s man page for details on flavours."
done
# check for restore circumstances, normally this is invoked by the restore command
[ "${ezjail_fromarchive}" -a "${ezjail_exists}" ] && exerr "Error: You can not restore an archive over an existing jail.\n '${ezjail_admin} delete -w ${ezjail_name}' the old version first."
[ "${ezjail_fromarchive}" -a "${ezjail_flavour}" ] && exerr "Error: Cannot apply flavours to a jail being restored."
[ "${ezjail_fromarchive}" -a "${ezjail_fromarchive}" != "-" -a ! -r "${ezjail_fromarchive}" ] && exerr "Error: No archive found at ${ezjail_fromarchive}."
# Ensure existence of our control directory
mkdir -p "${ezjail_jailcfgs}" || exerr "Error: ezjail can not create its control directory ${ezjail_jailcfgs}."
#
# All sanity checks that may lead to errors are hopefully passed here
#
if [ "${ezjail_imagetype}" ]; then
# Strip trailing slashes from jail root, those would confuse image path
ezjail_image=${ezjail_rootdir%/}; while [ "${ezjail_image}" -a -z "${ezjail_image%%*/}" ]; do ezjail_image=${ezjail_image%/}; done
[ "${ezjail_image}" ] || exerr "Error: Could not determine image file name.\n Something is wrong with the jail root: ${ezjail_rootdir}."
# Location of our image file
ezjail_image="${ezjail_image}.img"
# zfs does not use image files
[ "${ezjail_imagetype}" = "zfs" ] && unset ezjail_image
# Prepare crypto jail so that an attacker cannot guess which blocks
# have been written
case ${ezjail_imagetype} in bde|eli) ezjail_sourcedevice="/dev/random";; simple) ezjail_sourcedevice="/dev/zero";; esac
# If NOT exist and imagetype not ZFS, create image
if [ -z "${ezjail_exists}" -a ${ezjail_imagetype} != "zfs" ]; then
[ -e "${ezjail_image}" ] && exerr "Error: A file exists at ${ezjail_image}.\n Won't overwrite an existing image."
# Now create jail disc image
touch "${ezjail_image}"
echo "Creating jail image ${ezjail_image}. This may take a while."
if [ "${ezjail_imageblockcount}" -gt 0 ]; then
dd if="${ezjail_sourcedevice}" of="${ezjail_image}" bs=1m count=${ezjail_imageblockcount} || exerr "Error: Could not (or not fully) create the image file.\n You might want to check (and possibly remove) the file ${ezjail_image}.\n The image size provided was ${ezjail_imagesize}."
fi
if [ "${ezjail_imagerestbytes}" -gt 0 ]; then
( dd if="${ezjail_sourcedevice}" bs=${ezjail_imagerestbytes} count=1 >> "${ezjail_image}" ) || exerr "Error: Could not (or not fully) create the image file.\n You might want to check (and possibly remove) the file ${ezjail_image}.\n The image size provided was ${ezjail_imagesize}."
fi
# Attach device
ezjail_imagedevice=`mdconfig -a -t vnode -f "${ezjail_image}"`
ezjail_devicelink="${ezjail_rootdir}.device"
[ $? -eq 0 ] || detach_images || exerr "Error: Could not attach image device.\n Command failed was 'mdconfig -a -t vnode -f ${ezjail_image}'."
fi
case ${ezjail_imagetype} in
bde|eli)
# parse imageparams, generate attachparams
ezjail_attachblocking="YES"
if [ "${ezjail_imageparams}" ]; then
ezjail_attachparams=`eval parse_g${ezjail_imagetype}_attach_args ${ezjail_imageparams}` || unset ezjail_attachblocking
fi
case ${ezjail_imagetype} in
bde) init_cmd="gbde init /dev/${ezjail_imagedevice} ${ezjail_imageparams}"
attach_cmd="gbde attach /dev/${ezjail_imagedevice} ${ezjail_attachparams}";;
eli) init_cmd="geli init ${ezjail_imageparams} /dev/${ezjail_imagedevice}"
attach_cmd="geli attach ${ezjail_attachparams} /dev/${ezjail_imagedevice}";;
esac
if [ -z "${ezjail_exists}" ]; then
[ "${ezjail_attachblocking}" ] && echo "Initialising crypto device. You will be asked to enter a new passphrase twice... "
( echo ${init_cmd} | /bin/sh ) || detach_images || exerr "Error: Could not initialise crypto image."
[ "${ezjail_attachblocking}" ] && echo "Attaching crypto device. You will be asked to enter the new passphrase... "
( echo ${attach_cmd} | /bin/sh ) || detach_images || exerr "Error: Could not attach crypto image."
fi
ezjail_device="${ezjail_imagedevice}.${ezjail_imagetype}"
;;
simple)
ezjail_device=${ezjail_imagedevice}
;;
zfs)
: ${ezjail_parentzfs=${ezjail_jailzfs}}
if [ -z "${ezjail_exists}" ]; then
[ "${ezjail_imagesize}" ] && ezjail_zfs_jail_properties="${ezjail_zfs_jail_properties} -o quota=${ezjail_imagesize}"
[ -d "${ezjail_jaildir}/${ezjail_hostname}" ] && exerr "Error: Could not create jail root mount point ${ezjail_rootdir}"
check_for_zfs_exist "${ezjail_parentzfs}" || exerr "Error: The parent zfs dataset does not exist.\n Use 'zfs create -p ${ezjail_parentzfs}' to create it."
/sbin/zfs create -o mountpoint=${ezjail_rootdir} ${ezjail_zfs_jail_properties} ${ezjail_parentzfs}/${ezjail_hostname}
else
check_for_zfs_exist "${ezjail_parentzfs}/${ezjail_hostname}" || exerr "Error: The existing destination is not a ZFS filesystem."
fi
;;
esac
if [ -z "${ezjail_exists}" -a ${ezjail_imagetype} != "zfs" ]; then
# Format memory image
newfs -U "/dev/${ezjail_device}" || detach_images || exerr "Error: Could not newfs /dev/${ezjail_device}."
# Create mount point and mount
mkdir -p "${ezjail_rootdir}" || detach_images || exerr "Error: Could not create jail root mount point ${ezjail_rootdir}."
mount "/dev/${ezjail_device}" "${ezjail_rootdir}" || detach_images || exerr "Error: Could not mount /dev/${ezjail_device} to ${ezjail_root}."
else
if [ -e "${ezjail_rootdir}" -a ! -d "${ezjail_rootdir}" ]; then
[ "${ezjail_rootdir%%*.img}" ] || exerr "Error: Could not create mount point for your jails image.\n A file exists at its location.\n Try '${ezjail_admin} create -x -r ${ezjail_rootdir%%.img} ${ezjail_name} ${ezjail_ips}' instead."
exerr "Error: Could not create mount point for your jails image.\n A file exists at its location."
fi
[ -d "${ezjail_rootdir}" ] || mkdir -p "${ezjail_rootdir}"
fi
fi
if [ "${ezjail_fromarchive}" ]; then
unset ezjail_archive_opt
ezjail_makeabsolute ezjail_fromarchive
[ "${ezjail_fromarchive}" = "-" ] && unset ezjail_archive_opt || ezjail_archive_opt="-f ${ezjail_fromarchive}"
mkdir -p "${ezjail_rootdir}" && cd "${ezjail_rootdir}" && pax -rz -pe ${ezjail_archive_opt} -s:^ezjail:.: ezjail/*
[ $? -eq 0 ] || detach_images || exerr "Error: Could not extract archive from ${ezjail_fromarchive}."
elif [ -z "${ezjail_exists}" ]; then
# now take a copy of our template jail
if [ "${ezjail_imagetype}" = "zfs" -a "${ezjail_use_zfs}" = "YES" ]; then
# create ZFS filesystem first when using ZFS
/sbin/zfs snapshot ${ezjail_jailzfs}/newjail@_vanilla_newjail_
/sbin/zfs send ${ezjail_jailzfs}/newjail@_vanilla_newjail_ | zfs receive -F ${ezjail_parentzfs}/${ezjail_hostname}
[ -z "${ezjail_keepsnapshot}" ] && /sbin/zfs destroy ${ezjail_parentzfs}/${ezjail_hostname}@_vanilla_newjail_
/sbin/zfs destroy ${ezjail_jailzfs}/newjail@_vanilla_newjail_
else
mkdir -p "${ezjail_rootdir}" && cd "${ezjail_jailtemplate}" && find . | cpio -p -v "${ezjail_rootdir}" > /dev/null
fi
[ $? -eq 0 ] || detach_images || exerr "Error: Could not copy template jail."
fi
# if a soft link is necessary, create it now
[ "${ezjail_softlink}" ] && ln -fs "${ezjail_rootdir}" "${ezjail_softlink}"
# if the automount feature is not disabled, this fstab entry for new jail
# will be obeyed
echo -n > /etc/fstab.${ezjail_safename}
if [ "${ezjail_imagetype}" -a "${ezjail_imagetype}" != "zfs" ] ; then
echo ${ezjail_devicelink} ${ezjail_rootdir} ufs rw 0 0 >> "/etc/fstab.${ezjail_safename}"
fi
echo ${ezjail_jailbase} ${ezjail_rootdir}/basejail nullfs ro 0 0 >> "/etc/fstab.${ezjail_safename}"
# now, where everything seems to have gone right, create control file in
# ezjails config dir
writejailinfo "${ezjail_config}" "${ezjail_fromarchive_config}"
# Final steps for flavour installation
if [ -z "${ezjail_exists}" ]; then
installed_flavours=0
for ezjail_flavour in ${ezjail_flavours}; do
# install files and config to new jail
cd "${ezjail_flavours_dir}/${ezjail_flavour}" && find . | cpio -p -u -v "${ezjail_rootdir}" > /dev/null
[ $? -eq 0 ] || echo "Warning: Could not fully install flavour ${ezjail_flavour}."
# if the packages are links and not files we have to copy them now
find "${ezjail_rootdir}/pkg/" -type l -exec cp -r -f {} {}.ezjail \; -exec mv {}.ezjail {} \;
# If an old style flavour config is found, make it auto run on jails startup
if [ -f "${ezjail_rootdir}/ezjail.flavour" ]; then
chmod 0755 "${ezjail_rootdir}/ezjail.flavour"
mv "${ezjail_rootdir}/ezjail.flavour" "${ezjail_rootdir}/ezjail.flavour".`printf %04d ${installed_flavours}`
[ $(( installed_flavours+=1 )) == 1 ] && echo "Note: Shell scripts for flavour ${ezjail_flavour} installed, flavourizing on jails first startup."
cat > "${ezjail_rootdir}/etc/rc.d/ezjail-config" <<"EOF"
#!/bin/sh
#
# BEFORE: DAEMON
# PROVIDE: ezjail-config
#
name=ezjail-config
start_cmd=flavour_setup
flavour_setup() {
# N.B.: Do NOT rm $0, it points to /etc/rc
rm -f "/etc/rc.d/ezjail-config"
for ezjail_flavour in /ezjail.flavour.*; do
[ -x "${ezjail_flavour}" ] && "${ezjail_flavour}"
rm -f "${ezjail_flavour}"
done
}
run_rc_command "$1"
EOF
chmod 0755 "${ezjail_rootdir}/etc/rc.d/ezjail-config"
fi
done
fi
# Detach (crypto and) memory discs
detach_images keep
#
# For user convenience some scenarios commonly causing headaches are checked
#
TIFS=${IFS}; IFS=,
for ezjail_ip_in in ${ezjail_ips}; do
# From 9.0 IP addresses can be prefixed by their interface, for now ignore
# the prefix
ezjail_ip="${ezjail_ip_in#*|}"
# If the IP address is not automatically configured, test if it is configured
# on a local interface
if [ "${ezjail_ip}" = "${ezjail_ip_in}" ]; then
case ${ezjail_ip} in *.*.*.*) _ping=ping;; *) _ping=ping6;; esac
# check, whether IP is configured on a local interface, warn if it isnt
${_ping} -S ${ezjail_ip} -q -c 1 localhost >/dev/null 2>/dev/null
[ $? -eq 0 ] || echo "Warning: IP ${ezjail_ip} not configured on a local interface."
fi
# check, whether some host system services do listen on the Jails IP
IFS=_
ezjail_listener=`sockstat -64l | grep "${ezjail_ip}:[[:digit:]]"`
[ $? -eq 0 ] && echo -e "Warning: Some services already seem to be listening on IP ${ezjail_ip}\n This may cause some confusion, here they are:\n${ezjail_listener}"
# collect list of jail ids with our ip address in their ip address set.
# Add none meaning the host system. Prepare this list as argument for pgrep
# by prepending -j to each jid
IFS=${TIFS}
_freebsd_version=`uname -r`
if [ ${_freebsd_version%%.*} -gt 7 ]; then
jail_ids=`( echo none=
jls -n | sed -E -n s/'.*jid=([0-9]+).*ip4\.addr=([0-9.,]+).*'/'\1=\2'/p | grep -Ee "${ezjail_ip}(,|$)"
jls -n | sed -E -n s/'.*jid=([0-9]+).*ip6\.addr=([0-9a-f:,]+).*'/'\1=\2'/p | grep -Ee "${ezjail_ip}(,|$)"
) | cut -d= -f1 | sed s/^/-j/`
# Fetch all corresponding process ids for all matching jail
jail_pids=`pgrep $jail_ids`
# expand pids to form a greppable expression
jail_grep=`echo $jail_pids | sed -E -e"s/ / )|( /g" -e"s/^/( /" -e"s/$/ )/"`
IFS=_
else
jail_grep=.
fi
ezjail_listener=`sockstat -46l | grep -E -e "\*:[[:digit:]]" | grep -E -e "${jail_grep}"`
[ $? -eq 0 ] && echo -e "Warning: Some services already seem to be listening on all IP, (including ${ezjail_ip})\n This may cause some confusion, here they are:\n${ezjail_listener}"
IFS=,
done
IFS=${TIFS}
[ "${ezjail_imagetype}" -a "${ezjail_imagetype}" != "zfs" ] && echo "Note: To administrate your image jail, attach it using the '${ezjail_admin} config -i attach ${ezjail_hostname}' command."
;;
######################## ezjail-admin DELETE ########################
delete)
# Clean variables, prevent pollution
unset ezjail_wipeme ezjail_forcestop
shift; while getopts :wf arg; do case ${arg} in
w) ezjail_wipeme="YES";;
f) ezjail_forcestop="YES";;
?) exerr ${ezjail_usage_delete};;
esac; done; shift $(( $OPTIND - 1 ))
# we need name of jail to vanish
[ $# -eq 1 ] || exerr ${ezjail_usage_delete}
# Get all info we have on that jail
fetchjailinfo $1
# check for existence of jail in our records
[ "${ezjail_config}" ] || exerr "Error: Nothing known about jail ${ezjail_name}."
# check for an active ZFS zpool
[ "${ezjail_imagetype}" = "zfs" ] && check_for_zpool
if [ "${ezjail_id}" ]; then
# if jail is still running, refuse to go any further
[ "${ezjail_forcestop}" ] || exerr "Error: Jail appears to be still running.\n '${ezjail_admin} stop ${ezjail_name}' it first or use '${ezjail_admin} delete -f ${ezjail_name}' to force stop."
# This one will also exerr on failure
start_stop_jail_by_script onestop ${ezjail_name}
fi
if [ "${ezjail_attached}" ]; then
# if jail is attached and detach is not forced, refuse to go any further
[ "${ezjail_forcestop}" ] || exerr "Error: Jail image file ${ezjail_image} is attached as ${ezjail_device}.\n '${ezjail_admin} config -i detach ${ezjail_name}' it first, or use '${ezjail_admin} delete -f ${ezjail_name}' to force detach."
detach_images keep
# See, if it successfully detached
fetchjailinfo ${ezjail_name}
[ "${ezjail_attached}" ] && exerr "Error: Could not detach ${ezjail_name}.\n You need to detach it by hand."
fi
# now we know everything we need to let the jail be gone. remove entry
# from ezjail resource structure, delete fstab.JAILNAME
rm -f "${ezjail_config}" "/etc/fstab.${ezjail_safename}"
# if there is a soft link pointing to the jail root, remove it
[ -L "${ezjail_softlink}" ] && rm -f "${ezjail_softlink}"
# if wiping the jail was requested, remove it
if [ "${ezjail_wipeme}" ]; then
case ${ezjail_imagetype} in
simple|bde|eli)
[ "${ezjail_image}" ] && rm -f "${ezjail_image}" "${ezjail_image%.img}.device"
;;
zfs)
if ! /sbin/zfs destroy -r ${ezjail_parentzfs}/${ezjail_hostname}; then
[ "${ezjail_forcestop}" ] || exerr "Error: Could not destroy file system ${ezjail_parentzfs}/${ezjail_hostname} for jail ${ezjail_name}."
echo "Warning: Could not destroy file system ${ezjail_parentzfs}/${ezjail_hostname} for jail ${ezjail_name}."
echo " Sleeping 5 seconds before retrying forcefully."
sleep 5
/sbin/zfs destroy -rf ${ezjail_parentzfs}/${ezjail_hostname} || exerr "Error: Failed to destroy file system."
echo "Success."
fi
;;
*)
chflags -R noschg "${ezjail_rootdir}"
;;
esac
rm -rf "${ezjail_rootdir}"
fi
;;
######################## ezjail-admin LIST ########################
list)
[ $# -eq 1 ] || exerr ${ezjail_usage_list}
unset ezjail_list
cd ${ezjail_jailcfgs} && ezjail_list=`ls -A | xargs rcorder`
printf "%-3s %-4s %-15s %-30s %s\\n" STA JID IP Hostname "Root Directory"
echo "--- ---- --------------- ------------------------------ ------------------------"
for ezjail in ${ezjail_list}; do
fetchjailinfo ${ezjail%.norun}
case ${ezjail_imagetype} in simple) ezjail_state="I";; bde) ezjail_state="B";; eli) ezjail_state="E";; zfs) ezjail_state="Z";; *) ezjail_state="D";; esac
if [ "${ezjail_id}" ]; then
ezjail_state="${ezjail_state}R"
else
[ "${ezjail_attached}" ] && ezjail_state="${ezjail_state}A" || ezjail_state="${ezjail_state}S"
fi
[ "${ezjail_safename}" != "${ezjail}" ] && ezjail_state="${ezjail_state}N"
TIFS=${IFS}; IFS=,; unset _multiline
for ezjail_ip in ${ezjail_ips:="-"}; do
if [ -z "${_multiline}" ]; then
printf "%-3s %-4s %-15s %-30s %s\\n" "${ezjail_state}" "${ezjail_id:-N/A}" "${ezjail_ip#*|}" "${ezjail_hostname}" "${ezjail_rootdir}"
_multiline=yes
else
printf " %-4s %s\\n" "${ezjail_id:-N/A}" "${ezjail_ip}"
fi
done
IFS=${TIFS}
done
;;
######################## ezjail-admin UPDATE ########################
setup|update)
# Clean variables, prevent pollution
unset ezjail_provideports ezjail_installaction ezjail_osversion_source ezjail_osversion_destination ezjail_source
shift; while getopts :biuUpPs: arg; do case ${arg} in
b) ezjail_installaction="buildworld installworld";;
i) ezjail_installaction="installworld";;
u) ezjail_installaction="hbsd-update";;
U) ezjail_installaction="hbsd-update-build";;
s) ezjail_source=${OPTARG};;
P) ezjail_provideports="YES"; ezjail_installaction="none";;
p) ezjail_provideports="YES";;
?) exerr ${ezjail_usage_update};;
esac; done; shift $(( ${OPTIND} - 1 ))