Project

General

Profile

Download (22.3 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
		if is_pkg_installed ${product}-repo-devel; then
296
			local _repo_pkg="${product}-repo-devel"
297
		else
298
			local _repo_pkg="${product}-repo"
299
		fi
300

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

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

    
318
		if [ $(pkg upgrade -nq | wc -l) -le 1 ]; then
319
			_echo "Your packages are up to date"
320
			_exit 0
321
		fi
322

    
323
		if [ -n "${dry_run}" ]; then
324
			pkg ${pkg_chroot} upgrade -nq 2>&1 | tee -a ${logfile}
325
			_exit 0
326
		fi
327

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

    
353
		pkg_unlock ${kernel_pkg}
354

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

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

    
372
		# Download all upgrade packages first
373
		fetch_upgrade_packages
374

    
375
		if [ $(pkg ${pkg_chroot} upgrade -nq ${kernel_pkg} | wc -l) -gt 1 ]; then
376
			_exec "pkg ${pkg_chroot} upgrade ${kernel_pkg}" "Upgrading ${product} kernel"
377
		fi
378

    
379
		pkg ${pkg_chroot} annotate -q -M ${kernel_pkg} next_stage 2
380
		next_stage=2
381

    
382
		if [ -n "${need_reboot}" -a "${platform}" != "nanobsd" ]; then
383
			do_reboot
384
			_exit 0
385
		fi
386
	fi
387

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

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

    
410
		if [ $(pkg ${pkg_chroot} upgrade -nq | wc -l) -gt 1 ]; then
411
			delete_annotation=1
412
			_exec "pkg ${pkg_chroot} upgrade" "Upgrading necessary packages"
413
			delete_annotation=""
414
		fi
415

    
416
		# XXX: workaround for #5300
417
		sort -u ${chroot_dir}/usr/local/etc/php/extensions.ini > /tmp/extensions.ini
418
		mv /tmp/extensions.ini ${chroot_dir}/usr/local/etc/php/extensions.ini
419

    
420
		pkg ${pkg_chroot} annotate -q -M ${kernel_pkg} next_stage 3
421
		next_stage=3
422

    
423
		pkg_unlock "${pkg_prefix}*"
424
		unlock_additional_pkgs=""
425

    
426
		if [ -n "${need_reboot}" -a "${platform}" = "nanobsd" ]; then
427
			switch_active_nanobsd_partition
428
			do_reboot
429
			_exit 0
430
		fi
431

    
432
		if [ -n "${booting}" ]; then
433
			_exit 0
434
		fi
435
	fi
436

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

    
444
		pkg ${pkg_chroot} annotate -q -D ${kernel_pkg} next_stage
445

    
446
		# cleanup caches
447
		_exec "pkg ${pkg_chroot} autoremove" "Removing unnecessary packages" mute ignore_result
448
		_exec "pkg ${pkg_chroot} clean" "Cleanup pkg cache" mute ignore_result
449
	fi
450

    
451
	gitsync=$(/usr/local/sbin/read_xml_tag.sh boolean system/gitsync/synconupgrade)
452
	if [ "${gitsync}" = "true" ]; then
453
		repository_url=$(/usr/local/sbin/read_xml_tag.sh string system/gitsync/repositoryurl)
454
		branch=$(/usr/local/sbin/read_xml_tag.sh string system/gitsync/branch)
455

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

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

    
476
	pkg_update
477

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

    
496
	_echo "Your system is up to date"
497
	_exit 0
498
}
499

    
500
setup_nanobsd_env() {
501
	if [ "${platform}" != "nanobsd" ]; then
502
		return;
503
	fi
504

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

    
510
	if [ ! -e "${_update_partition}" ]; then
511
		_echo "Secondary partition (${_update_partition}), used for upgrade not found"
512
		_exit 1
513
	fi
514

    
515
	# Remove /dev
516
	_update_partition=$(echo ${_update_partition} | sed 's,^/dev/,,')
517
	local _update_slice=$(glabel status -s | awk "\$1 == \"${_update_partition}\" { print \$3 }")
518

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

    
524
	_update_slice="/dev/${_update_slice}"
525

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

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

    
536
	sed -i '' -e "s,^${_cur_partition},/dev/${_update_partition}," \
537
		${chroot_dir}/etc/fstab
538

    
539
	pkg_chroot="-c ${chroot_dir}"
540
}
541

    
542
switch_active_nanobsd_partition() {
543
	if [ "${platform}" != "nanobsd" ]; then
544
		return;
545
	fi
546

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

    
552
	if ! echo "${_i}" | egrep -q '^[0-9]$'; then
553
		_echo "Invalid partition label ${_cur_partition}"
554
		_exit 1
555
	fi
556

    
557
	# pfsense0 == part 1 / pfsense1 == part 2
558
	if [ ${_i} -eq 0 ]; then
559
		_i=2
560
	else
561
		_i=1
562
	fi
563

    
564
	_exec "gpart set -a active -i ${_i} ${_disk}" "Setting secondary partition as active" mute
565
}
566

    
567
is_pkg_installed() {
568
	local _pkg_name="${1}"
569
	shift
570
	local _pkg_chroot="$@"
571

    
572
	pkg ${_pkg_chroot} info -e ${_pkg_name}
573
	return $?
574
}
575

    
576
compare_pkg_version() {
577
	local _pkg_name="${1}"
578

    
579
	if ! is_pkg_installed ${_pkg_name} ${pkg_chroot}; then
580
		echo '!'
581
		return -1
582
	fi
583

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

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

    
591
	local _rver=$(pkg ${pkg_chroot} rquery %v ${_pkg_name})
592

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

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

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

    
605
	echo ${_version}
606
	return 0
607
}
608

    
609
pkg_install() {
610
	local _pkg_name="${1}"
611

    
612
	local _force=""
613
	if [ -n "${2}" ]; then
614
		_force="-f"
615
	fi
616

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

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

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

    
641
	_exec "pkg_with_pb ${_cmd}${dry_run:+ }${dry_run} ${_pkg_name}" "${_msg} ${_pkg_name}"
642
	_exec "pkg clean" "Cleaning up cache" mute ignore_result
643
}
644

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

    
656
pkg_delete() {
657
	local _pkg_name="${1}"
658

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

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

    
669
	_exec "pkg_with_pb delete${dry_run:+ }${dry_run} ${_pkg_name}" "Removing ${_pkg_name}"
670
	_exec "pkg autoremove" "Removing stale packages" mute ignore_result
671
}
672

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

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

    
697
export LANG=C
698

    
699
pid_file="/var/run/$(basename $0).pid"
700
logfile="/cf/conf/upgrade_log.txt"
701
stdout='/dev/null'
702

    
703
# pkg should not ask for confirmations
704
export ASSUME_ALWAYS_YES=true
705

    
706
export product=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_name pfSense)
707
export pkg_prefix=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var pkg_prefix pfSense-pkg-)
708
export platform=$(cat /etc/platform)
709

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

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

    
725
# Flags used in _exit
726
export delete_annotation=""
727
export unlock_additional_pkgs=""
728

    
729
# Upgrade process on nanobsd will happen in chroot
730
export pkg_chroot=""
731
export chroot_dir=""
732

    
733
# Save nc_pid to be able to kill it
734
export nc_pid=""
735

    
736
# Reboot after 10 seconds
737
export reboot_after=10
738

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

    
837
if [ -n "${force_ipv4}" ]; then
838
	export IP_VERSION="4"
839
elif [ -n "${force_ipv6}" ]; then
840
	export IP_VERSION="6"
841
fi
842

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

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

    
851
if [ -z "${booting}" -o "${boot_stage}" != "2" ]; then
852
	/usr/local/bin/php /etc/rc.conf_mount_rw
853
fi
854

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

    
859
echo $$ > ${pid_file}
860

    
861
trap _exit 1 2 15 EXIT
862

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

    
867
progress_file=${logfile%.*}.json
868

    
869
if [ -e "${progress_file}" ]; then
870
	rm -f ${progress_file}
871
fi
872

    
873
case "${action}" in
874
	check)
875
		check_upgrade
876
		;;
877
	upgrade)
878
		pkg_upgrade
879
		;;
880
	update)
881
		pkg_update force
882
		;;
883
	install)
884
		if [ ${action_pkg} == "ALL_PACKAGES" ] && [ -n ${force} ]; then
885
			pkg_reinstall_all
886
		else
887
			pkg_install ${action_pkg} ${force}
888
		fi
889
		;;
890
	delete)
891
		if [ ${action_pkg} == "ALL_PACKAGES" ] && [ -n ${force} ]; then
892
			pkg_delete_all
893
		else
894
			pkg_delete ${action_pkg}
895
		fi
896
		;;
897
	*)
898
		_echo "ERROR: Invalid action!"
899
		_exit 1
900
esac
901

    
902
_exit 0
(10-10/23)