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