freebsd-update.sh revision 196392
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 196392 2009-08-19 20:47:31Z simon $ 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 287173564Scperciva} 288173564Scperciva 289161748Scperciva# Define what happens to output of utilities 290161748Scpercivaconfig_VerboseLevel () { 291161748Scperciva if [ -z ${VERBOSELEVEL} ]; then 292161748Scperciva case $1 in 293161748Scperciva [Dd][Ee][Bb][Uu][Gg]) 294161748Scperciva VERBOSELEVEL=debug 295161748Scperciva ;; 296161748Scperciva [Nn][Oo][Ss][Tt][Aa][Tt][Ss]) 297161748Scperciva VERBOSELEVEL=nostats 298161748Scperciva ;; 299161748Scperciva [Ss][Tt][Aa][Tt][Ss]) 300161748Scperciva VERBOSELEVEL=stats 301161748Scperciva ;; 302161748Scperciva *) 303161748Scperciva return 1 304161748Scperciva ;; 305161748Scperciva esac 306161748Scperciva else 307161748Scperciva return 1 308161748Scperciva fi 309161748Scperciva} 310161748Scperciva 311196392Ssimonconfig_BackupKernel () { 312196392Ssimon if [ -z ${BACKUPKERNEL} ]; then 313196392Ssimon case $1 in 314196392Ssimon [Yy][Ee][Ss]) 315196392Ssimon BACKUPKERNEL=yes 316196392Ssimon ;; 317196392Ssimon [Nn][Oo]) 318196392Ssimon BACKUPKERNEL=no 319196392Ssimon ;; 320196392Ssimon *) 321196392Ssimon return 1 322196392Ssimon ;; 323196392Ssimon esac 324196392Ssimon else 325196392Ssimon return 1 326196392Ssimon fi 327196392Ssimon} 328196392Ssimon 329196392Ssimonconfig_BackupKernelDir () { 330196392Ssimon if [ -z ${BACKUPKERNELDIR} ]; then 331196392Ssimon if [ -z "$1" ]; then 332196392Ssimon echo "BackupKernelDir set to empty dir" 333196392Ssimon return 1 334196392Ssimon fi 335196392Ssimon 336196392Ssimon # We check for some paths which would be extremely odd 337196392Ssimon # to use, but which could cause a lot of problems if 338196392Ssimon # used. 339196392Ssimon case $1 in 340196392Ssimon /|/bin|/boot|/etc|/lib|/libexec|/sbin|/usr|/var) 341196392Ssimon echo "BackupKernelDir set to invalid path $1" 342196392Ssimon return 1 343196392Ssimon ;; 344196392Ssimon /*) 345196392Ssimon BACKUPKERNELDIR=$1 346196392Ssimon ;; 347196392Ssimon *) 348196392Ssimon echo "BackupKernelDir ($1) is not an absolute path" 349196392Ssimon return 1 350196392Ssimon ;; 351196392Ssimon esac 352196392Ssimon else 353196392Ssimon return 1 354196392Ssimon fi 355196392Ssimon} 356196392Ssimon 357196392Ssimonconfig_BackupKernelSymbolFiles () { 358196392Ssimon if [ -z ${BACKUPKERNELSYMBOLFILES} ]; then 359196392Ssimon case $1 in 360196392Ssimon [Yy][Ee][Ss]) 361196392Ssimon BACKUPKERNELSYMBOLFILES=yes 362196392Ssimon ;; 363196392Ssimon [Nn][Oo]) 364196392Ssimon BACKUPKERNELSYMBOLFILES=no 365196392Ssimon ;; 366196392Ssimon *) 367196392Ssimon return 1 368196392Ssimon ;; 369196392Ssimon esac 370196392Ssimon else 371196392Ssimon return 1 372196392Ssimon fi 373196392Ssimon} 374196392Ssimon 375161748Scperciva# Handle one line of configuration 376161748Scpercivaconfigline () { 377161748Scperciva if [ $# -eq 0 ]; then 378161748Scperciva return 379161748Scperciva fi 380161748Scperciva 381161748Scperciva OPT=$1 382161748Scperciva shift 383161748Scperciva config_${OPT} $@ 384161748Scperciva} 385161748Scperciva 386161748Scperciva#### Parameter handling functions. 387161748Scperciva 388161748Scperciva# Initialize parameters to null, just in case they're 389161748Scperciva# set in the environment. 390161748Scpercivainit_params () { 391161748Scperciva # Configration settings 392161748Scperciva nullconfig 393161748Scperciva 394161748Scperciva # No configuration file set yet 395161748Scperciva CONFFILE="" 396161748Scperciva 397161748Scperciva # No commands specified yet 398161748Scperciva COMMANDS="" 399161748Scperciva} 400161748Scperciva 401161748Scperciva# Parse the command line 402161748Scpercivaparse_cmdline () { 403161748Scperciva while [ $# -gt 0 ]; do 404161748Scperciva case "$1" in 405161748Scperciva # Location of configuration file 406161748Scperciva -f) 407161748Scperciva if [ $# -eq 1 ]; then usage; fi 408161748Scperciva if [ ! -z "${CONFFILE}" ]; then usage; fi 409161748Scperciva shift; CONFFILE="$1" 410161748Scperciva ;; 411161748Scperciva 412161748Scperciva # Configuration file equivalents 413161748Scperciva -b) 414161748Scperciva if [ $# -eq 1 ]; then usage; fi; shift 415161748Scperciva config_BaseDir $1 || usage 416161748Scperciva ;; 417161748Scperciva -d) 418161748Scperciva if [ $# -eq 1 ]; then usage; fi; shift 419161748Scperciva config_WorkDir $1 || usage 420161748Scperciva ;; 421161748Scperciva -k) 422161748Scperciva if [ $# -eq 1 ]; then usage; fi; shift 423161748Scperciva config_KeyPrint $1 || usage 424161748Scperciva ;; 425161748Scperciva -s) 426161748Scperciva if [ $# -eq 1 ]; then usage; fi; shift 427161748Scperciva config_ServerName $1 || usage 428161748Scperciva ;; 429173564Scperciva -r) 430173564Scperciva if [ $# -eq 1 ]; then usage; fi; shift 431173564Scperciva config_TargetRelease $1 || usage 432173564Scperciva ;; 433161748Scperciva -t) 434161748Scperciva if [ $# -eq 1 ]; then usage; fi; shift 435161748Scperciva config_MailTo $1 || usage 436161748Scperciva ;; 437161748Scperciva -v) 438161748Scperciva if [ $# -eq 1 ]; then usage; fi; shift 439161748Scperciva config_VerboseLevel $1 || usage 440161748Scperciva ;; 441161748Scperciva 442161748Scperciva # Aliases for "-v debug" and "-v nostats" 443161748Scperciva --debug) 444161748Scperciva config_VerboseLevel debug || usage 445161748Scperciva ;; 446161748Scperciva --no-stats) 447161748Scperciva config_VerboseLevel nostats || usage 448161748Scperciva ;; 449161748Scperciva 450161748Scperciva # Commands 451181142Scperciva cron | fetch | upgrade | install | rollback | IDS) 452161748Scperciva COMMANDS="${COMMANDS} $1" 453161748Scperciva ;; 454161748Scperciva 455161748Scperciva # Anything else is an error 456161748Scperciva *) 457161748Scperciva usage 458161748Scperciva ;; 459161748Scperciva esac 460161748Scperciva shift 461161748Scperciva done 462161748Scperciva 463161748Scperciva # Make sure we have at least one command 464161748Scperciva if [ -z "${COMMANDS}" ]; then 465161748Scperciva usage 466161748Scperciva fi 467161748Scperciva} 468161748Scperciva 469161748Scperciva# Parse the configuration file 470161748Scpercivaparse_conffile () { 471161748Scperciva # If a configuration file was specified on the command line, check 472161748Scperciva # that it exists and is readable. 473161748Scperciva if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then 474161748Scperciva echo -n "File does not exist " 475161748Scperciva echo -n "or is not readable: " 476161748Scperciva echo ${CONFFILE} 477161748Scperciva exit 1 478161748Scperciva fi 479161748Scperciva 480161748Scperciva # If a configuration file was not specified on the command line, 481161748Scperciva # use the default configuration file path. If that default does 482161748Scperciva # not exist, give up looking for any configuration. 483161748Scperciva if [ -z "${CONFFILE}" ]; then 484161748Scperciva CONFFILE="/etc/freebsd-update.conf" 485161748Scperciva if [ ! -r "${CONFFILE}" ]; then 486161748Scperciva return 487161748Scperciva fi 488161748Scperciva fi 489161748Scperciva 490161748Scperciva # Save the configuration options specified on the command line, and 491161748Scperciva # clear all the options in preparation for reading the config file. 492161748Scperciva saveconfig 493161748Scperciva nullconfig 494161748Scperciva 495161748Scperciva # Read the configuration file. Anything after the first '#' is 496161748Scperciva # ignored, and any blank lines are ignored. 497161748Scperciva L=0 498161748Scperciva while read LINE; do 499161748Scperciva L=$(($L + 1)) 500161748Scperciva LINEX=`echo "${LINE}" | cut -f 1 -d '#'` 501161748Scperciva if ! configline ${LINEX}; then 502161748Scperciva echo "Error processing configuration file, line $L:" 503161748Scperciva echo "==> ${LINE}" 504161748Scperciva exit 1 505161748Scperciva fi 506161748Scperciva done < ${CONFFILE} 507161748Scperciva 508161748Scperciva # Merge the settings read from the configuration file with those 509161748Scperciva # provided at the command line. 510161748Scperciva mergeconfig 511161748Scperciva} 512161748Scperciva 513161748Scperciva# Provide some default parameters 514161748Scpercivadefault_params () { 515161748Scperciva # Save any parameters already configured, and clear the slate 516161748Scperciva saveconfig 517161748Scperciva nullconfig 518161748Scperciva 519161748Scperciva # Default configurations 520161748Scperciva config_WorkDir /var/db/freebsd-update 521161748Scperciva config_MailTo root 522161748Scperciva config_AllowAdd yes 523161748Scperciva config_AllowDelete yes 524161748Scperciva config_KeepModifiedMetadata yes 525161748Scperciva config_BaseDir / 526161748Scperciva config_VerboseLevel stats 527173564Scperciva config_StrictComponents no 528196392Ssimon config_BackupKernel yes 529196392Ssimon config_BackupKernelDir /boot/kernel.old 530196392Ssimon config_BackupKernelSymbolFiles no 531161748Scperciva 532161748Scperciva # Merge these defaults into the earlier-configured settings 533161748Scperciva mergeconfig 534161748Scperciva} 535161748Scperciva 536161748Scperciva# Set utility output filtering options, based on ${VERBOSELEVEL} 537161748Scpercivafetch_setup_verboselevel () { 538161748Scperciva case ${VERBOSELEVEL} in 539161748Scperciva debug) 540161748Scperciva QUIETREDIR="/dev/stderr" 541161748Scperciva QUIETFLAG=" " 542161748Scperciva STATSREDIR="/dev/stderr" 543161748Scperciva DDSTATS=".." 544161748Scperciva XARGST="-t" 545161748Scperciva NDEBUG=" " 546161748Scperciva ;; 547161748Scperciva nostats) 548161748Scperciva QUIETREDIR="" 549161748Scperciva QUIETFLAG="" 550161748Scperciva STATSREDIR="/dev/null" 551161748Scperciva DDSTATS=".." 552161748Scperciva XARGST="" 553161748Scperciva NDEBUG="" 554161748Scperciva ;; 555161748Scperciva stats) 556161748Scperciva QUIETREDIR="/dev/null" 557161748Scperciva QUIETFLAG="-q" 558161748Scperciva STATSREDIR="/dev/stdout" 559161748Scperciva DDSTATS="" 560161748Scperciva XARGST="" 561161748Scperciva NDEBUG="-n" 562161748Scperciva ;; 563161748Scperciva esac 564161748Scperciva} 565161748Scperciva 566161748Scperciva# Perform sanity checks and set some final parameters 567161748Scperciva# in preparation for fetching files. Figure out which 568161748Scperciva# set of updates should be downloaded: If the user is 569161748Scperciva# running *-p[0-9]+, strip off the last part; if the 570161748Scperciva# user is running -SECURITY, call it -RELEASE. Chdir 571161748Scperciva# into the working directory. 572161748Scpercivafetch_check_params () { 573161748Scperciva export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)" 574161748Scperciva 575161748Scperciva _SERVERNAME_z=\ 576161748Scperciva"SERVERNAME must be given via command line or configuration file." 577161748Scperciva _KEYPRINT_z="Key must be given via -k option or configuration file." 578161748Scperciva _KEYPRINT_bad="Invalid key fingerprint: " 579161748Scperciva _WORKDIR_bad="Directory does not exist or is not writable: " 580161748Scperciva 581161748Scperciva if [ -z "${SERVERNAME}" ]; then 582161748Scperciva echo -n "`basename $0`: " 583161748Scperciva echo "${_SERVERNAME_z}" 584161748Scperciva exit 1 585161748Scperciva fi 586161748Scperciva if [ -z "${KEYPRINT}" ]; then 587161748Scperciva echo -n "`basename $0`: " 588161748Scperciva echo "${_KEYPRINT_z}" 589161748Scperciva exit 1 590161748Scperciva fi 591161748Scperciva if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then 592161748Scperciva echo -n "`basename $0`: " 593161748Scperciva echo -n "${_KEYPRINT_bad}" 594161748Scperciva echo ${KEYPRINT} 595161748Scperciva exit 1 596161748Scperciva fi 597161748Scperciva if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 598161748Scperciva echo -n "`basename $0`: " 599161748Scperciva echo -n "${_WORKDIR_bad}" 600161748Scperciva echo ${WORKDIR} 601161748Scperciva exit 1 602161748Scperciva fi 603161748Scperciva cd ${WORKDIR} || exit 1 604161748Scperciva 605161748Scperciva # Generate release number. The s/SECURITY/RELEASE/ bit exists 606161748Scperciva # to provide an upgrade path for FreeBSD Update 1.x users, since 607161748Scperciva # the kernels provided by FreeBSD Update 1.x are always labelled 608161748Scperciva # as X.Y-SECURITY. 609161748Scperciva RELNUM=`uname -r | 610161748Scperciva sed -E 's,-p[0-9]+,,' | 611161748Scperciva sed -E 's,-SECURITY,-RELEASE,'` 612161748Scperciva ARCH=`uname -m` 613161748Scperciva FETCHDIR=${RELNUM}/${ARCH} 614173564Scperciva PATCHDIR=${RELNUM}/${ARCH}/bp 615161748Scperciva 616161748Scperciva # Figure out what directory contains the running kernel 617161748Scperciva BOOTFILE=`sysctl -n kern.bootfile` 618161748Scperciva KERNELDIR=${BOOTFILE%/kernel} 619161748Scperciva if ! [ -d ${KERNELDIR} ]; then 620161748Scperciva echo "Cannot identify running kernel" 621161748Scperciva exit 1 622161748Scperciva fi 623161748Scperciva 624167189Scperciva # Figure out what kernel configuration is running. We start with 625167189Scperciva # the output of `uname -i`, and then make the following adjustments: 626167189Scperciva # 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config 627167189Scperciva # file says "ident SMP-GENERIC", I don't know... 628167189Scperciva # 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64" 629167189Scperciva # _and_ `sysctl kern.version` contains a line which ends "/SMP", then 630167189Scperciva # we're running an SMP kernel. This mis-identification is a bug 631167189Scperciva # which was fixed in 6.2-STABLE. 632167189Scperciva KERNCONF=`uname -i` 633167189Scperciva if [ ${KERNCONF} = "SMP-GENERIC" ]; then 634167189Scperciva KERNCONF=SMP 635167189Scperciva fi 636167189Scperciva if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then 637167189Scperciva if sysctl kern.version | grep -qE '/SMP$'; then 638167189Scperciva KERNCONF=SMP 639167189Scperciva fi 640167189Scperciva fi 641167189Scperciva 642161748Scperciva # Define some paths 643161748Scperciva BSPATCH=/usr/bin/bspatch 644161748Scperciva SHA256=/sbin/sha256 645161748Scperciva PHTTPGET=/usr/libexec/phttpget 646161748Scperciva 647161748Scperciva # Set up variables relating to VERBOSELEVEL 648161748Scperciva fetch_setup_verboselevel 649161748Scperciva 650161748Scperciva # Construct a unique name from ${BASEDIR} 651161748Scperciva BDHASH=`echo ${BASEDIR} | sha256 -q` 652161748Scperciva} 653161748Scperciva 654173564Scperciva# Perform sanity checks etc. before fetching upgrades. 655173564Scpercivaupgrade_check_params () { 656173564Scperciva fetch_check_params 657173564Scperciva 658173564Scperciva # Unless set otherwise, we're upgrading to the same kernel config. 659173564Scperciva NKERNCONF=${KERNCONF} 660173564Scperciva 661173564Scperciva # We need TARGETRELEASE set 662173564Scperciva _TARGETRELEASE_z="Release target must be specified via -r option." 663173564Scperciva if [ -z "${TARGETRELEASE}" ]; then 664173564Scperciva echo -n "`basename $0`: " 665173564Scperciva echo "${_TARGETRELEASE_z}" 666173564Scperciva exit 1 667173564Scperciva fi 668173564Scperciva 669173564Scperciva # The target release should be != the current release. 670173564Scperciva if [ "${TARGETRELEASE}" = "${RELNUM}" ]; then 671173564Scperciva echo -n "`basename $0`: " 672173564Scperciva echo "Cannot upgrade from ${RELNUM} to itself" 673173564Scperciva exit 1 674173564Scperciva fi 675173564Scperciva 676173564Scperciva # Turning off AllowAdd or AllowDelete is a bad idea for upgrades. 677173564Scperciva if [ "${ALLOWADD}" = "no" ]; then 678173564Scperciva echo -n "`basename $0`: " 679173564Scperciva echo -n "WARNING: \"AllowAdd no\" is a bad idea " 680173564Scperciva echo "when upgrading between releases." 681173564Scperciva echo 682173564Scperciva fi 683173564Scperciva if [ "${ALLOWDELETE}" = "no" ]; then 684173564Scperciva echo -n "`basename $0`: " 685173564Scperciva echo -n "WARNING: \"AllowDelete no\" is a bad idea " 686173564Scperciva echo "when upgrading between releases." 687173564Scperciva echo 688173564Scperciva fi 689173564Scperciva 690173564Scperciva # Set EDITOR to /usr/bin/vi if it isn't already set 691173564Scperciva : ${EDITOR:='/usr/bin/vi'} 692173564Scperciva} 693173564Scperciva 694161748Scperciva# Perform sanity checks and set some final parameters in 695161748Scperciva# preparation for installing updates. 696161748Scpercivainstall_check_params () { 697161748Scperciva # Check that we are root. All sorts of things won't work otherwise. 698161748Scperciva if [ `id -u` != 0 ]; then 699161748Scperciva echo "You must be root to run this." 700161748Scperciva exit 1 701161748Scperciva fi 702161748Scperciva 703173441Scperciva # Check that securelevel <= 0. Otherwise we can't update schg files. 704173441Scperciva if [ `sysctl -n kern.securelevel` -gt 0 ]; then 705173441Scperciva echo "Updates cannot be installed when the system securelevel" 706173441Scperciva echo "is greater than zero." 707173441Scperciva exit 1 708173441Scperciva fi 709173441Scperciva 710161748Scperciva # Check that we have a working directory 711161748Scperciva _WORKDIR_bad="Directory does not exist or is not writable: " 712161748Scperciva if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 713161748Scperciva echo -n "`basename $0`: " 714161748Scperciva echo -n "${_WORKDIR_bad}" 715161748Scperciva echo ${WORKDIR} 716161748Scperciva exit 1 717161748Scperciva fi 718161748Scperciva cd ${WORKDIR} || exit 1 719161748Scperciva 720161748Scperciva # Construct a unique name from ${BASEDIR} 721161748Scperciva BDHASH=`echo ${BASEDIR} | sha256 -q` 722161748Scperciva 723161748Scperciva # Check that we have updates ready to install 724161748Scperciva if ! [ -L ${BDHASH}-install ]; then 725161748Scperciva echo "No updates are available to install." 726161748Scperciva echo "Run '$0 fetch' first." 727161748Scperciva exit 1 728161748Scperciva fi 729161748Scperciva if ! [ -f ${BDHASH}-install/INDEX-OLD ] || 730161748Scperciva ! [ -f ${BDHASH}-install/INDEX-NEW ]; then 731161748Scperciva echo "Update manifest is corrupt -- this should never happen." 732161748Scperciva echo "Re-run '$0 fetch'." 733161748Scperciva exit 1 734161748Scperciva fi 735196392Ssimon 736196392Ssimon # Figure out what directory contains the running kernel 737196392Ssimon BOOTFILE=`sysctl -n kern.bootfile` 738196392Ssimon KERNELDIR=${BOOTFILE%/kernel} 739196392Ssimon if ! [ -d ${KERNELDIR} ]; then 740196392Ssimon echo "Cannot identify running kernel" 741196392Ssimon exit 1 742196392Ssimon fi 743161748Scperciva} 744161748Scperciva 745161748Scperciva# Perform sanity checks and set some final parameters in 746161748Scperciva# preparation for UNinstalling updates. 747161748Scpercivarollback_check_params () { 748161748Scperciva # Check that we are root. All sorts of things won't work otherwise. 749161748Scperciva if [ `id -u` != 0 ]; then 750161748Scperciva echo "You must be root to run this." 751161748Scperciva exit 1 752161748Scperciva fi 753161748Scperciva 754161748Scperciva # Check that we have a working directory 755161748Scperciva _WORKDIR_bad="Directory does not exist or is not writable: " 756161748Scperciva if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 757161748Scperciva echo -n "`basename $0`: " 758161748Scperciva echo -n "${_WORKDIR_bad}" 759161748Scperciva echo ${WORKDIR} 760161748Scperciva exit 1 761161748Scperciva fi 762161748Scperciva cd ${WORKDIR} || exit 1 763161748Scperciva 764161748Scperciva # Construct a unique name from ${BASEDIR} 765161748Scperciva BDHASH=`echo ${BASEDIR} | sha256 -q` 766161748Scperciva 767161748Scperciva # Check that we have updates ready to rollback 768161748Scperciva if ! [ -L ${BDHASH}-rollback ]; then 769161748Scperciva echo "No rollback directory found." 770161748Scperciva exit 1 771161748Scperciva fi 772161748Scperciva if ! [ -f ${BDHASH}-rollback/INDEX-OLD ] || 773161748Scperciva ! [ -f ${BDHASH}-rollback/INDEX-NEW ]; then 774161748Scperciva echo "Update manifest is corrupt -- this should never happen." 775161748Scperciva exit 1 776161748Scperciva fi 777161748Scperciva} 778161748Scperciva 779181142Scperciva# Perform sanity checks and set some final parameters 780181142Scperciva# in preparation for comparing the system against the 781181142Scperciva# published index. Figure out which index we should 782181142Scperciva# compare against: If the user is running *-p[0-9]+, 783181142Scperciva# strip off the last part; if the user is running 784181142Scperciva# -SECURITY, call it -RELEASE. Chdir into the working 785181142Scperciva# directory. 786181142ScpercivaIDS_check_params () { 787181142Scperciva export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)" 788181142Scperciva 789181142Scperciva _SERVERNAME_z=\ 790181142Scperciva"SERVERNAME must be given via command line or configuration file." 791181142Scperciva _KEYPRINT_z="Key must be given via -k option or configuration file." 792181142Scperciva _KEYPRINT_bad="Invalid key fingerprint: " 793181142Scperciva _WORKDIR_bad="Directory does not exist or is not writable: " 794181142Scperciva 795181142Scperciva if [ -z "${SERVERNAME}" ]; then 796181142Scperciva echo -n "`basename $0`: " 797181142Scperciva echo "${_SERVERNAME_z}" 798181142Scperciva exit 1 799181142Scperciva fi 800181142Scperciva if [ -z "${KEYPRINT}" ]; then 801181142Scperciva echo -n "`basename $0`: " 802181142Scperciva echo "${_KEYPRINT_z}" 803181142Scperciva exit 1 804181142Scperciva fi 805181142Scperciva if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then 806181142Scperciva echo -n "`basename $0`: " 807181142Scperciva echo -n "${_KEYPRINT_bad}" 808181142Scperciva echo ${KEYPRINT} 809181142Scperciva exit 1 810181142Scperciva fi 811181142Scperciva if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 812181142Scperciva echo -n "`basename $0`: " 813181142Scperciva echo -n "${_WORKDIR_bad}" 814181142Scperciva echo ${WORKDIR} 815181142Scperciva exit 1 816181142Scperciva fi 817181142Scperciva cd ${WORKDIR} || exit 1 818181142Scperciva 819181142Scperciva # Generate release number. The s/SECURITY/RELEASE/ bit exists 820181142Scperciva # to provide an upgrade path for FreeBSD Update 1.x users, since 821181142Scperciva # the kernels provided by FreeBSD Update 1.x are always labelled 822181142Scperciva # as X.Y-SECURITY. 823181142Scperciva RELNUM=`uname -r | 824181142Scperciva sed -E 's,-p[0-9]+,,' | 825181142Scperciva sed -E 's,-SECURITY,-RELEASE,'` 826181142Scperciva ARCH=`uname -m` 827181142Scperciva FETCHDIR=${RELNUM}/${ARCH} 828181142Scperciva PATCHDIR=${RELNUM}/${ARCH}/bp 829181142Scperciva 830181142Scperciva # Figure out what directory contains the running kernel 831181142Scperciva BOOTFILE=`sysctl -n kern.bootfile` 832181142Scperciva KERNELDIR=${BOOTFILE%/kernel} 833181142Scperciva if ! [ -d ${KERNELDIR} ]; then 834181142Scperciva echo "Cannot identify running kernel" 835181142Scperciva exit 1 836181142Scperciva fi 837181142Scperciva 838181142Scperciva # Figure out what kernel configuration is running. We start with 839181142Scperciva # the output of `uname -i`, and then make the following adjustments: 840181142Scperciva # 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config 841181142Scperciva # file says "ident SMP-GENERIC", I don't know... 842181142Scperciva # 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64" 843181142Scperciva # _and_ `sysctl kern.version` contains a line which ends "/SMP", then 844181142Scperciva # we're running an SMP kernel. This mis-identification is a bug 845181142Scperciva # which was fixed in 6.2-STABLE. 846181142Scperciva KERNCONF=`uname -i` 847181142Scperciva if [ ${KERNCONF} = "SMP-GENERIC" ]; then 848181142Scperciva KERNCONF=SMP 849181142Scperciva fi 850181142Scperciva if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then 851181142Scperciva if sysctl kern.version | grep -qE '/SMP$'; then 852181142Scperciva KERNCONF=SMP 853181142Scperciva fi 854181142Scperciva fi 855181142Scperciva 856181142Scperciva # Define some paths 857181142Scperciva SHA256=/sbin/sha256 858181142Scperciva PHTTPGET=/usr/libexec/phttpget 859181142Scperciva 860181142Scperciva # Set up variables relating to VERBOSELEVEL 861181142Scperciva fetch_setup_verboselevel 862181142Scperciva} 863181142Scperciva 864161748Scperciva#### Core functionality -- the actual work gets done here 865161748Scperciva 866161748Scperciva# Use an SRV query to pick a server. If the SRV query doesn't provide 867161748Scperciva# a useful answer, use the server name specified by the user. 868161748Scperciva# Put another way... look up _http._tcp.${SERVERNAME} and pick a server 869161748Scperciva# from that; or if no servers are returned, use ${SERVERNAME}. 870161748Scperciva# This allows a user to specify "portsnap.freebsd.org" (in which case 871161748Scperciva# portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org" 872161748Scperciva# (in which case portsnap will use that particular server, since there 873161748Scperciva# won't be an SRV entry for that name). 874161748Scperciva# 875161748Scperciva# We ignore the Port field, since we are always going to use port 80. 876161748Scperciva 877161748Scperciva# Fetch the mirror list, but do not pick a mirror yet. Returns 1 if 878161748Scperciva# no mirrors are available for any reason. 879161748Scpercivafetch_pick_server_init () { 880161748Scperciva : > serverlist_tried 881161748Scperciva 882161748Scperciva# Check that host(1) exists (i.e., that the system wasn't built with the 883161748Scperciva# WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist. 884161748Scperciva if ! which -s host; then 885161748Scperciva : > serverlist_full 886161748Scperciva return 1 887161748Scperciva fi 888161748Scperciva 889161748Scperciva echo -n "Looking up ${SERVERNAME} mirrors... " 890161748Scperciva 891161748Scperciva# Issue the SRV query and pull out the Priority, Weight, and Target fields. 892161748Scperciva# BIND 9 prints "$name has SRV record ..." while BIND 8 prints 893161748Scperciva# "$name server selection ..."; we allow either format. 894161748Scperciva MLIST="_http._tcp.${SERVERNAME}" 895161748Scperciva host -t srv "${MLIST}" | 896161748Scperciva sed -nE "s/${MLIST} (has SRV record|server selection) //p" | 897161748Scperciva cut -f 1,2,4 -d ' ' | 898161748Scperciva sed -e 's/\.$//' | 899161748Scperciva sort > serverlist_full 900161748Scperciva 901161748Scperciva# If no records, give up -- we'll just use the server name we were given. 902161748Scperciva if [ `wc -l < serverlist_full` -eq 0 ]; then 903161748Scperciva echo "none found." 904161748Scperciva return 1 905161748Scperciva fi 906161748Scperciva 907161748Scperciva# Report how many mirrors we found. 908161748Scperciva echo `wc -l < serverlist_full` "mirrors found." 909161748Scperciva 910161748Scperciva# Generate a random seed for use in picking mirrors. If HTTP_PROXY 911161748Scperciva# is set, this will be used to generate the seed; otherwise, the seed 912161748Scperciva# will be random. 913161748Scperciva if [ -n "${HTTP_PROXY}${http_proxy}" ]; then 914161748Scperciva RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" | 915161748Scperciva tr -d 'a-f' | 916161748Scperciva cut -c 1-9` 917161748Scperciva else 918161748Scperciva RANDVALUE=`jot -r 1 0 999999999` 919161748Scperciva fi 920161748Scperciva} 921161748Scperciva 922161748Scperciva# Pick a mirror. Returns 1 if we have run out of mirrors to try. 923161748Scpercivafetch_pick_server () { 924161748Scperciva# Generate a list of not-yet-tried mirrors 925161748Scperciva sort serverlist_tried | 926161748Scperciva comm -23 serverlist_full - > serverlist 927161748Scperciva 928161748Scperciva# Have we run out of mirrors? 929161748Scperciva if [ `wc -l < serverlist` -eq 0 ]; then 930161748Scperciva echo "No mirrors remaining, giving up." 931161748Scperciva return 1 932161748Scperciva fi 933161748Scperciva 934161748Scperciva# Find the highest priority level (lowest numeric value). 935161748Scperciva SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1` 936161748Scperciva 937161748Scperciva# Add up the weights of the response lines at that priority level. 938161748Scperciva SRV_WSUM=0; 939161748Scperciva while read X; do 940161748Scperciva case "$X" in 941161748Scperciva ${SRV_PRIORITY}\ *) 942161748Scperciva SRV_W=`echo $X | cut -f 2 -d ' '` 943161748Scperciva SRV_WSUM=$(($SRV_WSUM + $SRV_W)) 944161748Scperciva ;; 945161748Scperciva esac 946161748Scperciva done < serverlist 947161748Scperciva 948161748Scperciva# If all the weights are 0, pretend that they are all 1 instead. 949161748Scperciva if [ ${SRV_WSUM} -eq 0 ]; then 950161748Scperciva SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l` 951161748Scperciva SRV_W_ADD=1 952161748Scperciva else 953161748Scperciva SRV_W_ADD=0 954161748Scperciva fi 955161748Scperciva 956161748Scperciva# Pick a value between 0 and the sum of the weights - 1 957161748Scperciva SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}` 958161748Scperciva 959161748Scperciva# Read through the list of mirrors and set SERVERNAME. Write the line 960161748Scperciva# corresponding to the mirror we selected into serverlist_tried so that 961161748Scperciva# we won't try it again. 962161748Scperciva while read X; do 963161748Scperciva case "$X" in 964161748Scperciva ${SRV_PRIORITY}\ *) 965161748Scperciva SRV_W=`echo $X | cut -f 2 -d ' '` 966161748Scperciva SRV_W=$(($SRV_W + $SRV_W_ADD)) 967161748Scperciva if [ $SRV_RND -lt $SRV_W ]; then 968161748Scperciva SERVERNAME=`echo $X | cut -f 3 -d ' '` 969161748Scperciva echo "$X" >> serverlist_tried 970161748Scperciva break 971161748Scperciva else 972161748Scperciva SRV_RND=$(($SRV_RND - $SRV_W)) 973161748Scperciva fi 974161748Scperciva ;; 975161748Scperciva esac 976161748Scperciva done < serverlist 977161748Scperciva} 978161748Scperciva 979161748Scperciva# Take a list of ${oldhash}|${newhash} and output a list of needed patches, 980161748Scperciva# i.e., those for which we have ${oldhash} and don't have ${newhash}. 981161748Scpercivafetch_make_patchlist () { 982161748Scperciva grep -vE "^([0-9a-f]{64})\|\1$" | 983161748Scperciva tr '|' ' ' | 984161748Scperciva while read X Y; do 985161748Scperciva if [ -f "files/${Y}.gz" ] || 986161748Scperciva [ ! -f "files/${X}.gz" ]; then 987161748Scperciva continue 988161748Scperciva fi 989161748Scperciva echo "${X}|${Y}" 990161748Scperciva done | uniq 991161748Scperciva} 992161748Scperciva 993161748Scperciva# Print user-friendly progress statistics 994161748Scpercivafetch_progress () { 995161748Scperciva LNC=0 996161748Scperciva while read x; do 997161748Scperciva LNC=$(($LNC + 1)) 998161748Scperciva if [ $(($LNC % 10)) = 0 ]; then 999161748Scperciva echo -n $LNC 1000161748Scperciva elif [ $(($LNC % 2)) = 0 ]; then 1001161748Scperciva echo -n . 1002161748Scperciva fi 1003161748Scperciva done 1004161748Scperciva echo -n " " 1005161748Scperciva} 1006161748Scperciva 1007173564Scperciva# Function for asking the user if everything is ok 1008173564Scpercivacontinuep () { 1009173564Scperciva while read -p "Does this look reasonable (y/n)? " CONTINUE; do 1010173564Scperciva case "${CONTINUE}" in 1011173564Scperciva y*) 1012173564Scperciva return 0 1013173564Scperciva ;; 1014173564Scperciva n*) 1015173564Scperciva return 1 1016173564Scperciva ;; 1017173564Scperciva esac 1018173564Scperciva done 1019173564Scperciva} 1020173564Scperciva 1021161748Scperciva# Initialize the working directory 1022161748Scpercivaworkdir_init () { 1023161748Scperciva mkdir -p files 1024161748Scperciva touch tINDEX.present 1025161748Scperciva} 1026161748Scperciva 1027161748Scperciva# Check that we have a public key with an appropriate hash, or 1028161748Scperciva# fetch the key if it doesn't exist. Returns 1 if the key has 1029161748Scperciva# not yet been fetched. 1030161748Scpercivafetch_key () { 1031161748Scperciva if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then 1032161748Scperciva return 0 1033161748Scperciva fi 1034161748Scperciva 1035161748Scperciva echo -n "Fetching public key from ${SERVERNAME}... " 1036161748Scperciva rm -f pub.ssl 1037161748Scperciva fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/pub.ssl \ 1038161748Scperciva 2>${QUIETREDIR} || true 1039161748Scperciva if ! [ -r pub.ssl ]; then 1040161748Scperciva echo "failed." 1041161748Scperciva return 1 1042161748Scperciva fi 1043161748Scperciva if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then 1044161748Scperciva echo "key has incorrect hash." 1045161748Scperciva rm -f pub.ssl 1046161748Scperciva return 1 1047161748Scperciva fi 1048161748Scperciva echo "done." 1049161748Scperciva} 1050161748Scperciva 1051161748Scperciva# Fetch metadata signature, aka "tag". 1052161748Scpercivafetch_tag () { 1053173564Scperciva echo -n "Fetching metadata signature " 1054173564Scperciva echo ${NDEBUG} "for ${RELNUM} from ${SERVERNAME}... " 1055161748Scperciva rm -f latest.ssl 1056161748Scperciva fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/latest.ssl \ 1057161748Scperciva 2>${QUIETREDIR} || true 1058161748Scperciva if ! [ -r latest.ssl ]; then 1059161748Scperciva echo "failed." 1060161748Scperciva return 1 1061161748Scperciva fi 1062161748Scperciva 1063161748Scperciva openssl rsautl -pubin -inkey pub.ssl -verify \ 1064161748Scperciva < latest.ssl > tag.new 2>${QUIETREDIR} || true 1065161748Scperciva rm latest.ssl 1066161748Scperciva 1067161748Scperciva if ! [ `wc -l < tag.new` = 1 ] || 1068161748Scperciva ! grep -qE \ 1069161748Scperciva "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \ 1070161748Scperciva tag.new; then 1071161748Scperciva echo "invalid signature." 1072161748Scperciva return 1 1073161748Scperciva fi 1074161748Scperciva 1075161748Scperciva echo "done." 1076161748Scperciva 1077161748Scperciva RELPATCHNUM=`cut -f 4 -d '|' < tag.new` 1078161748Scperciva TINDEXHASH=`cut -f 5 -d '|' < tag.new` 1079161748Scperciva EOLTIME=`cut -f 6 -d '|' < tag.new` 1080161748Scperciva} 1081161748Scperciva 1082161748Scperciva# Sanity-check the patch number in a tag, to make sure that we're not 1083161748Scperciva# going to "update" backwards and to prevent replay attacks. 1084161748Scpercivafetch_tagsanity () { 1085161748Scperciva # Check that we're not going to move from -pX to -pY with Y < X. 1086161748Scperciva RELPX=`uname -r | sed -E 's,.*-,,'` 1087161748Scperciva if echo ${RELPX} | grep -qE '^p[0-9]+$'; then 1088161748Scperciva RELPX=`echo ${RELPX} | cut -c 2-` 1089161748Scperciva else 1090161748Scperciva RELPX=0 1091161748Scperciva fi 1092161748Scperciva if [ "${RELPATCHNUM}" -lt "${RELPX}" ]; then 1093161748Scperciva echo 1094161748Scperciva echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})" 1095161748Scperciva echo " appear older than what" 1096161748Scperciva echo "we are currently running (`uname -r`)!" 1097161748Scperciva echo "Cowardly refusing to proceed any further." 1098161748Scperciva return 1 1099161748Scperciva fi 1100161748Scperciva 1101161748Scperciva # If "tag" exists and corresponds to ${RELNUM}, make sure that 1102161748Scperciva # it contains a patch number <= RELPATCHNUM, in order to protect 1103161748Scperciva # against rollback (replay) attacks. 1104161748Scperciva if [ -f tag ] && 1105161748Scperciva grep -qE \ 1106161748Scperciva "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \ 1107161748Scperciva tag; then 1108161748Scperciva LASTRELPATCHNUM=`cut -f 4 -d '|' < tag` 1109161748Scperciva 1110161748Scperciva if [ "${RELPATCHNUM}" -lt "${LASTRELPATCHNUM}" ]; then 1111161748Scperciva echo 1112161748Scperciva echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})" 1113161748Scperciva echo " are older than the" 1114161748Scperciva echo -n "most recently seen updates" 1115161748Scperciva echo " (${RELNUM}-p${LASTRELPATCHNUM})." 1116161748Scperciva echo "Cowardly refusing to proceed any further." 1117161748Scperciva return 1 1118161748Scperciva fi 1119161748Scperciva fi 1120161748Scperciva} 1121161748Scperciva 1122161748Scperciva# Fetch metadata index file 1123161748Scpercivafetch_metadata_index () { 1124161748Scperciva echo ${NDEBUG} "Fetching metadata index... " 1125161748Scperciva rm -f ${TINDEXHASH} 1126161748Scperciva fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/t/${TINDEXHASH} 1127161748Scperciva 2>${QUIETREDIR} 1128161748Scperciva if ! [ -f ${TINDEXHASH} ]; then 1129161748Scperciva echo "failed." 1130161748Scperciva return 1 1131161748Scperciva fi 1132161748Scperciva if [ `${SHA256} -q ${TINDEXHASH}` != ${TINDEXHASH} ]; then 1133161748Scperciva echo "update metadata index corrupt." 1134161748Scperciva return 1 1135161748Scperciva fi 1136161748Scperciva echo "done." 1137161748Scperciva} 1138161748Scperciva 1139161748Scperciva# Print an error message about signed metadata being bogus. 1140161748Scpercivafetch_metadata_bogus () { 1141161748Scperciva echo 1142161748Scperciva echo "The update metadata$1 is correctly signed, but" 1143161748Scperciva echo "failed an integrity check." 1144161748Scperciva echo "Cowardly refusing to proceed any further." 1145161748Scperciva return 1 1146161748Scperciva} 1147161748Scperciva 1148161748Scperciva# Construct tINDEX.new by merging the lines named in $1 from ${TINDEXHASH} 1149161748Scperciva# with the lines not named in $@ from tINDEX.present (if that file exists). 1150161748Scpercivafetch_metadata_index_merge () { 1151161748Scperciva for METAFILE in $@; do 1152161748Scperciva if [ `grep -E "^${METAFILE}\|" ${TINDEXHASH} | wc -l` \ 1153161748Scperciva -ne 1 ]; then 1154161748Scperciva fetch_metadata_bogus " index" 1155161748Scperciva return 1 1156161748Scperciva fi 1157161748Scperciva 1158161748Scperciva grep -E "${METAFILE}\|" ${TINDEXHASH} 1159161748Scperciva done | 1160161748Scperciva sort > tINDEX.wanted 1161161748Scperciva 1162161748Scperciva if [ -f tINDEX.present ]; then 1163161748Scperciva join -t '|' -v 2 tINDEX.wanted tINDEX.present | 1164161748Scperciva sort -m - tINDEX.wanted > tINDEX.new 1165161748Scperciva rm tINDEX.wanted 1166161748Scperciva else 1167161748Scperciva mv tINDEX.wanted tINDEX.new 1168161748Scperciva fi 1169161748Scperciva} 1170161748Scperciva 1171161748Scperciva# Sanity check all the lines of tINDEX.new. Even if more metadata lines 1172161748Scperciva# are added by future versions of the server, this won't cause problems, 1173161748Scperciva# since the only lines which appear in tINDEX.new are the ones which we 1174161748Scperciva# specifically grepped out of ${TINDEXHASH}. 1175161748Scpercivafetch_metadata_index_sanity () { 1176161748Scperciva if grep -qvE '^[0-9A-Z.-]+\|[0-9a-f]{64}$' tINDEX.new; then 1177161748Scperciva fetch_metadata_bogus " index" 1178161748Scperciva return 1 1179161748Scperciva fi 1180161748Scperciva} 1181161748Scperciva 1182161748Scperciva# Sanity check the metadata file $1. 1183161748Scpercivafetch_metadata_sanity () { 1184161748Scperciva # Some aliases to save space later: ${P} is a character which can 1185161748Scperciva # appear in a path; ${M} is the four numeric metadata fields; and 1186161748Scperciva # ${H} is a sha256 hash. 1187161748Scperciva P="[-+./:=_[[:alnum:]]" 1188161748Scperciva M="[0-9]+\|[0-9]+\|[0-9]+\|[0-9]+" 1189161748Scperciva H="[0-9a-f]{64}" 1190161748Scperciva 1191161748Scperciva # Check that the first four fields make sense. 1192161748Scperciva if gunzip -c < files/$1.gz | 1193161748Scperciva grep -qvE "^[a-z]+\|[0-9a-z]+\|${P}+\|[fdL-]\|"; then 1194161748Scperciva fetch_metadata_bogus "" 1195161748Scperciva return 1 1196161748Scperciva fi 1197161748Scperciva 1198161748Scperciva # Remove the first three fields. 1199161748Scperciva gunzip -c < files/$1.gz | 1200161748Scperciva cut -f 4- -d '|' > sanitycheck.tmp 1201161748Scperciva 1202161748Scperciva # Sanity check entries with type 'f' 1203161748Scperciva if grep -E '^f' sanitycheck.tmp | 1204161748Scperciva grep -qvE "^f\|${M}\|${H}\|${P}*\$"; then 1205161748Scperciva fetch_metadata_bogus "" 1206161748Scperciva return 1 1207161748Scperciva fi 1208161748Scperciva 1209161748Scperciva # Sanity check entries with type 'd' 1210161748Scperciva if grep -E '^d' sanitycheck.tmp | 1211161748Scperciva grep -qvE "^d\|${M}\|\|\$"; then 1212161748Scperciva fetch_metadata_bogus "" 1213161748Scperciva return 1 1214161748Scperciva fi 1215161748Scperciva 1216161748Scperciva # Sanity check entries with type 'L' 1217161748Scperciva if grep -E '^L' sanitycheck.tmp | 1218161748Scperciva grep -qvE "^L\|${M}\|${P}*\|\$"; then 1219161748Scperciva fetch_metadata_bogus "" 1220161748Scperciva return 1 1221161748Scperciva fi 1222161748Scperciva 1223161748Scperciva # Sanity check entries with type '-' 1224161748Scperciva if grep -E '^-' sanitycheck.tmp | 1225161748Scperciva grep -qvE "^-\|\|\|\|\|\|"; then 1226161748Scperciva fetch_metadata_bogus "" 1227161748Scperciva return 1 1228161748Scperciva fi 1229161748Scperciva 1230161748Scperciva # Clean up 1231161748Scperciva rm sanitycheck.tmp 1232161748Scperciva} 1233161748Scperciva 1234161748Scperciva# Fetch the metadata index and metadata files listed in $@, 1235161748Scperciva# taking advantage of metadata patches where possible. 1236161748Scpercivafetch_metadata () { 1237161748Scperciva fetch_metadata_index || return 1 1238161748Scperciva fetch_metadata_index_merge $@ || return 1 1239161748Scperciva fetch_metadata_index_sanity || return 1 1240161748Scperciva 1241161748Scperciva # Generate a list of wanted metadata patches 1242161748Scperciva join -t '|' -o 1.2,2.2 tINDEX.present tINDEX.new | 1243161748Scperciva fetch_make_patchlist > patchlist 1244161748Scperciva 1245161748Scperciva if [ -s patchlist ]; then 1246161748Scperciva # Attempt to fetch metadata patches 1247161748Scperciva echo -n "Fetching `wc -l < patchlist | tr -d ' '` " 1248161748Scperciva echo ${NDEBUG} "metadata patches.${DDSTATS}" 1249161748Scperciva tr '|' '-' < patchlist | 1250161748Scperciva lam -s "${FETCHDIR}/tp/" - -s ".gz" | 1251161748Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1252161748Scperciva 2>${STATSREDIR} | fetch_progress 1253161748Scperciva echo "done." 1254161748Scperciva 1255161748Scperciva # Attempt to apply metadata patches 1256161748Scperciva echo -n "Applying metadata patches... " 1257161748Scperciva tr '|' ' ' < patchlist | 1258161748Scperciva while read X Y; do 1259161748Scperciva if [ ! -f "${X}-${Y}.gz" ]; then continue; fi 1260161748Scperciva gunzip -c < ${X}-${Y}.gz > diff 1261161748Scperciva gunzip -c < files/${X}.gz > diff-OLD 1262161748Scperciva 1263161748Scperciva # Figure out which lines are being added and removed 1264161748Scperciva grep -E '^-' diff | 1265161748Scperciva cut -c 2- | 1266161748Scperciva while read PREFIX; do 1267161748Scperciva look "${PREFIX}" diff-OLD 1268161748Scperciva done | 1269161748Scperciva sort > diff-rm 1270161748Scperciva grep -E '^\+' diff | 1271161748Scperciva cut -c 2- > diff-add 1272161748Scperciva 1273161748Scperciva # Generate the new file 1274161748Scperciva comm -23 diff-OLD diff-rm | 1275161748Scperciva sort - diff-add > diff-NEW 1276161748Scperciva 1277161748Scperciva if [ `${SHA256} -q diff-NEW` = ${Y} ]; then 1278161748Scperciva mv diff-NEW files/${Y} 1279161748Scperciva gzip -n files/${Y} 1280161748Scperciva else 1281161748Scperciva mv diff-NEW ${Y}.bad 1282161748Scperciva fi 1283161748Scperciva rm -f ${X}-${Y}.gz diff 1284161748Scperciva rm -f diff-OLD diff-NEW diff-add diff-rm 1285161748Scperciva done 2>${QUIETREDIR} 1286161748Scperciva echo "done." 1287161748Scperciva fi 1288161748Scperciva 1289161748Scperciva # Update metadata without patches 1290161748Scperciva cut -f 2 -d '|' < tINDEX.new | 1291161748Scperciva while read Y; do 1292161748Scperciva if [ ! -f "files/${Y}.gz" ]; then 1293161748Scperciva echo ${Y}; 1294161748Scperciva fi 1295164600Scperciva done | 1296164600Scperciva sort -u > filelist 1297161748Scperciva 1298161748Scperciva if [ -s filelist ]; then 1299161748Scperciva echo -n "Fetching `wc -l < filelist | tr -d ' '` " 1300161748Scperciva echo ${NDEBUG} "metadata files... " 1301161748Scperciva lam -s "${FETCHDIR}/m/" - -s ".gz" < filelist | 1302161748Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1303161748Scperciva 2>${QUIETREDIR} 1304161748Scperciva 1305161748Scperciva while read Y; do 1306161748Scperciva if ! [ -f ${Y}.gz ]; then 1307161748Scperciva echo "failed." 1308161748Scperciva return 1 1309161748Scperciva fi 1310161748Scperciva if [ `gunzip -c < ${Y}.gz | 1311161748Scperciva ${SHA256} -q` = ${Y} ]; then 1312161748Scperciva mv ${Y}.gz files/${Y}.gz 1313161748Scperciva else 1314161748Scperciva echo "metadata is corrupt." 1315161748Scperciva return 1 1316161748Scperciva fi 1317161748Scperciva done < filelist 1318161748Scperciva echo "done." 1319161748Scperciva fi 1320161748Scperciva 1321161748Scperciva# Sanity-check the metadata files. 1322161748Scperciva cut -f 2 -d '|' tINDEX.new > filelist 1323161748Scperciva while read X; do 1324161748Scperciva fetch_metadata_sanity ${X} || return 1 1325161748Scperciva done < filelist 1326161748Scperciva 1327161748Scperciva# Remove files which are no longer needed 1328161748Scperciva cut -f 2 -d '|' tINDEX.present | 1329161748Scperciva sort > oldfiles 1330161748Scperciva cut -f 2 -d '|' tINDEX.new | 1331161748Scperciva sort | 1332161748Scperciva comm -13 - oldfiles | 1333161748Scperciva lam -s "files/" - -s ".gz" | 1334161748Scperciva xargs rm -f 1335161748Scperciva rm patchlist filelist oldfiles 1336161748Scperciva rm ${TINDEXHASH} 1337161748Scperciva 1338161748Scperciva# We're done! 1339161748Scperciva mv tINDEX.new tINDEX.present 1340161748Scperciva mv tag.new tag 1341161748Scperciva 1342161748Scperciva return 0 1343161748Scperciva} 1344161748Scperciva 1345173564Scperciva# Extract a subset of a downloaded metadata file containing only the parts 1346173564Scperciva# which are listed in COMPONENTS. 1347173564Scpercivafetch_filter_metadata_components () { 1348173564Scperciva METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'` 1349173564Scperciva gunzip -c < files/${METAHASH}.gz > $1.all 1350173564Scperciva 1351173564Scperciva # Fish out the lines belonging to components we care about. 1352173564Scperciva for C in ${COMPONENTS}; do 1353173564Scperciva look "`echo ${C} | tr '/' '|'`|" $1.all 1354173564Scperciva done > $1 1355173564Scperciva 1356173564Scperciva # Remove temporary file. 1357173564Scperciva rm $1.all 1358173564Scperciva} 1359173564Scperciva 1360161869Scperciva# Generate a filtered version of the metadata file $1 from the downloaded 1361161748Scperciva# file, by fishing out the lines corresponding to components we're trying 1362161748Scperciva# to keep updated, and then removing lines corresponding to paths we want 1363161748Scperciva# to ignore. 1364161748Scpercivafetch_filter_metadata () { 1365173564Scperciva # Fish out the lines belonging to components we care about. 1366173564Scperciva fetch_filter_metadata_components $1 1367161748Scperciva 1368161748Scperciva # Canonicalize directory names by removing any trailing / in 1369161748Scperciva # order to avoid listing directories multiple times if they 1370161748Scperciva # belong to multiple components. Turning "/" into "" doesn't 1371161748Scperciva # matter, since we add a leading "/" when we use paths later. 1372173564Scperciva cut -f 3- -d '|' $1 | 1373161748Scperciva sed -e 's,/|d|,|d|,' | 1374161748Scperciva sort -u > $1.tmp 1375161748Scperciva 1376161748Scperciva # Figure out which lines to ignore and remove them. 1377161748Scperciva for X in ${IGNOREPATHS}; do 1378161748Scperciva grep -E "^${X}" $1.tmp 1379161748Scperciva done | 1380161748Scperciva sort -u | 1381161748Scperciva comm -13 - $1.tmp > $1 1382161748Scperciva 1383161748Scperciva # Remove temporary files. 1384173564Scperciva rm $1.tmp 1385161748Scperciva} 1386161748Scperciva 1387173564Scperciva# Filter the metadata file $1 by adding lines with "/boot/$2" 1388164600Scperciva# replaced by ${KERNELDIR} (which is `sysctl -n kern.bootfile` minus the 1389173564Scperciva# trailing "/kernel"); and if "/boot/$2" does not exist, remove 1390164600Scperciva# the original lines which start with that. 1391164600Scperciva# Put another way: Deal with the fact that the FOO kernel is sometimes 1392164600Scperciva# installed in /boot/FOO/ and is sometimes installed elsewhere. 1393161748Scpercivafetch_filter_kernel_names () { 1394173564Scperciva grep ^/boot/$2 $1 | 1395173564Scperciva sed -e "s,/boot/$2,${KERNELDIR},g" | 1396161748Scperciva sort - $1 > $1.tmp 1397161748Scperciva mv $1.tmp $1 1398164600Scperciva 1399173564Scperciva if ! [ -d /boot/$2 ]; then 1400173564Scperciva grep -v ^/boot/$2 $1 > $1.tmp 1401164600Scperciva mv $1.tmp $1 1402164600Scperciva fi 1403161748Scperciva} 1404161748Scperciva 1405161748Scperciva# For all paths appearing in $1 or $3, inspect the system 1406161748Scperciva# and generate $2 describing what is currently installed. 1407161748Scpercivafetch_inspect_system () { 1408161748Scperciva # No errors yet... 1409161748Scperciva rm -f .err 1410161748Scperciva 1411161748Scperciva # Tell the user why his disk is suddenly making lots of noise 1412161748Scperciva echo -n "Inspecting system... " 1413161748Scperciva 1414161748Scperciva # Generate list of files to inspect 1415161748Scperciva cat $1 $3 | 1416161748Scperciva cut -f 1 -d '|' | 1417161748Scperciva sort -u > filelist 1418161748Scperciva 1419161748Scperciva # Examine each file and output lines of the form 1420161748Scperciva # /path/to/file|type|device-inum|user|group|perm|flags|value 1421161748Scperciva # sorted by device and inode number. 1422161748Scperciva while read F; do 1423161748Scperciva # If the symlink/file/directory does not exist, record this. 1424161748Scperciva if ! [ -e ${BASEDIR}/${F} ]; then 1425161748Scperciva echo "${F}|-||||||" 1426161748Scperciva continue 1427161748Scperciva fi 1428161748Scperciva if ! [ -r ${BASEDIR}/${F} ]; then 1429161748Scperciva echo "Cannot read file: ${BASEDIR}/${F}" \ 1430161748Scperciva >/dev/stderr 1431161748Scperciva touch .err 1432161748Scperciva return 1 1433161748Scperciva fi 1434161748Scperciva 1435161748Scperciva # Otherwise, output an index line. 1436161748Scperciva if [ -L ${BASEDIR}/${F} ]; then 1437161748Scperciva echo -n "${F}|L|" 1438161748Scperciva stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F}; 1439161748Scperciva readlink ${BASEDIR}/${F}; 1440161748Scperciva elif [ -f ${BASEDIR}/${F} ]; then 1441161748Scperciva echo -n "${F}|f|" 1442161748Scperciva stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F}; 1443161748Scperciva sha256 -q ${BASEDIR}/${F}; 1444161748Scperciva elif [ -d ${BASEDIR}/${F} ]; then 1445161748Scperciva echo -n "${F}|d|" 1446161748Scperciva stat -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F}; 1447161748Scperciva else 1448161748Scperciva echo "Unknown file type: ${BASEDIR}/${F}" \ 1449161748Scperciva >/dev/stderr 1450161748Scperciva touch .err 1451161748Scperciva return 1 1452161748Scperciva fi 1453161748Scperciva done < filelist | 1454161748Scperciva sort -k 3,3 -t '|' > $2.tmp 1455161748Scperciva rm filelist 1456161748Scperciva 1457161748Scperciva # Check if an error occured during system inspection 1458161748Scperciva if [ -f .err ]; then 1459161748Scperciva return 1 1460161748Scperciva fi 1461161748Scperciva 1462161748Scperciva # Convert to the form 1463161748Scperciva # /path/to/file|type|user|group|perm|flags|value|hlink 1464161748Scperciva # by resolving identical device and inode numbers into hard links. 1465161748Scperciva cut -f 1,3 -d '|' $2.tmp | 1466161748Scperciva sort -k 1,1 -t '|' | 1467161748Scperciva sort -s -u -k 2,2 -t '|' | 1468161748Scperciva join -1 2 -2 3 -t '|' - $2.tmp | 1469161748Scperciva awk -F \| -v OFS=\| \ 1470161748Scperciva '{ 1471161748Scperciva if (($2 == $3) || ($4 == "-")) 1472161748Scperciva print $3,$4,$5,$6,$7,$8,$9,"" 1473161748Scperciva else 1474161748Scperciva print $3,$4,$5,$6,$7,$8,$9,$2 1475161748Scperciva }' | 1476161748Scperciva sort > $2 1477161748Scperciva rm $2.tmp 1478161748Scperciva 1479161748Scperciva # We're finished looking around 1480161748Scperciva echo "done." 1481161748Scperciva} 1482161748Scperciva 1483173564Scperciva# For any paths matching ${MERGECHANGES}, compare $1 and $2 and find any 1484173564Scperciva# files which differ; generate $3 containing these paths and the old hashes. 1485173564Scpercivafetch_filter_mergechanges () { 1486173564Scperciva # Pull out the paths and hashes of the files matching ${MERGECHANGES}. 1487173564Scperciva for F in $1 $2; do 1488173564Scperciva for X in ${MERGECHANGES}; do 1489173564Scperciva grep -E "^${X}" ${F} 1490173564Scperciva done | 1491173564Scperciva cut -f 1,2,7 -d '|' | 1492173564Scperciva sort > ${F}-values 1493173564Scperciva done 1494173564Scperciva 1495173564Scperciva # Any line in $2-values which doesn't appear in $1-values and is a 1496173564Scperciva # file means that we should list the path in $3. 1497173564Scperciva comm -13 $1-values $2-values | 1498173564Scperciva fgrep '|f|' | 1499173564Scperciva cut -f 1 -d '|' > $2-paths 1500173564Scperciva 1501173564Scperciva # For each path, pull out one (and only one!) entry from $1-values. 1502173564Scperciva # Note that we cannot distinguish which "old" version the user made 1503173564Scperciva # changes to; but hopefully any changes which occur due to security 1504173564Scperciva # updates will exist in both the "new" version and the version which 1505173564Scperciva # the user has installed, so the merging will still work. 1506173564Scperciva while read X; do 1507173564Scperciva look "${X}|" $1-values | 1508173564Scperciva head -1 1509173564Scperciva done < $2-paths > $3 1510173564Scperciva 1511173564Scperciva # Clean up 1512173564Scperciva rm $1-values $2-values $2-paths 1513173564Scperciva} 1514173564Scperciva 1515161748Scperciva# For any paths matching ${UPDATEIFUNMODIFIED}, remove lines from $[123] 1516173564Scperciva# which correspond to lines in $2 with hashes not matching $1 or $3, unless 1517173564Scperciva# the paths are listed in $4. For entries in $2 marked "not present" 1518173564Scperciva# (aka. type -), remove lines from $[123] unless there is a corresponding 1519173564Scperciva# entry in $1. 1520161748Scpercivafetch_filter_unmodified_notpresent () { 1521161748Scperciva # Figure out which lines of $1 and $3 correspond to bits which 1522161748Scperciva # should only be updated if they haven't changed, and fish out 1523161748Scperciva # the (path, type, value) tuples. 1524161748Scperciva # NOTE: We don't consider a file to be "modified" if it matches 1525161748Scperciva # the hash from $3. 1526161748Scperciva for X in ${UPDATEIFUNMODIFIED}; do 1527161748Scperciva grep -E "^${X}" $1 1528161748Scperciva grep -E "^${X}" $3 1529161748Scperciva done | 1530161748Scperciva cut -f 1,2,7 -d '|' | 1531161748Scperciva sort > $1-values 1532161748Scperciva 1533161748Scperciva # Do the same for $2. 1534161748Scperciva for X in ${UPDATEIFUNMODIFIED}; do 1535161748Scperciva grep -E "^${X}" $2 1536161748Scperciva done | 1537161748Scperciva cut -f 1,2,7 -d '|' | 1538161748Scperciva sort > $2-values 1539161748Scperciva 1540161748Scperciva # Any entry in $2-values which is not in $1-values corresponds to 1541173564Scperciva # a path which we need to remove from $1, $2, and $3, unless it 1542173564Scperciva # that path appears in $4. 1543173564Scperciva comm -13 $1-values $2-values | 1544173564Scperciva sort -t '|' -k 1,1 > mlines.tmp 1545173564Scperciva cut -f 1 -d '|' $4 | 1546173564Scperciva sort | 1547173564Scperciva join -v 2 -t '|' - mlines.tmp | 1548173564Scperciva sort > mlines 1549173564Scperciva rm $1-values $2-values mlines.tmp 1550161748Scperciva 1551161748Scperciva # Any lines in $2 which are not in $1 AND are "not present" lines 1552161748Scperciva # also belong in mlines. 1553161748Scperciva comm -13 $1 $2 | 1554161748Scperciva cut -f 1,2,7 -d '|' | 1555161748Scperciva fgrep '|-|' >> mlines 1556161748Scperciva 1557161748Scperciva # Remove lines from $1, $2, and $3 1558161748Scperciva for X in $1 $2 $3; do 1559161748Scperciva sort -t '|' -k 1,1 ${X} > ${X}.tmp 1560161748Scperciva cut -f 1 -d '|' < mlines | 1561161748Scperciva sort | 1562161748Scperciva join -v 2 -t '|' - ${X}.tmp | 1563161748Scperciva sort > ${X} 1564161748Scperciva rm ${X}.tmp 1565161748Scperciva done 1566161748Scperciva 1567161748Scperciva # Store a list of the modified files, for future reference 1568161748Scperciva fgrep -v '|-|' mlines | 1569161748Scperciva cut -f 1 -d '|' > modifiedfiles 1570161748Scperciva rm mlines 1571161748Scperciva} 1572161748Scperciva 1573161748Scperciva# For each entry in $1 of type -, remove any corresponding 1574161748Scperciva# entry from $2 if ${ALLOWADD} != "yes". Remove all entries 1575161748Scperciva# of type - from $1. 1576161748Scpercivafetch_filter_allowadd () { 1577161748Scperciva cut -f 1,2 -d '|' < $1 | 1578161748Scperciva fgrep '|-' | 1579161748Scperciva cut -f 1 -d '|' > filesnotpresent 1580161748Scperciva 1581161748Scperciva if [ ${ALLOWADD} != "yes" ]; then 1582161748Scperciva sort < $2 | 1583161748Scperciva join -v 1 -t '|' - filesnotpresent | 1584161748Scperciva sort > $2.tmp 1585161748Scperciva mv $2.tmp $2 1586161748Scperciva fi 1587161748Scperciva 1588161748Scperciva sort < $1 | 1589161748Scperciva join -v 1 -t '|' - filesnotpresent | 1590161748Scperciva sort > $1.tmp 1591161748Scperciva mv $1.tmp $1 1592161748Scperciva rm filesnotpresent 1593161748Scperciva} 1594161748Scperciva 1595161748Scperciva# If ${ALLOWDELETE} != "yes", then remove any entries from $1 1596161748Scperciva# which don't correspond to entries in $2. 1597161748Scpercivafetch_filter_allowdelete () { 1598161748Scperciva # Produce a lists ${PATH}|${TYPE} 1599161748Scperciva for X in $1 $2; do 1600161748Scperciva cut -f 1-2 -d '|' < ${X} | 1601161748Scperciva sort -u > ${X}.nodes 1602161748Scperciva done 1603161748Scperciva 1604161748Scperciva # Figure out which lines need to be removed from $1. 1605161748Scperciva if [ ${ALLOWDELETE} != "yes" ]; then 1606161748Scperciva comm -23 $1.nodes $2.nodes > $1.badnodes 1607161748Scperciva else 1608161748Scperciva : > $1.badnodes 1609161748Scperciva fi 1610161748Scperciva 1611161748Scperciva # Remove the relevant lines from $1 1612161748Scperciva while read X; do 1613161748Scperciva look "${X}|" $1 1614161748Scperciva done < $1.badnodes | 1615161748Scperciva comm -13 - $1 > $1.tmp 1616161748Scperciva mv $1.tmp $1 1617161748Scperciva 1618161748Scperciva rm $1.badnodes $1.nodes $2.nodes 1619161748Scperciva} 1620161748Scperciva 1621161748Scperciva# If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in $2 1622161748Scperciva# with metadata not matching any entry in $1, replace the corresponding 1623161748Scperciva# line of $3 with one having the same metadata as the entry in $2. 1624161748Scpercivafetch_filter_modified_metadata () { 1625161748Scperciva # Fish out the metadata from $1 and $2 1626161748Scperciva for X in $1 $2; do 1627161748Scperciva cut -f 1-6 -d '|' < ${X} > ${X}.metadata 1628161748Scperciva done 1629161748Scperciva 1630161748Scperciva # Find the metadata we need to keep 1631161748Scperciva if [ ${KEEPMODIFIEDMETADATA} = "yes" ]; then 1632161748Scperciva comm -13 $1.metadata $2.metadata > keepmeta 1633161748Scperciva else 1634161748Scperciva : > keepmeta 1635161748Scperciva fi 1636161748Scperciva 1637161748Scperciva # Extract the lines which we need to remove from $3, and 1638161748Scperciva # construct the lines which we need to add to $3. 1639161748Scperciva : > $3.remove 1640161748Scperciva : > $3.add 1641161748Scperciva while read LINE; do 1642161748Scperciva NODE=`echo "${LINE}" | cut -f 1-2 -d '|'` 1643161748Scperciva look "${NODE}|" $3 >> $3.remove 1644161748Scperciva look "${NODE}|" $3 | 1645161748Scperciva cut -f 7- -d '|' | 1646161748Scperciva lam -s "${LINE}|" - >> $3.add 1647161748Scperciva done < keepmeta 1648161748Scperciva 1649161748Scperciva # Remove the specified lines and add the new lines. 1650161748Scperciva sort $3.remove | 1651161748Scperciva comm -13 - $3 | 1652161748Scperciva sort -u - $3.add > $3.tmp 1653161748Scperciva mv $3.tmp $3 1654161748Scperciva 1655161748Scperciva rm keepmeta $1.metadata $2.metadata $3.add $3.remove 1656161748Scperciva} 1657161748Scperciva 1658161748Scperciva# Remove lines from $1 and $2 which are identical; 1659161748Scperciva# no need to update a file if it isn't changing. 1660161748Scpercivafetch_filter_uptodate () { 1661161748Scperciva comm -23 $1 $2 > $1.tmp 1662161748Scperciva comm -13 $1 $2 > $2.tmp 1663161748Scperciva 1664161748Scperciva mv $1.tmp $1 1665161748Scperciva mv $2.tmp $2 1666161748Scperciva} 1667161748Scperciva 1668173564Scperciva# Fetch any "clean" old versions of files we need for merging changes. 1669173564Scpercivafetch_files_premerge () { 1670173564Scperciva # We only need to do anything if $1 is non-empty. 1671173564Scperciva if [ -s $1 ]; then 1672173564Scperciva # Tell the user what we're doing 1673173564Scperciva echo -n "Fetching files from ${OLDRELNUM} for merging... " 1674173564Scperciva 1675173564Scperciva # List of files wanted 1676173564Scperciva fgrep '|f|' < $1 | 1677173564Scperciva cut -f 3 -d '|' | 1678173564Scperciva sort -u > files.wanted 1679173564Scperciva 1680173564Scperciva # Only fetch the files we don't already have 1681173564Scperciva while read Y; do 1682173564Scperciva if [ ! -f "files/${Y}.gz" ]; then 1683173564Scperciva echo ${Y}; 1684173564Scperciva fi 1685173564Scperciva done < files.wanted > filelist 1686173564Scperciva 1687173564Scperciva # Actually fetch them 1688173564Scperciva lam -s "${OLDFETCHDIR}/f/" - -s ".gz" < filelist | 1689173564Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1690173564Scperciva 2>${QUIETREDIR} 1691173564Scperciva 1692173564Scperciva # Make sure we got them all, and move them into /files/ 1693173564Scperciva while read Y; do 1694173564Scperciva if ! [ -f ${Y}.gz ]; then 1695173564Scperciva echo "failed." 1696173564Scperciva return 1 1697173564Scperciva fi 1698173564Scperciva if [ `gunzip -c < ${Y}.gz | 1699173564Scperciva ${SHA256} -q` = ${Y} ]; then 1700173564Scperciva mv ${Y}.gz files/${Y}.gz 1701173564Scperciva else 1702173564Scperciva echo "${Y} has incorrect hash." 1703173564Scperciva return 1 1704173564Scperciva fi 1705173564Scperciva done < filelist 1706173564Scperciva echo "done." 1707173564Scperciva 1708173564Scperciva # Clean up 1709173564Scperciva rm filelist files.wanted 1710173564Scperciva fi 1711173564Scperciva} 1712173564Scperciva 1713161748Scperciva# Prepare to fetch files: Generate a list of the files we need, 1714161748Scperciva# copy the unmodified files we have into /files/, and generate 1715161748Scperciva# a list of patches to download. 1716161748Scpercivafetch_files_prepare () { 1717161748Scperciva # Tell the user why his disk is suddenly making lots of noise 1718161748Scperciva echo -n "Preparing to download files... " 1719161748Scperciva 1720161748Scperciva # Reduce indices to ${PATH}|${HASH} pairs 1721161748Scperciva for X in $1 $2 $3; do 1722161748Scperciva cut -f 1,2,7 -d '|' < ${X} | 1723161748Scperciva fgrep '|f|' | 1724161748Scperciva cut -f 1,3 -d '|' | 1725161748Scperciva sort > ${X}.hashes 1726161748Scperciva done 1727161748Scperciva 1728161748Scperciva # List of files wanted 1729161748Scperciva cut -f 2 -d '|' < $3.hashes | 1730173441Scperciva sort -u | 1731173441Scperciva while read HASH; do 1732173441Scperciva if ! [ -f files/${HASH}.gz ]; then 1733173441Scperciva echo ${HASH} 1734173441Scperciva fi 1735173441Scperciva done > files.wanted 1736161748Scperciva 1737161748Scperciva # Generate a list of unmodified files 1738161748Scperciva comm -12 $1.hashes $2.hashes | 1739161748Scperciva sort -k 1,1 -t '|' > unmodified.files 1740161748Scperciva 1741161748Scperciva # Copy all files into /files/. We only need the unmodified files 1742161748Scperciva # for use in patching; but we'll want all of them if the user asks 1743161748Scperciva # to rollback the updates later. 1744171784Scperciva while read LINE; do 1745171784Scperciva F=`echo "${LINE}" | cut -f 1 -d '|'` 1746171784Scperciva HASH=`echo "${LINE}" | cut -f 2 -d '|'` 1747171784Scperciva 1748171784Scperciva # Skip files we already have. 1749171784Scperciva if [ -f files/${HASH}.gz ]; then 1750171784Scperciva continue 1751171784Scperciva fi 1752171784Scperciva 1753171784Scperciva # Make sure the file hasn't changed. 1754161748Scperciva cp "${BASEDIR}/${F}" tmpfile 1755171784Scperciva if [ `sha256 -q tmpfile` != ${HASH} ]; then 1756171784Scperciva echo 1757171784Scperciva echo "File changed while FreeBSD Update running: ${F}" 1758171784Scperciva return 1 1759171784Scperciva fi 1760171784Scperciva 1761171784Scperciva # Place the file into storage. 1762171784Scperciva gzip -c < tmpfile > files/${HASH}.gz 1763161748Scperciva rm tmpfile 1764171784Scperciva done < $2.hashes 1765161748Scperciva 1766161748Scperciva # Produce a list of patches to download 1767161748Scperciva sort -k 1,1 -t '|' $3.hashes | 1768161748Scperciva join -t '|' -o 2.2,1.2 - unmodified.files | 1769161748Scperciva fetch_make_patchlist > patchlist 1770161748Scperciva 1771161748Scperciva # Garbage collect 1772161748Scperciva rm unmodified.files $1.hashes $2.hashes $3.hashes 1773161748Scperciva 1774161748Scperciva # We don't need the list of possible old files any more. 1775161748Scperciva rm $1 1776161748Scperciva 1777161748Scperciva # We're finished making noise 1778161748Scperciva echo "done." 1779161748Scperciva} 1780161748Scperciva 1781161748Scperciva# Fetch files. 1782161748Scpercivafetch_files () { 1783161748Scperciva # Attempt to fetch patches 1784161748Scperciva if [ -s patchlist ]; then 1785161748Scperciva echo -n "Fetching `wc -l < patchlist | tr -d ' '` " 1786161748Scperciva echo ${NDEBUG} "patches.${DDSTATS}" 1787161748Scperciva tr '|' '-' < patchlist | 1788173564Scperciva lam -s "${PATCHDIR}/" - | 1789161748Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1790161748Scperciva 2>${STATSREDIR} | fetch_progress 1791161748Scperciva echo "done." 1792161748Scperciva 1793161748Scperciva # Attempt to apply patches 1794161748Scperciva echo -n "Applying patches... " 1795161748Scperciva tr '|' ' ' < patchlist | 1796161748Scperciva while read X Y; do 1797161748Scperciva if [ ! -f "${X}-${Y}" ]; then continue; fi 1798161748Scperciva gunzip -c < files/${X}.gz > OLD 1799161748Scperciva 1800161748Scperciva bspatch OLD NEW ${X}-${Y} 1801161748Scperciva 1802161748Scperciva if [ `${SHA256} -q NEW` = ${Y} ]; then 1803161748Scperciva mv NEW files/${Y} 1804161748Scperciva gzip -n files/${Y} 1805161748Scperciva fi 1806161748Scperciva rm -f diff OLD NEW ${X}-${Y} 1807161748Scperciva done 2>${QUIETREDIR} 1808161748Scperciva echo "done." 1809161748Scperciva fi 1810161748Scperciva 1811161748Scperciva # Download files which couldn't be generate via patching 1812161748Scperciva while read Y; do 1813161748Scperciva if [ ! -f "files/${Y}.gz" ]; then 1814161748Scperciva echo ${Y}; 1815161748Scperciva fi 1816161748Scperciva done < files.wanted > filelist 1817161748Scperciva 1818161748Scperciva if [ -s filelist ]; then 1819161748Scperciva echo -n "Fetching `wc -l < filelist | tr -d ' '` " 1820161748Scperciva echo ${NDEBUG} "files... " 1821161748Scperciva lam -s "${FETCHDIR}/f/" - -s ".gz" < filelist | 1822161748Scperciva xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1823161748Scperciva 2>${QUIETREDIR} 1824161748Scperciva 1825161748Scperciva while read Y; do 1826161748Scperciva if ! [ -f ${Y}.gz ]; then 1827161748Scperciva echo "failed." 1828161748Scperciva return 1 1829161748Scperciva fi 1830161748Scperciva if [ `gunzip -c < ${Y}.gz | 1831161748Scperciva ${SHA256} -q` = ${Y} ]; then 1832161748Scperciva mv ${Y}.gz files/${Y}.gz 1833161748Scperciva else 1834161748Scperciva echo "${Y} has incorrect hash." 1835161748Scperciva return 1 1836161748Scperciva fi 1837161748Scperciva done < filelist 1838161748Scperciva echo "done." 1839161748Scperciva fi 1840161748Scperciva 1841161748Scperciva # Clean up 1842161748Scperciva rm files.wanted filelist patchlist 1843161748Scperciva} 1844161748Scperciva 1845161748Scperciva# Create and populate install manifest directory; and report what updates 1846161748Scperciva# are available. 1847161748Scpercivafetch_create_manifest () { 1848161748Scperciva # If we have an existing install manifest, nuke it. 1849161748Scperciva if [ -L "${BDHASH}-install" ]; then 1850161748Scperciva rm -r ${BDHASH}-install/ 1851161748Scperciva rm ${BDHASH}-install 1852161748Scperciva fi 1853161748Scperciva 1854161748Scperciva # Report to the user if any updates were avoided due to local changes 1855161748Scperciva if [ -s modifiedfiles ]; then 1856161748Scperciva echo 1857161748Scperciva echo -n "The following files are affected by updates, " 1858161748Scperciva echo "but no changes have" 1859161748Scperciva echo -n "been downloaded because the files have been " 1860161748Scperciva echo "modified locally:" 1861161748Scperciva cat modifiedfiles 1862173564Scperciva fi | more 1863161748Scperciva rm modifiedfiles 1864161748Scperciva 1865161748Scperciva # If no files will be updated, tell the user and exit 1866161748Scperciva if ! [ -s INDEX-PRESENT ] && 1867161748Scperciva ! [ -s INDEX-NEW ]; then 1868161748Scperciva rm INDEX-PRESENT INDEX-NEW 1869161748Scperciva echo 1870161748Scperciva echo -n "No updates needed to update system to " 1871161748Scperciva echo "${RELNUM}-p${RELPATCHNUM}." 1872161748Scperciva return 1873161748Scperciva fi 1874161748Scperciva 1875161748Scperciva # Divide files into (a) removed files, (b) added files, and 1876161748Scperciva # (c) updated files. 1877161748Scperciva cut -f 1 -d '|' < INDEX-PRESENT | 1878161748Scperciva sort > INDEX-PRESENT.flist 1879161748Scperciva cut -f 1 -d '|' < INDEX-NEW | 1880161748Scperciva sort > INDEX-NEW.flist 1881161748Scperciva comm -23 INDEX-PRESENT.flist INDEX-NEW.flist > files.removed 1882161748Scperciva comm -13 INDEX-PRESENT.flist INDEX-NEW.flist > files.added 1883161748Scperciva comm -12 INDEX-PRESENT.flist INDEX-NEW.flist > files.updated 1884161748Scperciva rm INDEX-PRESENT.flist INDEX-NEW.flist 1885161748Scperciva 1886161748Scperciva # Report removed files, if any 1887161748Scperciva if [ -s files.removed ]; then 1888161748Scperciva echo 1889161748Scperciva echo -n "The following files will be removed " 1890161748Scperciva echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:" 1891161748Scperciva cat files.removed 1892173564Scperciva fi | more 1893161748Scperciva rm files.removed 1894161748Scperciva 1895161748Scperciva # Report added files, if any 1896161748Scperciva if [ -s files.added ]; then 1897161748Scperciva echo 1898161748Scperciva echo -n "The following files will be added " 1899161748Scperciva echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:" 1900161748Scperciva cat files.added 1901173564Scperciva fi | more 1902161748Scperciva rm files.added 1903161748Scperciva 1904161748Scperciva # Report updated files, if any 1905161748Scperciva if [ -s files.updated ]; then 1906161748Scperciva echo 1907161748Scperciva echo -n "The following files will be updated " 1908161748Scperciva echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:" 1909161748Scperciva 1910161748Scperciva cat files.updated 1911173564Scperciva fi | more 1912161748Scperciva rm files.updated 1913161748Scperciva 1914161748Scperciva # Create a directory for the install manifest. 1915161748Scperciva MDIR=`mktemp -d install.XXXXXX` || return 1 1916161748Scperciva 1917161748Scperciva # Populate it 1918161748Scperciva mv INDEX-PRESENT ${MDIR}/INDEX-OLD 1919161748Scperciva mv INDEX-NEW ${MDIR}/INDEX-NEW 1920161748Scperciva 1921161748Scperciva # Link it into place 1922161748Scperciva ln -s ${MDIR} ${BDHASH}-install 1923161748Scperciva} 1924161748Scperciva 1925161748Scperciva# Warn about any upcoming EoL 1926161748Scpercivafetch_warn_eol () { 1927161748Scperciva # What's the current time? 1928161748Scperciva NOWTIME=`date "+%s"` 1929161748Scperciva 1930161748Scperciva # When did we last warn about the EoL date? 1931161748Scperciva if [ -f lasteolwarn ]; then 1932161748Scperciva LASTWARN=`cat lasteolwarn` 1933161748Scperciva else 1934161748Scperciva LASTWARN=`expr ${NOWTIME} - 63072000` 1935161748Scperciva fi 1936161748Scperciva 1937161748Scperciva # If the EoL time is past, warn. 1938161748Scperciva if [ ${EOLTIME} -lt ${NOWTIME} ]; then 1939161748Scperciva echo 1940161748Scperciva cat <<-EOF 1941161869Scperciva WARNING: `uname -sr` HAS PASSED ITS END-OF-LIFE DATE. 1942161748Scperciva Any security issues discovered after `date -r ${EOLTIME}` 1943161748Scperciva will not have been corrected. 1944161748Scperciva EOF 1945161748Scperciva return 1 1946161748Scperciva fi 1947161748Scperciva 1948161748Scperciva # Figure out how long it has been since we last warned about the 1949161748Scperciva # upcoming EoL, and how much longer we have left. 1950161748Scperciva SINCEWARN=`expr ${NOWTIME} - ${LASTWARN}` 1951161748Scperciva TIMELEFT=`expr ${EOLTIME} - ${NOWTIME}` 1952161748Scperciva 1953171838Scperciva # Don't warn if the EoL is more than 3 months away 1954171838Scperciva if [ ${TIMELEFT} -gt 7884000 ]; then 1955161748Scperciva return 0 1956161748Scperciva fi 1957161748Scperciva 1958161748Scperciva # Don't warn if the time remaining is more than 3 times the time 1959161748Scperciva # since the last warning. 1960161748Scperciva if [ ${TIMELEFT} -gt `expr ${SINCEWARN} \* 3` ]; then 1961161748Scperciva return 0 1962161748Scperciva fi 1963161748Scperciva 1964161748Scperciva # Figure out what time units to use. 1965161748Scperciva if [ ${TIMELEFT} -lt 604800 ]; then 1966161748Scperciva UNIT="day" 1967161748Scperciva SIZE=86400 1968161748Scperciva elif [ ${TIMELEFT} -lt 2678400 ]; then 1969161748Scperciva UNIT="week" 1970161748Scperciva SIZE=604800 1971161748Scperciva else 1972161748Scperciva UNIT="month" 1973161748Scperciva SIZE=2678400 1974161748Scperciva fi 1975161748Scperciva 1976161748Scperciva # Compute the right number of units 1977161748Scperciva NUM=`expr ${TIMELEFT} / ${SIZE}` 1978161748Scperciva if [ ${NUM} != 1 ]; then 1979161748Scperciva UNIT="${UNIT}s" 1980161748Scperciva fi 1981161748Scperciva 1982161748Scperciva # Print the warning 1983161748Scperciva echo 1984161748Scperciva cat <<-EOF 1985161748Scperciva WARNING: `uname -sr` is approaching its End-of-Life date. 1986161748Scperciva It is strongly recommended that you upgrade to a newer 1987161748Scperciva release within the next ${NUM} ${UNIT}. 1988161748Scperciva EOF 1989161748Scperciva 1990161748Scperciva # Update the stored time of last warning 1991161748Scperciva echo ${NOWTIME} > lasteolwarn 1992161748Scperciva} 1993161748Scperciva 1994161748Scperciva# Do the actual work involved in "fetch" / "cron". 1995161748Scpercivafetch_run () { 1996161748Scperciva workdir_init || return 1 1997161748Scperciva 1998161748Scperciva # Prepare the mirror list. 1999161748Scperciva fetch_pick_server_init && fetch_pick_server 2000161748Scperciva 2001161748Scperciva # Try to fetch the public key until we run out of servers. 2002161748Scperciva while ! fetch_key; do 2003161748Scperciva fetch_pick_server || return 1 2004161748Scperciva done 2005161748Scperciva 2006161748Scperciva # Try to fetch the metadata index signature ("tag") until we run 2007161748Scperciva # out of available servers; and sanity check the downloaded tag. 2008161748Scperciva while ! fetch_tag; do 2009161748Scperciva fetch_pick_server || return 1 2010161748Scperciva done 2011161748Scperciva fetch_tagsanity || return 1 2012161748Scperciva 2013161748Scperciva # Fetch the latest INDEX-NEW and INDEX-OLD files. 2014161748Scperciva fetch_metadata INDEX-NEW INDEX-OLD || return 1 2015161748Scperciva 2016161748Scperciva # Generate filtered INDEX-NEW and INDEX-OLD files containing only 2017161748Scperciva # the lines which (a) belong to components we care about, and (b) 2018161748Scperciva # don't correspond to paths we're explicitly ignoring. 2019161748Scperciva fetch_filter_metadata INDEX-NEW || return 1 2020161748Scperciva fetch_filter_metadata INDEX-OLD || return 1 2021161748Scperciva 2022173564Scperciva # Translate /boot/${KERNCONF} into ${KERNELDIR} 2023173564Scperciva fetch_filter_kernel_names INDEX-NEW ${KERNCONF} 2024173564Scperciva fetch_filter_kernel_names INDEX-OLD ${KERNCONF} 2025161748Scperciva 2026161748Scperciva # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the 2027161748Scperciva # system and generate an INDEX-PRESENT file. 2028161748Scperciva fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2029161748Scperciva 2030161748Scperciva # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which 2031161748Scperciva # correspond to lines in INDEX-PRESENT with hashes not appearing 2032161748Scperciva # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in 2033161748Scperciva # INDEX-PRESENT has type - and there isn't a corresponding entry in 2034161748Scperciva # INDEX-OLD with type -. 2035173564Scperciva fetch_filter_unmodified_notpresent \ 2036173564Scperciva INDEX-OLD INDEX-PRESENT INDEX-NEW /dev/null 2037161748Scperciva 2038161748Scperciva # For each entry in INDEX-PRESENT of type -, remove any corresponding 2039161748Scperciva # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries 2040161748Scperciva # of type - from INDEX-PRESENT. 2041161748Scperciva fetch_filter_allowadd INDEX-PRESENT INDEX-NEW 2042161748Scperciva 2043161748Scperciva # If ${ALLOWDELETE} != "yes", then remove any entries from 2044161748Scperciva # INDEX-PRESENT which don't correspond to entries in INDEX-NEW. 2045161748Scperciva fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW 2046161748Scperciva 2047161748Scperciva # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in 2048161748Scperciva # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD, 2049161748Scperciva # replace the corresponding line of INDEX-NEW with one having the 2050161748Scperciva # same metadata as the entry in INDEX-PRESENT. 2051161748Scperciva fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW 2052161748Scperciva 2053161748Scperciva # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical; 2054161748Scperciva # no need to update a file if it isn't changing. 2055161748Scperciva fetch_filter_uptodate INDEX-PRESENT INDEX-NEW 2056161748Scperciva 2057161748Scperciva # Prepare to fetch files: Generate a list of the files we need, 2058161748Scperciva # copy the unmodified files we have into /files/, and generate 2059161748Scperciva # a list of patches to download. 2060171784Scperciva fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2061161748Scperciva 2062161748Scperciva # Fetch files. 2063161748Scperciva fetch_files || return 1 2064161748Scperciva 2065161748Scperciva # Create and populate install manifest directory; and report what 2066161748Scperciva # updates are available. 2067161748Scperciva fetch_create_manifest || return 1 2068161748Scperciva 2069161748Scperciva # Warn about any upcoming EoL 2070161748Scperciva fetch_warn_eol || return 1 2071161748Scperciva} 2072161748Scperciva 2073173564Scperciva# If StrictComponents is not "yes", generate a new components list 2074173564Scperciva# with only the components which appear to be installed. 2075173564Scpercivaupgrade_guess_components () { 2076173564Scperciva if [ "${STRICTCOMPONENTS}" = "no" ]; then 2077173564Scperciva # Generate filtered INDEX-ALL with only the components listed 2078173564Scperciva # in COMPONENTS. 2079173564Scperciva fetch_filter_metadata_components $1 || return 1 2080173564Scperciva 2081173564Scperciva # Tell the user why his disk is suddenly making lots of noise 2082173564Scperciva echo -n "Inspecting system... " 2083173564Scperciva 2084173564Scperciva # Look at the files on disk, and assume that a component is 2085173564Scperciva # supposed to be present if it is more than half-present. 2086173564Scperciva cut -f 1-3 -d '|' < INDEX-ALL | 2087173564Scperciva tr '|' ' ' | 2088173564Scperciva while read C S F; do 2089173564Scperciva if [ -e ${BASEDIR}/${F} ]; then 2090173564Scperciva echo "+ ${C}|${S}" 2091173564Scperciva fi 2092173564Scperciva echo "= ${C}|${S}" 2093173564Scperciva done | 2094173564Scperciva sort | 2095173564Scperciva uniq -c | 2096173564Scperciva sed -E 's,^ +,,' > compfreq 2097173564Scperciva grep ' = ' compfreq | 2098173564Scperciva cut -f 1,3 -d ' ' | 2099173564Scperciva sort -k 2,2 -t ' ' > compfreq.total 2100173564Scperciva grep ' + ' compfreq | 2101173564Scperciva cut -f 1,3 -d ' ' | 2102173564Scperciva sort -k 2,2 -t ' ' > compfreq.present 2103173564Scperciva join -t ' ' -1 2 -2 2 compfreq.present compfreq.total | 2104173564Scperciva while read S P T; do 2105173564Scperciva if [ ${P} -gt `expr ${T} / 2` ]; then 2106173564Scperciva echo ${S} 2107173564Scperciva fi 2108173564Scperciva done > comp.present 2109173564Scperciva cut -f 2 -d ' ' < compfreq.total > comp.total 2110173564Scperciva rm INDEX-ALL compfreq compfreq.total compfreq.present 2111173564Scperciva 2112173564Scperciva # We're done making noise. 2113173564Scperciva echo "done." 2114173564Scperciva 2115173564Scperciva # Sometimes the kernel isn't installed where INDEX-ALL 2116173564Scperciva # thinks that it should be: In particular, it is often in 2117173564Scperciva # /boot/kernel instead of /boot/GENERIC or /boot/SMP. To 2118173564Scperciva # deal with this, if "kernel|X" is listed in comp.total 2119173564Scperciva # (i.e., is a component which would be upgraded if it is 2120173564Scperciva # found to be present) we will add it to comp.present. 2121173564Scperciva # If "kernel|<anything>" is in comp.total but "kernel|X" is 2122173564Scperciva # not, we print a warning -- the user is running a kernel 2123173564Scperciva # which isn't part of the release. 2124173564Scperciva KCOMP=`echo ${KERNCONF} | tr 'A-Z' 'a-z'` 2125173564Scperciva grep -E "^kernel\|${KCOMP}\$" comp.total >> comp.present 2126173564Scperciva 2127173564Scperciva if grep -qE "^kernel\|" comp.total && 2128173564Scperciva ! grep -qE "^kernel\|${KCOMP}\$" comp.total; then 2129173564Scperciva cat <<-EOF 2130173564Scperciva 2131173564ScpercivaWARNING: This system is running a "${KCOMP}" kernel, which is not a 2132173564Scpercivakernel configuration distributed as part of FreeBSD ${RELNUM}. 2133173564ScpercivaThis kernel will not be updated: you MUST update the kernel manually 2134173564Scpercivabefore running "$0 install". 2135173564Scperciva EOF 2136173564Scperciva fi 2137173564Scperciva 2138173564Scperciva # Re-sort the list of installed components and generate 2139173564Scperciva # the list of non-installed components. 2140173564Scperciva sort -u < comp.present > comp.present.tmp 2141173564Scperciva mv comp.present.tmp comp.present 2142173564Scperciva comm -13 comp.present comp.total > comp.absent 2143173564Scperciva 2144173564Scperciva # Ask the user to confirm that what we have is correct. To 2145173564Scperciva # reduce user confusion, translate "X|Y" back to "X/Y" (as 2146173564Scperciva # subcomponents must be listed in the configuration file). 2147173564Scperciva echo 2148173564Scperciva echo -n "The following components of FreeBSD " 2149173564Scperciva echo "seem to be installed:" 2150173564Scperciva tr '|' '/' < comp.present | 2151173564Scperciva fmt -72 2152173564Scperciva echo 2153173564Scperciva echo -n "The following components of FreeBSD " 2154173564Scperciva echo "do not seem to be installed:" 2155173564Scperciva tr '|' '/' < comp.absent | 2156173564Scperciva fmt -72 2157173564Scperciva echo 2158173564Scperciva continuep || return 1 2159173564Scperciva echo 2160173564Scperciva 2161173564Scperciva # Suck the generated list of components into ${COMPONENTS}. 2162173564Scperciva # Note that comp.present.tmp is used due to issues with 2163173564Scperciva # pipelines and setting variables. 2164173564Scperciva COMPONENTS="" 2165173564Scperciva tr '|' '/' < comp.present > comp.present.tmp 2166173564Scperciva while read C; do 2167173564Scperciva COMPONENTS="${COMPONENTS} ${C}" 2168173564Scperciva done < comp.present.tmp 2169173564Scperciva 2170173564Scperciva # Delete temporary files 2171173564Scperciva rm comp.present comp.present.tmp comp.absent comp.total 2172173564Scperciva fi 2173173564Scperciva} 2174173564Scperciva 2175173564Scperciva# If StrictComponents is not "yes", COMPONENTS contains an entry 2176173564Scperciva# corresponding to the currently running kernel, and said kernel 2177173564Scperciva# does not exist in the new release, add "kernel/generic" to the 2178173564Scperciva# list of components. 2179173564Scpercivaupgrade_guess_new_kernel () { 2180173564Scperciva if [ "${STRICTCOMPONENTS}" = "no" ]; then 2181173564Scperciva # Grab the unfiltered metadata file. 2182173564Scperciva METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'` 2183173564Scperciva gunzip -c < files/${METAHASH}.gz > $1.all 2184173564Scperciva 2185173564Scperciva # If "kernel/${KCOMP}" is in ${COMPONENTS} and that component 2186173564Scperciva # isn't in $1.all, we need to add kernel/generic. 2187173564Scperciva for C in ${COMPONENTS}; do 2188173564Scperciva if [ ${C} = "kernel/${KCOMP}" ] && 2189173564Scperciva ! grep -qE "^kernel\|${KCOMP}\|" $1.all; then 2190173564Scperciva COMPONENTS="${COMPONENTS} kernel/generic" 2191173564Scperciva NKERNCONF="GENERIC" 2192173564Scperciva cat <<-EOF 2193173564Scperciva 2194173564ScpercivaWARNING: This system is running a "${KCOMP}" kernel, which is not a 2195173564Scpercivakernel configuration distributed as part of FreeBSD ${RELNUM}. 2196173564ScpercivaAs part of upgrading to FreeBSD ${RELNUM}, this kernel will be 2197173564Scpercivareplaced with a "generic" kernel. 2198173564Scperciva EOF 2199173564Scperciva continuep || return 1 2200173564Scperciva fi 2201173564Scperciva done 2202173564Scperciva 2203173564Scperciva # Don't need this any more... 2204173564Scperciva rm $1.all 2205173564Scperciva fi 2206173564Scperciva} 2207173564Scperciva 2208173564Scperciva# Convert INDEX-OLD (last release) and INDEX-ALL (new release) into 2209173564Scperciva# INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades). 2210173564Scpercivaupgrade_oldall_to_oldnew () { 2211173564Scperciva # For each ${F}|... which appears in INDEX-ALL but does not appear 2212173564Scperciva # in INDEX-OLD, add ${F}|-|||||| to INDEX-OLD. 2213173564Scperciva cut -f 1 -d '|' < $1 | 2214173564Scperciva sort -u > $1.paths 2215173564Scperciva cut -f 1 -d '|' < $2 | 2216173564Scperciva sort -u | 2217173564Scperciva comm -13 $1.paths - | 2218173564Scperciva lam - -s "|-||||||" | 2219173564Scperciva sort - $1 > $1.tmp 2220173564Scperciva mv $1.tmp $1 2221173564Scperciva 2222173564Scperciva # Remove lines from INDEX-OLD which also appear in INDEX-ALL 2223173564Scperciva comm -23 $1 $2 > $1.tmp 2224173564Scperciva mv $1.tmp $1 2225173564Scperciva 2226173564Scperciva # Remove lines from INDEX-ALL which have a file name not appearing 2227173564Scperciva # anywhere in INDEX-OLD (since these must be files which haven't 2228173564Scperciva # changed -- if they were new, there would be an entry of type "-"). 2229173564Scperciva cut -f 1 -d '|' < $1 | 2230173564Scperciva sort -u > $1.paths 2231173564Scperciva sort -k 1,1 -t '|' < $2 | 2232173564Scperciva join -t '|' - $1.paths | 2233173564Scperciva sort > $2.tmp 2234173564Scperciva rm $1.paths 2235173564Scperciva mv $2.tmp $2 2236173564Scperciva 2237173564Scperciva # Rename INDEX-ALL to INDEX-NEW. 2238173564Scperciva mv $2 $3 2239173564Scperciva} 2240173564Scperciva 2241173564Scperciva# From the list of "old" files in $1, merge changes in $2 with those in $3, 2242173564Scperciva# and update $3 to reflect the hashes of merged files. 2243173564Scpercivaupgrade_merge () { 2244173564Scperciva # We only need to do anything if $1 is non-empty. 2245173564Scperciva if [ -s $1 ]; then 2246173564Scperciva cut -f 1 -d '|' $1 | 2247173564Scperciva sort > $1-paths 2248173564Scperciva 2249173564Scperciva # Create staging area for merging files 2250173564Scperciva rm -rf merge/ 2251173564Scperciva while read F; do 2252173564Scperciva D=`dirname ${F}` 2253173564Scperciva mkdir -p merge/old/${D} 2254173564Scperciva mkdir -p merge/${OLDRELNUM}/${D} 2255173564Scperciva mkdir -p merge/${RELNUM}/${D} 2256173564Scperciva mkdir -p merge/new/${D} 2257173564Scperciva done < $1-paths 2258173564Scperciva 2259173564Scperciva # Copy in files 2260173564Scperciva while read F; do 2261173564Scperciva # Currently installed file 2262173564Scperciva V=`look "${F}|" $2 | cut -f 7 -d '|'` 2263173564Scperciva gunzip < files/${V}.gz > merge/old/${F} 2264173564Scperciva 2265173564Scperciva # Old release 2266173564Scperciva if look "${F}|" $1 | fgrep -q "|f|"; then 2267173564Scperciva V=`look "${F}|" $1 | cut -f 3 -d '|'` 2268173564Scperciva gunzip < files/${V}.gz \ 2269173564Scperciva > merge/${OLDRELNUM}/${F} 2270173564Scperciva fi 2271173564Scperciva 2272173564Scperciva # New release 2273173564Scperciva if look "${F}|" $3 | cut -f 1,2,7 -d '|' | 2274173564Scperciva fgrep -q "|f|"; then 2275173564Scperciva V=`look "${F}|" $3 | cut -f 7 -d '|'` 2276173564Scperciva gunzip < files/${V}.gz \ 2277173564Scperciva > merge/${RELNUM}/${F} 2278173564Scperciva fi 2279173564Scperciva done < $1-paths 2280173564Scperciva 2281173564Scperciva # Attempt to automatically merge changes 2282173564Scperciva echo -n "Attempting to automatically merge " 2283173564Scperciva echo -n "changes in files..." 2284173564Scperciva : > failed.merges 2285173564Scperciva while read F; do 2286173564Scperciva # If the file doesn't exist in the new release, 2287173564Scperciva # the result of "merging changes" is having the file 2288173564Scperciva # not exist. 2289173564Scperciva if ! [ -f merge/${RELNUM}/${F} ]; then 2290173564Scperciva continue 2291173564Scperciva fi 2292173564Scperciva 2293173564Scperciva # If the file didn't exist in the old release, we're 2294173564Scperciva # going to throw away the existing file and hope that 2295173564Scperciva # the version from the new release is what we want. 2296173564Scperciva if ! [ -f merge/${OLDRELNUM}/${F} ]; then 2297173564Scperciva cp merge/${RELNUM}/${F} merge/new/${F} 2298173564Scperciva continue 2299173564Scperciva fi 2300173564Scperciva 2301173564Scperciva # Some files need special treatment. 2302173564Scperciva case ${F} in 2303173564Scperciva /etc/spwd.db | /etc/pwd.db | /etc/login.conf.db) 2304173564Scperciva # Don't merge these -- we're rebuild them 2305173564Scperciva # after updates are installed. 2306173564Scperciva cp merge/old/${F} merge/new/${F} 2307173564Scperciva ;; 2308173564Scperciva *) 2309173564Scperciva if ! merge -p -L "current version" \ 2310173564Scperciva -L "${OLDRELNUM}" -L "${RELNUM}" \ 2311173564Scperciva merge/old/${F} \ 2312173564Scperciva merge/${OLDRELNUM}/${F} \ 2313173564Scperciva merge/${RELNUM}/${F} \ 2314173564Scperciva > merge/new/${F} 2>/dev/null; then 2315173564Scperciva echo ${F} >> failed.merges 2316173564Scperciva fi 2317173564Scperciva ;; 2318173564Scperciva esac 2319173564Scperciva done < $1-paths 2320173564Scperciva echo " done." 2321173564Scperciva 2322173564Scperciva # Ask the user to handle any files which didn't merge. 2323173564Scperciva while read F; do 2324173564Scperciva cat <<-EOF 2325173564Scperciva 2326173564ScpercivaThe following file could not be merged automatically: ${F} 2327173564ScpercivaPress Enter to edit this file in ${EDITOR} and resolve the conflicts 2328173564Scpercivamanually... 2329173564Scperciva EOF 2330173564Scperciva read dummy </dev/tty 2331173564Scperciva ${EDITOR} `pwd`/merge/new/${F} < /dev/tty 2332173564Scperciva done < failed.merges 2333173564Scperciva rm failed.merges 2334173564Scperciva 2335173564Scperciva # Ask the user to confirm that he likes how the result 2336173564Scperciva # of merging files. 2337173564Scperciva while read F; do 2338173564Scperciva # Skip files which haven't changed. 2339173564Scperciva if [ -f merge/new/${F} ] && 2340173564Scperciva cmp -s merge/old/${F} merge/new/${F}; then 2341173564Scperciva continue 2342173564Scperciva fi 2343173564Scperciva 2344173564Scperciva # Warn about files which are ceasing to exist. 2345173564Scperciva if ! [ -f merge/new/${F} ]; then 2346173564Scperciva cat <<-EOF 2347173564Scperciva 2348173564ScpercivaThe following file will be removed, as it no longer exists in 2349173564ScpercivaFreeBSD ${RELNUM}: ${F} 2350173564Scperciva EOF 2351173564Scperciva continuep < /dev/tty || return 1 2352173564Scperciva continue 2353173564Scperciva fi 2354173564Scperciva 2355173564Scperciva # Print changes for the user's approval. 2356173564Scperciva cat <<-EOF 2357173564Scperciva 2358173564ScpercivaThe following changes, which occurred between FreeBSD ${OLDRELNUM} and 2359173564ScpercivaFreeBSD ${RELNUM} have been merged into ${F}: 2360173564ScpercivaEOF 2361173564Scperciva diff -U 5 -L "current version" -L "new version" \ 2362173564Scperciva merge/old/${F} merge/new/${F} || true 2363173564Scperciva continuep < /dev/tty || return 1 2364173564Scperciva done < $1-paths 2365173564Scperciva 2366173564Scperciva # Store merged files. 2367173564Scperciva while read F; do 2368177527Scperciva if [ -f merge/new/${F} ]; then 2369177527Scperciva V=`${SHA256} -q merge/new/${F}` 2370173564Scperciva 2371173564Scperciva gzip -c < merge/new/${F} > files/${V}.gz 2372173564Scperciva echo "${F}|${V}" 2373173564Scperciva fi 2374173564Scperciva done < $1-paths > newhashes 2375173564Scperciva 2376173564Scperciva # Pull lines out from $3 which need to be updated to 2377173564Scperciva # reflect merged files. 2378173564Scperciva while read F; do 2379173564Scperciva look "${F}|" $3 2380173564Scperciva done < $1-paths > $3-oldlines 2381173564Scperciva 2382173564Scperciva # Update lines to reflect merged files 2383173564Scperciva join -t '|' -o 1.1,1.2,1.3,1.4,1.5,1.6,2.2,1.8 \ 2384173564Scperciva $3-oldlines newhashes > $3-newlines 2385173564Scperciva 2386173564Scperciva # Remove old lines from $3 and add new lines. 2387173564Scperciva sort $3-oldlines | 2388173564Scperciva comm -13 - $3 | 2389173564Scperciva sort - $3-newlines > $3.tmp 2390173564Scperciva mv $3.tmp $3 2391173564Scperciva 2392173564Scperciva # Clean up 2393173564Scperciva rm $1-paths newhashes $3-oldlines $3-newlines 2394173564Scperciva rm -rf merge/ 2395173564Scperciva fi 2396173564Scperciva 2397173564Scperciva # We're done with merging files. 2398173564Scperciva rm $1 2399173564Scperciva} 2400173564Scperciva 2401173564Scperciva# Do the work involved in fetching upgrades to a new release 2402173564Scpercivaupgrade_run () { 2403173564Scperciva workdir_init || return 1 2404173564Scperciva 2405173564Scperciva # Prepare the mirror list. 2406173564Scperciva fetch_pick_server_init && fetch_pick_server 2407173564Scperciva 2408173564Scperciva # Try to fetch the public key until we run out of servers. 2409173564Scperciva while ! fetch_key; do 2410173564Scperciva fetch_pick_server || return 1 2411173564Scperciva done 2412173564Scperciva 2413173564Scperciva # Try to fetch the metadata index signature ("tag") until we run 2414173564Scperciva # out of available servers; and sanity check the downloaded tag. 2415173564Scperciva while ! fetch_tag; do 2416173564Scperciva fetch_pick_server || return 1 2417173564Scperciva done 2418173564Scperciva fetch_tagsanity || return 1 2419173564Scperciva 2420173564Scperciva # Fetch the INDEX-OLD and INDEX-ALL. 2421173564Scperciva fetch_metadata INDEX-OLD INDEX-ALL || return 1 2422173564Scperciva 2423173564Scperciva # If StrictComponents is not "yes", generate a new components list 2424173564Scperciva # with only the components which appear to be installed. 2425173564Scperciva upgrade_guess_components INDEX-ALL || return 1 2426173564Scperciva 2427173564Scperciva # Generate filtered INDEX-OLD and INDEX-ALL files containing only 2428173564Scperciva # the components we want and without anything marked as "Ignore". 2429173564Scperciva fetch_filter_metadata INDEX-OLD || return 1 2430173564Scperciva fetch_filter_metadata INDEX-ALL || return 1 2431173564Scperciva 2432173564Scperciva # Merge the INDEX-OLD and INDEX-ALL files into INDEX-OLD. 2433173564Scperciva sort INDEX-OLD INDEX-ALL > INDEX-OLD.tmp 2434173564Scperciva mv INDEX-OLD.tmp INDEX-OLD 2435173564Scperciva rm INDEX-ALL 2436173564Scperciva 2437173564Scperciva # Adjust variables for fetching files from the new release. 2438173564Scperciva OLDRELNUM=${RELNUM} 2439173564Scperciva RELNUM=${TARGETRELEASE} 2440173564Scperciva OLDFETCHDIR=${FETCHDIR} 2441173564Scperciva FETCHDIR=${RELNUM}/${ARCH} 2442173564Scperciva 2443173564Scperciva # Try to fetch the NEW metadata index signature ("tag") until we run 2444173564Scperciva # out of available servers; and sanity check the downloaded tag. 2445173564Scperciva while ! fetch_tag; do 2446173564Scperciva fetch_pick_server || return 1 2447173564Scperciva done 2448173564Scperciva 2449173564Scperciva # Fetch the new INDEX-ALL. 2450173564Scperciva fetch_metadata INDEX-ALL || return 1 2451173564Scperciva 2452173564Scperciva # If StrictComponents is not "yes", COMPONENTS contains an entry 2453173564Scperciva # corresponding to the currently running kernel, and said kernel 2454173564Scperciva # does not exist in the new release, add "kernel/generic" to the 2455173564Scperciva # list of components. 2456173564Scperciva upgrade_guess_new_kernel INDEX-ALL || return 1 2457173564Scperciva 2458173564Scperciva # Filter INDEX-ALL to contain only the components we want and without 2459173564Scperciva # anything marked as "Ignore". 2460173564Scperciva fetch_filter_metadata INDEX-ALL || return 1 2461173564Scperciva 2462173564Scperciva # Convert INDEX-OLD (last release) and INDEX-ALL (new release) into 2463173564Scperciva # INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades). 2464173564Scperciva upgrade_oldall_to_oldnew INDEX-OLD INDEX-ALL INDEX-NEW 2465173564Scperciva 2466173564Scperciva # Translate /boot/${KERNCONF} or /boot/${NKERNCONF} into ${KERNELDIR} 2467173564Scperciva fetch_filter_kernel_names INDEX-NEW ${NKERNCONF} 2468173564Scperciva fetch_filter_kernel_names INDEX-OLD ${KERNCONF} 2469173564Scperciva 2470173564Scperciva # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the 2471173564Scperciva # system and generate an INDEX-PRESENT file. 2472173564Scperciva fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2473173564Scperciva 2474173564Scperciva # Based on ${MERGECHANGES}, generate a file tomerge-old with the 2475173564Scperciva # paths and hashes of old versions of files to merge. 2476173564Scperciva fetch_filter_mergechanges INDEX-OLD INDEX-PRESENT tomerge-old 2477173564Scperciva 2478173564Scperciva # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which 2479173564Scperciva # correspond to lines in INDEX-PRESENT with hashes not appearing 2480173564Scperciva # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in 2481173564Scperciva # INDEX-PRESENT has type - and there isn't a corresponding entry in 2482173564Scperciva # INDEX-OLD with type -. 2483173564Scperciva fetch_filter_unmodified_notpresent \ 2484173564Scperciva INDEX-OLD INDEX-PRESENT INDEX-NEW tomerge-old 2485173564Scperciva 2486173564Scperciva # For each entry in INDEX-PRESENT of type -, remove any corresponding 2487173564Scperciva # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries 2488173564Scperciva # of type - from INDEX-PRESENT. 2489173564Scperciva fetch_filter_allowadd INDEX-PRESENT INDEX-NEW 2490173564Scperciva 2491173564Scperciva # If ${ALLOWDELETE} != "yes", then remove any entries from 2492173564Scperciva # INDEX-PRESENT which don't correspond to entries in INDEX-NEW. 2493173564Scperciva fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW 2494173564Scperciva 2495173564Scperciva # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in 2496173564Scperciva # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD, 2497173564Scperciva # replace the corresponding line of INDEX-NEW with one having the 2498173564Scperciva # same metadata as the entry in INDEX-PRESENT. 2499173564Scperciva fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW 2500173564Scperciva 2501173564Scperciva # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical; 2502173564Scperciva # no need to update a file if it isn't changing. 2503173564Scperciva fetch_filter_uptodate INDEX-PRESENT INDEX-NEW 2504173564Scperciva 2505173564Scperciva # Fetch "clean" files from the old release for merging changes. 2506173564Scperciva fetch_files_premerge tomerge-old 2507173564Scperciva 2508173564Scperciva # Prepare to fetch files: Generate a list of the files we need, 2509173564Scperciva # copy the unmodified files we have into /files/, and generate 2510173564Scperciva # a list of patches to download. 2511173564Scperciva fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2512173564Scperciva 2513173564Scperciva # Fetch patches from to-${RELNUM}/${ARCH}/bp/ 2514173564Scperciva PATCHDIR=to-${RELNUM}/${ARCH}/bp 2515173564Scperciva fetch_files || return 1 2516173564Scperciva 2517173564Scperciva # Merge configuration file changes. 2518173564Scperciva upgrade_merge tomerge-old INDEX-PRESENT INDEX-NEW || return 1 2519173564Scperciva 2520173564Scperciva # Create and populate install manifest directory; and report what 2521173564Scperciva # updates are available. 2522173564Scperciva fetch_create_manifest || return 1 2523173564Scperciva 2524173564Scperciva # Leave a note behind to tell the "install" command that the kernel 2525173564Scperciva # needs to be installed before the world. 2526173564Scperciva touch ${BDHASH}-install/kernelfirst 2527173564Scperciva} 2528173564Scperciva 2529161748Scperciva# Make sure that all the file hashes mentioned in $@ have corresponding 2530161748Scperciva# gzipped files stored in /files/. 2531161748Scpercivainstall_verify () { 2532161748Scperciva # Generate a list of hashes 2533161748Scperciva cat $@ | 2534161748Scperciva cut -f 2,7 -d '|' | 2535161748Scperciva grep -E '^f' | 2536161748Scperciva cut -f 2 -d '|' | 2537161748Scperciva sort -u > filelist 2538161748Scperciva 2539161748Scperciva # Make sure all the hashes exist 2540161748Scperciva while read HASH; do 2541161748Scperciva if ! [ -f files/${HASH}.gz ]; then 2542161748Scperciva echo -n "Update files missing -- " 2543161748Scperciva echo "this should never happen." 2544161748Scperciva echo "Re-run '$0 fetch'." 2545161748Scperciva return 1 2546161748Scperciva fi 2547161748Scperciva done < filelist 2548161748Scperciva 2549161748Scperciva # Clean up 2550161748Scperciva rm filelist 2551161748Scperciva} 2552161748Scperciva 2553161748Scperciva# Remove the system immutable flag from files 2554161748Scpercivainstall_unschg () { 2555161748Scperciva # Generate file list 2556161748Scperciva cat $@ | 2557161748Scperciva cut -f 1 -d '|' > filelist 2558161748Scperciva 2559161748Scperciva # Remove flags 2560161748Scperciva while read F; do 2561169603Scperciva if ! [ -e ${BASEDIR}/${F} ]; then 2562161748Scperciva continue 2563161748Scperciva fi 2564161748Scperciva 2565169603Scperciva chflags noschg ${BASEDIR}/${F} || return 1 2566161748Scperciva done < filelist 2567161748Scperciva 2568161748Scperciva # Clean up 2569161748Scperciva rm filelist 2570161748Scperciva} 2571161748Scperciva 2572196392Ssimon# Decide which directory name to use for kernel backups. 2573196392Ssimonbackup_kernel_finddir () { 2574196392Ssimon CNT=0 2575196392Ssimon while true ; do 2576196392Ssimon # Pathname does not exist, so it is OK use that name 2577196392Ssimon # for backup directory. 2578196392Ssimon if [ ! -e $BACKUPKERNELDIR ]; then 2579196392Ssimon return 0 2580196392Ssimon fi 2581196392Ssimon 2582196392Ssimon # If directory do exist, we only use if it has our 2583196392Ssimon # marker file. 2584196392Ssimon if [ -d $BACKUPKERNELDIR -a \ 2585196392Ssimon -e $BACKUPKERNELDIR/.freebsd-update ]; then 2586196392Ssimon return 0 2587196392Ssimon fi 2588196392Ssimon 2589196392Ssimon # We could not use current directory name, so add counter to 2590196392Ssimon # the end and try again. 2591196392Ssimon CNT=$((CNT + 1)) 2592196392Ssimon if [ $CNT -gt 9 ]; then 2593196392Ssimon echo "Could not find valid backup dir ($BACKUPKERNELDIR)" 2594196392Ssimon exit 1 2595196392Ssimon fi 2596196392Ssimon BACKUPKERNELDIR="`echo $BACKUPKERNELDIR | sed -Ee 's/[0-9]\$//'`" 2597196392Ssimon BACKUPKERNELDIR="${BACKUPKERNELDIR}${CNT}" 2598196392Ssimon done 2599196392Ssimon} 2600196392Ssimon 2601196392Ssimon# Backup the current kernel using hardlinks, if not disabled by user. 2602196392Ssimon# Since we delete all files in the directory used for previous backups 2603196392Ssimon# we create a marker file called ".freebsd-update" in the directory so 2604196392Ssimon# we can determine on the next run that the directory was created by 2605196392Ssimon# freebsd-update and we then do not accidentally remove user files in 2606196392Ssimon# the unlikely case that the user has created a directory with a 2607196392Ssimon# conflicting name. 2608196392Ssimonbackup_kernel () { 2609196392Ssimon # Only make kernel backup is so configured. 2610196392Ssimon if [ $BACKUPKERNEL != yes ]; then 2611196392Ssimon return 0 2612196392Ssimon fi 2613196392Ssimon 2614196392Ssimon # Decide which directory name to use for kernel backups. 2615196392Ssimon backup_kernel_finddir 2616196392Ssimon 2617196392Ssimon # Remove old kernel backup files. If $BACKUPKERNELDIR was 2618196392Ssimon # "not ours", backup_kernel_finddir would have exited, so 2619196392Ssimon # deleting the directory content is as safe as we can make it. 2620196392Ssimon if [ -d $BACKUPKERNELDIR ]; then 2621196392Ssimon rm -f $BACKUPKERNELDIR/* 2622196392Ssimon fi 2623196392Ssimon 2624196392Ssimon # Create directory for backup if it doesn't exist. 2625196392Ssimon mkdir -p $BACKUPKERNELDIR 2626196392Ssimon 2627196392Ssimon # Mark the directory as having been created by freebsd-update. 2628196392Ssimon touch $BACKUPKERNELDIR/.freebsd-update 2629196392Ssimon if [ $? -ne 0 ]; then 2630196392Ssimon echo "Could not create kernel backup directory" 2631196392Ssimon exit 1 2632196392Ssimon fi 2633196392Ssimon 2634196392Ssimon # Disable pathname expansion to be sure *.symbols is not 2635196392Ssimon # expanded. 2636196392Ssimon set -f 2637196392Ssimon 2638196392Ssimon # Use find to ignore symbol files, unless disabled by user. 2639196392Ssimon if [ $BACKUPKERNELSYMBOLFILES = yes ]; then 2640196392Ssimon FINDFILTER="" 2641196392Ssimon else 2642196392Ssimon FINDFILTER=-"a ! -name *.symbols" 2643196392Ssimon fi 2644196392Ssimon 2645196392Ssimon # Backup all the kernel files using hardlinks. 2646196392Ssimon find $KERNELDIR -type f $FINDFILTER | \ 2647196392Ssimon sed -Ee "s,($KERNELDIR)/?(.*),\1/\2 ${BACKUPKERNELDIR}/\2," | \ 2648196392Ssimon xargs -n 2 cp -pl 2649196392Ssimon 2650196392Ssimon # Re-enable patchname expansion. 2651196392Ssimon set +f 2652196392Ssimon} 2653196392Ssimon 2654161748Scperciva# Install new files 2655161748Scpercivainstall_from_index () { 2656161748Scperciva # First pass: Do everything apart from setting file flags. We 2657161748Scperciva # can't set flags yet, because schg inhibits hard linking. 2658161748Scperciva sort -k 1,1 -t '|' $1 | 2659161748Scperciva tr '|' ' ' | 2660161748Scperciva while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do 2661161748Scperciva case ${TYPE} in 2662161748Scperciva d) 2663161748Scperciva # Create a directory 2664161748Scperciva install -d -o ${OWNER} -g ${GROUP} \ 2665161748Scperciva -m ${PERM} ${BASEDIR}/${FPATH} 2666161748Scperciva ;; 2667161748Scperciva f) 2668161748Scperciva if [ -z "${LINK}" ]; then 2669161748Scperciva # Create a file, without setting flags. 2670161748Scperciva gunzip < files/${HASH}.gz > ${HASH} 2671161748Scperciva install -S -o ${OWNER} -g ${GROUP} \ 2672161748Scperciva -m ${PERM} ${HASH} ${BASEDIR}/${FPATH} 2673161748Scperciva rm ${HASH} 2674161748Scperciva else 2675161748Scperciva # Create a hard link. 2676169603Scperciva ln -f ${BASEDIR}/${LINK} ${BASEDIR}/${FPATH} 2677161748Scperciva fi 2678161748Scperciva ;; 2679161748Scperciva L) 2680161748Scperciva # Create a symlink 2681161748Scperciva ln -sfh ${HASH} ${BASEDIR}/${FPATH} 2682161748Scperciva ;; 2683161748Scperciva esac 2684161748Scperciva done 2685161748Scperciva 2686161748Scperciva # Perform a second pass, adding file flags. 2687161748Scperciva tr '|' ' ' < $1 | 2688161748Scperciva while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do 2689161748Scperciva if [ ${TYPE} = "f" ] && 2690161748Scperciva ! [ ${FLAGS} = "0" ]; then 2691161748Scperciva chflags ${FLAGS} ${BASEDIR}/${FPATH} 2692161748Scperciva fi 2693161748Scperciva done 2694161748Scperciva} 2695161748Scperciva 2696161748Scperciva# Remove files which we want to delete 2697161748Scpercivainstall_delete () { 2698161748Scperciva # Generate list of new files 2699161748Scperciva cut -f 1 -d '|' < $2 | 2700161748Scperciva sort > newfiles 2701161748Scperciva 2702161748Scperciva # Generate subindex of old files we want to nuke 2703161748Scperciva sort -k 1,1 -t '|' $1 | 2704161748Scperciva join -t '|' -v 1 - newfiles | 2705164600Scperciva sort -r -k 1,1 -t '|' | 2706161748Scperciva cut -f 1,2 -d '|' | 2707161748Scperciva tr '|' ' ' > killfiles 2708161748Scperciva 2709161748Scperciva # Remove the offending bits 2710161748Scperciva while read FPATH TYPE; do 2711161748Scperciva case ${TYPE} in 2712161748Scperciva d) 2713161748Scperciva rmdir ${BASEDIR}/${FPATH} 2714161748Scperciva ;; 2715161748Scperciva f) 2716161748Scperciva rm ${BASEDIR}/${FPATH} 2717161748Scperciva ;; 2718161748Scperciva L) 2719161748Scperciva rm ${BASEDIR}/${FPATH} 2720161748Scperciva ;; 2721161748Scperciva esac 2722161748Scperciva done < killfiles 2723161748Scperciva 2724161748Scperciva # Clean up 2725161748Scperciva rm newfiles killfiles 2726161748Scperciva} 2727161748Scperciva 2728173564Scperciva# Install new files, delete old files, and update linker.hints 2729173564Scpercivainstall_files () { 2730173564Scperciva # If we haven't already dealt with the kernel, deal with it. 2731173564Scperciva if ! [ -f $1/kerneldone ]; then 2732173564Scperciva grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD 2733173564Scperciva grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW 2734173564Scperciva 2735196392Ssimon # Backup current kernel before installing a new one 2736196392Ssimon backup_kernel || return 1 2737196392Ssimon 2738173564Scperciva # Install new files 2739173564Scperciva install_from_index INDEX-NEW || return 1 2740173564Scperciva 2741173564Scperciva # Remove files which need to be deleted 2742173564Scperciva install_delete INDEX-OLD INDEX-NEW || return 1 2743173564Scperciva 2744173564Scperciva # Update linker.hints if necessary 2745173564Scperciva if [ -s INDEX-OLD -o -s INDEX-NEW ]; then 2746173564Scperciva kldxref -R /boot/ 2>/dev/null 2747173564Scperciva fi 2748173564Scperciva 2749173564Scperciva # We've finished updating the kernel. 2750173564Scperciva touch $1/kerneldone 2751173564Scperciva 2752173564Scperciva # Do we need to ask for a reboot now? 2753173564Scperciva if [ -f $1/kernelfirst ] && 2754173564Scperciva [ -s INDEX-OLD -o -s INDEX-NEW ]; then 2755173564Scperciva cat <<-EOF 2756173564Scperciva 2757173564ScpercivaKernel updates have been installed. Please reboot and run 2758173564Scperciva"$0 install" again to finish installing updates. 2759173564Scperciva EOF 2760173564Scperciva exit 0 2761173564Scperciva fi 2762161748Scperciva fi 2763173564Scperciva 2764173564Scperciva # If we haven't already dealt with the world, deal with it. 2765173564Scperciva if ! [ -f $1/worlddone ]; then 2766173564Scperciva # Install new shared libraries next 2767173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2768177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2769173564Scperciva install_from_index INDEX-NEW || return 1 2770173564Scperciva 2771173564Scperciva # Deal with everything else 2772173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2773177601Scperciva grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2774173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2775177601Scperciva grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2776173564Scperciva install_from_index INDEX-NEW || return 1 2777173564Scperciva install_delete INDEX-OLD INDEX-NEW || return 1 2778173564Scperciva 2779173564Scperciva # Rebuild /etc/spwd.db and /etc/pwd.db if necessary. 2780173564Scperciva if [ /etc/master.passwd -nt /etc/spwd.db ] || 2781173564Scperciva [ /etc/master.passwd -nt /etc/pwd.db ]; then 2782173564Scperciva pwd_mkdb /etc/master.passwd 2783173564Scperciva fi 2784173564Scperciva 2785173564Scperciva # Rebuild /etc/login.conf.db if necessary. 2786173564Scperciva if [ /etc/login.conf -nt /etc/login.conf.db ]; then 2787173564Scperciva cap_mkdb /etc/login.conf 2788173564Scperciva fi 2789173564Scperciva 2790173564Scperciva # We've finished installing the world and deleting old files 2791173564Scperciva # which are not shared libraries. 2792173564Scperciva touch $1/worlddone 2793173564Scperciva 2794173564Scperciva # Do we need to ask the user to portupgrade now? 2795173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2796177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2797173564Scperciva cut -f 1 -d '|' | 2798173564Scperciva sort > newfiles 2799173564Scperciva if grep -vE '^/boot/' $1/INDEX-OLD | 2800177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2801173564Scperciva cut -f 1 -d '|' | 2802173564Scperciva sort | 2803173564Scperciva join -v 1 - newfiles | 2804173564Scperciva grep -q .; then 2805173564Scperciva cat <<-EOF 2806173564Scperciva 2807173564ScpercivaCompleting this upgrade requires removing old shared object files. 2808173564ScpercivaPlease rebuild all installed 3rd party software (e.g., programs 2809173564Scpercivainstalled from the ports tree) and then run "$0 install" 2810173564Scpercivaagain to finish installing updates. 2811173564Scperciva EOF 2812173564Scperciva rm newfiles 2813173564Scperciva exit 0 2814173564Scperciva fi 2815173564Scperciva rm newfiles 2816173564Scperciva fi 2817173564Scperciva 2818173564Scperciva # Remove old shared libraries 2819173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2820177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2821173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2822177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2823173564Scperciva install_delete INDEX-OLD INDEX-NEW || return 1 2824173564Scperciva 2825173564Scperciva # Remove temporary files 2826173564Scperciva rm INDEX-OLD INDEX-NEW 2827161748Scperciva} 2828161748Scperciva 2829161748Scperciva# Rearrange bits to allow the installed updates to be rolled back 2830161748Scpercivainstall_setup_rollback () { 2831173564Scperciva # Remove the "reboot after installing kernel", "kernel updated", and 2832173564Scperciva # "finished installing the world" flags if present -- they are 2833173564Scperciva # irrelevant when rolling back updates. 2834173564Scperciva if [ -f ${BDHASH}-install/kernelfirst ]; then 2835173564Scperciva rm ${BDHASH}-install/kernelfirst 2836173564Scperciva rm ${BDHASH}-install/kerneldone 2837173564Scperciva fi 2838173564Scperciva if [ -f ${BDHASH}-install/worlddone ]; then 2839173564Scperciva rm ${BDHASH}-install/worlddone 2840173564Scperciva fi 2841173564Scperciva 2842161748Scperciva if [ -L ${BDHASH}-rollback ]; then 2843161748Scperciva mv ${BDHASH}-rollback ${BDHASH}-install/rollback 2844161748Scperciva fi 2845161748Scperciva 2846161748Scperciva mv ${BDHASH}-install ${BDHASH}-rollback 2847161748Scperciva} 2848161748Scperciva 2849161748Scperciva# Actually install updates 2850161748Scpercivainstall_run () { 2851161748Scperciva echo -n "Installing updates..." 2852161748Scperciva 2853161748Scperciva # Make sure we have all the files we should have 2854161748Scperciva install_verify ${BDHASH}-install/INDEX-OLD \ 2855161748Scperciva ${BDHASH}-install/INDEX-NEW || return 1 2856161748Scperciva 2857161748Scperciva # Remove system immutable flag from files 2858161748Scperciva install_unschg ${BDHASH}-install/INDEX-OLD \ 2859161748Scperciva ${BDHASH}-install/INDEX-NEW || return 1 2860161748Scperciva 2861173564Scperciva # Install new files, delete old files, and update linker.hints 2862173564Scperciva install_files ${BDHASH}-install || return 1 2863161748Scperciva 2864161748Scperciva # Rearrange bits to allow the installed updates to be rolled back 2865161748Scperciva install_setup_rollback 2866161748Scperciva 2867161748Scperciva echo " done." 2868161748Scperciva} 2869161748Scperciva 2870161748Scperciva# Rearrange bits to allow the previous set of updates to be rolled back next. 2871161748Scpercivarollback_setup_rollback () { 2872161748Scperciva if [ -L ${BDHASH}-rollback/rollback ]; then 2873161748Scperciva mv ${BDHASH}-rollback/rollback rollback-tmp 2874161748Scperciva rm -r ${BDHASH}-rollback/ 2875161748Scperciva rm ${BDHASH}-rollback 2876161748Scperciva mv rollback-tmp ${BDHASH}-rollback 2877161748Scperciva else 2878161748Scperciva rm -r ${BDHASH}-rollback/ 2879161748Scperciva rm ${BDHASH}-rollback 2880161748Scperciva fi 2881161748Scperciva} 2882161748Scperciva 2883173564Scperciva# Install old files, delete new files, and update linker.hints 2884173564Scpercivarollback_files () { 2885173671Scperciva # Install old shared library files which don't have the same path as 2886173671Scperciva # a new shared library file. 2887173671Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2888177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2889173671Scperciva cut -f 1 -d '|' | 2890173671Scperciva sort > INDEX-NEW.libs.flist 2891173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2892177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2893173671Scperciva sort -k 1,1 -t '|' - | 2894173671Scperciva join -t '|' -v 1 - INDEX-NEW.libs.flist > INDEX-OLD 2895173564Scperciva install_from_index INDEX-OLD || return 1 2896173564Scperciva 2897173564Scperciva # Deal with files which are neither kernel nor shared library 2898173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2899177601Scperciva grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2900173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2901177601Scperciva grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2902173564Scperciva install_from_index INDEX-OLD || return 1 2903173564Scperciva install_delete INDEX-NEW INDEX-OLD || return 1 2904173564Scperciva 2905173671Scperciva # Install any old shared library files which we didn't install above. 2906173671Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2907177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' | 2908173671Scperciva sort -k 1,1 -t '|' - | 2909173671Scperciva join -t '|' - INDEX-NEW.libs.flist > INDEX-OLD 2910173671Scperciva install_from_index INDEX-OLD || return 1 2911173671Scperciva 2912173564Scperciva # Delete unneeded shared library files 2913173564Scperciva grep -vE '^/boot/' $1/INDEX-OLD | 2914177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2915173564Scperciva grep -vE '^/boot/' $1/INDEX-NEW | 2916177601Scperciva grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2917173564Scperciva install_delete INDEX-NEW INDEX-OLD || return 1 2918173564Scperciva 2919173564Scperciva # Deal with kernel files 2920173564Scperciva grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD 2921173564Scperciva grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW 2922173564Scperciva install_from_index INDEX-OLD || return 1 2923173564Scperciva install_delete INDEX-NEW INDEX-OLD || return 1 2924173564Scperciva if [ -s INDEX-OLD -o -s INDEX-NEW ]; then 2925173564Scperciva kldxref -R /boot/ 2>/dev/null 2926173564Scperciva fi 2927173564Scperciva 2928173564Scperciva # Remove temporary files 2929173672Scperciva rm INDEX-OLD INDEX-NEW INDEX-NEW.libs.flist 2930173564Scperciva} 2931173564Scperciva 2932161748Scperciva# Actually rollback updates 2933161748Scpercivarollback_run () { 2934161748Scperciva echo -n "Uninstalling updates..." 2935161748Scperciva 2936161748Scperciva # If there are updates waiting to be installed, remove them; we 2937161748Scperciva # want the user to re-run 'fetch' after rolling back updates. 2938161748Scperciva if [ -L ${BDHASH}-install ]; then 2939161748Scperciva rm -r ${BDHASH}-install/ 2940161748Scperciva rm ${BDHASH}-install 2941161748Scperciva fi 2942161748Scperciva 2943161748Scperciva # Make sure we have all the files we should have 2944161748Scperciva install_verify ${BDHASH}-rollback/INDEX-NEW \ 2945161748Scperciva ${BDHASH}-rollback/INDEX-OLD || return 1 2946161748Scperciva 2947161748Scperciva # Remove system immutable flag from files 2948161748Scperciva install_unschg ${BDHASH}-rollback/INDEX-NEW \ 2949161748Scperciva ${BDHASH}-rollback/INDEX-OLD || return 1 2950161748Scperciva 2951173564Scperciva # Install old files, delete new files, and update linker.hints 2952173564Scperciva rollback_files ${BDHASH}-rollback || return 1 2953161748Scperciva 2954161748Scperciva # Remove the rollback directory and the symlink pointing to it; and 2955161748Scperciva # rearrange bits to allow the previous set of updates to be rolled 2956161748Scperciva # back next. 2957161748Scperciva rollback_setup_rollback 2958161748Scperciva 2959161748Scperciva echo " done." 2960161748Scperciva} 2961161748Scperciva 2962181142Scperciva# Compare INDEX-ALL and INDEX-PRESENT and print warnings about differences. 2963181142ScpercivaIDS_compare () { 2964181425Scperciva # Get all the lines which mismatch in something other than file 2965181425Scperciva # flags. We ignore file flags because sysinstall doesn't seem to 2966181425Scperciva # set them when it installs FreeBSD; warning about these adds a 2967181425Scperciva # very large amount of noise. 2968181425Scperciva cut -f 1-5,7-8 -d '|' $1 > $1.noflags 2969181425Scperciva sort -k 1,1 -t '|' $1.noflags > $1.sorted 2970181425Scperciva cut -f 1-5,7-8 -d '|' $2 | 2971181425Scperciva comm -13 $1.noflags - | 2972181425Scperciva fgrep -v '|-|||||' | 2973181142Scperciva sort -k 1,1 -t '|' | 2974181142Scperciva join -t '|' $1.sorted - > INDEX-NOTMATCHING 2975181142Scperciva 2976181142Scperciva # Ignore files which match IDSIGNOREPATHS. 2977181142Scperciva for X in ${IDSIGNOREPATHS}; do 2978181142Scperciva grep -E "^${X}" INDEX-NOTMATCHING 2979181142Scperciva done | 2980181142Scperciva sort -u | 2981181142Scperciva comm -13 - INDEX-NOTMATCHING > INDEX-NOTMATCHING.tmp 2982181142Scperciva mv INDEX-NOTMATCHING.tmp INDEX-NOTMATCHING 2983181142Scperciva 2984181142Scperciva # Go through the lines and print warnings. 2985181142Scperciva while read LINE; do 2986181142Scperciva FPATH=`echo "${LINE}" | cut -f 1 -d '|'` 2987181142Scperciva TYPE=`echo "${LINE}" | cut -f 2 -d '|'` 2988181142Scperciva OWNER=`echo "${LINE}" | cut -f 3 -d '|'` 2989181142Scperciva GROUP=`echo "${LINE}" | cut -f 4 -d '|'` 2990181142Scperciva PERM=`echo "${LINE}" | cut -f 5 -d '|'` 2991181425Scperciva HASH=`echo "${LINE}" | cut -f 6 -d '|'` 2992181425Scperciva LINK=`echo "${LINE}" | cut -f 7 -d '|'` 2993181425Scperciva P_TYPE=`echo "${LINE}" | cut -f 8 -d '|'` 2994181425Scperciva P_OWNER=`echo "${LINE}" | cut -f 9 -d '|'` 2995181425Scperciva P_GROUP=`echo "${LINE}" | cut -f 10 -d '|'` 2996181425Scperciva P_PERM=`echo "${LINE}" | cut -f 11 -d '|'` 2997181425Scperciva P_HASH=`echo "${LINE}" | cut -f 12 -d '|'` 2998181425Scperciva P_LINK=`echo "${LINE}" | cut -f 13 -d '|'` 2999181142Scperciva 3000181142Scperciva # Warn about different object types. 3001181142Scperciva if ! [ "${TYPE}" = "${P_TYPE}" ]; then 3002181142Scperciva echo -n "${FPATH} is a " 3003181142Scperciva case "${P_TYPE}" in 3004181142Scperciva f) echo -n "regular file, " 3005181142Scperciva ;; 3006181142Scperciva d) echo -n "directory, " 3007181142Scperciva ;; 3008181142Scperciva L) echo -n "symlink, " 3009181142Scperciva ;; 3010181142Scperciva esac 3011181142Scperciva echo -n "but should be a " 3012181142Scperciva case "${TYPE}" in 3013181142Scperciva f) echo -n "regular file." 3014181142Scperciva ;; 3015181142Scperciva d) echo -n "directory." 3016181142Scperciva ;; 3017181142Scperciva L) echo -n "symlink." 3018181142Scperciva ;; 3019181142Scperciva esac 3020181142Scperciva echo 3021181142Scperciva 3022181142Scperciva # Skip other tests, since they don't make sense if 3023181142Scperciva # we're comparing different object types. 3024181142Scperciva continue 3025181142Scperciva fi 3026181142Scperciva 3027181142Scperciva # Warn about different owners. 3028181142Scperciva if ! [ "${OWNER}" = "${P_OWNER}" ]; then 3029181142Scperciva echo -n "${FPATH} is owned by user id ${P_OWNER}, " 3030181142Scperciva echo "but should be owned by user id ${OWNER}." 3031181142Scperciva fi 3032181142Scperciva 3033181142Scperciva # Warn about different groups. 3034181142Scperciva if ! [ "${GROUP}" = "${P_GROUP}" ]; then 3035181142Scperciva echo -n "${FPATH} is owned by group id ${P_GROUP}, " 3036181142Scperciva echo "but should be owned by group id ${GROUP}." 3037181142Scperciva fi 3038181142Scperciva 3039181142Scperciva # Warn about different permissions. We do not warn about 3040181142Scperciva # different permissions on symlinks, since some archivers 3041181142Scperciva # don't extract symlink permissions correctly and they are 3042181142Scperciva # ignored anyway. 3043181142Scperciva if ! [ "${PERM}" = "${P_PERM}" ] && 3044181142Scperciva ! [ "${TYPE}" = "L" ]; then 3045181142Scperciva echo -n "${FPATH} has ${P_PERM} permissions, " 3046181142Scperciva echo "but should have ${PERM} permissions." 3047181142Scperciva fi 3048181142Scperciva 3049181142Scperciva # Warn about different file hashes / symlink destinations. 3050181142Scperciva if ! [ "${HASH}" = "${P_HASH}" ]; then 3051181142Scperciva if [ "${TYPE}" = "L" ]; then 3052181142Scperciva echo -n "${FPATH} is a symlink to ${P_HASH}, " 3053181142Scperciva echo "but should be a symlink to ${HASH}." 3054181142Scperciva fi 3055181142Scperciva if [ "${TYPE}" = "f" ]; then 3056181142Scperciva echo -n "${FPATH} has SHA256 hash ${P_HASH}, " 3057181142Scperciva echo "but should have SHA256 hash ${HASH}." 3058181142Scperciva fi 3059181142Scperciva fi 3060181142Scperciva 3061181142Scperciva # We don't warn about different hard links, since some 3062181142Scperciva # some archivers break hard links, and as long as the 3063181142Scperciva # underlying data is correct they really don't matter. 3064181142Scperciva done < INDEX-NOTMATCHING 3065181142Scperciva 3066181142Scperciva # Clean up 3067181425Scperciva rm $1 $1.noflags $1.sorted $2 INDEX-NOTMATCHING 3068181142Scperciva} 3069181142Scperciva 3070181142Scperciva# Do the work involved in comparing the system to a "known good" index 3071181142ScpercivaIDS_run () { 3072181142Scperciva workdir_init || return 1 3073181142Scperciva 3074181142Scperciva # Prepare the mirror list. 3075181142Scperciva fetch_pick_server_init && fetch_pick_server 3076181142Scperciva 3077181142Scperciva # Try to fetch the public key until we run out of servers. 3078181142Scperciva while ! fetch_key; do 3079181142Scperciva fetch_pick_server || return 1 3080181142Scperciva done 3081181142Scperciva 3082181142Scperciva # Try to fetch the metadata index signature ("tag") until we run 3083181142Scperciva # out of available servers; and sanity check the downloaded tag. 3084181142Scperciva while ! fetch_tag; do 3085181142Scperciva fetch_pick_server || return 1 3086181142Scperciva done 3087181142Scperciva fetch_tagsanity || return 1 3088181142Scperciva 3089181142Scperciva # Fetch INDEX-OLD and INDEX-ALL. 3090181142Scperciva fetch_metadata INDEX-OLD INDEX-ALL || return 1 3091181142Scperciva 3092181142Scperciva # Generate filtered INDEX-OLD and INDEX-ALL files containing only 3093181142Scperciva # the components we want and without anything marked as "Ignore". 3094181142Scperciva fetch_filter_metadata INDEX-OLD || return 1 3095181142Scperciva fetch_filter_metadata INDEX-ALL || return 1 3096181142Scperciva 3097181142Scperciva # Merge the INDEX-OLD and INDEX-ALL files into INDEX-ALL. 3098181142Scperciva sort INDEX-OLD INDEX-ALL > INDEX-ALL.tmp 3099181142Scperciva mv INDEX-ALL.tmp INDEX-ALL 3100181142Scperciva rm INDEX-OLD 3101181142Scperciva 3102181142Scperciva # Translate /boot/${KERNCONF} to ${KERNELDIR} 3103181142Scperciva fetch_filter_kernel_names INDEX-ALL ${KERNCONF} 3104181142Scperciva 3105181142Scperciva # Inspect the system and generate an INDEX-PRESENT file. 3106181142Scperciva fetch_inspect_system INDEX-ALL INDEX-PRESENT /dev/null || return 1 3107181142Scperciva 3108181142Scperciva # Compare INDEX-ALL and INDEX-PRESENT and print warnings about any 3109181142Scperciva # differences. 3110181142Scperciva IDS_compare INDEX-ALL INDEX-PRESENT 3111181142Scperciva} 3112181142Scperciva 3113161748Scperciva#### Main functions -- call parameter-handling and core functions 3114161748Scperciva 3115161748Scperciva# Using the command line, configuration file, and defaults, 3116161748Scperciva# set all the parameters which are needed later. 3117161748Scpercivaget_params () { 3118161748Scperciva init_params 3119161748Scperciva parse_cmdline $@ 3120161748Scperciva parse_conffile 3121161748Scperciva default_params 3122161748Scperciva} 3123161748Scperciva 3124161748Scperciva# Fetch command. Make sure that we're being called 3125161748Scperciva# interactively, then run fetch_check_params and fetch_run 3126161748Scpercivacmd_fetch () { 3127161748Scperciva if [ ! -t 0 ]; then 3128161748Scperciva echo -n "`basename $0` fetch should not " 3129161748Scperciva echo "be run non-interactively." 3130161748Scperciva echo "Run `basename $0` cron instead." 3131161748Scperciva exit 1 3132161748Scperciva fi 3133161748Scperciva fetch_check_params 3134161748Scperciva fetch_run || exit 1 3135161748Scperciva} 3136161748Scperciva 3137161748Scperciva# Cron command. Make sure the parameters are sensible; wait 3138161748Scperciva# rand(3600) seconds; then fetch updates. While fetching updates, 3139161748Scperciva# send output to a temporary file; only print that file if the 3140161748Scperciva# fetching failed. 3141161748Scpercivacmd_cron () { 3142161748Scperciva fetch_check_params 3143161748Scperciva sleep `jot -r 1 0 3600` 3144161748Scperciva 3145161748Scperciva TMPFILE=`mktemp /tmp/freebsd-update.XXXXXX` || exit 1 3146161748Scperciva if ! fetch_run >> ${TMPFILE} || 3147161748Scperciva ! grep -q "No updates needed" ${TMPFILE} || 3148161748Scperciva [ ${VERBOSELEVEL} = "debug" ]; then 3149161748Scperciva mail -s "`hostname` security updates" ${MAILTO} < ${TMPFILE} 3150161748Scperciva fi 3151161748Scperciva 3152161748Scperciva rm ${TMPFILE} 3153161748Scperciva} 3154161748Scperciva 3155173564Scperciva# Fetch files for upgrading to a new release. 3156173564Scpercivacmd_upgrade () { 3157173564Scperciva upgrade_check_params 3158173564Scperciva upgrade_run || exit 1 3159173564Scperciva} 3160173564Scperciva 3161161748Scperciva# Install downloaded updates. 3162161748Scpercivacmd_install () { 3163161748Scperciva install_check_params 3164161748Scperciva install_run || exit 1 3165161748Scperciva} 3166161748Scperciva 3167161748Scperciva# Rollback most recently installed updates. 3168161748Scpercivacmd_rollback () { 3169161748Scperciva rollback_check_params 3170161748Scperciva rollback_run || exit 1 3171161748Scperciva} 3172161748Scperciva 3173181142Scperciva# Compare system against a "known good" index. 3174181142Scpercivacmd_IDS () { 3175181142Scperciva IDS_check_params 3176181142Scperciva IDS_run || exit 1 3177181142Scperciva} 3178181142Scperciva 3179161748Scperciva#### Entry point 3180161748Scperciva 3181161748Scperciva# Make sure we find utilities from the base system 3182161748Scpercivaexport PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH} 3183161748Scperciva 3184163564Scperciva# Set LC_ALL in order to avoid problems with character ranges like [A-Z]. 3185163564Scpercivaexport LC_ALL=C 3186163564Scperciva 3187161748Scpercivaget_params $@ 3188161748Scpercivafor COMMAND in ${COMMANDS}; do 3189161748Scperciva cmd_${COMMAND} 3190161748Scpercivadone 3191