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