summaryrefslogtreecommitdiffstats
path: root/sysutils/pfSense-upgrade/files/pfSense-upgrade
blob: 52ad05ac4a0006c82e10dc63c7b5d667637b83e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
#!/bin/sh
#
# pfSense-upgrade
#
# part of pfSense (https://www.pfsense.org)
# Copyright (c) 2015-2016 Rubicon Communications, LLC (Netgate)
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

usage() {
	me=$(basename $0)
	cat << EOD >&2
Usage: ${me} [-46bdfhnRUy] [-l logfile] [-p socket] [-c|-u|[-i|-d] pkg_name]"
	-4          - Force IPv4"
	-6          - Force IPv6"
	-b          - Platform is booting"
	-d          - Turn on debug"
	-f          - Force package installation"
	-h          - Show this usage help"
	-l logfile  - Logfile path (defaults to /cf/conf/upgrade_log.txt)"
	-n          - Dry run"
	-p socket   - Write pkg progress to socket"
	-R          - Do not reboot (this can be dangerous)"
	-U          - Do not update repository information"
	-y          - Assume yes as the answer to any possible interaction"
"
The following parameters are mutually exclusive:"
	-c          - Check if upgrade is necessary"
	-i pkg_name - Install package PKG_NAME"
	-r pkg_name - Remove package PKG_NAME"
	-u          - Update repository information"
EOD
}

_echo() {
	local _n=""
	if [ "${1}" = "-n" ]; then
		shift
		_n="-n"
	fi

	if [ -z "${logfile}" ]; then
		logfile=/dev/null
	fi

	echo ${_n} "${@}" | tee -a ${logfile}
}

_exec() {
	local _cmd="${1}"
	local _msg="${2}"
	local _mute="${3}"
	local _ignore_result="${4}"
	local _stdout="${stdout}"

	if [ -z "${_cmd}" -o -z "${_msg}" ]; then
		return 1
	fi

	if [ "${_mute}" != "mute" ]; then
		_stdout=''
	fi

	_echo -n ">>> ${_msg}... "
	if [ -z "${_stdout}" ]; then
		_echo ""
		# Ref. https://stackoverflow.com/a/30658405
		exec 4>&1
		local _result=$( \
		    { { ${_cmd} 2>&1 3>&-; printf $? 1>&3; } 4>&- \
		    | tee -a ${logfile} 1>&4; } 3>&1)
		exec 4>&-
	else
		# Ref. https://stackoverflow.com/a/30658405
		exec 4>&1
		local _result=$( \
		    { { ${_cmd} >${_stdout} 2>&1 3>&-; printf $? 1>&3; } 4>&- \
		    | tee -a ${logfile} 1>&4; } 3>&1)
		exec 4>&-
	fi

	if [ ${_result} -eq 0 -o -n "${_ignore_result}" ]; then
		[ -n "${_stdout}" ] \
		    && _echo "done."
		return 0
	else
		[ -n "${_stdout}" ] \
		    && _echo "failed."
		_exit 1
	fi
}

_exit() {
	trap "-" 1 2 15 EXIT

	if [ -n "${delete_pid}" -a -f "${pid_file}" ]; then
		rm -f ${pid_file}
	fi

	if [ -z "${booting}" -o "${boot_stage}" != "2" ]; then
		php /etc/rc.conf_mount_ro
	fi

	if [ -n "${nc_pid}" ] && ps -p ${nc_pid} >/dev/null 2>&1; then
		kill ${nc_pid}
	fi

	if [ -n "${delete_annotation}" ]; then
		pkg annotate -q -D ${kernel_pkg} next_stage
	fi

	if [ -n "${unlock_additional_pkgs}" ]; then
		pkg_unlock "${pkg_prefix}*"
	fi

	local _rc=${1:-"0"}

	# If EVENT_PIPE is defined, GUI is calling
	if [ -n "${progress_socket}" ]; then
		local _need_reboot_str=""
		[ -n "${need_reboot}" ] \
		    && _need_reboot_str=" __REBOOT_AFTER=${reboot_after}"
		_echo "__RC=${_rc}${_need_reboot_str}"
	fi

	exit ${_rc}
}

pkg_with_pb() {
	local _event_pipe=""

	if [ -n "${progress_socket}" ]; then
		if [ -e "${progress_socket}" ]; then
			rm -f ${progress_socket}
		fi

		_event_pipe="-o EVENT_PIPE=${progress_socket}"

		nc -lU ${progress_socket} >> ${progress_file} &
		nc_pid=$!

		while [ ! -e "${progress_socket}" ]; do
			sleep 0.1
		done
	fi

	pkg ${_event_pipe} $@
	local _pkg_result=$?
	nc_pid=""
	return ${_pkg_result}
}

fetch_upgrade_packages() {
	_exec "pkg_with_pb upgrade -F" "Downloading upgrade packages"
}

pkg_lock() {
	local _pkgs="$@"

	if [ -z "${_pkgs}" ]; then
		return
	fi

	for _pkg in ${_pkgs}; do
		for _pkg_name in $(pkg query -g %n "${_pkg}"); do
			_locked=$(pkg query %k ${_pkg_name})
			if [ "${_locked}" != "0" ]; then
				continue
			fi
			_exec "pkg lock ${_pkg_name}" \
			    "Locking package ${_pkg_name}" mute
		done
	done
}

pkg_unlock() {
	local _pkgs="$@"

	if [ -z "${_pkgs}" ]; then
		return
	fi

	for _pkg in ${_pkgs}; do
		for _pkg_name in $(pkg query -g %n "${_pkg}"); do
			_locked=$(pkg query %k ${_pkg_name})
			if [ "${_locked}" != "1" ]; then
				continue
			fi
			_exec "pkg unlock ${_pkg_name}" \
			    "Unlocking package ${_pkg_name}" mute
		done
	done
}

set_vital_flag() {
	for _pkg in "$@"; do
		local _vflag=$(pkg query %V ${_pkg} 2>/dev/null)

		[ "${_vflag}" != "0" ] \
		    && continue

		_exec "pkg set -y -v 1 ${_pkg}" \
		    "Setting vital flag on ${_pkg}" mute
	done
}

abi_setup() {
	local _freebsd_version=$(uname -r)
	local _pkg_repo_conf="/usr/local/etc/pkg/repos/${product}.conf"

	local _arch=$(uname -p)

	CUR_ABI="FreeBSD:${_freebsd_version%%.*}:${_arch}"
	CUR_ALTABI="freebsd:${_freebsd_version%%.*}"

	if [ "${_arch}" = "armv6" ]; then
		CUR_ALTABI="${CUR_ALTABI}:${_arch}:32:el:eabi:hardfp"
	elif [ "${_arch}" = "i386" ]; then
		CUR_ALTABI="${CUR_ALTABI}:x86:32"
	else
		CUR_ALTABI="${CUR_ALTABI}:x86:64"
	fi

	if [ ! -e ${_pkg_repo_conf} ]; then
		validate_repo_conf
	fi

	local _repo_abi_file=$(readlink ${_pkg_repo_conf})

	if [ -f ${_repo_abi_file%%.conf}.abi ]; then
		ABI=$(cat ${_repo_abi_file%%.conf}.abi)
	else
		ABI=${CUR_ABI}
	fi

	if [ -f ${_repo_abi_file%%.conf}.altabi ]; then
		ALTABI=$(cat ${_repo_abi_file%%.conf}.altabi)
	else
		ALTABI=${CUR_ALTABI}
	fi

	# Make sure pkg.conf is set properly so GUI can work
	echo "ABI=${ABI}" > /usr/local/etc/pkg.conf
	echo "ALTABI=${ALTABI}" >> /usr/local/etc/pkg.conf

	if [ "${CUR_ABI}" = "${ABI}" -o "${CUR_ABI}" = "${ALTABI}" ] ; then
		NEW_MAJOR=""
	else
		NEW_MAJOR=1
		ABI="${ALTABI}"
	fi

	export CUR_ABI CUR_ALTABI ABI ALTABI NEW_MAJOR
}

pkg_update() {
	local _force=""
	local _mute=""

	[ "${1}" = "force" ] \
	    && _force=" -f"

	if [ -z "${_force}" -a -n "${dont_update}" ]; then
		return 0
	fi

	[ "${2}" = "mute" ] \
	    && _mute="mute"

	abi_setup

	_exec "pkg update${_force}" "Updating repositories metadata" ${_mute}
}

pkg_upgrade_repo() {
	if [ -z "${NEW_MAJOR}" -a "$(compare_pkg_version pkg)" = "<" ]; then
		_exec "pkg upgrade${dont_update} pkg" "Upgrading pkg" mute
		pkg_update force
	fi

	local _repo_pkg="${product}-repo"

	# Deprecated pa
	if is_pkg_installed ${product}-repo-devel; then
		_exec "pkg set -A 1 ${product}-repo-devel" \
		    "Scheduling package ${product}-repo-devel for removal"
		_exec "pkg install${dont_update} ${_repo_pkg}" \
		    "Installing ${_repo_pkg}" mute
		_exec "pkg delete ${product}-repo-devel" \
		    "Removing ${product}-repo-devel" mute ignore_result
		validate_repo_conf
		pkg_update force
	fi

	[ -n "${NEW_MAJOR}" ] \
	    && return

	case "$(compare_pkg_version ${_repo_pkg})" in
		"<")
			local _force=""
			;;
		">")
			local _force=" -f"
			;;
		"=")
			return
			;;
		*)
			_echo "ERROR: Unable to compare version of ${_repo_pkg}"
			_exit 1
	esac

	cp /usr/local/etc/pkg/repos/${product}.conf /tmp/${product}.conf.copy
	_exec "pkg upgrade${dont_update}${_force} ${_repo_pkg}" \
	    "Upgrading ${_repo_pkg}" mute
	abi_setup
	# If conf differs, for an update
	if ! cmp -s /usr/local/etc/pkg/repos/${product}.conf \
	    /tmp/${product}.conf.copy; then
		pkg_update force

		# New repo may contain newer pkg
		if [ -z "${NEW_MAJOR}" -a "$(compare_pkg_version pkg)" = "<" ]
		then
			_exec "pkg upgrade pkg" "Upgrading pkg" mute
			pkg_update force
		fi
	fi
	rm -f /tmp/${product}.conf.copy
}

upgrade_available() {
	# XXX locked packages message should be printed on stderr by pkg?
	local _lines=$(pkg upgrade${dont_update} -nq "$@" 2>/dev/null \
	    | sed -e '/^$/d; /is locked and may not be modified/d' \
	    | wc -l)

	test ${_lines} -gt 0
	return $?
}

pkg_upgrade() {
	# figure out which kernel variant is running
	export kernel_pkg=$(pkg query %n $(pkg info ${product}-kernel-\* | \
	    grep -v -- -debug-))

	# Detect if has u-boot package installed
	unset uboot_bin
	unset uboot_pkg
	unset uboot_mntp
	if pkg info ${product}-u-boot-ufw\* >/dev/null 2>&1; then
		export uboot_pkg=$(pkg query %n \
		    $(pkg info ${product}-u-boot-ufw\*))
		export uboot_mntp=/boot/msdos
		export uboot_bin=u-boot.img
	fi
	if pkg info ${product}-u-boot-sg3100\* >/dev/null 2>&1; then
		export uboot_pkg=$(pkg query %n \
		    $(pkg info ${product}-u-boot-sg3100\*))
		export uboot_mntp=/boot/u-boot
		export uboot_bin=u-boot.bin
	fi

	if [ -n "${uboot_pkg}" -a \
	    "$(compare_pkg_version ${uboot_pkg})" = "<" ]; then
		if [ ! -f ${uboot_mntp}/${uboot_bin} ]; then
			# Try to mount /boot/msdos
			mount ${uboot_mntp} >/dev/null 2>&1
		fi

		if [ ! -f ${uboot_mntp}/${uboot_bin} ]; then
			_echo "ERROR: u-boot partition (${uboot_mntp}) is not" \
			    "properly mounted"
			_exit 1
		fi
	fi

	if [ -z "${kernel_pkg}" ]; then
		_echo "ERROR: It was not possible to identify which" \
		    "${product} kernel is installed"
		_exit 1
	fi

	export next_stage=$(pkg annotate -q -S ${kernel_pkg} next_stage)
	export is_pkg_locked=$(pkg annotate -q -S ${kernel_pkg} new_major)

	if [ -n "${next_stage}" -a -n "${booting}" -a -n "${boot_stage}" ]; then
		if [ ${boot_stage} != ${next_stage} ]; then
			_exit 0
		fi
	fi

	# If it's booting and first stage didn't run, just exit
	if [ -n "${booting}" -a -z "${next_stage}" ]; then
		_exit 0
	fi

	need_reboot=1
	# First upgrade stage
	if [ -z "${next_stage}" ]; then
		if [ -f "${logfile}" ]; then
			rm -f ${logfile}
		fi

		pkg_update

		# Lock pkg to avoid having pkg binary for version N+1 installed
		# on N
		if [ -n "${NEW_MAJOR}" ]; then
			pkg_lock pkg
		fi

		pkg_upgrade_repo

		# If a new version of pfSense-upgrade is available, upgrade it
		# and return a special code 99 used by wrapper to run newer
		# version again using the same parameters
		if [ "$(compare_pkg_version ${product}-upgrade)" = "<" ]; then
			_exec "pkg upgrade${dont_update} ${product}-upgrade" \
			    "Upgrading ${product}-upgrade" mute
			[ -n "${NEW_MAJOR}" ] \
			    && pkg_unlock pkg
			_exit 99
		fi

		# Old versions used to lock kernel and uboot, make sure we
		# get it reverted
		if pkg lock -l 2>&1 | grep -q "kernel"; then
			pkg_unlock ${kernel_pkg}
		fi
		if [ -n "${uboot_pkg}" ] && \
		    pkg lock -l 2>&1 | grep -q "u-boot"; then
			pkg_unlock ${uboot_pkg}
		fi

		# Always make sure important packages are set as vital
		set_vital_flag pkg ${kernel_pkg} ${product} ${product}-rc \
		    ${product}-base ${uboot_pkg}

		if ! upgrade_available; then
			[ -n "${NEW_MAJOR}" ] \
			    && pkg_unlock pkg
			_echo "Your packages are up to date"
			_exit 0
		fi

		if [ -n "${dry_run}" ]; then
			[ -n "${NEW_MAJOR}" ] \
			    && pkg_unlock pkg
			pkg upgrade${dont_update} -nq 2>&1 | tee -a ${logfile}
			_exit 0
		fi

		local _meta_pkg=$(get_meta_pkg_name)
		if [ "${platform}" = "nanobsd" ]; then
			_echo "**** WARNING ****"
			_echo ""
			_echo "NanoBSD platform is no longer supported."
			_echo ""
			_echo "You can find instructions on how to convert it"
			    "to Full Installation at http://bit.ly/2wDinm8"
			_exit 1
		fi

		# ZFS panics with reroot, do not use it in this case
		# https://redmine.pfsense.org/issues/6045
		if ! kldstat -qm zfs; then
			# If kernel is not going to be upgraded, reroot
			if ! upgrade_available ${kernel_pkg}; then
				reboot_params="-r"
			fi
		fi

		if [ -z "${yes}" ]; then
			# Show user which packages are going to be upgraded
			pkg upgrade${dont_update} -nq 2>&1 | tee -a ${logfile}

			_echo ""
			_echo "**** WARNING ****"
			_echo "Reboot will be required!!"
			_echo -n "Proceed with upgrade? (y/N) "
			read answer
			if [ "${answer}" != "y" ]; then
				_echo "Aborting..."
				_exit 0
			fi
		fi

		# Unlock pkg to make sure it's downloaded
		if [ -n "${NEW_MAJOR}" ]; then
			pkg_unlock pkg
		fi

		# Download all upgrade packages first
		fetch_upgrade_packages

		# Then lock it again
		if [ -n "${NEW_MAJOR}" ]; then
			pkg_lock pkg
		fi

		if upgrade_available ${kernel_pkg}; then
			# pfSense-SG-3100 is obsolete and must be removed
			# before upgrade kernel / rc and preserve
			# loader.rc.local just to be extra safe
			if is_pkg_installed ${product}-SG-3100; then
				rm -f /tmp/loader.rc.local >/dev/null 2>&1
				[ -f /boot/loader.rc.local ] \
				    && cp /boot/loader.rc.local /tmp 2>/dev/null
				_exec "pkg delete ${product}-SG-3100" \
				    "Removing ${product}-SG-3100" mute \
				    ignore_result
				[ -f /tmp/loader.rc.local ] \
				    && cp /tmp/loader.rc.local /boot 2>/dev/null
			fi
			_exec "pkg upgrade -U ${kernel_pkg}" \
			    "Upgrading ${product} kernel"
		fi

		if [ -n "${uboot_pkg}" ] && upgrade_available ${uboot_pkg}; then
			_exec "pkg upgrade -U ${uboot_pkg}" \
			    "Upgrading ${product} u-boot"
		fi

		if [ -n "${NEW_MAJOR}" ]; then
			pkg annotate -q -M ${kernel_pkg} new_major 1
		fi

		pkg annotate -q -M ${kernel_pkg} next_stage 2
		next_stage=2

		do_reboot
		_exit 0
	fi

	if [ "${next_stage}" = "2" ]; then
		pkg_lock "${pkg_prefix}*"
		unlock_additional_pkgs=1

		# XXX: Workaround to upgrade strongswan
		# If those symlinks are present, pkg exit because it expects
		# them to be a directory
		if upgrade_available strongswan; then
			[ -L /usr/local/etc/ipsec.d ] \
			    && rm -f /usr/local/etc/ipsec.d
			[ -L /usr/local/etc/ipsec.conf ] \
			    && rm -f /usr/local/etc/ipsec.conf
			[ -L /usr/local/etc/strongswan.d ] \
			    && rm -f /usr/local/etc/strongswan.d
			[ -L /usr/local/etc/strongswan.conf ] \
			    && rm -f /usr/local/etc/strongswan.conf
		fi

		if upgrade_available; then
			delete_annotation=1
			if [ -n "${is_pkg_locked}" ]; then
				# Upgrade pkg
				pkg_unlock pkg
				pkg annotate -q -D ${kernel_pkg} new_major
				_exec "pkg-static install -f pkg" \
				    "Reinstalling pkg due to ABI change"

				# Upgrade base packages using pkg-static
				_exec "pkg-static upgrade -r ${product}-core" \
				    "Upgrading necessary core packages"

				# Make sure PHP setup is fine
				/etc/rc.php_ini_setup >/dev/null 2>&1
			fi

			_exec "pkg upgrade${dont_update}" \
			    "Upgrading necessary packages"
			delete_annotation=""

			# Always make sure important packages are set as vital
			set_vital_flag pkg ${kernel_pkg} ${product} \
			    ${product}-rc ${product}-base ${uboot_pkg}
		fi

		pkg annotate -q -M ${kernel_pkg} next_stage 3
		next_stage=3

		pkg_unlock "${pkg_prefix}*"
		unlock_additional_pkgs=""

		# 2nd reboot during new major version upgrade
		if [ -n "${is_pkg_locked}" ]; then
			# Reboot immediately
			do_reboot now
			_exit 0
		fi

		if [ -n "${booting}" ]; then
			_exit 0
		fi
	fi

	if [ "${next_stage}" = "3" ]; then
		if upgrade_available; then
			delete_annotation=1
			_exec "pkg upgrade${dont_update}" \
			    "Upgrading necessary packages"
			delete_annotation=""
		fi

		pkg annotate -q -D ${kernel_pkg} next_stage

		# cleanup caches
		_exec "pkg autoremove" "Removing unnecessary packages" mute \
		    ignore_result
		_exec "pkg clean" "Cleanup pkg cache" mute ignore_result
	fi

	gitsync=$(read_xml_tag.sh boolean system/gitsync/synconupgrade)
	if [ "${gitsync}" = "true" ]; then
		repository_url=$(read_xml_tag.sh string \
		    system/gitsync/repositoryurl)
		branch=$(read_xml_tag.sh string system/gitsync/branch)

		minimal=$(read_xml_tag.sh boolean system/gitsync/minimal)
		diff=$(read_xml_tag.sh boolean system/gitsync/diff)
		show_files=$(read_xml_tag.sh boolean system/gitsync/show_files)
		show_command=$(read_xml_tag.sh boolean \
		    system/gitsync/show_command)
		dryrun=$(read_xml_tag.sh boolean system/gitsync/dryrun)

		mute='mute'
		options=''
		if [ "${minimal}" = "true" ]; then
			options=${options}' --minimal'
		fi
		if [ "${diff}" = "true" ]; then
			options=${options}' --diff'
		fi
		if [ "${show_files}" = "true" ]; then
			options=${options}' --show-files'
			mute='off'
		fi
		if [ "${show_command}" = "true" ]; then
			options=${options}' --show-command'
			mute='off'
		fi
		if [ "${dryrun}" = "true" ]; then
			options=${options}' --dry-run'
		fi

		# Repository URL is not mandatory
		if [ -n "${branch}" ]; then
			_exec "pfSsh.php playback gitsync ${repositoryurl} \
			    ${branch} ${options} --upgrading" \
			    "Running gitsync" ${mute} ignore_result
		fi
	fi

	# Save a copy of latest finished upgrade process
	cp -f ${logfile} ${upgrade_logfile}
}

get_meta_pkg_name() {
	# figure out main meta package name
	if is_pkg_installed ${product}-vmware; then
		echo "${product}-vmware"
	elif is_pkg_installed ${product}; then
		echo "${product}"
	else
		_echo "ERROR: It was not possible to identify which" \
		    "${product} meta package is installed"
		_exit 1
	fi
}

check_upgrade() {
	local _meta_pkg=$(get_meta_pkg_name)
	local _core_pkgs=$(pkg query -e \
	    "%n ~ ${product}-kernel-* || %n ~ ${product}-base*" %n)

	pkg_update "" mute

	pkg_upgrade_repo

	for _pkg in ${_meta_pkg} ${_core_pkgs}; do
		[ "$(compare_pkg_version ${_pkg})" != "<" ] \
		    && continue

		local _new_version=$(pkg rquery -U %v ${_pkg})
		_echo "${_new_version} version of ${product} is available"
		_exit 2
	done

	_echo "Your system is up to date"
	_exit 0
}

is_pkg_installed() {
	local _pkg_name="${1}"

	pkg info -e ${_pkg_name}
	return $?
}

compare_pkg_version() {
	local _pkg_name="${1}"

	if [ -z "${_pkg_name}" ]; then
		echo '!'
		return 1
	fi

	if ! is_pkg_installed ${_pkg_name}; then
		echo '!'
		return 1
	fi

	local _lver=$(pkg query %v ${_pkg_name})

	if [ -z "${_lver}" ]; then
		_echo "ERROR: It was not possible to determine ${_pkg_name}" \
		    "local version"
		_exit 1
	fi

	local _rver=$(pkg rquery -U %v ${_pkg_name})

	if [ -z "${_rver}" ]; then
		_echo "ERROR: It was not possible to determine ${_pkg_name}" \
		    "remote version"
		_exit 1
	fi

	local _version=$(pkg version -t ${_lver} ${_rver})

	if [ $? -ne 0 ]; then
		_echo "ERROR: Error comparing ${_pkg_name} local and remote" \
		    "versions"
		_exit 1
	fi

	echo ${_version}
	return 0
}

pkg_install() {
	local _pkg_name="${1}"

	local _force=""
	if [ -n "${2}" ]; then
		_force="-f"
	fi

	if [ -z "${_pkg_name}" ]; then
		_echo "ERROR: Blank package name"
		_exit 1
	fi

	if is_pkg_installed ${_pkg_name}; then
		local _cversion=$(compare_pkg_version ${_pkg_name})

		if [ -z "${_force}" ]; then
			if [ "${_cversion}" = "=" ]; then
				_echo "Package ${_pkg_name} is up to date"
				_exit 0
			elif [ "${_cversion}" = ">" ]; then
				_echo "Installed ${_pkg_name} version is" \
				    "newer than remote"
				_exit 0
			fi
		fi
		local _cmd="upgrade ${_force}"
		local _msg="Upgrading"
	else
		local _cmd="install"
		local _msg="Installing"
	fi

	_exec "pkg_with_pb ${_cmd}${dry_run:+ }${dry_run} ${_pkg_name}" \
	    "${_msg} ${_pkg_name}"
	_exec "pkg clean" "Cleaning up cache" mute ignore_result
}

# Reinstall every pfSense-pkg-* package
pkg_reinstall_all() {
	for _pkg in $(pkg query -e '%a == 0' %n); do
		case ${_pkg} in "${pkg_prefix}"* )
			_echo "Reinstalling ${_pkg}"
			pkg_install ${_pkg} 1
			;;
		esac
	done
}

pkg_delete() {
	local _pkg_name="${1}"

	if [ -z "${_pkg_name}" ]; then
		_echo "ERROR: Blank package name"
		_exit 1
	fi

	if ! is_pkg_installed ${_pkg_name}; then
		_echo "ERROR: Package ${_pkg_name} is not installed"
		_exit 1
	fi

	_exec "pkg_with_pb delete${dry_run:+ }${dry_run} ${_pkg_name}" \
	    "Removing ${_pkg_name}"
	_exec "pkg autoremove" "Removing stale packages" mute ignore_result
}

# Delete every pfSense-pkg-* package
pkg_delete_all() {
	for _pkg in $(pkg query -e '%a == 0' %n); do
		case ${_pkg} in "${pkg_prefix}"* )
			_echo "Removing ${_pkg}"
			pkg_delete ${_pkg}
			;;
		esac
	done
}

do_reboot() {
	local _now="$1"

	local _msg=""
	if [ "${_now}" = "now" ]; then
		_msg="now."
	else
		_msg="in ${reboot_after} seconds."
	fi

	if [ -z "${dont_reboot}" ]; then
		_echo "Upgrade is complete.  Rebooting ${_msg}"
		echo "Upgrade is complete.  Rebooting ${_msg}" | wall
		/etc/rc.notify_message -e -g -m \
		    "Upgrade is complete.  Rebooting ${_msg}" >/dev/null 2>&1
		if [ "${_now}" = "now" ]; then
			/etc/rc.reboot ${reboot_params}
		else
			(sleep ${reboot_after} \
			    && /etc/rc.reboot ${reboot_params}) &
		fi
	else
		_echo "Upgrade is complete."
		echo "Upgrade is complete." | wall
		/etc/rc.notify_message -e -g -m "Upgrade is complete." \
		    >/dev/null 2>&1
	fi
}

validate_repo_conf() {
	# Make sure to use default repo conf when it doesn't exist
	pkg_repo_conf="/usr/local/etc/pkg/repos/${product}.conf"
	default="/usr/local/share/${product}/pkg/repos/${product}-repo.conf"

	pkg_repo_conf_path=$(read_xml_tag.sh string system/pkg_repo_conf_path)

	if [ -z "${pkg_repo_conf_path}" -o ! -f "${pkg_repo_conf_path}" ]; then
		pkg_repo_conf_path=${default}
	fi

	if [ -f "${pkg_repo_conf_path}" ]; then
		if [ -e "${pkg_repo_conf}" -a ! -L "${pkg_repo_conf}" ]; then
			rm -f ${pkg_repo_conf}
			ln -sf ${pkg_repo_conf_path} ${pkg_repo_conf}
		fi

		if [ "$(readlink ${pkg_repo_conf})" != \
		    "${pkg_repo_conf_path}" ]; then
			mkdir -p /usr/local/etc/pkg/repos
			ln -sf ${pkg_repo_conf_path} ${pkg_repo_conf}
		fi
	fi
}

export LANG=C

pid_file="/var/run/$(basename $0).pid"
logfile="/cf/conf/upgrade_log.txt"
upgrade_logfile="/cf/conf/upgrade_log.latest.txt"
stdout='/dev/null'

# Export necessary PATH
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# Setup proxy settings
HTTP_PROXY=$(read_xml_tag.sh string system/proxyurl)
if [ "${HTTP_PROXY}" != "" ]; then
	HTTP_PROXY_PORT=$(read_xml_tag.sh string system/proxyport)
	if [ "${HTTP_PROXY_PORT}" != "" ]; then
		HTTP_PROXY="${HTTP_PROXY}:${HTTP_PROXY_PORT}"
	fi
	export HTTP_PROXY
fi

# Only set proxy variables on FreeBSD 11 until the fix is on 10
# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=194483
if $(uname -r | grep -q '^11'); then
	HTTP_PROXY_USER=$(read_xml_tag.sh string system/proxyuser)
	HTTP_PROXY_PASS=$(read_xml_tag.sh string system/proxypass)
	if [ "${HTTP_PROXY_USER}" != "" ] &&
	    [ "${HTTP_PROXY_PASS}" != "" ]; then
		HTTP_PROXY_AUTH="${HTTP_PROXY_USER}:${HTTP_PROXY_PASS}"
		HTTP_PROXY_AUTH="basic:*:${HTTP_PROXY_AUTH}"
		export HTTP_PROXY_AUTH
	fi
fi

# pkg should not ask for confirmations
export ASSUME_ALWAYS_YES=true
export FETCH_TIMEOUT=5
export FETCH_RETRY=2

export product=$(php -n /usr/local/sbin/read_global_var product_name pfSense)
export pkg_prefix=$(php -n /usr/local/sbin/read_global_var pkg_prefix \
    pfSense-pkg-)
export platform=$(cat /etc/platform)

USE_MFS_TMPVAR=$(read_xml_tag.sh boolean system/use_mfs_tmpvar)
if [ "${USE_MFS_TMPVAR}" = "true" ]; then
	export PKG_DBDIR=/root/var/db/pkg
	export PKG_CACHEDIR=/root/var/cache/pkg
fi

product_version=$(cat /etc/version)
do_not_send_uniqueid=$(read_xml_tag.sh boolean system/do_not_send_uniqueid)
if [ "${do_not_send_uniqueid}" != "true" ]; then
	uniqueid=$(gnid)
	export HTTP_USER_AGENT="${product}/${product_version}:${uniqueid}"
else
	export HTTP_USER_AGENT="${product}/${product_version}"
fi

validate_repo_conf

# Flags used in _exit
export delete_annotation=""
export unlock_additional_pkgs=""
export delete_pid=""

# Save nc_pid to be able to kill it
export nc_pid=""

# Reboot after 10 seconds
export reboot_after=10

# Used to set -r and reroot
export reboot_params=""

unset dry_run
unset dont_reboot
unset dont_update
unset booting
unset boot_stage
unset force
unset yes
unset progress_file
unset progress_socket
unset action
unset action_pkg
unset force_ipv4
unset force_ipv6
while getopts 46b:cdfi:hp:l:nr:RuUy opt; do
	case ${opt} in
		4)
			if [ -n "${force_ipv6}" ]; then
				usage
				exit 1
			fi
			force_ipv4=1
			;;
		6)
			if [ -n "${force_ipv4}" ]; then
				usage
				exit 1
			fi
			force_ipv6=1
			;;
		b)
			booting=1
			boot_stage="${OPTARG}"
			;;
		c)
			action="check"
			;;
		d)
			stdout=''
			;;
		f)
			force=1
			;;
		i)
			if [ -n "${action}" ]; then
				usage
				exit 1
			fi
			action="install"
			action_pkg="${OPTARG}"
			;;
		h)
			usage
			exit 0
			;;
		l)
			logfile="${OPTARG}"
			if [ -z "${logfile}" ]; then
				usage
				exit 1
			fi
			;;
		n)
			dry_run="-n"
			;;
		p)
			progress_socket="${OPTARG}"
			if [ -z "${progress_socket}" ]; then
				usage
				exit 1
			fi
			;;
		r)
			if [ -n "${action}" ]; then
				usage
				exit 1
			fi
			action="delete"
			action_pkg="${OPTARG}"
			;;
		R)
			dont_reboot=1
			;;
		u)
			if [ -n "${action}" ]; then
				usage
				exit 1
			fi
			action="update"
			;;
		U)
			dont_update=" -U"
			;;
		y)
			yes=1
			;;
		*)
			usage
			exit 1
			;;
	esac
done

if [ -n "${force_ipv4}" ]; then
	export IP_VERSION="4"
elif [ -n "${force_ipv6}" ]; then
	export IP_VERSION="6"
fi

# Set default action when no parameter is set
: ${action:="upgrade"}

if pgrep -qF ${pid_file} >/dev/null 2>&1; then
	echo "Another instance is already running... Aborting!"
	exit 1
fi

echo $$ > ${pid_file}

# Since this point it's safe to remove pid_file
delete_pid=1

trap _exit 1 2 15 EXIT

block_external_services=$(read_xml_tag.sh boolean \
    system/block_external_services)

if [ "${block_external_services}" = "true" ]; then
	echo "Aborted due to block_external_services flag"
	exit 0
fi

if [ -z "${booting}" -o "${boot_stage}" != "2" ]; then
	php /etc/rc.conf_mount_rw
fi

if [ -n "${booting}" ]; then
	export REPO_AUTOUPDATE=false
fi

if [ "${action}" != "upgrade" -a -f "${logfile}" ]; then
	rm -f ${logfile}
fi

progress_file=${logfile%.*}.json

if [ -e "${progress_file}" ]; then
	rm -f ${progress_file}
fi

abi_setup

if [ -n "${NEW_MAJOR}" -a "${action}" = "install" ]; then
	_echo "WARNING: Current pkg repository has a new OS major version."
	_echo "         ${product} should be upgraded before doing any other"
	_echo "         operation"
	_exit 1
fi

case "${action}" in
	check)
		check_upgrade
		;;
	upgrade)
		pkg_upgrade
		;;
	update)
		pkg_update force
		;;
	install)
		if [ ${action_pkg} = "ALL_PACKAGES" ] && [ -n ${force} ]; then
			pkg_reinstall_all
		else
			pkg_install ${action_pkg} ${force}
		fi
		;;
	delete)
		if [ ${action_pkg} = "ALL_PACKAGES" ] && [ -n ${force} ]; then
			pkg_delete_all
		else
			pkg_delete ${action_pkg}
		fi
		;;
	*)
		_echo "ERROR: Invalid action!"
		_exit 1
esac

_exit 0
OpenPOWER on IntegriCloud