-
Notifications
You must be signed in to change notification settings - Fork 0
/
jailer.sh
executable file
·1199 lines (1063 loc) · 39.7 KB
/
jailer.sh
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
# shellcheck disable=SC2039,SC2059,SC2181
########################
## Variabledefinition ##
########################
# remove comment for "Debug" mode
#set -x
# jailer configuration file
JAILER_CONF_DIR="/usr/local/etc"
# template directory
JAILER_TEMPLATE_DIR="/usr/local/share/jailer"
# ANSI Color Codes
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
BLUE="\033[0;34m"
PURPLE="\033[0;35m"
CYAN="\033[0;36m"
WHITE="\033[0;37m"
# Bold an underline
BOLD="\033[1m"
UNDERLINE="\033[4m"
# End ofANSI Code
ANSI_END="\033[0m"
# define messagestrings
INFO_STRING=""
ERROR_STRING="[ERROR]"
WARN_STRING="[WARN]"
# Program basename
PGM="${0##*/}" # Program basename
VERSION="2.5"
# Number of arguments
ARG_NUM=$#
# Global exit status
SUCCESS=0
FAILURE=1
# Default netmask for VNET jails
# can be overwritten in jailer.conf
JAIL_NETMASK="24"
# Create zfs pools with/without compression
# can be overwritten in jailer.conf
ZFS_COMPRESSION="YES"
# Install the pkg tool at jail creation
INSTALL_PKGTOOL="YES"
# Default pkgbase repositoryname,
# can be overwritten in jailer.conf
REPO_NAME="FreeBSD-pkgbase"
# Repository where the "official" packages are hosted,
# defaults to the FreeBSD Projects repository. When a
# poudriere repo is hosted change the in jailer.conf
OFFICIAL_REPO_NAME="FreeBSD"
# Write actions of the script to a logfile: YES/NO
# DEFAULT: NO
WRITE_LOGFILE="NO"
# Make jail managable via ansible by
# - install python37 package
# - adding an user ansible (uid: 3456, group :wheel, password login: disabled)
# Defaults: see jailer.conf
ENABLE_ANSIBLE="NO"
# enable sshd
# DEFAULT: NO
ENABLE_SSHD="NO"
# load configuration file
# default values
# check for config file
if [ ! -f "${JAILER_CONF_DIR}/jailer.conf" ]; then
printf "${RED}${ERROR_STRING}${ANSI_END} config file ${BOLD}${WHITE}${JAILER_CONF_DIR}/jailer.conf${ANSI_END} does not exist!"
exit ${FAILURE}
else
# shellcheck source=/dev/null
. "${JAILER_CONF_DIR}/jailer.conf"
fi
# initialise variables
JAIL_NAME=""
JAIL_CONF="/etc/jail.conf"
JAIL_IP=""
JAIL_UUID=$( uuidgen )
NAME_SERVER=$( local-unbound-control list_forwards | awk '/^. IN/ {print $NF}' )
LOG_FILE=""
ABI_VERSION=$( pkg config abi )
PKGS=""
SERVICES=""
COPY_FILES=""
AUTO_START="NO"
BASE_UPDATE="NO"
PKG_UPDATE="NO"
PKG_QUIET=""
ADD_USER="NO"
USER_NAME=""
USER_ID=3001
VNET="NO"
MINIJAIL="NO"
INTERFACE_ID=0
USE_PAGER="NO"
# if domainname is not set in jailer.conf
# set it to hosts domain.name
# can be overwritten with the parameter -d
if [ "${JAIL_DOMAINNAME}" = "" ] ; then
JAIL_DOMAINNAME=$( hostname -d )
fi
##################################
## functions ##
##################################
#
# check
#
checkResult()
{
if [ "$1" -eq 0 ]; then
printf "${GREEN}[OK]${ANSI_END}\n"
else
printf "${RED}${ERROR_STRING}${ANSI_END}\n"
fi
}
#
# decipher the programm arguments
#
get_args()
{
while getopts "a:c:d:e:h:i:m:N:P:r:t:u:AblMpqsSv" option
do
case $option in
a)
if [ ! X"${OPTARG}" = "X" ]; then
ABI_VERSION=${OPTARG}
fi
;;
A)
ENABLE_ANSIBLE="YES"
;;
b)
BASE_UPDATE="YES"
;;
c)
if [ ! X"${OPTARG}" = "X" ]; then
COPY_FILES=${OPTARG}
fi
;;
d)
if [ ! X"${OPTARG}" = "X" ]; then
JAIL_DOMAINNAME=${OPTARG}
fi
;;
e)
if [ ! X"${OPTARG}" = "X" ]; then
SERVICES="${SERVICES} ${OPTARG}"
fi
;;
h)
if [ ! X"${OPTARG}" = "X" ]; then
JAIL_HOSTNAME=${OPTARG}
fi
;;
i)
if expr "${OPTARG}" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' > /dev/null; then
JAIL_IP=${OPTARG}
else
printf "${RED}${ERROR_STRING}${ANSI_END} Invalid IP address ${BOLD}${WHITE}${OPTARG}${ANSI_END}\n"
exit 2
fi
;;
l)
USE_PAGER="YES"
;;
m)
if [ ! X"${OPTARG}" = "X" ]; then
if ( echo "${OPTARG}" | grep -E -q '^(254|252|248|240|224|192|128)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)' ); then
JAIL_NETMASK=" netmask ${OPTARG}"
elif [ ! -z "${OPTARG##*[!0-9]*}" ] && [ "${OPTARG}" -ge 0 ] && [ "${OPTARG}" -le 30 ] 2>/dev/null; then
JAIL_NETMASK="${OPTARG}"
else
printf "${RED}${ERROR_STRING}${ANSI_END} invalid netmask: ${BOLD}${WHITE}${OPTARG}${ANSI_END}\n"
exit 1
fi
fi
;;
M)
MINIJAIL="YES"
;;
N)
if expr "${OPTARG}" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' > /dev/null; then
NAME_SERVER=${OPTARG}
else
printf "${RED}${ERROR_STRING}${ANSI_END} Invalid IP address for nameserver ${BOLD}${WHITE}${OPTARG}${ANSI_END}\n"
exit 2
fi
;;
p)
PKG_UPDATE="TRUE"
;;
P)
if [ ! X"${OPTARG}" = "X" ]; then
PKGS=${OPTARG}
else
printf "${BLUE}${INFO_STRING}${ANSI_END}No packages specified.\n"
fi
;;
r)
if [ ! X"${OPTARG}" = "X" ]; then
REPO_NAME=${OPTARG}
check_repo
else
printf "${BLUE}${INFO_STRING}${ANSI_END}No repository specified, using default ${BOLD}${WHITE}${REPO_NAME}${ANSI_END}\n"
fi
;;
q)
PKG_QUIET="--quiet"
;;
s)
AUTO_START="YES"
;;
S)
ENABLE_SSHD="YES"
;;
t)
if [ ! X"${OPTARG}" = "X" ]; then
TIME_ZONE=${OPTARG}
else
printf "${BLUE}${INFO_STRING}${ANSI_END}No timezone specified, using default ${BOLD}${WHITE}${TIME_ZONE}${ANSI_END}\n"
fi
;;
u)
if [ ! X"${OPTARG}" = "X" ]; then
USER_NAME=${OPTARG}
ADD_USER="YES"
fi
;;
v)
JAIL_TEMPLATE="jail-vnet.template"
VNET="YES"
;;
*)
;;
esac
done
shift $((OPTIND - 1))
}
#
# print the usage message
#
usage()
{
### NAME
printf "${BOLD}NAME${ANSI_END}\n"
printf " ${BOLD}${PGM}${ANSI_END} – create, destroy and update FreeBSD jails\n"
echo ""
### SYNOPSIS
printf "${BOLD}SYNOPSIS${ANSI_END}\n"
printf " ${BOLD}${PGM} create jailname${ANSI_END} ${BOLD}-i${ANSI_END} ${UNDERLINE}ipaddress${ANSI_END} [${BOLD}-h${ANSI_END} ${UNDERLINE}hostname${ANSI_END} ${BOLD}-d${ANSI_END} ${UNDERLINE}domainname${ANSI_END} ${BOLD}-t${ANSI_END} ${UNDERLINE}timezone${ANSI_END} ${BOLD}-r${ANSI_END} ${UNDERLINE}reponame${ANSI_END} ${BOLD}-n${ANSI_END} ${UNDERLINE}ipaddress${ANSI_END} ${BOLD}-v -P${ANSI_END} ${UNDERLINE}\"list of packages\"${ANSI_END} ${BOLD}-a${ANSI_END} ${UNDERLINE}ABI_Version${ANSI_END} ${BOLD}-e${ANSI_END} ${UNDERLINE}\"list of services\"${ANSI_END} ${BOLD}-s -q -o${ANSI_END}]\n" "${PGM}"
printf " ${BOLD}${PGM} destroy${ANSI_END} ${UNDERLINE}jailname${ANSI_END}\n"
printf " ${BOLD}${PGM} update${ANSI_END} ${UNDERLINE}jailname${ANSI_END} [-${BOLD}b -p -s${ANSI_END}]\n"
printf " ${BOLD}${PGM} list${ANSI_END}\n"
printf " ${BOLD}${PGM} start${ANSI_END} [${UNDERLINE}jailname${ANSI_END}]\n"
printf " ${BOLD}${PGM} stop${ANSI_END} [${UNDERLINE}jailname${ANSI_END}]\n"
printf " ${BOLD}${PGM} restart${ANSI_END} [${UNDERLINE}jailname${ANSI_END}]\n"
printf " ${BOLD}${PGM} reloadpf${ANSI_END}\n"
printf " ${BOLD}${PGM} login${ANSI_END} ${UNDERLINE}jailname${ANSI_END}\n"
printf " ${BOLD}${PGM} exec${ANSI_END} ${UNDERLINE}jailname${ANSI_END} ${UNDERLINE}command${ANSI_END}\n"
printf " ${BOLD}${PGM} repos${ANSI_END} ${UNDERLINE}jailname${ANSI_END}\n"
printf " ${BOLD}${PGM} help${ANSI_END} [${BOLD}-l${ANSI_END}]\n"
echo ""
### DESCRIPTION
printf "${BOLD}DESCRIPTION${ANSI_END}\n"
printf "\tThe ${BOLD}${PGM}${ANSI_END} command creates, destroys and controls FreeBSD jails build from a pkgbase or basecore repositories.\n"
echo ""
printf " ${BOLD}${PGM} create jailname${ANSI_END} ${BOLD}-i${ANSI_END} ${UNDERLINE}ipaddress${ANSI_END} [${BOLD}-t${ANSI_END} ${UNDERLINE}timezone${ANSI_END} ${BOLD}-r${ANSI_END} ${UNDERLINE}reponame${ANSI_END} ${BOLD}-n${ANSI_END} ${UNDERLINE}ipaddress${ANSI_END} ${BOLD}-v -P${ANSI_END} ${UNDERLINE}\"list of packages\"${ANSI_END} ${BOLD}-a${ANSI_END} ${UNDERLINE}ABI_Version${ANSI_END} ${BOLD}-e${ANSI_END} ${UNDERLINE}\"list of services\"${ANSI_END} ${BOLD}-s -q${ANSI_END}]\n"
printf "\t${BOLD}-i${ANSI_END} ${UNDERLINE}ipaddress${ANSI_END}\n\t\tSet IP address of jail.\n"
echo ""
printf "\t${BOLD}-m${ANSI_END} ${UNDERLINE}netmask${ANSI_END}\n\t\tSet netmask of VNET ip address. use CIDR form (e.g. /24) or \"longform\" (e.g. 255.255.255.0)\n"
echo ""
printf "\t${BOLD}-t${ANSI_END} ${UNDERLINE}timezone${ANSI_END}\n\t\tSet Timezone of jail.\n"
echo ""
printf "\t${BOLD}-N${ANSI_END} ${UNDERLINE}ipaddress${ANSI_END}\n\t\tSet DNS server IP address of jail.\n"
echo ""
printf "\t${BOLD}-v${ANSI_END} \tcreate a VNET jail.\n"
echo ""
printf "\t${BOLD}-S${ANSI_END} \tenable SSH daemon in jail.\n"
echo ""
printf "\t${BOLD}-r${ANSI_END} ${UNDERLINE}reponame${ANSI_END}\n\t\tSet pkg repository of jail.\n"
echo ""
printf "\t${BOLD}-u${ANSI_END} ${UNDERLINE}username${ANSI_END}\n\t\tCreate user ${UNDERLINE}username${ANSI_END} in jail with uid ${USER_ID}. If -u argument is used, a value is mandatory.\n\t\tNOTE: password = username\n"
echo ""
printf "\t${BOLD}-h${ANSI_END} ${UNDERLINE}hostname${ANSI_END}\n\t\tSet hostname of the jail. If not set, the jailname is used.\n"
echo ""
printf "\t${BOLD}-d${ANSI_END} ${UNDERLINE}domainname${ANSI_END}\n\t\tSet domainname of the jail. If not set, the domain of the host is used.\n"
echo ""
printf "\t${BOLD}-A${ANSI_END} \tprepare jail to be managed by ansible, so install necessary packages, create user ansible and load the ssh key to the jail.\n"
echo ""
printf "\t${BOLD}-P${ANSI_END} ${UNDERLINE}\"list of packages\"${ANSI_END}\n\t\tPackages to install in the jail, the list is seperated by whitespace.\n"
echo ""
printf "\t${BOLD}-c${ANSI_END} ${UNDERLINE}\"source_file:target_file(,...)\"${ANSI_END}\n\t\tCopy files INTO the jail.\n"
echo ""
printf "\t\t%s\n" "NOTE:"
printf "\t\t%s\n" " • use a comma seperated list for multiple copies"
printf "\t\t%s\n" " • consider beginning and trailing slashes"
printf "\t\t%s\n" " • consider the file permissions"
printf "\t\t%s\n" " • consider whitespaces in the parameter string"
echo ""
printf "\t${BOLD}-a${ANSI_END} ${UNDERLINE}ABI_Version${ANSI_END}\n\t\tSet the ABI Version to match the packages to be installed to the jail.\n"
echo ""
printf "\t\t%s\n" "NOTE: Possible values for ABI_VERSION"
printf "\t\t%s\n" " • FreeBSD:13:amd64"
printf "\t\t%s\n" " • FreeBSD:14:amd64"
echo ""
printf "\t${BOLD}-M${ANSI_END}\tuse minimal basecore package. (see: ${UNDERLINE}https://github.com/mjakob-gh/create-basecore-pkg${ANSI_END})\n"
echo ""
printf "\t${BOLD}-e${ANSI_END} ${UNDERLINE}\"list of services\"${ANSI_END}\n\t\tEnable existing or just now installed services (see -P parameter), the list is seperated by whitespace.\n"
echo ""
printf "\t${BOLD}-s${ANSI_END}\tStart the jail after the installation is finished.\n"
echo ""
printf "\t${BOLD}-q${ANSI_END}\tDo not show output of pkg.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} destroy${ANSI_END} ${UNDERLINE}jailname${ANSI_END}\n\t\tThe jail is stopped, the dataset destroyed and the ${UNDERLINE}jailname${ANSI_END} removed from jail.conf\n"
echo ""
echo ""
printf " ${BOLD}${PGM} update${ANSI_END} ${UNDERLINE}jailname${ANSI_END} [-${BOLD}b -p -s${ANSI_END}]\n"
printf "\t${BOLD}-b${ANSI_END}\tUpdate the pkgbase system.\n"
echo ""
printf "\t${BOLD}-p${ANSI_END}\tUpdate the installed packages.\n"
echo ""
printf "\t${BOLD}-s${ANSI_END}\tRestart jail after update.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} list${ANSI_END}\n\t\tList status of all running jails.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} start${ANSI_END} [${UNDERLINE}jailname${ANSI_END}]\n\t\tStart all jails, or only given ${UNDERLINE}jailname${ANSI_END}.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} stop${ANSI_END} [${UNDERLINE}jailname${ANSI_END}]\n\t\tStop all jails, or only given ${UNDERLINE}jailname${ANSI_END}.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} restart${ANSI_END} [${UNDERLINE}jailname${ANSI_END}]\n\t\tRestart all jails, or only given ${UNDERLINE}jailname${ANSI_END}.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} reloadpf${ANSI_END}\n\t\tReload pf firewall rules.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} login${ANSI_END} ${UNDERLINE}jailname${ANSI_END}\n\t\tLogin to ${UNDERLINE}jailname${ANSI_END} as user root.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} exec${ANSI_END} ${UNDERLINE}jailname${ANSI_END} ${UNDERLINE}command${ANSI_END}\n\t\tExecutes ${UNDERLINE}command${ANSI_END} inside the jail ${UNDERLINE}jailname${ANSI_END}.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} repos${ANSI_END} ${UNDERLINE}jailname${ANSI_END}\n\t\tview the repos configured inside the jail ${UNDERLINE}jailname${ANSI_END}.\n"
echo ""
echo ""
printf " ${BOLD}${PGM} help${ANSI_END} [${BOLD}-l${ANSI_END}]\n\t\tPrint this help message.\n"
printf "\t${BOLD}-l${ANSI_END}\tOpen help message in pager.\n"
echo ""
### DESCRIPTION
printf "${BOLD}DESCRIPTION${ANSI_END}\n"
printf "\tThe ${BOLD}${PGM}${ANSI_END} command creates, destroys and manages FreeBSD jails.\n"
echo ""
### FILES
printf "${BOLD}FILES${ANSI_END}\n"
printf "\t${JAILER_CONF_DIR}/jailer.conf\n\t${JAILER_TEMPLATE_DIR}/*\n"
echo ""
### EXIT STATUS
printf "${BOLD}EXIT STATUS${ANSI_END}\n"
printf "\tThe ${BOLD}${PGM}${ANSI_END} utility exit 0 on success, 1 if there are problems with the installation and 2 if the wrong arguments are given.\n"
echo ""
### SEE ALSO
printf "${BOLD}SEE ALSO${ANSI_END}\n"
printf "\tjail(8), jail.conf(5), rc.conf(5), zfs(8)\n"
echo ""
exit ${SUCCESS}
}
#
# check the jailer setup
#
validate_setup()
{
# only root can start the programm
if [ "$( id -u )" -ne 0 ]; then
printf "${RED}${ERROR_STRING}${ANSI_END} ${PGM} must run as root!\n"
exit "${FAILURE}"
fi
# check if jails are enabled
if [ ! "$( sysrc -n jail_enable )" = "YES" ] ; then
printf "${YELLOW}${WARN_STRING}${ANSI_END} jails service is not enabled.\n"
fi
# check for template file
if [ "$( find "${JAILER_TEMPLATE_DIR}" -name '*.template' | wc -l | sed 's/[[:space:]]//g' )" -eq 0 ] ; then
printf "${RED}${ERROR_STRING}${ANSI_END} template files \"${JAILER_TEMPLATE_DIR}/*\" do not exist!\n"
exit ${FAILURE}
fi
}
#
# check if repository configuration exists
#
check_repo()
{
if [ ! -f "/usr/local/etc/pkg/repos/${REPO_NAME}.conf" ]; then
printf "${RED}${ERROR_STRING}${ANSI_END} no repository ${BOLD}${WHITE}${REPO_NAME}${ANSI_END} found!"
exit 2
fi
}
#
# check if given jailname already exists in jail.conf
#
check_jailconf()
{
if grep -e "^${JAIL_NAME} {" ${JAIL_CONF} > /dev/null 2>&1; then
return ${SUCCESS}
else
return ${FAILURE}
fi
}
#
# check if given jaildataset already exists
#
check_dataset()
{
if zfs list "${JAIL_DATASET_ROOT}/${JAIL_NAME}" > /dev/null 2>&1; then
return ${SUCCESS}
else
return ${FAILURE}
fi
}
#
# create the dataset
#
create_dataset()
{
if [ "${ZFS_COMPRESSION}" = "NO" ] ; then
COMPRESS="off"
else
COMPRESS="on"
fi
printf "${BLUE}${INFO_STRING}${ANSI_END}Create zfs dataset: ${BOLD}${WHITE}${JAIL_DATASET_ROOT}/${JAIL_NAME}${ANSI_END}\n"
set -o pipefail
zfs create -o compression="${COMPRESS}" "${JAIL_DATASET_ROOT}/${JAIL_NAME}" | tee -a "${LOG_FILE}"
set +o pipefail
}
#
# install the pkgbase pkgs
#
install_baseos_pkg()
{
# create "${JAIL_DIR}/var/cache/pkg", or pkg complains
mkdir -p "${JAIL_DIR}/var/cache/pkg" || exit 1
if [ $MINIJAIL = "YES" ]; then
REPO_NAME="FreeBSD-basecore"
CORE_PKGS="FreeBSD-basecore"
EXTRA_PKGS=""
else
# EXTRA_PKGS: Some additional basesystem pkgs, extend the list if needed
case "${ABI_VERSION}" in
*13*|*14*)
# FreeBSD 13/14
CORE_PKGS="FreeBSD-utilities"
EXTRA_PKGS="FreeBSD-rc FreeBSD-dma FreeBSD-libexecinfo FreeBSD-ssh FreeBSD-vi FreeBSD-at FreeBSD-zoneinfo"
;;
*)
printf "${RED}${ERROR_STRING}${ANSI_END} invalid OS Version detectet: ${BOLD}${WHITE}${ABI_VERSION}${ANSI_END}\n"
exit ${FAILURE}
;;
esac
fi
printf "${BLUE}${INFO_STRING}${ANSI_END}Using repository: ${BOLD}${WHITE}${REPO_NAME}${ANSI_END}\n"
printf "${BLUE}${INFO_STRING}${ANSI_END}Install pkg: ${BOLD}${WHITE}${CORE_PKGS} ${EXTRA_PKGS}${ANSI_END}\n"
echo ""
# Install the base system
set -o pipefail
# the packages must be passed to pkg as multiple parameters, so dont use quotes and ignore the shellcheck error
# shellcheck disable=SC2086
pkg --rootdir "${JAIL_DIR}" -o ASSUME_ALWAYS_YES=true -o ABI="${ABI_VERSION}" install ${PKG_QUIET} --repository "${REPO_NAME}" ${CORE_PKGS} ${EXTRA_PKGS} | tee -a "${LOG_FILE}"
pkg --rootdir "${JAIL_DIR}" -o ASSUME_ALWAYS_YES=true clean | tee -a "${LOG_FILE}"
set +o pipefail
echo ""
}
#
# install pkg programm
#
install_pkgtool()
{
# install the pkg package
set -o pipefail
pkg --rootdir "${JAIL_DIR}" -R "${JAIL_DIR}/etc/pkg/" -o ASSUME_ALWAYS_YES=true -o ABI="${ABI_VERSION}" install ${PKG_QUIET} --repository "${OFFICIAL_REPO_NAME}" pkg | tee -a "${LOG_FILE}"
pkg --rootdir "${JAIL_DIR}" -R "${JAIL_DIR}/etc/pkg/" -o ASSUME_ALWAYS_YES=true ${PKG_QUIET} clean --all --quiet | tee -a "${LOG_FILE}"
set +o pipefail
echo ""
}
#
# install additional packages
#
install_pkgs()
{
_PKGS=$1
pkg --jail "${JAIL_NAME}" -o ASSUME_ALWAYS_YES=true update -f ${PKG_QUIET} --repository "${OFFICIAL_REPO_NAME}"
if [ ! X"${_PKGS}" = "X" ]; then
printf "\n${BLUE}${INFO_STRING}${ANSI_END}Install pkgs:\n"
# install the pkg package
install_pkgtool
for PKG in ${_PKGS}
do
printf "${BLUE}Installing %s${ANSI_END}\n" "${PKG}"
set -o pipefail
pkg --jail "${JAIL_NAME}" -o ASSUME_ALWAYS_YES=true install ${PKG_QUIET} --repository "${OFFICIAL_REPO_NAME}" "${PKG}" | tee -a "${LOG_FILE}"
if [ $? -lt 0 ]; then
printf "${RED}${ERROR_STRING}${ANSI_END} installation of ${BOLD}${WHITE}%s${ANSI_END} failed" "${PKG}"
fi
set +o pipefail
done
pkg --jail "${JAIL_NAME}" -o ASSUME_ALWAYS_YES=true ${PKG_QUIET} clean --all --quiet | tee -a "${LOG_FILE}"
fi
}
#
# enable given services
#
enable_services()
{
if [ ! X"${SERVICES}" = "X" ]; then
printf "${BLUE}${INFO_STRING}${ANSI_END}Enabling services: "
for SERVICE in ${SERVICES}
do
printf "${BLUE}${INFO_STRING}${ANSI_END}${BOLD}${WHITE}${SERVICE}${ANSI_END} "
service -j "${JAIL_NAME}" "${SERVICE}" enable > /dev/null
done
echo ""
fi
}
#
# copy files into the jail
#
copy_files()
{
if [ ! X"${COPY_FILES}" = "X" ]; then
printf "${BLUE}${INFO_STRING}${ANSI_END}Copying files:\n"
OLDIFS=$IFS
IFS=","
(
for COPY_FILE in $COPY_FILES
do
SRC=${COPY_FILE%%:*}
DST=${COPY_FILE##*:}
echo "${SRC} -> ${JAIL_DIR}/${DST}"
cp -a "${SRC}" "${JAIL_DIR}/${DST}"
done
) | column -t
IFS=$OLDIFS
echo ""
fi
}
#
# get next interface ID
#
get_next_interface_id()
{
# Check for IFIDs in jail.conf AND in the actual network configuration,
# to avoid unintentional duplicates
ID_IFCONFIG=$( ifconfig | awk '/IFID/{gsub("IFID=","",$2); print $2}' | sort -u | tail -1 )
ID_JAILCONF=$( awk '/IFID=/ {print $6}' /etc/jail.conf | sed 's#.IFID=##g' | sort -u | tail -1 )
# there are no IFIDs on any networkinterfaces
if [ -z "${ID_IFCONFIG}" ] ; then
ID_IFCONFIG=-1
fi
# there are no IFIDs in jail.conf
if [ -z "${ID_JAILCONF}" ] ; then
ID_JAILCONF=-1
fi
# so set the value to -1 to start the IFIDs with 0
# the IFIDs are identical, pick one and add 1
if [ ${ID_IFCONFIG} -eq ${ID_JAILCONF} ] ; then
INTERFACE_ID=$(( ID_IFCONFIG + 1 ))
# the IFID in ID_IFCONFIG is larger, so pick the larger one and add 1
elif [ ${ID_IFCONFIG} -gt ${ID_JAILCONF} ] ; then
INTERFACE_ID=$(( ID_IFCONFIG + 1 ))
# the IFID in ID_IFCONFIG is smaller, so pick the larger one and add 1
elif [ ${ID_IFCONFIG} -lt ${ID_JAILCONF} ] ; then
INTERFACE_ID=$(( ID_JAILCONF + 1 ))
# there is no yet any IFID, so start with 0
else
INTERFACE_ID=0
fi
}
#
# add the jail configuration to jail.conf
#
create_jailconf_entry()
{
get_next_interface_id
if [ "${JAIL_HOSTNAME}" = "" ] ; then
JAIL_HOSTNAME="${JAIL_NAME}"
fi
printf "${BLUE}${INFO_STRING}${ANSI_END}Jail config: ${BOLD}${WHITE}${JAIL_CONF}${ANSI_END}\n"
sed -e "s|%%JAIL_NAME%%|${JAIL_NAME}|g" \
-e "s|%%JAIL_DOMAINNAME%%|${JAIL_DOMAINNAME}|g" \
-e "s|%%JAIL_HOSTNAME%%|${JAIL_HOSTNAME}|g" \
-e "s|%%JAIL_INTERFACE%%|${JAIL_INTERFACE}|g" \
-e "s|%%JAIL_UUID%%|${JAIL_UUID}|g" \
-e "s|%%JAIL_IP%%|${JAIL_IP}|g" \
-e "s|%%JAIL_NETMASK%%|${JAIL_NETMASK}|g" \
-e "s|%%JAIL_DIR%%|${JAIL_DIR}|g" \
-e "s|%%INTERFACE_ID%%|${INTERFACE_ID}|g" \
-e "s|%%BRIDGE%%|${BRIDGE}|g" \
-e "s|%%GATEWAY%%|${GATEWAY}|g" \
"${TEMPLATE_DIR}/${JAIL_TEMPLATE}" >> "${JAIL_CONF}"
}
#
# change and do some additional things
#
setup_system()
{
printf "${BLUE}${INFO_STRING}${ANSI_END}Setup jail: ${BOLD}${WHITE}${JAIL_NAME}${ANSI_END}\n"
# add some default values for /etc/rc.conf
# but first create the file, so sysrc wont show an error
touch "${JAIL_DIR}/etc/rc.conf"
# System
printf "${BLUE}${INFO_STRING}${ANSI_END}Configure syslog: ${BOLD}${WHITE}syslogd_flags: -s -> -ss${ANSI_END}\n"
sysrc -R "${JAIL_DIR}" syslogd_flags="-ss" > /dev/null
# set timezone in jail
printf "${BLUE}${INFO_STRING}${ANSI_END}Setup timezone: ${BOLD}${WHITE}${TIME_ZONE}${ANSI_END}\n"
tzsetup -sC "${JAIL_DIR}" "${TIME_ZONE}"
# Network
printf "${BLUE}${INFO_STRING}${ANSI_END}Add nameserver: ${BOLD}${WHITE}${NAME_SERVER}${ANSI_END}\n"
echo "nameserver ${NAME_SERVER}" > "${JAIL_DIR}/etc/resolv.conf"
# print the IP Adress
if [ ${VNET} = "YES" ]; then
printf "${BLUE}${INFO_STRING}${ANSI_END}Add VNET IP: ${BOLD}${WHITE}${JAIL_IP}${ANSI_END}\n"
printf "${BLUE}${INFO_STRING}${ANSI_END}Use netmask: ${BOLD}${WHITE}${JAIL_NETMASK}${ANSI_END}\n"
fi
# configure mailing
printf "${BLUE}${INFO_STRING}${ANSI_END}Disable mailer: ${BOLD}${WHITE}sendmail${ANSI_END}\n"
printf "${BLUE}${INFO_STRING}${ANSI_END}Enable mailer: ${BOLD}${WHITE}dma${ANSI_END}\n"
(
sysrc -R "${JAIL_DIR}" sendmail_enable=NO
sysrc -R "${JAIL_DIR}" sendmail_submit_enable=NO
sysrc -R "${JAIL_DIR}" sendmail_outbound_enable=NO
sysrc -R "${JAIL_DIR}" sendmail_msp_queue_enable=NO
) > /dev/null
# mail configuration
if [ ! -d "${JAIL_DIR}/etc/mail/" ]; then
mkdir "${JAIL_DIR}/etc/mail/"
fi
echo "sendmail /usr/libexec/dma" > "${JAIL_DIR}/etc/mail/mailer.conf"
echo "mailq /usr/libexec/dma" >> "${JAIL_DIR}/etc/mail/mailer.conf"
# enable "improved" .cshrc for root
cp "${JAILER_TEMPLATE_DIR}/dot.cshrc" "${JAIL_DIR}/.cshrc"
if [ ! -d "${JAIL_DIR}/usr/include" ] ; then
mkdir "${JAIL_DIR}/usr/include"
fi
# modify motd entry
MOTD_FILE="${JAIL_DIR}/etc/motd.template"
printf "\n\t\"Go directly to Jail. Do not pass GO, do not collect \$200\"\n\n" > "${MOTD_FILE}"
}
#
# setup the pkg repository
#
setup_repository()
{
if [ $MINIJAIL = "YES" ]; then
REPO_NAME="FreeBSD-basecore"
else
REPO_NAME="FreeBSD-pkgbase"
fi
# setup repository
printf "${BLUE}${INFO_STRING}${ANSI_END}Enable repository: ${BOLD}${WHITE}FreeBSD${ANSI_END}\n"
mkdir -p "${JAIL_DIR}/usr/local/etc/pkg/repos"
echo 'FreeBSD: { enabled: yes }' > "${JAIL_DIR}/usr/local/etc/pkg/repos/FreeBSD.conf"
printf "${BLUE}${INFO_STRING}${ANSI_END}Enable repository: ${BOLD}${WHITE}${REPO_NAME}${ANSI_END}\n"
if [ -f "${TEMPLATE_DIR}/FreeBSD-repo.conf.template" ]; then
sed -e "s|%%REPO_NAME%%|${REPO_NAME}|g" \
-e "s|%%REPO_HOST%%|${REPO_HOST}|g" \
"${TEMPLATE_DIR}/FreeBSD-repo.conf.template" >> "${JAIL_DIR}/usr/local/etc/pkg/repos/FreeBSD-base.conf"
else
printf "${YELLOW}${WARN_STRING}${ANSI_END} \"FreeBSD-repo.conf.template\" not found, please check ${BOLD}${WHITE}${TEMPLATE_DIR}${ANSI_END}\n"
fi
echo ""
}
#
# delete the jail dataset
#
destroy_dataset()
{
if check_dataset; then
printf "${BLUE}${INFO_STRING}${ANSI_END}Deleting dataset: ${BOLD}${WHITE}${JAIL_DATASET_ROOT}/${JAIL_NAME}${ANSI_END}\n"
# force the unmounting of the dataset to prevent problems
# zfs "destroying" the dataset
umount -f "${JAIL_DIR}"
zfs destroy "${JAIL_DATASET_ROOT}/${JAIL_NAME}" | tee -a "${LOG_FILE}"
else
printf "${RED}${ERROR_STRING}${ANSI_END} No dataset ${BOLD}${WHITE}${JAIL_DATASET_ROOT}/${JAIL_NAME}${ANSI_END}\n"
fi
}
#
# remove the jail configuration from jail.conf
#
destroy_jailconf_entry()
{
if check_jailconf; then
printf "${BLUE}${INFO_STRING}${ANSI_END}Deleting entry: ${BOLD}${WHITE}${JAIL_NAME}${ANSI_END}\n"
sed -i '' "/^${JAIL_NAME}[[:space:]]*{/,/^[[:space:]]*}[[:space:]]*$/d" "${JAIL_CONF}"
else
printf "${RED}${ERROR_STRING}${ANSI_END} no entry ${BOLD}${WHITE}${JAIL_NAME}${ANSI_END} in ${BOLD}${WHITE}${JAIL_CONF}${ANSI_END}\n"
fi
}
#
# create a logfile to protocol the script actions
#
create_log_file()
{
if [ ${WRITE_LOGFILE} = "YES" ]; then
LOG_FILE="/tmp/jailer_${ACTION}_${JAIL_NAME}_$(date +%Y%m%d%H%M).log"
printf "${BLUE}${INFO_STRING}${ANSI_END}Creating logfile: ${BOLD}${WHITE}${LOG_FILE}${ANSI_END}\n"
else
LOG_FILE="/dev/null"
fi
}
#
# finally create the jail
#
create_jail()
{
if [ X"${JAIL_IP}" = "X" ]; then
printf "${RED}${ERROR_STRING}${ANSI_END} No ip adresse given (-i)\n"
exit 2
fi
JAIL_DIR="$( zfs get -H -o value mountpoint "${JAIL_DATASET_ROOT}" )/${JAIL_NAME}"
if check_jailconf; then
printf "${RED}${ERROR_STRING}${ANSI_END} ${BOLD}${WHITE}${JAIL_NAME}${ANSI_END} already exists in ${BOLD}${WHITE}${JAIL_CONF}${ANSI_END}!\n"
exit 2
elif check_dataset; then
printf "${RED}${ERROR_STRING}${ANSI_END} Dataset ${BOLD}${WHITE}${JAIL_DATASET_ROOT}/${JAIL_NAME}${ANSI_END} already exists!\n"
exit 2
else
create_dataset
install_baseos_pkg
create_jailconf_entry
setup_system
setup_repository
if [ "${INSTALL_PKGTOOL}" = "YES" ]; then
install_pkgtool
fi
printf "${BLUE}${INFO_STRING}${ANSI_END}"
start_jail "${JAIL_NAME}"
if [ "$(sysrc -n pf_enable)" = "YES" ]; then
service pf reload > /dev/null
fi
# install additional packages
install_pkgs "${PKGS}"
# enable services specified in -e argument
enable_services
# create the user when argument -u is set
if [ "${ADD_USER}" = "YES" ]; then
pw -R "${JAIL_DIR}" useradd -n "${USER_NAME}" -u ${USER_ID} -g wheel -c "Inside man" -s /bin/tcsh -m -w yes
fi
printf "${BLUE}${INFO_STRING}${ANSI_END}"
stop_jail "${JAIL_NAME}"
# copy files into the jail specified in -c argument
copy_files
# start the jail when -s argument is set
if [ ${AUTO_START} = "YES" ]; then
printf "${BLUE}${INFO_STRING}${ANSI_END}"
start_jail "${JAIL_NAME}"
fi
fi
}
#
# remove the jail from the system
#
destroy_jail()
{
JAIL_DIR="$( zfs get -H -o value mountpoint "${JAIL_DATASET_ROOT}" )/${JAIL_NAME}"
if ! check_jailconf; then
printf "${RED}${ERROR_STRING}${ANSI_END} Jail ${BOLD}${WHITE}${JAIL_NAME}${ANSI_END} does not exist!\n"
exit 2
elif ! check_dataset; then
printf "${RED}${ERROR_STRING}${ANSI_END} Dataset ${BOLD}${WHITE}${JAIL_DATASET_ROOT}/${JAIL_NAME}${ANSI_END} does not exist!\n"
exit 2
else
printf "${BLUE}${INFO_STRING}${ANSI_END}"
stop_jail "${JAIL_NAME}"
destroy_jailconf_entry
destroy_dataset
fi
echo ""
}
#
# update the base jail and/or installed packages
#
update_jail()
{
JAIL_DIR="$( zfs get -H -o value mountpoint "${JAIL_DATASET_ROOT}" )/${JAIL_NAME}"
if [ "${BASE_UPDATE}" = "YES" ]; then
printf "${BLUE}${INFO_STRING}${ANSI_END}Updating system\n"
set -o pipefail
jexec -l "${JAIL_NAME}" pkg -o ASSUME_ALWAYS_YES=true update --repository FreeBSD-base ${PKG_QUIET} | tee -a "${LOG_FILE}"
jexec -l "${JAIL_NAME}" pkg -o ASSUME_ALWAYS_YES=true upgrade --repository FreeBSD-base ${PKG_QUIET} | tee -a "${LOG_FILE}"
set +o pipefail
if [ $? -lt 0 ]; then
printf "${RED}${ERROR_STRING}${ANSI_END} Update of base failed!\n"
fi
echo ""
fi
if [ ${PKG_UPDATE} = "YES" ]; then
printf "${BLUE}${INFO_STRING}${ANSI_END}Updating packages\n"
set -o pipefail
jexec -l "${JAIL_NAME}" pkg -o ASSUME_ALWAYS_YES=true update --repository FreeBSD ${PKG_QUIET} | tee -a "${LOG_FILE}"
jexec -l "${JAIL_NAME}" pkg -o ASSUME_ALWAYS_YES=true upgrade --repository FreeBSD ${PKG_QUIET} | tee -a "${LOG_FILE}"
set +o pipefail
if [ $? -lt 0 ]; then
printf "${RED}${ERROR_STRING}${ANSI_END} Update of the installed packages failed!\n"
fi
echo ""
fi
}
#
# start all or a named jail
#
start_jail()
{
if [ "$1" = "" ] ; then
printf "${BLUE}${INFO_STRING}${ANSI_END}"
service jail start
else
printf "${BLUE}${INFO_STRING}${ANSI_END}"
service jail start "$1"
fi
if [ "$(sysrc -n pf_enable)" = "YES" ]; then
printf "${BLUE}${INFO_STRING}${ANSI_END}"
service pf reload
fi
}
#
# stop all or a named jail
#
stop_jail()
{
if [ "$1" = "" ] ; then
printf "${BLUE}${INFO_STRING}${ANSI_END}"
service jail stop
else
printf "${BLUE}${INFO_STRING}${ANSI_END}"
service jail stop "$1"
fi
if [ "$(sysrc -n pf_enable)" = "YES" ]; then
printf "${BLUE}${INFO_STRING}${ANSI_END}"
service pf reload
fi
}
#
# reload the pf rules, if pf is active
#
reload_pf()
{
if [ "$(sysrc -n pf_enable)" = "YES" ]; then
printf "${BLUE}${INFO_STRING}${ANSI_END}"
service pf reload
fi
}
#
# setup system to be managed by ansible
#
enable_ansible()
{
install_pkgs "${ANSIBLE_PKGS}"
# ansible is best used via ssh, so enable
# sshd when installing ansible
ENABLE_SSHD="YES"
# create ansible user
printf "add user ${WHITE}${ANSIBLE_USER_NAME}${ANSI_END} (UID: ${WHITE}${ANSIBLE_USER_UID}${ANSI_END}) "
pw -R "${JAIL_DIR}" useradd -n "${ANSIBLE_USER_NAME}" -u "${ANSIBLE_USER_UID}" -g wheel -c "ansible user" -s /bin/sh -m -w random > /dev/null
checkResult $?
# "install" the default ssh public key of the ansible user
printf "copy ssh public-key "
mkdir -p "${JAIL_DIR}/home/${ANSIBLE_USER_NAME}/.ssh"
if [ -d "${JAIL_DIR}/home/${ANSIBLE_USER_NAME}/.ssh" ] ; then
echo "${ANSIBLE_USER_PUBKEY}" > "${JAIL_DIR}/home/${ANSIBLE_USER_NAME}/.ssh/authorized_keys"
chown -R ${ANSIBLE_USER_UID} "${JAIL_DIR}/home/${ANSIBLE_USER_NAME}/.ssh"
checkResult $?
else
checkResult 1
fi