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