freebsd-update.sh revision 212431
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 212431 2010-09-10 19:20:52Z cperciva $ 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. 575161748Scpercivafetch_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 658173564Scperciva# Perform sanity checks etc. before fetching upgrades. 659173564Scpercivaupgrade_check_params () { 660173564Scperciva fetch_check_params 661173564Scperciva 662173564Scperciva # Unless set otherwise, we're upgrading to the same kernel config. 663173564Scperciva NKERNCONF=${KERNCONF} 664173564Scperciva 665173564Scperciva # We need TARGETRELEASE set 666173564Scperciva _TARGETRELEASE_z="Release target must be specified via -r option." 667173564Scperciva if [ -z "${TARGETRELEASE}" ]; then 668173564Scperciva echo -n "`basename $0`: " 669173564Scperciva echo "${_TARGETRELEASE_z}" 670173564Scperciva exit 1 671173564Scperciva fi 672173564Scperciva 673173564Scperciva # The target release should be != the current release. 674173564Scperciva if [ "${TARGETRELEASE}" = "${RELNUM}" ]; then 675173564Scperciva echo -n "`basename $0`: " 676173564Scperciva echo "Cannot upgrade from ${RELNUM} to itself" 677173564Scperciva exit 1 678173564Scperciva fi 679173564Scperciva 680173564Scperciva # Turning off AllowAdd or AllowDelete is a bad idea for upgrades. 681173564Scperciva if [ "${ALLOWADD}" = "no" ]; then 682173564Scperciva echo -n "`basename $0`: " 683173564Scperciva echo -n "WARNING: \"AllowAdd no\" is a bad idea " 684173564Scperciva echo "when upgrading between releases." 685173564Scperciva echo 686173564Scperciva fi 687173564Scperciva if [ "${ALLOWDELETE}" = "no" ]; then 688173564Scperciva echo -n "`basename $0`: " 689173564Scperciva echo -n "WARNING: \"AllowDelete no\" is a bad idea " 690173564Scperciva echo "when upgrading between releases." 691173564Scperciva echo 692173564Scperciva fi 693173564Scperciva 694173564Scperciva # Set EDITOR to /usr/bin/vi if it isn't already set 695173564Scperciva : ${EDITOR:='/usr/bin/vi'} 696173564Scperciva} 697173564Scperciva 698161748Scperciva# Perform sanity checks and set some final parameters in 699161748Scperciva# preparation for installing updates. 700161748Scpercivainstall_check_params () { 701161748Scperciva # Check that we are root. All sorts of things won't work otherwise. 702161748Scperciva if [ `id -u` != 0 ]; then 703161748Scperciva echo "You must be root to run this." 704161748Scperciva exit 1 705161748Scperciva fi 706161748Scperciva 707173441Scperciva # Check that securelevel <= 0. Otherwise we can't update schg files. 708173441Scperciva if [ `sysctl -n kern.securelevel` -gt 0 ]; then 709173441Scperciva echo "Updates cannot be installed when the system securelevel" 710173441Scperciva echo "is greater than zero." 711173441Scperciva exit 1 712173441Scperciva fi 713173441Scperciva 714161748Scperciva # Check that we have a working directory 715161748Scperciva _WORKDIR_bad="Directory does not exist or is not writable: " 716161748Scperciva if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 717161748Scperciva echo -n "`basename $0`: " 718161748Scperciva echo -n "${_WORKDIR_bad}" 719161748Scperciva echo ${WORKDIR} 720161748Scperciva exit 1 721161748Scperciva fi 722161748Scperciva cd ${WORKDIR} || exit 1 723161748Scperciva 724161748Scperciva # Construct a unique name from ${BASEDIR} 725161748Scperciva BDHASH=`echo ${BASEDIR} | sha256 -q` 726161748Scperciva 727161748Scperciva # Check that we have updates ready to install 728161748Scperciva if ! [ -L ${BDHASH}-install ]; then 729161748Scperciva echo "No updates are available to install." 730161748Scperciva echo "Run '$0 fetch' first." 731161748Scperciva exit 1 732161748Scperciva fi 733161748Scperciva if ! [ -f ${BDHASH}-install/INDEX-OLD ] || 734161748Scperciva ! [ -f ${BDHASH}-install/INDEX-NEW ]; then 735161748Scperciva echo "Update manifest is corrupt -- this should never happen." 736161748Scperciva echo "Re-run '$0 fetch'." 737161748Scperciva exit 1 738161748Scperciva fi 739196392Ssimon 740196392Ssimon # Figure out what directory contains the running kernel 741196392Ssimon BOOTFILE=`sysctl -n kern.bootfile` 742196392Ssimon KERNELDIR=${BOOTFILE%/kernel} 743196392Ssimon if ! [ -d ${KERNELDIR} ]; then 744196392Ssimon echo "Cannot identify running kernel" 745196392Ssimon exit 1 746196392Ssimon fi 747161748Scperciva} 748161748Scperciva 749161748Scperciva# Perform sanity checks and set some final parameters in 750161748Scperciva# preparation for UNinstalling updates. 751161748Scpercivarollback_check_params () { 752161748Scperciva # Check that we are root. All sorts of things won't work otherwise. 753161748Scperciva if [ `id -u` != 0 ]; then 754161748Scperciva echo "You must be root to run this." 755161748Scperciva exit 1 756161748Scperciva fi 757161748Scperciva 758161748Scperciva # Check that we have a working directory 759161748Scperciva _WORKDIR_bad="Directory does not exist or is not writable: " 760161748Scperciva if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 761161748Scperciva echo -n "`basename $0`: " 762161748Scperciva echo -n "${_WORKDIR_bad}" 763161748Scperciva echo ${WORKDIR} 764161748Scperciva exit 1 765161748Scperciva fi 766161748Scperciva cd ${WORKDIR} || exit 1 767161748Scperciva 768161748Scperciva # Construct a unique name from ${BASEDIR} 769161748Scperciva BDHASH=`echo ${BASEDIR} | sha256 -q` 770161748Scperciva 771161748Scperciva # Check that we have updates ready to rollback 772161748Scperciva if ! [ -L ${BDHASH}-rollback ]; then 773161748Scperciva echo "No rollback directory found." 774161748Scperciva exit 1 775161748Scperciva fi 776161748Scperciva if ! [ -f ${BDHASH}-rollback/INDEX-OLD ] || 777161748Scperciva ! [ -f ${BDHASH}-rollback/INDEX-NEW ]; then 778161748Scperciva echo "Update manifest is corrupt -- this should never happen." 779161748Scperciva exit 1 780161748Scperciva fi 781161748Scperciva} 782161748Scperciva 783181142Scperciva# Perform sanity checks and set some final parameters 784181142Scperciva# in preparation for comparing the system against the 785181142Scperciva# published index. Figure out which index we should 786181142Scperciva# compare against: If the user is running *-p[0-9]+, 787181142Scperciva# strip off the last part; if the user is running 788181142Scperciva# -SECURITY, call it -RELEASE. Chdir into the working 789181142Scperciva# directory. 790181142ScpercivaIDS_check_params () { 791181142Scperciva export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)" 792181142Scperciva 793181142Scperciva _SERVERNAME_z=\ 794181142Scperciva"SERVERNAME must be given via command line or configuration file." 795181142Scperciva _KEYPRINT_z="Key must be given via -k option or configuration file." 796181142Scperciva _KEYPRINT_bad="Invalid key fingerprint: " 797181142Scperciva _WORKDIR_bad="Directory does not exist or is not writable: " 798181142Scperciva 799181142Scperciva if [ -z "${SERVERNAME}" ]; then 800181142Scperciva echo -n "`basename $0`: " 801181142Scperciva echo "${_SERVERNAME_z}" 802181142Scperciva exit 1 803181142Scperciva fi 804181142Scperciva if [ -z "${KEYPRINT}" ]; then 805181142Scperciva echo -n "`basename $0`: " 806181142Scperciva echo "${_KEYPRINT_z}" 807181142Scperciva exit 1 808181142Scperciva fi 809181142Scperciva if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then 810181142Scperciva echo -n "`basename $0`: " 811181142Scperciva echo -n "${_KEYPRINT_bad}" 812181142Scperciva echo ${KEYPRINT} 813181142Scperciva exit 1 814181142Scperciva fi 815181142Scperciva if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 816181142Scperciva echo -n "`basename $0`: " 817181142Scperciva echo -n "${_WORKDIR_bad}" 818181142Scperciva echo ${WORKDIR} 819181142Scperciva exit 1 820181142Scperciva fi 821181142Scperciva cd ${WORKDIR} || exit 1 822181142Scperciva 823181142Scperciva # Generate release number. The s/SECURITY/RELEASE/ bit exists 824181142Scperciva # to provide an upgrade path for FreeBSD Update 1.x users, since 825181142Scperciva # the kernels provided by FreeBSD Update 1.x are always labelled 826181142Scperciva # as X.Y-SECURITY. 827181142Scperciva RELNUM=`uname -r | 828181142Scperciva sed -E 's,-p[0-9]+,,' | 829181142Scperciva sed -E 's,-SECURITY,-RELEASE,'` 830181142Scperciva ARCH=`uname -m` 831181142Scperciva FETCHDIR=${RELNUM}/${ARCH} 832181142Scperciva PATCHDIR=${RELNUM}/${ARCH}/bp 833181142Scperciva 834181142Scperciva # Figure out what directory contains the running kernel 835181142Scperciva BOOTFILE=`sysctl -n kern.bootfile` 836181142Scperciva KERNELDIR=${BOOTFILE%/kernel} 837181142Scperciva if ! [ -d ${KERNELDIR} ]; then 838181142Scperciva echo "Cannot identify running kernel" 839181142Scperciva exit 1 840181142Scperciva fi 841181142Scperciva 842181142Scperciva # Figure out what kernel configuration is running. We start with 843181142Scperciva # the output of `uname -i`, and then make the following adjustments: 844181142Scperciva # 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config 845181142Scperciva # file says "ident SMP-GENERIC", I don't know... 846181142Scperciva # 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64" 847181142Scperciva # _and_ `sysctl kern.version` contains a line which ends "/SMP", then 848181142Scperciva # we're running an SMP kernel. This mis-identification is a bug 849181142Scperciva # which was fixed in 6.2-STABLE. 850181142Scperciva KERNCONF=`uname -i` 851181142Scperciva if [ ${KERNCONF} = "SMP-GENERIC" ]; then 852181142Scperciva KERNCONF=SMP 853181142Scperciva fi 854181142Scperciva if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then 855181142Scperciva if sysctl kern.version | grep -qE '/SMP$'; then 856181142Scperciva KERNCONF=SMP 857181142Scperciva fi 858181142Scperciva fi 859181142Scperciva 860181142Scperciva # Define some paths 861181142Scperciva SHA256=/sbin/sha256 862181142Scperciva PHTTPGET=/usr/libexec/phttpget 863181142Scperciva 864181142Scperciva # Set up variables relating to VERBOSELEVEL 865181142Scperciva fetch_setup_verboselevel 866181142Scperciva} 867181142Scperciva 868161748Scperciva#### Core functionality -- the actual work gets done here 869161748Scperciva 870161748Scperciva# Use an SRV query to pick a server. If the SRV query doesn't provide 871161748Scperciva# a useful answer, use the server name specified by the user. 872161748Scperciva# Put another way... look up _http._tcp.${SERVERNAME} and pick a server 873161748Scperciva# from that; or if no servers are returned, use ${SERVERNAME}. 874161748Scperciva# This allows a user to specify "portsnap.freebsd.org" (in which case 875161748Scperciva# portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org" 876161748Scperciva# (in which case portsnap will use that particular server, since there 877161748Scperciva# won't be an SRV entry for that name). 878161748Scperciva# 879161748Scperciva# We ignore the Port field, since we are always going to use port 80. 880161748Scperciva 881161748Scperciva# Fetch the mirror list, but do not pick a mirror yet. Returns 1 if 882161748Scperciva# no mirrors are available for any reason. 883161748Scpercivafetch_pick_server_init () { 884161748Scperciva : > serverlist_tried 885161748Scperciva 886161748Scperciva# Check that host(1) exists (i.e., that the system wasn't built with the 887161748Scperciva# WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist. 888161748Scperciva if ! which -s host; then 889161748Scperciva : > serverlist_full 890161748Scperciva return 1 891161748Scperciva fi 892161748Scperciva 893161748Scperciva echo -n "Looking up ${SERVERNAME} mirrors... " 894161748Scperciva 895161748Scperciva# Issue the SRV query and pull out the Priority, Weight, and Target fields. 896161748Scperciva# BIND 9 prints "$name has SRV record ..." while BIND 8 prints 897161748Scperciva# "$name server selection ..."; we allow either format. 898161748Scperciva MLIST="_http._tcp.${SERVERNAME}" 899161748Scperciva host -t srv "${MLIST}" | 900161748Scperciva sed -nE "s/${MLIST} (has SRV record|server selection) //p" | 901161748Scperciva cut -f 1,2,4 -d ' ' | 902161748Scperciva sed -e 's/\.$//' | 903161748Scperciva sort > serverlist_full 904161748Scperciva 905161748Scperciva# If no records, give up -- we'll just use the server name we were given. 906161748Scperciva if [ `wc -l < serverlist_full` -eq 0 ]; then 907161748Scperciva echo "none found." 908161748Scperciva return 1 909161748Scperciva fi 910161748Scperciva 911161748Scperciva# Report how many mirrors we found. 912161748Scperciva echo `wc -l < serverlist_full` "mirrors found." 913161748Scperciva 914161748Scperciva# Generate a random seed for use in picking mirrors. If HTTP_PROXY 915161748Scperciva# is set, this will be used to generate the seed; otherwise, the seed 916161748Scperciva# will be random. 917161748Scperciva if [ -n "${HTTP_PROXY}${http_proxy}" ]; then 918161748Scperciva RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" | 919161748Scperciva tr -d 'a-f' | 920161748Scperciva cut -c 1-9` 921161748Scperciva else 922161748Scperciva RANDVALUE=`jot -r 1 0 999999999` 923161748Scperciva fi 924161748Scperciva} 925161748Scperciva 926161748Scperciva# Pick a mirror. Returns 1 if we have run out of mirrors to try. 927161748Scpercivafetch_pick_server () { 928161748Scperciva# Generate a list of not-yet-tried mirrors 929161748Scperciva sort serverlist_tried | 930161748Scperciva comm -23 serverlist_full - > serverlist 931161748Scperciva 932161748Scperciva# Have we run out of mirrors? 933161748Scperciva if [ `wc -l < serverlist` -eq 0 ]; then 934161748Scperciva echo "No mirrors remaining, giving up." 935161748Scperciva return 1 936161748Scperciva fi 937161748Scperciva 938161748Scperciva# Find the highest priority level (lowest numeric value). 939161748Scperciva SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1` 940161748Scperciva 941161748Scperciva# Add up the weights of the response lines at that priority level. 942161748Scperciva SRV_WSUM=0; 943161748Scperciva while read X; do 944161748Scperciva case "$X" in 945161748Scperciva ${SRV_PRIORITY}\ *) 946161748Scperciva SRV_W=`echo $X | cut -f 2 -d ' '` 947161748Scperciva SRV_WSUM=$(($SRV_WSUM + $SRV_W)) 948161748Scperciva ;; 949161748Scperciva esac 950161748Scperciva done < serverlist 951161748Scperciva 952161748Scperciva# If all the weights are 0, pretend that they are all 1 instead. 953161748Scperciva if [ ${SRV_WSUM} -eq 0 ]; then 954161748Scperciva SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l` 955161748Scperciva SRV_W_ADD=1 956161748Scperciva else 957161748Scperciva SRV_W_ADD=0 958161748Scperciva fi 959161748Scperciva 960161748Scperciva# Pick a value between 0 and the sum of the weights - 1 961161748Scperciva SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}` 962161748Scperciva 963161748Scperciva# Read through the list of mirrors and set SERVERNAME. Write the line 964161748Scperciva# corresponding to the mirror we selected into serverlist_tried so that 965161748Scperciva# we won't try it again. 966161748Scperciva while read X; do 967161748Scperciva case "$X" in 968161748Scperciva ${SRV_PRIORITY}\ *) 969161748Scperciva SRV_W=`echo $X | cut -f 2 -d ' '` 970161748Scperciva SRV_W=$(($SRV_W + $SRV_W_ADD)) 971161748Scperciva if [ $SRV_RND -lt $SRV_W ]; then 972161748Scperciva SERVERNAME=`echo $X | cut -f 3 -d ' '` 973161748Scperciva echo "$X" >> serverlist_tried 974161748Scperciva break 975161748Scperciva else 976161748Scperciva SRV_RND=$(($SRV_RND - $SRV_W)) 977161748Scperciva fi 978161748Scperciva ;; 979161748Scperciva esac 980161748Scperciva done < serverlist 981161748Scperciva} 982161748Scperciva 983161748Scperciva# Take a list of ${oldhash}|${newhash} and output a list of needed patches, 984161748Scperciva# i.e., those for which we have ${oldhash} and don't have ${newhash}. 985161748Scpercivafetch_make_patchlist () { 986161748Scperciva grep -vE "^([0-9a-f]{64})\|\1$" | 987161748Scperciva tr '|' ' ' | 988161748Scperciva while read X Y; do 989161748Scperciva if [ -f "files/${Y}.gz" ] || 990161748Scperciva [ ! -f "files/${X}.gz" ]; then 991161748Scperciva continue 992161748Scperciva fi 993161748Scperciva echo "${X}|${Y}" 994161748Scperciva done | uniq 995161748Scperciva} 996161748Scperciva 997161748Scperciva# Print user-friendly progress statistics 998161748Scpercivafetch_progress () { 999161748Scperciva LNC=0 1000161748Scperciva while read x; do 1001161748Scperciva LNC=$(($LNC + 1)) 1002161748Scperciva if [ $(($LNC % 10)) = 0 ]; then 1003161748Scperciva echo -n $LNC 1004161748Scperciva elif [ $(($LNC % 2)) = 0 ]; then 1005161748Scperciva echo -n . 1006161748Scperciva fi 1007161748Scperciva done 1008161748Scperciva echo -n " " 1009161748Scperciva} 1010161748Scperciva 1011173564Scperciva# Function for asking the user if everything is ok 1012173564Scpercivacontinuep () { 1013173564Scperciva while read -p "Does this look reasonable (y/n)? " CONTINUE; do 1014173564Scperciva case "${CONTINUE}" in 1015173564Scperciva y*) 1016173564Scperciva return 0 1017173564Scperciva ;; 1018173564Scperciva n*) 1019173564Scperciva return 1 1020173564Scperciva ;; 1021173564Scperciva esac 1022173564Scperciva done 1023173564Scperciva} 1024173564Scperciva 1025161748Scperciva# Initialize the working directory 1026161748Scpercivaworkdir_init () { 1027161748Scperciva mkdir -p files 1028161748Scperciva touch tINDEX.present 1029161748Scperciva} 1030161748Scperciva 1031161748Scperciva# Check that we have a public key with an appropriate hash, or 1032161748Scperciva# fetch the key if it doesn't exist. Returns 1 if the key has 1033161748Scperciva# not yet been fetched. 1034161748Scpercivafetch_key () { 1035161748Scperciva if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then 1036161748Scperciva return 0 1037161748Scperciva fi 1038161748Scperciva 1039161748Scperciva echo -n "Fetching public key from ${SERVERNAME}... " 1040161748Scperciva rm -f pub.ssl 1041161748Scperciva fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/pub.ssl \ 1042161748Scperciva 2>${QUIETREDIR} || true 1043161748Scperciva if ! [ -r pub.ssl ]; then 1044161748Scperciva echo "failed." 1045161748Scperciva return 1 1046161748Scperciva fi 1047161748Scperciva if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then 1048161748Scperciva echo "key has incorrect hash." 1049161748Scperciva rm -f pub.ssl 1050161748Scperciva return 1 1051161748Scperciva fi 1052161748Scperciva echo "done." 1053161748Scperciva} 1054161748Scperciva 1055161748Scperciva# Fetch metadata signature, aka "tag". 1056161748Scpercivafetch_tag () { 1057173564Scperciva echo -n "Fetching metadata signature " 1058173564Scperciva echo ${NDEBUG} "for ${RELNUM} from ${SERVERNAME}... " 1059161748Scperciva rm -f latest.ssl 1060161748Scperciva fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/latest.ssl \ 1061161748Scperciva 2>${QUIETREDIR} || true 1062161748Scperciva if ! [ -r latest.ssl ]; then 1063161748Scperciva echo "failed." 1064161748Scperciva return 1 1065161748Scperciva fi 1066161748Scperciva 1067161748Scperciva openssl rsautl -pubin -inkey pub.ssl -verify \ 1068161748Scperciva < latest.ssl > tag.new 2>${QUIETREDIR} || true 1069161748Scperciva rm latest.ssl 1070161748Scperciva 1071161748Scperciva if ! [ `wc -l < tag.new` = 1 ] || 1072161748Scperciva ! grep -qE \ 1073161748Scperciva "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \ 1074161748Scperciva tag.new; then 1075161748Scperciva echo "invalid signature." 1076161748Scperciva return 1 1077161748Scperciva fi 1078161748Scperciva 1079161748Scperciva echo "done." 1080161748Scperciva 1081161748Scperciva RELPATCHNUM=`cut -f 4 -d '|' < tag.new` 1082161748Scperciva TINDEXHASH=`cut -f 5 -d '|' < tag.new` 1083161748Scperciva EOLTIME=`cut -f 6 -d '|' < tag.new` 1084161748Scperciva} 1085161748Scperciva 1086161748Scperciva# Sanity-check the patch number in a tag, to make sure that we're not 1087161748Scperciva# going to "update" backwards and to prevent replay attacks. 1088161748Scpercivafetch_tagsanity () { 1089161748Scperciva # Check that we're not going to move from -pX to -pY with Y < X. 1090161748Scperciva RELPX=`uname -r | sed -E 's,.*-,,'` 1091161748Scperciva if echo ${RELPX} | grep -qE '^p[0-9]+$'; then 1092161748Scperciva RELPX=`echo ${RELPX} | cut -c 2-` 1093161748Scperciva else 1094161748Scperciva RELPX=0 1095161748Scperciva fi 1096161748Scperciva if [ "${RELPATCHNUM}" -lt "${RELPX}" ]; then 1097161748Scperciva echo 1098161748Scperciva echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})" 1099161748Scperciva echo " appear older than what" 1100161748Scperciva echo "we are currently running (`uname -r`)!" 1101161748Scperciva echo "Cowardly refusing to proceed any further." 1102161748Scperciva return 1 1103161748Scperciva fi 1104161748Scperciva 1105161748Scperciva # If "tag" exists and corresponds to ${RELNUM}, make sure that 1106161748Scperciva # it contains a patch number <= RELPATCHNUM, in order to protect 1107161748Scperciva # against rollback (replay) attacks. 1108161748Scperciva if [ -f tag ] && 1109161748Scperciva grep -qE \ 1110161748Scperciva "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \ 1111161748Scperciva tag; then 1112161748Scperciva LASTRELPATCHNUM=`cut -f 4 -d '|' < tag` 1113161748Scperciva 1114161748Scperciva if [ "${RELPATCHNUM}" -lt "${LASTRELPATCHNUM}" ]; then 1115161748Scperciva echo 1116161748Scperciva echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})" 1117161748Scperciva echo " are older than the" 1118161748Scperciva echo -n "most recently seen updates" 1119161748Scperciva echo " (${RELNUM}-p${LASTRELPATCHNUM})." 1120161748Scperciva echo "Cowardly refusing to proceed any further." 1121161748Scperciva return 1 1122161748Scperciva fi 1123161748Scperciva fi 1124161748Scperciva} 1125161748Scperciva 1126161748Scperciva# Fetch metadata index file 1127161748Scpercivafetch_metadata_index () { 1128161748Scperciva echo ${NDEBUG} "Fetching metadata index... " 1129161748Scperciva rm -f ${TINDEXHASH} 1130161748Scperciva fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/t/${TINDEXHASH} 1131161748Scperciva 2>${QUIETREDIR} 1132161748Scperciva if ! [ -f ${TINDEXHASH} ]; then 1133161748Scperciva echo "failed." 1134161748Scperciva return 1 1135161748Scperciva fi 1136161748Scperciva if [ `${SHA256} -q ${TINDEXHASH}` != ${TINDEXHASH} ]; then 1137161748Scperciva echo "update metadata index corrupt." 1138161748Scperciva return 1 1139161748Scperciva fi 1140161748Scperciva echo "done." 1141161748Scperciva} 1142161748Scperciva 1143161748Scperciva# Print an error message about signed metadata being bogus. 1144161748Scpercivafetch_metadata_bogus () { 1145161748Scperciva echo 1146161748Scperciva echo "The update metadata$1 is correctly signed, but" 1147161748Scperciva echo "failed an integrity check." 1148161748Scperciva echo "Cowardly refusing to proceed any further." 1149161748Scperciva return 1 1150161748Scperciva} 1151161748Scperciva 1152161748Scperciva# Construct tINDEX.new by merging the lines named in $1 from ${TINDEXHASH} 1153161748Scperciva# with the lines not named in $@ from tINDEX.present (if that file exists). 1154161748Scpercivafetch_metadata_index_merge () { 1155161748Scperciva for METAFILE in $@; do 1156161748Scperciva if [ `grep -E "^${METAFILE}\|" ${TINDEXHASH} | wc -l` \ 1157161748Scperciva -ne 1 ]; then 1158161748Scperciva fetch_metadata_bogus " index" 1159161748Scperciva return 1 1160161748Scperciva fi 1161161748Scperciva 1162161748Scperciva grep -E "${METAFILE}\|" ${TINDEXHASH} 1163161748Scperciva done | 1164161748Scperciva sort > tINDEX.wanted 1165161748Scperciva 1166161748Scperciva if [ -f tINDEX.present ]; then 1167161748Scperciva join -t '|' -v 2 tINDEX.wanted tINDEX.present | 1168161748Scperciva sort -m - tINDEX.wanted > tINDEX.new 1169161748Scperciva rm tINDEX.wanted 1170161748Scperciva else 1171161748Scperciva mv tINDEX.wanted tINDEX.new 1172161748Scperciva fi 1173161748Scperciva} 1174161748Scperciva 1175161748Scperciva# Sanity check all the lines of tINDEX.new. Even if more metadata lines 1176161748Scperciva# are added by future versions of the server, this won't cause problems, 1177161748Scperciva# since the only lines which appear in tINDEX.new are the ones which we 1178161748Scperciva# specifically grepped out of ${TINDEXHASH}. 1179161748Scpercivafetch_metadata_index_sanity () { 1180161748Scperciva if grep -qvE '^[0-9A-Z.-]+\|[0-9a-f]{64}$' tINDEX.new; then 1181161748Scperciva fetch_metadata_bogus " index" 1182161748Scperciva return 1 1183161748Scperciva fi 1184161748Scperciva} 1185161748Scperciva 1186161748Scperciva# Sanity check the metadata file $1. 1187161748Scpercivafetch_metadata_sanity () { 1188161748Scperciva # Some aliases to save space later: ${P} is a character which can 1189161748Scperciva # appear in a path; ${M} is the four numeric metadata fields; and 1190161748Scperciva # ${H} is a sha256 hash. 1191161748Scperciva P="[-+./:=_[[:alnum:]]" 1192161748Scperciva M="[0-9]+\|[0-9]+\|[0-9]+\|[0-9]+" 1193161748Scperciva H="[0-9a-f]{64}" 1194161748Scperciva 1195161748Scperciva # Check that the first four fields make sense. 1196161748Scperciva if gunzip -c < files/$1.gz | 1197161748Scperciva grep -qvE "^[a-z]+\|[0-9a-z]+\|${P}+\|[fdL-]\|"; then 1198161748Scperciva fetch_metadata_bogus "" 1199161748Scperciva return 1 1200161748Scperciva fi 1201161748Scperciva 1202161748Scperciva # Remove the first three fields. 1203161748Scperciva gunzip -c < files/$1.gz | 1204161748Scperciva cut -f 4- -d '|' > sanitycheck.tmp 1205161748Scperciva 1206161748Scperciva # Sanity check entries with type 'f' 1207161748Scperciva if grep -E '^f' sanitycheck.tmp | 1208161748Scperciva grep -qvE "^f\|${M}\|${H}\|${P}*\$"; then 1209161748Scperciva fetch_metadata_bogus "" 1210161748Scperciva return 1 1211161748Scperciva fi 1212161748Scperciva 1213161748Scperciva # Sanity check entries with type 'd' 1214161748Scperciva if grep -E '^d' sanitycheck.tmp | 1215161748Scperciva grep -qvE "^d\|${M}\|\|\$"; then 1216161748Scperciva fetch_metadata_bogus "" 1217161748Scperciva return 1 1218161748Scperciva fi 1219161748Scperciva 1220161748Scperciva # Sanity check entries with type 'L' 1221161748Scperciva if grep -E '^L' sanitycheck.tmp | 1222161748Scperciva grep -qvE "^L\|${M}\|${P}*\|\$"; then 1223161748Scperciva fetch_metadata_bogus "" 1224161748Scperciva return 1 1225161748Scperciva fi 1226161748Scperciva 1227161748Scperciva # Sanity check entries with type '-' 1228161748Scperciva if grep -E '^-' sanitycheck.tmp | 1229161748Scperciva grep -qvE "^-\|\|\|\|\|\|"; then 1230161748Scperciva fetch_metadata_bogus "" 1231161748Scperciva return 1 1232161748Scperciva fi 1233161748Scperciva 1234161748Scperciva # Clean up 1235161748Scperciva rm sanitycheck.tmp 1236161748Scperciva} 1237161748Scperciva 1238161748Scperciva# Fetch the metadata index and metadata files listed in $@, 1239161748Scperciva# taking advantage of metadata patches where possible. 1240161748Scpercivafetch_metadata () { 1241161748Scperciva fetch_metadata_index || return 1 1242161748Scperciva fetch_metadata_index_merge $@ || return 1 1243161748Scperciva fetch_metadata_index_sanity || return 1 1244161748Scperciva 1245161748Scperciva # Generate a list of wanted metadata patches 1246161748Scperciva join -t '|' -o 1.2,2.2 tINDEX.present tINDEX.new | 1247161748Scperciva fetch_make_patchlist > patchlist 1248161748Scperciva 1249161748Scperciva if [ -s patchlist ]; then 1250161748Scperciva # Attempt to fetch metadata patches 1251161748Scperciva echo -n "Fetching `wc -l < patchlist | tr -d ' '` " 1252161748Scperciva echo ${NDEBUG} "metadata patches.${DDSTATS}" 1253161748Scperciva tr '|' '-' < patchlist | 1254161748Scperciva lam -s "${FETCHDIR}/tp/" - -s ".gz" | 1255161748Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1256161748Scperciva 2>${STATSREDIR} | fetch_progress 1257161748Scperciva echo "done." 1258161748Scperciva 1259161748Scperciva # Attempt to apply metadata patches 1260161748Scperciva echo -n "Applying metadata patches... " 1261161748Scperciva tr '|' ' ' < patchlist | 1262161748Scperciva while read X Y; do 1263161748Scperciva if [ ! -f "${X}-${Y}.gz" ]; then continue; fi 1264161748Scperciva gunzip -c < ${X}-${Y}.gz > diff 1265161748Scperciva gunzip -c < files/${X}.gz > diff-OLD 1266161748Scperciva 1267161748Scperciva # Figure out which lines are being added and removed 1268161748Scperciva grep -E '^-' diff | 1269161748Scperciva cut -c 2- | 1270161748Scperciva while read PREFIX; do 1271161748Scperciva look "${PREFIX}" diff-OLD 1272161748Scperciva done | 1273161748Scperciva sort > diff-rm 1274161748Scperciva grep -E '^\+' diff | 1275161748Scperciva cut -c 2- > diff-add 1276161748Scperciva 1277161748Scperciva # Generate the new file 1278161748Scperciva comm -23 diff-OLD diff-rm | 1279161748Scperciva sort - diff-add > diff-NEW 1280161748Scperciva 1281161748Scperciva if [ `${SHA256} -q diff-NEW` = ${Y} ]; then 1282161748Scperciva mv diff-NEW files/${Y} 1283161748Scperciva gzip -n files/${Y} 1284161748Scperciva else 1285161748Scperciva mv diff-NEW ${Y}.bad 1286161748Scperciva fi 1287161748Scperciva rm -f ${X}-${Y}.gz diff 1288161748Scperciva rm -f diff-OLD diff-NEW diff-add diff-rm 1289161748Scperciva done 2>${QUIETREDIR} 1290161748Scperciva echo "done." 1291161748Scperciva fi 1292161748Scperciva 1293161748Scperciva # Update metadata without patches 1294161748Scperciva cut -f 2 -d '|' < tINDEX.new | 1295161748Scperciva while read Y; do 1296161748Scperciva if [ ! -f "files/${Y}.gz" ]; then 1297161748Scperciva echo ${Y}; 1298161748Scperciva fi 1299164600Scperciva done | 1300164600Scperciva sort -u > filelist 1301161748Scperciva 1302161748Scperciva if [ -s filelist ]; then 1303161748Scperciva echo -n "Fetching `wc -l < filelist | tr -d ' '` " 1304161748Scperciva echo ${NDEBUG} "metadata files... " 1305161748Scperciva lam -s "${FETCHDIR}/m/" - -s ".gz" < filelist | 1306161748Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1307161748Scperciva 2>${QUIETREDIR} 1308161748Scperciva 1309161748Scperciva while read Y; do 1310161748Scperciva if ! [ -f ${Y}.gz ]; then 1311161748Scperciva echo "failed." 1312161748Scperciva return 1 1313161748Scperciva fi 1314161748Scperciva if [ `gunzip -c < ${Y}.gz | 1315161748Scperciva ${SHA256} -q` = ${Y} ]; then 1316161748Scperciva mv ${Y}.gz files/${Y}.gz 1317161748Scperciva else 1318161748Scperciva echo "metadata is corrupt." 1319161748Scperciva return 1 1320161748Scperciva fi 1321161748Scperciva done < filelist 1322161748Scperciva echo "done." 1323161748Scperciva fi 1324161748Scperciva 1325161748Scperciva# Sanity-check the metadata files. 1326161748Scperciva cut -f 2 -d '|' tINDEX.new > filelist 1327161748Scperciva while read X; do 1328161748Scperciva fetch_metadata_sanity ${X} || return 1 1329161748Scperciva done < filelist 1330161748Scperciva 1331161748Scperciva# Remove files which are no longer needed 1332161748Scperciva cut -f 2 -d '|' tINDEX.present | 1333161748Scperciva sort > oldfiles 1334161748Scperciva cut -f 2 -d '|' tINDEX.new | 1335161748Scperciva sort | 1336161748Scperciva comm -13 - oldfiles | 1337161748Scperciva lam -s "files/" - -s ".gz" | 1338161748Scperciva xargs rm -f 1339161748Scperciva rm patchlist filelist oldfiles 1340161748Scperciva rm ${TINDEXHASH} 1341161748Scperciva 1342161748Scperciva# We're done! 1343161748Scperciva mv tINDEX.new tINDEX.present 1344161748Scperciva mv tag.new tag 1345161748Scperciva 1346161748Scperciva return 0 1347161748Scperciva} 1348161748Scperciva 1349173564Scperciva# Extract a subset of a downloaded metadata file containing only the parts 1350173564Scperciva# which are listed in COMPONENTS. 1351173564Scpercivafetch_filter_metadata_components () { 1352173564Scperciva METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'` 1353173564Scperciva gunzip -c < files/${METAHASH}.gz > $1.all 1354173564Scperciva 1355173564Scperciva # Fish out the lines belonging to components we care about. 1356173564Scperciva for C in ${COMPONENTS}; do 1357173564Scperciva look "`echo ${C} | tr '/' '|'`|" $1.all 1358173564Scperciva done > $1 1359173564Scperciva 1360173564Scperciva # Remove temporary file. 1361173564Scperciva rm $1.all 1362173564Scperciva} 1363173564Scperciva 1364161869Scperciva# Generate a filtered version of the metadata file $1 from the downloaded 1365161748Scperciva# file, by fishing out the lines corresponding to components we're trying 1366161748Scperciva# to keep updated, and then removing lines corresponding to paths we want 1367161748Scperciva# to ignore. 1368161748Scpercivafetch_filter_metadata () { 1369173564Scperciva # Fish out the lines belonging to components we care about. 1370173564Scperciva fetch_filter_metadata_components $1 1371161748Scperciva 1372161748Scperciva # Canonicalize directory names by removing any trailing / in 1373161748Scperciva # order to avoid listing directories multiple times if they 1374161748Scperciva # belong to multiple components. Turning "/" into "" doesn't 1375161748Scperciva # matter, since we add a leading "/" when we use paths later. 1376173564Scperciva cut -f 3- -d '|' $1 | 1377161748Scperciva sed -e 's,/|d|,|d|,' | 1378161748Scperciva sort -u > $1.tmp 1379161748Scperciva 1380161748Scperciva # Figure out which lines to ignore and remove them. 1381161748Scperciva for X in ${IGNOREPATHS}; do 1382161748Scperciva grep -E "^${X}" $1.tmp 1383161748Scperciva done | 1384161748Scperciva sort -u | 1385161748Scperciva comm -13 - $1.tmp > $1 1386161748Scperciva 1387161748Scperciva # Remove temporary files. 1388173564Scperciva rm $1.tmp 1389161748Scperciva} 1390161748Scperciva 1391173564Scperciva# Filter the metadata file $1 by adding lines with "/boot/$2" 1392164600Scperciva# replaced by ${KERNELDIR} (which is `sysctl -n kern.bootfile` minus the 1393173564Scperciva# trailing "/kernel"); and if "/boot/$2" does not exist, remove 1394164600Scperciva# the original lines which start with that. 1395164600Scperciva# Put another way: Deal with the fact that the FOO kernel is sometimes 1396164600Scperciva# installed in /boot/FOO/ and is sometimes installed elsewhere. 1397161748Scpercivafetch_filter_kernel_names () { 1398173564Scperciva grep ^/boot/$2 $1 | 1399173564Scperciva sed -e "s,/boot/$2,${KERNELDIR},g" | 1400161748Scperciva sort - $1 > $1.tmp 1401161748Scperciva mv $1.tmp $1 1402164600Scperciva 1403173564Scperciva if ! [ -d /boot/$2 ]; then 1404173564Scperciva grep -v ^/boot/$2 $1 > $1.tmp 1405164600Scperciva mv $1.tmp $1 1406164600Scperciva fi 1407161748Scperciva} 1408161748Scperciva 1409161748Scperciva# For all paths appearing in $1 or $3, inspect the system 1410161748Scperciva# and generate $2 describing what is currently installed. 1411161748Scpercivafetch_inspect_system () { 1412161748Scperciva # No errors yet... 1413161748Scperciva rm -f .err 1414161748Scperciva 1415161748Scperciva # Tell the user why his disk is suddenly making lots of noise 1416161748Scperciva echo -n "Inspecting system... " 1417161748Scperciva 1418161748Scperciva # Generate list of files to inspect 1419161748Scperciva cat $1 $3 | 1420161748Scperciva cut -f 1 -d '|' | 1421161748Scperciva sort -u > filelist 1422161748Scperciva 1423161748Scperciva # Examine each file and output lines of the form 1424161748Scperciva # /path/to/file|type|device-inum|user|group|perm|flags|value 1425161748Scperciva # sorted by device and inode number. 1426161748Scperciva while read F; do 1427161748Scperciva # If the symlink/file/directory does not exist, record this. 1428161748Scperciva if ! [ -e ${BASEDIR}/${F} ]; then 1429161748Scperciva echo "${F}|-||||||" 1430161748Scperciva continue 1431161748Scperciva fi 1432161748Scperciva if ! [ -r ${BASEDIR}/${F} ]; then 1433161748Scperciva echo "Cannot read file: ${BASEDIR}/${F}" \ 1434161748Scperciva >/dev/stderr 1435161748Scperciva touch .err 1436161748Scperciva return 1 1437161748Scperciva fi 1438161748Scperciva 1439161748Scperciva # Otherwise, output an index line. 1440161748Scperciva if [ -L ${BASEDIR}/${F} ]; then 1441161748Scperciva echo -n "${F}|L|" 1442161748Scperciva stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F}; 1443161748Scperciva readlink ${BASEDIR}/${F}; 1444161748Scperciva elif [ -f ${BASEDIR}/${F} ]; then 1445161748Scperciva echo -n "${F}|f|" 1446161748Scperciva stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F}; 1447161748Scperciva sha256 -q ${BASEDIR}/${F}; 1448161748Scperciva elif [ -d ${BASEDIR}/${F} ]; then 1449161748Scperciva echo -n "${F}|d|" 1450161748Scperciva stat -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F}; 1451161748Scperciva else 1452161748Scperciva echo "Unknown file type: ${BASEDIR}/${F}" \ 1453161748Scperciva >/dev/stderr 1454161748Scperciva touch .err 1455161748Scperciva return 1 1456161748Scperciva fi 1457161748Scperciva done < filelist | 1458161748Scperciva sort -k 3,3 -t '|' > $2.tmp 1459161748Scperciva rm filelist 1460161748Scperciva 1461161748Scperciva # Check if an error occured during system inspection 1462161748Scperciva if [ -f .err ]; then 1463161748Scperciva return 1 1464161748Scperciva fi 1465161748Scperciva 1466161748Scperciva # Convert to the form 1467161748Scperciva # /path/to/file|type|user|group|perm|flags|value|hlink 1468161748Scperciva # by resolving identical device and inode numbers into hard links. 1469161748Scperciva cut -f 1,3 -d '|' $2.tmp | 1470161748Scperciva sort -k 1,1 -t '|' | 1471161748Scperciva sort -s -u -k 2,2 -t '|' | 1472161748Scperciva join -1 2 -2 3 -t '|' - $2.tmp | 1473161748Scperciva awk -F \| -v OFS=\| \ 1474161748Scperciva '{ 1475161748Scperciva if (($2 == $3) || ($4 == "-")) 1476161748Scperciva print $3,$4,$5,$6,$7,$8,$9,"" 1477161748Scperciva else 1478161748Scperciva print $3,$4,$5,$6,$7,$8,$9,$2 1479161748Scperciva }' | 1480161748Scperciva sort > $2 1481161748Scperciva rm $2.tmp 1482161748Scperciva 1483161748Scperciva # We're finished looking around 1484161748Scperciva echo "done." 1485161748Scperciva} 1486161748Scperciva 1487173564Scperciva# For any paths matching ${MERGECHANGES}, compare $1 and $2 and find any 1488173564Scperciva# files which differ; generate $3 containing these paths and the old hashes. 1489173564Scpercivafetch_filter_mergechanges () { 1490173564Scperciva # Pull out the paths and hashes of the files matching ${MERGECHANGES}. 1491173564Scperciva for F in $1 $2; do 1492173564Scperciva for X in ${MERGECHANGES}; do 1493173564Scperciva grep -E "^${X}" ${F} 1494173564Scperciva done | 1495173564Scperciva cut -f 1,2,7 -d '|' | 1496173564Scperciva sort > ${F}-values 1497173564Scperciva done 1498173564Scperciva 1499173564Scperciva # Any line in $2-values which doesn't appear in $1-values and is a 1500173564Scperciva # file means that we should list the path in $3. 1501173564Scperciva comm -13 $1-values $2-values | 1502173564Scperciva fgrep '|f|' | 1503173564Scperciva cut -f 1 -d '|' > $2-paths 1504173564Scperciva 1505173564Scperciva # For each path, pull out one (and only one!) entry from $1-values. 1506173564Scperciva # Note that we cannot distinguish which "old" version the user made 1507173564Scperciva # changes to; but hopefully any changes which occur due to security 1508173564Scperciva # updates will exist in both the "new" version and the version which 1509173564Scperciva # the user has installed, so the merging will still work. 1510173564Scperciva while read X; do 1511173564Scperciva look "${X}|" $1-values | 1512173564Scperciva head -1 1513173564Scperciva done < $2-paths > $3 1514173564Scperciva 1515173564Scperciva # Clean up 1516173564Scperciva rm $1-values $2-values $2-paths 1517173564Scperciva} 1518173564Scperciva 1519161748Scperciva# For any paths matching ${UPDATEIFUNMODIFIED}, remove lines from $[123] 1520173564Scperciva# which correspond to lines in $2 with hashes not matching $1 or $3, unless 1521173564Scperciva# the paths are listed in $4. For entries in $2 marked "not present" 1522173564Scperciva# (aka. type -), remove lines from $[123] unless there is a corresponding 1523173564Scperciva# entry in $1. 1524161748Scpercivafetch_filter_unmodified_notpresent () { 1525161748Scperciva # Figure out which lines of $1 and $3 correspond to bits which 1526161748Scperciva # should only be updated if they haven't changed, and fish out 1527161748Scperciva # the (path, type, value) tuples. 1528161748Scperciva # NOTE: We don't consider a file to be "modified" if it matches 1529161748Scperciva # the hash from $3. 1530161748Scperciva for X in ${UPDATEIFUNMODIFIED}; do 1531161748Scperciva grep -E "^${X}" $1 1532161748Scperciva grep -E "^${X}" $3 1533161748Scperciva done | 1534161748Scperciva cut -f 1,2,7 -d '|' | 1535161748Scperciva sort > $1-values 1536161748Scperciva 1537161748Scperciva # Do the same for $2. 1538161748Scperciva for X in ${UPDATEIFUNMODIFIED}; do 1539161748Scperciva grep -E "^${X}" $2 1540161748Scperciva done | 1541161748Scperciva cut -f 1,2,7 -d '|' | 1542161748Scperciva sort > $2-values 1543161748Scperciva 1544161748Scperciva # Any entry in $2-values which is not in $1-values corresponds to 1545173564Scperciva # a path which we need to remove from $1, $2, and $3, unless it 1546173564Scperciva # that path appears in $4. 1547173564Scperciva comm -13 $1-values $2-values | 1548173564Scperciva sort -t '|' -k 1,1 > mlines.tmp 1549173564Scperciva cut -f 1 -d '|' $4 | 1550173564Scperciva sort | 1551173564Scperciva join -v 2 -t '|' - mlines.tmp | 1552173564Scperciva sort > mlines 1553173564Scperciva rm $1-values $2-values mlines.tmp 1554161748Scperciva 1555161748Scperciva # Any lines in $2 which are not in $1 AND are "not present" lines 1556161748Scperciva # also belong in mlines. 1557161748Scperciva comm -13 $1 $2 | 1558161748Scperciva cut -f 1,2,7 -d '|' | 1559161748Scperciva fgrep '|-|' >> mlines 1560161748Scperciva 1561161748Scperciva # Remove lines from $1, $2, and $3 1562161748Scperciva for X in $1 $2 $3; do 1563161748Scperciva sort -t '|' -k 1,1 ${X} > ${X}.tmp 1564161748Scperciva cut -f 1 -d '|' < mlines | 1565161748Scperciva sort | 1566161748Scperciva join -v 2 -t '|' - ${X}.tmp | 1567161748Scperciva sort > ${X} 1568161748Scperciva rm ${X}.tmp 1569161748Scperciva done 1570161748Scperciva 1571161748Scperciva # Store a list of the modified files, for future reference 1572161748Scperciva fgrep -v '|-|' mlines | 1573161748Scperciva cut -f 1 -d '|' > modifiedfiles 1574161748Scperciva rm mlines 1575161748Scperciva} 1576161748Scperciva 1577161748Scperciva# For each entry in $1 of type -, remove any corresponding 1578161748Scperciva# entry from $2 if ${ALLOWADD} != "yes". Remove all entries 1579161748Scperciva# of type - from $1. 1580161748Scpercivafetch_filter_allowadd () { 1581161748Scperciva cut -f 1,2 -d '|' < $1 | 1582161748Scperciva fgrep '|-' | 1583161748Scperciva cut -f 1 -d '|' > filesnotpresent 1584161748Scperciva 1585161748Scperciva if [ ${ALLOWADD} != "yes" ]; then 1586161748Scperciva sort < $2 | 1587161748Scperciva join -v 1 -t '|' - filesnotpresent | 1588161748Scperciva sort > $2.tmp 1589161748Scperciva mv $2.tmp $2 1590161748Scperciva fi 1591161748Scperciva 1592161748Scperciva sort < $1 | 1593161748Scperciva join -v 1 -t '|' - filesnotpresent | 1594161748Scperciva sort > $1.tmp 1595161748Scperciva mv $1.tmp $1 1596161748Scperciva rm filesnotpresent 1597161748Scperciva} 1598161748Scperciva 1599161748Scperciva# If ${ALLOWDELETE} != "yes", then remove any entries from $1 1600161748Scperciva# which don't correspond to entries in $2. 1601161748Scpercivafetch_filter_allowdelete () { 1602161748Scperciva # Produce a lists ${PATH}|${TYPE} 1603161748Scperciva for X in $1 $2; do 1604161748Scperciva cut -f 1-2 -d '|' < ${X} | 1605161748Scperciva sort -u > ${X}.nodes 1606161748Scperciva done 1607161748Scperciva 1608161748Scperciva # Figure out which lines need to be removed from $1. 1609161748Scperciva if [ ${ALLOWDELETE} != "yes" ]; then 1610161748Scperciva comm -23 $1.nodes $2.nodes > $1.badnodes 1611161748Scperciva else 1612161748Scperciva : > $1.badnodes 1613161748Scperciva fi 1614161748Scperciva 1615161748Scperciva # Remove the relevant lines from $1 1616161748Scperciva while read X; do 1617161748Scperciva look "${X}|" $1 1618161748Scperciva done < $1.badnodes | 1619161748Scperciva comm -13 - $1 > $1.tmp 1620161748Scperciva mv $1.tmp $1 1621161748Scperciva 1622161748Scperciva rm $1.badnodes $1.nodes $2.nodes 1623161748Scperciva} 1624161748Scperciva 1625161748Scperciva# If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in $2 1626161748Scperciva# with metadata not matching any entry in $1, replace the corresponding 1627161748Scperciva# line of $3 with one having the same metadata as the entry in $2. 1628161748Scpercivafetch_filter_modified_metadata () { 1629161748Scperciva # Fish out the metadata from $1 and $2 1630161748Scperciva for X in $1 $2; do 1631161748Scperciva cut -f 1-6 -d '|' < ${X} > ${X}.metadata 1632161748Scperciva done 1633161748Scperciva 1634161748Scperciva # Find the metadata we need to keep 1635161748Scperciva if [ ${KEEPMODIFIEDMETADATA} = "yes" ]; then 1636161748Scperciva comm -13 $1.metadata $2.metadata > keepmeta 1637161748Scperciva else 1638161748Scperciva : > keepmeta 1639161748Scperciva fi 1640161748Scperciva 1641161748Scperciva # Extract the lines which we need to remove from $3, and 1642161748Scperciva # construct the lines which we need to add to $3. 1643161748Scperciva : > $3.remove 1644161748Scperciva : > $3.add 1645161748Scperciva while read LINE; do 1646161748Scperciva NODE=`echo "${LINE}" | cut -f 1-2 -d '|'` 1647161748Scperciva look "${NODE}|" $3 >> $3.remove 1648161748Scperciva look "${NODE}|" $3 | 1649161748Scperciva cut -f 7- -d '|' | 1650161748Scperciva lam -s "${LINE}|" - >> $3.add 1651161748Scperciva done < keepmeta 1652161748Scperciva 1653161748Scperciva # Remove the specified lines and add the new lines. 1654161748Scperciva sort $3.remove | 1655161748Scperciva comm -13 - $3 | 1656161748Scperciva sort -u - $3.add > $3.tmp 1657161748Scperciva mv $3.tmp $3 1658161748Scperciva 1659161748Scperciva rm keepmeta $1.metadata $2.metadata $3.add $3.remove 1660161748Scperciva} 1661161748Scperciva 1662161748Scperciva# Remove lines from $1 and $2 which are identical; 1663161748Scperciva# no need to update a file if it isn't changing. 1664161748Scpercivafetch_filter_uptodate () { 1665161748Scperciva comm -23 $1 $2 > $1.tmp 1666161748Scperciva comm -13 $1 $2 > $2.tmp 1667161748Scperciva 1668161748Scperciva mv $1.tmp $1 1669161748Scperciva mv $2.tmp $2 1670161748Scperciva} 1671161748Scperciva 1672173564Scperciva# Fetch any "clean" old versions of files we need for merging changes. 1673173564Scpercivafetch_files_premerge () { 1674173564Scperciva # We only need to do anything if $1 is non-empty. 1675173564Scperciva if [ -s $1 ]; then 1676173564Scperciva # Tell the user what we're doing 1677173564Scperciva echo -n "Fetching files from ${OLDRELNUM} for merging... " 1678173564Scperciva 1679173564Scperciva # List of files wanted 1680173564Scperciva fgrep '|f|' < $1 | 1681173564Scperciva cut -f 3 -d '|' | 1682173564Scperciva sort -u > files.wanted 1683173564Scperciva 1684173564Scperciva # Only fetch the files we don't already have 1685173564Scperciva while read Y; do 1686173564Scperciva if [ ! -f "files/${Y}.gz" ]; then 1687173564Scperciva echo ${Y}; 1688173564Scperciva fi 1689173564Scperciva done < files.wanted > filelist 1690173564Scperciva 1691173564Scperciva # Actually fetch them 1692173564Scperciva lam -s "${OLDFETCHDIR}/f/" - -s ".gz" < filelist | 1693173564Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1694173564Scperciva 2>${QUIETREDIR} 1695173564Scperciva 1696173564Scperciva # Make sure we got them all, and move them into /files/ 1697173564Scperciva while read Y; do 1698173564Scperciva if ! [ -f ${Y}.gz ]; then 1699173564Scperciva echo "failed." 1700173564Scperciva return 1 1701173564Scperciva fi 1702173564Scperciva if [ `gunzip -c < ${Y}.gz | 1703173564Scperciva ${SHA256} -q` = ${Y} ]; then 1704173564Scperciva mv ${Y}.gz files/${Y}.gz 1705173564Scperciva else 1706173564Scperciva echo "${Y} has incorrect hash." 1707173564Scperciva return 1 1708173564Scperciva fi 1709173564Scperciva done < filelist 1710173564Scperciva echo "done." 1711173564Scperciva 1712173564Scperciva # Clean up 1713173564Scperciva rm filelist files.wanted 1714173564Scperciva fi 1715173564Scperciva} 1716173564Scperciva 1717161748Scperciva# Prepare to fetch files: Generate a list of the files we need, 1718161748Scperciva# copy the unmodified files we have into /files/, and generate 1719161748Scperciva# a list of patches to download. 1720161748Scpercivafetch_files_prepare () { 1721161748Scperciva # Tell the user why his disk is suddenly making lots of noise 1722161748Scperciva echo -n "Preparing to download files... " 1723161748Scperciva 1724161748Scperciva # Reduce indices to ${PATH}|${HASH} pairs 1725161748Scperciva for X in $1 $2 $3; do 1726161748Scperciva cut -f 1,2,7 -d '|' < ${X} | 1727161748Scperciva fgrep '|f|' | 1728161748Scperciva cut -f 1,3 -d '|' | 1729161748Scperciva sort > ${X}.hashes 1730161748Scperciva done 1731161748Scperciva 1732161748Scperciva # List of files wanted 1733161748Scperciva cut -f 2 -d '|' < $3.hashes | 1734173441Scperciva sort -u | 1735173441Scperciva while read HASH; do 1736173441Scperciva if ! [ -f files/${HASH}.gz ]; then 1737173441Scperciva echo ${HASH} 1738173441Scperciva fi 1739173441Scperciva done > files.wanted 1740161748Scperciva 1741161748Scperciva # Generate a list of unmodified files 1742161748Scperciva comm -12 $1.hashes $2.hashes | 1743161748Scperciva sort -k 1,1 -t '|' > unmodified.files 1744161748Scperciva 1745161748Scperciva # Copy all files into /files/. We only need the unmodified files 1746161748Scperciva # for use in patching; but we'll want all of them if the user asks 1747161748Scperciva # to rollback the updates later. 1748171784Scperciva while read LINE; do 1749171784Scperciva F=`echo "${LINE}" | cut -f 1 -d '|'` 1750171784Scperciva HASH=`echo "${LINE}" | cut -f 2 -d '|'` 1751171784Scperciva 1752171784Scperciva # Skip files we already have. 1753171784Scperciva if [ -f files/${HASH}.gz ]; then 1754171784Scperciva continue 1755171784Scperciva fi 1756171784Scperciva 1757171784Scperciva # Make sure the file hasn't changed. 1758161748Scperciva cp "${BASEDIR}/${F}" tmpfile 1759171784Scperciva if [ `sha256 -q tmpfile` != ${HASH} ]; then 1760171784Scperciva echo 1761171784Scperciva echo "File changed while FreeBSD Update running: ${F}" 1762171784Scperciva return 1 1763171784Scperciva fi 1764171784Scperciva 1765171784Scperciva # Place the file into storage. 1766171784Scperciva gzip -c < tmpfile > files/${HASH}.gz 1767161748Scperciva rm tmpfile 1768171784Scperciva done < $2.hashes 1769161748Scperciva 1770161748Scperciva # Produce a list of patches to download 1771161748Scperciva sort -k 1,1 -t '|' $3.hashes | 1772161748Scperciva join -t '|' -o 2.2,1.2 - unmodified.files | 1773161748Scperciva fetch_make_patchlist > patchlist 1774161748Scperciva 1775161748Scperciva # Garbage collect 1776161748Scperciva rm unmodified.files $1.hashes $2.hashes $3.hashes 1777161748Scperciva 1778161748Scperciva # We don't need the list of possible old files any more. 1779161748Scperciva rm $1 1780161748Scperciva 1781161748Scperciva # We're finished making noise 1782161748Scperciva echo "done." 1783161748Scperciva} 1784161748Scperciva 1785161748Scperciva# Fetch files. 1786161748Scpercivafetch_files () { 1787161748Scperciva # Attempt to fetch patches 1788161748Scperciva if [ -s patchlist ]; then 1789161748Scperciva echo -n "Fetching `wc -l < patchlist | tr -d ' '` " 1790161748Scperciva echo ${NDEBUG} "patches.${DDSTATS}" 1791161748Scperciva tr '|' '-' < patchlist | 1792173564Scperciva lam -s "${PATCHDIR}/" - | 1793161748Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1794161748Scperciva 2>${STATSREDIR} | fetch_progress 1795161748Scperciva echo "done." 1796161748Scperciva 1797161748Scperciva # Attempt to apply patches 1798161748Scperciva echo -n "Applying patches... " 1799161748Scperciva tr '|' ' ' < patchlist | 1800161748Scperciva while read X Y; do 1801161748Scperciva if [ ! -f "${X}-${Y}" ]; then continue; fi 1802161748Scperciva gunzip -c < files/${X}.gz > OLD 1803161748Scperciva 1804161748Scperciva bspatch OLD NEW ${X}-${Y} 1805161748Scperciva 1806161748Scperciva if [ `${SHA256} -q NEW` = ${Y} ]; then 1807161748Scperciva mv NEW files/${Y} 1808161748Scperciva gzip -n files/${Y} 1809161748Scperciva fi 1810161748Scperciva rm -f diff OLD NEW ${X}-${Y} 1811161748Scperciva done 2>${QUIETREDIR} 1812161748Scperciva echo "done." 1813161748Scperciva fi 1814161748Scperciva 1815161748Scperciva # Download files which couldn't be generate via patching 1816161748Scperciva while read Y; do 1817161748Scperciva if [ ! -f "files/${Y}.gz" ]; then 1818161748Scperciva echo ${Y}; 1819161748Scperciva fi 1820161748Scperciva done < files.wanted > filelist 1821161748Scperciva 1822161748Scperciva if [ -s filelist ]; then 1823161748Scperciva echo -n "Fetching `wc -l < filelist | tr -d ' '` " 1824161748Scperciva echo ${NDEBUG} "files... " 1825161748Scperciva lam -s "${FETCHDIR}/f/" - -s ".gz" < filelist | 1826161748Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1827161748Scperciva 2>${QUIETREDIR} 1828161748Scperciva 1829161748Scperciva while read Y; do 1830161748Scperciva if ! [ -f ${Y}.gz ]; then 1831161748Scperciva echo "failed." 1832161748Scperciva return 1 1833161748Scperciva fi 1834161748Scperciva if [ `gunzip -c < ${Y}.gz | 1835161748Scperciva ${SHA256} -q` = ${Y} ]; then 1836161748Scperciva mv ${Y}.gz files/${Y}.gz 1837161748Scperciva else 1838161748Scperciva echo "${Y} has incorrect hash." 1839161748Scperciva return 1 1840161748Scperciva fi 1841161748Scperciva done < filelist 1842161748Scperciva echo "done." 1843161748Scperciva fi 1844161748Scperciva 1845161748Scperciva # Clean up 1846161748Scperciva rm files.wanted filelist patchlist 1847161748Scperciva} 1848161748Scperciva 1849161748Scperciva# Create and populate install manifest directory; and report what updates 1850161748Scperciva# are available. 1851161748Scpercivafetch_create_manifest () { 1852161748Scperciva # If we have an existing install manifest, nuke it. 1853161748Scperciva if [ -L "${BDHASH}-install" ]; then 1854161748Scperciva rm -r ${BDHASH}-install/ 1855161748Scperciva rm ${BDHASH}-install 1856161748Scperciva fi 1857161748Scperciva 1858161748Scperciva # Report to the user if any updates were avoided due to local changes 1859161748Scperciva if [ -s modifiedfiles ]; then 1860161748Scperciva echo 1861161748Scperciva echo -n "The following files are affected by updates, " 1862161748Scperciva echo "but no changes have" 1863161748Scperciva echo -n "been downloaded because the files have been " 1864161748Scperciva echo "modified locally:" 1865161748Scperciva cat modifiedfiles 1866173564Scperciva fi | more 1867161748Scperciva rm modifiedfiles 1868161748Scperciva 1869161748Scperciva # If no files will be updated, tell the user and exit 1870161748Scperciva if ! [ -s INDEX-PRESENT ] && 1871161748Scperciva ! [ -s INDEX-NEW ]; then 1872161748Scperciva rm INDEX-PRESENT INDEX-NEW 1873161748Scperciva echo 1874161748Scperciva echo -n "No updates needed to update system to " 1875161748Scperciva echo "${RELNUM}-p${RELPATCHNUM}." 1876161748Scperciva return 1877161748Scperciva fi 1878161748Scperciva 1879161748Scperciva # Divide files into (a) removed files, (b) added files, and 1880161748Scperciva # (c) updated files. 1881161748Scperciva cut -f 1 -d '|' < INDEX-PRESENT | 1882161748Scperciva sort > INDEX-PRESENT.flist 1883161748Scperciva cut -f 1 -d '|' < INDEX-NEW | 1884161748Scperciva sort > INDEX-NEW.flist 1885161748Scperciva comm -23 INDEX-PRESENT.flist INDEX-NEW.flist > files.removed 1886161748Scperciva comm -13 INDEX-PRESENT.flist INDEX-NEW.flist > files.added 1887161748Scperciva comm -12 INDEX-PRESENT.flist INDEX-NEW.flist > files.updated 1888161748Scperciva rm INDEX-PRESENT.flist INDEX-NEW.flist 1889161748Scperciva 1890161748Scperciva # Report removed files, if any 1891161748Scperciva if [ -s files.removed ]; then 1892161748Scperciva echo 1893161748Scperciva echo -n "The following files will be removed " 1894161748Scperciva echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:" 1895161748Scperciva cat files.removed 1896173564Scperciva fi | more 1897161748Scperciva rm files.removed 1898161748Scperciva 1899161748Scperciva # Report added files, if any 1900161748Scperciva if [ -s files.added ]; then 1901161748Scperciva echo 1902161748Scperciva echo -n "The following files will be added " 1903161748Scperciva echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:" 1904161748Scperciva cat files.added 1905173564Scperciva fi | more 1906161748Scperciva rm files.added 1907161748Scperciva 1908161748Scperciva # Report updated files, if any 1909161748Scperciva if [ -s files.updated ]; then 1910161748Scperciva echo 1911161748Scperciva echo -n "The following files will be updated " 1912161748Scperciva echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:" 1913161748Scperciva 1914161748Scperciva cat files.updated 1915173564Scperciva fi | more 1916161748Scperciva rm files.updated 1917161748Scperciva 1918161748Scperciva # Create a directory for the install manifest. 1919161748Scperciva MDIR=`mktemp -d install.XXXXXX` || return 1 1920161748Scperciva 1921161748Scperciva # Populate it 1922161748Scperciva mv INDEX-PRESENT ${MDIR}/INDEX-OLD 1923161748Scperciva mv INDEX-NEW ${MDIR}/INDEX-NEW 1924161748Scperciva 1925161748Scperciva # Link it into place 1926161748Scperciva ln -s ${MDIR} ${BDHASH}-install 1927161748Scperciva} 1928161748Scperciva 1929161748Scperciva# Warn about any upcoming EoL 1930161748Scpercivafetch_warn_eol () { 1931161748Scperciva # What's the current time? 1932161748Scperciva NOWTIME=`date "+%s"` 1933161748Scperciva 1934161748Scperciva # When did we last warn about the EoL date? 1935161748Scperciva if [ -f lasteolwarn ]; then 1936161748Scperciva LASTWARN=`cat lasteolwarn` 1937161748Scperciva else 1938161748Scperciva LASTWARN=`expr ${NOWTIME} - 63072000` 1939161748Scperciva fi 1940161748Scperciva 1941161748Scperciva # If the EoL time is past, warn. 1942161748Scperciva if [ ${EOLTIME} -lt ${NOWTIME} ]; then 1943161748Scperciva echo 1944161748Scperciva cat <<-EOF 1945161869Scperciva WARNING: `uname -sr` HAS PASSED ITS END-OF-LIFE DATE. 1946161748Scperciva Any security issues discovered after `date -r ${EOLTIME}` 1947161748Scperciva will not have been corrected. 1948161748Scperciva EOF 1949161748Scperciva return 1 1950161748Scperciva fi 1951161748Scperciva 1952161748Scperciva # Figure out how long it has been since we last warned about the 1953161748Scperciva # upcoming EoL, and how much longer we have left. 1954161748Scperciva SINCEWARN=`expr ${NOWTIME} - ${LASTWARN}` 1955161748Scperciva TIMELEFT=`expr ${EOLTIME} - ${NOWTIME}` 1956161748Scperciva 1957171838Scperciva # Don't warn if the EoL is more than 3 months away 1958171838Scperciva if [ ${TIMELEFT} -gt 7884000 ]; then 1959161748Scperciva return 0 1960161748Scperciva fi 1961161748Scperciva 1962161748Scperciva # Don't warn if the time remaining is more than 3 times the time 1963161748Scperciva # since the last warning. 1964161748Scperciva if [ ${TIMELEFT} -gt `expr ${SINCEWARN} \* 3` ]; then 1965161748Scperciva return 0 1966161748Scperciva fi 1967161748Scperciva 1968161748Scperciva # Figure out what time units to use. 1969161748Scperciva if [ ${TIMELEFT} -lt 604800 ]; then 1970161748Scperciva UNIT="day" 1971161748Scperciva SIZE=86400 1972161748Scperciva elif [ ${TIMELEFT} -lt 2678400 ]; then 1973161748Scperciva UNIT="week" 1974161748Scperciva SIZE=604800 1975161748Scperciva else 1976161748Scperciva UNIT="month" 1977161748Scperciva SIZE=2678400 1978161748Scperciva fi 1979161748Scperciva 1980161748Scperciva # Compute the right number of units 1981161748Scperciva NUM=`expr ${TIMELEFT} / ${SIZE}` 1982161748Scperciva if [ ${NUM} != 1 ]; then 1983161748Scperciva UNIT="${UNIT}s" 1984161748Scperciva fi 1985161748Scperciva 1986161748Scperciva # Print the warning 1987161748Scperciva echo 1988161748Scperciva cat <<-EOF 1989161748Scperciva WARNING: `uname -sr` is approaching its End-of-Life date. 1990161748Scperciva It is strongly recommended that you upgrade to a newer 1991161748Scperciva release within the next ${NUM} ${UNIT}. 1992161748Scperciva EOF 1993161748Scperciva 1994161748Scperciva # Update the stored time of last warning 1995161748Scperciva echo ${NOWTIME} > lasteolwarn 1996161748Scperciva} 1997161748Scperciva 1998161748Scperciva# Do the actual work involved in "fetch" / "cron". 1999161748Scpercivafetch_run () { 2000161748Scperciva workdir_init || return 1 2001161748Scperciva 2002161748Scperciva # Prepare the mirror list. 2003161748Scperciva fetch_pick_server_init && fetch_pick_server 2004161748Scperciva 2005161748Scperciva # Try to fetch the public key until we run out of servers. 2006161748Scperciva while ! fetch_key; do 2007161748Scperciva fetch_pick_server || return 1 2008161748Scperciva done 2009161748Scperciva 2010161748Scperciva # Try to fetch the metadata index signature ("tag") until we run 2011161748Scperciva # out of available servers; and sanity check the downloaded tag. 2012161748Scperciva while ! fetch_tag; do 2013161748Scperciva fetch_pick_server || return 1 2014161748Scperciva done 2015161748Scperciva fetch_tagsanity || return 1 2016161748Scperciva 2017161748Scperciva # Fetch the latest INDEX-NEW and INDEX-OLD files. 2018161748Scperciva fetch_metadata INDEX-NEW INDEX-OLD || return 1 2019161748Scperciva 2020161748Scperciva # Generate filtered INDEX-NEW and INDEX-OLD files containing only 2021161748Scperciva # the lines which (a) belong to components we care about, and (b) 2022161748Scperciva # don't correspond to paths we're explicitly ignoring. 2023161748Scperciva fetch_filter_metadata INDEX-NEW || return 1 2024161748Scperciva fetch_filter_metadata INDEX-OLD || return 1 2025161748Scperciva 2026173564Scperciva # Translate /boot/${KERNCONF} into ${KERNELDIR} 2027173564Scperciva fetch_filter_kernel_names INDEX-NEW ${KERNCONF} 2028173564Scperciva fetch_filter_kernel_names INDEX-OLD ${KERNCONF} 2029161748Scperciva 2030161748Scperciva # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the 2031161748Scperciva # system and generate an INDEX-PRESENT file. 2032161748Scperciva fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2033161748Scperciva 2034161748Scperciva # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which 2035161748Scperciva # correspond to lines in INDEX-PRESENT with hashes not appearing 2036161748Scperciva # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in 2037161748Scperciva # INDEX-PRESENT has type - and there isn't a corresponding entry in 2038161748Scperciva # INDEX-OLD with type -. 2039173564Scperciva fetch_filter_unmodified_notpresent \ 2040173564Scperciva INDEX-OLD INDEX-PRESENT INDEX-NEW /dev/null 2041161748Scperciva 2042161748Scperciva # For each entry in INDEX-PRESENT of type -, remove any corresponding 2043161748Scperciva # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries 2044161748Scperciva # of type - from INDEX-PRESENT. 2045161748Scperciva fetch_filter_allowadd INDEX-PRESENT INDEX-NEW 2046161748Scperciva 2047161748Scperciva # If ${ALLOWDELETE} != "yes", then remove any entries from 2048161748Scperciva # INDEX-PRESENT which don't correspond to entries in INDEX-NEW. 2049161748Scperciva fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW 2050161748Scperciva 2051161748Scperciva # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in 2052161748Scperciva # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD, 2053161748Scperciva # replace the corresponding line of INDEX-NEW with one having the 2054161748Scperciva # same metadata as the entry in INDEX-PRESENT. 2055161748Scperciva fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW 2056161748Scperciva 2057161748Scperciva # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical; 2058161748Scperciva # no need to update a file if it isn't changing. 2059161748Scperciva fetch_filter_uptodate INDEX-PRESENT INDEX-NEW 2060161748Scperciva 2061161748Scperciva # Prepare to fetch files: Generate a list of the files we need, 2062161748Scperciva # copy the unmodified files we have into /files/, and generate 2063161748Scperciva # a list of patches to download. 2064171784Scperciva fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2065161748Scperciva 2066161748Scperciva # Fetch files. 2067161748Scperciva fetch_files || return 1 2068161748Scperciva 2069161748Scperciva # Create and populate install manifest directory; and report what 2070161748Scperciva # updates are available. 2071161748Scperciva fetch_create_manifest || return 1 2072161748Scperciva 2073161748Scperciva # Warn about any upcoming EoL 2074161748Scperciva fetch_warn_eol || return 1 2075161748Scperciva} 2076161748Scperciva 2077173564Scperciva# If StrictComponents is not "yes", generate a new components list 2078173564Scperciva# with only the components which appear to be installed. 2079173564Scpercivaupgrade_guess_components () { 2080173564Scperciva if [ "${STRICTCOMPONENTS}" = "no" ]; then 2081173564Scperciva # Generate filtered INDEX-ALL with only the components listed 2082173564Scperciva # in COMPONENTS. 2083173564Scperciva fetch_filter_metadata_components $1 || return 1 2084173564Scperciva 2085173564Scperciva # Tell the user why his disk is suddenly making lots of noise 2086173564Scperciva echo -n "Inspecting system... " 2087173564Scperciva 2088173564Scperciva # Look at the files on disk, and assume that a component is 2089173564Scperciva # supposed to be present if it is more than half-present. 2090173564Scperciva cut -f 1-3 -d '|' < INDEX-ALL | 2091173564Scperciva tr '|' ' ' | 2092173564Scperciva while read C S F; do 2093173564Scperciva if [ -e ${BASEDIR}/${F} ]; then 2094173564Scperciva echo "+ ${C}|${S}" 2095173564Scperciva fi 2096173564Scperciva echo "= ${C}|${S}" 2097173564Scperciva done | 2098173564Scperciva sort | 2099173564Scperciva uniq -c | 2100173564Scperciva sed -E 's,^ +,,' > compfreq 2101173564Scperciva grep ' = ' compfreq | 2102173564Scperciva cut -f 1,3 -d ' ' | 2103173564Scperciva sort -k 2,2 -t ' ' > compfreq.total 2104173564Scperciva grep ' + ' compfreq | 2105173564Scperciva cut -f 1,3 -d ' ' | 2106173564Scperciva sort -k 2,2 -t ' ' > compfreq.present 2107173564Scperciva join -t ' ' -1 2 -2 2 compfreq.present compfreq.total | 2108173564Scperciva while read S P T; do 2109173564Scperciva if [ ${P} -gt `expr ${T} / 2` ]; then 2110173564Scperciva echo ${S} 2111173564Scperciva fi 2112173564Scperciva done > comp.present 2113173564Scperciva cut -f 2 -d ' ' < compfreq.total > comp.total 2114173564Scperciva rm INDEX-ALL compfreq compfreq.total compfreq.present 2115173564Scperciva 2116173564Scperciva # We're done making noise. 2117173564Scperciva echo "done." 2118173564Scperciva 2119173564Scperciva # Sometimes the kernel isn't installed where INDEX-ALL 2120173564Scperciva # thinks that it should be: In particular, it is often in 2121173564Scperciva # /boot/kernel instead of /boot/GENERIC or /boot/SMP. To 2122173564Scperciva # deal with this, if "kernel|X" is listed in comp.total 2123173564Scperciva # (i.e., is a component which would be upgraded if it is 2124173564Scperciva # found to be present) we will add it to comp.present. 2125173564Scperciva # If "kernel|<anything>" is in comp.total but "kernel|X" is 2126173564Scperciva # not, we print a warning -- the user is running a kernel 2127173564Scperciva # which isn't part of the release. 2128173564Scperciva KCOMP=`echo ${KERNCONF} | tr 'A-Z' 'a-z'` 2129173564Scperciva grep -E "^kernel\|${KCOMP}\$" comp.total >> comp.present 2130173564Scperciva 2131173564Scperciva if grep -qE "^kernel\|" comp.total && 2132173564Scperciva ! grep -qE "^kernel\|${KCOMP}\$" comp.total; then 2133173564Scperciva cat <<-EOF 2134173564Scperciva 2135173564ScpercivaWARNING: This system is running a "${KCOMP}" kernel, which is not a 2136173564Scpercivakernel configuration distributed as part of FreeBSD ${RELNUM}. 2137173564ScpercivaThis kernel will not be updated: you MUST update the kernel manually 2138173564Scpercivabefore running "$0 install". 2139173564Scperciva EOF 2140173564Scperciva fi 2141173564Scperciva 2142173564Scperciva # Re-sort the list of installed components and generate 2143173564Scperciva # the list of non-installed components. 2144173564Scperciva sort -u < comp.present > comp.present.tmp 2145173564Scperciva mv comp.present.tmp comp.present 2146173564Scperciva comm -13 comp.present comp.total > comp.absent 2147173564Scperciva 2148173564Scperciva # Ask the user to confirm that what we have is correct. To 2149173564Scperciva # reduce user confusion, translate "X|Y" back to "X/Y" (as 2150173564Scperciva # subcomponents must be listed in the configuration file). 2151173564Scperciva echo 2152173564Scperciva echo -n "The following components of FreeBSD " 2153173564Scperciva echo "seem to be installed:" 2154173564Scperciva tr '|' '/' < comp.present | 2155173564Scperciva fmt -72 2156173564Scperciva echo 2157173564Scperciva echo -n "The following components of FreeBSD " 2158173564Scperciva echo "do not seem to be installed:" 2159173564Scperciva tr '|' '/' < comp.absent | 2160173564Scperciva fmt -72 2161173564Scperciva echo 2162173564Scperciva continuep || return 1 2163173564Scperciva echo 2164173564Scperciva 2165173564Scperciva # Suck the generated list of components into ${COMPONENTS}. 2166173564Scperciva # Note that comp.present.tmp is used due to issues with 2167173564Scperciva # pipelines and setting variables. 2168173564Scperciva COMPONENTS="" 2169173564Scperciva tr '|' '/' < comp.present > comp.present.tmp 2170173564Scperciva while read C; do 2171173564Scperciva COMPONENTS="${COMPONENTS} ${C}" 2172173564Scperciva done < comp.present.tmp 2173173564Scperciva 2174173564Scperciva # Delete temporary files 2175173564Scperciva rm comp.present comp.present.tmp comp.absent comp.total 2176173564Scperciva fi 2177173564Scperciva} 2178173564Scperciva 2179173564Scperciva# If StrictComponents is not "yes", COMPONENTS contains an entry 2180173564Scperciva# corresponding to the currently running kernel, and said kernel 2181173564Scperciva# does not exist in the new release, add "kernel/generic" to the 2182173564Scperciva# list of components. 2183173564Scpercivaupgrade_guess_new_kernel () { 2184173564Scperciva if [ "${STRICTCOMPONENTS}" = "no" ]; then 2185173564Scperciva # Grab the unfiltered metadata file. 2186173564Scperciva METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'` 2187173564Scperciva gunzip -c < files/${METAHASH}.gz > $1.all 2188173564Scperciva 2189173564Scperciva # If "kernel/${KCOMP}" is in ${COMPONENTS} and that component 2190173564Scperciva # isn't in $1.all, we need to add kernel/generic. 2191173564Scperciva for C in ${COMPONENTS}; do 2192173564Scperciva if [ ${C} = "kernel/${KCOMP}" ] && 2193173564Scperciva ! grep -qE "^kernel\|${KCOMP}\|" $1.all; then 2194173564Scperciva COMPONENTS="${COMPONENTS} kernel/generic" 2195173564Scperciva NKERNCONF="GENERIC" 2196173564Scperciva cat <<-EOF 2197173564Scperciva 2198173564ScpercivaWARNING: This system is running a "${KCOMP}" kernel, which is not a 2199173564Scpercivakernel configuration distributed as part of FreeBSD ${RELNUM}. 2200173564ScpercivaAs part of upgrading to FreeBSD ${RELNUM}, this kernel will be 2201173564Scpercivareplaced with a "generic" kernel. 2202173564Scperciva EOF 2203173564Scperciva continuep || return 1 2204173564Scperciva fi 2205173564Scperciva done 2206173564Scperciva 2207173564Scperciva # Don't need this any more... 2208173564Scperciva rm $1.all 2209173564Scperciva fi 2210173564Scperciva} 2211173564Scperciva 2212173564Scperciva# Convert INDEX-OLD (last release) and INDEX-ALL (new release) into 2213173564Scperciva# INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades). 2214173564Scpercivaupgrade_oldall_to_oldnew () { 2215173564Scperciva # For each ${F}|... which appears in INDEX-ALL but does not appear 2216173564Scperciva # in INDEX-OLD, add ${F}|-|||||| to INDEX-OLD. 2217173564Scperciva cut -f 1 -d '|' < $1 | 2218173564Scperciva sort -u > $1.paths 2219173564Scperciva cut -f 1 -d '|' < $2 | 2220173564Scperciva sort -u | 2221173564Scperciva comm -13 $1.paths - | 2222173564Scperciva lam - -s "|-||||||" | 2223173564Scperciva sort - $1 > $1.tmp 2224173564Scperciva mv $1.tmp $1 2225173564Scperciva 2226173564Scperciva # Remove lines from INDEX-OLD which also appear in INDEX-ALL 2227173564Scperciva comm -23 $1 $2 > $1.tmp 2228173564Scperciva mv $1.tmp $1 2229173564Scperciva 2230173564Scperciva # Remove lines from INDEX-ALL which have a file name not appearing 2231173564Scperciva # anywhere in INDEX-OLD (since these must be files which haven't 2232173564Scperciva # changed -- if they were new, there would be an entry of type "-"). 2233173564Scperciva cut -f 1 -d '|' < $1 | 2234173564Scperciva sort -u > $1.paths 2235173564Scperciva sort -k 1,1 -t '|' < $2 | 2236173564Scperciva join -t '|' - $1.paths | 2237173564Scperciva sort > $2.tmp 2238173564Scperciva rm $1.paths 2239173564Scperciva mv $2.tmp $2 2240173564Scperciva 2241173564Scperciva # Rename INDEX-ALL to INDEX-NEW. 2242173564Scperciva mv $2 $3 2243173564Scperciva} 2244173564Scperciva 2245173564Scperciva# From the list of "old" files in $1, merge changes in $2 with those in $3, 2246173564Scperciva# and update $3 to reflect the hashes of merged files. 2247173564Scpercivaupgrade_merge () { 2248173564Scperciva # We only need to do anything if $1 is non-empty. 2249173564Scperciva if [ -s $1 ]; then 2250173564Scperciva cut -f 1 -d '|' $1 | 2251173564Scperciva sort > $1-paths 2252173564Scperciva 2253173564Scperciva # Create staging area for merging files 2254173564Scperciva rm -rf merge/ 2255173564Scperciva while read F; do 2256173564Scperciva D=`dirname ${F}` 2257173564Scperciva mkdir -p merge/old/${D} 2258173564Scperciva mkdir -p merge/${OLDRELNUM}/${D} 2259173564Scperciva mkdir -p merge/${RELNUM}/${D} 2260173564Scperciva mkdir -p merge/new/${D} 2261173564Scperciva done < $1-paths 2262173564Scperciva 2263173564Scperciva # Copy in files 2264173564Scperciva while read F; do 2265173564Scperciva # Currently installed file 2266173564Scperciva V=`look "${F}|" $2 | cut -f 7 -d '|'` 2267173564Scperciva gunzip < files/${V}.gz > merge/old/${F} 2268173564Scperciva 2269173564Scperciva # Old release 2270173564Scperciva if look "${F}|" $1 | fgrep -q "|f|"; then 2271173564Scperciva V=`look "${F}|" $1 | cut -f 3 -d '|'` 2272173564Scperciva gunzip < files/${V}.gz \ 2273173564Scperciva > merge/${OLDRELNUM}/${F} 2274173564Scperciva fi 2275173564Scperciva 2276173564Scperciva # New release 2277173564Scperciva if look "${F}|" $3 | cut -f 1,2,7 -d '|' | 2278173564Scperciva fgrep -q "|f|"; then 2279173564Scperciva V=`look "${F}|" $3 | cut -f 7 -d '|'` 2280173564Scperciva gunzip < files/${V}.gz \ 2281173564Scperciva > merge/${RELNUM}/${F} 2282173564Scperciva fi 2283173564Scperciva done < $1-paths 2284173564Scperciva 2285173564Scperciva # Attempt to automatically merge changes 2286173564Scperciva echo -n "Attempting to automatically merge " 2287173564Scperciva echo -n "changes in files..." 2288173564Scperciva : > failed.merges 2289173564Scperciva while read F; do 2290173564Scperciva # If the file doesn't exist in the new release, 2291173564Scperciva # the result of "merging changes" is having the file 2292173564Scperciva # not exist. 2293173564Scperciva if ! [ -f merge/${RELNUM}/${F} ]; then 2294173564Scperciva continue 2295173564Scperciva fi 2296173564Scperciva 2297173564Scperciva # If the file didn't exist in the old release, we're 2298173564Scperciva # going to throw away the existing file and hope that 2299173564Scperciva # the version from the new release is what we want. 2300173564Scperciva if ! [ -f merge/${OLDRELNUM}/${F} ]; then 2301173564Scperciva cp merge/${RELNUM}/${F} merge/new/${F} 2302173564Scperciva continue 2303173564Scperciva fi 2304173564Scperciva 2305173564Scperciva # Some files need special treatment. 2306173564Scperciva case ${F} in 2307173564Scperciva /etc/spwd.db | /etc/pwd.db | /etc/login.conf.db) 2308173564Scperciva # Don't merge these -- we're rebuild them 2309173564Scperciva # after updates are installed. 2310173564Scperciva cp merge/old/${F} merge/new/${F} 2311173564Scperciva ;; 2312173564Scperciva *) 2313173564Scperciva if ! merge -p -L "current version" \ 2314173564Scperciva -L "${OLDRELNUM}" -L "${RELNUM}" \ 2315173564Scperciva merge/old/${F} \ 2316173564Scperciva merge/${OLDRELNUM}/${F} \ 2317173564Scperciva merge/${RELNUM}/${F} \ 2318173564Scperciva > merge/new/${F} 2>/dev/null; then 2319173564Scperciva echo ${F} >> failed.merges 2320173564Scperciva fi 2321173564Scperciva ;; 2322173564Scperciva esac 2323173564Scperciva done < $1-paths 2324173564Scperciva echo " done." 2325173564Scperciva 2326173564Scperciva # Ask the user to handle any files which didn't merge. 2327173564Scperciva while read F; do 2328173564Scperciva cat <<-EOF 2329173564Scperciva 2330173564ScpercivaThe following file could not be merged automatically: ${F} 2331173564ScpercivaPress Enter to edit this file in ${EDITOR} and resolve the conflicts 2332173564Scpercivamanually... 2333173564Scperciva EOF 2334173564Scperciva read dummy </dev/tty 2335173564Scperciva ${EDITOR} `pwd`/merge/new/${F} < /dev/tty 2336173564Scperciva done < failed.merges 2337173564Scperciva rm failed.merges 2338173564Scperciva 2339173564Scperciva # Ask the user to confirm that he likes how the result 2340173564Scperciva # of merging files. 2341173564Scperciva while read F; do 2342173564Scperciva # Skip files which haven't changed. 2343173564Scperciva if [ -f merge/new/${F} ] && 2344173564Scperciva cmp -s merge/old/${F} merge/new/${F}; then 2345173564Scperciva continue 2346173564Scperciva fi 2347173564Scperciva 2348173564Scperciva # Warn about files which are ceasing to exist. 2349173564Scperciva if ! [ -f merge/new/${F} ]; then 2350173564Scperciva cat <<-EOF 2351173564Scperciva 2352173564ScpercivaThe following file will be removed, as it no longer exists in 2353173564ScpercivaFreeBSD ${RELNUM}: ${F} 2354173564Scperciva EOF 2355173564Scperciva continuep < /dev/tty || return 1 2356173564Scperciva continue 2357173564Scperciva fi 2358173564Scperciva 2359173564Scperciva # Print changes for the user's approval. 2360173564Scperciva cat <<-EOF 2361173564Scperciva 2362173564ScpercivaThe following changes, which occurred between FreeBSD ${OLDRELNUM} and 2363173564ScpercivaFreeBSD ${RELNUM} have been merged into ${F}: 2364173564ScpercivaEOF 2365173564Scperciva diff -U 5 -L "current version" -L "new version" \ 2366173564Scperciva merge/old/${F} merge/new/${F} || true 2367173564Scperciva continuep < /dev/tty || return 1 2368173564Scperciva done < $1-paths 2369173564Scperciva 2370173564Scperciva # Store merged files. 2371173564Scperciva while read F; do 2372177527Scperciva if [ -f merge/new/${F} ]; then 2373177527Scperciva V=`${SHA256} -q merge/new/${F}` 2374173564Scperciva 2375173564Scperciva gzip -c < merge/new/${F} > files/${V}.gz 2376173564Scperciva echo "${F}|${V}" 2377173564Scperciva fi 2378173564Scperciva done < $1-paths > newhashes 2379173564Scperciva 2380173564Scperciva # Pull lines out from $3 which need to be updated to 2381173564Scperciva # reflect merged files. 2382173564Scperciva while read F; do 2383173564Scperciva look "${F}|" $3 2384173564Scperciva done < $1-paths > $3-oldlines 2385173564Scperciva 2386173564Scperciva # Update lines to reflect merged files 2387173564Scperciva join -t '|' -o 1.1,1.2,1.3,1.4,1.5,1.6,2.2,1.8 \ 2388173564Scperciva $3-oldlines newhashes > $3-newlines 2389173564Scperciva 2390173564Scperciva # Remove old lines from $3 and add new lines. 2391173564Scperciva sort $3-oldlines | 2392173564Scperciva comm -13 - $3 | 2393173564Scperciva sort - $3-newlines > $3.tmp 2394173564Scperciva mv $3.tmp $3 2395173564Scperciva 2396173564Scperciva # Clean up 2397173564Scperciva rm $1-paths newhashes $3-oldlines $3-newlines 2398173564Scperciva rm -rf merge/ 2399173564Scperciva fi 2400173564Scperciva 2401173564Scperciva # We're done with merging files. 2402173564Scperciva rm $1 2403173564Scperciva} 2404173564Scperciva 2405173564Scperciva# Do the work involved in fetching upgrades to a new release 2406173564Scpercivaupgrade_run () { 2407173564Scperciva workdir_init || return 1 2408173564Scperciva 2409173564Scperciva # Prepare the mirror list. 2410173564Scperciva fetch_pick_server_init && fetch_pick_server 2411173564Scperciva 2412173564Scperciva # Try to fetch the public key until we run out of servers. 2413173564Scperciva while ! fetch_key; do 2414173564Scperciva fetch_pick_server || return 1 2415173564Scperciva done 2416173564Scperciva 2417173564Scperciva # Try to fetch the metadata index signature ("tag") until we run 2418173564Scperciva # out of available servers; and sanity check the downloaded tag. 2419173564Scperciva while ! fetch_tag; do 2420173564Scperciva fetch_pick_server || return 1 2421173564Scperciva done 2422173564Scperciva fetch_tagsanity || return 1 2423173564Scperciva 2424173564Scperciva # Fetch the INDEX-OLD and INDEX-ALL. 2425173564Scperciva fetch_metadata INDEX-OLD INDEX-ALL || return 1 2426173564Scperciva 2427173564Scperciva # If StrictComponents is not "yes", generate a new components list 2428173564Scperciva # with only the components which appear to be installed. 2429173564Scperciva upgrade_guess_components INDEX-ALL || return 1 2430173564Scperciva 2431173564Scperciva # Generate filtered INDEX-OLD and INDEX-ALL files containing only 2432173564Scperciva # the components we want and without anything marked as "Ignore". 2433173564Scperciva fetch_filter_metadata INDEX-OLD || return 1 2434173564Scperciva fetch_filter_metadata INDEX-ALL || return 1 2435173564Scperciva 2436173564Scperciva # Merge the INDEX-OLD and INDEX-ALL files into INDEX-OLD. 2437173564Scperciva sort INDEX-OLD INDEX-ALL > INDEX-OLD.tmp 2438173564Scperciva mv INDEX-OLD.tmp INDEX-OLD 2439173564Scperciva rm INDEX-ALL 2440173564Scperciva 2441173564Scperciva # Adjust variables for fetching files from the new release. 2442173564Scperciva OLDRELNUM=${RELNUM} 2443173564Scperciva RELNUM=${TARGETRELEASE} 2444173564Scperciva OLDFETCHDIR=${FETCHDIR} 2445173564Scperciva FETCHDIR=${RELNUM}/${ARCH} 2446173564Scperciva 2447173564Scperciva # Try to fetch the NEW metadata index signature ("tag") until we run 2448173564Scperciva # out of available servers; and sanity check the downloaded tag. 2449173564Scperciva while ! fetch_tag; do 2450173564Scperciva fetch_pick_server || return 1 2451173564Scperciva done 2452173564Scperciva 2453173564Scperciva # Fetch the new INDEX-ALL. 2454173564Scperciva fetch_metadata INDEX-ALL || return 1 2455173564Scperciva 2456173564Scperciva # If StrictComponents is not "yes", COMPONENTS contains an entry 2457173564Scperciva # corresponding to the currently running kernel, and said kernel 2458173564Scperciva # does not exist in the new release, add "kernel/generic" to the 2459173564Scperciva # list of components. 2460173564Scperciva upgrade_guess_new_kernel INDEX-ALL || return 1 2461173564Scperciva 2462173564Scperciva # Filter INDEX-ALL to contain only the components we want and without 2463173564Scperciva # anything marked as "Ignore". 2464173564Scperciva fetch_filter_metadata INDEX-ALL || return 1 2465173564Scperciva 2466173564Scperciva # Convert INDEX-OLD (last release) and INDEX-ALL (new release) into 2467173564Scperciva # INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades). 2468173564Scperciva upgrade_oldall_to_oldnew INDEX-OLD INDEX-ALL INDEX-NEW 2469173564Scperciva 2470173564Scperciva # Translate /boot/${KERNCONF} or /boot/${NKERNCONF} into ${KERNELDIR} 2471173564Scperciva fetch_filter_kernel_names INDEX-NEW ${NKERNCONF} 2472173564Scperciva fetch_filter_kernel_names INDEX-OLD ${KERNCONF} 2473173564Scperciva 2474173564Scperciva # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the 2475173564Scperciva # system and generate an INDEX-PRESENT file. 2476173564Scperciva fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2477173564Scperciva 2478173564Scperciva # Based on ${MERGECHANGES}, generate a file tomerge-old with the 2479173564Scperciva # paths and hashes of old versions of files to merge. 2480173564Scperciva fetch_filter_mergechanges INDEX-OLD INDEX-PRESENT tomerge-old 2481173564Scperciva 2482173564Scperciva # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which 2483173564Scperciva # correspond to lines in INDEX-PRESENT with hashes not appearing 2484173564Scperciva # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in 2485173564Scperciva # INDEX-PRESENT has type - and there isn't a corresponding entry in 2486173564Scperciva # INDEX-OLD with type -. 2487173564Scperciva fetch_filter_unmodified_notpresent \ 2488173564Scperciva INDEX-OLD INDEX-PRESENT INDEX-NEW tomerge-old 2489173564Scperciva 2490173564Scperciva # For each entry in INDEX-PRESENT of type -, remove any corresponding 2491173564Scperciva # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries 2492173564Scperciva # of type - from INDEX-PRESENT. 2493173564Scperciva fetch_filter_allowadd INDEX-PRESENT INDEX-NEW 2494173564Scperciva 2495173564Scperciva # If ${ALLOWDELETE} != "yes", then remove any entries from 2496173564Scperciva # INDEX-PRESENT which don't correspond to entries in INDEX-NEW. 2497173564Scperciva fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW 2498173564Scperciva 2499173564Scperciva # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in 2500173564Scperciva # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD, 2501173564Scperciva # replace the corresponding line of INDEX-NEW with one having the 2502173564Scperciva # same metadata as the entry in INDEX-PRESENT. 2503173564Scperciva fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW 2504173564Scperciva 2505173564Scperciva # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical; 2506173564Scperciva # no need to update a file if it isn't changing. 2507173564Scperciva fetch_filter_uptodate INDEX-PRESENT INDEX-NEW 2508173564Scperciva 2509173564Scperciva # Fetch "clean" files from the old release for merging changes. 2510173564Scperciva fetch_files_premerge tomerge-old 2511173564Scperciva 2512173564Scperciva # Prepare to fetch files: Generate a list of the files we need, 2513173564Scperciva # copy the unmodified files we have into /files/, and generate 2514173564Scperciva # a list of patches to download. 2515173564Scperciva fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2516173564Scperciva 2517173564Scperciva # Fetch patches from to-${RELNUM}/${ARCH}/bp/ 2518173564Scperciva PATCHDIR=to-${RELNUM}/${ARCH}/bp 2519173564Scperciva fetch_files || return 1 2520173564Scperciva 2521173564Scperciva # Merge configuration file changes. 2522173564Scperciva upgrade_merge tomerge-old INDEX-PRESENT INDEX-NEW || return 1 2523173564Scperciva 2524173564Scperciva # Create and populate install manifest directory; and report what 2525173564Scperciva # updates are available. 2526173564Scperciva fetch_create_manifest || return 1 2527173564Scperciva 2528173564Scperciva # Leave a note behind to tell the "install" command that the kernel 2529173564Scperciva # needs to be installed before the world. 2530173564Scperciva touch ${BDHASH}-install/kernelfirst 2531212431Scperciva 2532212431Scperciva # Remind the user that they need to run "freebsd-update install" 2533212431Scperciva # to install the downloaded bits, in case they didn't RTFM. 2534212431Scperciva echo "To install the downloaded upgrades, run \"$0 install\"." 2535173564Scperciva} 2536173564Scperciva 2537161748Scperciva# Make sure that all the file hashes mentioned in $@ have corresponding 2538161748Scperciva# gzipped files stored in /files/. 2539161748Scpercivainstall_verify () { 2540161748Scperciva # Generate a list of hashes 2541161748Scperciva cat $@ | 2542161748Scperciva cut -f 2,7 -d '|' | 2543161748Scperciva grep -E '^f' | 2544161748Scperciva cut -f 2 -d '|' | 2545161748Scperciva sort -u > filelist 2546161748Scperciva 2547161748Scperciva # Make sure all the hashes exist 2548161748Scperciva while read HASH; do 2549161748Scperciva if ! [ -f files/${HASH}.gz ]; then 2550161748Scperciva echo -n "Update files missing -- " 2551161748Scperciva echo "this should never happen." 2552161748Scperciva echo "Re-run '$0 fetch'." 2553161748Scperciva return 1 2554161748Scperciva fi 2555161748Scperciva done < filelist 2556161748Scperciva 2557161748Scperciva # Clean up 2558161748Scperciva rm filelist 2559161748Scperciva} 2560161748Scperciva 2561161748Scperciva# Remove the system immutable flag from files 2562161748Scpercivainstall_unschg () { 2563161748Scperciva # Generate file list 2564161748Scperciva cat $@ | 2565161748Scperciva cut -f 1 -d '|' > filelist 2566161748Scperciva 2567161748Scperciva # Remove flags 2568161748Scperciva while read F; do 2569169603Scperciva if ! [ -e ${BASEDIR}/${F} ]; then 2570161748Scperciva continue 2571161748Scperciva fi 2572161748Scperciva 2573169603Scperciva chflags noschg ${BASEDIR}/${F} || return 1 2574161748Scperciva done < filelist 2575161748Scperciva 2576161748Scperciva # Clean up 2577161748Scperciva rm filelist 2578161748Scperciva} 2579161748Scperciva 2580196392Ssimon# Decide which directory name to use for kernel backups. 2581196392Ssimonbackup_kernel_finddir () { 2582196392Ssimon CNT=0 2583196392Ssimon while true ; do 2584196392Ssimon # Pathname does not exist, so it is OK use that name 2585196392Ssimon # for backup directory. 2586196392Ssimon if [ ! -e $BACKUPKERNELDIR ]; then 2587196392Ssimon return 0 2588196392Ssimon fi 2589196392Ssimon 2590196392Ssimon # If directory do exist, we only use if it has our 2591196392Ssimon # marker file. 2592196392Ssimon if [ -d $BACKUPKERNELDIR -a \ 2593196392Ssimon -e $BACKUPKERNELDIR/.freebsd-update ]; then 2594196392Ssimon return 0 2595196392Ssimon fi 2596196392Ssimon 2597196392Ssimon # We could not use current directory name, so add counter to 2598196392Ssimon # the end and try again. 2599196392Ssimon CNT=$((CNT + 1)) 2600196392Ssimon if [ $CNT -gt 9 ]; then 2601196392Ssimon echo "Could not find valid backup dir ($BACKUPKERNELDIR)" 2602196392Ssimon exit 1 2603196392Ssimon fi 2604196392Ssimon BACKUPKERNELDIR="`echo $BACKUPKERNELDIR | sed -Ee 's/[0-9]\$//'`" 2605196392Ssimon BACKUPKERNELDIR="${BACKUPKERNELDIR}${CNT}" 2606196392Ssimon done 2607196392Ssimon} 2608196392Ssimon 2609196392Ssimon# Backup the current kernel using hardlinks, if not disabled by user. 2610196392Ssimon# Since we delete all files in the directory used for previous backups 2611196392Ssimon# we create a marker file called ".freebsd-update" in the directory so 2612196392Ssimon# we can determine on the next run that the directory was created by 2613196392Ssimon# freebsd-update and we then do not accidentally remove user files in 2614196392Ssimon# the unlikely case that the user has created a directory with a 2615196392Ssimon# conflicting name. 2616196392Ssimonbackup_kernel () { 2617196392Ssimon # Only make kernel backup is so configured. 2618196392Ssimon if [ $BACKUPKERNEL != yes ]; then 2619196392Ssimon return 0 2620196392Ssimon fi 2621196392Ssimon 2622196392Ssimon # Decide which directory name to use for kernel backups. 2623196392Ssimon backup_kernel_finddir 2624196392Ssimon 2625196392Ssimon # Remove old kernel backup files. If $BACKUPKERNELDIR was 2626196392Ssimon # "not ours", backup_kernel_finddir would have exited, so 2627196392Ssimon # deleting the directory content is as safe as we can make it. 2628196392Ssimon if [ -d $BACKUPKERNELDIR ]; then 2629196392Ssimon rm -f $BACKUPKERNELDIR/* 2630196392Ssimon fi 2631196392Ssimon 2632196392Ssimon # Create directory for backup if it doesn't exist. 2633196392Ssimon mkdir -p $BACKUPKERNELDIR 2634196392Ssimon 2635196392Ssimon # Mark the directory as having been created by freebsd-update. 2636196392Ssimon touch $BACKUPKERNELDIR/.freebsd-update 2637196392Ssimon if [ $? -ne 0 ]; then 2638196392Ssimon echo "Could not create kernel backup directory" 2639196392Ssimon exit 1 2640196392Ssimon fi 2641196392Ssimon 2642196392Ssimon # Disable pathname expansion to be sure *.symbols is not 2643196392Ssimon # expanded. 2644196392Ssimon set -f 2645196392Ssimon 2646196392Ssimon # Use find to ignore symbol files, unless disabled by user. 2647196392Ssimon if [ $BACKUPKERNELSYMBOLFILES = yes ]; then 2648196392Ssimon FINDFILTER="" 2649196392Ssimon else 2650196392Ssimon FINDFILTER=-"a ! -name *.symbols" 2651196392Ssimon fi 2652196392Ssimon 2653196392Ssimon # Backup all the kernel files using hardlinks. 2654196392Ssimon find $KERNELDIR -type f $FINDFILTER | \ 2655196392Ssimon sed -Ee "s,($KERNELDIR)/?(.*),\1/\2 ${BACKUPKERNELDIR}/\2," | \ 2656196392Ssimon xargs -n 2 cp -pl 2657196392Ssimon 2658196392Ssimon # Re-enable patchname expansion. 2659196392Ssimon set +f 2660196392Ssimon} 2661196392Ssimon 2662161748Scperciva# Install new files 2663161748Scpercivainstall_from_index () { 2664161748Scperciva # First pass: Do everything apart from setting file flags. We 2665161748Scperciva # can't set flags yet, because schg inhibits hard linking. 2666161748Scperciva sort -k 1,1 -t '|' $1 | 2667161748Scperciva tr '|' ' ' | 2668161748Scperciva while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do 2669161748Scperciva case ${TYPE} in 2670161748Scperciva d) 2671161748Scperciva # Create a directory 2672161748Scperciva install -d -o ${OWNER} -g ${GROUP} \ 2673161748Scperciva -m ${PERM} ${BASEDIR}/${FPATH} 2674161748Scperciva ;; 2675161748Scperciva f) 2676161748Scperciva if [ -z "${LINK}" ]; then 2677161748Scperciva # Create a file, without setting flags. 2678161748Scperciva gunzip < files/${HASH}.gz > ${HASH} 2679161748Scperciva install -S -o ${OWNER} -g ${GROUP} \ 2680161748Scperciva -m ${PERM} ${HASH} ${BASEDIR}/${FPATH} 2681161748Scperciva rm ${HASH} 2682161748Scperciva else 2683161748Scperciva # Create a hard link. 2684169603Scperciva ln -f ${BASEDIR}/${LINK} ${BASEDIR}/${FPATH} 2685161748Scperciva fi 2686161748Scperciva ;; 2687161748Scperciva L) 2688161748Scperciva # Create a symlink 2689161748Scperciva ln -sfh ${HASH} ${BASEDIR}/${FPATH} 2690161748Scperciva ;; 2691161748Scperciva esac 2692161748Scperciva done 2693161748Scperciva 2694161748Scperciva # Perform a second pass, adding file flags. 2695161748Scperciva tr '|' ' ' < $1 | 2696161748Scperciva while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do 2697161748Scperciva if [ ${TYPE} = "f" ] && 2698161748Scperciva ! [ ${FLAGS} = "0" ]; then 2699161748Scperciva chflags ${FLAGS} ${BASEDIR}/${FPATH} 2700161748Scperciva fi 2701161748Scperciva done 2702161748Scperciva} 2703161748Scperciva 2704161748Scperciva# Remove files which we want to delete 2705161748Scpercivainstall_delete () { 2706161748Scperciva # Generate list of new files 2707161748Scperciva cut -f 1 -d '|' < $2 | 2708161748Scperciva sort > newfiles 2709161748Scperciva 2710161748Scperciva # Generate subindex of old files we want to nuke 2711161748Scperciva sort -k 1,1 -t '|' $1 | 2712161748Scperciva join -t '|' -v 1 - newfiles | 2713164600Scperciva sort -r -k 1,1 -t '|' | 2714161748Scperciva cut -f 1,2 -d '|' | 2715161748Scperciva tr '|' ' ' > killfiles 2716161748Scperciva 2717161748Scperciva # Remove the offending bits 2718161748Scperciva while read FPATH TYPE; do 2719161748Scperciva case ${TYPE} in 2720161748Scperciva d) 2721161748Scperciva rmdir ${BASEDIR}/${FPATH} 2722161748Scperciva ;; 2723161748Scperciva f) 2724161748Scperciva rm ${BASEDIR}/${FPATH} 2725161748Scperciva ;; 2726161748Scperciva L) 2727161748Scperciva rm ${BASEDIR}/${FPATH} 2728161748Scperciva ;; 2729161748Scperciva esac 2730161748Scperciva done < killfiles 2731161748Scperciva 2732161748Scperciva # Clean up 2733161748Scperciva rm newfiles killfiles 2734161748Scperciva} 2735161748Scperciva 2736173564Scperciva# Install new files, delete old files, and update linker.hints 2737173564Scpercivainstall_files () { 2738173564Scperciva # If we haven't already dealt with the kernel, deal with it. 2739173564Scperciva if ! [ -f $1/kerneldone ]; then 2740173564Scperciva grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD 2741173564Scperciva grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW 2742173564Scperciva 2743196392Ssimon # Backup current kernel before installing a new one 2744196392Ssimon backup_kernel || return 1 2745196392Ssimon 2746173564Scperciva # Install new files 2747173564Scperciva install_from_index INDEX-NEW || return 1 2748173564Scperciva 2749173564Scperciva # Remove files which need to be deleted 2750173564Scperciva install_delete INDEX-OLD INDEX-NEW || return 1 2751173564Scperciva 2752173564Scperciva # Update linker.hints if necessary 2753173564Scperciva if [ -s INDEX-OLD -o -s INDEX-NEW ]; then 2754173564Scperciva kldxref -R /boot/ 2>/dev/null 2755173564Scperciva fi 2756173564Scperciva 2757173564Scperciva # We've finished updating the kernel. 2758173564Scperciva touch $1/kerneldone 2759173564Scperciva 2760173564Scperciva # Do we need to ask for a reboot now? 2761173564Scperciva if [ -f $1/kernelfirst ] && 2762173564Scperciva [ -s INDEX-OLD -o -s INDEX-NEW ]; then 2763173564Scperciva cat <<-EOF 2764173564Scperciva 2765173564ScpercivaKernel updates have been installed. Please reboot and run 2766173564Scperciva"$0 install" again to finish installing updates. 2767173564Scperciva EOF 2768173564Scperciva exit 0 2769173564Scperciva fi 2770161748Scperciva fi 2771173564Scperciva 2772173564Scperciva # If we haven't already dealt with the world, deal with it. 2773173564Scperciva if ! [ -f $1/worlddone ]; then 2774173564Scperciva # Install new shared libraries next 2775173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2776177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2777173564Scperciva install_from_index INDEX-NEW || return 1 2778173564Scperciva 2779173564Scperciva # Deal with everything else 2780173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2781177601Scperciva grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2782173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2783177601Scperciva grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2784173564Scperciva install_from_index INDEX-NEW || return 1 2785173564Scperciva install_delete INDEX-OLD INDEX-NEW || return 1 2786173564Scperciva 2787173564Scperciva # Rebuild /etc/spwd.db and /etc/pwd.db if necessary. 2788173564Scperciva if [ /etc/master.passwd -nt /etc/spwd.db ] || 2789173564Scperciva [ /etc/master.passwd -nt /etc/pwd.db ]; then 2790173564Scperciva pwd_mkdb /etc/master.passwd 2791173564Scperciva fi 2792173564Scperciva 2793173564Scperciva # Rebuild /etc/login.conf.db if necessary. 2794173564Scperciva if [ /etc/login.conf -nt /etc/login.conf.db ]; then 2795173564Scperciva cap_mkdb /etc/login.conf 2796173564Scperciva fi 2797173564Scperciva 2798173564Scperciva # We've finished installing the world and deleting old files 2799173564Scperciva # which are not shared libraries. 2800173564Scperciva touch $1/worlddone 2801173564Scperciva 2802173564Scperciva # Do we need to ask the user to portupgrade now? 2803173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2804177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2805173564Scperciva cut -f 1 -d '|' | 2806173564Scperciva sort > newfiles 2807173564Scperciva if grep -vE '^/boot/' $1/INDEX-OLD | 2808177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2809173564Scperciva cut -f 1 -d '|' | 2810173564Scperciva sort | 2811173564Scperciva join -v 1 - newfiles | 2812173564Scperciva grep -q .; then 2813173564Scperciva cat <<-EOF 2814173564Scperciva 2815173564ScpercivaCompleting this upgrade requires removing old shared object files. 2816173564ScpercivaPlease rebuild all installed 3rd party software (e.g., programs 2817173564Scpercivainstalled from the ports tree) and then run "$0 install" 2818173564Scpercivaagain to finish installing updates. 2819173564Scperciva EOF 2820173564Scperciva rm newfiles 2821173564Scperciva exit 0 2822173564Scperciva fi 2823173564Scperciva rm newfiles 2824173564Scperciva fi 2825173564Scperciva 2826173564Scperciva # Remove old shared libraries 2827173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2828177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2829173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2830177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2831173564Scperciva install_delete INDEX-OLD INDEX-NEW || return 1 2832173564Scperciva 2833173564Scperciva # Remove temporary files 2834173564Scperciva rm INDEX-OLD INDEX-NEW 2835161748Scperciva} 2836161748Scperciva 2837161748Scperciva# Rearrange bits to allow the installed updates to be rolled back 2838161748Scpercivainstall_setup_rollback () { 2839173564Scperciva # Remove the "reboot after installing kernel", "kernel updated", and 2840173564Scperciva # "finished installing the world" flags if present -- they are 2841173564Scperciva # irrelevant when rolling back updates. 2842173564Scperciva if [ -f ${BDHASH}-install/kernelfirst ]; then 2843173564Scperciva rm ${BDHASH}-install/kernelfirst 2844173564Scperciva rm ${BDHASH}-install/kerneldone 2845173564Scperciva fi 2846173564Scperciva if [ -f ${BDHASH}-install/worlddone ]; then 2847173564Scperciva rm ${BDHASH}-install/worlddone 2848173564Scperciva fi 2849173564Scperciva 2850161748Scperciva if [ -L ${BDHASH}-rollback ]; then 2851161748Scperciva mv ${BDHASH}-rollback ${BDHASH}-install/rollback 2852161748Scperciva fi 2853161748Scperciva 2854161748Scperciva mv ${BDHASH}-install ${BDHASH}-rollback 2855161748Scperciva} 2856161748Scperciva 2857161748Scperciva# Actually install updates 2858161748Scpercivainstall_run () { 2859161748Scperciva echo -n "Installing updates..." 2860161748Scperciva 2861161748Scperciva # Make sure we have all the files we should have 2862161748Scperciva install_verify ${BDHASH}-install/INDEX-OLD \ 2863161748Scperciva ${BDHASH}-install/INDEX-NEW || return 1 2864161748Scperciva 2865161748Scperciva # Remove system immutable flag from files 2866161748Scperciva install_unschg ${BDHASH}-install/INDEX-OLD \ 2867161748Scperciva ${BDHASH}-install/INDEX-NEW || return 1 2868161748Scperciva 2869173564Scperciva # Install new files, delete old files, and update linker.hints 2870173564Scperciva install_files ${BDHASH}-install || return 1 2871161748Scperciva 2872161748Scperciva # Rearrange bits to allow the installed updates to be rolled back 2873161748Scperciva install_setup_rollback 2874161748Scperciva 2875161748Scperciva echo " done." 2876161748Scperciva} 2877161748Scperciva 2878161748Scperciva# Rearrange bits to allow the previous set of updates to be rolled back next. 2879161748Scpercivarollback_setup_rollback () { 2880161748Scperciva if [ -L ${BDHASH}-rollback/rollback ]; then 2881161748Scperciva mv ${BDHASH}-rollback/rollback rollback-tmp 2882161748Scperciva rm -r ${BDHASH}-rollback/ 2883161748Scperciva rm ${BDHASH}-rollback 2884161748Scperciva mv rollback-tmp ${BDHASH}-rollback 2885161748Scperciva else 2886161748Scperciva rm -r ${BDHASH}-rollback/ 2887161748Scperciva rm ${BDHASH}-rollback 2888161748Scperciva fi 2889161748Scperciva} 2890161748Scperciva 2891173564Scperciva# Install old files, delete new files, and update linker.hints 2892173564Scpercivarollback_files () { 2893173671Scperciva # Install old shared library files which don't have the same path as 2894173671Scperciva # a new shared library file. 2895173671Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2896177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2897173671Scperciva cut -f 1 -d '|' | 2898173671Scperciva sort > INDEX-NEW.libs.flist 2899173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2900177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2901173671Scperciva sort -k 1,1 -t '|' - | 2902173671Scperciva join -t '|' -v 1 - INDEX-NEW.libs.flist > INDEX-OLD 2903173564Scperciva install_from_index INDEX-OLD || return 1 2904173564Scperciva 2905173564Scperciva # Deal with files which are neither kernel nor shared library 2906173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2907177601Scperciva grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2908173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2909177601Scperciva grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2910173564Scperciva install_from_index INDEX-OLD || return 1 2911173564Scperciva install_delete INDEX-NEW INDEX-OLD || return 1 2912173564Scperciva 2913173671Scperciva # Install any old shared library files which we didn't install above. 2914173671Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2915177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2916173671Scperciva sort -k 1,1 -t '|' - | 2917173671Scperciva join -t '|' - INDEX-NEW.libs.flist > INDEX-OLD 2918173671Scperciva install_from_index INDEX-OLD || return 1 2919173671Scperciva 2920173564Scperciva # Delete unneeded shared library files 2921173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2922177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2923173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2924177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2925173564Scperciva install_delete INDEX-NEW INDEX-OLD || return 1 2926173564Scperciva 2927173564Scperciva # Deal with kernel files 2928173564Scperciva grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD 2929173564Scperciva grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW 2930173564Scperciva install_from_index INDEX-OLD || return 1 2931173564Scperciva install_delete INDEX-NEW INDEX-OLD || return 1 2932173564Scperciva if [ -s INDEX-OLD -o -s INDEX-NEW ]; then 2933173564Scperciva kldxref -R /boot/ 2>/dev/null 2934173564Scperciva fi 2935173564Scperciva 2936173564Scperciva # Remove temporary files 2937173672Scperciva rm INDEX-OLD INDEX-NEW INDEX-NEW.libs.flist 2938173564Scperciva} 2939173564Scperciva 2940161748Scperciva# Actually rollback updates 2941161748Scpercivarollback_run () { 2942161748Scperciva echo -n "Uninstalling updates..." 2943161748Scperciva 2944161748Scperciva # If there are updates waiting to be installed, remove them; we 2945161748Scperciva # want the user to re-run 'fetch' after rolling back updates. 2946161748Scperciva if [ -L ${BDHASH}-install ]; then 2947161748Scperciva rm -r ${BDHASH}-install/ 2948161748Scperciva rm ${BDHASH}-install 2949161748Scperciva fi 2950161748Scperciva 2951161748Scperciva # Make sure we have all the files we should have 2952161748Scperciva install_verify ${BDHASH}-rollback/INDEX-NEW \ 2953161748Scperciva ${BDHASH}-rollback/INDEX-OLD || return 1 2954161748Scperciva 2955161748Scperciva # Remove system immutable flag from files 2956161748Scperciva install_unschg ${BDHASH}-rollback/INDEX-NEW \ 2957161748Scperciva ${BDHASH}-rollback/INDEX-OLD || return 1 2958161748Scperciva 2959173564Scperciva # Install old files, delete new files, and update linker.hints 2960173564Scperciva rollback_files ${BDHASH}-rollback || return 1 2961161748Scperciva 2962161748Scperciva # Remove the rollback directory and the symlink pointing to it; and 2963161748Scperciva # rearrange bits to allow the previous set of updates to be rolled 2964161748Scperciva # back next. 2965161748Scperciva rollback_setup_rollback 2966161748Scperciva 2967161748Scperciva echo " done." 2968161748Scperciva} 2969161748Scperciva 2970181142Scperciva# Compare INDEX-ALL and INDEX-PRESENT and print warnings about differences. 2971181142ScpercivaIDS_compare () { 2972181425Scperciva # Get all the lines which mismatch in something other than file 2973181425Scperciva # flags. We ignore file flags because sysinstall doesn't seem to 2974181425Scperciva # set them when it installs FreeBSD; warning about these adds a 2975181425Scperciva # very large amount of noise. 2976181425Scperciva cut -f 1-5,7-8 -d '|' $1 > $1.noflags 2977181425Scperciva sort -k 1,1 -t '|' $1.noflags > $1.sorted 2978181425Scperciva cut -f 1-5,7-8 -d '|' $2 | 2979181425Scperciva comm -13 $1.noflags - | 2980181425Scperciva fgrep -v '|-|||||' | 2981181142Scperciva sort -k 1,1 -t '|' | 2982181142Scperciva join -t '|' $1.sorted - > INDEX-NOTMATCHING 2983181142Scperciva 2984181142Scperciva # Ignore files which match IDSIGNOREPATHS. 2985181142Scperciva for X in ${IDSIGNOREPATHS}; do 2986181142Scperciva grep -E "^${X}" INDEX-NOTMATCHING 2987181142Scperciva done | 2988181142Scperciva sort -u | 2989181142Scperciva comm -13 - INDEX-NOTMATCHING > INDEX-NOTMATCHING.tmp 2990181142Scperciva mv INDEX-NOTMATCHING.tmp INDEX-NOTMATCHING 2991181142Scperciva 2992181142Scperciva # Go through the lines and print warnings. 2993181142Scperciva while read LINE; do 2994181142Scperciva FPATH=`echo "${LINE}" | cut -f 1 -d '|'` 2995181142Scperciva TYPE=`echo "${LINE}" | cut -f 2 -d '|'` 2996181142Scperciva OWNER=`echo "${LINE}" | cut -f 3 -d '|'` 2997181142Scperciva GROUP=`echo "${LINE}" | cut -f 4 -d '|'` 2998181142Scperciva PERM=`echo "${LINE}" | cut -f 5 -d '|'` 2999181425Scperciva HASH=`echo "${LINE}" | cut -f 6 -d '|'` 3000181425Scperciva LINK=`echo "${LINE}" | cut -f 7 -d '|'` 3001181425Scperciva P_TYPE=`echo "${LINE}" | cut -f 8 -d '|'` 3002181425Scperciva P_OWNER=`echo "${LINE}" | cut -f 9 -d '|'` 3003181425Scperciva P_GROUP=`echo "${LINE}" | cut -f 10 -d '|'` 3004181425Scperciva P_PERM=`echo "${LINE}" | cut -f 11 -d '|'` 3005181425Scperciva P_HASH=`echo "${LINE}" | cut -f 12 -d '|'` 3006181425Scperciva P_LINK=`echo "${LINE}" | cut -f 13 -d '|'` 3007181142Scperciva 3008181142Scperciva # Warn about different object types. 3009181142Scperciva if ! [ "${TYPE}" = "${P_TYPE}" ]; then 3010181142Scperciva echo -n "${FPATH} is a " 3011181142Scperciva case "${P_TYPE}" in 3012181142Scperciva f) echo -n "regular file, " 3013181142Scperciva ;; 3014181142Scperciva d) echo -n "directory, " 3015181142Scperciva ;; 3016181142Scperciva L) echo -n "symlink, " 3017181142Scperciva ;; 3018181142Scperciva esac 3019181142Scperciva echo -n "but should be a " 3020181142Scperciva case "${TYPE}" in 3021181142Scperciva f) echo -n "regular file." 3022181142Scperciva ;; 3023181142Scperciva d) echo -n "directory." 3024181142Scperciva ;; 3025181142Scperciva L) echo -n "symlink." 3026181142Scperciva ;; 3027181142Scperciva esac 3028181142Scperciva echo 3029181142Scperciva 3030181142Scperciva # Skip other tests, since they don't make sense if 3031181142Scperciva # we're comparing different object types. 3032181142Scperciva continue 3033181142Scperciva fi 3034181142Scperciva 3035181142Scperciva # Warn about different owners. 3036181142Scperciva if ! [ "${OWNER}" = "${P_OWNER}" ]; then 3037181142Scperciva echo -n "${FPATH} is owned by user id ${P_OWNER}, " 3038181142Scperciva echo "but should be owned by user id ${OWNER}." 3039181142Scperciva fi 3040181142Scperciva 3041181142Scperciva # Warn about different groups. 3042181142Scperciva if ! [ "${GROUP}" = "${P_GROUP}" ]; then 3043181142Scperciva echo -n "${FPATH} is owned by group id ${P_GROUP}, " 3044181142Scperciva echo "but should be owned by group id ${GROUP}." 3045181142Scperciva fi 3046181142Scperciva 3047181142Scperciva # Warn about different permissions. We do not warn about 3048181142Scperciva # different permissions on symlinks, since some archivers 3049181142Scperciva # don't extract symlink permissions correctly and they are 3050181142Scperciva # ignored anyway. 3051181142Scperciva if ! [ "${PERM}" = "${P_PERM}" ] && 3052181142Scperciva ! [ "${TYPE}" = "L" ]; then 3053181142Scperciva echo -n "${FPATH} has ${P_PERM} permissions, " 3054181142Scperciva echo "but should have ${PERM} permissions." 3055181142Scperciva fi 3056181142Scperciva 3057181142Scperciva # Warn about different file hashes / symlink destinations. 3058181142Scperciva if ! [ "${HASH}" = "${P_HASH}" ]; then 3059181142Scperciva if [ "${TYPE}" = "L" ]; then 3060181142Scperciva echo -n "${FPATH} is a symlink to ${P_HASH}, " 3061181142Scperciva echo "but should be a symlink to ${HASH}." 3062181142Scperciva fi 3063181142Scperciva if [ "${TYPE}" = "f" ]; then 3064181142Scperciva echo -n "${FPATH} has SHA256 hash ${P_HASH}, " 3065181142Scperciva echo "but should have SHA256 hash ${HASH}." 3066181142Scperciva fi 3067181142Scperciva fi 3068181142Scperciva 3069181142Scperciva # We don't warn about different hard links, since some 3070181142Scperciva # some archivers break hard links, and as long as the 3071181142Scperciva # underlying data is correct they really don't matter. 3072181142Scperciva done < INDEX-NOTMATCHING 3073181142Scperciva 3074181142Scperciva # Clean up 3075181425Scperciva rm $1 $1.noflags $1.sorted $2 INDEX-NOTMATCHING 3076181142Scperciva} 3077181142Scperciva 3078181142Scperciva# Do the work involved in comparing the system to a "known good" index 3079181142ScpercivaIDS_run () { 3080181142Scperciva workdir_init || return 1 3081181142Scperciva 3082181142Scperciva # Prepare the mirror list. 3083181142Scperciva fetch_pick_server_init && fetch_pick_server 3084181142Scperciva 3085181142Scperciva # Try to fetch the public key until we run out of servers. 3086181142Scperciva while ! fetch_key; do 3087181142Scperciva fetch_pick_server || return 1 3088181142Scperciva done 3089181142Scperciva 3090181142Scperciva # Try to fetch the metadata index signature ("tag") until we run 3091181142Scperciva # out of available servers; and sanity check the downloaded tag. 3092181142Scperciva while ! fetch_tag; do 3093181142Scperciva fetch_pick_server || return 1 3094181142Scperciva done 3095181142Scperciva fetch_tagsanity || return 1 3096181142Scperciva 3097181142Scperciva # Fetch INDEX-OLD and INDEX-ALL. 3098181142Scperciva fetch_metadata INDEX-OLD INDEX-ALL || return 1 3099181142Scperciva 3100181142Scperciva # Generate filtered INDEX-OLD and INDEX-ALL files containing only 3101181142Scperciva # the components we want and without anything marked as "Ignore". 3102181142Scperciva fetch_filter_metadata INDEX-OLD || return 1 3103181142Scperciva fetch_filter_metadata INDEX-ALL || return 1 3104181142Scperciva 3105181142Scperciva # Merge the INDEX-OLD and INDEX-ALL files into INDEX-ALL. 3106181142Scperciva sort INDEX-OLD INDEX-ALL > INDEX-ALL.tmp 3107181142Scperciva mv INDEX-ALL.tmp INDEX-ALL 3108181142Scperciva rm INDEX-OLD 3109181142Scperciva 3110181142Scperciva # Translate /boot/${KERNCONF} to ${KERNELDIR} 3111181142Scperciva fetch_filter_kernel_names INDEX-ALL ${KERNCONF} 3112181142Scperciva 3113181142Scperciva # Inspect the system and generate an INDEX-PRESENT file. 3114181142Scperciva fetch_inspect_system INDEX-ALL INDEX-PRESENT /dev/null || return 1 3115181142Scperciva 3116181142Scperciva # Compare INDEX-ALL and INDEX-PRESENT and print warnings about any 3117181142Scperciva # differences. 3118181142Scperciva IDS_compare INDEX-ALL INDEX-PRESENT 3119181142Scperciva} 3120181142Scperciva 3121161748Scperciva#### Main functions -- call parameter-handling and core functions 3122161748Scperciva 3123161748Scperciva# Using the command line, configuration file, and defaults, 3124161748Scperciva# set all the parameters which are needed later. 3125161748Scpercivaget_params () { 3126161748Scperciva init_params 3127161748Scperciva parse_cmdline $@ 3128161748Scperciva parse_conffile 3129161748Scperciva default_params 3130161748Scperciva} 3131161748Scperciva 3132161748Scperciva# Fetch command. Make sure that we're being called 3133161748Scperciva# interactively, then run fetch_check_params and fetch_run 3134161748Scpercivacmd_fetch () { 3135161748Scperciva if [ ! -t 0 ]; then 3136161748Scperciva echo -n "`basename $0` fetch should not " 3137161748Scperciva echo "be run non-interactively." 3138161748Scperciva echo "Run `basename $0` cron instead." 3139161748Scperciva exit 1 3140161748Scperciva fi 3141161748Scperciva fetch_check_params 3142161748Scperciva fetch_run || exit 1 3143161748Scperciva} 3144161748Scperciva 3145161748Scperciva# Cron command. Make sure the parameters are sensible; wait 3146161748Scperciva# rand(3600) seconds; then fetch updates. While fetching updates, 3147161748Scperciva# send output to a temporary file; only print that file if the 3148161748Scperciva# fetching failed. 3149161748Scpercivacmd_cron () { 3150161748Scperciva fetch_check_params 3151161748Scperciva sleep `jot -r 1 0 3600` 3152161748Scperciva 3153161748Scperciva TMPFILE=`mktemp /tmp/freebsd-update.XXXXXX` || exit 1 3154161748Scperciva if ! fetch_run >> ${TMPFILE} || 3155161748Scperciva ! grep -q "No updates needed" ${TMPFILE} || 3156161748Scperciva [ ${VERBOSELEVEL} = "debug" ]; then 3157161748Scperciva mail -s "`hostname` security updates" ${MAILTO} < ${TMPFILE} 3158161748Scperciva fi 3159161748Scperciva 3160161748Scperciva rm ${TMPFILE} 3161161748Scperciva} 3162161748Scperciva 3163173564Scperciva# Fetch files for upgrading to a new release. 3164173564Scpercivacmd_upgrade () { 3165173564Scperciva upgrade_check_params 3166173564Scperciva upgrade_run || exit 1 3167173564Scperciva} 3168173564Scperciva 3169161748Scperciva# Install downloaded updates. 3170161748Scpercivacmd_install () { 3171161748Scperciva install_check_params 3172161748Scperciva install_run || exit 1 3173161748Scperciva} 3174161748Scperciva 3175161748Scperciva# Rollback most recently installed updates. 3176161748Scpercivacmd_rollback () { 3177161748Scperciva rollback_check_params 3178161748Scperciva rollback_run || exit 1 3179161748Scperciva} 3180161748Scperciva 3181181142Scperciva# Compare system against a "known good" index. 3182181142Scpercivacmd_IDS () { 3183181142Scperciva IDS_check_params 3184181142Scperciva IDS_run || exit 1 3185181142Scperciva} 3186181142Scperciva 3187161748Scperciva#### Entry point 3188161748Scperciva 3189161748Scperciva# Make sure we find utilities from the base system 3190161748Scpercivaexport PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH} 3191161748Scperciva 3192163564Scperciva# Set LC_ALL in order to avoid problems with character ranges like [A-Z]. 3193163564Scpercivaexport LC_ALL=C 3194163564Scperciva 3195161748Scpercivaget_params $@ 3196161748Scpercivafor COMMAND in ${COMMANDS}; do 3197161748Scperciva cmd_${COMMAND} 3198161748Scpercivadone 3199