-
Notifications
You must be signed in to change notification settings - Fork 15
/
jss-in-a-box.sh
executable file
·1891 lines (1559 loc) · 65.8 KB
/
jss-in-a-box.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/bash
##########################################################################################
#
# JSS in a Box
# (aka a script to initialise, create, configure and delete JSS instances on a Ubuntu/RedHat server.)
# (with apologies to Tom Bridge and https://github.com/tbridge/munki-in-a-box)
#
# The MIT License (MIT)
# Copyright (c) 2015 <richard at richard-purves.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
##########################################################################################
# Author : Richard Purves <richard at richard-purves.com>
# Version 0.1 - 27th December 2015 - Initial Version
# Version 0.2 - 28th December 2015 - Completed structure and initialisation code
# Version 0.3 - 29th December 2015 - Completed all functions excluding database restoration
# Version 0.4 - 30th December 2015 - Interactive Mode complete. Code for SSL present but untested.
# Version 0.5 - 31st December 2015 - Implemented Ubuntu UFW rules for security
# Implemented ability to talk to an external MySQL server.
# Now creates/deletes JSS log files with modification to the JSS log4j file
# Version 0.6 - 31st December 2015 - Extracts the DataBase.xml file from the provided ROOT.war file
# Version 0.7 - 1st January 2016 - Code simplification and clean up. Was getting messy.
# Can be invoked by parameter and skip the opening menu for specific functions.
# Version 0.8 - 1st January 2016 - HTTPS SSL code implemented and working. Usually. There's all sorts of things out of my control that go wrong with this :(
# Version 0.9 - 2nd January 2016 - Added menu option for SSL certificate update. Checks to see if server.xml is backed up before working on it.
# - Tomcat set to use a max of 3Gb of server memory.
# - cron job created to run an SSL cert refresh every month.
# - This is due to LE's cert lifetime of 90 days and cron's inability to run past daily, monthly or yearly.
# Version 1.0 - 4th January 2016 - RELEASE - LetsEncrypt is now an optional install. Log files repointed on JSS upgrades too.
# - Cleaned up Tomcat caching issues with deleted instances.
# Version 1.0 - 31st March 2016 - Redhat compatible version. No new features so not a version increment.
# Version 1.1 - 12th April 2016 - Update of SSL cipher list to bring into line with https://jamfnation.jamfsoftware.com/article.html?id=384
# Version 2.0 - 14th April 2016 - Merged both scripts together. Now one big universal version with better OS checking.
# Version 2.1 - 16th April 2016 - Now manages Tomcat, MySQL and Java memory settings. Calculated per current formulas on JAMF's CJA course.
# Version 2.2 - 18th April 2016 - Optional redirection of HTTPS traffic to port 443 from 8443 via firewall rules.
# Version 2.3 - 21st April 2016 - Tomcat really doesn't like running with less than 1Gb ram, so we check for 1.5Gb available. Quit if not available.
# Version 2.4 - 22nd April 2016 - Choice of which supported Java version to install. In variable below.
# Version 2.5 - 15th June 2016 - Fixed missing rule in UFW configuration. Also fixed bug where connector keystore file isn't set properly.
# Version 2.6 - 23rd June 2016 - Recoded large chunks of the LetsEncrypt code due to them suddenly getting distribution via repo AND changing the name of the binary that does the work.
# Version 2.7 - 14th July 2016 - Added code to make sure that tomcat webapp folders have correct ownership of the appropriate tomcat user.
# Version 3.0 - 4th August 2016 - Removed Java 7, upgraded Tomcat to version 8. Refactored all the relevant code and paths to compensate for manual install. Down 300+ lines for same functionality!
# Version 3.1 - 6th August 2016 - Cleaned up some embarrassing typos and code swapping to do with LetsEncrypt. Also some interesting Tomcat 8 server.xml changes.
# Big thanks to Cody Butcher for his help on this!
# Version 3.2 - 17th February 2017 - Fixed some pesky mysql error redirection stuff. Won't be prompted for insecure command line anymore!
# Fixed some very annoying tomcat webapp folder ownership stuff that never worked properly. Apparently.
# Version 3.3 - 23rd February 2017 - Brought the Tomcat HTTPS connector settings into line with Jamf's current documentation.
# Version 3.5 - 23rd February 2017 - Major update to use Ubuntu 16.04 LTS in place of 14.04 LTS. Java now uses OpenJDK 8 on both OS. Tomcat has a unified systemd launcher. Code simplified.
# Big thanks to Rich Trouton for quickly helping me with the systemd testing today!
# Version 4.0 - 21st March 2017 - Change MySQL to install 5.7.14 or better with the release of JSS 9.98. Please note new password complexity requirements for db's!
# - Also fixed DUMB deltarpm bug. Removed MaxPermGen setting for Tomcat as Java 8 doesn't support it. Replaced with MetaspaceSize stuff.
# - Replaced LetsEncrypt crontab with a systemd job. OpenJDK replaced with Oracle for BOTH OS platforms due to CPU hammering issues.
# Version 4.1 - 27th March 2017 - Whoops, forgot to clean up a file. Thrown in some extra commands for db integrity checking post upload of database dump file.
# - DB check code thanks to Neil Martin: https://soundmacguy.wordpress.com/2017/03/27/jamf-pro-9-98-on-windows-migrating-to-mysql-5-7/
# Version 4.2 - 9th May 2017 - Fixed LetsEncrypt bug caused by deleting the wrong line in Tomcat server.xml file
# Version 4.3 - 17th May 2017 - Fixed code for new instance creation. Some of the variables were pointing to non-existant file paths. I do wonder what I was thinking at the time. Let's see if people read this.
# Version 4.4 - 19th May 2017 - Sole change to disable password complexity rules on MySQL 5.7
# Version 4.5 - 22nd June 2017 - Fixed bug with HTTPS config for tomcat. Was missing a closing bracket!
# Version 4.6 - 3rd August 2017 - Updated tomcat config due to new jamf security paper: https://resources.jamf.com/documents/white-papers/Securing-Your-Jamf-Server.pdf
# - Corrected very old systemd config bug with LetsEncrypt cert renewal timers.
# Version 4.7 - 7th August 2017 - It was pointed out to me that if Tomcat crashes, it would not auto restart. Fixed SystemD config to compensate. Also fixed shutdown and some RedHat java bugs.
# Version 4.8 - 16th August 2017 - Seems deleting instances wasn't cleaning out the Tomcat work folders properly. Fixed.
# Version 4.9 - 6th September 2017 - MySQL 5.7 has major changes to it's my.cnf file, so i've reworked the configuration code in that area.
# Version 4.91- 4th October 2017 - Fixed tomcat version finding bug reported by Mark Smith.
# Version 5.0 - 15th December 2017 - Changed Java Cryptography installation. Now it's just a preference change rather than a file replacement.
# Version 5.1 - 20th December 2017 - Last change for 2017! Java exiting returns a 143 exit code which systemd doesn't like. Well it does now!
# Version 5.2 - 20th February 2018 - Silly bug with upgrade all that meant it never finished.
# Version 5.3 - 28th February 2018 - Removed extra line from Tomcat connector configuration.
# Set up variables to be used here
# These variables are user modifiable. Don't forget to configure firewall settings from Line 808!
export useract="richardpurves" # Server admin username. Used for home location.
export letsencrypt="FALSE" # Set this to TRUE if you are going to use LetsEncrypt as your HTTPS Certificate Authority.
export sslTESTMODE="TRUE" # Set this to FALSE when you're confident it's generating proper certs for you
export httpsredirect="FALSE" # Set this to TRUE if you want your JSS to appear to be on port 443 using HTTPS
export ssldomain="jssinabox.domain.com" # Domain name for the SSL certificates
export sslemail="richard at richard-purves.com" # E-mail address for the SSL CA
export sslkeypass="changeit" # Password to the keystore. Default is "changeit". Please change it!
export mysqluser="root" # MySQL root account
export mysqlpw="Changeit1!" # MySQL root account password. Please change it and note security requirements below:
# At least one upper case letter, one lower case letter,
# one digit, and one special character
# and a minimum length of at least 8 characters.
export mysqlserveraddress="localhost" # IP/Hostname of MySQL server. Default is local server.
export dbuser="jamfsoftware" # Database username for JSS
export dbpass="Changeit1!" # Database password for JSS. Default is "changeit". Please change it and see MySQL restrictions above.
# These variables should not be tampered with or script functionality will be affected!
currentdir=$( pwd )
currentver="5.3"
currentverdate="28th February 2018"
export homefolder="/home/$useract" # Home folder base path
export rootwarloc="$homefolder" # Location of where you put the ROOT.war file
export logfiles="/var/log/JSS" # Location of ROOT and instance JSS log files
export tomcatloc="/opt/tomcat8" # Tomcat's installation path
export webapploc="$tomcatloc/webapps" # Tomcat web application install path
export cacheloc="$tomcatloc/work/Catalina/localhost" # Tomcat's webapp cache folder
export user="tomcat" # User and Group used for tomcat
export sslkeystorepath="$tomcatloc/keystore" # Keystore path
export server="$tomcatloc/conf/server.xml" # Tomcat server.xml path
export ubmycnfloc="/etc/mysql/mysql.conf.d/mysqld.cnf" # MySQL 5.7 configuration file path(s)
export rhmycnfloc="/etc/my.cnf"
export DataBaseLoc="WEB-INF/xml" # DataBase.xml location inside the JSS webapp
export DataBaseXML="$rootwarloc/DataBase.xml.original" # Location of the tmp DataBase.xml file we use for reference
export lepath="/etc/letsencrypt/live" # LetsEncrypt's certificate storage location
# All functions to be set up here
WhichDistAmI()
{
# First check is for Ubuntu 14.04 LTS
if [ -f "/usr/bin/lsb_release" ];
then
ubuntuVersion=`lsb_release -s -d`
case $ubuntuVersion in
*"Ubuntu 16.04"*)
OS="Ubuntu"
export OS
;;
*)
echo -e "Script requires Ubuntu 16.04 LTS. Exiting."
exit 1
;;
esac
fi
# Second check is for RedHat 7.x
if [ -f "/etc/redhat-release" ];
then
version=$( cat /etc/redhat-release | awk '{ print $7 }' | cut -c 1 )
# Is this RedHat 7 server?
if [[ $version != "7" ]];
then
echo -e "Script requires RedHat 7.x. Exiting."
exit 1
else
echo -e "Redhat 7 detected. Proceeding."
OS="RedHat"
export OS
fi
fi
# Last check is to see if we got a bite or not
if [[ $OS != "Ubuntu" && $OS != "RedHat" ]];
then
echo -e "Script requires either Ubuntu 16.04 LTS or RHEL 7.x. Exiting."
exit 1
fi
}
AmIroot()
{
# Check for root, quit if not present with a warning.
if [[ "$(id -u)" != "0" ]];
then
echo -e "Script needs to be run as root."
exit 1
else
echo -e "Script running as root. Proceeding."
fi
}
IsROOTwarPresent()
{
# Check for presence of ROOT.war file or we can't upgrade at all!
if [ ! -f "$rootwarloc/ROOT.war" ];
then
echo -e "\nMissing ROOT.war file from path: $rootwarloc \nPlease copy file to location and try again."
exit 1
else
echo -e "\nROOT.war present at path: $rootwarloc. Proceeding."
fi
}
TomcatService()
{
systemctl $1 tomcat
}
MySQLService()
{
if [[ $OS = "Ubuntu" ]];
then
systemctl $1 mysql
fi
if [[ $OS = "RedHat" ]];
then
systemctl $1 mysqld
fi
}
CheckMySQL()
{
if [[ $OS = "Ubuntu" ]];
then
export mysql=$(dpkg -l | grep "mysql-server" >/dev/null && echo "yes" || echo "no")
fi
if [[ $OS = "RedHat" ]];
then
export mysql=$(yum -q list installed mysql-community-server &>/dev/null && echo "yes" || echo "no")
fi
}
InstanceList()
{
# Is Tomcat present?
[ -d "$tomcatloc" ] && tomcat="yes" || tomcat="no"
if [[ $tomcat = "no" ]];
then
echo -e "\nTomcat 8 not present. Please install before trying again."
else
echo -e "\nJSS Instance List\n-----------------\n"
find $tomcatloc/webapps/* -maxdepth 0 -type d 2>/dev/null | sed -r 's/^.+\///'
fi
}
SetupTomcatUser()
{
export user="tomcat"
}
SetupLogs()
{
# Check and create the JSS log file folder if missing with appropriate permissions.
if [ ! -d $logfiles ];
then
mkdir $logfiles
SetupTomcatUser
chown -R $user:$user $logfiles
fi
}
UpdatePkgMgr()
{
if [[ $OS = "Ubuntu" ]];
then
echo -e "\nUpdating apt-get repository ...\n"
apt-get update -q
echo -e "\nUpgrading installed packages ...\n"
apt-get upgrade -q -y
fi
if [[ $OS = "RedHat" ]];
then
# Is the delta RPM module installed?
deltarpm=$(yum -q list installed deltarpm &>/dev/null && echo "yes" || echo "no")
if [[ $deltarpm = "no" ]];
then
echo -e "\nInstalling Delta RPM functionality\n"
yum -q -y install deltarpm
else
echo -e "\nDelta RPM already present. Proceeding."
fi
echo -e "\nUpdating yum repository ...\n"
yum -q -y update
fi
}
InstallGit()
{
if [[ $OS = "Ubuntu" ]];
then
# Is git present?
git=$(dpkg -l | grep -w "git" >/dev/null && echo "yes" || echo "no")
if [[ $git = "no" ]];
then
echo -e "\ngit not present. Installing\n"
apt-get install -q -y git
else
echo -e "\ngit already present. Proceeding."
fi
fi
if [[ $OS = "RedHat" ]];
then
# Is git present?
git=$(yum -q list installed git &>/dev/null && echo "yes" || echo "no" )
if [[ $git = "no" ]];
then
echo -e "\ngit not present. Installing."
yum -q -y install git
else
echo -e "\ngit already present. Proceeding."
fi
fi
}
InstallCurl()
{
if [[ $OS = "Ubuntu" ]];
then
# Is curl present?
git=$(dpkg -l | grep -w "curl" >/dev/null && echo "yes" || echo "no")
if [[ $git = "no" ]];
then
echo -e "\ncurl not present. Installing\n"
apt-get install -q -y curl
else
echo -e "\ncurl already present. Proceeding."
fi
fi
if [[ $OS = "RedHat" ]];
then
# Is wget present?
wget=$(yum -q list installed curl &>/dev/null && echo "yes" || echo "no" )
if [[ $wget = "no" ]];
then
echo -e "\nwcurl not present. Installing."
yum -q -y install curl
else
echo -e "\ncurl already present. Proceeding."
fi
fi
}
InstallWget()
{
if [[ $OS = "Ubuntu" ]];
then
# Is wget present?
git=$(dpkg -l | grep -w "wget" >/dev/null && echo "yes" || echo "no")
if [[ $git = "no" ]];
then
echo -e "\nwget not present. Installing\n"
apt-get install -q -y wget
else
echo -e "\nwget already present. Proceeding."
fi
fi
if [[ $OS = "RedHat" ]];
then
# Is wget present?
wget=$(yum -q list installed wget &>/dev/null && echo "yes" || echo "no" )
if [[ $wget = "no" ]];
then
echo -e "\nwget not present. Installing."
yum -q -y install wget
else
echo -e "\nwget already present. Proceeding."
fi
fi
}
InstallUnzip()
{
if [[ $OS = "Ubuntu" ]];
then
# Is unzip installed?
unzip=$(dpkg -l | grep -w "unzip" >/dev/null && echo "yes" || echo "no")
if [[ $unzip = "no" ]];
then
echo -e "\nunzip not present. Installing\n"
apt-get install -q -y unzip
else
echo -e "\nunzip already present. Proceeding."
fi
fi
if [[ $OS = "RedHat" ]];
then
# Is unzip installed?
unzip=$(yum -q list installed unzip &>/dev/null && echo "yes" || echo "no" )
if [[ $unzip = "no" ]];
then
echo -e "\nunzip not present. Installing\n"
yum -q -y install unzip
else
echo -e "\nunzip already present. Proceeding."
fi
fi
}
PrepDBfile()
{
# Is unzip installed? Check by calling the unzip function.
InstallUnzip
# Check for presence of DataBase.xml.original file.
# If missing, extract the DataBase.xml and place it in the $rootwarloc directory
# Rename to DataBase.xml.original
if [ ! -f "$rootwarloc/DataBase.xml.original" ];
then
echo -e "\nExtracting DataBase.xml from ROOT.war\n"
unzip -j $rootwarloc/ROOT.war "WEB-INF/xml/DataBase.xml" -d $rootwarloc
mv $rootwarloc/DataBase.xml $rootwarloc/DataBase.xml.original
else
echo -e "\nDataBase.xml.original found at path: $rootwarloc. Proceeding."
fi
}
InstallFirewall()
{
if [[ $OS = "Ubuntu" ]];
then
# Is UFW present?
ufw=$(dpkg -l | grep "ufw" >/dev/null && echo "yes" || echo "no")
if [[ $ufw = "no" ]];
then
echo -e "\nufw not present. Installing.\n"
apt-get install -q -y ufw
else
echo -e "\nufw already installed. Proceeding."
fi
fi
if [[ $OS = "RedHat" ]];
then
# Is firewalld installed?
fwd=$(yum -q list installed firewalld &>/dev/null && echo "yes" || echo "no" )
if [[ $fwd = "no" ]];
then
echo -e "\nFirewallD not present. Installing."
yum -q -y install firewalld
else
echo -e "\nFirewallD already installed. Proceeding."
fi
fi
}
InstallOpenSSH()
{
if [[ $OS = "Ubuntu" ]];
then
# Is OpenSSH present?
openssh=$(dpkg -l | grep "openssh" >/dev/null && echo "yes" || echo "no")
if [[ $openssh = "no" ]];
then
echo -e "\nopenssh not present. Installing.\n"
apt-get install -q -y openssh
else
echo -e "\nopenssh already installed. Proceeding."
fi
fi
if [[ $OS = "RedHat" ]];
then
# Is OpenSSH present?
openssh=$(yum -q list installed openssh &>/dev/null && echo "yes" || echo "no" )
if [[ $openssh = "no" ]];
then
echo -e "\nopenssh not present. Installing.\n"
yum -q -y install openssh
else
echo -e "\nopenssh already installed. Proceeding."
fi
fi
}
InstallOpenVMTools()
{
if [[ $OS = "Ubuntu" ]];
then
# Are the open-vm-tools present?
openvmtools=$(dpkg -l | grep "open-vm-tools" >/dev/null && echo "yes" || echo "no")
if [[ $openvmtools = "no" ]];
then
echo -e "\nopen vm tools not present. Installing."
echo -e "\nGetting VMware packaging keys from server."
wget http://packages.vmware.com/tools/keys/VMWARE-PACKAGING-GPG-DSA-KEY.pub
wget http://packages.vmware.com/tools/keys/VMWARE-PACKAGING-GPG-RSA-KEY.pub
echo -e "\nInstalling VMware packaging keys into apt."
apt-key add ./VMWARE-PACKAGING-GPG-DSA-KEY.pub
apt-key add ./VMWARE-PACKAGING-GPG-RSA-KEY.pub
echo -e "\nCleaning up key files."
rm ./VMWARE-PACKAGING-GPG-DSA-KEY.pub
rm ./VMWARE-PACKAGING-GPG-RSA-KEY.pub
echo -e "\nInstalling open vm tools."
apt-get install -q -y open-vm-tools
else
echo -e "\nopen vm tools already installed. Proceeding."
fi
fi
if [[ $OS = "RedHat" ]];
then
# Are the open-vm-tools present?
openvmtools=$(yum -q list installed open-vm-tools &>/dev/null && echo "yes" || echo "no" )
if [[ $openvmtools = "no" ]];
then
echo -e "\nopen vm tools not present. Installing."
yum -q -y install open-vm-tools
else
echo -e "\nopen vm tools already installed. Proceeding."
fi
fi
}
InstallJava()
{
if [[ $OS = "Ubuntu" ]];
then
# Is Oracle Java 1.8 present?
java8=$(dpkg -l | grep "oracle-java8-installer" >/dev/null && echo "yes" || echo "no")
if [[ $java8 = "no" ]];
then
echo -e "\nOracle Java 8 not present. Installing."
apt install -q -y software-properties-common
echo -e "\nAdding webupd8team repository to list.\n"
add-apt-repository -y ppa:webupd8team/java
apt-get update -q
echo -e "\nInstalling Oracle Java 8.\n"
echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
apt-get install -q -y oracle-java8-installer
echo -e "\nSetting Oracle Java 8 to the system default.\n"
apt-get install -q -y oracle-java8-set-default
echo -e "\nSetting JAVA_HOME to use Oracle Java 8.\n"
echo "JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre/bin/java " >> /etc/environment
echo -e "\nEnabling Java Cryptography Extensions"
sed -i 's/#crypto.policy=unlimited/crypto.policy=unlimited/' /etc/java-8-oracle/security/java.security
else
echo -e "\nOracle Java 8 already installed. Proceeding."
fi
fi
if [[ $OS = "RedHat" ]];
then
# Is Oracle Java 1.8 present?
java8=$( [ -d "/usr/java" ] && echo "yes" || echo "no" )
if [[ $java8 = "no" ]];
then
echo -e "\nOracle Java 8 not present. Installing."
echo -e "\nFinding latest RPM download link.\n"
dl=$( curl -s "https://www.java.com/en/download/manual.jsp" | grep "x64 RPM" | grep -Eo "(http)://[a-zA-Z0-9./?=_-]*" | awk "NR>1{print $1}" )
echo -e "\nDownloading latest Oracle Java RPM\n"
curl -v -j -k -L -H "Cookie:oraclelicense=accept-securebackup-cookie" "$dl" > $rootwarloc/oracle-java.rpm
echo -e "\nInstalling Oracle Java RPM\n"
rpm -ivh $rootwarloc/oracle-java.rpm
echo -e "\nCleaning up downloaded files\n"
rm $rootwarloc/oracle-java.rpm
echo -e "\nSetting JAVA_HOME to use Oracle Java 8.\n"
echo "JAVA_HOME=/usr/java/default/" >> /etc/environment
echo -e "\nEnabling Java Cryptography Extensions\n"
sed -i 's/#crypto.policy=unlimited/crypto.policy=unlimited/' /usr/java/default/lib/security/java.security
else
echo -e "\nOracle 8 already installed. Proceeding."
fi
fi
}
InstallTomcat()
{
# Is Tomcat present?
[ -d "$tomcatloc" ] && tomcat="yes" || tomcat="no"
if [[ $tomcat = "no" ]];
then
echo -e "\nTomcat 8 not installed. Installing."
# Create tomcat group
echo -e "\nCreating user group: $user"
groupadd $user
if [[ $OS = "Ubuntu" ]];
then
echo -e "\nUbuntu detected."
# Create tomcat user - Ubuntu 14.04 LTS
echo -e "\nCreating tomcat user: $user"
useradd -s /bin/false -g "$user" -d /opt/tomcat "$user"
fi
if [[ $OS = "RedHat" ]];
then
echo -e "\nRedHat detected."
# Create tomcat user - RHEL 7.x
echo -e "\nCreating tomcat user: $user"
useradd -M -s /sbin/nologin -g "$user" -d /opt/tomcat "$user"
fi
# Create systemd script so tomcat can run as a service
echo -e "\nCreating systemd script for Tomcat 8"
systemd='# Systemd unit file for tomcat
# Created by JSS in a Box
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/opt/tomcat8/bin/startup.sh
ExecStop=/opt/tomcat8/bin/shutdown.sh
User=tomcat
Group=tomcat
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Alias=tomcat.service
[Service]
SuccessExitStatus=143'
echo "$systemd" > /lib/systemd/system/tomcat.service
systemctl daemon-reload
# Find latest tomcat version
echo -e "\nFinding latest Tomcat 8 version"
version=$( curl -s http://tomcat.apache.org/download-80.cgi | grep "<h3 id=\"8.0" | head -n1 | awk '{gsub("<[^>]*>", "")}1' | tr -d ' \t\n\r\f' )
echo -e "\nVersion: $version"
# Work out proper download path
path="http://www.apache.org/dist/tomcat/tomcat-8/v${version}/bin/apache-tomcat-${version}.tar.gz"
# Grab the latest .tar.gz distribution and install to /opt
echo -e "\nDownloading and installing latest Tomcat 8"
cd /opt
wget $path
tar -xzf /opt/apache-tomcat-${version}.tar.gz
mv /opt/apache-tomcat-${version} "$tomcatloc"
rm /opt/apache-tomcat-${version}.tar.gz
# Configure permissions on tomcat installation
echo -e "\nConfiguring Tomcat folder permissions"
chown -R $user:$user $tomcatloc
chmod g+rwx $tomcatloc/conf
chmod g+r $tomcatloc/conf/*
chmod g+rwx $tomcatloc/webapps
chmod g+r $tomcatloc/webapps/*
# Configure Tomcat environment variable
echo -e "\nSetting Tomcat environment variable"
echo "export CATALINA_HOME="${tomcatloc}"" >> ~/.bashrc
source ~/.bashrc
# Create Tomcat setenv.sh configuration file
setenv='#!/bin/bash
# Tomcat configuration file. Generated by JSS-in-a-Box.
CATALINA_BASE='"'$tomcatloc'"'
CATALINA_HOME="$CATALINA_BASE"
CATALINA_OPTS="-Xms1024m -Xmx3072m"
CATALINA_OPTS="$CATALINA_OPTS -Xss256k"
CATALINA_OPTS="$CATALINA_OPTS -XX:MetaspaceSize=512m"
CATALINA_OPTS="$CATALINA_OPTS -XX:MaxMetaspaceSize=3072m"
CATALINA_OPTS="$CATALINA_OPTS -XX:MaxGCPauseMillis=1500"
CATALINA_OPTS="$CATALINA_OPTS -XX:GCTimeRatio=9"
CATALINA_OPTS="$CATALINA_OPTS -Djava.awt.headless=true"
CATALINA_OPTS="$CATALINA_OPTS -server"
CATALINA_OPTS="$CATALINA_OPTS -XX:+DisableExplicitGC"
CATALINA_OPTS="$CATALINA_OPTS -Djava.security.egd=file:/dev/./urandom"'
echo "$setenv" > $tomcatloc/bin/setenv.sh
chown tomcat:tomcat $tomcatloc/bin/setenv.sh
chmod 750 $tomcatloc/bin/setenv.sh
# Clean default tomcat webapps out. We don't require them and could be a security hazard.
echo -e "\nClearing out Tomcat default installations"
rm -rf $tomcatloc/webapps/* 2>/dev/null
echo -e "\nEnabling Tomcat to start on system restart"
systemctl enable tomcat
else
echo -e "\nTomcat already present. Proceeding."
fi
}
InstallMySQL()
{
if [[ $OS = "Ubuntu" ]];
then
# Is MySQL 5.7 present?
mysql=$(dpkg -l | grep "mysql-server-5.7" >/dev/null && echo "yes" || echo "no")
if [[ $mysql = "no" ]];
then
echo -e "\nMySQL 5.7 not present. Installing\n"
echo -e "\nPreconfiguration of MySQL repo before adding to system\n"
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password password $mysqlpw"
debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password_again password $mysqlpw"
echo -e "\nAdding MySQL APT repo to system\n"
wget https://dev.mysql.com/get/mysql-apt-config_0.8.3-1_all.deb -P $homefolder
dpkg -i $homefolder/mysql-apt-config_0.8.3-1_all.deb
apt-get update -q
rm $homefolder/mysql-apt-config_0.8.3-1_all.deb
echo -e "\nInstalling MySQL 5.7\n"
apt-get install -q -y mysql-server
echo -e "\nEnabling MySQL to start on system restart"
systemctl enable mysql
echo -e "\nStarting MySQL 5.7"
MySQLService start
else
echo -e "\nMySQL 5.7 already present. Proceeding."
fi
fi
if [[ $OS = "RedHat" ]];
then
# Is MySQL 5.7 present?
mysql=$(yum -q list installed mysql-community-server &>/dev/null && echo "yes" || echo "no")
if [[ $mysql = "no" ]];
then
echo -e "\nMySQL 5.7 not present. Installing."
echo -e "\nAdding MySQL 5.7 to yum repo list\n"
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm -P $homefolder
rpm -ivh $homefolder/mysql57-community-release-el7-9.noarch.rpm
rm $homefolder/mysql57-community-release-el7-9.noarch.rpm
echo -e "\nInstalling MySQL 5.7\n"
yum -y --nogpgcheck install mysql-community-server
echo -e "\nEnabling MySQL to start on system restart"
systemctl enable mysqld
echo -e "\nStarting MySQL 5.7"
MySQLService start
echo -e "\nChanging MySQL 5.7 root account password\n"
temppw=$( grep 'temporary password' /var/log/mysqld.log | awk '{ print $11 }' )
mysql -h$mysqlserveraddress -u$mysqluser -p$temppw --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$mysqlpw';" 2>/dev/null
mysql -h$mysqlserveraddress -u$mysqluser -p$mysqlpw -e "uninstall plugin validate_password;" 2>/dev/null
else
echo -e "\nMySQL 5.7 already present. Proceeding."
fi
fi
}
SetupFirewall()
{
# It's time to harden the server firewall.
if [[ $OS = "Ubuntu" ]];
then
# For Ubuntu, we're using UFW to do this.
ufw --force disable # Disables the firewall before we make our changes
ufw --force reset # Resets any firewall rules
ufw allow ssh # Port 22
# ufw allow http # Port 80 (JSS used to use this for xml lookups. Unsure if still needed.)
ufw allow smtp # Port 25
ufw allow ntp # Port 123
# ufw allow ldap # Port 389 (unsecure port. use for internal servers only)
# ufw allow ldaps # Port 636 (ssl ldap. hopefully to be used in preference to above)
ufw allow https # Port 443
ufw allow mysql # Port 3306
ufw allow http-alt # Port 8080 (delete once you got SSL working)
ufw allow 8443 # Port 8443 (delete if you enable 443 redirection or are using 8080 above)
ufw allow 2195 # Apple Push Notification Service
ufw allow 2196 # Apple Push Notification Service
ufw allow 5223 # Apple Push Notification Service
ufw allow 5228 # Google Cloud Messaging
ufw --force enable # Turns on the firewall. May cause ssh disruption in the process.
# Is https redirect enabled? If so, we have to set up some firewall rules to direct 8443 to 443.
# We're going to use firewall port redirection as the alternative using authbind has proven unreliable.
# And we have to add the rules manually before totally restarting the service too. Fun.
if [[ $httpsredirect = "TRUE" ]];
then
# Append the following lines into the before firewall rules file.
echo "# Port 443 to 8443 redirect rules - added by JSS in a Box" >> /etc/ufw/before.rules
echo "*nat" >> /etc/ufw/before.rules
echo ":PREROUTING ACCEPT [0:0]" >> /etc/ufw/before.rules
echo "-A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443" >> /etc/ufw/before.rules
echo "COMMIT" >> /etc/ufw/before.rules
# Restart the entire UFW service or this won't work until reboot.
service ufw restart
fi
fi
if [[ $OS = "RedHat" ]];
then
# For RedHat, we're using FireWallD.
echo -e "\nEnabling FirewallD service."
systemctl start firewalld
echo -e "\nConfiguring FirewallD service\n"
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=smtp
firewall-cmd --permanent --add-service=ntp
#firewall-cmd --permanent --add-service=ldap
#firewall-cmd --permanent --add-service=ldaps
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-service=mysql
firewall-cmd --permanent --add-port=8080/tcp # HTTP JSS (delete once you got SSL working)
firewall-cmd --permanent --add-port=8443/tcp # HTTPS JSS (delete 8080 once you got SSL working)
firewall-cmd --permanent --add-port=2195/tcp # Apple Push Notification Service
firewall-cmd --permanent --add-port=2196/tcp # Apple Push Notification Service
firewall-cmd --permanent --add-port=5223/tcp # Apple Push Notification Service
firewall-cmd --permanent --add-port=5228/tcp # Google Cloud Messaging
echo -e "\nEnabling FirewallD rule changes\n"
firewall-cmd --reload
echo -e "\nEnabling FirewallD to start on system reboot"
systemctl enable firewalld
# Is https redirect enabled? If so, we have to set up some firewall rules to direct 443 to 8443.
# A little nicer than UFW, as there are commands to do this instead of directly manipulating firewall rules.
if [[ $httpsredirect = "TRUE" ]];
then
firewall-cmd --permanent --add-masquerade
firewall-cmd --permanent --add-forward-port=port=443:proto=tcp:toport=8443
firewall-cmd --reload
fi
fi
}
InstallLetsEncrypt()
{
# Is LetsEncrypt present?
# This one is a git clone, rather than an installation for cross distro reasons. We'll be putting this in /opt/letsencrypt
if [ ! -d "/opt/letsencrypt" ];
then
echo -e "\nLetsEncrypt not present. Downloading installation script from dl.eff.org.\n"
mkdir /opt/letsencrypt
cd /opt/letsencrypt
wget https://dl.eff.org/certbot-auto
chmod +x /opt/letsencrypt/certbot-auto
sudo -H /opt/letsencrypt/certbot-auto -n
cd $currentdir
else
echo -e "\nLetsEncrypt is already installed. Proceeding."
return
fi
# We'll be doing some work with Tomcat so let's stop the service to make sure we don't hurt anything.
echo "\nStopping Tomcat service\n"
TomcatService stop
# Get LetsEncrypt to generate the appropriate certificate files for later processing.
# We're using sudo -H even as root because there's some weird errors that happen if you don't.
echo -e "\nObtaining Certificate from LetsEncrypt Certificate Authority\n"
if [[ $sslTESTMODE = "TRUE" ]];
then
sudo -H /opt/letsencrypt/certbot-auto certonly --standalone -m $sslemail -d $ssldomain --agree-tos --test-cert
else
sudo -H /opt/letsencrypt/certbot-auto certonly --standalone -m $sslemail -d $ssldomain --agree-tos
fi
# Code to generate a Java KeyStore for Tomcat from what's provided by LetsEncrypt
# Based on work by Carmelo Scollo (https://melo.myds.me)
# https://community.letsencrypt.org/t/how-to-use-the-certificate-for-tomcat/3677/9
# Create a keystore folder for Tomcat with the correct permissions
mkdir $sslkeystorepath
chown $user:$user $sslkeystorepath
chmod 755 $sslkeystorepath
# Ok we got LetsEncrypt certificate files, let's make a PKCS12 combined key file from them.
echo -e "\nMerging LetsEncrypt certificates into a PKCS12 file\n"
openssl pkcs12 \
-export \
-in $lepath/$ssldomain/fullchain.pem \
-inkey $lepath/$ssldomain/privkey.pem \
-out $sslkeystorepath/fullchain_and_key.p12 \
-password pass:$sslkeypass \
-name tomcat
# Now we convert our .p12 file into a java keystore that Tomcat likes better.
echo -e "\nConverting PKCS12 file into a Java KeyStore\n"
keytool -importkeystore \
-deststorepass $sslkeypass \
-destkeypass $sslkeypass \
-destkeystore $sslkeystorepath/keystore.jks \
-srckeystore $sslkeystorepath/fullchain_and_key.p12 \
-srcstoretype PKCS12 \
-srcstorepass $sslkeypass \
-alias tomcat
# Clean up on aisle three!
rm $sslkeystorepath/fullchain_and_key.p12
# Tomcat server.xml was previous prepared in the ConfigureMemoryUseage function.
# Now we disable HTTP and enable the HTTPS connectors
echo -e "\nDisabling Tomcat HTTP connector\n"
sed -i '75i<!--' $server
sed -i '80i-->' $server
echo -e "\nEnabling Tomcat HTTPS Connector with executor\n"
sed -i '107d' $server
sed -i '86d' $server
# We're done here. Start 'er up.
TomcatService start
# Oh wait, we have to set up a periodic renewal since LetsEncrypt doesn't do that for Tomcat
# (at time of coding - 23rd June 2016)
# This should run every night at midnight. LE certs last 90 days but quicker is fine as it won't renew.
# crontab -l | { echo "0 0 * * * $homefolder/jss-in-a-box.sh -s"; } | crontab -
# We must set up a recurring job to renew the certs using systemd.
# Using https://mjanja.ch/2015/06/replacing-cron-jobs-with-systemd-timers/ as a handy resource
lerenew='[Unit]
Description=LetsEncrypt Tomcat certificate renewal
[Timer]
OnCalendar=daily
[Install]
WantedBy=timers.target'
echo "$lerenew" > /etc/systemd/system/le-renew.timer
leservice='[Unit]
Description=LetsEncrypt Tomcat certificate renewal
[Service]
Type=simple
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart="$homefolder"/jss-in-a-box.sh -s'
echo "$leservice" > /etc/systemd/system/le-renew.service
# Files are in place. Start the timer and enable it for system reboots too.
systemctl start le-renew.timer
systemctl enable le-renew.timer
}
UpdateLeSSLKeys()
{
# update SSL keys from LetsEncrypt