freebsd-update.sh revision 276157
1161748Scperciva#!/bin/sh
2161748Scperciva
3161748Scperciva#-
4173441Scperciva# Copyright 2004-2007 Colin Percival
5161748Scperciva# All rights reserved
6161748Scperciva#
7161748Scperciva# Redistribution and use in source and binary forms, with or without
8161748Scperciva# modification, are permitted providing that the following conditions 
9161748Scperciva# are met:
10161748Scperciva# 1. Redistributions of source code must retain the above copyright
11161748Scperciva#    notice, this list of conditions and the following disclaimer.
12161748Scperciva# 2. Redistributions in binary form must reproduce the above copyright
13161748Scperciva#    notice, this list of conditions and the following disclaimer in the
14161748Scperciva#    documentation and/or other materials provided with the distribution.
15161748Scperciva#
16161748Scperciva# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17161748Scperciva# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18161748Scperciva# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19161748Scperciva# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20161748Scperciva# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21161748Scperciva# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22161748Scperciva# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23161748Scperciva# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24161748Scperciva# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25161748Scperciva# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26161748Scperciva# POSSIBILITY OF SUCH DAMAGE.
27161748Scperciva
28161748Scperciva# $FreeBSD: releng/9.3/usr.sbin/freebsd-update/freebsd-update.sh 276157 2014-12-23 22:54:25Z des $
29161748Scperciva
30161748Scperciva#### Usage function -- called from command-line handling code.
31161748Scperciva
32161748Scperciva# Usage instructions.  Options not listed:
33161748Scperciva# --debug	-- don't filter output from utilities
34161748Scperciva# --no-stats	-- don't show progress statistics while fetching files
35161748Scpercivausage () {
36161748Scperciva	cat <<EOF
37161748Scpercivausage: `basename $0` [options] command ... [path]
38161748Scperciva
39161748ScpercivaOptions:
40161748Scperciva  -b basedir   -- Operate on a system mounted at basedir
41161748Scperciva                  (default: /)
42161748Scperciva  -d workdir   -- Store working files in workdir
43161748Scperciva                  (default: /var/db/freebsd-update/)
44161748Scperciva  -f conffile  -- Read configuration options from conffile
45161748Scperciva                  (default: /etc/freebsd-update.conf)
46161748Scperciva  -k KEY       -- Trust an RSA key with SHA256 hash of KEY
47173564Scperciva  -r release   -- Target for upgrade (e.g., 6.2-RELEASE)
48161748Scperciva  -s server    -- Server from which to fetch updates
49161748Scperciva                  (default: update.FreeBSD.org)
50161748Scperciva  -t address   -- Mail output of cron command, if any, to address
51161748Scperciva                  (default: root)
52161748ScpercivaCommands:
53161748Scperciva  fetch        -- Fetch updates from server
54161748Scperciva  cron         -- Sleep rand(3600) seconds, fetch updates, and send an
55161748Scperciva                  email if updates were found
56173564Scperciva  upgrade      -- Fetch upgrades to FreeBSD version specified via -r option
57173564Scperciva  install      -- Install downloaded updates or upgrades
58161748Scperciva  rollback     -- Uninstall most recently installed updates
59181142Scperciva  IDS          -- Compare the system against an index of "known good" files.
60161748ScpercivaEOF
61161748Scperciva	exit 0
62161748Scperciva}
63161748Scperciva
64161748Scperciva#### Configuration processing functions
65161748Scperciva
66161748Scperciva#-
67161748Scperciva# Configuration options are set in the following order of priority:
68161748Scperciva# 1. Command line options
69161748Scperciva# 2. Configuration file options
70161748Scperciva# 3. Default options
71161748Scperciva# In addition, certain options (e.g., IgnorePaths) can be specified multiple
72161748Scperciva# times and (as long as these are all in the same place, e.g., inside the
73161748Scperciva# configuration file) they will accumulate.  Finally, because the path to the
74161748Scperciva# configuration file can be specified at the command line, the entire command
75161748Scperciva# line must be processed before we start reading the configuration file.
76161748Scperciva#
77161748Scperciva# Sound like a mess?  It is.  Here's how we handle this:
78161748Scperciva# 1. Initialize CONFFILE and all the options to "".
79161748Scperciva# 2. Process the command line.  Throw an error if a non-accumulating option
80161748Scperciva#    is specified twice.
81161748Scperciva# 3. If CONFFILE is "", set CONFFILE to /etc/freebsd-update.conf .
82161748Scperciva# 4. For all the configuration options X, set X_saved to X.
83161748Scperciva# 5. Initialize all the options to "".
84161748Scperciva# 6. Read CONFFILE line by line, parsing options.
85161748Scperciva# 7. For each configuration option X, set X to X_saved iff X_saved is not "".
86161748Scperciva# 8. Repeat steps 4-7, except setting options to their default values at (6).
87161748Scperciva
88161748ScpercivaCONFIGOPTIONS="KEYPRINT WORKDIR SERVERNAME MAILTO ALLOWADD ALLOWDELETE
89161748Scperciva    KEEPMODIFIEDMETADATA COMPONENTS IGNOREPATHS UPDATEIFUNMODIFIED
90181142Scperciva    BASEDIR VERBOSELEVEL TARGETRELEASE STRICTCOMPONENTS MERGECHANGES
91196392Ssimon    IDSIGNOREPATHS BACKUPKERNEL BACKUPKERNELDIR BACKUPKERNELSYMBOLFILES"
92161748Scperciva
93161748Scperciva# Set all the configuration options to "".
94161748Scpercivanullconfig () {
95161748Scperciva	for X in ${CONFIGOPTIONS}; do
96161748Scperciva		eval ${X}=""
97161748Scperciva	done
98161748Scperciva}
99161748Scperciva
100161748Scperciva# For each configuration option X, set X_saved to X.
101161748Scpercivasaveconfig () {
102161748Scperciva	for X in ${CONFIGOPTIONS}; do
103161748Scperciva		eval ${X}_saved=\$${X}
104161748Scperciva	done
105161748Scperciva}
106161748Scperciva
107161748Scperciva# For each configuration option X, set X to X_saved if X_saved is not "".
108161748Scpercivamergeconfig () {
109161748Scperciva	for X in ${CONFIGOPTIONS}; do
110161748Scperciva		eval _=\$${X}_saved
111161748Scperciva		if ! [ -z "${_}" ]; then
112161748Scperciva			eval ${X}=\$${X}_saved
113161748Scperciva		fi
114161748Scperciva	done
115161748Scperciva}
116161748Scperciva
117161748Scperciva# Set the trusted keyprint.
118161748Scpercivaconfig_KeyPrint () {
119161748Scperciva	if [ -z ${KEYPRINT} ]; then
120161748Scperciva		KEYPRINT=$1
121161748Scperciva	else
122161748Scperciva		return 1
123161748Scperciva	fi
124161748Scperciva}
125161748Scperciva
126161748Scperciva# Set the working directory.
127161748Scpercivaconfig_WorkDir () {
128161748Scperciva	if [ -z ${WORKDIR} ]; then
129161748Scperciva		WORKDIR=$1
130161748Scperciva	else
131161748Scperciva		return 1
132161748Scperciva	fi
133161748Scperciva}
134161748Scperciva
135161748Scperciva# Set the name of the server (pool) from which to fetch updates
136161748Scpercivaconfig_ServerName () {
137161748Scperciva	if [ -z ${SERVERNAME} ]; then
138161748Scperciva		SERVERNAME=$1
139161748Scperciva	else
140161748Scperciva		return 1
141161748Scperciva	fi
142161748Scperciva}
143161748Scperciva
144161748Scperciva# Set the address to which 'cron' output will be mailed.
145161748Scpercivaconfig_MailTo () {
146161748Scperciva	if [ -z ${MAILTO} ]; then
147161748Scperciva		MAILTO=$1
148161748Scperciva	else
149161748Scperciva		return 1
150161748Scperciva	fi
151161748Scperciva}
152161748Scperciva
153161748Scperciva# Set whether FreeBSD Update is allowed to add files (or directories, or
154161748Scperciva# symlinks) which did not previously exist.
155161748Scpercivaconfig_AllowAdd () {
156161748Scperciva	if [ -z ${ALLOWADD} ]; then
157161748Scperciva		case $1 in
158161748Scperciva		[Yy][Ee][Ss])
159161748Scperciva			ALLOWADD=yes
160161748Scperciva			;;
161161748Scperciva		[Nn][Oo])
162161748Scperciva			ALLOWADD=no
163161748Scperciva			;;
164161748Scperciva		*)
165161748Scperciva			return 1
166161748Scperciva			;;
167161748Scperciva		esac
168161748Scperciva	else
169161748Scperciva		return 1
170161748Scperciva	fi
171161748Scperciva}
172161748Scperciva
173161748Scperciva# Set whether FreeBSD Update is allowed to remove files/directories/symlinks.
174161748Scpercivaconfig_AllowDelete () {
175161748Scperciva	if [ -z ${ALLOWDELETE} ]; then
176161748Scperciva		case $1 in
177161748Scperciva		[Yy][Ee][Ss])
178161748Scperciva			ALLOWDELETE=yes
179161748Scperciva			;;
180161748Scperciva		[Nn][Oo])
181161748Scperciva			ALLOWDELETE=no
182161748Scperciva			;;
183161748Scperciva		*)
184161748Scperciva			return 1
185161748Scperciva			;;
186161748Scperciva		esac
187161748Scperciva	else
188161748Scperciva		return 1
189161748Scperciva	fi
190161748Scperciva}
191161748Scperciva
192161748Scperciva# Set whether FreeBSD Update should keep existing inode ownership,
193161748Scperciva# permissions, and flags, in the event that they have been modified locally
194161748Scperciva# after the release.
195161748Scpercivaconfig_KeepModifiedMetadata () {
196161748Scperciva	if [ -z ${KEEPMODIFIEDMETADATA} ]; then
197161748Scperciva		case $1 in
198161748Scperciva		[Yy][Ee][Ss])
199161748Scperciva			KEEPMODIFIEDMETADATA=yes
200161748Scperciva			;;
201161748Scperciva		[Nn][Oo])
202161748Scperciva			KEEPMODIFIEDMETADATA=no
203161748Scperciva			;;
204161748Scperciva		*)
205161748Scperciva			return 1
206161748Scperciva			;;
207161748Scperciva		esac
208161748Scperciva	else
209161748Scperciva		return 1
210161748Scperciva	fi
211161748Scperciva}
212161748Scperciva
213161748Scperciva# Add to the list of components which should be kept updated.
214161748Scpercivaconfig_Components () {
215161748Scperciva	for C in $@; do
216161748Scperciva		COMPONENTS="${COMPONENTS} ${C}"
217161748Scperciva	done
218161748Scperciva}
219161748Scperciva
220161748Scperciva# Add to the list of paths under which updates will be ignored.
221161748Scpercivaconfig_IgnorePaths () {
222161748Scperciva	for C in $@; do
223161748Scperciva		IGNOREPATHS="${IGNOREPATHS} ${C}"
224161748Scperciva	done
225161748Scperciva}
226161748Scperciva
227181142Scperciva# Add to the list of paths which IDS should ignore.
228181142Scpercivaconfig_IDSIgnorePaths () {
229181142Scperciva	for C in $@; do
230181142Scperciva		IDSIGNOREPATHS="${IDSIGNOREPATHS} ${C}"
231181142Scperciva	done
232181142Scperciva}
233181142Scperciva
234161748Scperciva# Add to the list of paths within which updates will be performed only if the
235161748Scperciva# file on disk has not been modified locally.
236161748Scpercivaconfig_UpdateIfUnmodified () {
237161748Scperciva	for C in $@; do
238161748Scperciva		UPDATEIFUNMODIFIED="${UPDATEIFUNMODIFIED} ${C}"
239161748Scperciva	done
240161748Scperciva}
241161748Scperciva
242173564Scperciva# Add to the list of paths within which updates to text files will be merged
243173564Scperciva# instead of overwritten.
244173564Scpercivaconfig_MergeChanges () {
245173564Scperciva	for C in $@; do
246173564Scperciva		MERGECHANGES="${MERGECHANGES} ${C}"
247173564Scperciva	done
248173564Scperciva}
249173564Scperciva
250161748Scperciva# Work on a FreeBSD installation mounted under $1
251161748Scpercivaconfig_BaseDir () {
252161748Scperciva	if [ -z ${BASEDIR} ]; then
253161748Scperciva		BASEDIR=$1
254161748Scperciva	else
255161748Scperciva		return 1
256161748Scperciva	fi
257161748Scperciva}
258161748Scperciva
259173564Scperciva# When fetching upgrades, should we assume the user wants exactly the
260173564Scperciva# components listed in COMPONENTS, rather than trying to guess based on
261173564Scperciva# what's currently installed?
262173564Scpercivaconfig_StrictComponents () {
263173564Scperciva	if [ -z ${STRICTCOMPONENTS} ]; then
264173564Scperciva		case $1 in
265173564Scperciva		[Yy][Ee][Ss])
266173564Scperciva			STRICTCOMPONENTS=yes
267173564Scperciva			;;
268173564Scperciva		[Nn][Oo])
269173564Scperciva			STRICTCOMPONENTS=no
270173564Scperciva			;;
271173564Scperciva		*)
272173564Scperciva			return 1
273173564Scperciva			;;
274173564Scperciva		esac
275173564Scperciva	else
276173564Scperciva		return 1
277173564Scperciva	fi
278173564Scperciva}
279173564Scperciva
280173564Scperciva# Upgrade to FreeBSD $1
281173564Scpercivaconfig_TargetRelease () {
282173564Scperciva	if [ -z ${TARGETRELEASE} ]; then
283173564Scperciva		TARGETRELEASE=$1
284173564Scperciva	else
285173564Scperciva		return 1
286173564Scperciva	fi
287197618Scperciva	if echo ${TARGETRELEASE} | grep -qE '^[0-9.]+$'; then
288197618Scperciva		TARGETRELEASE="${TARGETRELEASE}-RELEASE"
289197618Scperciva	fi
290173564Scperciva}
291173564Scperciva
292161748Scperciva# Define what happens to output of utilities
293161748Scpercivaconfig_VerboseLevel () {
294161748Scperciva	if [ -z ${VERBOSELEVEL} ]; then
295161748Scperciva		case $1 in
296161748Scperciva		[Dd][Ee][Bb][Uu][Gg])
297161748Scperciva			VERBOSELEVEL=debug
298161748Scperciva			;;
299161748Scperciva		[Nn][Oo][Ss][Tt][Aa][Tt][Ss])
300161748Scperciva			VERBOSELEVEL=nostats
301161748Scperciva			;;
302161748Scperciva		[Ss][Tt][Aa][Tt][Ss])
303161748Scperciva			VERBOSELEVEL=stats
304161748Scperciva			;;
305161748Scperciva		*)
306161748Scperciva			return 1
307161748Scperciva			;;
308161748Scperciva		esac
309161748Scperciva	else
310161748Scperciva		return 1
311161748Scperciva	fi
312161748Scperciva}
313161748Scperciva
314196392Ssimonconfig_BackupKernel () {
315196392Ssimon	if [ -z ${BACKUPKERNEL} ]; then
316196392Ssimon		case $1 in
317196392Ssimon		[Yy][Ee][Ss])
318196392Ssimon			BACKUPKERNEL=yes
319196392Ssimon			;;
320196392Ssimon		[Nn][Oo])
321196392Ssimon			BACKUPKERNEL=no
322196392Ssimon			;;
323196392Ssimon		*)
324196392Ssimon			return 1
325196392Ssimon			;;
326196392Ssimon		esac
327196392Ssimon	else
328196392Ssimon		return 1
329196392Ssimon	fi
330196392Ssimon}
331196392Ssimon
332196392Ssimonconfig_BackupKernelDir () {
333196392Ssimon	if [ -z ${BACKUPKERNELDIR} ]; then
334196392Ssimon		if [ -z "$1" ]; then
335196392Ssimon			echo "BackupKernelDir set to empty dir"
336196392Ssimon			return 1
337196392Ssimon		fi
338196392Ssimon
339196392Ssimon		# We check for some paths which would be extremely odd
340196392Ssimon		# to use, but which could cause a lot of problems if
341196392Ssimon		# used.
342196392Ssimon		case $1 in
343196392Ssimon		/|/bin|/boot|/etc|/lib|/libexec|/sbin|/usr|/var)
344196392Ssimon			echo "BackupKernelDir set to invalid path $1"
345196392Ssimon			return 1
346196392Ssimon			;;
347196392Ssimon		/*)
348196392Ssimon			BACKUPKERNELDIR=$1
349196392Ssimon			;;
350196392Ssimon		*)
351196392Ssimon			echo "BackupKernelDir ($1) is not an absolute path"
352196392Ssimon			return 1
353196392Ssimon			;;
354196392Ssimon		esac
355196392Ssimon	else
356196392Ssimon		return 1
357196392Ssimon	fi
358196392Ssimon}
359196392Ssimon
360196392Ssimonconfig_BackupKernelSymbolFiles () {
361196392Ssimon	if [ -z ${BACKUPKERNELSYMBOLFILES} ]; then
362196392Ssimon		case $1 in
363196392Ssimon		[Yy][Ee][Ss])
364196392Ssimon			BACKUPKERNELSYMBOLFILES=yes
365196392Ssimon			;;
366196392Ssimon		[Nn][Oo])
367196392Ssimon			BACKUPKERNELSYMBOLFILES=no
368196392Ssimon			;;
369196392Ssimon		*)
370196392Ssimon			return 1
371196392Ssimon			;;
372196392Ssimon		esac
373196392Ssimon	else
374196392Ssimon		return 1
375196392Ssimon	fi
376196392Ssimon}
377196392Ssimon
378161748Scperciva# Handle one line of configuration
379161748Scpercivaconfigline () {
380161748Scperciva	if [ $# -eq 0 ]; then
381161748Scperciva		return
382161748Scperciva	fi
383161748Scperciva
384161748Scperciva	OPT=$1
385161748Scperciva	shift
386161748Scperciva	config_${OPT} $@
387161748Scperciva}
388161748Scperciva
389161748Scperciva#### Parameter handling functions.
390161748Scperciva
391161748Scperciva# Initialize parameters to null, just in case they're
392161748Scperciva# set in the environment.
393161748Scpercivainit_params () {
394161748Scperciva	# Configration settings
395161748Scperciva	nullconfig
396161748Scperciva
397161748Scperciva	# No configuration file set yet
398161748Scperciva	CONFFILE=""
399161748Scperciva
400161748Scperciva	# No commands specified yet
401161748Scperciva	COMMANDS=""
402161748Scperciva}
403161748Scperciva
404161748Scperciva# Parse the command line
405161748Scpercivaparse_cmdline () {
406161748Scperciva	while [ $# -gt 0 ]; do
407161748Scperciva		case "$1" in
408161748Scperciva		# Location of configuration file
409161748Scperciva		-f)
410161748Scperciva			if [ $# -eq 1 ]; then usage; fi
411161748Scperciva			if [ ! -z "${CONFFILE}" ]; then usage; fi
412161748Scperciva			shift; CONFFILE="$1"
413161748Scperciva			;;
414161748Scperciva
415161748Scperciva		# Configuration file equivalents
416161748Scperciva		-b)
417161748Scperciva			if [ $# -eq 1 ]; then usage; fi; shift
418161748Scperciva			config_BaseDir $1 || usage
419161748Scperciva			;;
420161748Scperciva		-d)
421161748Scperciva			if [ $# -eq 1 ]; then usage; fi; shift
422161748Scperciva			config_WorkDir $1 || usage
423161748Scperciva			;;
424161748Scperciva		-k)
425161748Scperciva			if [ $# -eq 1 ]; then usage; fi; shift
426161748Scperciva			config_KeyPrint $1 || usage
427161748Scperciva			;;
428161748Scperciva		-s)
429161748Scperciva			if [ $# -eq 1 ]; then usage; fi; shift
430161748Scperciva			config_ServerName $1 || usage
431161748Scperciva			;;
432173564Scperciva		-r)
433173564Scperciva			if [ $# -eq 1 ]; then usage; fi; shift
434173564Scperciva			config_TargetRelease $1 || usage
435173564Scperciva			;;
436161748Scperciva		-t)
437161748Scperciva			if [ $# -eq 1 ]; then usage; fi; shift
438161748Scperciva			config_MailTo $1 || usage
439161748Scperciva			;;
440161748Scperciva		-v)
441161748Scperciva			if [ $# -eq 1 ]; then usage; fi; shift
442161748Scperciva			config_VerboseLevel $1 || usage
443161748Scperciva			;;
444161748Scperciva
445161748Scperciva		# Aliases for "-v debug" and "-v nostats"
446161748Scperciva		--debug)
447161748Scperciva			config_VerboseLevel debug || usage
448161748Scperciva			;;
449161748Scperciva		--no-stats)
450161748Scperciva			config_VerboseLevel nostats || usage
451161748Scperciva			;;
452161748Scperciva
453161748Scperciva		# Commands
454181142Scperciva		cron | fetch | upgrade | install | rollback | IDS)
455161748Scperciva			COMMANDS="${COMMANDS} $1"
456161748Scperciva			;;
457161748Scperciva
458161748Scperciva		# Anything else is an error
459161748Scperciva		*)
460161748Scperciva			usage
461161748Scperciva			;;
462161748Scperciva		esac
463161748Scperciva		shift
464161748Scperciva	done
465161748Scperciva
466161748Scperciva	# Make sure we have at least one command
467161748Scperciva	if [ -z "${COMMANDS}" ]; then
468161748Scperciva		usage
469161748Scperciva	fi
470161748Scperciva}
471161748Scperciva
472161748Scperciva# Parse the configuration file
473161748Scpercivaparse_conffile () {
474161748Scperciva	# If a configuration file was specified on the command line, check
475161748Scperciva	# that it exists and is readable.
476161748Scperciva	if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then
477161748Scperciva		echo -n "File does not exist "
478161748Scperciva		echo -n "or is not readable: "
479161748Scperciva		echo ${CONFFILE}
480161748Scperciva		exit 1
481161748Scperciva	fi
482161748Scperciva
483161748Scperciva	# If a configuration file was not specified on the command line,
484161748Scperciva	# use the default configuration file path.  If that default does
485161748Scperciva	# not exist, give up looking for any configuration.
486161748Scperciva	if [ -z "${CONFFILE}" ]; then
487161748Scperciva		CONFFILE="/etc/freebsd-update.conf"
488161748Scperciva		if [ ! -r "${CONFFILE}" ]; then
489161748Scperciva			return
490161748Scperciva		fi
491161748Scperciva	fi
492161748Scperciva
493161748Scperciva	# Save the configuration options specified on the command line, and
494161748Scperciva	# clear all the options in preparation for reading the config file.
495161748Scperciva	saveconfig
496161748Scperciva	nullconfig
497161748Scperciva
498161748Scperciva	# Read the configuration file.  Anything after the first '#' is
499161748Scperciva	# ignored, and any blank lines are ignored.
500161748Scperciva	L=0
501161748Scperciva	while read LINE; do
502161748Scperciva		L=$(($L + 1))
503161748Scperciva		LINEX=`echo "${LINE}" | cut -f 1 -d '#'`
504161748Scperciva		if ! configline ${LINEX}; then
505161748Scperciva			echo "Error processing configuration file, line $L:"
506161748Scperciva			echo "==> ${LINE}"
507161748Scperciva			exit 1
508161748Scperciva		fi
509161748Scperciva	done < ${CONFFILE}
510161748Scperciva
511161748Scperciva	# Merge the settings read from the configuration file with those
512161748Scperciva	# provided at the command line.
513161748Scperciva	mergeconfig
514161748Scperciva}
515161748Scperciva
516161748Scperciva# Provide some default parameters
517161748Scpercivadefault_params () {
518161748Scperciva	# Save any parameters already configured, and clear the slate
519161748Scperciva	saveconfig
520161748Scperciva	nullconfig
521161748Scperciva
522161748Scperciva	# Default configurations
523161748Scperciva	config_WorkDir /var/db/freebsd-update
524161748Scperciva	config_MailTo root
525161748Scperciva	config_AllowAdd yes
526161748Scperciva	config_AllowDelete yes
527161748Scperciva	config_KeepModifiedMetadata yes
528161748Scperciva	config_BaseDir /
529161748Scperciva	config_VerboseLevel stats
530173564Scperciva	config_StrictComponents no
531196392Ssimon	config_BackupKernel yes
532196392Ssimon	config_BackupKernelDir /boot/kernel.old
533196392Ssimon	config_BackupKernelSymbolFiles no
534161748Scperciva
535161748Scperciva	# Merge these defaults into the earlier-configured settings
536161748Scperciva	mergeconfig
537161748Scperciva}
538161748Scperciva
539161748Scperciva# Set utility output filtering options, based on ${VERBOSELEVEL}
540161748Scpercivafetch_setup_verboselevel () {
541161748Scperciva	case ${VERBOSELEVEL} in
542161748Scperciva	debug)
543161748Scperciva		QUIETREDIR="/dev/stderr"
544161748Scperciva		QUIETFLAG=" "
545161748Scperciva		STATSREDIR="/dev/stderr"
546161748Scperciva		DDSTATS=".."
547161748Scperciva		XARGST="-t"
548161748Scperciva		NDEBUG=" "
549161748Scperciva		;;
550161748Scperciva	nostats)
551161748Scperciva		QUIETREDIR=""
552161748Scperciva		QUIETFLAG=""
553161748Scperciva		STATSREDIR="/dev/null"
554161748Scperciva		DDSTATS=".."
555161748Scperciva		XARGST=""
556161748Scperciva		NDEBUG=""
557161748Scperciva		;;
558161748Scperciva	stats)
559161748Scperciva		QUIETREDIR="/dev/null"
560161748Scperciva		QUIETFLAG="-q"
561161748Scperciva		STATSREDIR="/dev/stdout"
562161748Scperciva		DDSTATS=""
563161748Scperciva		XARGST=""
564161748Scperciva		NDEBUG="-n"
565161748Scperciva		;;
566161748Scperciva	esac
567161748Scperciva}
568161748Scperciva
569161748Scperciva# Perform sanity checks and set some final parameters
570161748Scperciva# in preparation for fetching files.  Figure out which
571161748Scperciva# set of updates should be downloaded: If the user is
572161748Scperciva# running *-p[0-9]+, strip off the last part; if the
573161748Scperciva# user is running -SECURITY, call it -RELEASE.  Chdir
574161748Scperciva# into the working directory.
575212434Scpercivafetchupgrade_check_params () {
576161748Scperciva	export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
577161748Scperciva
578161748Scperciva	_SERVERNAME_z=\
579161748Scperciva"SERVERNAME must be given via command line or configuration file."
580161748Scperciva	_KEYPRINT_z="Key must be given via -k option or configuration file."
581161748Scperciva	_KEYPRINT_bad="Invalid key fingerprint: "
582161748Scperciva	_WORKDIR_bad="Directory does not exist or is not writable: "
583161748Scperciva
584161748Scperciva	if [ -z "${SERVERNAME}" ]; then
585161748Scperciva		echo -n "`basename $0`: "
586161748Scperciva		echo "${_SERVERNAME_z}"
587161748Scperciva		exit 1
588161748Scperciva	fi
589161748Scperciva	if [ -z "${KEYPRINT}" ]; then
590161748Scperciva		echo -n "`basename $0`: "
591161748Scperciva		echo "${_KEYPRINT_z}"
592161748Scperciva		exit 1
593161748Scperciva	fi
594161748Scperciva	if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
595161748Scperciva		echo -n "`basename $0`: "
596161748Scperciva		echo -n "${_KEYPRINT_bad}"
597161748Scperciva		echo ${KEYPRINT}
598161748Scperciva		exit 1
599161748Scperciva	fi
600161748Scperciva	if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
601161748Scperciva		echo -n "`basename $0`: "
602161748Scperciva		echo -n "${_WORKDIR_bad}"
603161748Scperciva		echo ${WORKDIR}
604161748Scperciva		exit 1
605161748Scperciva	fi
606200054Scperciva	chmod 700 ${WORKDIR}
607161748Scperciva	cd ${WORKDIR} || exit 1
608161748Scperciva
609161748Scperciva	# Generate release number.  The s/SECURITY/RELEASE/ bit exists
610161748Scperciva	# to provide an upgrade path for FreeBSD Update 1.x users, since
611161748Scperciva	# the kernels provided by FreeBSD Update 1.x are always labelled
612161748Scperciva	# as X.Y-SECURITY.
613161748Scperciva	RELNUM=`uname -r |
614161748Scperciva	    sed -E 's,-p[0-9]+,,' |
615161748Scperciva	    sed -E 's,-SECURITY,-RELEASE,'`
616161748Scperciva	ARCH=`uname -m`
617161748Scperciva	FETCHDIR=${RELNUM}/${ARCH}
618173564Scperciva	PATCHDIR=${RELNUM}/${ARCH}/bp
619161748Scperciva
620161748Scperciva	# Figure out what directory contains the running kernel
621161748Scperciva	BOOTFILE=`sysctl -n kern.bootfile`
622161748Scperciva	KERNELDIR=${BOOTFILE%/kernel}
623161748Scperciva	if ! [ -d ${KERNELDIR} ]; then
624161748Scperciva		echo "Cannot identify running kernel"
625161748Scperciva		exit 1
626161748Scperciva	fi
627161748Scperciva
628167189Scperciva	# Figure out what kernel configuration is running.  We start with
629167189Scperciva	# the output of `uname -i`, and then make the following adjustments:
630167189Scperciva	# 1. Replace "SMP-GENERIC" with "SMP".  Why the SMP kernel config
631167189Scperciva	# file says "ident SMP-GENERIC", I don't know...
632167189Scperciva	# 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
633167189Scperciva	# _and_ `sysctl kern.version` contains a line which ends "/SMP", then
634167189Scperciva	# we're running an SMP kernel.  This mis-identification is a bug
635167189Scperciva	# which was fixed in 6.2-STABLE.
636167189Scperciva	KERNCONF=`uname -i`
637167189Scperciva	if [ ${KERNCONF} = "SMP-GENERIC" ]; then
638167189Scperciva		KERNCONF=SMP
639167189Scperciva	fi
640167189Scperciva	if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
641167189Scperciva		if sysctl kern.version | grep -qE '/SMP$'; then
642167189Scperciva			KERNCONF=SMP
643167189Scperciva		fi
644167189Scperciva	fi
645167189Scperciva
646161748Scperciva	# Define some paths
647161748Scperciva	BSPATCH=/usr/bin/bspatch
648161748Scperciva	SHA256=/sbin/sha256
649161748Scperciva	PHTTPGET=/usr/libexec/phttpget
650161748Scperciva
651161748Scperciva	# Set up variables relating to VERBOSELEVEL
652161748Scperciva	fetch_setup_verboselevel
653161748Scperciva
654161748Scperciva	# Construct a unique name from ${BASEDIR}
655161748Scperciva	BDHASH=`echo ${BASEDIR} | sha256 -q`
656161748Scperciva}
657161748Scperciva
658212434Scperciva# Perform sanity checks etc. before fetching updates.
659212434Scpercivafetch_check_params () {
660212434Scperciva	fetchupgrade_check_params
661212434Scperciva
662212434Scperciva	if ! [ -z "${TARGETRELEASE}" ]; then
663212434Scperciva		echo -n "`basename $0`: "
664212434Scperciva		echo -n "-r option is meaningless with 'fetch' command.  "
665212434Scperciva		echo "(Did you mean 'upgrade' instead?)"
666212434Scperciva		exit 1
667212434Scperciva	fi
668212434Scperciva}
669212434Scperciva
670173564Scperciva# Perform sanity checks etc. before fetching upgrades.
671173564Scpercivaupgrade_check_params () {
672212434Scperciva	fetchupgrade_check_params
673173564Scperciva
674173564Scperciva	# Unless set otherwise, we're upgrading to the same kernel config.
675173564Scperciva	NKERNCONF=${KERNCONF}
676173564Scperciva
677173564Scperciva	# We need TARGETRELEASE set
678173564Scperciva	_TARGETRELEASE_z="Release target must be specified via -r option."
679173564Scperciva	if [ -z "${TARGETRELEASE}" ]; then
680173564Scperciva		echo -n "`basename $0`: "
681173564Scperciva		echo "${_TARGETRELEASE_z}"
682173564Scperciva		exit 1
683173564Scperciva	fi
684173564Scperciva
685173564Scperciva	# The target release should be != the current release.
686173564Scperciva	if [ "${TARGETRELEASE}" = "${RELNUM}" ]; then
687173564Scperciva		echo -n "`basename $0`: "
688173564Scperciva		echo "Cannot upgrade from ${RELNUM} to itself"
689173564Scperciva		exit 1
690173564Scperciva	fi
691173564Scperciva
692173564Scperciva	# Turning off AllowAdd or AllowDelete is a bad idea for upgrades.
693173564Scperciva	if [ "${ALLOWADD}" = "no" ]; then
694173564Scperciva		echo -n "`basename $0`: "
695173564Scperciva		echo -n "WARNING: \"AllowAdd no\" is a bad idea "
696173564Scperciva		echo "when upgrading between releases."
697173564Scperciva		echo
698173564Scperciva	fi
699173564Scperciva	if [ "${ALLOWDELETE}" = "no" ]; then
700173564Scperciva		echo -n "`basename $0`: "
701173564Scperciva		echo -n "WARNING: \"AllowDelete no\" is a bad idea "
702173564Scperciva		echo "when upgrading between releases."
703173564Scperciva		echo
704173564Scperciva	fi
705173564Scperciva
706173564Scperciva	# Set EDITOR to /usr/bin/vi if it isn't already set
707173564Scperciva	: ${EDITOR:='/usr/bin/vi'}
708173564Scperciva}
709173564Scperciva
710161748Scperciva# Perform sanity checks and set some final parameters in
711161748Scperciva# preparation for installing updates.
712161748Scpercivainstall_check_params () {
713161748Scperciva	# Check that we are root.  All sorts of things won't work otherwise.
714161748Scperciva	if [ `id -u` != 0 ]; then
715161748Scperciva		echo "You must be root to run this."
716161748Scperciva		exit 1
717161748Scperciva	fi
718161748Scperciva
719173441Scperciva	# Check that securelevel <= 0.  Otherwise we can't update schg files.
720173441Scperciva	if [ `sysctl -n kern.securelevel` -gt 0 ]; then
721173441Scperciva		echo "Updates cannot be installed when the system securelevel"
722173441Scperciva		echo "is greater than zero."
723173441Scperciva		exit 1
724173441Scperciva	fi
725173441Scperciva
726161748Scperciva	# Check that we have a working directory
727161748Scperciva	_WORKDIR_bad="Directory does not exist or is not writable: "
728161748Scperciva	if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
729161748Scperciva		echo -n "`basename $0`: "
730161748Scperciva		echo -n "${_WORKDIR_bad}"
731161748Scperciva		echo ${WORKDIR}
732161748Scperciva		exit 1
733161748Scperciva	fi
734161748Scperciva	cd ${WORKDIR} || exit 1
735161748Scperciva
736161748Scperciva	# Construct a unique name from ${BASEDIR}
737161748Scperciva	BDHASH=`echo ${BASEDIR} | sha256 -q`
738161748Scperciva
739161748Scperciva	# Check that we have updates ready to install
740161748Scperciva	if ! [ -L ${BDHASH}-install ]; then
741161748Scperciva		echo "No updates are available to install."
742161748Scperciva		echo "Run '$0 fetch' first."
743161748Scperciva		exit 1
744161748Scperciva	fi
745161748Scperciva	if ! [ -f ${BDHASH}-install/INDEX-OLD ] ||
746161748Scperciva	    ! [ -f ${BDHASH}-install/INDEX-NEW ]; then
747161748Scperciva		echo "Update manifest is corrupt -- this should never happen."
748161748Scperciva		echo "Re-run '$0 fetch'."
749161748Scperciva		exit 1
750161748Scperciva	fi
751196392Ssimon
752196392Ssimon	# Figure out what directory contains the running kernel
753196392Ssimon	BOOTFILE=`sysctl -n kern.bootfile`
754196392Ssimon	KERNELDIR=${BOOTFILE%/kernel}
755196392Ssimon	if ! [ -d ${KERNELDIR} ]; then
756196392Ssimon		echo "Cannot identify running kernel"
757196392Ssimon		exit 1
758196392Ssimon	fi
759161748Scperciva}
760161748Scperciva
761161748Scperciva# Perform sanity checks and set some final parameters in
762161748Scperciva# preparation for UNinstalling updates.
763161748Scpercivarollback_check_params () {
764161748Scperciva	# Check that we are root.  All sorts of things won't work otherwise.
765161748Scperciva	if [ `id -u` != 0 ]; then
766161748Scperciva		echo "You must be root to run this."
767161748Scperciva		exit 1
768161748Scperciva	fi
769161748Scperciva
770161748Scperciva	# Check that we have a working directory
771161748Scperciva	_WORKDIR_bad="Directory does not exist or is not writable: "
772161748Scperciva	if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
773161748Scperciva		echo -n "`basename $0`: "
774161748Scperciva		echo -n "${_WORKDIR_bad}"
775161748Scperciva		echo ${WORKDIR}
776161748Scperciva		exit 1
777161748Scperciva	fi
778161748Scperciva	cd ${WORKDIR} || exit 1
779161748Scperciva
780161748Scperciva	# Construct a unique name from ${BASEDIR}
781161748Scperciva	BDHASH=`echo ${BASEDIR} | sha256 -q`
782161748Scperciva
783161748Scperciva	# Check that we have updates ready to rollback
784161748Scperciva	if ! [ -L ${BDHASH}-rollback ]; then
785161748Scperciva		echo "No rollback directory found."
786161748Scperciva		exit 1
787161748Scperciva	fi
788161748Scperciva	if ! [ -f ${BDHASH}-rollback/INDEX-OLD ] ||
789161748Scperciva	    ! [ -f ${BDHASH}-rollback/INDEX-NEW ]; then
790161748Scperciva		echo "Update manifest is corrupt -- this should never happen."
791161748Scperciva		exit 1
792161748Scperciva	fi
793161748Scperciva}
794161748Scperciva
795181142Scperciva# Perform sanity checks and set some final parameters
796181142Scperciva# in preparation for comparing the system against the
797181142Scperciva# published index.  Figure out which index we should
798181142Scperciva# compare against: If the user is running *-p[0-9]+,
799181142Scperciva# strip off the last part; if the user is running
800181142Scperciva# -SECURITY, call it -RELEASE.  Chdir into the working
801181142Scperciva# directory.
802181142ScpercivaIDS_check_params () {
803181142Scperciva	export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
804181142Scperciva
805181142Scperciva	_SERVERNAME_z=\
806181142Scperciva"SERVERNAME must be given via command line or configuration file."
807181142Scperciva	_KEYPRINT_z="Key must be given via -k option or configuration file."
808181142Scperciva	_KEYPRINT_bad="Invalid key fingerprint: "
809181142Scperciva	_WORKDIR_bad="Directory does not exist or is not writable: "
810181142Scperciva
811181142Scperciva	if [ -z "${SERVERNAME}" ]; then
812181142Scperciva		echo -n "`basename $0`: "
813181142Scperciva		echo "${_SERVERNAME_z}"
814181142Scperciva		exit 1
815181142Scperciva	fi
816181142Scperciva	if [ -z "${KEYPRINT}" ]; then
817181142Scperciva		echo -n "`basename $0`: "
818181142Scperciva		echo "${_KEYPRINT_z}"
819181142Scperciva		exit 1
820181142Scperciva	fi
821181142Scperciva	if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
822181142Scperciva		echo -n "`basename $0`: "
823181142Scperciva		echo -n "${_KEYPRINT_bad}"
824181142Scperciva		echo ${KEYPRINT}
825181142Scperciva		exit 1
826181142Scperciva	fi
827181142Scperciva	if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
828181142Scperciva		echo -n "`basename $0`: "
829181142Scperciva		echo -n "${_WORKDIR_bad}"
830181142Scperciva		echo ${WORKDIR}
831181142Scperciva		exit 1
832181142Scperciva	fi
833181142Scperciva	cd ${WORKDIR} || exit 1
834181142Scperciva
835181142Scperciva	# Generate release number.  The s/SECURITY/RELEASE/ bit exists
836181142Scperciva	# to provide an upgrade path for FreeBSD Update 1.x users, since
837181142Scperciva	# the kernels provided by FreeBSD Update 1.x are always labelled
838181142Scperciva	# as X.Y-SECURITY.
839181142Scperciva	RELNUM=`uname -r |
840181142Scperciva	    sed -E 's,-p[0-9]+,,' |
841181142Scperciva	    sed -E 's,-SECURITY,-RELEASE,'`
842181142Scperciva	ARCH=`uname -m`
843181142Scperciva	FETCHDIR=${RELNUM}/${ARCH}
844181142Scperciva	PATCHDIR=${RELNUM}/${ARCH}/bp
845181142Scperciva
846181142Scperciva	# Figure out what directory contains the running kernel
847181142Scperciva	BOOTFILE=`sysctl -n kern.bootfile`
848181142Scperciva	KERNELDIR=${BOOTFILE%/kernel}
849181142Scperciva	if ! [ -d ${KERNELDIR} ]; then
850181142Scperciva		echo "Cannot identify running kernel"
851181142Scperciva		exit 1
852181142Scperciva	fi
853181142Scperciva
854181142Scperciva	# Figure out what kernel configuration is running.  We start with
855181142Scperciva	# the output of `uname -i`, and then make the following adjustments:
856181142Scperciva	# 1. Replace "SMP-GENERIC" with "SMP".  Why the SMP kernel config
857181142Scperciva	# file says "ident SMP-GENERIC", I don't know...
858181142Scperciva	# 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
859181142Scperciva	# _and_ `sysctl kern.version` contains a line which ends "/SMP", then
860181142Scperciva	# we're running an SMP kernel.  This mis-identification is a bug
861181142Scperciva	# which was fixed in 6.2-STABLE.
862181142Scperciva	KERNCONF=`uname -i`
863181142Scperciva	if [ ${KERNCONF} = "SMP-GENERIC" ]; then
864181142Scperciva		KERNCONF=SMP
865181142Scperciva	fi
866181142Scperciva	if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
867181142Scperciva		if sysctl kern.version | grep -qE '/SMP$'; then
868181142Scperciva			KERNCONF=SMP
869181142Scperciva		fi
870181142Scperciva	fi
871181142Scperciva
872181142Scperciva	# Define some paths
873181142Scperciva	SHA256=/sbin/sha256
874181142Scperciva	PHTTPGET=/usr/libexec/phttpget
875181142Scperciva
876181142Scperciva	# Set up variables relating to VERBOSELEVEL
877181142Scperciva	fetch_setup_verboselevel
878181142Scperciva}
879181142Scperciva
880161748Scperciva#### Core functionality -- the actual work gets done here
881161748Scperciva
882161748Scperciva# Use an SRV query to pick a server.  If the SRV query doesn't provide
883161748Scperciva# a useful answer, use the server name specified by the user.
884161748Scperciva# Put another way... look up _http._tcp.${SERVERNAME} and pick a server
885161748Scperciva# from that; or if no servers are returned, use ${SERVERNAME}.
886161748Scperciva# This allows a user to specify "portsnap.freebsd.org" (in which case
887161748Scperciva# portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org"
888161748Scperciva# (in which case portsnap will use that particular server, since there
889161748Scperciva# won't be an SRV entry for that name).
890161748Scperciva#
891161748Scperciva# We ignore the Port field, since we are always going to use port 80.
892161748Scperciva
893161748Scperciva# Fetch the mirror list, but do not pick a mirror yet.  Returns 1 if
894161748Scperciva# no mirrors are available for any reason.
895161748Scpercivafetch_pick_server_init () {
896161748Scperciva	: > serverlist_tried
897161748Scperciva
898161748Scperciva# Check that host(1) exists (i.e., that the system wasn't built with the
899161748Scperciva# WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist.
900161748Scperciva	if ! which -s host; then
901161748Scperciva		: > serverlist_full
902161748Scperciva		return 1
903161748Scperciva	fi
904161748Scperciva
905161748Scperciva	echo -n "Looking up ${SERVERNAME} mirrors... "
906161748Scperciva
907161748Scperciva# Issue the SRV query and pull out the Priority, Weight, and Target fields.
908161748Scperciva# BIND 9 prints "$name has SRV record ..." while BIND 8 prints
909161748Scperciva# "$name server selection ..."; we allow either format.
910161748Scperciva	MLIST="_http._tcp.${SERVERNAME}"
911161748Scperciva	host -t srv "${MLIST}" |
912161748Scperciva	    sed -nE "s/${MLIST} (has SRV record|server selection) //p" |
913161748Scperciva	    cut -f 1,2,4 -d ' ' |
914161748Scperciva	    sed -e 's/\.$//' |
915161748Scperciva	    sort > serverlist_full
916161748Scperciva
917161748Scperciva# If no records, give up -- we'll just use the server name we were given.
918161748Scperciva	if [ `wc -l < serverlist_full` -eq 0 ]; then
919161748Scperciva		echo "none found."
920161748Scperciva		return 1
921161748Scperciva	fi
922161748Scperciva
923161748Scperciva# Report how many mirrors we found.
924161748Scperciva	echo `wc -l < serverlist_full` "mirrors found."
925161748Scperciva
926161748Scperciva# Generate a random seed for use in picking mirrors.  If HTTP_PROXY
927161748Scperciva# is set, this will be used to generate the seed; otherwise, the seed
928161748Scperciva# will be random.
929161748Scperciva	if [ -n "${HTTP_PROXY}${http_proxy}" ]; then
930161748Scperciva		RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" |
931161748Scperciva		    tr -d 'a-f' |
932161748Scperciva		    cut -c 1-9`
933161748Scperciva	else
934161748Scperciva		RANDVALUE=`jot -r 1 0 999999999`
935161748Scperciva	fi
936161748Scperciva}
937161748Scperciva
938161748Scperciva# Pick a mirror.  Returns 1 if we have run out of mirrors to try.
939161748Scpercivafetch_pick_server () {
940161748Scperciva# Generate a list of not-yet-tried mirrors
941161748Scperciva	sort serverlist_tried |
942161748Scperciva	    comm -23 serverlist_full - > serverlist
943161748Scperciva
944161748Scperciva# Have we run out of mirrors?
945161748Scperciva	if [ `wc -l < serverlist` -eq 0 ]; then
946161748Scperciva		echo "No mirrors remaining, giving up."
947161748Scperciva		return 1
948161748Scperciva	fi
949161748Scperciva
950161748Scperciva# Find the highest priority level (lowest numeric value).
951161748Scperciva	SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1`
952161748Scperciva
953161748Scperciva# Add up the weights of the response lines at that priority level.
954161748Scperciva	SRV_WSUM=0;
955161748Scperciva	while read X; do
956161748Scperciva		case "$X" in
957161748Scperciva		${SRV_PRIORITY}\ *)
958161748Scperciva			SRV_W=`echo $X | cut -f 2 -d ' '`
959161748Scperciva			SRV_WSUM=$(($SRV_WSUM + $SRV_W))
960161748Scperciva			;;
961161748Scperciva		esac
962161748Scperciva	done < serverlist
963161748Scperciva
964161748Scperciva# If all the weights are 0, pretend that they are all 1 instead.
965161748Scperciva	if [ ${SRV_WSUM} -eq 0 ]; then
966161748Scperciva		SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l`
967161748Scperciva		SRV_W_ADD=1
968161748Scperciva	else
969161748Scperciva		SRV_W_ADD=0
970161748Scperciva	fi
971161748Scperciva
972161748Scperciva# Pick a value between 0 and the sum of the weights - 1
973161748Scperciva	SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}`
974161748Scperciva
975161748Scperciva# Read through the list of mirrors and set SERVERNAME.  Write the line
976161748Scperciva# corresponding to the mirror we selected into serverlist_tried so that
977161748Scperciva# we won't try it again.
978161748Scperciva	while read X; do
979161748Scperciva		case "$X" in
980161748Scperciva		${SRV_PRIORITY}\ *)
981161748Scperciva			SRV_W=`echo $X | cut -f 2 -d ' '`
982161748Scperciva			SRV_W=$(($SRV_W + $SRV_W_ADD))
983161748Scperciva			if [ $SRV_RND -lt $SRV_W ]; then
984161748Scperciva				SERVERNAME=`echo $X | cut -f 3 -d ' '`
985161748Scperciva				echo "$X" >> serverlist_tried
986161748Scperciva				break
987161748Scperciva			else
988161748Scperciva				SRV_RND=$(($SRV_RND - $SRV_W))
989161748Scperciva			fi
990161748Scperciva			;;
991161748Scperciva		esac
992161748Scperciva	done < serverlist
993161748Scperciva}
994161748Scperciva
995161748Scperciva# Take a list of ${oldhash}|${newhash} and output a list of needed patches,
996161748Scperciva# i.e., those for which we have ${oldhash} and don't have ${newhash}.
997161748Scpercivafetch_make_patchlist () {
998161748Scperciva	grep -vE "^([0-9a-f]{64})\|\1$" |
999161748Scperciva	    tr '|' ' ' |
1000161748Scperciva		while read X Y; do
1001161748Scperciva			if [ -f "files/${Y}.gz" ] ||
1002161748Scperciva			    [ ! -f "files/${X}.gz" ]; then
1003161748Scperciva				continue
1004161748Scperciva			fi
1005161748Scperciva			echo "${X}|${Y}"
1006161748Scperciva		done | uniq
1007161748Scperciva}
1008161748Scperciva
1009161748Scperciva# Print user-friendly progress statistics
1010161748Scpercivafetch_progress () {
1011161748Scperciva	LNC=0
1012161748Scperciva	while read x; do
1013161748Scperciva		LNC=$(($LNC + 1))
1014161748Scperciva		if [ $(($LNC % 10)) = 0 ]; then
1015161748Scperciva			echo -n $LNC
1016161748Scperciva		elif [ $(($LNC % 2)) = 0 ]; then
1017161748Scperciva			echo -n .
1018161748Scperciva		fi
1019161748Scperciva	done
1020161748Scperciva	echo -n " "
1021161748Scperciva}
1022161748Scperciva
1023173564Scperciva# Function for asking the user if everything is ok
1024173564Scpercivacontinuep () {
1025173564Scperciva	while read -p "Does this look reasonable (y/n)? " CONTINUE; do
1026173564Scperciva		case "${CONTINUE}" in
1027173564Scperciva		y*)
1028173564Scperciva			return 0
1029173564Scperciva			;;
1030173564Scperciva		n*)
1031173564Scperciva			return 1
1032173564Scperciva			;;
1033173564Scperciva		esac
1034173564Scperciva	done
1035173564Scperciva}
1036173564Scperciva
1037161748Scperciva# Initialize the working directory
1038161748Scpercivaworkdir_init () {
1039161748Scperciva	mkdir -p files
1040161748Scperciva	touch tINDEX.present
1041161748Scperciva}
1042161748Scperciva
1043161748Scperciva# Check that we have a public key with an appropriate hash, or
1044161748Scperciva# fetch the key if it doesn't exist.  Returns 1 if the key has
1045161748Scperciva# not yet been fetched.
1046161748Scpercivafetch_key () {
1047161748Scperciva	if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
1048161748Scperciva		return 0
1049161748Scperciva	fi
1050161748Scperciva
1051161748Scperciva	echo -n "Fetching public key from ${SERVERNAME}... "
1052161748Scperciva	rm -f pub.ssl
1053161748Scperciva	fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/pub.ssl \
1054161748Scperciva	    2>${QUIETREDIR} || true
1055161748Scperciva	if ! [ -r pub.ssl ]; then
1056161748Scperciva		echo "failed."
1057161748Scperciva		return 1
1058161748Scperciva	fi
1059161748Scperciva	if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
1060161748Scperciva		echo "key has incorrect hash."
1061161748Scperciva		rm -f pub.ssl
1062161748Scperciva		return 1
1063161748Scperciva	fi
1064161748Scperciva	echo "done."
1065161748Scperciva}
1066161748Scperciva
1067161748Scperciva# Fetch metadata signature, aka "tag".
1068161748Scpercivafetch_tag () {
1069173564Scperciva	echo -n "Fetching metadata signature "
1070173564Scperciva	echo ${NDEBUG} "for ${RELNUM} from ${SERVERNAME}... "
1071161748Scperciva	rm -f latest.ssl
1072161748Scperciva	fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/latest.ssl	\
1073161748Scperciva	    2>${QUIETREDIR} || true
1074161748Scperciva	if ! [ -r latest.ssl ]; then
1075161748Scperciva		echo "failed."
1076161748Scperciva		return 1
1077161748Scperciva	fi
1078161748Scperciva
1079161748Scperciva	openssl rsautl -pubin -inkey pub.ssl -verify		\
1080161748Scperciva	    < latest.ssl > tag.new 2>${QUIETREDIR} || true
1081161748Scperciva	rm latest.ssl
1082161748Scperciva
1083161748Scperciva	if ! [ `wc -l < tag.new` = 1 ] ||
1084161748Scperciva	    ! grep -qE	\
1085161748Scperciva    "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
1086161748Scperciva		tag.new; then
1087161748Scperciva		echo "invalid signature."
1088161748Scperciva		return 1
1089161748Scperciva	fi
1090161748Scperciva
1091161748Scperciva	echo "done."
1092161748Scperciva
1093161748Scperciva	RELPATCHNUM=`cut -f 4 -d '|' < tag.new`
1094161748Scperciva	TINDEXHASH=`cut -f 5 -d '|' < tag.new`
1095161748Scperciva	EOLTIME=`cut -f 6 -d '|' < tag.new`
1096161748Scperciva}
1097161748Scperciva
1098161748Scperciva# Sanity-check the patch number in a tag, to make sure that we're not
1099161748Scperciva# going to "update" backwards and to prevent replay attacks.
1100161748Scpercivafetch_tagsanity () {
1101161748Scperciva	# Check that we're not going to move from -pX to -pY with Y < X.
1102161748Scperciva	RELPX=`uname -r | sed -E 's,.*-,,'`
1103161748Scperciva	if echo ${RELPX} | grep -qE '^p[0-9]+$'; then
1104161748Scperciva		RELPX=`echo ${RELPX} | cut -c 2-`
1105161748Scperciva	else
1106161748Scperciva		RELPX=0
1107161748Scperciva	fi
1108161748Scperciva	if [ "${RELPATCHNUM}" -lt "${RELPX}" ]; then
1109161748Scperciva		echo
1110161748Scperciva		echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
1111161748Scperciva		echo " appear older than what"
1112161748Scperciva		echo "we are currently running (`uname -r`)!"
1113161748Scperciva		echo "Cowardly refusing to proceed any further."
1114161748Scperciva		return 1
1115161748Scperciva	fi
1116161748Scperciva
1117161748Scperciva	# If "tag" exists and corresponds to ${RELNUM}, make sure that
1118161748Scperciva	# it contains a patch number <= RELPATCHNUM, in order to protect
1119161748Scperciva	# against rollback (replay) attacks.
1120161748Scperciva	if [ -f tag ] &&
1121161748Scperciva	    grep -qE	\
1122161748Scperciva    "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
1123161748Scperciva		tag; then
1124161748Scperciva		LASTRELPATCHNUM=`cut -f 4 -d '|' < tag`
1125161748Scperciva
1126161748Scperciva		if [ "${RELPATCHNUM}" -lt "${LASTRELPATCHNUM}" ]; then
1127161748Scperciva			echo
1128161748Scperciva			echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
1129161748Scperciva			echo " are older than the"
1130161748Scperciva			echo -n "most recently seen updates"
1131161748Scperciva			echo " (${RELNUM}-p${LASTRELPATCHNUM})."
1132161748Scperciva			echo "Cowardly refusing to proceed any further."
1133161748Scperciva			return 1
1134161748Scperciva		fi
1135161748Scperciva	fi
1136161748Scperciva}
1137161748Scperciva
1138161748Scperciva# Fetch metadata index file
1139161748Scpercivafetch_metadata_index () {
1140161748Scperciva	echo ${NDEBUG} "Fetching metadata index... "
1141161748Scperciva	rm -f ${TINDEXHASH}
1142161748Scperciva	fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/t/${TINDEXHASH}
1143161748Scperciva	    2>${QUIETREDIR}
1144161748Scperciva	if ! [ -f ${TINDEXHASH} ]; then
1145161748Scperciva		echo "failed."
1146161748Scperciva		return 1
1147161748Scperciva	fi
1148161748Scperciva	if [ `${SHA256} -q ${TINDEXHASH}` != ${TINDEXHASH} ]; then
1149161748Scperciva		echo "update metadata index corrupt."
1150161748Scperciva		return 1
1151161748Scperciva	fi
1152161748Scperciva	echo "done."
1153161748Scperciva}
1154161748Scperciva
1155161748Scperciva# Print an error message about signed metadata being bogus.
1156161748Scpercivafetch_metadata_bogus () {
1157161748Scperciva	echo
1158161748Scperciva	echo "The update metadata$1 is correctly signed, but"
1159161748Scperciva	echo "failed an integrity check."
1160161748Scperciva	echo "Cowardly refusing to proceed any further."
1161161748Scperciva	return 1
1162161748Scperciva}
1163161748Scperciva
1164161748Scperciva# Construct tINDEX.new by merging the lines named in $1 from ${TINDEXHASH}
1165161748Scperciva# with the lines not named in $@ from tINDEX.present (if that file exists).
1166161748Scpercivafetch_metadata_index_merge () {
1167161748Scperciva	for METAFILE in $@; do
1168161748Scperciva		if [ `grep -E "^${METAFILE}\|" ${TINDEXHASH} | wc -l`	\
1169161748Scperciva		    -ne 1 ]; then
1170161748Scperciva			fetch_metadata_bogus " index"
1171161748Scperciva			return 1
1172161748Scperciva		fi
1173161748Scperciva
1174161748Scperciva		grep -E "${METAFILE}\|" ${TINDEXHASH}
1175161748Scperciva	done |
1176161748Scperciva	    sort > tINDEX.wanted
1177161748Scperciva
1178161748Scperciva	if [ -f tINDEX.present ]; then
1179161748Scperciva		join -t '|' -v 2 tINDEX.wanted tINDEX.present |
1180161748Scperciva		    sort -m - tINDEX.wanted > tINDEX.new
1181161748Scperciva		rm tINDEX.wanted
1182161748Scperciva	else
1183161748Scperciva		mv tINDEX.wanted tINDEX.new
1184161748Scperciva	fi
1185161748Scperciva}
1186161748Scperciva
1187161748Scperciva# Sanity check all the lines of tINDEX.new.  Even if more metadata lines
1188161748Scperciva# are added by future versions of the server, this won't cause problems,
1189161748Scperciva# since the only lines which appear in tINDEX.new are the ones which we
1190161748Scperciva# specifically grepped out of ${TINDEXHASH}.
1191161748Scpercivafetch_metadata_index_sanity () {
1192161748Scperciva	if grep -qvE '^[0-9A-Z.-]+\|[0-9a-f]{64}$' tINDEX.new; then
1193161748Scperciva		fetch_metadata_bogus " index"
1194161748Scperciva		return 1
1195161748Scperciva	fi
1196161748Scperciva}
1197161748Scperciva
1198161748Scperciva# Sanity check the metadata file $1.
1199161748Scpercivafetch_metadata_sanity () {
1200161748Scperciva	# Some aliases to save space later: ${P} is a character which can
1201161748Scperciva	# appear in a path; ${M} is the four numeric metadata fields; and
1202161748Scperciva	# ${H} is a sha256 hash.
1203257192Sdelphij	P="[-+./:=%@_[~[:alnum:]]"
1204161748Scperciva	M="[0-9]+\|[0-9]+\|[0-9]+\|[0-9]+"
1205161748Scperciva	H="[0-9a-f]{64}"
1206161748Scperciva
1207161748Scperciva	# Check that the first four fields make sense.
1208161748Scperciva	if gunzip -c < files/$1.gz |
1209161748Scperciva	    grep -qvE "^[a-z]+\|[0-9a-z]+\|${P}+\|[fdL-]\|"; then
1210161748Scperciva		fetch_metadata_bogus ""
1211161748Scperciva		return 1
1212161748Scperciva	fi
1213161748Scperciva
1214161748Scperciva	# Remove the first three fields.
1215161748Scperciva	gunzip -c < files/$1.gz |
1216161748Scperciva	    cut -f 4- -d '|' > sanitycheck.tmp
1217161748Scperciva
1218161748Scperciva	# Sanity check entries with type 'f'
1219161748Scperciva	if grep -E '^f' sanitycheck.tmp |
1220161748Scperciva	    grep -qvE "^f\|${M}\|${H}\|${P}*\$"; then
1221161748Scperciva		fetch_metadata_bogus ""
1222161748Scperciva		return 1
1223161748Scperciva	fi
1224161748Scperciva
1225161748Scperciva	# Sanity check entries with type 'd'
1226161748Scperciva	if grep -E '^d' sanitycheck.tmp |
1227161748Scperciva	    grep -qvE "^d\|${M}\|\|\$"; then
1228161748Scperciva		fetch_metadata_bogus ""
1229161748Scperciva		return 1
1230161748Scperciva	fi
1231161748Scperciva
1232161748Scperciva	# Sanity check entries with type 'L'
1233161748Scperciva	if grep -E '^L' sanitycheck.tmp |
1234161748Scperciva	    grep -qvE "^L\|${M}\|${P}*\|\$"; then
1235161748Scperciva		fetch_metadata_bogus ""
1236161748Scperciva		return 1
1237161748Scperciva	fi
1238161748Scperciva
1239161748Scperciva	# Sanity check entries with type '-'
1240161748Scperciva	if grep -E '^-' sanitycheck.tmp |
1241161748Scperciva	    grep -qvE "^-\|\|\|\|\|\|"; then
1242161748Scperciva		fetch_metadata_bogus ""
1243161748Scperciva		return 1
1244161748Scperciva	fi
1245161748Scperciva
1246161748Scperciva	# Clean up
1247161748Scperciva	rm sanitycheck.tmp
1248161748Scperciva}
1249161748Scperciva
1250161748Scperciva# Fetch the metadata index and metadata files listed in $@,
1251161748Scperciva# taking advantage of metadata patches where possible.
1252161748Scpercivafetch_metadata () {
1253161748Scperciva	fetch_metadata_index || return 1
1254161748Scperciva	fetch_metadata_index_merge $@ || return 1
1255161748Scperciva	fetch_metadata_index_sanity || return 1
1256161748Scperciva
1257161748Scperciva	# Generate a list of wanted metadata patches
1258161748Scperciva	join -t '|' -o 1.2,2.2 tINDEX.present tINDEX.new |
1259161748Scperciva	    fetch_make_patchlist > patchlist
1260161748Scperciva
1261161748Scperciva	if [ -s patchlist ]; then
1262161748Scperciva		# Attempt to fetch metadata patches
1263161748Scperciva		echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
1264161748Scperciva		echo ${NDEBUG} "metadata patches.${DDSTATS}"
1265161748Scperciva		tr '|' '-' < patchlist |
1266161748Scperciva		    lam -s "${FETCHDIR}/tp/" - -s ".gz" |
1267161748Scperciva		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1268161748Scperciva			2>${STATSREDIR} | fetch_progress
1269161748Scperciva		echo "done."
1270161748Scperciva
1271161748Scperciva		# Attempt to apply metadata patches
1272161748Scperciva		echo -n "Applying metadata patches... "
1273161748Scperciva		tr '|' ' ' < patchlist |
1274161748Scperciva		    while read X Y; do
1275161748Scperciva			if [ ! -f "${X}-${Y}.gz" ]; then continue; fi
1276161748Scperciva			gunzip -c < ${X}-${Y}.gz > diff
1277161748Scperciva			gunzip -c < files/${X}.gz > diff-OLD
1278161748Scperciva
1279161748Scperciva			# Figure out which lines are being added and removed
1280161748Scperciva			grep -E '^-' diff |
1281161748Scperciva			    cut -c 2- |
1282161748Scperciva			    while read PREFIX; do
1283161748Scperciva				look "${PREFIX}" diff-OLD
1284161748Scperciva			    done |
1285161748Scperciva			    sort > diff-rm
1286161748Scperciva			grep -E '^\+' diff |
1287161748Scperciva			    cut -c 2- > diff-add
1288161748Scperciva
1289161748Scperciva			# Generate the new file
1290161748Scperciva			comm -23 diff-OLD diff-rm |
1291161748Scperciva			    sort - diff-add > diff-NEW
1292161748Scperciva
1293161748Scperciva			if [ `${SHA256} -q diff-NEW` = ${Y} ]; then
1294161748Scperciva				mv diff-NEW files/${Y}
1295161748Scperciva				gzip -n files/${Y}
1296161748Scperciva			else
1297161748Scperciva				mv diff-NEW ${Y}.bad
1298161748Scperciva			fi
1299161748Scperciva			rm -f ${X}-${Y}.gz diff
1300161748Scperciva			rm -f diff-OLD diff-NEW diff-add diff-rm
1301161748Scperciva		done 2>${QUIETREDIR}
1302161748Scperciva		echo "done."
1303161748Scperciva	fi
1304161748Scperciva
1305161748Scperciva	# Update metadata without patches
1306161748Scperciva	cut -f 2 -d '|' < tINDEX.new |
1307161748Scperciva	    while read Y; do
1308161748Scperciva		if [ ! -f "files/${Y}.gz" ]; then
1309161748Scperciva			echo ${Y};
1310161748Scperciva		fi
1311164600Scperciva	    done |
1312164600Scperciva	    sort -u > filelist
1313161748Scperciva
1314161748Scperciva	if [ -s filelist ]; then
1315161748Scperciva		echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1316161748Scperciva		echo ${NDEBUG} "metadata files... "
1317161748Scperciva		lam -s "${FETCHDIR}/m/" - -s ".gz" < filelist |
1318161748Scperciva		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1319161748Scperciva		    2>${QUIETREDIR}
1320161748Scperciva
1321161748Scperciva		while read Y; do
1322161748Scperciva			if ! [ -f ${Y}.gz ]; then
1323161748Scperciva				echo "failed."
1324161748Scperciva				return 1
1325161748Scperciva			fi
1326161748Scperciva			if [ `gunzip -c < ${Y}.gz |
1327161748Scperciva			    ${SHA256} -q` = ${Y} ]; then
1328161748Scperciva				mv ${Y}.gz files/${Y}.gz
1329161748Scperciva			else
1330161748Scperciva				echo "metadata is corrupt."
1331161748Scperciva				return 1
1332161748Scperciva			fi
1333161748Scperciva		done < filelist
1334161748Scperciva		echo "done."
1335161748Scperciva	fi
1336161748Scperciva
1337161748Scperciva# Sanity-check the metadata files.
1338161748Scperciva	cut -f 2 -d '|' tINDEX.new > filelist
1339161748Scperciva	while read X; do
1340161748Scperciva		fetch_metadata_sanity ${X} || return 1
1341161748Scperciva	done < filelist
1342161748Scperciva
1343161748Scperciva# Remove files which are no longer needed
1344161748Scperciva	cut -f 2 -d '|' tINDEX.present |
1345161748Scperciva	    sort > oldfiles
1346161748Scperciva	cut -f 2 -d '|' tINDEX.new |
1347161748Scperciva	    sort |
1348161748Scperciva	    comm -13 - oldfiles |
1349161748Scperciva	    lam -s "files/" - -s ".gz" |
1350161748Scperciva	    xargs rm -f
1351161748Scperciva	rm patchlist filelist oldfiles
1352161748Scperciva	rm ${TINDEXHASH}
1353161748Scperciva
1354161748Scperciva# We're done!
1355161748Scperciva	mv tINDEX.new tINDEX.present
1356161748Scperciva	mv tag.new tag
1357161748Scperciva
1358161748Scperciva	return 0
1359161748Scperciva}
1360161748Scperciva
1361173564Scperciva# Extract a subset of a downloaded metadata file containing only the parts
1362173564Scperciva# which are listed in COMPONENTS.
1363173564Scpercivafetch_filter_metadata_components () {
1364173564Scperciva	METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'`
1365173564Scperciva	gunzip -c < files/${METAHASH}.gz > $1.all
1366173564Scperciva
1367173564Scperciva	# Fish out the lines belonging to components we care about.
1368173564Scperciva	for C in ${COMPONENTS}; do
1369173564Scperciva		look "`echo ${C} | tr '/' '|'`|" $1.all
1370173564Scperciva	done > $1
1371173564Scperciva
1372173564Scperciva	# Remove temporary file.
1373173564Scperciva	rm $1.all
1374173564Scperciva}
1375173564Scperciva
1376161869Scperciva# Generate a filtered version of the metadata file $1 from the downloaded
1377161748Scperciva# file, by fishing out the lines corresponding to components we're trying
1378161748Scperciva# to keep updated, and then removing lines corresponding to paths we want
1379161748Scperciva# to ignore.
1380161748Scpercivafetch_filter_metadata () {
1381173564Scperciva	# Fish out the lines belonging to components we care about.
1382173564Scperciva	fetch_filter_metadata_components $1
1383161748Scperciva
1384161748Scperciva	# Canonicalize directory names by removing any trailing / in
1385161748Scperciva	# order to avoid listing directories multiple times if they
1386161748Scperciva	# belong to multiple components.  Turning "/" into "" doesn't
1387161748Scperciva	# matter, since we add a leading "/" when we use paths later.
1388173564Scperciva	cut -f 3- -d '|' $1 |
1389161748Scperciva	    sed -e 's,/|d|,|d|,' |
1390276157Sdes	    sed -e 's,/|-|,|-|,' |
1391161748Scperciva	    sort -u > $1.tmp
1392161748Scperciva
1393161748Scperciva	# Figure out which lines to ignore and remove them.
1394161748Scperciva	for X in ${IGNOREPATHS}; do
1395161748Scperciva		grep -E "^${X}" $1.tmp
1396161748Scperciva	done |
1397161748Scperciva	    sort -u |
1398161748Scperciva	    comm -13 - $1.tmp > $1
1399161748Scperciva
1400161748Scperciva	# Remove temporary files.
1401173564Scperciva	rm $1.tmp
1402161748Scperciva}
1403161748Scperciva
1404173564Scperciva# Filter the metadata file $1 by adding lines with "/boot/$2"
1405164600Scperciva# replaced by ${KERNELDIR} (which is `sysctl -n kern.bootfile` minus the
1406173564Scperciva# trailing "/kernel"); and if "/boot/$2" does not exist, remove
1407164600Scperciva# the original lines which start with that.
1408164600Scperciva# Put another way: Deal with the fact that the FOO kernel is sometimes
1409164600Scperciva# installed in /boot/FOO/ and is sometimes installed elsewhere.
1410161748Scpercivafetch_filter_kernel_names () {
1411173564Scperciva	grep ^/boot/$2 $1 |
1412173564Scperciva	    sed -e "s,/boot/$2,${KERNELDIR},g" |
1413161748Scperciva	    sort - $1 > $1.tmp
1414161748Scperciva	mv $1.tmp $1
1415164600Scperciva
1416173564Scperciva	if ! [ -d /boot/$2 ]; then
1417173564Scperciva		grep -v ^/boot/$2 $1 > $1.tmp
1418164600Scperciva		mv $1.tmp $1
1419164600Scperciva	fi
1420161748Scperciva}
1421161748Scperciva
1422161748Scperciva# For all paths appearing in $1 or $3, inspect the system
1423161748Scperciva# and generate $2 describing what is currently installed.
1424161748Scpercivafetch_inspect_system () {
1425161748Scperciva	# No errors yet...
1426161748Scperciva	rm -f .err
1427161748Scperciva
1428161748Scperciva	# Tell the user why his disk is suddenly making lots of noise
1429161748Scperciva	echo -n "Inspecting system... "
1430161748Scperciva
1431161748Scperciva	# Generate list of files to inspect
1432161748Scperciva	cat $1 $3 |
1433161748Scperciva	    cut -f 1 -d '|' |
1434161748Scperciva	    sort -u > filelist
1435161748Scperciva
1436161748Scperciva	# Examine each file and output lines of the form
1437161748Scperciva	# /path/to/file|type|device-inum|user|group|perm|flags|value
1438161748Scperciva	# sorted by device and inode number.
1439161748Scperciva	while read F; do
1440161748Scperciva		# If the symlink/file/directory does not exist, record this.
1441161748Scperciva		if ! [ -e ${BASEDIR}/${F} ]; then
1442161748Scperciva			echo "${F}|-||||||"
1443161748Scperciva			continue
1444161748Scperciva		fi
1445161748Scperciva		if ! [ -r ${BASEDIR}/${F} ]; then
1446161748Scperciva			echo "Cannot read file: ${BASEDIR}/${F}"	\
1447161748Scperciva			    >/dev/stderr
1448161748Scperciva			touch .err
1449161748Scperciva			return 1
1450161748Scperciva		fi
1451161748Scperciva
1452161748Scperciva		# Otherwise, output an index line.
1453161748Scperciva		if [ -L ${BASEDIR}/${F} ]; then
1454161748Scperciva			echo -n "${F}|L|"
1455161748Scperciva			stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1456161748Scperciva			readlink ${BASEDIR}/${F};
1457161748Scperciva		elif [ -f ${BASEDIR}/${F} ]; then
1458161748Scperciva			echo -n "${F}|f|"
1459161748Scperciva			stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1460161748Scperciva			sha256 -q ${BASEDIR}/${F};
1461161748Scperciva		elif [ -d ${BASEDIR}/${F} ]; then
1462161748Scperciva			echo -n "${F}|d|"
1463161748Scperciva			stat -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1464161748Scperciva		else
1465161748Scperciva			echo "Unknown file type: ${BASEDIR}/${F}"	\
1466161748Scperciva			    >/dev/stderr
1467161748Scperciva			touch .err
1468161748Scperciva			return 1
1469161748Scperciva		fi
1470161748Scperciva	done < filelist |
1471161748Scperciva	    sort -k 3,3 -t '|' > $2.tmp
1472161748Scperciva	rm filelist
1473161748Scperciva
1474215087Sbcr	# Check if an error occurred during system inspection
1475161748Scperciva	if [ -f .err ]; then
1476161748Scperciva		return 1
1477161748Scperciva	fi
1478161748Scperciva
1479161748Scperciva	# Convert to the form
1480161748Scperciva	# /path/to/file|type|user|group|perm|flags|value|hlink
1481161748Scperciva	# by resolving identical device and inode numbers into hard links.
1482161748Scperciva	cut -f 1,3 -d '|' $2.tmp |
1483161748Scperciva	    sort -k 1,1 -t '|' |
1484161748Scperciva	    sort -s -u -k 2,2 -t '|' |
1485161748Scperciva	    join -1 2 -2 3 -t '|' - $2.tmp |
1486161748Scperciva	    awk -F \| -v OFS=\|		\
1487161748Scperciva		'{
1488161748Scperciva		    if (($2 == $3) || ($4 == "-"))
1489161748Scperciva			print $3,$4,$5,$6,$7,$8,$9,""
1490161748Scperciva		    else
1491161748Scperciva			print $3,$4,$5,$6,$7,$8,$9,$2
1492161748Scperciva		}' |
1493161748Scperciva	    sort > $2
1494161748Scperciva	rm $2.tmp
1495161748Scperciva
1496161748Scperciva	# We're finished looking around
1497161748Scperciva	echo "done."
1498161748Scperciva}
1499161748Scperciva
1500173564Scperciva# For any paths matching ${MERGECHANGES}, compare $1 and $2 and find any
1501173564Scperciva# files which differ; generate $3 containing these paths and the old hashes.
1502173564Scpercivafetch_filter_mergechanges () {
1503173564Scperciva	# Pull out the paths and hashes of the files matching ${MERGECHANGES}.
1504173564Scperciva	for F in $1 $2; do
1505173564Scperciva		for X in ${MERGECHANGES}; do
1506173564Scperciva			grep -E "^${X}" ${F}
1507173564Scperciva		done |
1508173564Scperciva		    cut -f 1,2,7 -d '|' |
1509173564Scperciva		    sort > ${F}-values
1510173564Scperciva	done
1511173564Scperciva
1512173564Scperciva	# Any line in $2-values which doesn't appear in $1-values and is a
1513173564Scperciva	# file means that we should list the path in $3.
1514173564Scperciva	comm -13 $1-values $2-values |
1515173564Scperciva	    fgrep '|f|' |
1516173564Scperciva	    cut -f 1 -d '|' > $2-paths
1517173564Scperciva
1518173564Scperciva	# For each path, pull out one (and only one!) entry from $1-values.
1519173564Scperciva	# Note that we cannot distinguish which "old" version the user made
1520173564Scperciva	# changes to; but hopefully any changes which occur due to security
1521173564Scperciva	# updates will exist in both the "new" version and the version which
1522173564Scperciva	# the user has installed, so the merging will still work.
1523173564Scperciva	while read X; do
1524173564Scperciva		look "${X}|" $1-values |
1525173564Scperciva		    head -1
1526173564Scperciva	done < $2-paths > $3
1527173564Scperciva
1528173564Scperciva	# Clean up
1529173564Scperciva	rm $1-values $2-values $2-paths
1530173564Scperciva}
1531173564Scperciva
1532161748Scperciva# For any paths matching ${UPDATEIFUNMODIFIED}, remove lines from $[123]
1533173564Scperciva# which correspond to lines in $2 with hashes not matching $1 or $3, unless
1534173564Scperciva# the paths are listed in $4.  For entries in $2 marked "not present"
1535173564Scperciva# (aka. type -), remove lines from $[123] unless there is a corresponding
1536173564Scperciva# entry in $1.
1537161748Scpercivafetch_filter_unmodified_notpresent () {
1538161748Scperciva	# Figure out which lines of $1 and $3 correspond to bits which
1539161748Scperciva	# should only be updated if they haven't changed, and fish out
1540161748Scperciva	# the (path, type, value) tuples.
1541161748Scperciva	# NOTE: We don't consider a file to be "modified" if it matches
1542161748Scperciva	# the hash from $3.
1543161748Scperciva	for X in ${UPDATEIFUNMODIFIED}; do
1544161748Scperciva		grep -E "^${X}" $1
1545161748Scperciva		grep -E "^${X}" $3
1546161748Scperciva	done |
1547161748Scperciva	    cut -f 1,2,7 -d '|' |
1548161748Scperciva	    sort > $1-values
1549161748Scperciva
1550161748Scperciva	# Do the same for $2.
1551161748Scperciva	for X in ${UPDATEIFUNMODIFIED}; do
1552161748Scperciva		grep -E "^${X}" $2
1553161748Scperciva	done |
1554161748Scperciva	    cut -f 1,2,7 -d '|' |
1555161748Scperciva	    sort > $2-values
1556161748Scperciva
1557161748Scperciva	# Any entry in $2-values which is not in $1-values corresponds to
1558173564Scperciva	# a path which we need to remove from $1, $2, and $3, unless it
1559173564Scperciva	# that path appears in $4.
1560173564Scperciva	comm -13 $1-values $2-values |
1561173564Scperciva	    sort -t '|' -k 1,1 > mlines.tmp
1562173564Scperciva	cut -f 1 -d '|' $4 |
1563173564Scperciva	    sort |
1564173564Scperciva	    join -v 2 -t '|' - mlines.tmp |
1565173564Scperciva	    sort > mlines
1566173564Scperciva	rm $1-values $2-values mlines.tmp
1567161748Scperciva
1568161748Scperciva	# Any lines in $2 which are not in $1 AND are "not present" lines
1569161748Scperciva	# also belong in mlines.
1570161748Scperciva	comm -13 $1 $2 |
1571161748Scperciva	    cut -f 1,2,7 -d '|' |
1572161748Scperciva	    fgrep '|-|' >> mlines
1573161748Scperciva
1574161748Scperciva	# Remove lines from $1, $2, and $3
1575161748Scperciva	for X in $1 $2 $3; do
1576161748Scperciva		sort -t '|' -k 1,1 ${X} > ${X}.tmp
1577161748Scperciva		cut -f 1 -d '|' < mlines |
1578161748Scperciva		    sort |
1579161748Scperciva		    join -v 2 -t '|' - ${X}.tmp |
1580161748Scperciva		    sort > ${X}
1581161748Scperciva		rm ${X}.tmp
1582161748Scperciva	done
1583161748Scperciva
1584161748Scperciva	# Store a list of the modified files, for future reference
1585161748Scperciva	fgrep -v '|-|' mlines |
1586161748Scperciva	    cut -f 1 -d '|' > modifiedfiles
1587161748Scperciva	rm mlines
1588161748Scperciva}
1589161748Scperciva
1590161748Scperciva# For each entry in $1 of type -, remove any corresponding
1591161748Scperciva# entry from $2 if ${ALLOWADD} != "yes".  Remove all entries
1592161748Scperciva# of type - from $1.
1593161748Scpercivafetch_filter_allowadd () {
1594161748Scperciva	cut -f 1,2 -d '|' < $1 |
1595161748Scperciva	    fgrep '|-' |
1596161748Scperciva	    cut -f 1 -d '|' > filesnotpresent
1597161748Scperciva
1598161748Scperciva	if [ ${ALLOWADD} != "yes" ]; then
1599161748Scperciva		sort < $2 |
1600161748Scperciva		    join -v 1 -t '|' - filesnotpresent |
1601161748Scperciva		    sort > $2.tmp
1602161748Scperciva		mv $2.tmp $2
1603161748Scperciva	fi
1604161748Scperciva
1605161748Scperciva	sort < $1 |
1606161748Scperciva	    join -v 1 -t '|' - filesnotpresent |
1607161748Scperciva	    sort > $1.tmp
1608161748Scperciva	mv $1.tmp $1
1609161748Scperciva	rm filesnotpresent
1610161748Scperciva}
1611161748Scperciva
1612161748Scperciva# If ${ALLOWDELETE} != "yes", then remove any entries from $1
1613161748Scperciva# which don't correspond to entries in $2.
1614161748Scpercivafetch_filter_allowdelete () {
1615161748Scperciva	# Produce a lists ${PATH}|${TYPE}
1616161748Scperciva	for X in $1 $2; do
1617161748Scperciva		cut -f 1-2 -d '|' < ${X} |
1618161748Scperciva		    sort -u > ${X}.nodes
1619161748Scperciva	done
1620161748Scperciva
1621161748Scperciva	# Figure out which lines need to be removed from $1.
1622161748Scperciva	if [ ${ALLOWDELETE} != "yes" ]; then
1623161748Scperciva		comm -23 $1.nodes $2.nodes > $1.badnodes
1624161748Scperciva	else
1625161748Scperciva		: > $1.badnodes
1626161748Scperciva	fi
1627161748Scperciva
1628161748Scperciva	# Remove the relevant lines from $1
1629161748Scperciva	while read X; do
1630161748Scperciva		look "${X}|" $1
1631161748Scperciva	done < $1.badnodes |
1632161748Scperciva	    comm -13 - $1 > $1.tmp
1633161748Scperciva	mv $1.tmp $1
1634161748Scperciva
1635161748Scperciva	rm $1.badnodes $1.nodes $2.nodes
1636161748Scperciva}
1637161748Scperciva
1638161748Scperciva# If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in $2
1639161748Scperciva# with metadata not matching any entry in $1, replace the corresponding
1640161748Scperciva# line of $3 with one having the same metadata as the entry in $2.
1641161748Scpercivafetch_filter_modified_metadata () {
1642161748Scperciva	# Fish out the metadata from $1 and $2
1643161748Scperciva	for X in $1 $2; do
1644161748Scperciva		cut -f 1-6 -d '|' < ${X} > ${X}.metadata
1645161748Scperciva	done
1646161748Scperciva
1647161748Scperciva	# Find the metadata we need to keep
1648161748Scperciva	if [ ${KEEPMODIFIEDMETADATA} = "yes" ]; then
1649161748Scperciva		comm -13 $1.metadata $2.metadata > keepmeta
1650161748Scperciva	else
1651161748Scperciva		: > keepmeta
1652161748Scperciva	fi
1653161748Scperciva
1654161748Scperciva	# Extract the lines which we need to remove from $3, and
1655161748Scperciva	# construct the lines which we need to add to $3.
1656161748Scperciva	: > $3.remove
1657161748Scperciva	: > $3.add
1658161748Scperciva	while read LINE; do
1659161748Scperciva		NODE=`echo "${LINE}" | cut -f 1-2 -d '|'`
1660161748Scperciva		look "${NODE}|" $3 >> $3.remove
1661161748Scperciva		look "${NODE}|" $3 |
1662161748Scperciva		    cut -f 7- -d '|' |
1663161748Scperciva		    lam -s "${LINE}|" - >> $3.add
1664161748Scperciva	done < keepmeta
1665161748Scperciva
1666161748Scperciva	# Remove the specified lines and add the new lines.
1667161748Scperciva	sort $3.remove |
1668161748Scperciva	    comm -13 - $3 |
1669161748Scperciva	    sort -u - $3.add > $3.tmp
1670161748Scperciva	mv $3.tmp $3
1671161748Scperciva
1672161748Scperciva	rm keepmeta $1.metadata $2.metadata $3.add $3.remove
1673161748Scperciva}
1674161748Scperciva
1675161748Scperciva# Remove lines from $1 and $2 which are identical;
1676161748Scperciva# no need to update a file if it isn't changing.
1677161748Scpercivafetch_filter_uptodate () {
1678161748Scperciva	comm -23 $1 $2 > $1.tmp
1679161748Scperciva	comm -13 $1 $2 > $2.tmp
1680161748Scperciva
1681161748Scperciva	mv $1.tmp $1
1682161748Scperciva	mv $2.tmp $2
1683161748Scperciva}
1684161748Scperciva
1685173564Scperciva# Fetch any "clean" old versions of files we need for merging changes.
1686173564Scpercivafetch_files_premerge () {
1687173564Scperciva	# We only need to do anything if $1 is non-empty.
1688173564Scperciva	if [ -s $1 ]; then
1689173564Scperciva		# Tell the user what we're doing
1690173564Scperciva		echo -n "Fetching files from ${OLDRELNUM} for merging... "
1691173564Scperciva
1692173564Scperciva		# List of files wanted
1693173564Scperciva		fgrep '|f|' < $1 |
1694173564Scperciva		    cut -f 3 -d '|' |
1695173564Scperciva		    sort -u > files.wanted
1696173564Scperciva
1697173564Scperciva		# Only fetch the files we don't already have
1698173564Scperciva		while read Y; do
1699173564Scperciva			if [ ! -f "files/${Y}.gz" ]; then
1700173564Scperciva				echo ${Y};
1701173564Scperciva			fi
1702173564Scperciva		done < files.wanted > filelist
1703173564Scperciva
1704173564Scperciva		# Actually fetch them
1705173564Scperciva		lam -s "${OLDFETCHDIR}/f/" - -s ".gz" < filelist |
1706173564Scperciva		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1707173564Scperciva		    2>${QUIETREDIR}
1708173564Scperciva
1709173564Scperciva		# Make sure we got them all, and move them into /files/
1710173564Scperciva		while read Y; do
1711173564Scperciva			if ! [ -f ${Y}.gz ]; then
1712173564Scperciva				echo "failed."
1713173564Scperciva				return 1
1714173564Scperciva			fi
1715173564Scperciva			if [ `gunzip -c < ${Y}.gz |
1716173564Scperciva			    ${SHA256} -q` = ${Y} ]; then
1717173564Scperciva				mv ${Y}.gz files/${Y}.gz
1718173564Scperciva			else
1719173564Scperciva				echo "${Y} has incorrect hash."
1720173564Scperciva				return 1
1721173564Scperciva			fi
1722173564Scperciva		done < filelist
1723173564Scperciva		echo "done."
1724173564Scperciva
1725173564Scperciva		# Clean up
1726173564Scperciva		rm filelist files.wanted
1727173564Scperciva	fi
1728173564Scperciva}
1729173564Scperciva
1730161748Scperciva# Prepare to fetch files: Generate a list of the files we need,
1731161748Scperciva# copy the unmodified files we have into /files/, and generate
1732161748Scperciva# a list of patches to download.
1733161748Scpercivafetch_files_prepare () {
1734161748Scperciva	# Tell the user why his disk is suddenly making lots of noise
1735161748Scperciva	echo -n "Preparing to download files... "
1736161748Scperciva
1737161748Scperciva	# Reduce indices to ${PATH}|${HASH} pairs
1738161748Scperciva	for X in $1 $2 $3; do
1739161748Scperciva		cut -f 1,2,7 -d '|' < ${X} |
1740161748Scperciva		    fgrep '|f|' |
1741161748Scperciva		    cut -f 1,3 -d '|' |
1742161748Scperciva		    sort > ${X}.hashes
1743161748Scperciva	done
1744161748Scperciva
1745161748Scperciva	# List of files wanted
1746161748Scperciva	cut -f 2 -d '|' < $3.hashes |
1747173441Scperciva	    sort -u |
1748173441Scperciva	    while read HASH; do
1749173441Scperciva		if ! [ -f files/${HASH}.gz ]; then
1750173441Scperciva			echo ${HASH}
1751173441Scperciva		fi
1752173441Scperciva	done > files.wanted
1753161748Scperciva
1754161748Scperciva	# Generate a list of unmodified files
1755161748Scperciva	comm -12 $1.hashes $2.hashes |
1756161748Scperciva	    sort -k 1,1 -t '|' > unmodified.files
1757161748Scperciva
1758161748Scperciva	# Copy all files into /files/.  We only need the unmodified files
1759161748Scperciva	# for use in patching; but we'll want all of them if the user asks
1760161748Scperciva	# to rollback the updates later.
1761171784Scperciva	while read LINE; do
1762171784Scperciva		F=`echo "${LINE}" | cut -f 1 -d '|'`
1763171784Scperciva		HASH=`echo "${LINE}" | cut -f 2 -d '|'`
1764171784Scperciva
1765171784Scperciva		# Skip files we already have.
1766171784Scperciva		if [ -f files/${HASH}.gz ]; then
1767171784Scperciva			continue
1768171784Scperciva		fi
1769171784Scperciva
1770171784Scperciva		# Make sure the file hasn't changed.
1771161748Scperciva		cp "${BASEDIR}/${F}" tmpfile
1772171784Scperciva		if [ `sha256 -q tmpfile` != ${HASH} ]; then
1773171784Scperciva			echo
1774171784Scperciva			echo "File changed while FreeBSD Update running: ${F}"
1775171784Scperciva			return 1
1776171784Scperciva		fi
1777171784Scperciva
1778171784Scperciva		# Place the file into storage.
1779171784Scperciva		gzip -c < tmpfile > files/${HASH}.gz
1780161748Scperciva		rm tmpfile
1781171784Scperciva	done < $2.hashes
1782161748Scperciva
1783161748Scperciva	# Produce a list of patches to download
1784161748Scperciva	sort -k 1,1 -t '|' $3.hashes |
1785161748Scperciva	    join -t '|' -o 2.2,1.2 - unmodified.files |
1786161748Scperciva	    fetch_make_patchlist > patchlist
1787161748Scperciva
1788161748Scperciva	# Garbage collect
1789161748Scperciva	rm unmodified.files $1.hashes $2.hashes $3.hashes
1790161748Scperciva
1791161748Scperciva	# We don't need the list of possible old files any more.
1792161748Scperciva	rm $1
1793161748Scperciva
1794161748Scperciva	# We're finished making noise
1795161748Scperciva	echo "done."
1796161748Scperciva}
1797161748Scperciva
1798161748Scperciva# Fetch files.
1799161748Scpercivafetch_files () {
1800161748Scperciva	# Attempt to fetch patches
1801161748Scperciva	if [ -s patchlist ]; then
1802161748Scperciva		echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
1803161748Scperciva		echo ${NDEBUG} "patches.${DDSTATS}"
1804161748Scperciva		tr '|' '-' < patchlist |
1805173564Scperciva		    lam -s "${PATCHDIR}/" - |
1806161748Scperciva		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1807161748Scperciva			2>${STATSREDIR} | fetch_progress
1808161748Scperciva		echo "done."
1809161748Scperciva
1810161748Scperciva		# Attempt to apply patches
1811161748Scperciva		echo -n "Applying patches... "
1812161748Scperciva		tr '|' ' ' < patchlist |
1813161748Scperciva		    while read X Y; do
1814161748Scperciva			if [ ! -f "${X}-${Y}" ]; then continue; fi
1815161748Scperciva			gunzip -c < files/${X}.gz > OLD
1816161748Scperciva
1817161748Scperciva			bspatch OLD NEW ${X}-${Y}
1818161748Scperciva
1819161748Scperciva			if [ `${SHA256} -q NEW` = ${Y} ]; then
1820161748Scperciva				mv NEW files/${Y}
1821161748Scperciva				gzip -n files/${Y}
1822161748Scperciva			fi
1823161748Scperciva			rm -f diff OLD NEW ${X}-${Y}
1824161748Scperciva		done 2>${QUIETREDIR}
1825161748Scperciva		echo "done."
1826161748Scperciva	fi
1827161748Scperciva
1828161748Scperciva	# Download files which couldn't be generate via patching
1829161748Scperciva	while read Y; do
1830161748Scperciva		if [ ! -f "files/${Y}.gz" ]; then
1831161748Scperciva			echo ${Y};
1832161748Scperciva		fi
1833161748Scperciva	done < files.wanted > filelist
1834161748Scperciva
1835161748Scperciva	if [ -s filelist ]; then
1836161748Scperciva		echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1837161748Scperciva		echo ${NDEBUG} "files... "
1838161748Scperciva		lam -s "${FETCHDIR}/f/" - -s ".gz" < filelist |
1839161748Scperciva		    xargs ${XARGST} ${PHTTPGET} ${SERVERNAME}	\
1840161748Scperciva		    2>${QUIETREDIR}
1841161748Scperciva
1842161748Scperciva		while read Y; do
1843161748Scperciva			if ! [ -f ${Y}.gz ]; then
1844161748Scperciva				echo "failed."
1845161748Scperciva				return 1
1846161748Scperciva			fi
1847161748Scperciva			if [ `gunzip -c < ${Y}.gz |
1848161748Scperciva			    ${SHA256} -q` = ${Y} ]; then
1849161748Scperciva				mv ${Y}.gz files/${Y}.gz
1850161748Scperciva			else
1851161748Scperciva				echo "${Y} has incorrect hash."
1852161748Scperciva				return 1
1853161748Scperciva			fi
1854161748Scperciva		done < filelist
1855161748Scperciva		echo "done."
1856161748Scperciva	fi
1857161748Scperciva
1858161748Scperciva	# Clean up
1859161748Scperciva	rm files.wanted filelist patchlist
1860161748Scperciva}
1861161748Scperciva
1862161748Scperciva# Create and populate install manifest directory; and report what updates
1863161748Scperciva# are available.
1864161748Scpercivafetch_create_manifest () {
1865161748Scperciva	# If we have an existing install manifest, nuke it.
1866161748Scperciva	if [ -L "${BDHASH}-install" ]; then
1867161748Scperciva		rm -r ${BDHASH}-install/
1868161748Scperciva		rm ${BDHASH}-install
1869161748Scperciva	fi
1870161748Scperciva
1871161748Scperciva	# Report to the user if any updates were avoided due to local changes
1872161748Scperciva	if [ -s modifiedfiles ]; then
1873161748Scperciva		echo
1874161748Scperciva		echo -n "The following files are affected by updates, "
1875161748Scperciva		echo "but no changes have"
1876161748Scperciva		echo -n "been downloaded because the files have been "
1877161748Scperciva		echo "modified locally:"
1878161748Scperciva		cat modifiedfiles
1879217767Sgordon	fi | $PAGER
1880161748Scperciva	rm modifiedfiles
1881161748Scperciva
1882161748Scperciva	# If no files will be updated, tell the user and exit
1883161748Scperciva	if ! [ -s INDEX-PRESENT ] &&
1884161748Scperciva	    ! [ -s INDEX-NEW ]; then
1885161748Scperciva		rm INDEX-PRESENT INDEX-NEW
1886161748Scperciva		echo
1887161748Scperciva		echo -n "No updates needed to update system to "
1888161748Scperciva		echo "${RELNUM}-p${RELPATCHNUM}."
1889161748Scperciva		return
1890161748Scperciva	fi
1891161748Scperciva
1892161748Scperciva	# Divide files into (a) removed files, (b) added files, and
1893161748Scperciva	# (c) updated files.
1894161748Scperciva	cut -f 1 -d '|' < INDEX-PRESENT |
1895161748Scperciva	    sort > INDEX-PRESENT.flist
1896161748Scperciva	cut -f 1 -d '|' < INDEX-NEW |
1897161748Scperciva	    sort > INDEX-NEW.flist
1898161748Scperciva	comm -23 INDEX-PRESENT.flist INDEX-NEW.flist > files.removed
1899161748Scperciva	comm -13 INDEX-PRESENT.flist INDEX-NEW.flist > files.added
1900161748Scperciva	comm -12 INDEX-PRESENT.flist INDEX-NEW.flist > files.updated
1901161748Scperciva	rm INDEX-PRESENT.flist INDEX-NEW.flist
1902161748Scperciva
1903161748Scperciva	# Report removed files, if any
1904161748Scperciva	if [ -s files.removed ]; then
1905161748Scperciva		echo
1906161748Scperciva		echo -n "The following files will be removed "
1907161748Scperciva		echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1908161748Scperciva		cat files.removed
1909217767Sgordon	fi | $PAGER
1910161748Scperciva	rm files.removed
1911161748Scperciva
1912161748Scperciva	# Report added files, if any
1913161748Scperciva	if [ -s files.added ]; then
1914161748Scperciva		echo
1915161748Scperciva		echo -n "The following files will be added "
1916161748Scperciva		echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1917161748Scperciva		cat files.added
1918217767Sgordon	fi | $PAGER
1919161748Scperciva	rm files.added
1920161748Scperciva
1921161748Scperciva	# Report updated files, if any
1922161748Scperciva	if [ -s files.updated ]; then
1923161748Scperciva		echo
1924161748Scperciva		echo -n "The following files will be updated "
1925161748Scperciva		echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1926161748Scperciva
1927161748Scperciva		cat files.updated
1928217767Sgordon	fi | $PAGER
1929161748Scperciva	rm files.updated
1930161748Scperciva
1931161748Scperciva	# Create a directory for the install manifest.
1932161748Scperciva	MDIR=`mktemp -d install.XXXXXX` || return 1
1933161748Scperciva
1934161748Scperciva	# Populate it
1935161748Scperciva	mv INDEX-PRESENT ${MDIR}/INDEX-OLD
1936161748Scperciva	mv INDEX-NEW ${MDIR}/INDEX-NEW
1937161748Scperciva
1938161748Scperciva	# Link it into place
1939161748Scperciva	ln -s ${MDIR} ${BDHASH}-install
1940161748Scperciva}
1941161748Scperciva
1942161748Scperciva# Warn about any upcoming EoL
1943161748Scpercivafetch_warn_eol () {
1944161748Scperciva	# What's the current time?
1945161748Scperciva	NOWTIME=`date "+%s"`
1946161748Scperciva
1947161748Scperciva	# When did we last warn about the EoL date?
1948161748Scperciva	if [ -f lasteolwarn ]; then
1949161748Scperciva		LASTWARN=`cat lasteolwarn`
1950161748Scperciva	else
1951161748Scperciva		LASTWARN=`expr ${NOWTIME} - 63072000`
1952161748Scperciva	fi
1953161748Scperciva
1954161748Scperciva	# If the EoL time is past, warn.
1955161748Scperciva	if [ ${EOLTIME} -lt ${NOWTIME} ]; then
1956161748Scperciva		echo
1957161748Scperciva		cat <<-EOF
1958161869Scperciva		WARNING: `uname -sr` HAS PASSED ITS END-OF-LIFE DATE.
1959161748Scperciva		Any security issues discovered after `date -r ${EOLTIME}`
1960161748Scperciva		will not have been corrected.
1961161748Scperciva		EOF
1962161748Scperciva		return 1
1963161748Scperciva	fi
1964161748Scperciva
1965161748Scperciva	# Figure out how long it has been since we last warned about the
1966161748Scperciva	# upcoming EoL, and how much longer we have left.
1967161748Scperciva	SINCEWARN=`expr ${NOWTIME} - ${LASTWARN}`
1968161748Scperciva	TIMELEFT=`expr ${EOLTIME} - ${NOWTIME}`
1969161748Scperciva
1970171838Scperciva	# Don't warn if the EoL is more than 3 months away
1971171838Scperciva	if [ ${TIMELEFT} -gt 7884000 ]; then
1972161748Scperciva		return 0
1973161748Scperciva	fi
1974161748Scperciva
1975161748Scperciva	# Don't warn if the time remaining is more than 3 times the time
1976161748Scperciva	# since the last warning.
1977161748Scperciva	if [ ${TIMELEFT} -gt `expr ${SINCEWARN} \* 3` ]; then
1978161748Scperciva		return 0
1979161748Scperciva	fi
1980161748Scperciva
1981161748Scperciva	# Figure out what time units to use.
1982161748Scperciva	if [ ${TIMELEFT} -lt 604800 ]; then
1983161748Scperciva		UNIT="day"
1984161748Scperciva		SIZE=86400
1985161748Scperciva	elif [ ${TIMELEFT} -lt 2678400 ]; then
1986161748Scperciva		UNIT="week"
1987161748Scperciva		SIZE=604800
1988161748Scperciva	else
1989161748Scperciva		UNIT="month"
1990161748Scperciva		SIZE=2678400
1991161748Scperciva	fi
1992161748Scperciva
1993161748Scperciva	# Compute the right number of units
1994161748Scperciva	NUM=`expr ${TIMELEFT} / ${SIZE}`
1995161748Scperciva	if [ ${NUM} != 1 ]; then
1996161748Scperciva		UNIT="${UNIT}s"
1997161748Scperciva	fi
1998161748Scperciva
1999161748Scperciva	# Print the warning
2000161748Scperciva	echo
2001161748Scperciva	cat <<-EOF
2002161748Scperciva		WARNING: `uname -sr` is approaching its End-of-Life date.
2003161748Scperciva		It is strongly recommended that you upgrade to a newer
2004161748Scperciva		release within the next ${NUM} ${UNIT}.
2005161748Scperciva	EOF
2006161748Scperciva
2007161748Scperciva	# Update the stored time of last warning
2008161748Scperciva	echo ${NOWTIME} > lasteolwarn
2009161748Scperciva}
2010161748Scperciva
2011161748Scperciva# Do the actual work involved in "fetch" / "cron".
2012161748Scpercivafetch_run () {
2013161748Scperciva	workdir_init || return 1
2014161748Scperciva
2015161748Scperciva	# Prepare the mirror list.
2016161748Scperciva	fetch_pick_server_init && fetch_pick_server
2017161748Scperciva
2018161748Scperciva	# Try to fetch the public key until we run out of servers.
2019161748Scperciva	while ! fetch_key; do
2020161748Scperciva		fetch_pick_server || return 1
2021161748Scperciva	done
2022161748Scperciva
2023161748Scperciva	# Try to fetch the metadata index signature ("tag") until we run
2024161748Scperciva	# out of available servers; and sanity check the downloaded tag.
2025161748Scperciva	while ! fetch_tag; do
2026161748Scperciva		fetch_pick_server || return 1
2027161748Scperciva	done
2028161748Scperciva	fetch_tagsanity || return 1
2029161748Scperciva
2030161748Scperciva	# Fetch the latest INDEX-NEW and INDEX-OLD files.
2031161748Scperciva	fetch_metadata INDEX-NEW INDEX-OLD || return 1
2032161748Scperciva
2033161748Scperciva	# Generate filtered INDEX-NEW and INDEX-OLD files containing only
2034161748Scperciva	# the lines which (a) belong to components we care about, and (b)
2035161748Scperciva	# don't correspond to paths we're explicitly ignoring.
2036161748Scperciva	fetch_filter_metadata INDEX-NEW || return 1
2037161748Scperciva	fetch_filter_metadata INDEX-OLD || return 1
2038161748Scperciva
2039173564Scperciva	# Translate /boot/${KERNCONF} into ${KERNELDIR}
2040173564Scperciva	fetch_filter_kernel_names INDEX-NEW ${KERNCONF}
2041173564Scperciva	fetch_filter_kernel_names INDEX-OLD ${KERNCONF}
2042161748Scperciva
2043161748Scperciva	# For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the
2044161748Scperciva	# system and generate an INDEX-PRESENT file.
2045161748Scperciva	fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2046161748Scperciva
2047161748Scperciva	# Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which
2048161748Scperciva	# correspond to lines in INDEX-PRESENT with hashes not appearing
2049161748Scperciva	# in INDEX-OLD or INDEX-NEW.  Also remove lines where the entry in
2050161748Scperciva	# INDEX-PRESENT has type - and there isn't a corresponding entry in
2051161748Scperciva	# INDEX-OLD with type -.
2052173564Scperciva	fetch_filter_unmodified_notpresent	\
2053173564Scperciva	    INDEX-OLD INDEX-PRESENT INDEX-NEW /dev/null
2054161748Scperciva
2055161748Scperciva	# For each entry in INDEX-PRESENT of type -, remove any corresponding
2056161748Scperciva	# entry from INDEX-NEW if ${ALLOWADD} != "yes".  Remove all entries
2057161748Scperciva	# of type - from INDEX-PRESENT.
2058161748Scperciva	fetch_filter_allowadd INDEX-PRESENT INDEX-NEW
2059161748Scperciva
2060161748Scperciva	# If ${ALLOWDELETE} != "yes", then remove any entries from
2061161748Scperciva	# INDEX-PRESENT which don't correspond to entries in INDEX-NEW.
2062161748Scperciva	fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW
2063161748Scperciva
2064161748Scperciva	# If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in
2065161748Scperciva	# INDEX-PRESENT with metadata not matching any entry in INDEX-OLD,
2066161748Scperciva	# replace the corresponding line of INDEX-NEW with one having the
2067161748Scperciva	# same metadata as the entry in INDEX-PRESENT.
2068161748Scperciva	fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW
2069161748Scperciva
2070161748Scperciva	# Remove lines from INDEX-PRESENT and INDEX-NEW which are identical;
2071161748Scperciva	# no need to update a file if it isn't changing.
2072161748Scperciva	fetch_filter_uptodate INDEX-PRESENT INDEX-NEW
2073161748Scperciva
2074161748Scperciva	# Prepare to fetch files: Generate a list of the files we need,
2075161748Scperciva	# copy the unmodified files we have into /files/, and generate
2076161748Scperciva	# a list of patches to download.
2077171784Scperciva	fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2078161748Scperciva
2079161748Scperciva	# Fetch files.
2080161748Scperciva	fetch_files || return 1
2081161748Scperciva
2082161748Scperciva	# Create and populate install manifest directory; and report what
2083161748Scperciva	# updates are available.
2084161748Scperciva	fetch_create_manifest || return 1
2085161748Scperciva
2086161748Scperciva	# Warn about any upcoming EoL
2087161748Scperciva	fetch_warn_eol || return 1
2088161748Scperciva}
2089161748Scperciva
2090173564Scperciva# If StrictComponents is not "yes", generate a new components list
2091173564Scperciva# with only the components which appear to be installed.
2092173564Scpercivaupgrade_guess_components () {
2093173564Scperciva	if [ "${STRICTCOMPONENTS}" = "no" ]; then
2094173564Scperciva		# Generate filtered INDEX-ALL with only the components listed
2095173564Scperciva		# in COMPONENTS.
2096173564Scperciva		fetch_filter_metadata_components $1 || return 1
2097173564Scperciva
2098173564Scperciva		# Tell the user why his disk is suddenly making lots of noise
2099173564Scperciva		echo -n "Inspecting system... "
2100173564Scperciva
2101173564Scperciva		# Look at the files on disk, and assume that a component is
2102173564Scperciva		# supposed to be present if it is more than half-present.
2103173564Scperciva		cut -f 1-3 -d '|' < INDEX-ALL |
2104173564Scperciva		    tr '|' ' ' |
2105173564Scperciva		    while read C S F; do
2106173564Scperciva			if [ -e ${BASEDIR}/${F} ]; then
2107173564Scperciva				echo "+ ${C}|${S}"
2108173564Scperciva			fi
2109173564Scperciva			echo "= ${C}|${S}"
2110173564Scperciva		    done |
2111173564Scperciva		    sort |
2112173564Scperciva		    uniq -c |
2113173564Scperciva		    sed -E 's,^ +,,' > compfreq
2114173564Scperciva		grep ' = ' compfreq |
2115173564Scperciva		    cut -f 1,3 -d ' ' |
2116173564Scperciva		    sort -k 2,2 -t ' ' > compfreq.total
2117173564Scperciva		grep ' + ' compfreq |
2118173564Scperciva		    cut -f 1,3 -d ' ' |
2119173564Scperciva		    sort -k 2,2 -t ' ' > compfreq.present
2120173564Scperciva		join -t ' ' -1 2 -2 2 compfreq.present compfreq.total |
2121173564Scperciva		    while read S P T; do
2122173564Scperciva			if [ ${P} -gt `expr ${T} / 2` ]; then
2123173564Scperciva				echo ${S}
2124173564Scperciva			fi
2125173564Scperciva		    done > comp.present
2126173564Scperciva		cut -f 2 -d ' ' < compfreq.total > comp.total
2127173564Scperciva		rm INDEX-ALL compfreq compfreq.total compfreq.present
2128173564Scperciva
2129173564Scperciva		# We're done making noise.
2130173564Scperciva		echo "done."
2131173564Scperciva
2132173564Scperciva		# Sometimes the kernel isn't installed where INDEX-ALL
2133173564Scperciva		# thinks that it should be: In particular, it is often in
2134173564Scperciva		# /boot/kernel instead of /boot/GENERIC or /boot/SMP.  To
2135173564Scperciva		# deal with this, if "kernel|X" is listed in comp.total
2136173564Scperciva		# (i.e., is a component which would be upgraded if it is
2137173564Scperciva		# found to be present) we will add it to comp.present.
2138173564Scperciva		# If "kernel|<anything>" is in comp.total but "kernel|X" is
2139173564Scperciva		# not, we print a warning -- the user is running a kernel
2140173564Scperciva		# which isn't part of the release.
2141173564Scperciva		KCOMP=`echo ${KERNCONF} | tr 'A-Z' 'a-z'`
2142173564Scperciva		grep -E "^kernel\|${KCOMP}\$" comp.total >> comp.present
2143173564Scperciva
2144173564Scperciva		if grep -qE "^kernel\|" comp.total &&
2145173564Scperciva		    ! grep -qE "^kernel\|${KCOMP}\$" comp.total; then
2146173564Scperciva			cat <<-EOF
2147173564Scperciva
2148173564ScpercivaWARNING: This system is running a "${KCOMP}" kernel, which is not a
2149173564Scpercivakernel configuration distributed as part of FreeBSD ${RELNUM}.
2150173564ScpercivaThis kernel will not be updated: you MUST update the kernel manually
2151173564Scpercivabefore running "$0 install".
2152173564Scperciva			EOF
2153173564Scperciva		fi
2154173564Scperciva
2155173564Scperciva		# Re-sort the list of installed components and generate
2156173564Scperciva		# the list of non-installed components.
2157173564Scperciva		sort -u < comp.present > comp.present.tmp
2158173564Scperciva		mv comp.present.tmp comp.present
2159173564Scperciva		comm -13 comp.present comp.total > comp.absent
2160173564Scperciva
2161173564Scperciva		# Ask the user to confirm that what we have is correct.  To
2162173564Scperciva		# reduce user confusion, translate "X|Y" back to "X/Y" (as
2163173564Scperciva		# subcomponents must be listed in the configuration file).
2164173564Scperciva		echo
2165173564Scperciva		echo -n "The following components of FreeBSD "
2166173564Scperciva		echo "seem to be installed:"
2167173564Scperciva		tr '|' '/' < comp.present |
2168173564Scperciva		    fmt -72
2169173564Scperciva		echo
2170173564Scperciva		echo -n "The following components of FreeBSD "
2171173564Scperciva		echo "do not seem to be installed:"
2172173564Scperciva		tr '|' '/' < comp.absent |
2173173564Scperciva		    fmt -72
2174173564Scperciva		echo
2175173564Scperciva		continuep || return 1
2176173564Scperciva		echo
2177173564Scperciva
2178173564Scperciva		# Suck the generated list of components into ${COMPONENTS}.
2179173564Scperciva		# Note that comp.present.tmp is used due to issues with
2180173564Scperciva		# pipelines and setting variables.
2181173564Scperciva		COMPONENTS=""
2182173564Scperciva		tr '|' '/' < comp.present > comp.present.tmp
2183173564Scperciva		while read C; do
2184173564Scperciva			COMPONENTS="${COMPONENTS} ${C}"
2185173564Scperciva		done < comp.present.tmp
2186173564Scperciva
2187173564Scperciva		# Delete temporary files
2188173564Scperciva		rm comp.present comp.present.tmp comp.absent comp.total
2189173564Scperciva	fi
2190173564Scperciva}
2191173564Scperciva
2192173564Scperciva# If StrictComponents is not "yes", COMPONENTS contains an entry
2193173564Scperciva# corresponding to the currently running kernel, and said kernel
2194173564Scperciva# does not exist in the new release, add "kernel/generic" to the
2195173564Scperciva# list of components.
2196173564Scpercivaupgrade_guess_new_kernel () {
2197173564Scperciva	if [ "${STRICTCOMPONENTS}" = "no" ]; then
2198173564Scperciva		# Grab the unfiltered metadata file.
2199173564Scperciva		METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'`
2200173564Scperciva		gunzip -c < files/${METAHASH}.gz > $1.all
2201173564Scperciva
2202173564Scperciva		# If "kernel/${KCOMP}" is in ${COMPONENTS} and that component
2203173564Scperciva		# isn't in $1.all, we need to add kernel/generic.
2204173564Scperciva		for C in ${COMPONENTS}; do
2205173564Scperciva			if [ ${C} = "kernel/${KCOMP}" ] &&
2206173564Scperciva			    ! grep -qE "^kernel\|${KCOMP}\|" $1.all; then
2207173564Scperciva				COMPONENTS="${COMPONENTS} kernel/generic"
2208173564Scperciva				NKERNCONF="GENERIC"
2209173564Scperciva				cat <<-EOF
2210173564Scperciva
2211173564ScpercivaWARNING: This system is running a "${KCOMP}" kernel, which is not a
2212173564Scpercivakernel configuration distributed as part of FreeBSD ${RELNUM}.
2213173564ScpercivaAs part of upgrading to FreeBSD ${RELNUM}, this kernel will be
2214173564Scpercivareplaced with a "generic" kernel.
2215173564Scperciva				EOF
2216173564Scperciva				continuep || return 1
2217173564Scperciva			fi
2218173564Scperciva		done
2219173564Scperciva
2220173564Scperciva		# Don't need this any more...
2221173564Scperciva		rm $1.all
2222173564Scperciva	fi
2223173564Scperciva}
2224173564Scperciva
2225173564Scperciva# Convert INDEX-OLD (last release) and INDEX-ALL (new release) into
2226173564Scperciva# INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades).
2227173564Scpercivaupgrade_oldall_to_oldnew () {
2228173564Scperciva	# For each ${F}|... which appears in INDEX-ALL but does not appear
2229173564Scperciva	# in INDEX-OLD, add ${F}|-|||||| to INDEX-OLD.
2230173564Scperciva	cut -f 1 -d '|' < $1 |
2231173564Scperciva	    sort -u > $1.paths
2232173564Scperciva	cut -f 1 -d '|' < $2 |
2233173564Scperciva	    sort -u |
2234173564Scperciva	    comm -13 $1.paths - |
2235173564Scperciva	    lam - -s "|-||||||" |
2236173564Scperciva	    sort - $1 > $1.tmp
2237173564Scperciva	mv $1.tmp $1
2238173564Scperciva
2239173564Scperciva	# Remove lines from INDEX-OLD which also appear in INDEX-ALL
2240173564Scperciva	comm -23 $1 $2 > $1.tmp
2241173564Scperciva	mv $1.tmp $1
2242173564Scperciva
2243173564Scperciva	# Remove lines from INDEX-ALL which have a file name not appearing
2244173564Scperciva	# anywhere in INDEX-OLD (since these must be files which haven't
2245173564Scperciva	# changed -- if they were new, there would be an entry of type "-").
2246173564Scperciva	cut -f 1 -d '|' < $1 |
2247173564Scperciva	    sort -u > $1.paths
2248173564Scperciva	sort -k 1,1 -t '|' < $2 |
2249173564Scperciva	    join -t '|' - $1.paths |
2250173564Scperciva	    sort > $2.tmp
2251173564Scperciva	rm $1.paths
2252173564Scperciva	mv $2.tmp $2
2253173564Scperciva
2254173564Scperciva	# Rename INDEX-ALL to INDEX-NEW.
2255173564Scperciva	mv $2 $3
2256173564Scperciva}
2257173564Scperciva
2258221780Scperciva# Helper for upgrade_merge: Return zero true iff the two files differ only
2259221780Scperciva# in the contents of their $FreeBSD: releng/9.3/usr.sbin/freebsd-update/freebsd-update.sh 276157 2014-12-23 22:54:25Z des $ tags.
2260221780Scpercivasamef () {
2261221780Scperciva	X=`sed -E 's/\\$FreeBSD.*\\$/\$FreeBSD\$/' < $1 | ${SHA256}`
2262221780Scperciva	Y=`sed -E 's/\\$FreeBSD.*\\$/\$FreeBSD\$/' < $2 | ${SHA256}`
2263221780Scperciva
2264221780Scperciva	if [ $X = $Y ]; then
2265221780Scperciva		return 0;
2266221780Scperciva	else
2267221780Scperciva		return 1;
2268221780Scperciva	fi
2269221780Scperciva}
2270221780Scperciva
2271173564Scperciva# From the list of "old" files in $1, merge changes in $2 with those in $3,
2272173564Scperciva# and update $3 to reflect the hashes of merged files.
2273173564Scpercivaupgrade_merge () {
2274173564Scperciva	# We only need to do anything if $1 is non-empty.
2275173564Scperciva	if [ -s $1 ]; then
2276173564Scperciva		cut -f 1 -d '|' $1 |
2277173564Scperciva		    sort > $1-paths
2278173564Scperciva
2279173564Scperciva		# Create staging area for merging files
2280173564Scperciva		rm -rf merge/
2281173564Scperciva		while read F; do
2282173564Scperciva			D=`dirname ${F}`
2283173564Scperciva			mkdir -p merge/old/${D}
2284173564Scperciva			mkdir -p merge/${OLDRELNUM}/${D}
2285173564Scperciva			mkdir -p merge/${RELNUM}/${D}
2286173564Scperciva			mkdir -p merge/new/${D}
2287173564Scperciva		done < $1-paths
2288173564Scperciva
2289173564Scperciva		# Copy in files
2290173564Scperciva		while read F; do
2291173564Scperciva			# Currently installed file
2292173564Scperciva			V=`look "${F}|" $2 | cut -f 7 -d '|'`
2293173564Scperciva			gunzip < files/${V}.gz > merge/old/${F}
2294173564Scperciva
2295173564Scperciva			# Old release
2296173564Scperciva			if look "${F}|" $1 | fgrep -q "|f|"; then
2297173564Scperciva				V=`look "${F}|" $1 | cut -f 3 -d '|'`
2298173564Scperciva				gunzip < files/${V}.gz		\
2299173564Scperciva				    > merge/${OLDRELNUM}/${F}
2300173564Scperciva			fi
2301173564Scperciva
2302173564Scperciva			# New release
2303173564Scperciva			if look "${F}|" $3 | cut -f 1,2,7 -d '|' |
2304173564Scperciva			    fgrep -q "|f|"; then
2305173564Scperciva				V=`look "${F}|" $3 | cut -f 7 -d '|'`
2306173564Scperciva				gunzip < files/${V}.gz		\
2307173564Scperciva				    > merge/${RELNUM}/${F}
2308173564Scperciva			fi
2309173564Scperciva		done < $1-paths
2310173564Scperciva
2311173564Scperciva		# Attempt to automatically merge changes
2312173564Scperciva		echo -n "Attempting to automatically merge "
2313173564Scperciva		echo -n "changes in files..."
2314173564Scperciva		: > failed.merges
2315173564Scperciva		while read F; do
2316173564Scperciva			# If the file doesn't exist in the new release,
2317173564Scperciva			# the result of "merging changes" is having the file
2318173564Scperciva			# not exist.
2319173564Scperciva			if ! [ -f merge/${RELNUM}/${F} ]; then
2320173564Scperciva				continue
2321173564Scperciva			fi
2322173564Scperciva
2323173564Scperciva			# If the file didn't exist in the old release, we're
2324173564Scperciva			# going to throw away the existing file and hope that
2325173564Scperciva			# the version from the new release is what we want.
2326173564Scperciva			if ! [ -f merge/${OLDRELNUM}/${F} ]; then
2327173564Scperciva				cp merge/${RELNUM}/${F} merge/new/${F}
2328173564Scperciva				continue
2329173564Scperciva			fi
2330173564Scperciva
2331173564Scperciva			# Some files need special treatment.
2332173564Scperciva			case ${F} in
2333173564Scperciva			/etc/spwd.db | /etc/pwd.db | /etc/login.conf.db)
2334173564Scperciva				# Don't merge these -- we're rebuild them
2335173564Scperciva				# after updates are installed.
2336173564Scperciva				cp merge/old/${F} merge/new/${F}
2337173564Scperciva				;;
2338173564Scperciva			*)
2339173564Scperciva				if ! merge -p -L "current version"	\
2340173564Scperciva				    -L "${OLDRELNUM}" -L "${RELNUM}"	\
2341173564Scperciva				    merge/old/${F}			\
2342173564Scperciva				    merge/${OLDRELNUM}/${F}		\
2343173564Scperciva				    merge/${RELNUM}/${F}		\
2344173564Scperciva				    > merge/new/${F} 2>/dev/null; then
2345173564Scperciva					echo ${F} >> failed.merges
2346173564Scperciva				fi
2347173564Scperciva				;;
2348173564Scperciva			esac
2349173564Scperciva		done < $1-paths
2350173564Scperciva		echo " done."
2351173564Scperciva
2352173564Scperciva		# Ask the user to handle any files which didn't merge.
2353173564Scperciva		while read F; do
2354221780Scperciva			# If the installed file differs from the version in
2355221780Scperciva			# the old release only due to $FreeBSD: releng/9.3/usr.sbin/freebsd-update/freebsd-update.sh 276157 2014-12-23 22:54:25Z des $ tag expansion
2356221780Scperciva			# then just use the version in the new release.
2357221780Scperciva			if samef merge/old/${F} merge/${OLDRELNUM}/${F}; then
2358221780Scperciva				cp merge/${RELNUM}/${F} merge/new/${F}
2359221780Scperciva				continue
2360221780Scperciva			fi
2361221780Scperciva
2362173564Scperciva			cat <<-EOF
2363173564Scperciva
2364173564ScpercivaThe following file could not be merged automatically: ${F}
2365173564ScpercivaPress Enter to edit this file in ${EDITOR} and resolve the conflicts
2366173564Scpercivamanually...
2367173564Scperciva			EOF
2368173564Scperciva			read dummy </dev/tty
2369173564Scperciva			${EDITOR} `pwd`/merge/new/${F} < /dev/tty
2370173564Scperciva		done < failed.merges
2371173564Scperciva		rm failed.merges
2372173564Scperciva
2373173564Scperciva		# Ask the user to confirm that he likes how the result
2374173564Scperciva		# of merging files.
2375173564Scperciva		while read F; do
2376221780Scperciva			# Skip files which haven't changed except possibly
2377221780Scperciva			# in their $FreeBSD: releng/9.3/usr.sbin/freebsd-update/freebsd-update.sh 276157 2014-12-23 22:54:25Z des $ tags.
2378221780Scperciva			if [ -f merge/old/${F} ] && [ -f merge/new/${F} ] &&
2379221780Scperciva			    samef merge/old/${F} merge/new/${F}; then
2380173564Scperciva				continue
2381173564Scperciva			fi
2382173564Scperciva
2383221780Scperciva			# Skip files where the installed file differs from
2384221780Scperciva			# the old file only due to $FreeBSD: releng/9.3/usr.sbin/freebsd-update/freebsd-update.sh 276157 2014-12-23 22:54:25Z des $ tags.
2385221780Scperciva			if [ -f merge/old/${F} ] &&
2386221780Scperciva			    [ -f merge/${OLDRELNUM}/${F} ] &&
2387221780Scperciva			    samef merge/old/${F} merge/${OLDRELNUM}/${F}; then
2388221780Scperciva				continue
2389221780Scperciva			fi
2390221780Scperciva
2391173564Scperciva			# Warn about files which are ceasing to exist.
2392173564Scperciva			if ! [ -f merge/new/${F} ]; then
2393173564Scperciva				cat <<-EOF
2394173564Scperciva
2395173564ScpercivaThe following file will be removed, as it no longer exists in
2396173564ScpercivaFreeBSD ${RELNUM}: ${F}
2397173564Scperciva				EOF
2398173564Scperciva				continuep < /dev/tty || return 1
2399173564Scperciva				continue
2400173564Scperciva			fi
2401173564Scperciva
2402173564Scperciva			# Print changes for the user's approval.
2403173564Scperciva			cat <<-EOF
2404173564Scperciva
2405173564ScpercivaThe following changes, which occurred between FreeBSD ${OLDRELNUM} and
2406173564ScpercivaFreeBSD ${RELNUM} have been merged into ${F}:
2407173564ScpercivaEOF
2408173564Scperciva			diff -U 5 -L "current version" -L "new version"	\
2409173564Scperciva			    merge/old/${F} merge/new/${F} || true
2410173564Scperciva			continuep < /dev/tty || return 1
2411173564Scperciva		done < $1-paths
2412173564Scperciva
2413173564Scperciva		# Store merged files.
2414173564Scperciva		while read F; do
2415177527Scperciva			if [ -f merge/new/${F} ]; then
2416177527Scperciva				V=`${SHA256} -q merge/new/${F}`
2417173564Scperciva
2418173564Scperciva				gzip -c < merge/new/${F} > files/${V}.gz
2419173564Scperciva				echo "${F}|${V}"
2420173564Scperciva			fi
2421173564Scperciva		done < $1-paths > newhashes
2422173564Scperciva
2423173564Scperciva		# Pull lines out from $3 which need to be updated to
2424173564Scperciva		# reflect merged files.
2425173564Scperciva		while read F; do
2426173564Scperciva			look "${F}|" $3
2427173564Scperciva		done < $1-paths > $3-oldlines
2428173564Scperciva
2429173564Scperciva		# Update lines to reflect merged files
2430173564Scperciva		join -t '|' -o 1.1,1.2,1.3,1.4,1.5,1.6,2.2,1.8		\
2431173564Scperciva		    $3-oldlines newhashes > $3-newlines
2432173564Scperciva
2433173564Scperciva		# Remove old lines from $3 and add new lines.
2434173564Scperciva		sort $3-oldlines |
2435173564Scperciva		    comm -13 - $3 |
2436173564Scperciva		    sort - $3-newlines > $3.tmp
2437173564Scperciva		mv $3.tmp $3
2438173564Scperciva
2439173564Scperciva		# Clean up
2440173564Scperciva		rm $1-paths newhashes $3-oldlines $3-newlines
2441173564Scperciva		rm -rf merge/
2442173564Scperciva	fi
2443173564Scperciva
2444173564Scperciva	# We're done with merging files.
2445173564Scperciva	rm $1
2446173564Scperciva}
2447173564Scperciva
2448173564Scperciva# Do the work involved in fetching upgrades to a new release
2449173564Scpercivaupgrade_run () {
2450173564Scperciva	workdir_init || return 1
2451173564Scperciva
2452173564Scperciva	# Prepare the mirror list.
2453173564Scperciva	fetch_pick_server_init && fetch_pick_server
2454173564Scperciva
2455173564Scperciva	# Try to fetch the public key until we run out of servers.
2456173564Scperciva	while ! fetch_key; do
2457173564Scperciva		fetch_pick_server || return 1
2458173564Scperciva	done
2459173564Scperciva 
2460173564Scperciva	# Try to fetch the metadata index signature ("tag") until we run
2461173564Scperciva	# out of available servers; and sanity check the downloaded tag.
2462173564Scperciva	while ! fetch_tag; do
2463173564Scperciva		fetch_pick_server || return 1
2464173564Scperciva	done
2465173564Scperciva	fetch_tagsanity || return 1
2466173564Scperciva
2467173564Scperciva	# Fetch the INDEX-OLD and INDEX-ALL.
2468173564Scperciva	fetch_metadata INDEX-OLD INDEX-ALL || return 1
2469173564Scperciva
2470173564Scperciva	# If StrictComponents is not "yes", generate a new components list
2471173564Scperciva	# with only the components which appear to be installed.
2472173564Scperciva	upgrade_guess_components INDEX-ALL || return 1
2473173564Scperciva
2474173564Scperciva	# Generate filtered INDEX-OLD and INDEX-ALL files containing only
2475173564Scperciva	# the components we want and without anything marked as "Ignore".
2476173564Scperciva	fetch_filter_metadata INDEX-OLD || return 1
2477173564Scperciva	fetch_filter_metadata INDEX-ALL || return 1
2478173564Scperciva
2479173564Scperciva	# Merge the INDEX-OLD and INDEX-ALL files into INDEX-OLD.
2480173564Scperciva	sort INDEX-OLD INDEX-ALL > INDEX-OLD.tmp
2481173564Scperciva	mv INDEX-OLD.tmp INDEX-OLD
2482173564Scperciva	rm INDEX-ALL
2483173564Scperciva
2484173564Scperciva	# Adjust variables for fetching files from the new release.
2485173564Scperciva	OLDRELNUM=${RELNUM}
2486173564Scperciva	RELNUM=${TARGETRELEASE}
2487173564Scperciva	OLDFETCHDIR=${FETCHDIR}
2488173564Scperciva	FETCHDIR=${RELNUM}/${ARCH}
2489173564Scperciva
2490173564Scperciva	# Try to fetch the NEW metadata index signature ("tag") until we run
2491173564Scperciva	# out of available servers; and sanity check the downloaded tag.
2492173564Scperciva	while ! fetch_tag; do
2493173564Scperciva		fetch_pick_server || return 1
2494173564Scperciva	done
2495173564Scperciva
2496173564Scperciva	# Fetch the new INDEX-ALL.
2497173564Scperciva	fetch_metadata INDEX-ALL || return 1
2498173564Scperciva
2499173564Scperciva	# If StrictComponents is not "yes", COMPONENTS contains an entry
2500173564Scperciva	# corresponding to the currently running kernel, and said kernel
2501173564Scperciva	# does not exist in the new release, add "kernel/generic" to the
2502173564Scperciva	# list of components.
2503173564Scperciva	upgrade_guess_new_kernel INDEX-ALL || return 1
2504173564Scperciva
2505173564Scperciva	# Filter INDEX-ALL to contain only the components we want and without
2506173564Scperciva	# anything marked as "Ignore".
2507173564Scperciva	fetch_filter_metadata INDEX-ALL || return 1
2508173564Scperciva
2509173564Scperciva	# Convert INDEX-OLD (last release) and INDEX-ALL (new release) into
2510173564Scperciva	# INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades).
2511173564Scperciva	upgrade_oldall_to_oldnew INDEX-OLD INDEX-ALL INDEX-NEW
2512173564Scperciva
2513173564Scperciva	# Translate /boot/${KERNCONF} or /boot/${NKERNCONF} into ${KERNELDIR}
2514173564Scperciva	fetch_filter_kernel_names INDEX-NEW ${NKERNCONF}
2515173564Scperciva	fetch_filter_kernel_names INDEX-OLD ${KERNCONF}
2516173564Scperciva
2517173564Scperciva	# For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the
2518173564Scperciva	# system and generate an INDEX-PRESENT file.
2519173564Scperciva	fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2520173564Scperciva
2521173564Scperciva	# Based on ${MERGECHANGES}, generate a file tomerge-old with the
2522173564Scperciva	# paths and hashes of old versions of files to merge.
2523173564Scperciva	fetch_filter_mergechanges INDEX-OLD INDEX-PRESENT tomerge-old
2524173564Scperciva
2525173564Scperciva	# Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which
2526173564Scperciva	# correspond to lines in INDEX-PRESENT with hashes not appearing
2527173564Scperciva	# in INDEX-OLD or INDEX-NEW.  Also remove lines where the entry in
2528173564Scperciva	# INDEX-PRESENT has type - and there isn't a corresponding entry in
2529173564Scperciva	# INDEX-OLD with type -.
2530173564Scperciva	fetch_filter_unmodified_notpresent	\
2531173564Scperciva	    INDEX-OLD INDEX-PRESENT INDEX-NEW tomerge-old
2532173564Scperciva
2533173564Scperciva	# For each entry in INDEX-PRESENT of type -, remove any corresponding
2534173564Scperciva	# entry from INDEX-NEW if ${ALLOWADD} != "yes".  Remove all entries
2535173564Scperciva	# of type - from INDEX-PRESENT.
2536173564Scperciva	fetch_filter_allowadd INDEX-PRESENT INDEX-NEW
2537173564Scperciva
2538173564Scperciva	# If ${ALLOWDELETE} != "yes", then remove any entries from
2539173564Scperciva	# INDEX-PRESENT which don't correspond to entries in INDEX-NEW.
2540173564Scperciva	fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW
2541173564Scperciva
2542173564Scperciva	# If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in
2543173564Scperciva	# INDEX-PRESENT with metadata not matching any entry in INDEX-OLD,
2544173564Scperciva	# replace the corresponding line of INDEX-NEW with one having the
2545173564Scperciva	# same metadata as the entry in INDEX-PRESENT.
2546173564Scperciva	fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW
2547173564Scperciva
2548173564Scperciva	# Remove lines from INDEX-PRESENT and INDEX-NEW which are identical;
2549173564Scperciva	# no need to update a file if it isn't changing.
2550173564Scperciva	fetch_filter_uptodate INDEX-PRESENT INDEX-NEW
2551173564Scperciva
2552173564Scperciva	# Fetch "clean" files from the old release for merging changes.
2553173564Scperciva	fetch_files_premerge tomerge-old
2554173564Scperciva
2555173564Scperciva	# Prepare to fetch files: Generate a list of the files we need,
2556173564Scperciva	# copy the unmodified files we have into /files/, and generate
2557173564Scperciva	# a list of patches to download.
2558173564Scperciva	fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2559173564Scperciva
2560173564Scperciva	# Fetch patches from to-${RELNUM}/${ARCH}/bp/
2561173564Scperciva	PATCHDIR=to-${RELNUM}/${ARCH}/bp
2562173564Scperciva	fetch_files || return 1
2563173564Scperciva
2564173564Scperciva	# Merge configuration file changes.
2565173564Scperciva	upgrade_merge tomerge-old INDEX-PRESENT INDEX-NEW || return 1
2566173564Scperciva
2567173564Scperciva	# Create and populate install manifest directory; and report what
2568173564Scperciva	# updates are available.
2569173564Scperciva	fetch_create_manifest || return 1
2570173564Scperciva
2571173564Scperciva	# Leave a note behind to tell the "install" command that the kernel
2572173564Scperciva	# needs to be installed before the world.
2573173564Scperciva	touch ${BDHASH}-install/kernelfirst
2574212431Scperciva
2575212431Scperciva	# Remind the user that they need to run "freebsd-update install"
2576212431Scperciva	# to install the downloaded bits, in case they didn't RTFM.
2577212431Scperciva	echo "To install the downloaded upgrades, run \"$0 install\"."
2578173564Scperciva}
2579173564Scperciva
2580161748Scperciva# Make sure that all the file hashes mentioned in $@ have corresponding
2581161748Scperciva# gzipped files stored in /files/.
2582161748Scpercivainstall_verify () {
2583161748Scperciva	# Generate a list of hashes
2584161748Scperciva	cat $@ |
2585161748Scperciva	    cut -f 2,7 -d '|' |
2586161748Scperciva	    grep -E '^f' |
2587161748Scperciva	    cut -f 2 -d '|' |
2588161748Scperciva	    sort -u > filelist
2589161748Scperciva
2590161748Scperciva	# Make sure all the hashes exist
2591161748Scperciva	while read HASH; do
2592161748Scperciva		if ! [ -f files/${HASH}.gz ]; then
2593161748Scperciva			echo -n "Update files missing -- "
2594161748Scperciva			echo "this should never happen."
2595161748Scperciva			echo "Re-run '$0 fetch'."
2596161748Scperciva			return 1
2597161748Scperciva		fi
2598161748Scperciva	done < filelist
2599161748Scperciva
2600161748Scperciva	# Clean up
2601161748Scperciva	rm filelist
2602161748Scperciva}
2603161748Scperciva
2604161748Scperciva# Remove the system immutable flag from files
2605161748Scpercivainstall_unschg () {
2606161748Scperciva	# Generate file list
2607161748Scperciva	cat $@ |
2608161748Scperciva	    cut -f 1 -d '|' > filelist
2609161748Scperciva
2610161748Scperciva	# Remove flags
2611161748Scperciva	while read F; do
2612169603Scperciva		if ! [ -e ${BASEDIR}/${F} ]; then
2613161748Scperciva			continue
2614161748Scperciva		fi
2615161748Scperciva
2616169603Scperciva		chflags noschg ${BASEDIR}/${F} || return 1
2617161748Scperciva	done < filelist
2618161748Scperciva
2619161748Scperciva	# Clean up
2620161748Scperciva	rm filelist
2621161748Scperciva}
2622161748Scperciva
2623196392Ssimon# Decide which directory name to use for kernel backups.
2624196392Ssimonbackup_kernel_finddir () {
2625196392Ssimon	CNT=0
2626196392Ssimon	while true ; do
2627196392Ssimon		# Pathname does not exist, so it is OK use that name
2628196392Ssimon		# for backup directory.
2629196392Ssimon		if [ ! -e $BACKUPKERNELDIR ]; then
2630196392Ssimon			return 0
2631196392Ssimon		fi
2632196392Ssimon
2633196392Ssimon		# If directory do exist, we only use if it has our
2634196392Ssimon		# marker file.
2635196392Ssimon		if [ -d $BACKUPKERNELDIR -a \
2636196392Ssimon			-e $BACKUPKERNELDIR/.freebsd-update ]; then
2637196392Ssimon			return 0
2638196392Ssimon		fi
2639196392Ssimon
2640196392Ssimon		# We could not use current directory name, so add counter to
2641196392Ssimon		# the end and try again.
2642196392Ssimon		CNT=$((CNT + 1))
2643196392Ssimon		if [ $CNT -gt 9 ]; then
2644196392Ssimon			echo "Could not find valid backup dir ($BACKUPKERNELDIR)"
2645196392Ssimon			exit 1
2646196392Ssimon		fi
2647196392Ssimon		BACKUPKERNELDIR="`echo $BACKUPKERNELDIR | sed -Ee 's/[0-9]\$//'`"
2648196392Ssimon		BACKUPKERNELDIR="${BACKUPKERNELDIR}${CNT}"
2649196392Ssimon	done
2650196392Ssimon}
2651196392Ssimon
2652196392Ssimon# Backup the current kernel using hardlinks, if not disabled by user.
2653196392Ssimon# Since we delete all files in the directory used for previous backups
2654196392Ssimon# we create a marker file called ".freebsd-update" in the directory so
2655196392Ssimon# we can determine on the next run that the directory was created by
2656196392Ssimon# freebsd-update and we then do not accidentally remove user files in
2657196392Ssimon# the unlikely case that the user has created a directory with a
2658196392Ssimon# conflicting name.
2659196392Ssimonbackup_kernel () {
2660196392Ssimon	# Only make kernel backup is so configured.
2661196392Ssimon	if [ $BACKUPKERNEL != yes ]; then
2662196392Ssimon		return 0
2663196392Ssimon	fi
2664196392Ssimon
2665196392Ssimon	# Decide which directory name to use for kernel backups.
2666196392Ssimon	backup_kernel_finddir
2667196392Ssimon
2668196392Ssimon	# Remove old kernel backup files.  If $BACKUPKERNELDIR was
2669196392Ssimon	# "not ours", backup_kernel_finddir would have exited, so
2670196392Ssimon	# deleting the directory content is as safe as we can make it.
2671196392Ssimon	if [ -d $BACKUPKERNELDIR ]; then
2672212505Sjh		rm -fr $BACKUPKERNELDIR
2673196392Ssimon	fi
2674196392Ssimon
2675212505Sjh	# Create directories for backup.
2676196392Ssimon	mkdir -p $BACKUPKERNELDIR
2677212505Sjh	mtree -cdn -p "${KERNELDIR}" | \
2678212505Sjh	    mtree -Ue -p "${BACKUPKERNELDIR}" > /dev/null
2679196392Ssimon
2680196392Ssimon	# Mark the directory as having been created by freebsd-update.
2681196392Ssimon	touch $BACKUPKERNELDIR/.freebsd-update
2682196392Ssimon	if [ $? -ne 0 ]; then
2683196392Ssimon		echo "Could not create kernel backup directory"
2684196392Ssimon		exit 1
2685196392Ssimon	fi
2686196392Ssimon
2687196392Ssimon	# Disable pathname expansion to be sure *.symbols is not
2688196392Ssimon	# expanded.
2689196392Ssimon	set -f
2690196392Ssimon
2691196392Ssimon	# Use find to ignore symbol files, unless disabled by user.
2692196392Ssimon	if [ $BACKUPKERNELSYMBOLFILES = yes ]; then
2693196392Ssimon		FINDFILTER=""
2694196392Ssimon	else
2695196392Ssimon		FINDFILTER=-"a ! -name *.symbols"
2696196392Ssimon	fi
2697196392Ssimon
2698196392Ssimon	# Backup all the kernel files using hardlinks.
2699212505Sjh	(cd $KERNELDIR && find . -type f $FINDFILTER -exec \
2700212505Sjh	    cp -pl '{}' ${BACKUPKERNELDIR}/'{}' \;)
2701196392Ssimon
2702196392Ssimon	# Re-enable patchname expansion.
2703196392Ssimon	set +f
2704196392Ssimon}
2705196392Ssimon
2706161748Scperciva# Install new files
2707161748Scpercivainstall_from_index () {
2708161748Scperciva	# First pass: Do everything apart from setting file flags.  We
2709161748Scperciva	# can't set flags yet, because schg inhibits hard linking.
2710161748Scperciva	sort -k 1,1 -t '|' $1 |
2711161748Scperciva	    tr '|' ' ' |
2712161748Scperciva	    while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
2713161748Scperciva		case ${TYPE} in
2714161748Scperciva		d)
2715161748Scperciva			# Create a directory
2716161748Scperciva			install -d -o ${OWNER} -g ${GROUP}		\
2717161748Scperciva			    -m ${PERM} ${BASEDIR}/${FPATH}
2718161748Scperciva			;;
2719161748Scperciva		f)
2720161748Scperciva			if [ -z "${LINK}" ]; then
2721161748Scperciva				# Create a file, without setting flags.
2722161748Scperciva				gunzip < files/${HASH}.gz > ${HASH}
2723161748Scperciva				install -S -o ${OWNER} -g ${GROUP}	\
2724161748Scperciva				    -m ${PERM} ${HASH} ${BASEDIR}/${FPATH}
2725161748Scperciva				rm ${HASH}
2726161748Scperciva			else
2727161748Scperciva				# Create a hard link.
2728169603Scperciva				ln -f ${BASEDIR}/${LINK} ${BASEDIR}/${FPATH}
2729161748Scperciva			fi
2730161748Scperciva			;;
2731161748Scperciva		L)
2732161748Scperciva			# Create a symlink
2733161748Scperciva			ln -sfh ${HASH} ${BASEDIR}/${FPATH}
2734161748Scperciva			;;
2735161748Scperciva		esac
2736161748Scperciva	    done
2737161748Scperciva
2738161748Scperciva	# Perform a second pass, adding file flags.
2739161748Scperciva	tr '|' ' ' < $1 |
2740161748Scperciva	    while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
2741161748Scperciva		if [ ${TYPE} = "f" ] &&
2742161748Scperciva		    ! [ ${FLAGS} = "0" ]; then
2743161748Scperciva			chflags ${FLAGS} ${BASEDIR}/${FPATH}
2744161748Scperciva		fi
2745161748Scperciva	    done
2746161748Scperciva}
2747161748Scperciva
2748161748Scperciva# Remove files which we want to delete
2749161748Scpercivainstall_delete () {
2750161748Scperciva	# Generate list of new files
2751161748Scperciva	cut -f 1 -d '|' < $2 |
2752161748Scperciva	    sort > newfiles
2753161748Scperciva
2754161748Scperciva	# Generate subindex of old files we want to nuke
2755161748Scperciva	sort -k 1,1 -t '|' $1 |
2756161748Scperciva	    join -t '|' -v 1 - newfiles |
2757164600Scperciva	    sort -r -k 1,1 -t '|' |
2758161748Scperciva	    cut -f 1,2 -d '|' |
2759161748Scperciva	    tr '|' ' ' > killfiles
2760161748Scperciva
2761161748Scperciva	# Remove the offending bits
2762161748Scperciva	while read FPATH TYPE; do
2763161748Scperciva		case ${TYPE} in
2764161748Scperciva		d)
2765161748Scperciva			rmdir ${BASEDIR}/${FPATH}
2766161748Scperciva			;;
2767161748Scperciva		f)
2768161748Scperciva			rm ${BASEDIR}/${FPATH}
2769161748Scperciva			;;
2770161748Scperciva		L)
2771161748Scperciva			rm ${BASEDIR}/${FPATH}
2772161748Scperciva			;;
2773161748Scperciva		esac
2774161748Scperciva	done < killfiles
2775161748Scperciva
2776161748Scperciva	# Clean up
2777161748Scperciva	rm newfiles killfiles
2778161748Scperciva}
2779161748Scperciva
2780173564Scperciva# Install new files, delete old files, and update linker.hints
2781173564Scpercivainstall_files () {
2782173564Scperciva	# If we haven't already dealt with the kernel, deal with it.
2783173564Scperciva	if ! [ -f $1/kerneldone ]; then
2784173564Scperciva		grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD
2785173564Scperciva		grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW
2786173564Scperciva
2787196392Ssimon		# Backup current kernel before installing a new one
2788196392Ssimon		backup_kernel || return 1
2789196392Ssimon
2790173564Scperciva		# Install new files
2791173564Scperciva		install_from_index INDEX-NEW || return 1
2792173564Scperciva
2793173564Scperciva		# Remove files which need to be deleted
2794173564Scperciva		install_delete INDEX-OLD INDEX-NEW || return 1
2795173564Scperciva
2796173564Scperciva		# Update linker.hints if necessary
2797173564Scperciva		if [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2798173564Scperciva			kldxref -R /boot/ 2>/dev/null
2799173564Scperciva		fi
2800173564Scperciva
2801173564Scperciva		# We've finished updating the kernel.
2802173564Scperciva		touch $1/kerneldone
2803173564Scperciva
2804173564Scperciva		# Do we need to ask for a reboot now?
2805173564Scperciva		if [ -f $1/kernelfirst ] &&
2806173564Scperciva		    [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2807173564Scperciva			cat <<-EOF
2808173564Scperciva
2809173564ScpercivaKernel updates have been installed.  Please reboot and run
2810173564Scperciva"$0 install" again to finish installing updates.
2811173564Scperciva			EOF
2812173564Scperciva			exit 0
2813173564Scperciva		fi
2814161748Scperciva	fi
2815173564Scperciva
2816173564Scperciva	# If we haven't already dealt with the world, deal with it.
2817173564Scperciva	if ! [ -f $1/worlddone ]; then
2818257192Sdelphij		# Create any necessary directories first
2819257192Sdelphij		grep -vE '^/boot/' $1/INDEX-NEW |
2820257192Sdelphij		    grep -E '^[^|]+\|d\|' > INDEX-NEW
2821257192Sdelphij		install_from_index INDEX-NEW || return 1
2822257192Sdelphij
2823173564Scperciva		# Install new shared libraries next
2824173564Scperciva		grep -vE '^/boot/' $1/INDEX-NEW |
2825257192Sdelphij		    grep -vE '^[^|]+\|d\|' |
2826257192Sdelphij		    grep -E '^[^|]*/lib/[^|]*\.so\.[0-9]+\|' > INDEX-NEW
2827173564Scperciva		install_from_index INDEX-NEW || return 1
2828173564Scperciva
2829173564Scperciva		# Deal with everything else
2830173564Scperciva		grep -vE '^/boot/' $1/INDEX-OLD |
2831257192Sdelphij		    grep -vE '^[^|]+\|d\|' |
2832257192Sdelphij		    grep -vE '^[^|]*/lib/[^|]*\.so\.[0-9]+\|' > INDEX-OLD
2833173564Scperciva		grep -vE '^/boot/' $1/INDEX-NEW |
2834257192Sdelphij		    grep -vE '^[^|]+\|d\|' |
2835257192Sdelphij		    grep -vE '^[^|]*/lib/[^|]*\.so\.[0-9]+\|' > INDEX-NEW
2836173564Scperciva		install_from_index INDEX-NEW || return 1
2837173564Scperciva		install_delete INDEX-OLD INDEX-NEW || return 1
2838173564Scperciva
2839173564Scperciva		# Rebuild /etc/spwd.db and /etc/pwd.db if necessary.
2840173564Scperciva		if [ /etc/master.passwd -nt /etc/spwd.db ] ||
2841173564Scperciva		    [ /etc/master.passwd -nt /etc/pwd.db ]; then
2842173564Scperciva			pwd_mkdb /etc/master.passwd
2843173564Scperciva		fi
2844173564Scperciva
2845173564Scperciva		# Rebuild /etc/login.conf.db if necessary.
2846173564Scperciva		if [ /etc/login.conf -nt /etc/login.conf.db ]; then
2847173564Scperciva			cap_mkdb /etc/login.conf
2848173564Scperciva		fi
2849173564Scperciva
2850173564Scperciva		# We've finished installing the world and deleting old files
2851173564Scperciva		# which are not shared libraries.
2852173564Scperciva		touch $1/worlddone
2853173564Scperciva
2854173564Scperciva		# Do we need to ask the user to portupgrade now?
2855173564Scperciva		grep -vE '^/boot/' $1/INDEX-NEW |
2856257192Sdelphij		    grep -E '^[^|]*/lib/[^|]*\.so\.[0-9]+\|' |
2857173564Scperciva		    cut -f 1 -d '|' |
2858173564Scperciva		    sort > newfiles
2859173564Scperciva		if grep -vE '^/boot/' $1/INDEX-OLD |
2860257192Sdelphij		    grep -E '^[^|]*/lib/[^|]*\.so\.[0-9]+\|' |
2861173564Scperciva		    cut -f 1 -d '|' |
2862173564Scperciva		    sort |
2863173564Scperciva		    join -v 1 - newfiles |
2864173564Scperciva		    grep -q .; then
2865173564Scperciva			cat <<-EOF
2866173564Scperciva
2867173564ScpercivaCompleting this upgrade requires removing old shared object files.
2868173564ScpercivaPlease rebuild all installed 3rd party software (e.g., programs
2869173564Scpercivainstalled from the ports tree) and then run "$0 install"
2870173564Scpercivaagain to finish installing updates.
2871173564Scperciva			EOF
2872173564Scperciva			rm newfiles
2873173564Scperciva			exit 0
2874173564Scperciva		fi
2875173564Scperciva		rm newfiles
2876173564Scperciva	fi
2877173564Scperciva
2878173564Scperciva	# Remove old shared libraries
2879173564Scperciva	grep -vE '^/boot/' $1/INDEX-NEW |
2880257192Sdelphij	    grep -vE '^[^|]+\|d\|' |
2881257192Sdelphij	    grep -E '^[^|]*/lib/[^|]*\.so\.[0-9]+\|' > INDEX-NEW
2882173564Scperciva	grep -vE '^/boot/' $1/INDEX-OLD |
2883257192Sdelphij	    grep -vE '^[^|]+\|d\|' |
2884257192Sdelphij	    grep -E '^[^|]*/lib/[^|]*\.so\.[0-9]+\|' > INDEX-OLD
2885173564Scperciva	install_delete INDEX-OLD INDEX-NEW || return 1
2886173564Scperciva
2887257192Sdelphij	# Remove old directories
2888258724Sdelphij	grep -vE '^/boot/' $1/INDEX-NEW |
2889258724Sdelphij	    grep -E '^[^|]+\|d\|' > INDEX-NEW
2890257192Sdelphij	grep -vE '^/boot/' $1/INDEX-OLD |
2891257192Sdelphij	    grep -E '^[^|]+\|d\|' > INDEX-OLD
2892257192Sdelphij	install_delete INDEX-OLD INDEX-NEW || return 1
2893257192Sdelphij
2894173564Scperciva	# Remove temporary files
2895173564Scperciva	rm INDEX-OLD INDEX-NEW
2896161748Scperciva}
2897161748Scperciva
2898161748Scperciva# Rearrange bits to allow the installed updates to be rolled back
2899161748Scpercivainstall_setup_rollback () {
2900173564Scperciva	# Remove the "reboot after installing kernel", "kernel updated", and
2901173564Scperciva	# "finished installing the world" flags if present -- they are
2902173564Scperciva	# irrelevant when rolling back updates.
2903173564Scperciva	if [ -f ${BDHASH}-install/kernelfirst ]; then
2904173564Scperciva		rm ${BDHASH}-install/kernelfirst
2905173564Scperciva		rm ${BDHASH}-install/kerneldone
2906173564Scperciva	fi
2907173564Scperciva	if [ -f ${BDHASH}-install/worlddone ]; then
2908173564Scperciva		rm ${BDHASH}-install/worlddone
2909173564Scperciva	fi
2910173564Scperciva
2911161748Scperciva	if [ -L ${BDHASH}-rollback ]; then
2912161748Scperciva		mv ${BDHASH}-rollback ${BDHASH}-install/rollback
2913161748Scperciva	fi
2914161748Scperciva
2915161748Scperciva	mv ${BDHASH}-install ${BDHASH}-rollback
2916161748Scperciva}
2917161748Scperciva
2918161748Scperciva# Actually install updates
2919161748Scpercivainstall_run () {
2920161748Scperciva	echo -n "Installing updates..."
2921161748Scperciva
2922161748Scperciva	# Make sure we have all the files we should have
2923161748Scperciva	install_verify ${BDHASH}-install/INDEX-OLD	\
2924161748Scperciva	    ${BDHASH}-install/INDEX-NEW || return 1
2925161748Scperciva
2926161748Scperciva	# Remove system immutable flag from files
2927161748Scperciva	install_unschg ${BDHASH}-install/INDEX-OLD	\
2928161748Scperciva	    ${BDHASH}-install/INDEX-NEW || return 1
2929161748Scperciva
2930173564Scperciva	# Install new files, delete old files, and update linker.hints
2931173564Scperciva	install_files ${BDHASH}-install || return 1
2932161748Scperciva
2933161748Scperciva	# Rearrange bits to allow the installed updates to be rolled back
2934161748Scperciva	install_setup_rollback
2935161748Scperciva
2936161748Scperciva	echo " done."
2937161748Scperciva}
2938161748Scperciva
2939161748Scperciva# Rearrange bits to allow the previous set of updates to be rolled back next.
2940161748Scpercivarollback_setup_rollback () {
2941161748Scperciva	if [ -L ${BDHASH}-rollback/rollback ]; then
2942161748Scperciva		mv ${BDHASH}-rollback/rollback rollback-tmp
2943161748Scperciva		rm -r ${BDHASH}-rollback/
2944161748Scperciva		rm ${BDHASH}-rollback
2945161748Scperciva		mv rollback-tmp ${BDHASH}-rollback
2946161748Scperciva	else
2947161748Scperciva		rm -r ${BDHASH}-rollback/
2948161748Scperciva		rm ${BDHASH}-rollback
2949161748Scperciva	fi
2950161748Scperciva}
2951161748Scperciva
2952173564Scperciva# Install old files, delete new files, and update linker.hints
2953173564Scpercivarollback_files () {
2954173671Scperciva	# Install old shared library files which don't have the same path as
2955173671Scperciva	# a new shared library file.
2956173671Scperciva	grep -vE '^/boot/' $1/INDEX-NEW |
2957177601Scperciva	    grep -E '/lib/.*\.so\.[0-9]+\|' |
2958173671Scperciva	    cut -f 1 -d '|' |
2959173671Scperciva	    sort > INDEX-NEW.libs.flist
2960173564Scperciva	grep -vE '^/boot/' $1/INDEX-OLD |
2961177601Scperciva	    grep -E '/lib/.*\.so\.[0-9]+\|' |
2962173671Scperciva	    sort -k 1,1 -t '|' - |
2963173671Scperciva	    join -t '|' -v 1 - INDEX-NEW.libs.flist > INDEX-OLD
2964173564Scperciva	install_from_index INDEX-OLD || return 1
2965173564Scperciva
2966173564Scperciva	# Deal with files which are neither kernel nor shared library
2967173564Scperciva	grep -vE '^/boot/' $1/INDEX-OLD |
2968177601Scperciva	    grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2969173564Scperciva	grep -vE '^/boot/' $1/INDEX-NEW |
2970177601Scperciva	    grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2971173564Scperciva	install_from_index INDEX-OLD || return 1
2972173564Scperciva	install_delete INDEX-NEW INDEX-OLD || return 1
2973173564Scperciva
2974173671Scperciva	# Install any old shared library files which we didn't install above.
2975173671Scperciva	grep -vE '^/boot/' $1/INDEX-OLD |
2976177601Scperciva	    grep -E '/lib/.*\.so\.[0-9]+\|' |
2977173671Scperciva	    sort -k 1,1 -t '|' - |
2978173671Scperciva	    join -t '|' - INDEX-NEW.libs.flist > INDEX-OLD
2979173671Scperciva	install_from_index INDEX-OLD || return 1
2980173671Scperciva
2981173564Scperciva	# Delete unneeded shared library files
2982173564Scperciva	grep -vE '^/boot/' $1/INDEX-OLD |
2983177601Scperciva	    grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2984173564Scperciva	grep -vE '^/boot/' $1/INDEX-NEW |
2985177601Scperciva	    grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2986173564Scperciva	install_delete INDEX-NEW INDEX-OLD || return 1
2987173564Scperciva
2988173564Scperciva	# Deal with kernel files
2989173564Scperciva	grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD
2990173564Scperciva	grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW
2991173564Scperciva	install_from_index INDEX-OLD || return 1
2992173564Scperciva	install_delete INDEX-NEW INDEX-OLD || return 1
2993173564Scperciva	if [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2994173564Scperciva		kldxref -R /boot/ 2>/dev/null
2995173564Scperciva	fi
2996173564Scperciva
2997173564Scperciva	# Remove temporary files
2998173672Scperciva	rm INDEX-OLD INDEX-NEW INDEX-NEW.libs.flist
2999173564Scperciva}
3000173564Scperciva
3001161748Scperciva# Actually rollback updates
3002161748Scpercivarollback_run () {
3003161748Scperciva	echo -n "Uninstalling updates..."
3004161748Scperciva
3005161748Scperciva	# If there are updates waiting to be installed, remove them; we
3006161748Scperciva	# want the user to re-run 'fetch' after rolling back updates.
3007161748Scperciva	if [ -L ${BDHASH}-install ]; then
3008161748Scperciva		rm -r ${BDHASH}-install/
3009161748Scperciva		rm ${BDHASH}-install
3010161748Scperciva	fi
3011161748Scperciva
3012161748Scperciva	# Make sure we have all the files we should have
3013161748Scperciva	install_verify ${BDHASH}-rollback/INDEX-NEW	\
3014161748Scperciva	    ${BDHASH}-rollback/INDEX-OLD || return 1
3015161748Scperciva
3016161748Scperciva	# Remove system immutable flag from files
3017161748Scperciva	install_unschg ${BDHASH}-rollback/INDEX-NEW	\
3018161748Scperciva	    ${BDHASH}-rollback/INDEX-OLD || return 1
3019161748Scperciva
3020173564Scperciva	# Install old files, delete new files, and update linker.hints
3021173564Scperciva	rollback_files ${BDHASH}-rollback || return 1
3022161748Scperciva
3023161748Scperciva	# Remove the rollback directory and the symlink pointing to it; and
3024161748Scperciva	# rearrange bits to allow the previous set of updates to be rolled
3025161748Scperciva	# back next.
3026161748Scperciva	rollback_setup_rollback
3027161748Scperciva
3028161748Scperciva	echo " done."
3029161748Scperciva}
3030161748Scperciva
3031181142Scperciva# Compare INDEX-ALL and INDEX-PRESENT and print warnings about differences.
3032181142ScpercivaIDS_compare () {
3033181425Scperciva	# Get all the lines which mismatch in something other than file
3034181425Scperciva	# flags.  We ignore file flags because sysinstall doesn't seem to
3035181425Scperciva	# set them when it installs FreeBSD; warning about these adds a
3036181425Scperciva	# very large amount of noise.
3037181425Scperciva	cut -f 1-5,7-8 -d '|' $1 > $1.noflags
3038181425Scperciva	sort -k 1,1 -t '|' $1.noflags > $1.sorted
3039181425Scperciva	cut -f 1-5,7-8 -d '|' $2 |
3040181425Scperciva	    comm -13 $1.noflags - |
3041181425Scperciva	    fgrep -v '|-|||||' |
3042181142Scperciva	    sort -k 1,1 -t '|' |
3043181142Scperciva	    join -t '|' $1.sorted - > INDEX-NOTMATCHING
3044181142Scperciva
3045181142Scperciva	# Ignore files which match IDSIGNOREPATHS.
3046181142Scperciva	for X in ${IDSIGNOREPATHS}; do
3047181142Scperciva		grep -E "^${X}" INDEX-NOTMATCHING
3048181142Scperciva	done |
3049181142Scperciva	    sort -u |
3050181142Scperciva	    comm -13 - INDEX-NOTMATCHING > INDEX-NOTMATCHING.tmp
3051181142Scperciva	mv INDEX-NOTMATCHING.tmp INDEX-NOTMATCHING
3052181142Scperciva
3053181142Scperciva	# Go through the lines and print warnings.
3054181142Scperciva	while read LINE; do
3055181142Scperciva		FPATH=`echo "${LINE}" | cut -f 1 -d '|'`
3056181142Scperciva		TYPE=`echo "${LINE}" | cut -f 2 -d '|'`
3057181142Scperciva		OWNER=`echo "${LINE}" | cut -f 3 -d '|'`
3058181142Scperciva		GROUP=`echo "${LINE}" | cut -f 4 -d '|'`
3059181142Scperciva		PERM=`echo "${LINE}" | cut -f 5 -d '|'`
3060181425Scperciva		HASH=`echo "${LINE}" | cut -f 6 -d '|'`
3061181425Scperciva		LINK=`echo "${LINE}" | cut -f 7 -d '|'`
3062181425Scperciva		P_TYPE=`echo "${LINE}" | cut -f 8 -d '|'`
3063181425Scperciva		P_OWNER=`echo "${LINE}" | cut -f 9 -d '|'`
3064181425Scperciva		P_GROUP=`echo "${LINE}" | cut -f 10 -d '|'`
3065181425Scperciva		P_PERM=`echo "${LINE}" | cut -f 11 -d '|'`
3066181425Scperciva		P_HASH=`echo "${LINE}" | cut -f 12 -d '|'`
3067181425Scperciva		P_LINK=`echo "${LINE}" | cut -f 13 -d '|'`
3068181142Scperciva
3069181142Scperciva		# Warn about different object types.
3070181142Scperciva		if ! [ "${TYPE}" = "${P_TYPE}" ]; then
3071181142Scperciva			echo -n "${FPATH} is a "
3072181142Scperciva			case "${P_TYPE}" in
3073181142Scperciva			f)	echo -n "regular file, "
3074181142Scperciva				;;
3075181142Scperciva			d)	echo -n "directory, "
3076181142Scperciva				;;
3077181142Scperciva			L)	echo -n "symlink, "
3078181142Scperciva				;;
3079181142Scperciva			esac
3080181142Scperciva			echo -n "but should be a "
3081181142Scperciva			case "${TYPE}" in
3082181142Scperciva			f)	echo -n "regular file."
3083181142Scperciva				;;
3084181142Scperciva			d)	echo -n "directory."
3085181142Scperciva				;;
3086181142Scperciva			L)	echo -n "symlink."
3087181142Scperciva				;;
3088181142Scperciva			esac
3089181142Scperciva			echo
3090181142Scperciva
3091181142Scperciva			# Skip other tests, since they don't make sense if
3092181142Scperciva			# we're comparing different object types.
3093181142Scperciva			continue
3094181142Scperciva		fi
3095181142Scperciva
3096181142Scperciva		# Warn about different owners.
3097181142Scperciva		if ! [ "${OWNER}" = "${P_OWNER}" ]; then
3098181142Scperciva			echo -n "${FPATH} is owned by user id ${P_OWNER}, "
3099181142Scperciva			echo "but should be owned by user id ${OWNER}."
3100181142Scperciva		fi
3101181142Scperciva
3102181142Scperciva		# Warn about different groups.
3103181142Scperciva		if ! [ "${GROUP}" = "${P_GROUP}" ]; then
3104181142Scperciva			echo -n "${FPATH} is owned by group id ${P_GROUP}, "
3105181142Scperciva			echo "but should be owned by group id ${GROUP}."
3106181142Scperciva		fi
3107181142Scperciva
3108181142Scperciva		# Warn about different permissions.  We do not warn about
3109181142Scperciva		# different permissions on symlinks, since some archivers
3110181142Scperciva		# don't extract symlink permissions correctly and they are
3111181142Scperciva		# ignored anyway.
3112181142Scperciva		if ! [ "${PERM}" = "${P_PERM}" ] &&
3113181142Scperciva		    ! [ "${TYPE}" = "L" ]; then
3114181142Scperciva			echo -n "${FPATH} has ${P_PERM} permissions, "
3115181142Scperciva			echo "but should have ${PERM} permissions."
3116181142Scperciva		fi
3117181142Scperciva
3118181142Scperciva		# Warn about different file hashes / symlink destinations.
3119181142Scperciva		if ! [ "${HASH}" = "${P_HASH}" ]; then
3120181142Scperciva			if [ "${TYPE}" = "L" ]; then
3121181142Scperciva				echo -n "${FPATH} is a symlink to ${P_HASH}, "
3122181142Scperciva				echo "but should be a symlink to ${HASH}."
3123181142Scperciva			fi
3124181142Scperciva			if [ "${TYPE}" = "f" ]; then
3125181142Scperciva				echo -n "${FPATH} has SHA256 hash ${P_HASH}, "
3126181142Scperciva				echo "but should have SHA256 hash ${HASH}."
3127181142Scperciva			fi
3128181142Scperciva		fi
3129181142Scperciva
3130181142Scperciva		# We don't warn about different hard links, since some
3131181142Scperciva		# some archivers break hard links, and as long as the
3132181142Scperciva		# underlying data is correct they really don't matter.
3133181142Scperciva	done < INDEX-NOTMATCHING
3134181142Scperciva
3135181142Scperciva	# Clean up
3136181425Scperciva	rm $1 $1.noflags $1.sorted $2 INDEX-NOTMATCHING
3137181142Scperciva}
3138181142Scperciva
3139181142Scperciva# Do the work involved in comparing the system to a "known good" index
3140181142ScpercivaIDS_run () {
3141181142Scperciva	workdir_init || return 1
3142181142Scperciva
3143181142Scperciva	# Prepare the mirror list.
3144181142Scperciva	fetch_pick_server_init && fetch_pick_server
3145181142Scperciva
3146181142Scperciva	# Try to fetch the public key until we run out of servers.
3147181142Scperciva	while ! fetch_key; do
3148181142Scperciva		fetch_pick_server || return 1
3149181142Scperciva	done
3150181142Scperciva 
3151181142Scperciva	# Try to fetch the metadata index signature ("tag") until we run
3152181142Scperciva	# out of available servers; and sanity check the downloaded tag.
3153181142Scperciva	while ! fetch_tag; do
3154181142Scperciva		fetch_pick_server || return 1
3155181142Scperciva	done
3156181142Scperciva	fetch_tagsanity || return 1
3157181142Scperciva
3158181142Scperciva	# Fetch INDEX-OLD and INDEX-ALL.
3159181142Scperciva	fetch_metadata INDEX-OLD INDEX-ALL || return 1
3160181142Scperciva
3161181142Scperciva	# Generate filtered INDEX-OLD and INDEX-ALL files containing only
3162181142Scperciva	# the components we want and without anything marked as "Ignore".
3163181142Scperciva	fetch_filter_metadata INDEX-OLD || return 1
3164181142Scperciva	fetch_filter_metadata INDEX-ALL || return 1
3165181142Scperciva
3166181142Scperciva	# Merge the INDEX-OLD and INDEX-ALL files into INDEX-ALL.
3167181142Scperciva	sort INDEX-OLD INDEX-ALL > INDEX-ALL.tmp
3168181142Scperciva	mv INDEX-ALL.tmp INDEX-ALL
3169181142Scperciva	rm INDEX-OLD
3170181142Scperciva
3171181142Scperciva	# Translate /boot/${KERNCONF} to ${KERNELDIR}
3172181142Scperciva	fetch_filter_kernel_names INDEX-ALL ${KERNCONF}
3173181142Scperciva
3174181142Scperciva	# Inspect the system and generate an INDEX-PRESENT file.
3175181142Scperciva	fetch_inspect_system INDEX-ALL INDEX-PRESENT /dev/null || return 1
3176181142Scperciva
3177181142Scperciva	# Compare INDEX-ALL and INDEX-PRESENT and print warnings about any
3178181142Scperciva	# differences.
3179181142Scperciva	IDS_compare INDEX-ALL INDEX-PRESENT
3180181142Scperciva}
3181181142Scperciva
3182161748Scperciva#### Main functions -- call parameter-handling and core functions
3183161748Scperciva
3184161748Scperciva# Using the command line, configuration file, and defaults,
3185161748Scperciva# set all the parameters which are needed later.
3186161748Scpercivaget_params () {
3187161748Scperciva	init_params
3188161748Scperciva	parse_cmdline $@
3189161748Scperciva	parse_conffile
3190161748Scperciva	default_params
3191161748Scperciva}
3192161748Scperciva
3193161748Scperciva# Fetch command.  Make sure that we're being called
3194161748Scperciva# interactively, then run fetch_check_params and fetch_run
3195161748Scpercivacmd_fetch () {
3196161748Scperciva	if [ ! -t 0 ]; then
3197161748Scperciva		echo -n "`basename $0` fetch should not "
3198161748Scperciva		echo "be run non-interactively."
3199161748Scperciva		echo "Run `basename $0` cron instead."
3200161748Scperciva		exit 1
3201161748Scperciva	fi
3202161748Scperciva	fetch_check_params
3203161748Scperciva	fetch_run || exit 1
3204161748Scperciva}
3205161748Scperciva
3206161748Scperciva# Cron command.  Make sure the parameters are sensible; wait
3207161748Scperciva# rand(3600) seconds; then fetch updates.  While fetching updates,
3208161748Scperciva# send output to a temporary file; only print that file if the
3209161748Scperciva# fetching failed.
3210161748Scpercivacmd_cron () {
3211161748Scperciva	fetch_check_params
3212161748Scperciva	sleep `jot -r 1 0 3600`
3213161748Scperciva
3214161748Scperciva	TMPFILE=`mktemp /tmp/freebsd-update.XXXXXX` || exit 1
3215161748Scperciva	if ! fetch_run >> ${TMPFILE} ||
3216161748Scperciva	    ! grep -q "No updates needed" ${TMPFILE} ||
3217161748Scperciva	    [ ${VERBOSELEVEL} = "debug" ]; then
3218161748Scperciva		mail -s "`hostname` security updates" ${MAILTO} < ${TMPFILE}
3219161748Scperciva	fi
3220161748Scperciva
3221161748Scperciva	rm ${TMPFILE}
3222161748Scperciva}
3223161748Scperciva
3224173564Scperciva# Fetch files for upgrading to a new release.
3225173564Scpercivacmd_upgrade () {
3226173564Scperciva	upgrade_check_params
3227173564Scperciva	upgrade_run || exit 1
3228173564Scperciva}
3229173564Scperciva
3230161748Scperciva# Install downloaded updates.
3231161748Scpercivacmd_install () {
3232161748Scperciva	install_check_params
3233161748Scperciva	install_run || exit 1
3234161748Scperciva}
3235161748Scperciva
3236161748Scperciva# Rollback most recently installed updates.
3237161748Scpercivacmd_rollback () {
3238161748Scperciva	rollback_check_params
3239161748Scperciva	rollback_run || exit 1
3240161748Scperciva}
3241161748Scperciva
3242181142Scperciva# Compare system against a "known good" index.
3243181142Scpercivacmd_IDS () {
3244181142Scperciva	IDS_check_params
3245181142Scperciva	IDS_run || exit 1
3246181142Scperciva}
3247181142Scperciva
3248161748Scperciva#### Entry point
3249161748Scperciva
3250161748Scperciva# Make sure we find utilities from the base system
3251161748Scpercivaexport PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH}
3252161748Scperciva
3253217767Sgordon# Set a pager if the user doesn't
3254217767Sgordonif [ -z "$PAGER" ]; then
3255217767Sgordon	PAGER=/usr/bin/more
3256217767Sgordonfi
3257217767Sgordon
3258163564Scperciva# Set LC_ALL in order to avoid problems with character ranges like [A-Z].
3259163564Scpercivaexport LC_ALL=C
3260163564Scperciva
3261161748Scpercivaget_params $@
3262161748Scpercivafor COMMAND in ${COMMANDS}; do
3263161748Scperciva	cmd_${COMMAND}
3264161748Scpercivadone
3265