source: src-sh/pc-extractoverlay/desktop-overlay/usr/sbin/freebsd-update @ 1620346

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 1620346 was 1620346, checked in by Kris Moore <kris@…>, 20 months ago

Initial import of PC-BSD /current/ SVN repo

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