freebsd-update.sh revision 217767
1#!/bin/sh
2
3#-
4# Copyright 2004-2007 Colin Percival
5# All rights reserved
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted providing that the following conditions 
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27
28# $FreeBSD: head/usr.sbin/freebsd-update/freebsd-update.sh 217767 2011-01-24 04:32:59Z gordon $
29
30#### Usage function -- called from command-line handling code.
31
32# Usage instructions.  Options not listed:
33# --debug	-- don't filter output from utilities
34# --no-stats	-- don't show progress statistics while fetching files
35usage () {
36	cat <<EOF
37usage: `basename $0` [options] command ... [path]
38
39Options:
40  -b basedir   -- Operate on a system mounted at basedir
41                  (default: /)
42  -d workdir   -- Store working files in workdir
43                  (default: /var/db/freebsd-update/)
44  -f conffile  -- Read configuration options from conffile
45                  (default: /etc/freebsd-update.conf)
46  -k KEY       -- Trust an RSA key with SHA256 hash of KEY
47  -r release   -- Target for upgrade (e.g., 6.2-RELEASE)
48  -s server    -- Server from which to fetch updates
49                  (default: update.FreeBSD.org)
50  -t address   -- Mail output of cron command, if any, to address
51                  (default: root)
52Commands:
53  fetch        -- Fetch updates from server
54  cron         -- Sleep rand(3600) seconds, fetch updates, and send an
55                  email if updates were found
56  upgrade      -- Fetch upgrades to FreeBSD version specified via -r option
57  install      -- Install downloaded updates or upgrades
58  rollback     -- Uninstall most recently installed updates
59  IDS          -- Compare the system against an index of "known good" files.
60EOF
61	exit 0
62}
63
64#### Configuration processing functions
65
66#-
67# Configuration options are set in the following order of priority:
68# 1. Command line options
69# 2. Configuration file options
70# 3. Default options
71# In addition, certain options (e.g., IgnorePaths) can be specified multiple
72# times and (as long as these are all in the same place, e.g., inside the
73# configuration file) they will accumulate.  Finally, because the path to the
74# configuration file can be specified at the command line, the entire command
75# line must be processed before we start reading the configuration file.
76#
77# Sound like a mess?  It is.  Here's how we handle this:
78# 1. Initialize CONFFILE and all the options to "".
79# 2. Process the command line.  Throw an error if a non-accumulating option
80#    is specified twice.
81# 3. If CONFFILE is "", set CONFFILE to /etc/freebsd-update.conf .
82# 4. For all the configuration options X, set X_saved to X.
83# 5. Initialize all the options to "".
84# 6. Read CONFFILE line by line, parsing options.
85# 7. For each configuration option X, set X to X_saved iff X_saved is not "".
86# 8. Repeat steps 4-7, except setting options to their default values at (6).
87
88CONFIGOPTIONS="KEYPRINT WORKDIR SERVERNAME MAILTO ALLOWADD ALLOWDELETE
89    KEEPMODIFIEDMETADATA COMPONENTS IGNOREPATHS UPDATEIFUNMODIFIED
90    BASEDIR VERBOSELEVEL TARGETRELEASE STRICTCOMPONENTS MERGECHANGES
91    IDSIGNOREPATHS BACKUPKERNEL BACKUPKERNELDIR BACKUPKERNELSYMBOLFILES"
92
93# Set all the configuration options to "".
94nullconfig () {
95	for X in ${CONFIGOPTIONS}; do
96		eval ${X}=""
97	done
98}
99
100# For each configuration option X, set X_saved to X.
101saveconfig () {
102	for X in ${CONFIGOPTIONS}; do
103		eval ${X}_saved=\$${X}
104	done
105}
106
107# For each configuration option X, set X to X_saved if X_saved is not "".
108mergeconfig () {
109	for X in ${CONFIGOPTIONS}; do
110		eval _=\$${X}_saved
111		if ! [ -z "${_}" ]; then
112			eval ${X}=\$${X}_saved
113		fi
114	done
115}
116
117# Set the trusted keyprint.
118config_KeyPrint () {
119	if [ -z ${KEYPRINT} ]; then
120		KEYPRINT=$1
121	else
122		return 1
123	fi
124}
125
126# Set the working directory.
127config_WorkDir () {
128	if [ -z ${WORKDIR} ]; then
129		WORKDIR=$1
130	else
131		return 1
132	fi
133}
134
135# Set the name of the server (pool) from which to fetch updates
136config_ServerName () {
137	if [ -z ${SERVERNAME} ]; then
138		SERVERNAME=$1
139	else
140		return 1
141	fi
142}
143
144# Set the address to which 'cron' output will be mailed.
145config_MailTo () {
146	if [ -z ${MAILTO} ]; then
147		MAILTO=$1
148	else
149		return 1
150	fi
151}
152
153# Set whether FreeBSD Update is allowed to add files (or directories, or
154# symlinks) which did not previously exist.
155config_AllowAdd () {
156	if [ -z ${ALLOWADD} ]; then
157		case $1 in
158		[Yy][Ee][Ss])
159			ALLOWADD=yes
160			;;
161		[Nn][Oo])
162			ALLOWADD=no
163			;;
164		*)
165			return 1
166			;;
167		esac
168	else
169		return 1
170	fi
171}
172
173# Set whether FreeBSD Update is allowed to remove files/directories/symlinks.
174config_AllowDelete () {
175	if [ -z ${ALLOWDELETE} ]; then
176		case $1 in
177		[Yy][Ee][Ss])
178			ALLOWDELETE=yes
179			;;
180		[Nn][Oo])
181			ALLOWDELETE=no
182			;;
183		*)
184			return 1
185			;;
186		esac
187	else
188		return 1
189	fi
190}
191
192# Set whether FreeBSD Update should keep existing inode ownership,
193# permissions, and flags, in the event that they have been modified locally
194# after the release.
195config_KeepModifiedMetadata () {
196	if [ -z ${KEEPMODIFIEDMETADATA} ]; then
197		case $1 in
198		[Yy][Ee][Ss])
199			KEEPMODIFIEDMETADATA=yes
200			;;
201		[Nn][Oo])
202			KEEPMODIFIEDMETADATA=no
203			;;
204		*)
205			return 1
206			;;
207		esac
208	else
209		return 1
210	fi
211}
212
213# Add to the list of components which should be kept updated.
214config_Components () {
215	for C in $@; do
216		COMPONENTS="${COMPONENTS} ${C}"
217	done
218}
219
220# Add to the list of paths under which updates will be ignored.
221config_IgnorePaths () {
222	for C in $@; do
223		IGNOREPATHS="${IGNOREPATHS} ${C}"
224	done
225}
226
227# Add to the list of paths which IDS should ignore.
228config_IDSIgnorePaths () {
229	for C in $@; do
230		IDSIGNOREPATHS="${IDSIGNOREPATHS} ${C}"
231	done
232}
233
234# Add to the list of paths within which updates will be performed only if the
235# file on disk has not been modified locally.
236config_UpdateIfUnmodified () {
237	for C in $@; do
238		UPDATEIFUNMODIFIED="${UPDATEIFUNMODIFIED} ${C}"
239	done
240}
241
242# Add to the list of paths within which updates to text files will be merged
243# instead of overwritten.
244config_MergeChanges () {
245	for C in $@; do
246		MERGECHANGES="${MERGECHANGES} ${C}"
247	done
248}
249
250# Work on a FreeBSD installation mounted under $1
251config_BaseDir () {
252	if [ -z ${BASEDIR} ]; then
253		BASEDIR=$1
254	else
255		return 1
256	fi
257}
258
259# When fetching upgrades, should we assume the user wants exactly the
260# components listed in COMPONENTS, rather than trying to guess based on
261# what's currently installed?
262config_StrictComponents () {
263	if [ -z ${STRICTCOMPONENTS} ]; then
264		case $1 in
265		[Yy][Ee][Ss])
266			STRICTCOMPONENTS=yes
267			;;
268		[Nn][Oo])
269			STRICTCOMPONENTS=no
270			;;
271		*)
272			return 1
273			;;
274		esac
275	else
276		return 1
277	fi
278}
279
280# Upgrade to FreeBSD $1
281config_TargetRelease () {
282	if [ -z ${TARGETRELEASE} ]; then
283		TARGETRELEASE=$1
284	else
285		return 1
286	fi
287	if echo ${TARGETRELEASE} | grep -qE '^[0-9.]+$'; then
288		TARGETRELEASE="${TARGETRELEASE}-RELEASE"
289	fi
290}
291
292# Define what happens to output of utilities
293config_VerboseLevel () {
294	if [ -z ${VERBOSELEVEL} ]; then
295		case $1 in
296		[Dd][Ee][Bb][Uu][Gg])
297			VERBOSELEVEL=debug
298			;;
299		[Nn][Oo][Ss][Tt][Aa][Tt][Ss])
300			VERBOSELEVEL=nostats
301			;;
302		[Ss][Tt][Aa][Tt][Ss])
303			VERBOSELEVEL=stats
304			;;
305		*)
306			return 1
307			;;
308		esac
309	else
310		return 1
311	fi
312}
313
314config_BackupKernel () {
315	if [ -z ${BACKUPKERNEL} ]; then
316		case $1 in
317		[Yy][Ee][Ss])
318			BACKUPKERNEL=yes
319			;;
320		[Nn][Oo])
321			BACKUPKERNEL=no
322			;;
323		*)
324			return 1
325			;;
326		esac
327	else
328		return 1
329	fi
330}
331
332config_BackupKernelDir () {
333	if [ -z ${BACKUPKERNELDIR} ]; then
334		if [ -z "$1" ]; then
335			echo "BackupKernelDir set to empty dir"
336			return 1
337		fi
338
339		# We check for some paths which would be extremely odd
340		# to use, but which could cause a lot of problems if
341		# used.
342		case $1 in
343		/|/bin|/boot|/etc|/lib|/libexec|/sbin|/usr|/var)
344			echo "BackupKernelDir set to invalid path $1"
345			return 1
346			;;
347		/*)
348			BACKUPKERNELDIR=$1
349			;;
350		*)
351			echo "BackupKernelDir ($1) is not an absolute path"
352			return 1
353			;;
354		esac
355	else
356		return 1
357	fi
358}
359
360config_BackupKernelSymbolFiles () {
361	if [ -z ${BACKUPKERNELSYMBOLFILES} ]; then
362		case $1 in
363		[Yy][Ee][Ss])
364			BACKUPKERNELSYMBOLFILES=yes
365			;;
366		[Nn][Oo])
367			BACKUPKERNELSYMBOLFILES=no
368			;;
369		*)
370			return 1
371			;;
372		esac
373	else
374		return 1
375	fi
376}
377
378# Handle one line of configuration
379configline () {
380	if [ $# -eq 0 ]; then
381		return
382	fi
383
384	OPT=$1
385	shift
386	config_${OPT} $@
387}
388
389#### Parameter handling functions.
390
391# Initialize parameters to null, just in case they're
392# set in the environment.
393init_params () {
394	# Configration settings
395	nullconfig
396
397	# No configuration file set yet
398	CONFFILE=""
399
400	# No commands specified yet
401	COMMANDS=""
402}
403
404# Parse the command line
405parse_cmdline () {
406	while [ $# -gt 0 ]; do
407		case "$1" in
408		# Location of configuration file
409		-f)
410			if [ $# -eq 1 ]; then usage; fi
411			if [ ! -z "${CONFFILE}" ]; then usage; fi
412			shift; CONFFILE="$1"
413			;;
414
415		# Configuration file equivalents
416		-b)
417			if [ $# -eq 1 ]; then usage; fi; shift
418			config_BaseDir $1 || usage
419			;;
420		-d)
421			if [ $# -eq 1 ]; then usage; fi; shift
422			config_WorkDir $1 || usage
423			;;
424		-k)
425			if [ $# -eq 1 ]; then usage; fi; shift
426			config_KeyPrint $1 || usage
427			;;
428		-s)
429			if [ $# -eq 1 ]; then usage; fi; shift
430			config_ServerName $1 || usage
431			;;
432		-r)
433			if [ $# -eq 1 ]; then usage; fi; shift
434			config_TargetRelease $1 || usage
435			;;
436		-t)
437			if [ $# -eq 1 ]; then usage; fi; shift
438			config_MailTo $1 || usage
439			;;
440		-v)
441			if [ $# -eq 1 ]; then usage; fi; shift
442			config_VerboseLevel $1 || usage
443			;;
444
445		# Aliases for "-v debug" and "-v nostats"
446		--debug)
447			config_VerboseLevel debug || usage
448			;;
449		--no-stats)
450			config_VerboseLevel nostats || usage
451			;;
452
453		# Commands
454		cron | fetch | upgrade | install | rollback | IDS)
455			COMMANDS="${COMMANDS} $1"
456			;;
457
458		# Anything else is an error
459		*)
460			usage
461			;;
462		esac
463		shift
464	done
465
466	# Make sure we have at least one command
467	if [ -z "${COMMANDS}" ]; then
468		usage
469	fi
470}
471
472# Parse the configuration file
473parse_conffile () {
474	# If a configuration file was specified on the command line, check
475	# that it exists and is readable.
476	if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then
477		echo -n "File does not exist "
478		echo -n "or is not readable: "
479		echo ${CONFFILE}
480		exit 1
481	fi
482
483	# If a configuration file was not specified on the command line,
484	# use the default configuration file path.  If that default does
485	# not exist, give up looking for any configuration.
486	if [ -z "${CONFFILE}" ]; then
487		CONFFILE="/etc/freebsd-update.conf"
488		if [ ! -r "${CONFFILE}" ]; then
489			return
490		fi
491	fi
492
493	# Save the configuration options specified on the command line, and
494	# clear all the options in preparation for reading the config file.
495	saveconfig
496	nullconfig
497
498	# Read the configuration file.  Anything after the first '#' is
499	# ignored, and any blank lines are ignored.
500	L=0
501	while read LINE; do
502		L=$(($L + 1))
503		LINEX=`echo "${LINE}" | cut -f 1 -d '#'`
504		if ! configline ${LINEX}; then
505			echo "Error processing configuration file, line $L:"
506			echo "==> ${LINE}"
507			exit 1
508		fi
509	done < ${CONFFILE}
510
511	# Merge the settings read from the configuration file with those
512	# provided at the command line.
513	mergeconfig
514}
515
516# Provide some default parameters
517default_params () {
518	# Save any parameters already configured, and clear the slate
519	saveconfig
520	nullconfig
521
522	# Default configurations
523	config_WorkDir /var/db/freebsd-update
524	config_MailTo root
525	config_AllowAdd yes
526	config_AllowDelete yes
527	config_KeepModifiedMetadata yes
528	config_BaseDir /
529	config_VerboseLevel stats
530	config_StrictComponents no
531	config_BackupKernel yes
532	config_BackupKernelDir /boot/kernel.old
533	config_BackupKernelSymbolFiles no
534
535	# Merge these defaults into the earlier-configured settings
536	mergeconfig
537}
538
539# Set utility output filtering options, based on ${VERBOSELEVEL}
540fetch_setup_verboselevel () {
541	case ${VERBOSELEVEL} in
542	debug)
543		QUIETREDIR="/dev/stderr"
544		QUIETFLAG=" "
545		STATSREDIR="/dev/stderr"
546		DDSTATS=".."
547		XARGST="-t"
548		NDEBUG=" "
549		;;
550	nostats)
551		QUIETREDIR=""
552		QUIETFLAG=""
553		STATSREDIR="/dev/null"
554		DDSTATS=".."
555		XARGST=""
556		NDEBUG=""
557		;;
558	stats)
559		QUIETREDIR="/dev/null"
560		QUIETFLAG="-q"
561		STATSREDIR="/dev/stdout"
562		DDSTATS=""
563		XARGST=""
564		NDEBUG="-n"
565		;;
566	esac
567}
568
569# Perform sanity checks and set some final parameters
570# in preparation for fetching files.  Figure out which
571# set of updates should be downloaded: If the user is
572# running *-p[0-9]+, strip off the last part; if the
573# user is running -SECURITY, call it -RELEASE.  Chdir
574# into the working directory.
575fetchupgrade_check_params () {
576	export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
577
578	_SERVERNAME_z=\
579"SERVERNAME must be given via command line or configuration file."
580	_KEYPRINT_z="Key must be given via -k option or configuration file."
581	_KEYPRINT_bad="Invalid key fingerprint: "
582	_WORKDIR_bad="Directory does not exist or is not writable: "
583
584	if [ -z "${SERVERNAME}" ]; then
585		echo -n "`basename $0`: "
586		echo "${_SERVERNAME_z}"
587		exit 1
588	fi
589	if [ -z "${KEYPRINT}" ]; then
590		echo -n "`basename $0`: "
591		echo "${_KEYPRINT_z}"
592		exit 1
593	fi
594	if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
595		echo -n "`basename $0`: "
596		echo -n "${_KEYPRINT_bad}"
597		echo ${KEYPRINT}
598		exit 1
599	fi
600	if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
601		echo -n "`basename $0`: "
602		echo -n "${_WORKDIR_bad}"
603		echo ${WORKDIR}
604		exit 1
605	fi
606	chmod 700 ${WORKDIR}
607	cd ${WORKDIR} || exit 1
608
609	# Generate release number.  The s/SECURITY/RELEASE/ bit exists
610	# to provide an upgrade path for FreeBSD Update 1.x users, since
611	# the kernels provided by FreeBSD Update 1.x are always labelled
612	# as X.Y-SECURITY.
613	RELNUM=`uname -r |
614	    sed -E 's,-p[0-9]+,,' |
615	    sed -E 's,-SECURITY,-RELEASE,'`
616	ARCH=`uname -m`
617	FETCHDIR=${RELNUM}/${ARCH}
618	PATCHDIR=${RELNUM}/${ARCH}/bp
619
620	# Figure out what directory contains the running kernel
621	BOOTFILE=`sysctl -n kern.bootfile`
622	KERNELDIR=${BOOTFILE%/kernel}
623	if ! [ -d ${KERNELDIR} ]; then
624		echo "Cannot identify running kernel"
625		exit 1
626	fi
627
628	# Figure out what kernel configuration is running.  We start with
629	# the output of `uname -i`, and then make the following adjustments:
630	# 1. Replace "SMP-GENERIC" with "SMP".  Why the SMP kernel config
631	# file says "ident SMP-GENERIC", I don't know...
632	# 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
633	# _and_ `sysctl kern.version` contains a line which ends "/SMP", then
634	# we're running an SMP kernel.  This mis-identification is a bug
635	# which was fixed in 6.2-STABLE.
636	KERNCONF=`uname -i`
637	if [ ${KERNCONF} = "SMP-GENERIC" ]; then
638		KERNCONF=SMP
639	fi
640	if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
641		if sysctl kern.version | grep -qE '/SMP$'; then
642			KERNCONF=SMP
643		fi
644	fi
645
646	# Define some paths
647	BSPATCH=/usr/bin/bspatch
648	SHA256=/sbin/sha256
649	PHTTPGET=/usr/libexec/phttpget
650
651	# Set up variables relating to VERBOSELEVEL
652	fetch_setup_verboselevel
653
654	# Construct a unique name from ${BASEDIR}
655	BDHASH=`echo ${BASEDIR} | sha256 -q`
656}
657
658# Perform sanity checks etc. before fetching updates.
659fetch_check_params () {
660	fetchupgrade_check_params
661
662	if ! [ -z "${TARGETRELEASE}" ]; then
663		echo -n "`basename $0`: "
664		echo -n "-r option is meaningless with 'fetch' command.  "
665		echo "(Did you mean 'upgrade' instead?)"
666		exit 1
667	fi
668}
669
670# Perform sanity checks etc. before fetching upgrades.
671upgrade_check_params () {
672	fetchupgrade_check_params
673
674	# Unless set otherwise, we're upgrading to the same kernel config.
675	NKERNCONF=${KERNCONF}
676
677	# We need TARGETRELEASE set
678	_TARGETRELEASE_z="Release target must be specified via -r option."
679	if [ -z "${TARGETRELEASE}" ]; then
680		echo -n "`basename $0`: "
681		echo "${_TARGETRELEASE_z}"
682		exit 1
683	fi
684
685	# The target release should be != the current release.
686	if [ "${TARGETRELEASE}" = "${RELNUM}" ]; then
687		echo -n "`basename $0`: "
688		echo "Cannot upgrade from ${RELNUM} to itself"
689		exit 1
690	fi
691
692	# Turning off AllowAdd or AllowDelete is a bad idea for upgrades.
693	if [ "${ALLOWADD}" = "no" ]; then
694		echo -n "`basename $0`: "
695		echo -n "WARNING: \"AllowAdd no\" is a bad idea "
696		echo "when upgrading between releases."
697		echo
698	fi
699	if [ "${ALLOWDELETE}" = "no" ]; then
700		echo -n "`basename $0`: "
701		echo -n "WARNING: \"AllowDelete no\" is a bad idea "
702		echo "when upgrading between releases."
703		echo
704	fi
705
706	# Set EDITOR to /usr/bin/vi if it isn't already set
707	: ${EDITOR:='/usr/bin/vi'}
708}
709
710# Perform sanity checks and set some final parameters in
711# preparation for installing updates.
712install_check_params () {
713	# Check that we are root.  All sorts of things won't work otherwise.
714	if [ `id -u` != 0 ]; then
715		echo "You must be root to run this."
716		exit 1
717	fi
718
719	# Check that securelevel <= 0.  Otherwise we can't update schg files.
720	if [ `sysctl -n kern.securelevel` -gt 0 ]; then
721		echo "Updates cannot be installed when the system securelevel"
722		echo "is greater than zero."
723		exit 1
724	fi
725
726	# Check that we have a working directory
727	_WORKDIR_bad="Directory does not exist or is not writable: "
728	if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
729		echo -n "`basename $0`: "
730		echo -n "${_WORKDIR_bad}"
731		echo ${WORKDIR}
732		exit 1
733	fi
734	cd ${WORKDIR} || exit 1
735
736	# Construct a unique name from ${BASEDIR}
737	BDHASH=`echo ${BASEDIR} | sha256 -q`
738
739	# Check that we have updates ready to install
740	if ! [ -L ${BDHASH}-install ]; then
741		echo "No updates are available to install."
742		echo "Run '$0 fetch' first."
743		exit 1
744	fi
745	if ! [ -f ${BDHASH}-install/INDEX-OLD ] ||
746	    ! [ -f ${BDHASH}-install/INDEX-NEW ]; then
747		echo "Update manifest is corrupt -- this should never happen."
748		echo "Re-run '$0 fetch'."
749		exit 1
750	fi
751
752	# Figure out what directory contains the running kernel
753	BOOTFILE=`sysctl -n kern.bootfile`
754	KERNELDIR=${BOOTFILE%/kernel}
755	if ! [ -d ${KERNELDIR} ]; then
756		echo "Cannot identify running kernel"
757		exit 1
758	fi
759}
760
761# Perform sanity checks and set some final parameters in
762# preparation for UNinstalling updates.
763rollback_check_params () {
764	# Check that we are root.  All sorts of things won't work otherwise.
765	if [ `id -u` != 0 ]; then
766		echo "You must be root to run this."
767		exit 1
768	fi
769
770	# Check that we have a working directory
771	_WORKDIR_bad="Directory does not exist or is not writable: "
772	if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
773		echo -n "`basename $0`: "
774		echo -n "${_WORKDIR_bad}"
775		echo ${WORKDIR}
776		exit 1
777	fi
778	cd ${WORKDIR} || exit 1
779
780	# Construct a unique name from ${BASEDIR}
781	BDHASH=`echo ${BASEDIR} | sha256 -q`
782
783	# Check that we have updates ready to rollback
784	if ! [ -L ${BDHASH}-rollback ]; then
785		echo "No rollback directory found."
786		exit 1
787	fi
788	if ! [ -f ${BDHASH}-rollback/INDEX-OLD ] ||
789	    ! [ -f ${BDHASH}-rollback/INDEX-NEW ]; then
790		echo "Update manifest is corrupt -- this should never happen."
791		exit 1
792	fi
793}
794
795# Perform sanity checks and set some final parameters
796# in preparation for comparing the system against the
797# published index.  Figure out which index we should
798# compare against: If the user is running *-p[0-9]+,
799# strip off the last part; if the user is running
800# -SECURITY, call it -RELEASE.  Chdir into the working
801# directory.
802IDS_check_params () {
803	export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
804
805	_SERVERNAME_z=\
806"SERVERNAME must be given via command line or configuration file."
807	_KEYPRINT_z="Key must be given via -k option or configuration file."
808	_KEYPRINT_bad="Invalid key fingerprint: "
809	_WORKDIR_bad="Directory does not exist or is not writable: "
810
811	if [ -z "${SERVERNAME}" ]; then
812		echo -n "`basename $0`: "
813		echo "${_SERVERNAME_z}"
814		exit 1
815	fi
816	if [ -z "${KEYPRINT}" ]; then
817		echo -n "`basename $0`: "
818		echo "${_KEYPRINT_z}"
819		exit 1
820	fi
821	if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
822		echo -n "`basename $0`: "
823		echo -n "${_KEYPRINT_bad}"
824		echo ${KEYPRINT}
825		exit 1
826	fi
827	if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
828		echo -n "`basename $0`: "
829		echo -n "${_WORKDIR_bad}"
830		echo ${WORKDIR}
831		exit 1
832	fi
833	cd ${WORKDIR} || exit 1
834
835	# Generate release number.  The s/SECURITY/RELEASE/ bit exists
836	# to provide an upgrade path for FreeBSD Update 1.x users, since
837	# the kernels provided by FreeBSD Update 1.x are always labelled
838	# as X.Y-SECURITY.
839	RELNUM=`uname -r |
840	    sed -E 's,-p[0-9]+,,' |
841	    sed -E 's,-SECURITY,-RELEASE,'`
842	ARCH=`uname -m`
843	FETCHDIR=${RELNUM}/${ARCH}
844	PATCHDIR=${RELNUM}/${ARCH}/bp
845
846	# Figure out what directory contains the running kernel
847	BOOTFILE=`sysctl -n kern.bootfile`
848	KERNELDIR=${BOOTFILE%/kernel}
849	if ! [ -d ${KERNELDIR} ]; then
850		echo "Cannot identify running kernel"
851		exit 1
852	fi
853
854	# Figure out what kernel configuration is running.  We start with
855	# the output of `uname -i`, and then make the following adjustments:
856	# 1. Replace "SMP-GENERIC" with "SMP".  Why the SMP kernel config
857	# file says "ident SMP-GENERIC", I don't know...
858	# 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
859	# _and_ `sysctl kern.version` contains a line which ends "/SMP", then
860	# we're running an SMP kernel.  This mis-identification is a bug
861	# which was fixed in 6.2-STABLE.
862	KERNCONF=`uname -i`
863	if [ ${KERNCONF} = "SMP-GENERIC" ]; then
864		KERNCONF=SMP
865	fi
866	if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
867		if sysctl kern.version | grep -qE '/SMP$'; then
868			KERNCONF=SMP
869		fi
870	fi
871
872	# Define some paths
873	SHA256=/sbin/sha256
874	PHTTPGET=/usr/libexec/phttpget
875
876	# Set up variables relating to VERBOSELEVEL
877	fetch_setup_verboselevel
878}
879
880#### Core functionality -- the actual work gets done here
881
882# Use an SRV query to pick a server.  If the SRV query doesn't provide
883# a useful answer, use the server name specified by the user.
884# Put another way... look up _http._tcp.${SERVERNAME} and pick a server
885# from that; or if no servers are returned, use ${SERVERNAME}.
886# This allows a user to specify "portsnap.freebsd.org" (in which case
887# portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org"
888# (in which case portsnap will use that particular server, since there
889# won't be an SRV entry for that name).
890#
891# We ignore the Port field, since we are always going to use port 80.
892
893# Fetch the mirror list, but do not pick a mirror yet.  Returns 1 if
894# no mirrors are available for any reason.
895fetch_pick_server_init () {
896	: > serverlist_tried
897
898# Check that host(1) exists (i.e., that the system wasn't built with the
899# WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist.
900	if ! which -s host; then
901		: > serverlist_full
902		return 1
903	fi
904
905	echo -n "Looking up ${SERVERNAME} mirrors... "
906
907# Issue the SRV query and pull out the Priority, Weight, and Target fields.
908# BIND 9 prints "$name has SRV record ..." while BIND 8 prints
909# "$name server selection ..."; we allow either format.
910	MLIST="_http._tcp.${SERVERNAME}"
911	host -t srv "${MLIST}" |
912	    sed -nE "s/${MLIST} (has SRV record|server selection) //p" |
913	    cut -f 1,2,4 -d ' ' |
914	    sed -e 's/\.$//' |
915	    sort > serverlist_full
916
917# If no records, give up -- we'll just use the server name we were given.
918	if [ `wc -l < serverlist_full` -eq 0 ]; then
919		echo "none found."
920		return 1
921	fi
922
923# Report how many mirrors we found.
924	echo `wc -l < serverlist_full` "mirrors found."
925
926# Generate a random seed for use in picking mirrors.  If HTTP_PROXY
927# is set, this will be used to generate the seed; otherwise, the seed
928# will be random.
929	if [ -n "${HTTP_PROXY}${http_proxy}" ]; then
930		RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" |
931		    tr -d 'a-f' |
932		    cut -c 1-9`
933	else
934		RANDVALUE=`jot -r 1 0 999999999`
935	fi
936}
937
938# Pick a mirror.  Returns 1 if we have run out of mirrors to try.
939fetch_pick_server () {
940# Generate a list of not-yet-tried mirrors
941	sort serverlist_tried |
942	    comm -23 serverlist_full - > serverlist
943
944# Have we run out of mirrors?
945	if [ `wc -l < serverlist` -eq 0 ]; then
946		echo "No mirrors remaining, giving up."
947		return 1
948	fi
949
950# Find the highest priority level (lowest numeric value).
951	SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1`
952
953# Add up the weights of the response lines at that priority level.
954	SRV_WSUM=0;
955	while read X; do
956		case "$X" in
957		${SRV_PRIORITY}\ *)
958			SRV_W=`echo $X | cut -f 2 -d ' '`
959			SRV_WSUM=$(($SRV_WSUM + $SRV_W))
960			;;
961		esac
962	done < serverlist
963
964# If all the weights are 0, pretend that they are all 1 instead.
965	if [ ${SRV_WSUM} -eq 0 ]; then
966		SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l`
967		SRV_W_ADD=1
968	else
969		SRV_W_ADD=0
970	fi
971
972# Pick a value between 0 and the sum of the weights - 1
973	SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}`
974
975# Read through the list of mirrors and set SERVERNAME.  Write the line
976# corresponding to the mirror we selected into serverlist_tried so that
977# we won't try it again.
978	while read X; do
979		case "$X" in
980		${SRV_PRIORITY}\ *)
981			SRV_W=`echo $X | cut -f 2 -d ' '`
982			SRV_W=$(($SRV_W + $SRV_W_ADD))
983			if [ $SRV_RND -lt $SRV_W ]; then
984				SERVERNAME=`echo $X | cut -f 3 -d ' '`
985				echo "$X" >> serverlist_tried
986				break
987			else
988				SRV_RND=$(($SRV_RND - $SRV_W))
989			fi
990			;;
991		esac
992	done < serverlist
993}
994
995# Take a list of ${oldhash}|${newhash} and output a list of needed patches,
996# i.e., those for which we have ${oldhash} and don't have ${newhash}.
997fetch_make_patchlist () {
998	grep -vE "^([0-9a-f]{64})\|\1$" |
999	    tr '|' ' ' |
1000		while read X Y; do
1001			if [ -f "files/${Y}.gz" ] ||
1002			    [ ! -f "files/${X}.gz" ]; then
1003				continue
1004			fi
1005			echo "${X}|${Y}"
1006		done | uniq
1007}
1008
1009# Print user-friendly progress statistics
1010fetch_progress () {
1011	LNC=0
1012	while read x; do
1013		LNC=$(($LNC + 1))
1014		if [ $(($LNC % 10)) = 0 ]; then
1015			echo -n $LNC
1016		elif [ $(($LNC % 2)) = 0 ]; then
1017			echo -n .
1018		fi
1019	done
1020	echo -n " "
1021}
1022
1023# Function for asking the user if everything is ok
1024continuep () {
1025	while read -p "Does this look reasonable (y/n)? " CONTINUE; do
1026		case "${CONTINUE}" in
1027		y*)
1028			return 0
1029			;;
1030		n*)
1031			return 1
1032			;;
1033		esac
1034	done
1035}
1036
1037# Initialize the working directory
1038workdir_init () {
1039	mkdir -p files
1040	touch tINDEX.present
1041}
1042
1043# Check that we have a public key with an appropriate hash, or
1044# fetch the key if it doesn't exist.  Returns 1 if the key has
1045# not yet been fetched.
1046fetch_key () {
1047	if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
1048		return 0
1049	fi
1050
1051	echo -n "Fetching public key from ${SERVERNAME}... "
1052	rm -f pub.ssl
1053	fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/pub.ssl \
1054	    2>${QUIETREDIR} || true
1055	if ! [ -r pub.ssl ]; then
1056		echo "failed."
1057		return 1
1058	fi
1059	if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
1060		echo "key has incorrect hash."
1061		rm -f pub.ssl
1062		return 1
1063	fi
1064	echo "done."
1065}
1066
1067# Fetch metadata signature, aka "tag".
1068fetch_tag () {
1069	echo -n "Fetching metadata signature "
1070	echo ${NDEBUG} "for ${RELNUM} from ${SERVERNAME}... "
1071	rm -f latest.ssl
1072	fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/latest.ssl	\
1073	    2>${QUIETREDIR} || true
1074	if ! [ -r latest.ssl ]; then
1075		echo "failed."
1076		return 1
1077	fi
1078
1079	openssl rsautl -pubin -inkey pub.ssl -verify		\
1080	    < latest.ssl > tag.new 2>${QUIETREDIR} || true
1081	rm latest.ssl
1082
1083	if ! [ `wc -l < tag.new` = 1 ] ||
1084	    ! grep -qE	\
1085    "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
1086		tag.new; then
1087		echo "invalid signature."
1088		return 1
1089	fi
1090
1091	echo "done."
1092
1093	RELPATCHNUM=`cut -f 4 -d '|' < tag.new`
1094	TINDEXHASH=`cut -f 5 -d '|' < tag.new`
1095	EOLTIME=`cut -f 6 -d '|' < tag.new`
1096}
1097
1098# Sanity-check the patch number in a tag, to make sure that we're not
1099# going to "update" backwards and to prevent replay attacks.
1100fetch_tagsanity () {
1101	# Check that we're not going to move from -pX to -pY with Y < X.
1102	RELPX=`uname -r | sed -E 's,.*-,,'`
1103	if echo ${RELPX} | grep -qE '^p[0-9]+$'; then
1104		RELPX=`echo ${RELPX} | cut -c 2-`
1105	else
1106		RELPX=0
1107	fi
1108	if [ "${RELPATCHNUM}" -lt "${RELPX}" ]; then
1109		echo
1110		echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
1111		echo " appear older than what"
1112		echo "we are currently running (`uname -r`)!"
1113		echo "Cowardly refusing to proceed any further."
1114		return 1
1115	fi
1116
1117	# If "tag" exists and corresponds to ${RELNUM}, make sure that
1118	# it contains a patch number <= RELPATCHNUM, in order to protect
1119	# against rollback (replay) attacks.
1120	if [ -f tag ] &&
1121	    grep -qE	\
1122    "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
1123		tag; then
1124		LASTRELPATCHNUM=`cut -f 4 -d '|' < tag`
1125
1126		if [ "${RELPATCHNUM}" -lt "${LASTRELPATCHNUM}" ]; then
1127			echo
1128			echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
1129			echo " are older than the"
1130			echo -n "most recently seen updates"
1131			echo " (${RELNUM}-p${LASTRELPATCHNUM})."
1132			echo "Cowardly refusing to proceed any further."
1133			return 1
1134		fi
1135	fi
1136}
1137
1138# Fetch metadata index file
1139fetch_metadata_index () {
1140	echo ${NDEBUG} "Fetching metadata index... "
1141	rm -f ${TINDEXHASH}
1142	fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/t/${TINDEXHASH}
1143	    2>${QUIETREDIR}
1144	if ! [ -f ${TINDEXHASH} ]; then
1145		echo "failed."
1146		return 1
1147	fi
1148	if [ `${SHA256} -q ${TINDEXHASH}` != ${TINDEXHASH} ]; then
1149		echo "update metadata index corrupt."
1150		return 1
1151	fi
1152	echo "done."
1153}
1154
1155# Print an error message about signed metadata being bogus.
1156fetch_metadata_bogus () {
1157	echo
1158	echo "The update metadata$1 is correctly signed, but"
1159	echo "failed an integrity check."
1160	echo "Cowardly refusing to proceed any further."
1161	return 1
1162}
1163
1164# Construct tINDEX.new by merging the lines named in $1 from ${TINDEXHASH}
1165# with the lines not named in $@ from tINDEX.present (if that file exists).
1166fetch_metadata_index_merge () {
1167	for METAFILE in $@; do
1168		if [ `grep -E "^${METAFILE}\|" ${TINDEXHASH} | wc -l`	\
1169		    -ne 1 ]; then
1170			fetch_metadata_bogus " index"
1171			return 1
1172		fi
1173
1174		grep -E "${METAFILE}\|" ${TINDEXHASH}
1175	done |
1176	    sort > tINDEX.wanted
1177
1178	if [ -f tINDEX.present ]; then
1179		join -t '|' -v 2 tINDEX.wanted tINDEX.present |
1180		    sort -m - tINDEX.wanted > tINDEX.new
1181		rm tINDEX.wanted
1182	else
1183		mv tINDEX.wanted tINDEX.new
1184	fi
1185}
1186
1187# Sanity check all the lines of tINDEX.new.  Even if more metadata lines
1188# are added by future versions of the server, this won't cause problems,
1189# since the only lines which appear in tINDEX.new are the ones which we
1190# specifically grepped out of ${TINDEXHASH}.
1191fetch_metadata_index_sanity () {
1192	if grep -qvE '^[0-9A-Z.-]+\|[0-9a-f]{64}$' tINDEX.new; then
1193		fetch_metadata_bogus " index"
1194		return 1
1195	fi
1196}
1197
1198# Sanity check the metadata file $1.
1199fetch_metadata_sanity () {
1200	# Some aliases to save space later: ${P} is a character which can
1201	# appear in a path; ${M} is the four numeric metadata fields; and
1202	# ${H} is a sha256 hash.
1203	P="[-+./:=_[[:alnum:]]"
1204	M="[0-9]+\|[0-9]+\|[0-9]+\|[0-9]+"
1205	H="[0-9a-f]{64}"
1206
1207	# Check that the first four fields make sense.
1208	if gunzip -c < files/$1.gz |
1209	    grep -qvE "^[a-z]+\|[0-9a-z]+\|${P}+\|[fdL-]\|"; then
1210		fetch_metadata_bogus ""
1211		return 1
1212	fi
1213
1214	# Remove the first three fields.
1215	gunzip -c < files/$1.gz |
1216	    cut -f 4- -d '|' > sanitycheck.tmp
1217
1218	# Sanity check entries with type 'f'
1219	if grep -E '^f' sanitycheck.tmp |
1220	    grep -qvE "^f\|${M}\|${H}\|${P}*\$"; then
1221		fetch_metadata_bogus ""
1222		return 1
1223	fi
1224
1225	# Sanity check entries with type 'd'
1226	if grep -E '^d' sanitycheck.tmp |
1227	    grep -qvE "^d\|${M}\|\|\$"; then
1228		fetch_metadata_bogus ""
1229		return 1
1230	fi
1231
1232	# Sanity check entries with type 'L'
1233	if grep -E '^L' sanitycheck.tmp |
1234	    grep -qvE "^L\|${M}\|${P}*\|\$"; then
1235		fetch_metadata_bogus ""
1236		return 1
1237	fi
1238
1239	# Sanity check entries with type '-'
1240	if grep -E '^-' sanitycheck.tmp |
1241	    grep -qvE "^-\|\|\|\|\|\|"; then
1242		fetch_metadata_bogus ""
1243		return 1
1244	fi
1245
1246	# Clean up
1247	rm sanitycheck.tmp
1248}
1249
1250# Fetch the metadata index and metadata files listed in $@,
1251# taking advantage of metadata patches where possible.
1252fetch_metadata () {
1253	fetch_metadata_index || return 1
1254	fetch_metadata_index_merge $@ || return 1
1255	fetch_metadata_index_sanity || return 1
1256
1257	# Generate a list of wanted metadata patches
1258	join -t '|' -o 1.2,2.2 tINDEX.present tINDEX.new |
1259	    fetch_make_patchlist > patchlist
1260
1261	if [ -s patchlist ]; then
1262		# Attempt to fetch metadata patches
1263		echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
1264		echo ${NDEBUG} "metadata patches.${DDSTATS}"
1265		tr '|' '-' < patchlist |
1266		    lam -s "${FETCHDIR}/tp/" - -s ".gz" |
1267		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1268			2>${STATSREDIR} | fetch_progress
1269		echo "done."
1270
1271		# Attempt to apply metadata patches
1272		echo -n "Applying metadata patches... "
1273		tr '|' ' ' < patchlist |
1274		    while read X Y; do
1275			if [ ! -f "${X}-${Y}.gz" ]; then continue; fi
1276			gunzip -c < ${X}-${Y}.gz > diff
1277			gunzip -c < files/${X}.gz > diff-OLD
1278
1279			# Figure out which lines are being added and removed
1280			grep -E '^-' diff |
1281			    cut -c 2- |
1282			    while read PREFIX; do
1283				look "${PREFIX}" diff-OLD
1284			    done |
1285			    sort > diff-rm
1286			grep -E '^\+' diff |
1287			    cut -c 2- > diff-add
1288
1289			# Generate the new file
1290			comm -23 diff-OLD diff-rm |
1291			    sort - diff-add > diff-NEW
1292
1293			if [ `${SHA256} -q diff-NEW` = ${Y} ]; then
1294				mv diff-NEW files/${Y}
1295				gzip -n files/${Y}
1296			else
1297				mv diff-NEW ${Y}.bad
1298			fi
1299			rm -f ${X}-${Y}.gz diff
1300			rm -f diff-OLD diff-NEW diff-add diff-rm
1301		done 2>${QUIETREDIR}
1302		echo "done."
1303	fi
1304
1305	# Update metadata without patches
1306	cut -f 2 -d '|' < tINDEX.new |
1307	    while read Y; do
1308		if [ ! -f "files/${Y}.gz" ]; then
1309			echo ${Y};
1310		fi
1311	    done |
1312	    sort -u > filelist
1313
1314	if [ -s filelist ]; then
1315		echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1316		echo ${NDEBUG} "metadata files... "
1317		lam -s "${FETCHDIR}/m/" - -s ".gz" < filelist |
1318		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1319		    2>${QUIETREDIR}
1320
1321		while read Y; do
1322			if ! [ -f ${Y}.gz ]; then
1323				echo "failed."
1324				return 1
1325			fi
1326			if [ `gunzip -c < ${Y}.gz |
1327			    ${SHA256} -q` = ${Y} ]; then
1328				mv ${Y}.gz files/${Y}.gz
1329			else
1330				echo "metadata is corrupt."
1331				return 1
1332			fi
1333		done < filelist
1334		echo "done."
1335	fi
1336
1337# Sanity-check the metadata files.
1338	cut -f 2 -d '|' tINDEX.new > filelist
1339	while read X; do
1340		fetch_metadata_sanity ${X} || return 1
1341	done < filelist
1342
1343# Remove files which are no longer needed
1344	cut -f 2 -d '|' tINDEX.present |
1345	    sort > oldfiles
1346	cut -f 2 -d '|' tINDEX.new |
1347	    sort |
1348	    comm -13 - oldfiles |
1349	    lam -s "files/" - -s ".gz" |
1350	    xargs rm -f
1351	rm patchlist filelist oldfiles
1352	rm ${TINDEXHASH}
1353
1354# We're done!
1355	mv tINDEX.new tINDEX.present
1356	mv tag.new tag
1357
1358	return 0
1359}
1360
1361# Extract a subset of a downloaded metadata file containing only the parts
1362# which are listed in COMPONENTS.
1363fetch_filter_metadata_components () {
1364	METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'`
1365	gunzip -c < files/${METAHASH}.gz > $1.all
1366
1367	# Fish out the lines belonging to components we care about.
1368	for C in ${COMPONENTS}; do
1369		look "`echo ${C} | tr '/' '|'`|" $1.all
1370	done > $1
1371
1372	# Remove temporary file.
1373	rm $1.all
1374}
1375
1376# Generate a filtered version of the metadata file $1 from the downloaded
1377# file, by fishing out the lines corresponding to components we're trying
1378# to keep updated, and then removing lines corresponding to paths we want
1379# to ignore.
1380fetch_filter_metadata () {
1381	# Fish out the lines belonging to components we care about.
1382	fetch_filter_metadata_components $1
1383
1384	# Canonicalize directory names by removing any trailing / in
1385	# order to avoid listing directories multiple times if they
1386	# belong to multiple components.  Turning "/" into "" doesn't
1387	# matter, since we add a leading "/" when we use paths later.
1388	cut -f 3- -d '|' $1 |
1389	    sed -e 's,/|d|,|d|,' |
1390	    sort -u > $1.tmp
1391
1392	# Figure out which lines to ignore and remove them.
1393	for X in ${IGNOREPATHS}; do
1394		grep -E "^${X}" $1.tmp
1395	done |
1396	    sort -u |
1397	    comm -13 - $1.tmp > $1
1398
1399	# Remove temporary files.
1400	rm $1.tmp
1401}
1402
1403# Filter the metadata file $1 by adding lines with "/boot/$2"
1404# replaced by ${KERNELDIR} (which is `sysctl -n kern.bootfile` minus the
1405# trailing "/kernel"); and if "/boot/$2" does not exist, remove
1406# the original lines which start with that.
1407# Put another way: Deal with the fact that the FOO kernel is sometimes
1408# installed in /boot/FOO/ and is sometimes installed elsewhere.
1409fetch_filter_kernel_names () {
1410	grep ^/boot/$2 $1 |
1411	    sed -e "s,/boot/$2,${KERNELDIR},g" |
1412	    sort - $1 > $1.tmp
1413	mv $1.tmp $1
1414
1415	if ! [ -d /boot/$2 ]; then
1416		grep -v ^/boot/$2 $1 > $1.tmp
1417		mv $1.tmp $1
1418	fi
1419}
1420
1421# For all paths appearing in $1 or $3, inspect the system
1422# and generate $2 describing what is currently installed.
1423fetch_inspect_system () {
1424	# No errors yet...
1425	rm -f .err
1426
1427	# Tell the user why his disk is suddenly making lots of noise
1428	echo -n "Inspecting system... "
1429
1430	# Generate list of files to inspect
1431	cat $1 $3 |
1432	    cut -f 1 -d '|' |
1433	    sort -u > filelist
1434
1435	# Examine each file and output lines of the form
1436	# /path/to/file|type|device-inum|user|group|perm|flags|value
1437	# sorted by device and inode number.
1438	while read F; do
1439		# If the symlink/file/directory does not exist, record this.
1440		if ! [ -e ${BASEDIR}/${F} ]; then
1441			echo "${F}|-||||||"
1442			continue
1443		fi
1444		if ! [ -r ${BASEDIR}/${F} ]; then
1445			echo "Cannot read file: ${BASEDIR}/${F}"	\
1446			    >/dev/stderr
1447			touch .err
1448			return 1
1449		fi
1450
1451		# Otherwise, output an index line.
1452		if [ -L ${BASEDIR}/${F} ]; then
1453			echo -n "${F}|L|"
1454			stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1455			readlink ${BASEDIR}/${F};
1456		elif [ -f ${BASEDIR}/${F} ]; then
1457			echo -n "${F}|f|"
1458			stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1459			sha256 -q ${BASEDIR}/${F};
1460		elif [ -d ${BASEDIR}/${F} ]; then
1461			echo -n "${F}|d|"
1462			stat -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1463		else
1464			echo "Unknown file type: ${BASEDIR}/${F}"	\
1465			    >/dev/stderr
1466			touch .err
1467			return 1
1468		fi
1469	done < filelist |
1470	    sort -k 3,3 -t '|' > $2.tmp
1471	rm filelist
1472
1473	# Check if an error occurred during system inspection
1474	if [ -f .err ]; then
1475		return 1
1476	fi
1477
1478	# Convert to the form
1479	# /path/to/file|type|user|group|perm|flags|value|hlink
1480	# by resolving identical device and inode numbers into hard links.
1481	cut -f 1,3 -d '|' $2.tmp |
1482	    sort -k 1,1 -t '|' |
1483	    sort -s -u -k 2,2 -t '|' |
1484	    join -1 2 -2 3 -t '|' - $2.tmp |
1485	    awk -F \| -v OFS=\|		\
1486		'{
1487		    if (($2 == $3) || ($4 == "-"))
1488			print $3,$4,$5,$6,$7,$8,$9,""
1489		    else
1490			print $3,$4,$5,$6,$7,$8,$9,$2
1491		}' |
1492	    sort > $2
1493	rm $2.tmp
1494
1495	# We're finished looking around
1496	echo "done."
1497}
1498
1499# For any paths matching ${MERGECHANGES}, compare $1 and $2 and find any
1500# files which differ; generate $3 containing these paths and the old hashes.
1501fetch_filter_mergechanges () {
1502	# Pull out the paths and hashes of the files matching ${MERGECHANGES}.
1503	for F in $1 $2; do
1504		for X in ${MERGECHANGES}; do
1505			grep -E "^${X}" ${F}
1506		done |
1507		    cut -f 1,2,7 -d '|' |
1508		    sort > ${F}-values
1509	done
1510
1511	# Any line in $2-values which doesn't appear in $1-values and is a
1512	# file means that we should list the path in $3.
1513	comm -13 $1-values $2-values |
1514	    fgrep '|f|' |
1515	    cut -f 1 -d '|' > $2-paths
1516
1517	# For each path, pull out one (and only one!) entry from $1-values.
1518	# Note that we cannot distinguish which "old" version the user made
1519	# changes to; but hopefully any changes which occur due to security
1520	# updates will exist in both the "new" version and the version which
1521	# the user has installed, so the merging will still work.
1522	while read X; do
1523		look "${X}|" $1-values |
1524		    head -1
1525	done < $2-paths > $3
1526
1527	# Clean up
1528	rm $1-values $2-values $2-paths
1529}
1530
1531# For any paths matching ${UPDATEIFUNMODIFIED}, remove lines from $[123]
1532# which correspond to lines in $2 with hashes not matching $1 or $3, unless
1533# the paths are listed in $4.  For entries in $2 marked "not present"
1534# (aka. type -), remove lines from $[123] unless there is a corresponding
1535# entry in $1.
1536fetch_filter_unmodified_notpresent () {
1537	# Figure out which lines of $1 and $3 correspond to bits which
1538	# should only be updated if they haven't changed, and fish out
1539	# the (path, type, value) tuples.
1540	# NOTE: We don't consider a file to be "modified" if it matches
1541	# the hash from $3.
1542	for X in ${UPDATEIFUNMODIFIED}; do
1543		grep -E "^${X}" $1
1544		grep -E "^${X}" $3
1545	done |
1546	    cut -f 1,2,7 -d '|' |
1547	    sort > $1-values
1548
1549	# Do the same for $2.
1550	for X in ${UPDATEIFUNMODIFIED}; do
1551		grep -E "^${X}" $2
1552	done |
1553	    cut -f 1,2,7 -d '|' |
1554	    sort > $2-values
1555
1556	# Any entry in $2-values which is not in $1-values corresponds to
1557	# a path which we need to remove from $1, $2, and $3, unless it
1558	# that path appears in $4.
1559	comm -13 $1-values $2-values |
1560	    sort -t '|' -k 1,1 > mlines.tmp
1561	cut -f 1 -d '|' $4 |
1562	    sort |
1563	    join -v 2 -t '|' - mlines.tmp |
1564	    sort > mlines
1565	rm $1-values $2-values mlines.tmp
1566
1567	# Any lines in $2 which are not in $1 AND are "not present" lines
1568	# also belong in mlines.
1569	comm -13 $1 $2 |
1570	    cut -f 1,2,7 -d '|' |
1571	    fgrep '|-|' >> mlines
1572
1573	# Remove lines from $1, $2, and $3
1574	for X in $1 $2 $3; do
1575		sort -t '|' -k 1,1 ${X} > ${X}.tmp
1576		cut -f 1 -d '|' < mlines |
1577		    sort |
1578		    join -v 2 -t '|' - ${X}.tmp |
1579		    sort > ${X}
1580		rm ${X}.tmp
1581	done
1582
1583	# Store a list of the modified files, for future reference
1584	fgrep -v '|-|' mlines |
1585	    cut -f 1 -d '|' > modifiedfiles
1586	rm mlines
1587}
1588
1589# For each entry in $1 of type -, remove any corresponding
1590# entry from $2 if ${ALLOWADD} != "yes".  Remove all entries
1591# of type - from $1.
1592fetch_filter_allowadd () {
1593	cut -f 1,2 -d '|' < $1 |
1594	    fgrep '|-' |
1595	    cut -f 1 -d '|' > filesnotpresent
1596
1597	if [ ${ALLOWADD} != "yes" ]; then
1598		sort < $2 |
1599		    join -v 1 -t '|' - filesnotpresent |
1600		    sort > $2.tmp
1601		mv $2.tmp $2
1602	fi
1603
1604	sort < $1 |
1605	    join -v 1 -t '|' - filesnotpresent |
1606	    sort > $1.tmp
1607	mv $1.tmp $1
1608	rm filesnotpresent
1609}
1610
1611# If ${ALLOWDELETE} != "yes", then remove any entries from $1
1612# which don't correspond to entries in $2.
1613fetch_filter_allowdelete () {
1614	# Produce a lists ${PATH}|${TYPE}
1615	for X in $1 $2; do
1616		cut -f 1-2 -d '|' < ${X} |
1617		    sort -u > ${X}.nodes
1618	done
1619
1620	# Figure out which lines need to be removed from $1.
1621	if [ ${ALLOWDELETE} != "yes" ]; then
1622		comm -23 $1.nodes $2.nodes > $1.badnodes
1623	else
1624		: > $1.badnodes
1625	fi
1626
1627	# Remove the relevant lines from $1
1628	while read X; do
1629		look "${X}|" $1
1630	done < $1.badnodes |
1631	    comm -13 - $1 > $1.tmp
1632	mv $1.tmp $1
1633
1634	rm $1.badnodes $1.nodes $2.nodes
1635}
1636
1637# If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in $2
1638# with metadata not matching any entry in $1, replace the corresponding
1639# line of $3 with one having the same metadata as the entry in $2.
1640fetch_filter_modified_metadata () {
1641	# Fish out the metadata from $1 and $2
1642	for X in $1 $2; do
1643		cut -f 1-6 -d '|' < ${X} > ${X}.metadata
1644	done
1645
1646	# Find the metadata we need to keep
1647	if [ ${KEEPMODIFIEDMETADATA} = "yes" ]; then
1648		comm -13 $1.metadata $2.metadata > keepmeta
1649	else
1650		: > keepmeta
1651	fi
1652
1653	# Extract the lines which we need to remove from $3, and
1654	# construct the lines which we need to add to $3.
1655	: > $3.remove
1656	: > $3.add
1657	while read LINE; do
1658		NODE=`echo "${LINE}" | cut -f 1-2 -d '|'`
1659		look "${NODE}|" $3 >> $3.remove
1660		look "${NODE}|" $3 |
1661		    cut -f 7- -d '|' |
1662		    lam -s "${LINE}|" - >> $3.add
1663	done < keepmeta
1664
1665	# Remove the specified lines and add the new lines.
1666	sort $3.remove |
1667	    comm -13 - $3 |
1668	    sort -u - $3.add > $3.tmp
1669	mv $3.tmp $3
1670
1671	rm keepmeta $1.metadata $2.metadata $3.add $3.remove
1672}
1673
1674# Remove lines from $1 and $2 which are identical;
1675# no need to update a file if it isn't changing.
1676fetch_filter_uptodate () {
1677	comm -23 $1 $2 > $1.tmp
1678	comm -13 $1 $2 > $2.tmp
1679
1680	mv $1.tmp $1
1681	mv $2.tmp $2
1682}
1683
1684# Fetch any "clean" old versions of files we need for merging changes.
1685fetch_files_premerge () {
1686	# We only need to do anything if $1 is non-empty.
1687	if [ -s $1 ]; then
1688		# Tell the user what we're doing
1689		echo -n "Fetching files from ${OLDRELNUM} for merging... "
1690
1691		# List of files wanted
1692		fgrep '|f|' < $1 |
1693		    cut -f 3 -d '|' |
1694		    sort -u > files.wanted
1695
1696		# Only fetch the files we don't already have
1697		while read Y; do
1698			if [ ! -f "files/${Y}.gz" ]; then
1699				echo ${Y};
1700			fi
1701		done < files.wanted > filelist
1702
1703		# Actually fetch them
1704		lam -s "${OLDFETCHDIR}/f/" - -s ".gz" < filelist |
1705		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1706		    2>${QUIETREDIR}
1707
1708		# Make sure we got them all, and move them into /files/
1709		while read Y; do
1710			if ! [ -f ${Y}.gz ]; then
1711				echo "failed."
1712				return 1
1713			fi
1714			if [ `gunzip -c < ${Y}.gz |
1715			    ${SHA256} -q` = ${Y} ]; then
1716				mv ${Y}.gz files/${Y}.gz
1717			else
1718				echo "${Y} has incorrect hash."
1719				return 1
1720			fi
1721		done < filelist
1722		echo "done."
1723
1724		# Clean up
1725		rm filelist files.wanted
1726	fi
1727}
1728
1729# Prepare to fetch files: Generate a list of the files we need,
1730# copy the unmodified files we have into /files/, and generate
1731# a list of patches to download.
1732fetch_files_prepare () {
1733	# Tell the user why his disk is suddenly making lots of noise
1734	echo -n "Preparing to download files... "
1735
1736	# Reduce indices to ${PATH}|${HASH} pairs
1737	for X in $1 $2 $3; do
1738		cut -f 1,2,7 -d '|' < ${X} |
1739		    fgrep '|f|' |
1740		    cut -f 1,3 -d '|' |
1741		    sort > ${X}.hashes
1742	done
1743
1744	# List of files wanted
1745	cut -f 2 -d '|' < $3.hashes |
1746	    sort -u |
1747	    while read HASH; do
1748		if ! [ -f files/${HASH}.gz ]; then
1749			echo ${HASH}
1750		fi
1751	done > files.wanted
1752
1753	# Generate a list of unmodified files
1754	comm -12 $1.hashes $2.hashes |
1755	    sort -k 1,1 -t '|' > unmodified.files
1756
1757	# Copy all files into /files/.  We only need the unmodified files
1758	# for use in patching; but we'll want all of them if the user asks
1759	# to rollback the updates later.
1760	while read LINE; do
1761		F=`echo "${LINE}" | cut -f 1 -d '|'`
1762		HASH=`echo "${LINE}" | cut -f 2 -d '|'`
1763
1764		# Skip files we already have.
1765		if [ -f files/${HASH}.gz ]; then
1766			continue
1767		fi
1768
1769		# Make sure the file hasn't changed.
1770		cp "${BASEDIR}/${F}" tmpfile
1771		if [ `sha256 -q tmpfile` != ${HASH} ]; then
1772			echo
1773			echo "File changed while FreeBSD Update running: ${F}"
1774			return 1
1775		fi
1776
1777		# Place the file into storage.
1778		gzip -c < tmpfile > files/${HASH}.gz
1779		rm tmpfile
1780	done < $2.hashes
1781
1782	# Produce a list of patches to download
1783	sort -k 1,1 -t '|' $3.hashes |
1784	    join -t '|' -o 2.2,1.2 - unmodified.files |
1785	    fetch_make_patchlist > patchlist
1786
1787	# Garbage collect
1788	rm unmodified.files $1.hashes $2.hashes $3.hashes
1789
1790	# We don't need the list of possible old files any more.
1791	rm $1
1792
1793	# We're finished making noise
1794	echo "done."
1795}
1796
1797# Fetch files.
1798fetch_files () {
1799	# Attempt to fetch patches
1800	if [ -s patchlist ]; then
1801		echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
1802		echo ${NDEBUG} "patches.${DDSTATS}"
1803		tr '|' '-' < patchlist |
1804		    lam -s "${PATCHDIR}/" - |
1805		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1806			2>${STATSREDIR} | fetch_progress
1807		echo "done."
1808
1809		# Attempt to apply patches
1810		echo -n "Applying patches... "
1811		tr '|' ' ' < patchlist |
1812		    while read X Y; do
1813			if [ ! -f "${X}-${Y}" ]; then continue; fi
1814			gunzip -c < files/${X}.gz > OLD
1815
1816			bspatch OLD NEW ${X}-${Y}
1817
1818			if [ `${SHA256} -q NEW` = ${Y} ]; then
1819				mv NEW files/${Y}
1820				gzip -n files/${Y}
1821			fi
1822			rm -f diff OLD NEW ${X}-${Y}
1823		done 2>${QUIETREDIR}
1824		echo "done."
1825	fi
1826
1827	# Download files which couldn't be generate via patching
1828	while read Y; do
1829		if [ ! -f "files/${Y}.gz" ]; then
1830			echo ${Y};
1831		fi
1832	done < files.wanted > filelist
1833
1834	if [ -s filelist ]; then
1835		echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1836		echo ${NDEBUG} "files... "
1837		lam -s "${FETCHDIR}/f/" - -s ".gz" < filelist |
1838		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1839		    2>${QUIETREDIR}
1840
1841		while read Y; do
1842			if ! [ -f ${Y}.gz ]; then
1843				echo "failed."
1844				return 1
1845			fi
1846			if [ `gunzip -c < ${Y}.gz |
1847			    ${SHA256} -q` = ${Y} ]; then
1848				mv ${Y}.gz files/${Y}.gz
1849			else
1850				echo "${Y} has incorrect hash."
1851				return 1
1852			fi
1853		done < filelist
1854		echo "done."
1855	fi
1856
1857	# Clean up
1858	rm files.wanted filelist patchlist
1859}
1860
1861# Create and populate install manifest directory; and report what updates
1862# are available.
1863fetch_create_manifest () {
1864	# If we have an existing install manifest, nuke it.
1865	if [ -L "${BDHASH}-install" ]; then
1866		rm -r ${BDHASH}-install/
1867		rm ${BDHASH}-install
1868	fi
1869
1870	# Report to the user if any updates were avoided due to local changes
1871	if [ -s modifiedfiles ]; then
1872		echo
1873		echo -n "The following files are affected by updates, "
1874		echo "but no changes have"
1875		echo -n "been downloaded because the files have been "
1876		echo "modified locally:"
1877		cat modifiedfiles
1878	fi | $PAGER
1879	rm modifiedfiles
1880
1881	# If no files will be updated, tell the user and exit
1882	if ! [ -s INDEX-PRESENT ] &&
1883	    ! [ -s INDEX-NEW ]; then
1884		rm INDEX-PRESENT INDEX-NEW
1885		echo
1886		echo -n "No updates needed to update system to "
1887		echo "${RELNUM}-p${RELPATCHNUM}."
1888		return
1889	fi
1890
1891	# Divide files into (a) removed files, (b) added files, and
1892	# (c) updated files.
1893	cut -f 1 -d '|' < INDEX-PRESENT |
1894	    sort > INDEX-PRESENT.flist
1895	cut -f 1 -d '|' < INDEX-NEW |
1896	    sort > INDEX-NEW.flist
1897	comm -23 INDEX-PRESENT.flist INDEX-NEW.flist > files.removed
1898	comm -13 INDEX-PRESENT.flist INDEX-NEW.flist > files.added
1899	comm -12 INDEX-PRESENT.flist INDEX-NEW.flist > files.updated
1900	rm INDEX-PRESENT.flist INDEX-NEW.flist
1901
1902	# Report removed files, if any
1903	if [ -s files.removed ]; then
1904		echo
1905		echo -n "The following files will be removed "
1906		echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1907		cat files.removed
1908	fi | $PAGER
1909	rm files.removed
1910
1911	# Report added files, if any
1912	if [ -s files.added ]; then
1913		echo
1914		echo -n "The following files will be added "
1915		echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1916		cat files.added
1917	fi | $PAGER
1918	rm files.added
1919
1920	# Report updated files, if any
1921	if [ -s files.updated ]; then
1922		echo
1923		echo -n "The following files will be updated "
1924		echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1925
1926		cat files.updated
1927	fi | $PAGER
1928	rm files.updated
1929
1930	# Create a directory for the install manifest.
1931	MDIR=`mktemp -d install.XXXXXX` || return 1
1932
1933	# Populate it
1934	mv INDEX-PRESENT ${MDIR}/INDEX-OLD
1935	mv INDEX-NEW ${MDIR}/INDEX-NEW
1936
1937	# Link it into place
1938	ln -s ${MDIR} ${BDHASH}-install
1939}
1940
1941# Warn about any upcoming EoL
1942fetch_warn_eol () {
1943	# What's the current time?
1944	NOWTIME=`date "+%s"`
1945
1946	# When did we last warn about the EoL date?
1947	if [ -f lasteolwarn ]; then
1948		LASTWARN=`cat lasteolwarn`
1949	else
1950		LASTWARN=`expr ${NOWTIME} - 63072000`
1951	fi
1952
1953	# If the EoL time is past, warn.
1954	if [ ${EOLTIME} -lt ${NOWTIME} ]; then
1955		echo
1956		cat <<-EOF
1957		WARNING: `uname -sr` HAS PASSED ITS END-OF-LIFE DATE.
1958		Any security issues discovered after `date -r ${EOLTIME}`
1959		will not have been corrected.
1960		EOF
1961		return 1
1962	fi
1963
1964	# Figure out how long it has been since we last warned about the
1965	# upcoming EoL, and how much longer we have left.
1966	SINCEWARN=`expr ${NOWTIME} - ${LASTWARN}`
1967	TIMELEFT=`expr ${EOLTIME} - ${NOWTIME}`
1968
1969	# Don't warn if the EoL is more than 3 months away
1970	if [ ${TIMELEFT} -gt 7884000 ]; then
1971		return 0
1972	fi
1973
1974	# Don't warn if the time remaining is more than 3 times the time
1975	# since the last warning.
1976	if [ ${TIMELEFT} -gt `expr ${SINCEWARN} \* 3` ]; then
1977		return 0
1978	fi
1979
1980	# Figure out what time units to use.
1981	if [ ${TIMELEFT} -lt 604800 ]; then
1982		UNIT="day"
1983		SIZE=86400
1984	elif [ ${TIMELEFT} -lt 2678400 ]; then
1985		UNIT="week"
1986		SIZE=604800
1987	else
1988		UNIT="month"
1989		SIZE=2678400
1990	fi
1991
1992	# Compute the right number of units
1993	NUM=`expr ${TIMELEFT} / ${SIZE}`
1994	if [ ${NUM} != 1 ]; then
1995		UNIT="${UNIT}s"
1996	fi
1997
1998	# Print the warning
1999	echo
2000	cat <<-EOF
2001		WARNING: `uname -sr` is approaching its End-of-Life date.
2002		It is strongly recommended that you upgrade to a newer
2003		release within the next ${NUM} ${UNIT}.
2004	EOF
2005
2006	# Update the stored time of last warning
2007	echo ${NOWTIME} > lasteolwarn
2008}
2009
2010# Do the actual work involved in "fetch" / "cron".
2011fetch_run () {
2012	workdir_init || return 1
2013
2014	# Prepare the mirror list.
2015	fetch_pick_server_init && fetch_pick_server
2016
2017	# Try to fetch the public key until we run out of servers.
2018	while ! fetch_key; do
2019		fetch_pick_server || return 1
2020	done
2021
2022	# Try to fetch the metadata index signature ("tag") until we run
2023	# out of available servers; and sanity check the downloaded tag.
2024	while ! fetch_tag; do
2025		fetch_pick_server || return 1
2026	done
2027	fetch_tagsanity || return 1
2028
2029	# Fetch the latest INDEX-NEW and INDEX-OLD files.
2030	fetch_metadata INDEX-NEW INDEX-OLD || return 1
2031
2032	# Generate filtered INDEX-NEW and INDEX-OLD files containing only
2033	# the lines which (a) belong to components we care about, and (b)
2034	# don't correspond to paths we're explicitly ignoring.
2035	fetch_filter_metadata INDEX-NEW || return 1
2036	fetch_filter_metadata INDEX-OLD || return 1
2037
2038	# Translate /boot/${KERNCONF} into ${KERNELDIR}
2039	fetch_filter_kernel_names INDEX-NEW ${KERNCONF}
2040	fetch_filter_kernel_names INDEX-OLD ${KERNCONF}
2041
2042	# For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the
2043	# system and generate an INDEX-PRESENT file.
2044	fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2045
2046	# Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which
2047	# correspond to lines in INDEX-PRESENT with hashes not appearing
2048	# in INDEX-OLD or INDEX-NEW.  Also remove lines where the entry in
2049	# INDEX-PRESENT has type - and there isn't a corresponding entry in
2050	# INDEX-OLD with type -.
2051	fetch_filter_unmodified_notpresent	\
2052	    INDEX-OLD INDEX-PRESENT INDEX-NEW /dev/null
2053
2054	# For each entry in INDEX-PRESENT of type -, remove any corresponding
2055	# entry from INDEX-NEW if ${ALLOWADD} != "yes".  Remove all entries
2056	# of type - from INDEX-PRESENT.
2057	fetch_filter_allowadd INDEX-PRESENT INDEX-NEW
2058
2059	# If ${ALLOWDELETE} != "yes", then remove any entries from
2060	# INDEX-PRESENT which don't correspond to entries in INDEX-NEW.
2061	fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW
2062
2063	# If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in
2064	# INDEX-PRESENT with metadata not matching any entry in INDEX-OLD,
2065	# replace the corresponding line of INDEX-NEW with one having the
2066	# same metadata as the entry in INDEX-PRESENT.
2067	fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW
2068
2069	# Remove lines from INDEX-PRESENT and INDEX-NEW which are identical;
2070	# no need to update a file if it isn't changing.
2071	fetch_filter_uptodate INDEX-PRESENT INDEX-NEW
2072
2073	# Prepare to fetch files: Generate a list of the files we need,
2074	# copy the unmodified files we have into /files/, and generate
2075	# a list of patches to download.
2076	fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2077
2078	# Fetch files.
2079	fetch_files || return 1
2080
2081	# Create and populate install manifest directory; and report what
2082	# updates are available.
2083	fetch_create_manifest || return 1
2084
2085	# Warn about any upcoming EoL
2086	fetch_warn_eol || return 1
2087}
2088
2089# If StrictComponents is not "yes", generate a new components list
2090# with only the components which appear to be installed.
2091upgrade_guess_components () {
2092	if [ "${STRICTCOMPONENTS}" = "no" ]; then
2093		# Generate filtered INDEX-ALL with only the components listed
2094		# in COMPONENTS.
2095		fetch_filter_metadata_components $1 || return 1
2096
2097		# Tell the user why his disk is suddenly making lots of noise
2098		echo -n "Inspecting system... "
2099
2100		# Look at the files on disk, and assume that a component is
2101		# supposed to be present if it is more than half-present.
2102		cut -f 1-3 -d '|' < INDEX-ALL |
2103		    tr '|' ' ' |
2104		    while read C S F; do
2105			if [ -e ${BASEDIR}/${F} ]; then
2106				echo "+ ${C}|${S}"
2107			fi
2108			echo "= ${C}|${S}"
2109		    done |
2110		    sort |
2111		    uniq -c |
2112		    sed -E 's,^ +,,' > compfreq
2113		grep ' = ' compfreq |
2114		    cut -f 1,3 -d ' ' |
2115		    sort -k 2,2 -t ' ' > compfreq.total
2116		grep ' + ' compfreq |
2117		    cut -f 1,3 -d ' ' |
2118		    sort -k 2,2 -t ' ' > compfreq.present
2119		join -t ' ' -1 2 -2 2 compfreq.present compfreq.total |
2120		    while read S P T; do
2121			if [ ${P} -gt `expr ${T} / 2` ]; then
2122				echo ${S}
2123			fi
2124		    done > comp.present
2125		cut -f 2 -d ' ' < compfreq.total > comp.total
2126		rm INDEX-ALL compfreq compfreq.total compfreq.present
2127
2128		# We're done making noise.
2129		echo "done."
2130
2131		# Sometimes the kernel isn't installed where INDEX-ALL
2132		# thinks that it should be: In particular, it is often in
2133		# /boot/kernel instead of /boot/GENERIC or /boot/SMP.  To
2134		# deal with this, if "kernel|X" is listed in comp.total
2135		# (i.e., is a component which would be upgraded if it is
2136		# found to be present) we will add it to comp.present.
2137		# If "kernel|<anything>" is in comp.total but "kernel|X" is
2138		# not, we print a warning -- the user is running a kernel
2139		# which isn't part of the release.
2140		KCOMP=`echo ${KERNCONF} | tr 'A-Z' 'a-z'`
2141		grep -E "^kernel\|${KCOMP}\$" comp.total >> comp.present
2142
2143		if grep -qE "^kernel\|" comp.total &&
2144		    ! grep -qE "^kernel\|${KCOMP}\$" comp.total; then
2145			cat <<-EOF
2146
2147WARNING: This system is running a "${KCOMP}" kernel, which is not a
2148kernel configuration distributed as part of FreeBSD ${RELNUM}.
2149This kernel will not be updated: you MUST update the kernel manually
2150before running "$0 install".
2151			EOF
2152		fi
2153
2154		# Re-sort the list of installed components and generate
2155		# the list of non-installed components.
2156		sort -u < comp.present > comp.present.tmp
2157		mv comp.present.tmp comp.present
2158		comm -13 comp.present comp.total > comp.absent
2159
2160		# Ask the user to confirm that what we have is correct.  To
2161		# reduce user confusion, translate "X|Y" back to "X/Y" (as
2162		# subcomponents must be listed in the configuration file).
2163		echo
2164		echo -n "The following components of FreeBSD "
2165		echo "seem to be installed:"
2166		tr '|' '/' < comp.present |
2167		    fmt -72
2168		echo
2169		echo -n "The following components of FreeBSD "
2170		echo "do not seem to be installed:"
2171		tr '|' '/' < comp.absent |
2172		    fmt -72
2173		echo
2174		continuep || return 1
2175		echo
2176
2177		# Suck the generated list of components into ${COMPONENTS}.
2178		# Note that comp.present.tmp is used due to issues with
2179		# pipelines and setting variables.
2180		COMPONENTS=""
2181		tr '|' '/' < comp.present > comp.present.tmp
2182		while read C; do
2183			COMPONENTS="${COMPONENTS} ${C}"
2184		done < comp.present.tmp
2185
2186		# Delete temporary files
2187		rm comp.present comp.present.tmp comp.absent comp.total
2188	fi
2189}
2190
2191# If StrictComponents is not "yes", COMPONENTS contains an entry
2192# corresponding to the currently running kernel, and said kernel
2193# does not exist in the new release, add "kernel/generic" to the
2194# list of components.
2195upgrade_guess_new_kernel () {
2196	if [ "${STRICTCOMPONENTS}" = "no" ]; then
2197		# Grab the unfiltered metadata file.
2198		METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'`
2199		gunzip -c < files/${METAHASH}.gz > $1.all
2200
2201		# If "kernel/${KCOMP}" is in ${COMPONENTS} and that component
2202		# isn't in $1.all, we need to add kernel/generic.
2203		for C in ${COMPONENTS}; do
2204			if [ ${C} = "kernel/${KCOMP}" ] &&
2205			    ! grep -qE "^kernel\|${KCOMP}\|" $1.all; then
2206				COMPONENTS="${COMPONENTS} kernel/generic"
2207				NKERNCONF="GENERIC"
2208				cat <<-EOF
2209
2210WARNING: This system is running a "${KCOMP}" kernel, which is not a
2211kernel configuration distributed as part of FreeBSD ${RELNUM}.
2212As part of upgrading to FreeBSD ${RELNUM}, this kernel will be
2213replaced with a "generic" kernel.
2214				EOF
2215				continuep || return 1
2216			fi
2217		done
2218
2219		# Don't need this any more...
2220		rm $1.all
2221	fi
2222}
2223
2224# Convert INDEX-OLD (last release) and INDEX-ALL (new release) into
2225# INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades).
2226upgrade_oldall_to_oldnew () {
2227	# For each ${F}|... which appears in INDEX-ALL but does not appear
2228	# in INDEX-OLD, add ${F}|-|||||| to INDEX-OLD.
2229	cut -f 1 -d '|' < $1 |
2230	    sort -u > $1.paths
2231	cut -f 1 -d '|' < $2 |
2232	    sort -u |
2233	    comm -13 $1.paths - |
2234	    lam - -s "|-||||||" |
2235	    sort - $1 > $1.tmp
2236	mv $1.tmp $1
2237
2238	# Remove lines from INDEX-OLD which also appear in INDEX-ALL
2239	comm -23 $1 $2 > $1.tmp
2240	mv $1.tmp $1
2241
2242	# Remove lines from INDEX-ALL which have a file name not appearing
2243	# anywhere in INDEX-OLD (since these must be files which haven't
2244	# changed -- if they were new, there would be an entry of type "-").
2245	cut -f 1 -d '|' < $1 |
2246	    sort -u > $1.paths
2247	sort -k 1,1 -t '|' < $2 |
2248	    join -t '|' - $1.paths |
2249	    sort > $2.tmp
2250	rm $1.paths
2251	mv $2.tmp $2
2252
2253	# Rename INDEX-ALL to INDEX-NEW.
2254	mv $2 $3
2255}
2256
2257# From the list of "old" files in $1, merge changes in $2 with those in $3,
2258# and update $3 to reflect the hashes of merged files.
2259upgrade_merge () {
2260	# We only need to do anything if $1 is non-empty.
2261	if [ -s $1 ]; then
2262		cut -f 1 -d '|' $1 |
2263		    sort > $1-paths
2264
2265		# Create staging area for merging files
2266		rm -rf merge/
2267		while read F; do
2268			D=`dirname ${F}`
2269			mkdir -p merge/old/${D}
2270			mkdir -p merge/${OLDRELNUM}/${D}
2271			mkdir -p merge/${RELNUM}/${D}
2272			mkdir -p merge/new/${D}
2273		done < $1-paths
2274
2275		# Copy in files
2276		while read F; do
2277			# Currently installed file
2278			V=`look "${F}|" $2 | cut -f 7 -d '|'`
2279			gunzip < files/${V}.gz > merge/old/${F}
2280
2281			# Old release
2282			if look "${F}|" $1 | fgrep -q "|f|"; then
2283				V=`look "${F}|" $1 | cut -f 3 -d '|'`
2284				gunzip < files/${V}.gz		\
2285				    > merge/${OLDRELNUM}/${F}
2286			fi
2287
2288			# New release
2289			if look "${F}|" $3 | cut -f 1,2,7 -d '|' |
2290			    fgrep -q "|f|"; then
2291				V=`look "${F}|" $3 | cut -f 7 -d '|'`
2292				gunzip < files/${V}.gz		\
2293				    > merge/${RELNUM}/${F}
2294			fi
2295		done < $1-paths
2296
2297		# Attempt to automatically merge changes
2298		echo -n "Attempting to automatically merge "
2299		echo -n "changes in files..."
2300		: > failed.merges
2301		while read F; do
2302			# If the file doesn't exist in the new release,
2303			# the result of "merging changes" is having the file
2304			# not exist.
2305			if ! [ -f merge/${RELNUM}/${F} ]; then
2306				continue
2307			fi
2308
2309			# If the file didn't exist in the old release, we're
2310			# going to throw away the existing file and hope that
2311			# the version from the new release is what we want.
2312			if ! [ -f merge/${OLDRELNUM}/${F} ]; then
2313				cp merge/${RELNUM}/${F} merge/new/${F}
2314				continue
2315			fi
2316
2317			# Some files need special treatment.
2318			case ${F} in
2319			/etc/spwd.db | /etc/pwd.db | /etc/login.conf.db)
2320				# Don't merge these -- we're rebuild them
2321				# after updates are installed.
2322				cp merge/old/${F} merge/new/${F}
2323				;;
2324			*)
2325				if ! merge -p -L "current version"	\
2326				    -L "${OLDRELNUM}" -L "${RELNUM}"	\
2327				    merge/old/${F}			\
2328				    merge/${OLDRELNUM}/${F}		\
2329				    merge/${RELNUM}/${F}		\
2330				    > merge/new/${F} 2>/dev/null; then
2331					echo ${F} >> failed.merges
2332				fi
2333				;;
2334			esac
2335		done < $1-paths
2336		echo " done."
2337
2338		# Ask the user to handle any files which didn't merge.
2339		while read F; do
2340			cat <<-EOF
2341
2342The following file could not be merged automatically: ${F}
2343Press Enter to edit this file in ${EDITOR} and resolve the conflicts
2344manually...
2345			EOF
2346			read dummy </dev/tty
2347			${EDITOR} `pwd`/merge/new/${F} < /dev/tty
2348		done < failed.merges
2349		rm failed.merges
2350
2351		# Ask the user to confirm that he likes how the result
2352		# of merging files.
2353		while read F; do
2354			# Skip files which haven't changed.
2355			if [ -f merge/new/${F} ] &&
2356			    cmp -s merge/old/${F} merge/new/${F}; then
2357				continue
2358			fi
2359
2360			# Warn about files which are ceasing to exist.
2361			if ! [ -f merge/new/${F} ]; then
2362				cat <<-EOF
2363
2364The following file will be removed, as it no longer exists in
2365FreeBSD ${RELNUM}: ${F}
2366				EOF
2367				continuep < /dev/tty || return 1
2368				continue
2369			fi
2370
2371			# Print changes for the user's approval.
2372			cat <<-EOF
2373
2374The following changes, which occurred between FreeBSD ${OLDRELNUM} and
2375FreeBSD ${RELNUM} have been merged into ${F}:
2376EOF
2377			diff -U 5 -L "current version" -L "new version"	\
2378			    merge/old/${F} merge/new/${F} || true
2379			continuep < /dev/tty || return 1
2380		done < $1-paths
2381
2382		# Store merged files.
2383		while read F; do
2384			if [ -f merge/new/${F} ]; then
2385				V=`${SHA256} -q merge/new/${F}`
2386
2387				gzip -c < merge/new/${F} > files/${V}.gz
2388				echo "${F}|${V}"
2389			fi
2390		done < $1-paths > newhashes
2391
2392		# Pull lines out from $3 which need to be updated to
2393		# reflect merged files.
2394		while read F; do
2395			look "${F}|" $3
2396		done < $1-paths > $3-oldlines
2397
2398		# Update lines to reflect merged files
2399		join -t '|' -o 1.1,1.2,1.3,1.4,1.5,1.6,2.2,1.8		\
2400		    $3-oldlines newhashes > $3-newlines
2401
2402		# Remove old lines from $3 and add new lines.
2403		sort $3-oldlines |
2404		    comm -13 - $3 |
2405		    sort - $3-newlines > $3.tmp
2406		mv $3.tmp $3
2407
2408		# Clean up
2409		rm $1-paths newhashes $3-oldlines $3-newlines
2410		rm -rf merge/
2411	fi
2412
2413	# We're done with merging files.
2414	rm $1
2415}
2416
2417# Do the work involved in fetching upgrades to a new release
2418upgrade_run () {
2419	workdir_init || return 1
2420
2421	# Prepare the mirror list.
2422	fetch_pick_server_init && fetch_pick_server
2423
2424	# Try to fetch the public key until we run out of servers.
2425	while ! fetch_key; do
2426		fetch_pick_server || return 1
2427	done
2428 
2429	# Try to fetch the metadata index signature ("tag") until we run
2430	# out of available servers; and sanity check the downloaded tag.
2431	while ! fetch_tag; do
2432		fetch_pick_server || return 1
2433	done
2434	fetch_tagsanity || return 1
2435
2436	# Fetch the INDEX-OLD and INDEX-ALL.
2437	fetch_metadata INDEX-OLD INDEX-ALL || return 1
2438
2439	# If StrictComponents is not "yes", generate a new components list
2440	# with only the components which appear to be installed.
2441	upgrade_guess_components INDEX-ALL || return 1
2442
2443	# Generate filtered INDEX-OLD and INDEX-ALL files containing only
2444	# the components we want and without anything marked as "Ignore".
2445	fetch_filter_metadata INDEX-OLD || return 1
2446	fetch_filter_metadata INDEX-ALL || return 1
2447
2448	# Merge the INDEX-OLD and INDEX-ALL files into INDEX-OLD.
2449	sort INDEX-OLD INDEX-ALL > INDEX-OLD.tmp
2450	mv INDEX-OLD.tmp INDEX-OLD
2451	rm INDEX-ALL
2452
2453	# Adjust variables for fetching files from the new release.
2454	OLDRELNUM=${RELNUM}
2455	RELNUM=${TARGETRELEASE}
2456	OLDFETCHDIR=${FETCHDIR}
2457	FETCHDIR=${RELNUM}/${ARCH}
2458
2459	# Try to fetch the NEW metadata index signature ("tag") until we run
2460	# out of available servers; and sanity check the downloaded tag.
2461	while ! fetch_tag; do
2462		fetch_pick_server || return 1
2463	done
2464
2465	# Fetch the new INDEX-ALL.
2466	fetch_metadata INDEX-ALL || return 1
2467
2468	# If StrictComponents is not "yes", COMPONENTS contains an entry
2469	# corresponding to the currently running kernel, and said kernel
2470	# does not exist in the new release, add "kernel/generic" to the
2471	# list of components.
2472	upgrade_guess_new_kernel INDEX-ALL || return 1
2473
2474	# Filter INDEX-ALL to contain only the components we want and without
2475	# anything marked as "Ignore".
2476	fetch_filter_metadata INDEX-ALL || return 1
2477
2478	# Convert INDEX-OLD (last release) and INDEX-ALL (new release) into
2479	# INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades).
2480	upgrade_oldall_to_oldnew INDEX-OLD INDEX-ALL INDEX-NEW
2481
2482	# Translate /boot/${KERNCONF} or /boot/${NKERNCONF} into ${KERNELDIR}
2483	fetch_filter_kernel_names INDEX-NEW ${NKERNCONF}
2484	fetch_filter_kernel_names INDEX-OLD ${KERNCONF}
2485
2486	# For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the
2487	# system and generate an INDEX-PRESENT file.
2488	fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2489
2490	# Based on ${MERGECHANGES}, generate a file tomerge-old with the
2491	# paths and hashes of old versions of files to merge.
2492	fetch_filter_mergechanges INDEX-OLD INDEX-PRESENT tomerge-old
2493
2494	# Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which
2495	# correspond to lines in INDEX-PRESENT with hashes not appearing
2496	# in INDEX-OLD or INDEX-NEW.  Also remove lines where the entry in
2497	# INDEX-PRESENT has type - and there isn't a corresponding entry in
2498	# INDEX-OLD with type -.
2499	fetch_filter_unmodified_notpresent	\
2500	    INDEX-OLD INDEX-PRESENT INDEX-NEW tomerge-old
2501
2502	# For each entry in INDEX-PRESENT of type -, remove any corresponding
2503	# entry from INDEX-NEW if ${ALLOWADD} != "yes".  Remove all entries
2504	# of type - from INDEX-PRESENT.
2505	fetch_filter_allowadd INDEX-PRESENT INDEX-NEW
2506
2507	# If ${ALLOWDELETE} != "yes", then remove any entries from
2508	# INDEX-PRESENT which don't correspond to entries in INDEX-NEW.
2509	fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW
2510
2511	# If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in
2512	# INDEX-PRESENT with metadata not matching any entry in INDEX-OLD,
2513	# replace the corresponding line of INDEX-NEW with one having the
2514	# same metadata as the entry in INDEX-PRESENT.
2515	fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW
2516
2517	# Remove lines from INDEX-PRESENT and INDEX-NEW which are identical;
2518	# no need to update a file if it isn't changing.
2519	fetch_filter_uptodate INDEX-PRESENT INDEX-NEW
2520
2521	# Fetch "clean" files from the old release for merging changes.
2522	fetch_files_premerge tomerge-old
2523
2524	# Prepare to fetch files: Generate a list of the files we need,
2525	# copy the unmodified files we have into /files/, and generate
2526	# a list of patches to download.
2527	fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2528
2529	# Fetch patches from to-${RELNUM}/${ARCH}/bp/
2530	PATCHDIR=to-${RELNUM}/${ARCH}/bp
2531	fetch_files || return 1
2532
2533	# Merge configuration file changes.
2534	upgrade_merge tomerge-old INDEX-PRESENT INDEX-NEW || return 1
2535
2536	# Create and populate install manifest directory; and report what
2537	# updates are available.
2538	fetch_create_manifest || return 1
2539
2540	# Leave a note behind to tell the "install" command that the kernel
2541	# needs to be installed before the world.
2542	touch ${BDHASH}-install/kernelfirst
2543
2544	# Remind the user that they need to run "freebsd-update install"
2545	# to install the downloaded bits, in case they didn't RTFM.
2546	echo "To install the downloaded upgrades, run \"$0 install\"."
2547}
2548
2549# Make sure that all the file hashes mentioned in $@ have corresponding
2550# gzipped files stored in /files/.
2551install_verify () {
2552	# Generate a list of hashes
2553	cat $@ |
2554	    cut -f 2,7 -d '|' |
2555	    grep -E '^f' |
2556	    cut -f 2 -d '|' |
2557	    sort -u > filelist
2558
2559	# Make sure all the hashes exist
2560	while read HASH; do
2561		if ! [ -f files/${HASH}.gz ]; then
2562			echo -n "Update files missing -- "
2563			echo "this should never happen."
2564			echo "Re-run '$0 fetch'."
2565			return 1
2566		fi
2567	done < filelist
2568
2569	# Clean up
2570	rm filelist
2571}
2572
2573# Remove the system immutable flag from files
2574install_unschg () {
2575	# Generate file list
2576	cat $@ |
2577	    cut -f 1 -d '|' > filelist
2578
2579	# Remove flags
2580	while read F; do
2581		if ! [ -e ${BASEDIR}/${F} ]; then
2582			continue
2583		fi
2584
2585		chflags noschg ${BASEDIR}/${F} || return 1
2586	done < filelist
2587
2588	# Clean up
2589	rm filelist
2590}
2591
2592# Decide which directory name to use for kernel backups.
2593backup_kernel_finddir () {
2594	CNT=0
2595	while true ; do
2596		# Pathname does not exist, so it is OK use that name
2597		# for backup directory.
2598		if [ ! -e $BACKUPKERNELDIR ]; then
2599			return 0
2600		fi
2601
2602		# If directory do exist, we only use if it has our
2603		# marker file.
2604		if [ -d $BACKUPKERNELDIR -a \
2605			-e $BACKUPKERNELDIR/.freebsd-update ]; then
2606			return 0
2607		fi
2608
2609		# We could not use current directory name, so add counter to
2610		# the end and try again.
2611		CNT=$((CNT + 1))
2612		if [ $CNT -gt 9 ]; then
2613			echo "Could not find valid backup dir ($BACKUPKERNELDIR)"
2614			exit 1
2615		fi
2616		BACKUPKERNELDIR="`echo $BACKUPKERNELDIR | sed -Ee 's/[0-9]\$//'`"
2617		BACKUPKERNELDIR="${BACKUPKERNELDIR}${CNT}"
2618	done
2619}
2620
2621# Backup the current kernel using hardlinks, if not disabled by user.
2622# Since we delete all files in the directory used for previous backups
2623# we create a marker file called ".freebsd-update" in the directory so
2624# we can determine on the next run that the directory was created by
2625# freebsd-update and we then do not accidentally remove user files in
2626# the unlikely case that the user has created a directory with a
2627# conflicting name.
2628backup_kernel () {
2629	# Only make kernel backup is so configured.
2630	if [ $BACKUPKERNEL != yes ]; then
2631		return 0
2632	fi
2633
2634	# Decide which directory name to use for kernel backups.
2635	backup_kernel_finddir
2636
2637	# Remove old kernel backup files.  If $BACKUPKERNELDIR was
2638	# "not ours", backup_kernel_finddir would have exited, so
2639	# deleting the directory content is as safe as we can make it.
2640	if [ -d $BACKUPKERNELDIR ]; then
2641		rm -fr $BACKUPKERNELDIR
2642	fi
2643
2644	# Create directories for backup.
2645	mkdir -p $BACKUPKERNELDIR
2646	mtree -cdn -p "${KERNELDIR}" | \
2647	    mtree -Ue -p "${BACKUPKERNELDIR}" > /dev/null
2648
2649	# Mark the directory as having been created by freebsd-update.
2650	touch $BACKUPKERNELDIR/.freebsd-update
2651	if [ $? -ne 0 ]; then
2652		echo "Could not create kernel backup directory"
2653		exit 1
2654	fi
2655
2656	# Disable pathname expansion to be sure *.symbols is not
2657	# expanded.
2658	set -f
2659
2660	# Use find to ignore symbol files, unless disabled by user.
2661	if [ $BACKUPKERNELSYMBOLFILES = yes ]; then
2662		FINDFILTER=""
2663	else
2664		FINDFILTER=-"a ! -name *.symbols"
2665	fi
2666
2667	# Backup all the kernel files using hardlinks.
2668	(cd $KERNELDIR && find . -type f $FINDFILTER -exec \
2669	    cp -pl '{}' ${BACKUPKERNELDIR}/'{}' \;)
2670
2671	# Re-enable patchname expansion.
2672	set +f
2673}
2674
2675# Install new files
2676install_from_index () {
2677	# First pass: Do everything apart from setting file flags.  We
2678	# can't set flags yet, because schg inhibits hard linking.
2679	sort -k 1,1 -t '|' $1 |
2680	    tr '|' ' ' |
2681	    while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
2682		case ${TYPE} in
2683		d)
2684			# Create a directory
2685			install -d -o ${OWNER} -g ${GROUP}		\
2686			    -m ${PERM} ${BASEDIR}/${FPATH}
2687			;;
2688		f)
2689			if [ -z "${LINK}" ]; then
2690				# Create a file, without setting flags.
2691				gunzip < files/${HASH}.gz > ${HASH}
2692				install -S -o ${OWNER} -g ${GROUP}	\
2693				    -m ${PERM} ${HASH} ${BASEDIR}/${FPATH}
2694				rm ${HASH}
2695			else
2696				# Create a hard link.
2697				ln -f ${BASEDIR}/${LINK} ${BASEDIR}/${FPATH}
2698			fi
2699			;;
2700		L)
2701			# Create a symlink
2702			ln -sfh ${HASH} ${BASEDIR}/${FPATH}
2703			;;
2704		esac
2705	    done
2706
2707	# Perform a second pass, adding file flags.
2708	tr '|' ' ' < $1 |
2709	    while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
2710		if [ ${TYPE} = "f" ] &&
2711		    ! [ ${FLAGS} = "0" ]; then
2712			chflags ${FLAGS} ${BASEDIR}/${FPATH}
2713		fi
2714	    done
2715}
2716
2717# Remove files which we want to delete
2718install_delete () {
2719	# Generate list of new files
2720	cut -f 1 -d '|' < $2 |
2721	    sort > newfiles
2722
2723	# Generate subindex of old files we want to nuke
2724	sort -k 1,1 -t '|' $1 |
2725	    join -t '|' -v 1 - newfiles |
2726	    sort -r -k 1,1 -t '|' |
2727	    cut -f 1,2 -d '|' |
2728	    tr '|' ' ' > killfiles
2729
2730	# Remove the offending bits
2731	while read FPATH TYPE; do
2732		case ${TYPE} in
2733		d)
2734			rmdir ${BASEDIR}/${FPATH}
2735			;;
2736		f)
2737			rm ${BASEDIR}/${FPATH}
2738			;;
2739		L)
2740			rm ${BASEDIR}/${FPATH}
2741			;;
2742		esac
2743	done < killfiles
2744
2745	# Clean up
2746	rm newfiles killfiles
2747}
2748
2749# Install new files, delete old files, and update linker.hints
2750install_files () {
2751	# If we haven't already dealt with the kernel, deal with it.
2752	if ! [ -f $1/kerneldone ]; then
2753		grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD
2754		grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW
2755
2756		# Backup current kernel before installing a new one
2757		backup_kernel || return 1
2758
2759		# Install new files
2760		install_from_index INDEX-NEW || return 1
2761
2762		# Remove files which need to be deleted
2763		install_delete INDEX-OLD INDEX-NEW || return 1
2764
2765		# Update linker.hints if necessary
2766		if [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2767			kldxref -R /boot/ 2>/dev/null
2768		fi
2769
2770		# We've finished updating the kernel.
2771		touch $1/kerneldone
2772
2773		# Do we need to ask for a reboot now?
2774		if [ -f $1/kernelfirst ] &&
2775		    [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2776			cat <<-EOF
2777
2778Kernel updates have been installed.  Please reboot and run
2779"$0 install" again to finish installing updates.
2780			EOF
2781			exit 0
2782		fi
2783	fi
2784
2785	# If we haven't already dealt with the world, deal with it.
2786	if ! [ -f $1/worlddone ]; then
2787		# Install new shared libraries next
2788		grep -vE '^/boot/' $1/INDEX-NEW |
2789		    grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2790		install_from_index INDEX-NEW || return 1
2791
2792		# Deal with everything else
2793		grep -vE '^/boot/' $1/INDEX-OLD |
2794		    grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2795		grep -vE '^/boot/' $1/INDEX-NEW |
2796		    grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2797		install_from_index INDEX-NEW || return 1
2798		install_delete INDEX-OLD INDEX-NEW || return 1
2799
2800		# Rebuild /etc/spwd.db and /etc/pwd.db if necessary.
2801		if [ /etc/master.passwd -nt /etc/spwd.db ] ||
2802		    [ /etc/master.passwd -nt /etc/pwd.db ]; then
2803			pwd_mkdb /etc/master.passwd
2804		fi
2805
2806		# Rebuild /etc/login.conf.db if necessary.
2807		if [ /etc/login.conf -nt /etc/login.conf.db ]; then
2808			cap_mkdb /etc/login.conf
2809		fi
2810
2811		# We've finished installing the world and deleting old files
2812		# which are not shared libraries.
2813		touch $1/worlddone
2814
2815		# Do we need to ask the user to portupgrade now?
2816		grep -vE '^/boot/' $1/INDEX-NEW |
2817		    grep -E '/lib/.*\.so\.[0-9]+\|' |
2818		    cut -f 1 -d '|' |
2819		    sort > newfiles
2820		if grep -vE '^/boot/' $1/INDEX-OLD |
2821		    grep -E '/lib/.*\.so\.[0-9]+\|' |
2822		    cut -f 1 -d '|' |
2823		    sort |
2824		    join -v 1 - newfiles |
2825		    grep -q .; then
2826			cat <<-EOF
2827
2828Completing this upgrade requires removing old shared object files.
2829Please rebuild all installed 3rd party software (e.g., programs
2830installed from the ports tree) and then run "$0 install"
2831again to finish installing updates.
2832			EOF
2833			rm newfiles
2834			exit 0
2835		fi
2836		rm newfiles
2837	fi
2838
2839	# Remove old shared libraries
2840	grep -vE '^/boot/' $1/INDEX-NEW |
2841	    grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2842	grep -vE '^/boot/' $1/INDEX-OLD |
2843	    grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2844	install_delete INDEX-OLD INDEX-NEW || return 1
2845
2846	# Remove temporary files
2847	rm INDEX-OLD INDEX-NEW
2848}
2849
2850# Rearrange bits to allow the installed updates to be rolled back
2851install_setup_rollback () {
2852	# Remove the "reboot after installing kernel", "kernel updated", and
2853	# "finished installing the world" flags if present -- they are
2854	# irrelevant when rolling back updates.
2855	if [ -f ${BDHASH}-install/kernelfirst ]; then
2856		rm ${BDHASH}-install/kernelfirst
2857		rm ${BDHASH}-install/kerneldone
2858	fi
2859	if [ -f ${BDHASH}-install/worlddone ]; then
2860		rm ${BDHASH}-install/worlddone
2861	fi
2862
2863	if [ -L ${BDHASH}-rollback ]; then
2864		mv ${BDHASH}-rollback ${BDHASH}-install/rollback
2865	fi
2866
2867	mv ${BDHASH}-install ${BDHASH}-rollback
2868}
2869
2870# Actually install updates
2871install_run () {
2872	echo -n "Installing updates..."
2873
2874	# Make sure we have all the files we should have
2875	install_verify ${BDHASH}-install/INDEX-OLD	\
2876	    ${BDHASH}-install/INDEX-NEW || return 1
2877
2878	# Remove system immutable flag from files
2879	install_unschg ${BDHASH}-install/INDEX-OLD	\
2880	    ${BDHASH}-install/INDEX-NEW || return 1
2881
2882	# Install new files, delete old files, and update linker.hints
2883	install_files ${BDHASH}-install || return 1
2884
2885	# Rearrange bits to allow the installed updates to be rolled back
2886	install_setup_rollback
2887
2888	echo " done."
2889}
2890
2891# Rearrange bits to allow the previous set of updates to be rolled back next.
2892rollback_setup_rollback () {
2893	if [ -L ${BDHASH}-rollback/rollback ]; then
2894		mv ${BDHASH}-rollback/rollback rollback-tmp
2895		rm -r ${BDHASH}-rollback/
2896		rm ${BDHASH}-rollback
2897		mv rollback-tmp ${BDHASH}-rollback
2898	else
2899		rm -r ${BDHASH}-rollback/
2900		rm ${BDHASH}-rollback
2901	fi
2902}
2903
2904# Install old files, delete new files, and update linker.hints
2905rollback_files () {
2906	# Install old shared library files which don't have the same path as
2907	# a new shared library file.
2908	grep -vE '^/boot/' $1/INDEX-NEW |
2909	    grep -E '/lib/.*\.so\.[0-9]+\|' |
2910	    cut -f 1 -d '|' |
2911	    sort > INDEX-NEW.libs.flist
2912	grep -vE '^/boot/' $1/INDEX-OLD |
2913	    grep -E '/lib/.*\.so\.[0-9]+\|' |
2914	    sort -k 1,1 -t '|' - |
2915	    join -t '|' -v 1 - INDEX-NEW.libs.flist > INDEX-OLD
2916	install_from_index INDEX-OLD || return 1
2917
2918	# Deal with files which are neither kernel nor shared library
2919	grep -vE '^/boot/' $1/INDEX-OLD |
2920	    grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2921	grep -vE '^/boot/' $1/INDEX-NEW |
2922	    grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2923	install_from_index INDEX-OLD || return 1
2924	install_delete INDEX-NEW INDEX-OLD || return 1
2925
2926	# Install any old shared library files which we didn't install above.
2927	grep -vE '^/boot/' $1/INDEX-OLD |
2928	    grep -E '/lib/.*\.so\.[0-9]+\|' |
2929	    sort -k 1,1 -t '|' - |
2930	    join -t '|' - INDEX-NEW.libs.flist > INDEX-OLD
2931	install_from_index INDEX-OLD || return 1
2932
2933	# Delete unneeded shared library files
2934	grep -vE '^/boot/' $1/INDEX-OLD |
2935	    grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2936	grep -vE '^/boot/' $1/INDEX-NEW |
2937	    grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2938	install_delete INDEX-NEW INDEX-OLD || return 1
2939
2940	# Deal with kernel files
2941	grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD
2942	grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW
2943	install_from_index INDEX-OLD || return 1
2944	install_delete INDEX-NEW INDEX-OLD || return 1
2945	if [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2946		kldxref -R /boot/ 2>/dev/null
2947	fi
2948
2949	# Remove temporary files
2950	rm INDEX-OLD INDEX-NEW INDEX-NEW.libs.flist
2951}
2952
2953# Actually rollback updates
2954rollback_run () {
2955	echo -n "Uninstalling updates..."
2956
2957	# If there are updates waiting to be installed, remove them; we
2958	# want the user to re-run 'fetch' after rolling back updates.
2959	if [ -L ${BDHASH}-install ]; then
2960		rm -r ${BDHASH}-install/
2961		rm ${BDHASH}-install
2962	fi
2963
2964	# Make sure we have all the files we should have
2965	install_verify ${BDHASH}-rollback/INDEX-NEW	\
2966	    ${BDHASH}-rollback/INDEX-OLD || return 1
2967
2968	# Remove system immutable flag from files
2969	install_unschg ${BDHASH}-rollback/INDEX-NEW	\
2970	    ${BDHASH}-rollback/INDEX-OLD || return 1
2971
2972	# Install old files, delete new files, and update linker.hints
2973	rollback_files ${BDHASH}-rollback || return 1
2974
2975	# Remove the rollback directory and the symlink pointing to it; and
2976	# rearrange bits to allow the previous set of updates to be rolled
2977	# back next.
2978	rollback_setup_rollback
2979
2980	echo " done."
2981}
2982
2983# Compare INDEX-ALL and INDEX-PRESENT and print warnings about differences.
2984IDS_compare () {
2985	# Get all the lines which mismatch in something other than file
2986	# flags.  We ignore file flags because sysinstall doesn't seem to
2987	# set them when it installs FreeBSD; warning about these adds a
2988	# very large amount of noise.
2989	cut -f 1-5,7-8 -d '|' $1 > $1.noflags
2990	sort -k 1,1 -t '|' $1.noflags > $1.sorted
2991	cut -f 1-5,7-8 -d '|' $2 |
2992	    comm -13 $1.noflags - |
2993	    fgrep -v '|-|||||' |
2994	    sort -k 1,1 -t '|' |
2995	    join -t '|' $1.sorted - > INDEX-NOTMATCHING
2996
2997	# Ignore files which match IDSIGNOREPATHS.
2998	for X in ${IDSIGNOREPATHS}; do
2999		grep -E "^${X}" INDEX-NOTMATCHING
3000	done |
3001	    sort -u |
3002	    comm -13 - INDEX-NOTMATCHING > INDEX-NOTMATCHING.tmp
3003	mv INDEX-NOTMATCHING.tmp INDEX-NOTMATCHING
3004
3005	# Go through the lines and print warnings.
3006	while read LINE; do
3007		FPATH=`echo "${LINE}" | cut -f 1 -d '|'`
3008		TYPE=`echo "${LINE}" | cut -f 2 -d '|'`
3009		OWNER=`echo "${LINE}" | cut -f 3 -d '|'`
3010		GROUP=`echo "${LINE}" | cut -f 4 -d '|'`
3011		PERM=`echo "${LINE}" | cut -f 5 -d '|'`
3012		HASH=`echo "${LINE}" | cut -f 6 -d '|'`
3013		LINK=`echo "${LINE}" | cut -f 7 -d '|'`
3014		P_TYPE=`echo "${LINE}" | cut -f 8 -d '|'`
3015		P_OWNER=`echo "${LINE}" | cut -f 9 -d '|'`
3016		P_GROUP=`echo "${LINE}" | cut -f 10 -d '|'`
3017		P_PERM=`echo "${LINE}" | cut -f 11 -d '|'`
3018		P_HASH=`echo "${LINE}" | cut -f 12 -d '|'`
3019		P_LINK=`echo "${LINE}" | cut -f 13 -d '|'`
3020
3021		# Warn about different object types.
3022		if ! [ "${TYPE}" = "${P_TYPE}" ]; then
3023			echo -n "${FPATH} is a "
3024			case "${P_TYPE}" in
3025			f)	echo -n "regular file, "
3026				;;
3027			d)	echo -n "directory, "
3028				;;
3029			L)	echo -n "symlink, "
3030				;;
3031			esac
3032			echo -n "but should be a "
3033			case "${TYPE}" in
3034			f)	echo -n "regular file."
3035				;;
3036			d)	echo -n "directory."
3037				;;
3038			L)	echo -n "symlink."
3039				;;
3040			esac
3041			echo
3042
3043			# Skip other tests, since they don't make sense if
3044			# we're comparing different object types.
3045			continue
3046		fi
3047
3048		# Warn about different owners.
3049		if ! [ "${OWNER}" = "${P_OWNER}" ]; then
3050			echo -n "${FPATH} is owned by user id ${P_OWNER}, "
3051			echo "but should be owned by user id ${OWNER}."
3052		fi
3053
3054		# Warn about different groups.
3055		if ! [ "${GROUP}" = "${P_GROUP}" ]; then
3056			echo -n "${FPATH} is owned by group id ${P_GROUP}, "
3057			echo "but should be owned by group id ${GROUP}."
3058		fi
3059
3060		# Warn about different permissions.  We do not warn about
3061		# different permissions on symlinks, since some archivers
3062		# don't extract symlink permissions correctly and they are
3063		# ignored anyway.
3064		if ! [ "${PERM}" = "${P_PERM}" ] &&
3065		    ! [ "${TYPE}" = "L" ]; then
3066			echo -n "${FPATH} has ${P_PERM} permissions, "
3067			echo "but should have ${PERM} permissions."
3068		fi
3069
3070		# Warn about different file hashes / symlink destinations.
3071		if ! [ "${HASH}" = "${P_HASH}" ]; then
3072			if [ "${TYPE}" = "L" ]; then
3073				echo -n "${FPATH} is a symlink to ${P_HASH}, "
3074				echo "but should be a symlink to ${HASH}."
3075			fi
3076			if [ "${TYPE}" = "f" ]; then
3077				echo -n "${FPATH} has SHA256 hash ${P_HASH}, "
3078				echo "but should have SHA256 hash ${HASH}."
3079			fi
3080		fi
3081
3082		# We don't warn about different hard links, since some
3083		# some archivers break hard links, and as long as the
3084		# underlying data is correct they really don't matter.
3085	done < INDEX-NOTMATCHING
3086
3087	# Clean up
3088	rm $1 $1.noflags $1.sorted $2 INDEX-NOTMATCHING
3089}
3090
3091# Do the work involved in comparing the system to a "known good" index
3092IDS_run () {
3093	workdir_init || return 1
3094
3095	# Prepare the mirror list.
3096	fetch_pick_server_init && fetch_pick_server
3097
3098	# Try to fetch the public key until we run out of servers.
3099	while ! fetch_key; do
3100		fetch_pick_server || return 1
3101	done
3102 
3103	# Try to fetch the metadata index signature ("tag") until we run
3104	# out of available servers; and sanity check the downloaded tag.
3105	while ! fetch_tag; do
3106		fetch_pick_server || return 1
3107	done
3108	fetch_tagsanity || return 1
3109
3110	# Fetch INDEX-OLD and INDEX-ALL.
3111	fetch_metadata INDEX-OLD INDEX-ALL || return 1
3112
3113	# Generate filtered INDEX-OLD and INDEX-ALL files containing only
3114	# the components we want and without anything marked as "Ignore".
3115	fetch_filter_metadata INDEX-OLD || return 1
3116	fetch_filter_metadata INDEX-ALL || return 1
3117
3118	# Merge the INDEX-OLD and INDEX-ALL files into INDEX-ALL.
3119	sort INDEX-OLD INDEX-ALL > INDEX-ALL.tmp
3120	mv INDEX-ALL.tmp INDEX-ALL
3121	rm INDEX-OLD
3122
3123	# Translate /boot/${KERNCONF} to ${KERNELDIR}
3124	fetch_filter_kernel_names INDEX-ALL ${KERNCONF}
3125
3126	# Inspect the system and generate an INDEX-PRESENT file.
3127	fetch_inspect_system INDEX-ALL INDEX-PRESENT /dev/null || return 1
3128
3129	# Compare INDEX-ALL and INDEX-PRESENT and print warnings about any
3130	# differences.
3131	IDS_compare INDEX-ALL INDEX-PRESENT
3132}
3133
3134#### Main functions -- call parameter-handling and core functions
3135
3136# Using the command line, configuration file, and defaults,
3137# set all the parameters which are needed later.
3138get_params () {
3139	init_params
3140	parse_cmdline $@
3141	parse_conffile
3142	default_params
3143}
3144
3145# Fetch command.  Make sure that we're being called
3146# interactively, then run fetch_check_params and fetch_run
3147cmd_fetch () {
3148	if [ ! -t 0 ]; then
3149		echo -n "`basename $0` fetch should not "
3150		echo "be run non-interactively."
3151		echo "Run `basename $0` cron instead."
3152		exit 1
3153	fi
3154	fetch_check_params
3155	fetch_run || exit 1
3156}
3157
3158# Cron command.  Make sure the parameters are sensible; wait
3159# rand(3600) seconds; then fetch updates.  While fetching updates,
3160# send output to a temporary file; only print that file if the
3161# fetching failed.
3162cmd_cron () {
3163	fetch_check_params
3164	sleep `jot -r 1 0 3600`
3165
3166	TMPFILE=`mktemp /tmp/freebsd-update.XXXXXX` || exit 1
3167	if ! fetch_run >> ${TMPFILE} ||
3168	    ! grep -q "No updates needed" ${TMPFILE} ||
3169	    [ ${VERBOSELEVEL} = "debug" ]; then
3170		mail -s "`hostname` security updates" ${MAILTO} < ${TMPFILE}
3171	fi
3172
3173	rm ${TMPFILE}
3174}
3175
3176# Fetch files for upgrading to a new release.
3177cmd_upgrade () {
3178	upgrade_check_params
3179	upgrade_run || exit 1
3180}
3181
3182# Install downloaded updates.
3183cmd_install () {
3184	install_check_params
3185	install_run || exit 1
3186}
3187
3188# Rollback most recently installed updates.
3189cmd_rollback () {
3190	rollback_check_params
3191	rollback_run || exit 1
3192}
3193
3194# Compare system against a "known good" index.
3195cmd_IDS () {
3196	IDS_check_params
3197	IDS_run || exit 1
3198}
3199
3200#### Entry point
3201
3202# Make sure we find utilities from the base system
3203export PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH}
3204
3205# Set a pager if the user doesn't
3206if [ -z "$PAGER" ]; then
3207	PAGER=/usr/bin/more
3208fi
3209
3210# Set LC_ALL in order to avoid problems with character ranges like [A-Z].
3211export LC_ALL=C
3212
3213get_params $@
3214for COMMAND in ${COMMANDS}; do
3215	cmd_${COMMAND}
3216done
3217