Project

General

Profile

Download (22.9 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2

    
3
# Copyright (c) 2015 Electric Sheep Fencing, LLC. All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions are met:
7
#
8
# 1. Redistributions of source code must retain the above copyright notice,
9
#    this list of conditions and the following disclaimer.
10
#
11
# 2. Redistributions in binary form must reproduce the above copyright
12
#    notice, this list of conditions and the following disclaimer in
13
#    the documentation and/or other materials provided with the
14
#    distribution.
15
#
16
# 3. All advertising materials mentioning features or use of this software
17
#    must display the following acknowledgment:
18
#    "This product includes software developed by the pfSense Project
19
#    for use in the pfSense® software distribution. (http://www.pfsense.org/).
20
#
21
# 4. The names "pfSense" and "pfSense Project" must not be used to
22
#    endorse or promote products derived from this software without
23
#    prior written permission. For written permission, please contact
24
#    coreteam@pfsense.org.
25
#
26
# 5. Products derived from this software may not be called "pfSense"
27
#    nor may "pfSense" appear in their names without prior written
28
#    permission of the Electric Sheep Fencing, LLC.
29
#
30
# 6. Redistributions of any form whatsoever must retain the following
31
#    acknowledgment:
32
#
33
# "This product includes software developed by the pfSense Project
34
# for use in the pfSense software distribution (http://www.pfsense.org/).
35
#
36
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
37
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
40
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47
# OF THE POSSIBILITY OF SUCH DAMAGE.
48

    
49
usage() {
50
	echo "Usage: $(basename ${0}) [-46bdyf] [-u|-i PKG_NAME|-r PKG_NAME]" >&2
51
	echo "	-4          - Force IPv4"
52
	echo "	-6          - Force IPv6"
53
	echo "	-b          - Platform is booting" >&2
54
	echo "	-c          - Check if upgrade is necessary" >&2
55
	echo "	-d          - Turn on debug" >&2
56
	echo "	-f          - Force package installation" >&2
57
	echo "	-h          - Show this usage help" >&2
58
	echo "	-l          - Logfile path (defaults to /cf/conf/upgrade_log.txt)" >&2
59
	echo "	-n          - Dry run" >&2
60
	echo "	-p socket   - Write pkg progress to socket"
61
	echo "	-R          - Do not reboot (this can be dangerous)"
62
	echo "	-y          - Assume yes as the answer to any possible interaction" >&2
63
	echo "" >&2
64
	echo "The following parameters are mutually exclusive:" >&2
65
	echo "	-i PKG_NAME - Install package PKG_NAME" >&2
66
	echo "	-r PKG_NAME - Remove package PKG_NAME" >&2
67
	echo "	-u          - Update repository information" >&2
68
}
69

    
70
_echo() {
71
	local _n=""
72
	if [ "${1}" = "-n" ]; then
73
		shift
74
		_n="-n"
75
	fi
76

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

    
81
	echo ${_n} "${1}" | tee -a ${logfile}
82
}
83

    
84
_exec() {
85
	local _cmd="${1}"
86
	local _msg="${2}"
87
	local _mute="${3}"
88
	local _ignore_result="${4}"
89
	local _stdout="${stdout}"
90

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

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

    
99
	_echo -n ">>> ${_msg}... "
100
	if [ -z "${_stdout}" ]; then
101
		_echo ""
102
		# Ref. http://stackoverflow.com/questions/1221833/bash-pipe-output-and-capture-exit-status
103
		exec 4>&1
104
		local _result=$({ { ${_cmd} 2>&1 3>&-; printf $? 1>&3; } 4>&- | \
105
			tee -a ${logfile} 1>&4; } 3>&1)
106
		exec 4>&-
107
	else
108
		# Ref. http://stackoverflow.com/questions/1221833/bash-pipe-output-and-capture-exit-status
109
		exec 4>&1
110
		local _result=$({ { ${_cmd} >${_stdout} 2>&1 3>&-; printf $? 1>&3; } 4>&- | \
111
			tee -a ${logfile} 1>&4; } 3>&1)
112
		exec 4>&-
113
	fi
114

    
115
	if [ ${_result} -eq 0 -o -n "${_ignore_result}" ]; then
116
		[ -n "${_stdout}" ] \
117
			&& _echo "done."
118
		return 0
119
	else
120
		[ -n "${_stdout}" ] \
121
			&& _echo "failed."
122
		_exit 1
123
	fi
124
}
125

    
126
_exit() {
127
	trap "-" 1 2 15 EXIT
128

    
129
	pkg_lock ${kernel_pkg}
130

    
131
	if [ -f "${pid_file}" ]; then
132
		rm -f ${pid_file}
133
	fi
134

    
135
	if [ -n "${chroot_dir}" ]; then
136
		umount -f ${chroot_dir} >/dev/null 2>&1
137
	fi
138

    
139
	if [ -z "${booting}" -o "${boot_stage}" != "2" ]; then
140
		/usr/local/bin/php /etc/rc.conf_mount_ro
141
	fi
142

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

    
147
	if [ -n "${delete_annotation}" ]; then
148
		pkg ${pkg_chroot} annotate -q -D ${kernel_pkg} next_stage
149
	fi
150

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

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

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

    
165
	exit ${_rc}
166
}
167

    
168
pkg_with_pb() {
169
	local _event_pipe=""
170

    
171
	if [ -n "${progress_socket}" ]; then
172
		if [ -e "${chroot_dir}${progress_socket}" ]; then
173
			rm -f ${chroot_dir}${progress_socket}
174
		fi
175

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

    
178
		nc -lU ${chroot_dir}${progress_socket} >> ${progress_file} &
179
		nc_pid=$!
180

    
181
		while [ ! -e "${chroot_dir}${progress_socket}" ]; do
182
			sleep 0.1
183
		done
184
	fi
185

    
186
	pkg ${_event_pipe} $@
187
	local _pkg_result=$?
188
	nc_pid=""
189
	return ${_pkg_result}
190
}
191

    
192
fetch_upgrade_packages() {
193
	local _pkgs_to_fetch=""
194
	if [ "${platform}" = "nanobsd" ]; then
195
		local _pkg=""
196

    
197
		# Check if all non-auto packages installed on 2nd partition are
198
		# installed on current one, if not, mark them to be deleted by
199
		# pkg autoremove
200
		for _pkg in $(pkg ${pkg_chroot} query -e '%a == 0' %n); do
201
			if ! pkg info -e ${_pkg}; then
202
				_exec "pkg ${pkg_chroot} set -A 1 ${_pkg}" "Scheduling package ${_pkg} for removal"
203
			fi
204
		done
205

    
206
		# Check if all non-auto packages installed on current partition are
207
		# installed on 2nd one, if not, we need to fetch them
208
		for _pkg in $(pkg query -e '%a == 0' %n); do
209
			if ! pkg ${pkg_chroot} info -e ${_pkg}; then
210
				_pkgs_to_fetch="${_pkgs_to_fetch}${_pkgs_to_fetch:+ }${_pkg}"
211
			fi
212
		done
213

    
214
	fi
215

    
216
	_exec "pkg_with_pb ${pkg_chroot} upgrade -F" "Downloading upgrade packages"
217

    
218
	if [ -n "${_pkgs_to_fetch}" ]; then
219
		_exec "pkg_with_pb ${pkg_chroot} fetch -d ${_pkgs_to_fetch}" \
220
			"Fetching packages not present on upgrade partition"
221
	fi
222
}
223

    
224
pkg_lock() {
225
	local _pkg="${1}"
226

    
227
	if [ -z "${_pkg}" ]; then
228
		return
229
	fi
230

    
231
	if [ "$(pkg ${pkg_chroot} query %k ${_pkg})" = "0" ]; then
232
		_exec "pkg ${pkg_chroot} lock ${_pkg}" "Locking package ${_pkg}" mute
233
	fi
234
}
235

    
236
pkg_unlock() {
237
	local _pkg="${1}"
238

    
239
	if [ -z "${_pkg}" ]; then
240
		return
241
	fi
242

    
243
	if [ "$(pkg ${pkg_chroot} query %k ${_pkg})" = "1" ]; then
244
		_exec "pkg ${pkg_chroot} unlock ${_pkg}" "Unlocking package ${_pkg}" mute
245
	fi
246
}
247

    
248
pkg_update() {
249
	local _run_update=1
250

    
251
	local _force=""
252
	if [ "${1}" = "force" ]; then
253
		_force=" -f"
254
	fi
255

    
256
	_exec "pkg ${pkg_chroot} update${_force}" "Updating repositories metadata"
257
}
258

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

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

    
268
	export next_stage=$(pkg annotate -q -S ${kernel_pkg} next_stage)
269

    
270
	if [ -n "${next_stage}" -a -n "${booting}" -a -n "${boot_stage}" ]; then
271
		if [ ${boot_stage} != ${next_stage} ]; then
272
			_exit 0
273
		fi
274
	fi
275

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

    
281
	unset need_reboot
282
	# First upgrade stage
283
	if [ -z "${next_stage}" ]; then
284
		if [ -f "${logfile}" ]; then
285
			rm -f ${logfile}
286
		fi
287

    
288
		pkg_update
289

    
290
		if [ "$(compare_pkg_version pkg)" = "<" ]; then
291
			_exec "pkg upgrade pkg" "Upgrading pkg" mute
292
			pkg_update force
293
		fi
294

    
295
		local _repo_pkg="${product}-repo"
296

    
297
		if [ "$(compare_pkg_version ${_repo_pkg})" = "<" ]; then
298
			cp /usr/local/etc/pkg/repos/${product}.conf \
299
				/tmp/${product}.conf.copy
300
			_exec "pkg upgrade ${_repo_pkg}" "Upgrading ${_repo_pkg}" mute
301
			# If conf differs, for an update
302
			if ! cmp -s /usr/local/etc/pkg/repos/${product}.conf /tmp/${product}.conf.copy; then
303
				pkg_update force
304

    
305
				# New repo may contain newer pkg
306
				if [ "$(compare_pkg_version pkg)" = "<" ]; then
307
					_exec "pkg upgrade pkg" "Upgrading pkg" mute
308
					pkg_update force
309
				fi
310
			fi
311
			rm -f /tmp/${product}.conf.copy
312
		fi
313

    
314
		if [ $(pkg upgrade -nq | wc -l) -le 1 ]; then
315
			_echo "Your packages are up to date"
316
			_exit 0
317
		fi
318

    
319
		if [ -n "${dry_run}" ]; then
320
			pkg ${pkg_chroot} upgrade -nq 2>&1 | tee -a ${logfile}
321
			_exit 0
322
		fi
323

    
324
		if [ $(pkg upgrade -r ${product}-core -nq | wc -l) -gt 1 ]; then
325
			if [ "${platform}" = "nanobsd" ]; then
326
				_echo "**** WARNING ****"
327
				_echo "Duplicate slice required!!"
328
				_echo ""
329
				_echo "Before starting the upgrade process, the currently mounted nanobsd partition"
330
				_echo "needs to be cloned to the secondary partition, where the update will happen"
331
				_echo ""
332
				_echo "After installation a reboot will be required to switch partition."
333
				_echo ""
334
				if [ -z "${yes}" ]; then
335
					_echo -n "Proceed with upgrade? (y/N) "
336
					read answer
337
					if [ "${answer}" != "y" ]; then
338
						_echo "Aborting..."
339
						_exit 0
340
					fi
341
					# Do not make the user have to answer again.
342
					yes=1
343
				fi
344
				setup_nanobsd_env
345
			fi
346
			need_reboot=1
347
		fi
348

    
349
		pkg_unlock ${kernel_pkg}
350

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

    
355
			_echo ""
356
			if [ -n "${need_reboot}" ]; then
357
				_echo "**** WARNING ****"
358
				_echo "Reboot will be required!!"
359
			fi
360
			_echo -n "Proceed with upgrade? (y/N) "
361
			read answer
362
			if [ "${answer}" != "y" ]; then
363
				_echo "Aborting..."
364
				_exit 0
365
			fi
366
		fi
367

    
368
		# Download all upgrade packages first
369
		fetch_upgrade_packages
370

    
371
		if [ $(pkg ${pkg_chroot} upgrade -nq ${kernel_pkg} | wc -l) -gt 1 ]; then
372
			_exec "pkg ${pkg_chroot} upgrade ${kernel_pkg}" "Upgrading ${product} kernel"
373
		fi
374

    
375
		pkg ${pkg_chroot} annotate -q -M ${kernel_pkg} next_stage 2
376
		next_stage=2
377

    
378
		if [ -n "${need_reboot}" -a "${platform}" != "nanobsd" ]; then
379
			do_reboot
380
			_exit 0
381
		fi
382
	fi
383

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

    
388
		# XXX: Workaround to upgrade strongswan
389
		# If those symlinks are present, pkg exit because it expects them
390
		# to be a directory
391
		if [ $(pkg ${pkg_chroot} upgrade -nq strongswan | wc -l) -gt 1 ]; then
392
			if [ -L ${chroot_dir}/usr/local/etc/ipsec.d ]; then
393
				rm -f ${chroot_dir}/usr/local/etc/ipsec.d
394
			fi
395
			if [ -L ${chroot_dir}/usr/local/etc/ipsec.conf ]; then
396
				rm -f ${chroot_dir}/usr/local/etc/ipsec.conf
397
			fi
398
			if [ -L ${chroot_dir}/usr/local/etc/strongswan.d ]; then
399
				rm -f ${chroot_dir}/usr/local/etc/strongswan.d
400
			fi
401
			if [ -L ${chroot_dir}/usr/local/etc/strongswan.conf ]; then
402
				rm -f ${chroot_dir}/usr/local/etc/strongswan.conf
403
			fi
404
		fi
405

    
406
		if [ $(pkg ${pkg_chroot} upgrade -nq | wc -l) -gt 1 ]; then
407
			delete_annotation=1
408
			_exec "pkg ${pkg_chroot} upgrade" "Upgrading necessary packages"
409
			delete_annotation=""
410
		fi
411

    
412
		# XXX: workaround for #5300
413
		sort -u ${chroot_dir}/usr/local/etc/php/extensions.ini > /tmp/extensions.ini
414
		mv /tmp/extensions.ini ${chroot_dir}/usr/local/etc/php/extensions.ini
415

    
416
		pkg ${pkg_chroot} annotate -q -M ${kernel_pkg} next_stage 3
417
		next_stage=3
418

    
419
		pkg_unlock "${pkg_prefix}*"
420
		unlock_additional_pkgs=""
421

    
422
		if [ -n "${need_reboot}" -a "${platform}" = "nanobsd" ]; then
423
			switch_active_nanobsd_partition
424
			do_reboot
425
			_exit 0
426
		fi
427

    
428
		if [ -n "${booting}" ]; then
429
			_exit 0
430
		fi
431
	fi
432

    
433
	if [ "${next_stage}" = "3" ]; then
434
		if [ $(pkg upgrade -nq | wc -l) -gt 1 ]; then
435
			delete_annotation=1
436
			_exec "pkg ${pkg_chroot} upgrade" "Upgrading necessary packages"
437
			delete_annotation=""
438
		fi
439

    
440
		pkg ${pkg_chroot} annotate -q -D ${kernel_pkg} next_stage
441

    
442
		# cleanup caches
443
		_exec "pkg ${pkg_chroot} autoremove" "Removing unnecessary packages" mute ignore_result
444
		_exec "pkg ${pkg_chroot} clean" "Cleanup pkg cache" mute ignore_result
445
	fi
446

    
447
	gitsync=$(/usr/local/sbin/read_xml_tag.sh boolean system/gitsync/synconupgrade)
448
	if [ "${gitsync}" = "true" ]; then
449
		repository_url=$(/usr/local/sbin/read_xml_tag.sh string system/gitsync/repositoryurl)
450
		branch=$(/usr/local/sbin/read_xml_tag.sh string system/gitsync/branch)
451

    
452
		# Repository URL is not mandatory
453
		if [ -n "${branch}" ]; then
454
			_exec "/usr/local/sbin/pfSsh.php playback gitsync \
455
				${repositoryurl} ${branch} --upgrading" \
456
				"Running gitsync" mute ignore_result
457
		fi
458
	fi
459
}
460

    
461
check_upgrade() {
462
	# figure out main meta package name
463
	if is_pkg_installed ${product}-vmware; then
464
		local _meta_pkg="${product}-vmware"
465
	elif is_pkg_installed ${product}; then
466
		local _meta_pkg="${product}"
467
	else
468
		_echo "ERROR: It was not possible to identify which ${product} meta package is installed"
469
		_exit 1
470
	fi
471

    
472
	pkg_update
473

    
474
	if [ "$(compare_pkg_version ${_meta_pkg})" = "<" ]; then
475
		local _new_version=$(pkg rquery %v ${_meta_pkg})
476
		_echo "${_new_version} version of ${product} is available"
477
		_exit 2
478
	else
479
		for _pkg in $(pkg query -e "%n ~ ${product}-*" %n); do
480
			# Ignore additional packages
481
			if echo "${_pkg}" | grep -q "^${pkg_prefix}"; then
482
				continue
483
			fi
484
			if [ "$(compare_pkg_version ${_pkg})" = "<" ]; then
485
				local _new_version=$(pkg rquery %v ${_pkg})
486
				_echo "${_new_version} version of ${_pkg} is available"
487
				_exit 2
488
			fi
489
		done
490
	fi
491

    
492
	_echo "Your system is up to date"
493
	_exit 0
494
}
495

    
496
setup_nanobsd_env() {
497
	if [ "${platform}" != "nanobsd" ]; then
498
		return;
499
	fi
500

    
501
	chroot_dir=/tmp/nanobsd_upgrade
502
	mkdir -p ${chroot_dir} 2>/dev/null
503
	local _cur_partition=$(mount -p / | cut -f1)
504
	local _update_partition=$(echo ${_cur_partition} | sed -e 's,0$,2,; s,1$,0,; s,2$,1,')
505

    
506
	if [ ! -e "${_update_partition}" ]; then
507
		_echo "Secondary partition (${_update_partition}), used for upgrade not found"
508
		_exit 1
509
	fi
510

    
511
	# Remove /dev
512
	_update_partition=$(echo ${_update_partition} | sed 's,^/dev/,,')
513
	local _update_slice=$(glabel status -s | awk "\$1 == \"${_update_partition}\" { print \$3 }")
514

    
515
	if [ -z "${_update_slice}" -o ! -e "/dev/${_update_slice}" ]; then
516
		_echo "Secondary slice (${_update_slice}), use_update_sliced for upgrade not found"
517
		_exit 1
518
	fi
519

    
520
	_update_slice="/dev/${_update_slice}"
521

    
522
	# Clone slice using same logic from nanobsd_clone_slice()
523
	sysctl kern.geom.debugflags=16 >/dev/null 2>&1
524
	_exec "dd if=/dev/zero of=${_update_slice} bs=1m count=1" "Cleaning secondary partition" mute
525
	_exec "dd if=${_cur_partition} of=${_update_slice} bs=64k" "Duplicating current slice" mute
526
	_exec "tunefs -L ${_update_partition##*/} ${_update_slice}" "Restoring slice label" mute
527
	sysctl kern.geom.debugflags=0 >/dev/null 2>&1
528

    
529
	_exec "/sbin/fsck -y -t ufs /dev/${_update_partition}" "Testing duplicated partition integrity" mute
530
	_exec "mount /dev/${_update_partition} ${chroot_dir}" "Mounting second partition to run upgrade" mute
531

    
532
	sed -i '' -e "s,^${_cur_partition},/dev/${_update_partition}," \
533
		${chroot_dir}/etc/fstab
534

    
535
	pkg_chroot="-c ${chroot_dir}"
536
}
537

    
538
switch_active_nanobsd_partition() {
539
	if [ "${platform}" != "nanobsd" ]; then
540
		return;
541
	fi
542

    
543
	local _cur_partition=$(mount -p / | cut -f1 | sed 's,^/dev/,,')
544
	local _disk=$(glabel status -s | \
545
		awk "\$1 == \"${_cur_partition}\" { print substr(\$3, 0, length(\$3)-3)}")
546
	local _i=$(echo ${_cur_partition} | cut -c ${#_cur_partition})
547

    
548
	if ! echo "${_i}" | egrep -q '^[0-9]$'; then
549
		_echo "Invalid partition label ${_cur_partition}"
550
		_exit 1
551
	fi
552

    
553
	# pfsense0 == part 1 / pfsense1 == part 2
554
	if [ ${_i} -eq 0 ]; then
555
		_i=2
556
	else
557
		_i=1
558
	fi
559

    
560
	_exec "gpart set -a active -i ${_i} ${_disk}" "Setting secondary partition as active" mute
561
}
562

    
563
is_pkg_installed() {
564
	local _pkg_name="${1}"
565
	shift
566
	local _pkg_chroot="$@"
567

    
568
	pkg ${_pkg_chroot} info -e ${_pkg_name}
569
	return $?
570
}
571

    
572
compare_pkg_version() {
573
	local _pkg_name="${1}"
574

    
575
	if ! is_pkg_installed ${_pkg_name} ${pkg_chroot}; then
576
		echo '!'
577
		return 1
578
	fi
579

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

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

    
587
	local _rver=$(pkg ${pkg_chroot} rquery %v ${_pkg_name})
588

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

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

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

    
601
	echo ${_version}
602
	return 0
603
}
604

    
605
pkg_install() {
606
	local _pkg_name="${1}"
607

    
608
	local _force=""
609
	if [ -n "${2}" ]; then
610
		_force="-f"
611
	fi
612

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

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

    
621
		if [ -z "${_force}" ]; then
622
			if [ "${_cversion}" = "=" ]; then
623
				_echo "Package ${_pkg_name} is up to date"
624
				_exit 0
625
			elif [ "${_cversion}" = ">" ]; then
626
				_echo "Installed ${_pkg_name} version is newer than remote"
627
				_exit 0
628
			fi
629
		fi
630
		local _cmd="upgrade ${_force}"
631
		local _msg="Upgrading"
632
	else
633
		local _cmd="install"
634
		local _msg="Installing"
635
	fi
636

    
637
	_exec "pkg_with_pb ${_cmd}${dry_run:+ }${dry_run} ${_pkg_name}" "${_msg} ${_pkg_name}"
638
	_exec "pkg clean" "Cleaning up cache" mute ignore_result
639
}
640

    
641
# Reinstall every pfSense-pkg-* package
642
pkg_reinstall_all() {
643
	for _pkg in $(pkg query -e '%a == 0' %n); do
644
		case ${_pkg} in "${pkg_prefix}"* )
645
			_echo "Reinstalling ${_pkg}"
646
			pkg_install ${_pkg} 1
647
			;;
648
		esac
649
	done
650
}
651

    
652
pkg_delete() {
653
	local _pkg_name="${1}"
654

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

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

    
665
	_exec "pkg_with_pb delete${dry_run:+ }${dry_run} ${_pkg_name}" "Removing ${_pkg_name}"
666
	_exec "pkg autoremove" "Removing stale packages" mute ignore_result
667
}
668

    
669
# Delete every pfSense-pkg-* package
670
pkg_delete_all() {
671
	for _pkg in $(pkg query -e '%a == 0' %n); do
672
		case ${_pkg} in "${pkg_prefix}"* )
673
			_echo "Removing ${_pkg}"
674
			pkg_delete ${_pkg}
675
			;;
676
		esac
677
	done
678
}
679

    
680
do_reboot() {
681
	if [ -z "${dont_reboot}" ]; then
682
		_echo "Upgrade is complete.  Rebooting in ${reboot_after} seconds."
683
		echo "Upgrade is complete.  Rebooting in ${reboot_after} seconds." | wall
684
		/etc/rc.notify_message -e -g -m "Upgrade is complete.  Rebooting in ${reboot_after} seconds."
685
		(sleep ${reboot_after} && /etc/rc.reboot) &
686
	else
687
		_echo "Upgrade is complete."
688
		echo "Upgrade is complete." | wall
689
		/etc/rc.notify_message -e -g -m "Upgrade is complete."
690
	fi
691
}
692

    
693
export LANG=C
694

    
695
pid_file="/var/run/$(basename $0).pid"
696
logfile="/cf/conf/upgrade_log.txt"
697
stdout='/dev/null'
698

    
699
# pkg should not ask for confirmations
700
export ASSUME_ALWAYS_YES=true
701

    
702
export product=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_name pfSense)
703
export pkg_prefix=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var pkg_prefix pfSense-pkg-)
704
export platform=$(cat /etc/platform)
705

    
706
USE_MFS_TMPVAR=$(/usr/local/sbin/read_xml_tag.sh boolean system/use_mfs_tmpvar)
707
if [ "${platform}" = "nanobsd" ] || [ "${USE_MFS_TMPVAR}" = "true" ]; then
708
	export PKG_DBDIR=/root/var/db/pkg
709
	export PKG_CACHEDIR=/root/var/cache/pkg
710
fi
711

    
712
product_version=$(cat /etc/version)
713
do_not_send_host_uuid=$(/usr/local/sbin/read_xml_tag.sh boolean system/do_not_send_host_uuid)
714
if [ "${do_not_send_host_uuid}" != "true" ]; then
715
	hostuuid=$(sysctl kern.hostuuid)
716
	export HTTP_USER_AGENT="${product}/${product_version}:${hostuuid}"
717
else
718
	export HTTP_USER_AGENT="${product}/${product_version}"
719
fi
720

    
721
# Make sure to use default repo conf when it doesn't exist
722
pkg_repo_conf="/usr/local/etc/pkg/repos/${product}.conf"
723
default_pkg_repo_conf_path="/usr/local/share/${product}/pkg/repos/${product}-repo.conf"
724

    
725
pkg_repo_conf_path=$(/usr/local/sbin/read_xml_tag.sh string system/pkg_repo_conf_path \
726
	${default_pkg_repo_conf_path})
727

    
728
if [ -z "${pkg_repo_conf_path}" -o ! -f "${pkg_repo_conf_path}" ]; then
729
	pkg_repo_conf_path=${default_pkg_repo_conf_path}
730
fi
731

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

    
737
if [ "$(readlink ${pkg_repo_conf})" != "${pkg_repo_conf_path}" ]; then
738
	ln -sf ${pkg_repo_conf_path} ${pkg_repo_conf}
739
fi
740

    
741
# Flags used in _exit
742
export delete_annotation=""
743
export unlock_additional_pkgs=""
744

    
745
# Upgrade process on nanobsd will happen in chroot
746
export pkg_chroot=""
747
export chroot_dir=""
748

    
749
# Save nc_pid to be able to kill it
750
export nc_pid=""
751

    
752
# Reboot after 10 seconds
753
export reboot_after=10
754

    
755
unset dry_run
756
unset dont_reboot
757
unset booting
758
unset boot_stage
759
unset force
760
unset yes
761
unset progress_file
762
unset progress_socket
763
unset action
764
unset action_pkg
765
unset force_ipv4
766
unset force_ipv6
767
while getopts 46b:cdfi:hp:l:nr:Ruy opt; do
768
	case ${opt} in
769
		4)
770
			if [ -n "${force_ipv6}" ]; then
771
				usage
772
				_exit 1
773
			fi
774
			force_ipv4=1
775
			;;
776
		6)
777
			if [ -n "${force_ipv4}" ]; then
778
				usage
779
				_exit 1
780
			fi
781
			force_ipv6=1
782
			;;
783
		b)
784
			booting=1
785
			boot_stage="${OPTARG}"
786
			;;
787
		c)
788
			action="check"
789
			;;
790
		d)
791
			stdout=''
792
			;;
793
		f)
794
			force=1
795
			;;
796
		i)
797
			if [ -n "${action}" ]; then
798
				usage
799
				_exit 1
800
			fi
801
			action="install"
802
			action_pkg="${OPTARG}"
803
			;;
804
		h)
805
			usage
806
			_exit 0
807
			;;
808
		l)
809
			logfile="${OPTARG}"
810
			if [ -z "${logfile}" ]; then
811
				usage
812
				_exit 1
813
			fi
814
			;;
815
		n)
816
			dry_run="-n"
817
			;;
818
		p)
819
			progress_socket="${OPTARG}"
820
			if [ -z "${progress_socket}" ]; then
821
				usage
822
				_exit 1
823
			fi
824
			;;
825
		r)
826
			if [ -n "${action}" ]; then
827
				usage
828
				_exit 1
829
			fi
830
			action="delete"
831
			action_pkg="${OPTARG}"
832
			;;
833
		R)
834
			dont_reboot=1
835
			;;
836
		u)
837
			if [ -n "${action}" ]; then
838
				usage
839
				_exit 1
840
			fi
841
			action="update"
842
			;;
843
		y)
844
			yes=1
845
			;;
846
		*)
847
			usage
848
			_exit 1
849
			;;
850
	esac
851
done
852

    
853
if [ -n "${force_ipv4}" ]; then
854
	export IP_VERSION="4"
855
elif [ -n "${force_ipv6}" ]; then
856
	export IP_VERSION="6"
857
fi
858

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

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

    
867
if [ -z "${booting}" -o "${boot_stage}" != "2" ]; then
868
	/usr/local/bin/php /etc/rc.conf_mount_rw
869
fi
870

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

    
875
echo $$ > ${pid_file}
876

    
877
trap _exit 1 2 15 EXIT
878

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

    
883
progress_file=${logfile%.*}.json
884

    
885
if [ -e "${progress_file}" ]; then
886
	rm -f ${progress_file}
887
fi
888

    
889
case "${action}" in
890
	check)
891
		check_upgrade
892
		;;
893
	upgrade)
894
		pkg_upgrade
895
		;;
896
	update)
897
		pkg_update force
898
		;;
899
	install)
900
		if [ ${action_pkg} == "ALL_PACKAGES" ] && [ -n ${force} ]; then
901
			pkg_reinstall_all
902
		else
903
			pkg_install ${action_pkg} ${force}
904
		fi
905
		;;
906
	delete)
907
		if [ ${action_pkg} == "ALL_PACKAGES" ] && [ -n ${force} ]; then
908
			pkg_delete_all
909
		else
910
			pkg_delete ${action_pkg}
911
		fi
912
		;;
913
	*)
914
		_echo "ERROR: Invalid action!"
915
		_exit 1
916
esac
917

    
918
_exit 0
(9-9/22)