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. 575fetch_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 upgrades. 659upgrade_check_params () { 660 fetch_check_params 661 662 # Unless set otherwise, we're upgrading to the same kernel config. 663 NKERNCONF=${KERNCONF} 664 665 # We need TARGETRELEASE set 666 _TARGETRELEASE_z="Release target must be specified via -r option." 667 if [ -z "${TARGETRELEASE}" ]; then 668 echo -n "`basename $0`: " 669 echo "${_TARGETRELEASE_z}" 670 exit 1 671 fi 672 673 # The target release should be != the current release. 674 if [ "${TARGETRELEASE}" = "${RELNUM}" ]; then 675 echo -n "`basename $0`: " 676 echo "Cannot upgrade from ${RELNUM} to itself" 677 exit 1 678 fi 679 680 # Turning off AllowAdd or AllowDelete is a bad idea for upgrades. 681 if [ "${ALLOWADD}" = "no" ]; then 682 echo -n "`basename $0`: " 683 echo -n "WARNING: \"AllowAdd no\" is a bad idea " 684 echo "when upgrading between releases." 685 echo 686 fi 687 if [ "${ALLOWDELETE}" = "no" ]; then 688 echo -n "`basename $0`: " 689 echo -n "WARNING: \"AllowDelete no\" is a bad idea " 690 echo "when upgrading between releases." 691 echo 692 fi 693 694 # Set EDITOR to /usr/bin/vi if it isn't already set 695 : ${EDITOR:='/usr/bin/vi'} 696} 697 698# Perform sanity checks and set some final parameters in 699# preparation for installing updates. 700install_check_params () { 701 # Check that we are root. All sorts of things won't work otherwise. 702 if [ `id -u` != 0 ]; then 703 echo "You must be root to run this." 704 exit 1 705 fi 706 707 # Check that securelevel <= 0. Otherwise we can't update schg files. 708 if [ `sysctl -n kern.securelevel` -gt 0 ]; then 709 echo "Updates cannot be installed when the system securelevel" 710 echo "is greater than zero." 711 exit 1 712 fi 713 714 # Check that we have a working directory 715 _WORKDIR_bad="Directory does not exist or is not writable: " 716 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 717 echo -n "`basename $0`: " 718 echo -n "${_WORKDIR_bad}" 719 echo ${WORKDIR} 720 exit 1 721 fi 722 cd ${WORKDIR} || exit 1 723 724 # Construct a unique name from ${BASEDIR} 725 BDHASH=`echo ${BASEDIR} | sha256 -q` 726 727 # Check that we have updates ready to install 728 if ! [ -L ${BDHASH}-install ]; then 729 echo "No updates are available to install." 730 echo "Run '$0 fetch' first." 731 exit 1 732 fi 733 if ! [ -f ${BDHASH}-install/INDEX-OLD ] || 734 ! [ -f ${BDHASH}-install/INDEX-NEW ]; then 735 echo "Update manifest is corrupt -- this should never happen." 736 echo "Re-run '$0 fetch'." 737 exit 1 738 fi 739 740 # Figure out what directory contains the running kernel 741 BOOTFILE=`sysctl -n kern.bootfile` 742 KERNELDIR=${BOOTFILE%/kernel} 743 if ! [ -d ${KERNELDIR} ]; then 744 echo "Cannot identify running kernel" 745 exit 1 746 fi 747} 748 749# Perform sanity checks and set some final parameters in 750# preparation for UNinstalling updates. 751rollback_check_params () { 752 # Check that we are root. All sorts of things won't work otherwise. 753 if [ `id -u` != 0 ]; then 754 echo "You must be root to run this." 755 exit 1 756 fi 757 758 # Check that we have a working directory 759 _WORKDIR_bad="Directory does not exist or is not writable: " 760 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 761 echo -n "`basename $0`: " 762 echo -n "${_WORKDIR_bad}" 763 echo ${WORKDIR} 764 exit 1 765 fi 766 cd ${WORKDIR} || exit 1 767 768 # Construct a unique name from ${BASEDIR} 769 BDHASH=`echo ${BASEDIR} | sha256 -q` 770 771 # Check that we have updates ready to rollback 772 if ! [ -L ${BDHASH}-rollback ]; then 773 echo "No rollback directory found." 774 exit 1 775 fi 776 if ! [ -f ${BDHASH}-rollback/INDEX-OLD ] || 777 ! [ -f ${BDHASH}-rollback/INDEX-NEW ]; then 778 echo "Update manifest is corrupt -- this should never happen." 779 exit 1 780 fi 781} 782 783# Perform sanity checks and set some final parameters 784# in preparation for comparing the system against the 785# published index. Figure out which index we should 786# compare against: If the user is running *-p[0-9]+, 787# strip off the last part; if the user is running 788# -SECURITY, call it -RELEASE. Chdir into the working 789# directory. 790IDS_check_params () { 791 export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)" 792 793 _SERVERNAME_z=\ 794"SERVERNAME must be given via command line or configuration file." 795 _KEYPRINT_z="Key must be given via -k option or configuration file." 796 _KEYPRINT_bad="Invalid key fingerprint: " 797 _WORKDIR_bad="Directory does not exist or is not writable: " 798 799 if [ -z "${SERVERNAME}" ]; then 800 echo -n "`basename $0`: " 801 echo "${_SERVERNAME_z}" 802 exit 1 803 fi 804 if [ -z "${KEYPRINT}" ]; then 805 echo -n "`basename $0`: " 806 echo "${_KEYPRINT_z}" 807 exit 1 808 fi 809 if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then 810 echo -n "`basename $0`: " 811 echo -n "${_KEYPRINT_bad}" 812 echo ${KEYPRINT} 813 exit 1 814 fi 815 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then 816 echo -n "`basename $0`: " 817 echo -n "${_WORKDIR_bad}" 818 echo ${WORKDIR} 819 exit 1 820 fi 821 cd ${WORKDIR} || exit 1 822 823 # Generate release number. The s/SECURITY/RELEASE/ bit exists 824 # to provide an upgrade path for FreeBSD Update 1.x users, since 825 # the kernels provided by FreeBSD Update 1.x are always labelled 826 # as X.Y-SECURITY. 827 RELNUM=`uname -r | 828 sed -E 's,-p[0-9]+,,' | 829 sed -E 's,-SECURITY,-RELEASE,'` 830 ARCH=`uname -m` 831 FETCHDIR=${RELNUM}/${ARCH} 832 PATCHDIR=${RELNUM}/${ARCH}/bp 833 834 # Figure out what directory contains the running kernel 835 BOOTFILE=`sysctl -n kern.bootfile` 836 KERNELDIR=${BOOTFILE%/kernel} 837 if ! [ -d ${KERNELDIR} ]; then 838 echo "Cannot identify running kernel" 839 exit 1 840 fi 841 842 # Figure out what kernel configuration is running. We start with 843 # the output of `uname -i`, and then make the following adjustments: 844 # 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config 845 # file says "ident SMP-GENERIC", I don't know... 846 # 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64" 847 # _and_ `sysctl kern.version` contains a line which ends "/SMP", then 848 # we're running an SMP kernel. This mis-identification is a bug 849 # which was fixed in 6.2-STABLE. 850 KERNCONF=`uname -i` 851 if [ ${KERNCONF} = "SMP-GENERIC" ]; then 852 KERNCONF=SMP 853 fi 854 if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then 855 if sysctl kern.version | grep -qE '/SMP$'; then 856 KERNCONF=SMP 857 fi 858 fi 859 860 # Define some paths 861 SHA256=/sbin/sha256 862 PHTTPGET=/usr/libexec/phttpget 863 864 # Set up variables relating to VERBOSELEVEL 865 fetch_setup_verboselevel 866} 867 868#### Core functionality -- the actual work gets done here 869 870# Use an SRV query to pick a server. If the SRV query doesn't provide 871# a useful answer, use the server name specified by the user. 872# Put another way... look up _http._tcp.${SERVERNAME} and pick a server 873# from that; or if no servers are returned, use ${SERVERNAME}. 874# This allows a user to specify "portsnap.freebsd.org" (in which case 875# portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org" 876# (in which case portsnap will use that particular server, since there 877# won't be an SRV entry for that name). 878# 879# We ignore the Port field, since we are always going to use port 80. 880 881# Fetch the mirror list, but do not pick a mirror yet. Returns 1 if 882# no mirrors are available for any reason. 883fetch_pick_server_init () { 884 : > serverlist_tried 885 886# Check that host(1) exists (i.e., that the system wasn't built with the 887# WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist. 888 if ! which -s host; then 889 : > serverlist_full 890 return 1 891 fi 892 893 echo -n "Looking up ${SERVERNAME} mirrors... " 894 895# Issue the SRV query and pull out the Priority, Weight, and Target fields. 896# BIND 9 prints "$name has SRV record ..." while BIND 8 prints 897# "$name server selection ..."; we allow either format. 898 MLIST="_http._tcp.${SERVERNAME}" 899 host -t srv "${MLIST}" | 900 sed -nE "s/${MLIST} (has SRV record|server selection) //p" | 901 cut -f 1,2,4 -d ' ' | 902 sed -e 's/\.$//' | 903 sort > serverlist_full 904 905# If no records, give up -- we'll just use the server name we were given. 906 if [ `wc -l < serverlist_full` -eq 0 ]; then 907 echo "none found." 908 return 1 909 fi 910 911# Report how many mirrors we found. 912 echo `wc -l < serverlist_full` "mirrors found." 913 914# Generate a random seed for use in picking mirrors. If HTTP_PROXY 915# is set, this will be used to generate the seed; otherwise, the seed 916# will be random. 917 if [ -n "${HTTP_PROXY}${http_proxy}" ]; then 918 RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" | 919 tr -d 'a-f' | 920 cut -c 1-9` 921 else 922 RANDVALUE=`jot -r 1 0 999999999` 923 fi 924} 925 926# Pick a mirror. Returns 1 if we have run out of mirrors to try. 927fetch_pick_server () { 928# Generate a list of not-yet-tried mirrors 929 sort serverlist_tried | 930 comm -23 serverlist_full - > serverlist 931 932# Have we run out of mirrors? 933 if [ `wc -l < serverlist` -eq 0 ]; then 934 echo "No mirrors remaining, giving up." 935 return 1 936 fi 937 938# Find the highest priority level (lowest numeric value). 939 SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1` 940 941# Add up the weights of the response lines at that priority level. 942 SRV_WSUM=0; 943 while read X; do 944 case "$X" in 945 ${SRV_PRIORITY}\ *) 946 SRV_W=`echo $X | cut -f 2 -d ' '` 947 SRV_WSUM=$(($SRV_WSUM + $SRV_W)) 948 ;; 949 esac 950 done < serverlist 951 952# If all the weights are 0, pretend that they are all 1 instead. 953 if [ ${SRV_WSUM} -eq 0 ]; then 954 SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l` 955 SRV_W_ADD=1 956 else 957 SRV_W_ADD=0 958 fi 959 960# Pick a value between 0 and the sum of the weights - 1 961 SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}` 962 963# Read through the list of mirrors and set SERVERNAME. Write the line 964# corresponding to the mirror we selected into serverlist_tried so that 965# we won't try it again. 966 while read X; do 967 case "$X" in 968 ${SRV_PRIORITY}\ *) 969 SRV_W=`echo $X | cut -f 2 -d ' '` 970 SRV_W=$(($SRV_W + $SRV_W_ADD)) 971 if [ $SRV_RND -lt $SRV_W ]; then 972 SERVERNAME=`echo $X | cut -f 3 -d ' '` 973 echo "$X" >> serverlist_tried 974 break 975 else 976 SRV_RND=$(($SRV_RND - $SRV_W)) 977 fi 978 ;; 979 esac 980 done < serverlist 981} 982 983# Take a list of ${oldhash}|${newhash} and output a list of needed patches, 984# i.e., those for which we have ${oldhash} and don't have ${newhash}. 985fetch_make_patchlist () { 986 grep -vE "^([0-9a-f]{64})\|\1$" | 987 tr '|' ' ' | 988 while read X Y; do 989 if [ -f "files/${Y}.gz" ] || 990 [ ! -f "files/${X}.gz" ]; then 991 continue 992 fi 993 echo "${X}|${Y}" 994 done | uniq 995} 996 997# Print user-friendly progress statistics 998fetch_progress () { 999 LNC=0 1000 while read x; do 1001 LNC=$(($LNC + 1)) 1002 if [ $(($LNC % 10)) = 0 ]; then 1003 echo -n $LNC 1004 elif [ $(($LNC % 2)) = 0 ]; then 1005 echo -n . 1006 fi 1007 done 1008 echo -n " " 1009} 1010 1011# Function for asking the user if everything is ok 1012continuep () { 1013 while read -p "Does this look reasonable (y/n)? " CONTINUE; do 1014 case "${CONTINUE}" in 1015 y*) 1016 return 0 1017 ;; 1018 n*) 1019 return 1 1020 ;; 1021 esac 1022 done 1023} 1024 1025# Initialize the working directory 1026workdir_init () { 1027 mkdir -p files 1028 touch tINDEX.present 1029} 1030 1031# Check that we have a public key with an appropriate hash, or 1032# fetch the key if it doesn't exist. Returns 1 if the key has 1033# not yet been fetched. 1034fetch_key () { 1035 if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then 1036 return 0 1037 fi 1038 1039 echo -n "Fetching public key from ${SERVERNAME}... " 1040 rm -f pub.ssl 1041 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/pub.ssl \ 1042 2>${QUIETREDIR} || true 1043 if ! [ -r pub.ssl ]; then 1044 echo "failed." 1045 return 1 1046 fi 1047 if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then 1048 echo "key has incorrect hash." 1049 rm -f pub.ssl 1050 return 1 1051 fi 1052 echo "done." 1053} 1054 1055# Fetch metadata signature, aka "tag". 1056fetch_tag () { 1057 echo -n "Fetching metadata signature " 1058 echo ${NDEBUG} "for ${RELNUM} from ${SERVERNAME}... " 1059 rm -f latest.ssl 1060 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/latest.ssl \ 1061 2>${QUIETREDIR} || true 1062 if ! [ -r latest.ssl ]; then 1063 echo "failed." 1064 return 1 1065 fi 1066 1067 openssl rsautl -pubin -inkey pub.ssl -verify \ 1068 < latest.ssl > tag.new 2>${QUIETREDIR} || true 1069 rm latest.ssl 1070 1071 if ! [ `wc -l < tag.new` = 1 ] || 1072 ! grep -qE \ 1073 "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \ 1074 tag.new; then 1075 echo "invalid signature." 1076 return 1 1077 fi 1078 1079 echo "done." 1080 1081 RELPATCHNUM=`cut -f 4 -d '|' < tag.new` 1082 TINDEXHASH=`cut -f 5 -d '|' < tag.new` 1083 EOLTIME=`cut -f 6 -d '|' < tag.new` 1084} 1085 1086# Sanity-check the patch number in a tag, to make sure that we're not 1087# going to "update" backwards and to prevent replay attacks. 1088fetch_tagsanity () { 1089 # Check that we're not going to move from -pX to -pY with Y < X. 1090 RELPX=`uname -r | sed -E 's,.*-,,'` 1091 if echo ${RELPX} | grep -qE '^p[0-9]+$'; then 1092 RELPX=`echo ${RELPX} | cut -c 2-` 1093 else 1094 RELPX=0 1095 fi 1096 if [ "${RELPATCHNUM}" -lt "${RELPX}" ]; then 1097 echo 1098 echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})" 1099 echo " appear older than what" 1100 echo "we are currently running (`uname -r`)!" 1101 echo "Cowardly refusing to proceed any further." 1102 return 1 1103 fi 1104 1105 # If "tag" exists and corresponds to ${RELNUM}, make sure that 1106 # it contains a patch number <= RELPATCHNUM, in order to protect 1107 # against rollback (replay) attacks. 1108 if [ -f tag ] && 1109 grep -qE \ 1110 "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \ 1111 tag; then 1112 LASTRELPATCHNUM=`cut -f 4 -d '|' < tag` 1113 1114 if [ "${RELPATCHNUM}" -lt "${LASTRELPATCHNUM}" ]; then 1115 echo 1116 echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})" 1117 echo " are older than the" 1118 echo -n "most recently seen updates" 1119 echo " (${RELNUM}-p${LASTRELPATCHNUM})." 1120 echo "Cowardly refusing to proceed any further." 1121 return 1 1122 fi 1123 fi 1124} 1125 1126# Fetch metadata index file 1127fetch_metadata_index () { 1128 echo ${NDEBUG} "Fetching metadata index... " 1129 rm -f ${TINDEXHASH} 1130 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/t/${TINDEXHASH} 1131 2>${QUIETREDIR} 1132 if ! [ -f ${TINDEXHASH} ]; then 1133 echo "failed." 1134 return 1 1135 fi 1136 if [ `${SHA256} -q ${TINDEXHASH}` != ${TINDEXHASH} ]; then 1137 echo "update metadata index corrupt." 1138 return 1 1139 fi 1140 echo "done." 1141} 1142 1143# Print an error message about signed metadata being bogus. 1144fetch_metadata_bogus () { 1145 echo 1146 echo "The update metadata$1 is correctly signed, but" 1147 echo "failed an integrity check." 1148 echo "Cowardly refusing to proceed any further." 1149 return 1 1150} 1151 1152# Construct tINDEX.new by merging the lines named in $1 from ${TINDEXHASH} 1153# with the lines not named in $@ from tINDEX.present (if that file exists). 1154fetch_metadata_index_merge () { 1155 for METAFILE in $@; do 1156 if [ `grep -E "^${METAFILE}\|" ${TINDEXHASH} | wc -l` \ 1157 -ne 1 ]; then 1158 fetch_metadata_bogus " index" 1159 return 1 1160 fi 1161 1162 grep -E "${METAFILE}\|" ${TINDEXHASH} 1163 done | 1164 sort > tINDEX.wanted 1165 1166 if [ -f tINDEX.present ]; then 1167 join -t '|' -v 2 tINDEX.wanted tINDEX.present | 1168 sort -m - tINDEX.wanted > tINDEX.new 1169 rm tINDEX.wanted 1170 else 1171 mv tINDEX.wanted tINDEX.new 1172 fi 1173} 1174 1175# Sanity check all the lines of tINDEX.new. Even if more metadata lines 1176# are added by future versions of the server, this won't cause problems, 1177# since the only lines which appear in tINDEX.new are the ones which we 1178# specifically grepped out of ${TINDEXHASH}. 1179fetch_metadata_index_sanity () { 1180 if grep -qvE '^[0-9A-Z.-]+\|[0-9a-f]{64}$' tINDEX.new; then 1181 fetch_metadata_bogus " index" 1182 return 1 1183 fi 1184} 1185 1186# Sanity check the metadata file $1. 1187fetch_metadata_sanity () { 1188 # Some aliases to save space later: ${P} is a character which can 1189 # appear in a path; ${M} is the four numeric metadata fields; and 1190 # ${H} is a sha256 hash. 1191 P="[-+./:=_[[:alnum:]]" 1192 M="[0-9]+\|[0-9]+\|[0-9]+\|[0-9]+" 1193 H="[0-9a-f]{64}" 1194 1195 # Check that the first four fields make sense. 1196 if gunzip -c < files/$1.gz | 1197 grep -qvE "^[a-z]+\|[0-9a-z]+\|${P}+\|[fdL-]\|"; then 1198 fetch_metadata_bogus "" 1199 return 1 1200 fi 1201 1202 # Remove the first three fields. 1203 gunzip -c < files/$1.gz | 1204 cut -f 4- -d '|' > sanitycheck.tmp 1205 1206 # Sanity check entries with type 'f' 1207 if grep -E '^f' sanitycheck.tmp | 1208 grep -qvE "^f\|${M}\|${H}\|${P}*\$"; then 1209 fetch_metadata_bogus "" 1210 return 1 1211 fi 1212 1213 # Sanity check entries with type 'd' 1214 if grep -E '^d' sanitycheck.tmp | 1215 grep -qvE "^d\|${M}\|\|\$"; then 1216 fetch_metadata_bogus "" 1217 return 1 1218 fi 1219 1220 # Sanity check entries with type 'L' 1221 if grep -E '^L' sanitycheck.tmp | 1222 grep -qvE "^L\|${M}\|${P}*\|\$"; then 1223 fetch_metadata_bogus "" 1224 return 1 1225 fi 1226 1227 # Sanity check entries with type '-' 1228 if grep -E '^-' sanitycheck.tmp | 1229 grep -qvE "^-\|\|\|\|\|\|"; then 1230 fetch_metadata_bogus "" 1231 return 1 1232 fi 1233 1234 # Clean up 1235 rm sanitycheck.tmp 1236} 1237 1238# Fetch the metadata index and metadata files listed in $@, 1239# taking advantage of metadata patches where possible. 1240fetch_metadata () { 1241 fetch_metadata_index || return 1 1242 fetch_metadata_index_merge $@ || return 1 1243 fetch_metadata_index_sanity || return 1 1244 1245 # Generate a list of wanted metadata patches 1246 join -t '|' -o 1.2,2.2 tINDEX.present tINDEX.new | 1247 fetch_make_patchlist > patchlist 1248 1249 if [ -s patchlist ]; then 1250 # Attempt to fetch metadata patches 1251 echo -n "Fetching `wc -l < patchlist | tr -d ' '` " 1252 echo ${NDEBUG} "metadata patches.${DDSTATS}" 1253 tr '|' '-' < patchlist | 1254 lam -s "${FETCHDIR}/tp/" - -s ".gz" | 1255 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1256 2>${STATSREDIR} | fetch_progress 1257 echo "done." 1258 1259 # Attempt to apply metadata patches 1260 echo -n "Applying metadata patches... " 1261 tr '|' ' ' < patchlist | 1262 while read X Y; do 1263 if [ ! -f "${X}-${Y}.gz" ]; then continue; fi 1264 gunzip -c < ${X}-${Y}.gz > diff 1265 gunzip -c < files/${X}.gz > diff-OLD 1266 1267 # Figure out which lines are being added and removed 1268 grep -E '^-' diff | 1269 cut -c 2- | 1270 while read PREFIX; do 1271 look "${PREFIX}" diff-OLD 1272 done | 1273 sort > diff-rm 1274 grep -E '^\+' diff | 1275 cut -c 2- > diff-add 1276 1277 # Generate the new file 1278 comm -23 diff-OLD diff-rm | 1279 sort - diff-add > diff-NEW 1280 1281 if [ `${SHA256} -q diff-NEW` = ${Y} ]; then 1282 mv diff-NEW files/${Y} 1283 gzip -n files/${Y} 1284 else 1285 mv diff-NEW ${Y}.bad 1286 fi 1287 rm -f ${X}-${Y}.gz diff 1288 rm -f diff-OLD diff-NEW diff-add diff-rm 1289 done 2>${QUIETREDIR} 1290 echo "done." 1291 fi 1292 1293 # Update metadata without patches 1294 cut -f 2 -d '|' < tINDEX.new | 1295 while read Y; do 1296 if [ ! -f "files/${Y}.gz" ]; then 1297 echo ${Y}; 1298 fi 1299 done | 1300 sort -u > filelist 1301 1302 if [ -s filelist ]; then 1303 echo -n "Fetching `wc -l < filelist | tr -d ' '` " 1304 echo ${NDEBUG} "metadata files... " 1305 lam -s "${FETCHDIR}/m/" - -s ".gz" < filelist | 1306 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1307 2>${QUIETREDIR} 1308 1309 while read Y; do 1310 if ! [ -f ${Y}.gz ]; then 1311 echo "failed." 1312 return 1 1313 fi 1314 if [ `gunzip -c < ${Y}.gz | 1315 ${SHA256} -q` = ${Y} ]; then 1316 mv ${Y}.gz files/${Y}.gz 1317 else 1318 echo "metadata is corrupt." 1319 return 1 1320 fi 1321 done < filelist 1322 echo "done." 1323 fi 1324 1325# Sanity-check the metadata files. 1326 cut -f 2 -d '|' tINDEX.new > filelist 1327 while read X; do 1328 fetch_metadata_sanity ${X} || return 1 1329 done < filelist 1330 1331# Remove files which are no longer needed 1332 cut -f 2 -d '|' tINDEX.present | 1333 sort > oldfiles 1334 cut -f 2 -d '|' tINDEX.new | 1335 sort | 1336 comm -13 - oldfiles | 1337 lam -s "files/" - -s ".gz" | 1338 xargs rm -f 1339 rm patchlist filelist oldfiles 1340 rm ${TINDEXHASH} 1341 1342# We're done! 1343 mv tINDEX.new tINDEX.present 1344 mv tag.new tag 1345 1346 return 0 1347} 1348 1349# Extract a subset of a downloaded metadata file containing only the parts 1350# which are listed in COMPONENTS. 1351fetch_filter_metadata_components () { 1352 METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'` 1353 gunzip -c < files/${METAHASH}.gz > $1.all 1354 1355 # Fish out the lines belonging to components we care about. 1356 for C in ${COMPONENTS}; do 1357 look "`echo ${C} | tr '/' '|'`|" $1.all 1358 done > $1 1359 1360 # Remove temporary file. 1361 rm $1.all 1362} 1363 1364# Generate a filtered version of the metadata file $1 from the downloaded 1365# file, by fishing out the lines corresponding to components we're trying 1366# to keep updated, and then removing lines corresponding to paths we want 1367# to ignore. 1368fetch_filter_metadata () { 1369 # Fish out the lines belonging to components we care about. 1370 fetch_filter_metadata_components $1 1371 1372 # Canonicalize directory names by removing any trailing / in 1373 # order to avoid listing directories multiple times if they 1374 # belong to multiple components. Turning "/" into "" doesn't 1375 # matter, since we add a leading "/" when we use paths later. 1376 cut -f 3- -d '|' $1 | 1377 sed -e 's,/|d|,|d|,' | 1378 sort -u > $1.tmp 1379 1380 # Figure out which lines to ignore and remove them. 1381 for X in ${IGNOREPATHS}; do 1382 grep -E "^${X}" $1.tmp 1383 done | 1384 sort -u | 1385 comm -13 - $1.tmp > $1 1386 1387 # Remove temporary files. 1388 rm $1.tmp 1389} 1390 1391# Filter the metadata file $1 by adding lines with "/boot/$2" 1392# replaced by ${KERNELDIR} (which is `sysctl -n kern.bootfile` minus the 1393# trailing "/kernel"); and if "/boot/$2" does not exist, remove 1394# the original lines which start with that. 1395# Put another way: Deal with the fact that the FOO kernel is sometimes 1396# installed in /boot/FOO/ and is sometimes installed elsewhere. 1397fetch_filter_kernel_names () { 1398 grep ^/boot/$2 $1 | 1399 sed -e "s,/boot/$2,${KERNELDIR},g" | 1400 sort - $1 > $1.tmp 1401 mv $1.tmp $1 1402 1403 if ! [ -d /boot/$2 ]; then 1404 grep -v ^/boot/$2 $1 > $1.tmp 1405 mv $1.tmp $1 1406 fi 1407} 1408 1409# For all paths appearing in $1 or $3, inspect the system 1410# and generate $2 describing what is currently installed. 1411fetch_inspect_system () { 1412 # No errors yet... 1413 rm -f .err 1414 1415 # Tell the user why his disk is suddenly making lots of noise 1416 echo -n "Inspecting system... " 1417 1418 # Generate list of files to inspect 1419 cat $1 $3 | 1420 cut -f 1 -d '|' | 1421 sort -u > filelist 1422 1423 # Examine each file and output lines of the form 1424 # /path/to/file|type|device-inum|user|group|perm|flags|value 1425 # sorted by device and inode number. 1426 while read F; do 1427 # If the symlink/file/directory does not exist, record this. 1428 if ! [ -e ${BASEDIR}/${F} ]; then 1429 echo "${F}|-||||||" 1430 continue 1431 fi 1432 if ! [ -r ${BASEDIR}/${F} ]; then 1433 echo "Cannot read file: ${BASEDIR}/${F}" \ 1434 >/dev/stderr 1435 touch .err 1436 return 1 1437 fi 1438 1439 # Otherwise, output an index line. 1440 if [ -L ${BASEDIR}/${F} ]; then 1441 echo -n "${F}|L|" 1442 stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F}; 1443 readlink ${BASEDIR}/${F}; 1444 elif [ -f ${BASEDIR}/${F} ]; then 1445 echo -n "${F}|f|" 1446 stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F}; 1447 sha256 -q ${BASEDIR}/${F}; 1448 elif [ -d ${BASEDIR}/${F} ]; then 1449 echo -n "${F}|d|" 1450 stat -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F}; 1451 else 1452 echo "Unknown file type: ${BASEDIR}/${F}" \ 1453 >/dev/stderr 1454 touch .err 1455 return 1 1456 fi 1457 done < filelist | 1458 sort -k 3,3 -t '|' > $2.tmp 1459 rm filelist 1460 1461 # Check if an error occured during system inspection 1462 if [ -f .err ]; then 1463 return 1 1464 fi 1465 1466 # Convert to the form 1467 # /path/to/file|type|user|group|perm|flags|value|hlink 1468 # by resolving identical device and inode numbers into hard links. 1469 cut -f 1,3 -d '|' $2.tmp | 1470 sort -k 1,1 -t '|' | 1471 sort -s -u -k 2,2 -t '|' | 1472 join -1 2 -2 3 -t '|' - $2.tmp | 1473 awk -F \| -v OFS=\| \ 1474 '{ 1475 if (($2 == $3) || ($4 == "-")) 1476 print $3,$4,$5,$6,$7,$8,$9,"" 1477 else 1478 print $3,$4,$5,$6,$7,$8,$9,$2 1479 }' | 1480 sort > $2 1481 rm $2.tmp 1482 1483 # We're finished looking around 1484 echo "done." 1485} 1486 1487# For any paths matching ${MERGECHANGES}, compare $1 and $2 and find any 1488# files which differ; generate $3 containing these paths and the old hashes. 1489fetch_filter_mergechanges () { 1490 # Pull out the paths and hashes of the files matching ${MERGECHANGES}. 1491 for F in $1 $2; do 1492 for X in ${MERGECHANGES}; do 1493 grep -E "^${X}" ${F} 1494 done | 1495 cut -f 1,2,7 -d '|' | 1496 sort > ${F}-values 1497 done 1498 1499 # Any line in $2-values which doesn't appear in $1-values and is a 1500 # file means that we should list the path in $3. 1501 comm -13 $1-values $2-values | 1502 fgrep '|f|' | 1503 cut -f 1 -d '|' > $2-paths 1504 1505 # For each path, pull out one (and only one!) entry from $1-values. 1506 # Note that we cannot distinguish which "old" version the user made 1507 # changes to; but hopefully any changes which occur due to security 1508 # updates will exist in both the "new" version and the version which 1509 # the user has installed, so the merging will still work. 1510 while read X; do 1511 look "${X}|" $1-values | 1512 head -1 1513 done < $2-paths > $3 1514 1515 # Clean up 1516 rm $1-values $2-values $2-paths 1517} 1518 1519# For any paths matching ${UPDATEIFUNMODIFIED}, remove lines from $[123] 1520# which correspond to lines in $2 with hashes not matching $1 or $3, unless 1521# the paths are listed in $4. For entries in $2 marked "not present" 1522# (aka. type -), remove lines from $[123] unless there is a corresponding 1523# entry in $1. 1524fetch_filter_unmodified_notpresent () { 1525 # Figure out which lines of $1 and $3 correspond to bits which 1526 # should only be updated if they haven't changed, and fish out 1527 # the (path, type, value) tuples. 1528 # NOTE: We don't consider a file to be "modified" if it matches 1529 # the hash from $3. 1530 for X in ${UPDATEIFUNMODIFIED}; do 1531 grep -E "^${X}" $1 1532 grep -E "^${X}" $3 1533 done | 1534 cut -f 1,2,7 -d '|' | 1535 sort > $1-values 1536 1537 # Do the same for $2. 1538 for X in ${UPDATEIFUNMODIFIED}; do 1539 grep -E "^${X}" $2 1540 done | 1541 cut -f 1,2,7 -d '|' | 1542 sort > $2-values 1543 1544 # Any entry in $2-values which is not in $1-values corresponds to 1545 # a path which we need to remove from $1, $2, and $3, unless it 1546 # that path appears in $4. 1547 comm -13 $1-values $2-values | 1548 sort -t '|' -k 1,1 > mlines.tmp 1549 cut -f 1 -d '|' $4 | 1550 sort | 1551 join -v 2 -t '|' - mlines.tmp | 1552 sort > mlines 1553 rm $1-values $2-values mlines.tmp 1554 1555 # Any lines in $2 which are not in $1 AND are "not present" lines 1556 # also belong in mlines. 1557 comm -13 $1 $2 | 1558 cut -f 1,2,7 -d '|' | 1559 fgrep '|-|' >> mlines 1560 1561 # Remove lines from $1, $2, and $3 1562 for X in $1 $2 $3; do 1563 sort -t '|' -k 1,1 ${X} > ${X}.tmp 1564 cut -f 1 -d '|' < mlines | 1565 sort | 1566 join -v 2 -t '|' - ${X}.tmp | 1567 sort > ${X} 1568 rm ${X}.tmp 1569 done 1570 1571 # Store a list of the modified files, for future reference 1572 fgrep -v '|-|' mlines | 1573 cut -f 1 -d '|' > modifiedfiles 1574 rm mlines 1575} 1576 1577# For each entry in $1 of type -, remove any corresponding 1578# entry from $2 if ${ALLOWADD} != "yes". Remove all entries 1579# of type - from $1. 1580fetch_filter_allowadd () { 1581 cut -f 1,2 -d '|' < $1 | 1582 fgrep '|-' | 1583 cut -f 1 -d '|' > filesnotpresent 1584 1585 if [ ${ALLOWADD} != "yes" ]; then 1586 sort < $2 | 1587 join -v 1 -t '|' - filesnotpresent | 1588 sort > $2.tmp 1589 mv $2.tmp $2 1590 fi 1591 1592 sort < $1 | 1593 join -v 1 -t '|' - filesnotpresent | 1594 sort > $1.tmp 1595 mv $1.tmp $1 1596 rm filesnotpresent 1597} 1598 1599# If ${ALLOWDELETE} != "yes", then remove any entries from $1 1600# which don't correspond to entries in $2. 1601fetch_filter_allowdelete () { 1602 # Produce a lists ${PATH}|${TYPE} 1603 for X in $1 $2; do 1604 cut -f 1-2 -d '|' < ${X} | 1605 sort -u > ${X}.nodes 1606 done 1607 1608 # Figure out which lines need to be removed from $1. 1609 if [ ${ALLOWDELETE} != "yes" ]; then 1610 comm -23 $1.nodes $2.nodes > $1.badnodes 1611 else 1612 : > $1.badnodes 1613 fi 1614 1615 # Remove the relevant lines from $1 1616 while read X; do 1617 look "${X}|" $1 1618 done < $1.badnodes | 1619 comm -13 - $1 > $1.tmp 1620 mv $1.tmp $1 1621 1622 rm $1.badnodes $1.nodes $2.nodes 1623} 1624 1625# If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in $2 1626# with metadata not matching any entry in $1, replace the corresponding 1627# line of $3 with one having the same metadata as the entry in $2. 1628fetch_filter_modified_metadata () { 1629 # Fish out the metadata from $1 and $2 1630 for X in $1 $2; do 1631 cut -f 1-6 -d '|' < ${X} > ${X}.metadata 1632 done 1633 1634 # Find the metadata we need to keep 1635 if [ ${KEEPMODIFIEDMETADATA} = "yes" ]; then 1636 comm -13 $1.metadata $2.metadata > keepmeta 1637 else 1638 : > keepmeta 1639 fi 1640 1641 # Extract the lines which we need to remove from $3, and 1642 # construct the lines which we need to add to $3. 1643 : > $3.remove 1644 : > $3.add 1645 while read LINE; do 1646 NODE=`echo "${LINE}" | cut -f 1-2 -d '|'` 1647 look "${NODE}|" $3 >> $3.remove 1648 look "${NODE}|" $3 | 1649 cut -f 7- -d '|' | 1650 lam -s "${LINE}|" - >> $3.add 1651 done < keepmeta 1652 1653 # Remove the specified lines and add the new lines. 1654 sort $3.remove | 1655 comm -13 - $3 | 1656 sort -u - $3.add > $3.tmp 1657 mv $3.tmp $3 1658 1659 rm keepmeta $1.metadata $2.metadata $3.add $3.remove 1660} 1661 1662# Remove lines from $1 and $2 which are identical; 1663# no need to update a file if it isn't changing. 1664fetch_filter_uptodate () { 1665 comm -23 $1 $2 > $1.tmp 1666 comm -13 $1 $2 > $2.tmp 1667 1668 mv $1.tmp $1 1669 mv $2.tmp $2 1670} 1671 1672# Fetch any "clean" old versions of files we need for merging changes. 1673fetch_files_premerge () { 1674 # We only need to do anything if $1 is non-empty. 1675 if [ -s $1 ]; then 1676 # Tell the user what we're doing 1677 echo -n "Fetching files from ${OLDRELNUM} for merging... " 1678 1679 # List of files wanted 1680 fgrep '|f|' < $1 | 1681 cut -f 3 -d '|' | 1682 sort -u > files.wanted 1683 1684 # Only fetch the files we don't already have 1685 while read Y; do 1686 if [ ! -f "files/${Y}.gz" ]; then 1687 echo ${Y}; 1688 fi 1689 done < files.wanted > filelist 1690 1691 # Actually fetch them 1692 lam -s "${OLDFETCHDIR}/f/" - -s ".gz" < filelist | 1693 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1694 2>${QUIETREDIR} 1695 1696 # Make sure we got them all, and move them into /files/ 1697 while read Y; do 1698 if ! [ -f ${Y}.gz ]; then 1699 echo "failed." 1700 return 1 1701 fi 1702 if [ `gunzip -c < ${Y}.gz | 1703 ${SHA256} -q` = ${Y} ]; then 1704 mv ${Y}.gz files/${Y}.gz 1705 else 1706 echo "${Y} has incorrect hash." 1707 return 1 1708 fi 1709 done < filelist 1710 echo "done." 1711 1712 # Clean up 1713 rm filelist files.wanted 1714 fi 1715} 1716 1717# Prepare to fetch files: Generate a list of the files we need, 1718# copy the unmodified files we have into /files/, and generate 1719# a list of patches to download. 1720fetch_files_prepare () { 1721 # Tell the user why his disk is suddenly making lots of noise 1722 echo -n "Preparing to download files... " 1723 1724 # Reduce indices to ${PATH}|${HASH} pairs 1725 for X in $1 $2 $3; do 1726 cut -f 1,2,7 -d '|' < ${X} | 1727 fgrep '|f|' | 1728 cut -f 1,3 -d '|' | 1729 sort > ${X}.hashes 1730 done 1731 1732 # List of files wanted 1733 cut -f 2 -d '|' < $3.hashes | 1734 sort -u | 1735 while read HASH; do 1736 if ! [ -f files/${HASH}.gz ]; then 1737 echo ${HASH} 1738 fi 1739 done > files.wanted 1740 1741 # Generate a list of unmodified files 1742 comm -12 $1.hashes $2.hashes | 1743 sort -k 1,1 -t '|' > unmodified.files 1744 1745 # Copy all files into /files/. We only need the unmodified files 1746 # for use in patching; but we'll want all of them if the user asks 1747 # to rollback the updates later. 1748 while read LINE; do 1749 F=`echo "${LINE}" | cut -f 1 -d '|'` 1750 HASH=`echo "${LINE}" | cut -f 2 -d '|'` 1751 1752 # Skip files we already have. 1753 if [ -f files/${HASH}.gz ]; then 1754 continue 1755 fi 1756 1757 # Make sure the file hasn't changed. 1758 cp "${BASEDIR}/${F}" tmpfile 1759 if [ `sha256 -q tmpfile` != ${HASH} ]; then 1760 echo 1761 echo "File changed while FreeBSD Update running: ${F}" 1762 return 1 1763 fi 1764 1765 # Place the file into storage. 1766 gzip -c < tmpfile > files/${HASH}.gz 1767 rm tmpfile 1768 done < $2.hashes 1769 1770 # Produce a list of patches to download 1771 sort -k 1,1 -t '|' $3.hashes | 1772 join -t '|' -o 2.2,1.2 - unmodified.files | 1773 fetch_make_patchlist > patchlist 1774 1775 # Garbage collect 1776 rm unmodified.files $1.hashes $2.hashes $3.hashes 1777 1778 # We don't need the list of possible old files any more. 1779 rm $1 1780 1781 # We're finished making noise 1782 echo "done." 1783} 1784 1785# Fetch files. 1786fetch_files () { 1787 # Attempt to fetch patches 1788 if [ -s patchlist ]; then 1789 echo -n "Fetching `wc -l < patchlist | tr -d ' '` " 1790 echo ${NDEBUG} "patches.${DDSTATS}" 1791 tr '|' '-' < patchlist | 1792 lam -s "${PATCHDIR}/" - | 1793 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1794 2>${STATSREDIR} | fetch_progress 1795 echo "done." 1796 1797 # Attempt to apply patches 1798 echo -n "Applying patches... " 1799 tr '|' ' ' < patchlist | 1800 while read X Y; do 1801 if [ ! -f "${X}-${Y}" ]; then continue; fi 1802 gunzip -c < files/${X}.gz > OLD 1803 1804 bspatch OLD NEW ${X}-${Y} 1805 1806 if [ `${SHA256} -q NEW` = ${Y} ]; then 1807 mv NEW files/${Y} 1808 gzip -n files/${Y} 1809 fi 1810 rm -f diff OLD NEW ${X}-${Y} 1811 done 2>${QUIETREDIR} 1812 echo "done." 1813 fi 1814 1815 # Download files which couldn't be generate via patching 1816 while read Y; do 1817 if [ ! -f "files/${Y}.gz" ]; then 1818 echo ${Y}; 1819 fi 1820 done < files.wanted > filelist 1821 1822 if [ -s filelist ]; then 1823 echo -n "Fetching `wc -l < filelist | tr -d ' '` " 1824 echo ${NDEBUG} "files... " 1825 lam -s "${FETCHDIR}/f/" - -s ".gz" < filelist | 1826 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \ 1827 2>${QUIETREDIR} 1828 1829 while read Y; do 1830 if ! [ -f ${Y}.gz ]; then 1831 echo "failed." 1832 return 1 1833 fi 1834 if [ `gunzip -c < ${Y}.gz | 1835 ${SHA256} -q` = ${Y} ]; then 1836 mv ${Y}.gz files/${Y}.gz 1837 else 1838 echo "${Y} has incorrect hash." 1839 return 1 1840 fi 1841 done < filelist 1842 echo "done." 1843 fi 1844 1845 # Clean up 1846 rm files.wanted filelist patchlist 1847} 1848 1849# Create and populate install manifest directory; and report what updates 1850# are available. 1851fetch_create_manifest () { 1852 # If we have an existing install manifest, nuke it. 1853 if [ -L "${BDHASH}-install" ]; then 1854 rm -r ${BDHASH}-install/ 1855 rm ${BDHASH}-install 1856 fi 1857 1858 # Report to the user if any updates were avoided due to local changes 1859 if [ -s modifiedfiles ]; then 1860 echo 1861 echo -n "The following files are affected by updates, " 1862 echo "but no changes have" 1863 echo -n "been downloaded because the files have been " 1864 echo "modified locally:" 1865 cat modifiedfiles 1866 fi | more 1867 rm modifiedfiles 1868 1869 # If no files will be updated, tell the user and exit 1870 if ! [ -s INDEX-PRESENT ] && 1871 ! [ -s INDEX-NEW ]; then 1872 rm INDEX-PRESENT INDEX-NEW 1873 echo 1874 echo -n "No updates needed to update system to " 1875 echo "${RELNUM}-p${RELPATCHNUM}." 1876 return 1877 fi 1878 1879 # Divide files into (a) removed files, (b) added files, and 1880 # (c) updated files. 1881 cut -f 1 -d '|' < INDEX-PRESENT | 1882 sort > INDEX-PRESENT.flist 1883 cut -f 1 -d '|' < INDEX-NEW | 1884 sort > INDEX-NEW.flist 1885 comm -23 INDEX-PRESENT.flist INDEX-NEW.flist > files.removed 1886 comm -13 INDEX-PRESENT.flist INDEX-NEW.flist > files.added 1887 comm -12 INDEX-PRESENT.flist INDEX-NEW.flist > files.updated 1888 rm INDEX-PRESENT.flist INDEX-NEW.flist 1889 1890 # Report removed files, if any 1891 if [ -s files.removed ]; then 1892 echo 1893 echo -n "The following files will be removed " 1894 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:" 1895 cat files.removed 1896 fi | more 1897 rm files.removed 1898 1899 # Report added files, if any 1900 if [ -s files.added ]; then 1901 echo 1902 echo -n "The following files will be added " 1903 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:" 1904 cat files.added 1905 fi | more 1906 rm files.added 1907 1908 # Report updated files, if any 1909 if [ -s files.updated ]; then 1910 echo 1911 echo -n "The following files will be updated " 1912 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:" 1913 1914 cat files.updated 1915 fi | more 1916 rm files.updated 1917 1918 # Create a directory for the install manifest. 1919 MDIR=`mktemp -d install.XXXXXX` || return 1 1920 1921 # Populate it 1922 mv INDEX-PRESENT ${MDIR}/INDEX-OLD 1923 mv INDEX-NEW ${MDIR}/INDEX-NEW 1924 1925 # Link it into place 1926 ln -s ${MDIR} ${BDHASH}-install 1927} 1928 1929# Warn about any upcoming EoL 1930fetch_warn_eol () { 1931 # What's the current time? 1932 NOWTIME=`date "+%s"` 1933 1934 # When did we last warn about the EoL date? 1935 if [ -f lasteolwarn ]; then 1936 LASTWARN=`cat lasteolwarn` 1937 else 1938 LASTWARN=`expr ${NOWTIME} - 63072000` 1939 fi 1940 1941 # If the EoL time is past, warn. 1942 if [ ${EOLTIME} -lt ${NOWTIME} ]; then 1943 echo 1944 cat <<-EOF 1945 WARNING: `uname -sr` HAS PASSED ITS END-OF-LIFE DATE. 1946 Any security issues discovered after `date -r ${EOLTIME}` 1947 will not have been corrected. 1948 EOF 1949 return 1 1950 fi 1951 1952 # Figure out how long it has been since we last warned about the 1953 # upcoming EoL, and how much longer we have left. 1954 SINCEWARN=`expr ${NOWTIME} - ${LASTWARN}` 1955 TIMELEFT=`expr ${EOLTIME} - ${NOWTIME}` 1956 1957 # Don't warn if the EoL is more than 3 months away 1958 if [ ${TIMELEFT} -gt 7884000 ]; then 1959 return 0 1960 fi 1961 1962 # Don't warn if the time remaining is more than 3 times the time 1963 # since the last warning. 1964 if [ ${TIMELEFT} -gt `expr ${SINCEWARN} \* 3` ]; then 1965 return 0 1966 fi 1967 1968 # Figure out what time units to use. 1969 if [ ${TIMELEFT} -lt 604800 ]; then 1970 UNIT="day" 1971 SIZE=86400 1972 elif [ ${TIMELEFT} -lt 2678400 ]; then 1973 UNIT="week" 1974 SIZE=604800 1975 else 1976 UNIT="month" 1977 SIZE=2678400 1978 fi 1979 1980 # Compute the right number of units 1981 NUM=`expr ${TIMELEFT} / ${SIZE}` 1982 if [ ${NUM} != 1 ]; then 1983 UNIT="${UNIT}s" 1984 fi 1985 1986 # Print the warning 1987 echo 1988 cat <<-EOF 1989 WARNING: `uname -sr` is approaching its End-of-Life date. 1990 It is strongly recommended that you upgrade to a newer 1991 release within the next ${NUM} ${UNIT}. 1992 EOF 1993 1994 # Update the stored time of last warning 1995 echo ${NOWTIME} > lasteolwarn 1996} 1997 1998# Do the actual work involved in "fetch" / "cron". 1999fetch_run () { 2000 workdir_init || return 1 2001 2002 # Prepare the mirror list. 2003 fetch_pick_server_init && fetch_pick_server 2004 2005 # Try to fetch the public key until we run out of servers. 2006 while ! fetch_key; do 2007 fetch_pick_server || return 1 2008 done 2009 2010 # Try to fetch the metadata index signature ("tag") until we run 2011 # out of available servers; and sanity check the downloaded tag. 2012 while ! fetch_tag; do 2013 fetch_pick_server || return 1 2014 done 2015 fetch_tagsanity || return 1 2016 2017 # Fetch the latest INDEX-NEW and INDEX-OLD files. 2018 fetch_metadata INDEX-NEW INDEX-OLD || return 1 2019 2020 # Generate filtered INDEX-NEW and INDEX-OLD files containing only 2021 # the lines which (a) belong to components we care about, and (b) 2022 # don't correspond to paths we're explicitly ignoring. 2023 fetch_filter_metadata INDEX-NEW || return 1 2024 fetch_filter_metadata INDEX-OLD || return 1 2025 2026 # Translate /boot/${KERNCONF} into ${KERNELDIR} 2027 fetch_filter_kernel_names INDEX-NEW ${KERNCONF} 2028 fetch_filter_kernel_names INDEX-OLD ${KERNCONF} 2029 2030 # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the 2031 # system and generate an INDEX-PRESENT file. 2032 fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2033 2034 # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which 2035 # correspond to lines in INDEX-PRESENT with hashes not appearing 2036 # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in 2037 # INDEX-PRESENT has type - and there isn't a corresponding entry in 2038 # INDEX-OLD with type -. 2039 fetch_filter_unmodified_notpresent \ 2040 INDEX-OLD INDEX-PRESENT INDEX-NEW /dev/null 2041 2042 # For each entry in INDEX-PRESENT of type -, remove any corresponding 2043 # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries 2044 # of type - from INDEX-PRESENT. 2045 fetch_filter_allowadd INDEX-PRESENT INDEX-NEW 2046 2047 # If ${ALLOWDELETE} != "yes", then remove any entries from 2048 # INDEX-PRESENT which don't correspond to entries in INDEX-NEW. 2049 fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW 2050 2051 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in 2052 # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD, 2053 # replace the corresponding line of INDEX-NEW with one having the 2054 # same metadata as the entry in INDEX-PRESENT. 2055 fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW 2056 2057 # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical; 2058 # no need to update a file if it isn't changing. 2059 fetch_filter_uptodate INDEX-PRESENT INDEX-NEW 2060 2061 # Prepare to fetch files: Generate a list of the files we need, 2062 # copy the unmodified files we have into /files/, and generate 2063 # a list of patches to download. 2064 fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2065 2066 # Fetch files. 2067 fetch_files || return 1 2068 2069 # Create and populate install manifest directory; and report what 2070 # updates are available. 2071 fetch_create_manifest || return 1 2072 2073 # Warn about any upcoming EoL 2074 fetch_warn_eol || return 1 2075} 2076 2077# If StrictComponents is not "yes", generate a new components list 2078# with only the components which appear to be installed. 2079upgrade_guess_components () { 2080 if [ "${STRICTCOMPONENTS}" = "no" ]; then 2081 # Generate filtered INDEX-ALL with only the components listed 2082 # in COMPONENTS. 2083 fetch_filter_metadata_components $1 || return 1 2084 2085 # Tell the user why his disk is suddenly making lots of noise 2086 echo -n "Inspecting system... " 2087 2088 # Look at the files on disk, and assume that a component is 2089 # supposed to be present if it is more than half-present. 2090 cut -f 1-3 -d '|' < INDEX-ALL | 2091 tr '|' ' ' | 2092 while read C S F; do 2093 if [ -e ${BASEDIR}/${F} ]; then 2094 echo "+ ${C}|${S}" 2095 fi 2096 echo "= ${C}|${S}" 2097 done | 2098 sort | 2099 uniq -c | 2100 sed -E 's,^ +,,' > compfreq 2101 grep ' = ' compfreq | 2102 cut -f 1,3 -d ' ' | 2103 sort -k 2,2 -t ' ' > compfreq.total 2104 grep ' + ' compfreq | 2105 cut -f 1,3 -d ' ' | 2106 sort -k 2,2 -t ' ' > compfreq.present 2107 join -t ' ' -1 2 -2 2 compfreq.present compfreq.total | 2108 while read S P T; do 2109 if [ ${P} -gt `expr ${T} / 2` ]; then 2110 echo ${S} 2111 fi 2112 done > comp.present 2113 cut -f 2 -d ' ' < compfreq.total > comp.total 2114 rm INDEX-ALL compfreq compfreq.total compfreq.present 2115 2116 # We're done making noise. 2117 echo "done." 2118 2119 # Sometimes the kernel isn't installed where INDEX-ALL 2120 # thinks that it should be: In particular, it is often in 2121 # /boot/kernel instead of /boot/GENERIC or /boot/SMP. To 2122 # deal with this, if "kernel|X" is listed in comp.total 2123 # (i.e., is a component which would be upgraded if it is 2124 # found to be present) we will add it to comp.present. 2125 # If "kernel|<anything>" is in comp.total but "kernel|X" is 2126 # not, we print a warning -- the user is running a kernel 2127 # which isn't part of the release. 2128 KCOMP=`echo ${KERNCONF} | tr 'A-Z' 'a-z'` 2129 grep -E "^kernel\|${KCOMP}\$" comp.total >> comp.present 2130 2131 if grep -qE "^kernel\|" comp.total && 2132 ! grep -qE "^kernel\|${KCOMP}\$" comp.total; then 2133 cat <<-EOF 2134 2135WARNING: This system is running a "${KCOMP}" kernel, which is not a 2136kernel configuration distributed as part of FreeBSD ${RELNUM}. 2137This kernel will not be updated: you MUST update the kernel manually 2138before running "$0 install". 2139 EOF 2140 fi 2141 2142 # Re-sort the list of installed components and generate 2143 # the list of non-installed components. 2144 sort -u < comp.present > comp.present.tmp 2145 mv comp.present.tmp comp.present 2146 comm -13 comp.present comp.total > comp.absent 2147 2148 # Ask the user to confirm that what we have is correct. To 2149 # reduce user confusion, translate "X|Y" back to "X/Y" (as 2150 # subcomponents must be listed in the configuration file). 2151 echo 2152 echo -n "The following components of FreeBSD " 2153 echo "seem to be installed:" 2154 tr '|' '/' < comp.present | 2155 fmt -72 2156 echo 2157 echo -n "The following components of FreeBSD " 2158 echo "do not seem to be installed:" 2159 tr '|' '/' < comp.absent | 2160 fmt -72 2161 echo 2162 continuep || return 1 2163 echo 2164 2165 # Suck the generated list of components into ${COMPONENTS}. 2166 # Note that comp.present.tmp is used due to issues with 2167 # pipelines and setting variables. 2168 COMPONENTS="" 2169 tr '|' '/' < comp.present > comp.present.tmp 2170 while read C; do 2171 COMPONENTS="${COMPONENTS} ${C}" 2172 done < comp.present.tmp 2173 2174 # Delete temporary files 2175 rm comp.present comp.present.tmp comp.absent comp.total 2176 fi 2177} 2178 2179# If StrictComponents is not "yes", COMPONENTS contains an entry 2180# corresponding to the currently running kernel, and said kernel 2181# does not exist in the new release, add "kernel/generic" to the 2182# list of components. 2183upgrade_guess_new_kernel () { 2184 if [ "${STRICTCOMPONENTS}" = "no" ]; then 2185 # Grab the unfiltered metadata file. 2186 METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'` 2187 gunzip -c < files/${METAHASH}.gz > $1.all 2188 2189 # If "kernel/${KCOMP}" is in ${COMPONENTS} and that component 2190 # isn't in $1.all, we need to add kernel/generic. 2191 for C in ${COMPONENTS}; do 2192 if [ ${C} = "kernel/${KCOMP}" ] && 2193 ! grep -qE "^kernel\|${KCOMP}\|" $1.all; then 2194 COMPONENTS="${COMPONENTS} kernel/generic" 2195 NKERNCONF="GENERIC" 2196 cat <<-EOF 2197 2198WARNING: This system is running a "${KCOMP}" kernel, which is not a 2199kernel configuration distributed as part of FreeBSD ${RELNUM}. 2200As part of upgrading to FreeBSD ${RELNUM}, this kernel will be 2201replaced with a "generic" kernel. 2202 EOF 2203 continuep || return 1 2204 fi 2205 done 2206 2207 # Don't need this any more... 2208 rm $1.all 2209 fi 2210} 2211 2212# Convert INDEX-OLD (last release) and INDEX-ALL (new release) into 2213# INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades). 2214upgrade_oldall_to_oldnew () { 2215 # For each ${F}|... which appears in INDEX-ALL but does not appear 2216 # in INDEX-OLD, add ${F}|-|||||| to INDEX-OLD. 2217 cut -f 1 -d '|' < $1 | 2218 sort -u > $1.paths 2219 cut -f 1 -d '|' < $2 | 2220 sort -u | 2221 comm -13 $1.paths - | 2222 lam - -s "|-||||||" | 2223 sort - $1 > $1.tmp 2224 mv $1.tmp $1 2225 2226 # Remove lines from INDEX-OLD which also appear in INDEX-ALL 2227 comm -23 $1 $2 > $1.tmp 2228 mv $1.tmp $1 2229 2230 # Remove lines from INDEX-ALL which have a file name not appearing 2231 # anywhere in INDEX-OLD (since these must be files which haven't 2232 # changed -- if they were new, there would be an entry of type "-"). 2233 cut -f 1 -d '|' < $1 | 2234 sort -u > $1.paths 2235 sort -k 1,1 -t '|' < $2 | 2236 join -t '|' - $1.paths | 2237 sort > $2.tmp 2238 rm $1.paths 2239 mv $2.tmp $2 2240 2241 # Rename INDEX-ALL to INDEX-NEW. 2242 mv $2 $3 2243} 2244 2245# From the list of "old" files in $1, merge changes in $2 with those in $3, 2246# and update $3 to reflect the hashes of merged files. 2247upgrade_merge () { 2248 # We only need to do anything if $1 is non-empty. 2249 if [ -s $1 ]; then 2250 cut -f 1 -d '|' $1 | 2251 sort > $1-paths 2252 2253 # Create staging area for merging files 2254 rm -rf merge/ 2255 while read F; do 2256 D=`dirname ${F}` 2257 mkdir -p merge/old/${D} 2258 mkdir -p merge/${OLDRELNUM}/${D} 2259 mkdir -p merge/${RELNUM}/${D} 2260 mkdir -p merge/new/${D} 2261 done < $1-paths 2262 2263 # Copy in files 2264 while read F; do 2265 # Currently installed file 2266 V=`look "${F}|" $2 | cut -f 7 -d '|'` 2267 gunzip < files/${V}.gz > merge/old/${F} 2268 2269 # Old release 2270 if look "${F}|" $1 | fgrep -q "|f|"; then 2271 V=`look "${F}|" $1 | cut -f 3 -d '|'` 2272 gunzip < files/${V}.gz \ 2273 > merge/${OLDRELNUM}/${F} 2274 fi 2275 2276 # New release 2277 if look "${F}|" $3 | cut -f 1,2,7 -d '|' | 2278 fgrep -q "|f|"; then 2279 V=`look "${F}|" $3 | cut -f 7 -d '|'` 2280 gunzip < files/${V}.gz \ 2281 > merge/${RELNUM}/${F} 2282 fi 2283 done < $1-paths 2284 2285 # Attempt to automatically merge changes 2286 echo -n "Attempting to automatically merge " 2287 echo -n "changes in files..." 2288 : > failed.merges 2289 while read F; do 2290 # If the file doesn't exist in the new release, 2291 # the result of "merging changes" is having the file 2292 # not exist. 2293 if ! [ -f merge/${RELNUM}/${F} ]; then 2294 continue 2295 fi 2296 2297 # If the file didn't exist in the old release, we're 2298 # going to throw away the existing file and hope that 2299 # the version from the new release is what we want. 2300 if ! [ -f merge/${OLDRELNUM}/${F} ]; then 2301 cp merge/${RELNUM}/${F} merge/new/${F} 2302 continue 2303 fi 2304 2305 # Some files need special treatment. 2306 case ${F} in 2307 /etc/spwd.db | /etc/pwd.db | /etc/login.conf.db) 2308 # Don't merge these -- we're rebuild them 2309 # after updates are installed. 2310 cp merge/old/${F} merge/new/${F} 2311 ;; 2312 *) 2313 if ! merge -p -L "current version" \ 2314 -L "${OLDRELNUM}" -L "${RELNUM}" \ 2315 merge/old/${F} \ 2316 merge/${OLDRELNUM}/${F} \ 2317 merge/${RELNUM}/${F} \ 2318 > merge/new/${F} 2>/dev/null; then 2319 echo ${F} >> failed.merges 2320 fi 2321 ;; 2322 esac 2323 done < $1-paths 2324 echo " done." 2325 2326 # Ask the user to handle any files which didn't merge. 2327 while read F; do 2328 cat <<-EOF 2329 2330The following file could not be merged automatically: ${F} 2331Press Enter to edit this file in ${EDITOR} and resolve the conflicts 2332manually... 2333 EOF 2334 read dummy </dev/tty 2335 ${EDITOR} `pwd`/merge/new/${F} < /dev/tty 2336 done < failed.merges 2337 rm failed.merges 2338 2339 # Ask the user to confirm that he likes how the result 2340 # of merging files. 2341 while read F; do 2342 # Skip files which haven't changed. 2343 if [ -f merge/new/${F} ] && 2344 cmp -s merge/old/${F} merge/new/${F}; then 2345 continue 2346 fi 2347 2348 # Warn about files which are ceasing to exist. 2349 if ! [ -f merge/new/${F} ]; then 2350 cat <<-EOF 2351 2352The following file will be removed, as it no longer exists in 2353FreeBSD ${RELNUM}: ${F} 2354 EOF 2355 continuep < /dev/tty || return 1 2356 continue 2357 fi 2358 2359 # Print changes for the user's approval. 2360 cat <<-EOF 2361 2362The following changes, which occurred between FreeBSD ${OLDRELNUM} and 2363FreeBSD ${RELNUM} have been merged into ${F}: 2364EOF 2365 diff -U 5 -L "current version" -L "new version" \ 2366 merge/old/${F} merge/new/${F} || true 2367 continuep < /dev/tty || return 1 2368 done < $1-paths 2369 2370 # Store merged files. 2371 while read F; do 2372 if [ -f merge/new/${F} ]; then 2373 V=`${SHA256} -q merge/new/${F}` 2374 2375 gzip -c < merge/new/${F} > files/${V}.gz 2376 echo "${F}|${V}" 2377 fi 2378 done < $1-paths > newhashes 2379 2380 # Pull lines out from $3 which need to be updated to 2381 # reflect merged files. 2382 while read F; do 2383 look "${F}|" $3 2384 done < $1-paths > $3-oldlines 2385 2386 # Update lines to reflect merged files 2387 join -t '|' -o 1.1,1.2,1.3,1.4,1.5,1.6,2.2,1.8 \ 2388 $3-oldlines newhashes > $3-newlines 2389 2390 # Remove old lines from $3 and add new lines. 2391 sort $3-oldlines | 2392 comm -13 - $3 | 2393 sort - $3-newlines > $3.tmp 2394 mv $3.tmp $3 2395 2396 # Clean up 2397 rm $1-paths newhashes $3-oldlines $3-newlines 2398 rm -rf merge/ 2399 fi 2400 2401 # We're done with merging files. 2402 rm $1 2403} 2404 2405# Do the work involved in fetching upgrades to a new release 2406upgrade_run () { 2407 workdir_init || return 1 2408 2409 # Prepare the mirror list. 2410 fetch_pick_server_init && fetch_pick_server 2411 2412 # Try to fetch the public key until we run out of servers. 2413 while ! fetch_key; do 2414 fetch_pick_server || return 1 2415 done 2416 2417 # Try to fetch the metadata index signature ("tag") until we run 2418 # out of available servers; and sanity check the downloaded tag. 2419 while ! fetch_tag; do 2420 fetch_pick_server || return 1 2421 done 2422 fetch_tagsanity || return 1 2423 2424 # Fetch the INDEX-OLD and INDEX-ALL. 2425 fetch_metadata INDEX-OLD INDEX-ALL || return 1 2426 2427 # If StrictComponents is not "yes", generate a new components list 2428 # with only the components which appear to be installed. 2429 upgrade_guess_components INDEX-ALL || return 1 2430 2431 # Generate filtered INDEX-OLD and INDEX-ALL files containing only 2432 # the components we want and without anything marked as "Ignore". 2433 fetch_filter_metadata INDEX-OLD || return 1 2434 fetch_filter_metadata INDEX-ALL || return 1 2435 2436 # Merge the INDEX-OLD and INDEX-ALL files into INDEX-OLD. 2437 sort INDEX-OLD INDEX-ALL > INDEX-OLD.tmp 2438 mv INDEX-OLD.tmp INDEX-OLD 2439 rm INDEX-ALL 2440 2441 # Adjust variables for fetching files from the new release. 2442 OLDRELNUM=${RELNUM} 2443 RELNUM=${TARGETRELEASE} 2444 OLDFETCHDIR=${FETCHDIR} 2445 FETCHDIR=${RELNUM}/${ARCH} 2446 2447 # Try to fetch the NEW metadata index signature ("tag") until we run 2448 # out of available servers; and sanity check the downloaded tag. 2449 while ! fetch_tag; do 2450 fetch_pick_server || return 1 2451 done 2452 2453 # Fetch the new INDEX-ALL. 2454 fetch_metadata INDEX-ALL || return 1 2455 2456 # If StrictComponents is not "yes", COMPONENTS contains an entry 2457 # corresponding to the currently running kernel, and said kernel 2458 # does not exist in the new release, add "kernel/generic" to the 2459 # list of components. 2460 upgrade_guess_new_kernel INDEX-ALL || return 1 2461 2462 # Filter INDEX-ALL to contain only the components we want and without 2463 # anything marked as "Ignore". 2464 fetch_filter_metadata INDEX-ALL || return 1 2465 2466 # Convert INDEX-OLD (last release) and INDEX-ALL (new release) into 2467 # INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades). 2468 upgrade_oldall_to_oldnew INDEX-OLD INDEX-ALL INDEX-NEW 2469 2470 # Translate /boot/${KERNCONF} or /boot/${NKERNCONF} into ${KERNELDIR} 2471 fetch_filter_kernel_names INDEX-NEW ${NKERNCONF} 2472 fetch_filter_kernel_names INDEX-OLD ${KERNCONF} 2473 2474 # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the 2475 # system and generate an INDEX-PRESENT file. 2476 fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2477 2478 # Based on ${MERGECHANGES}, generate a file tomerge-old with the 2479 # paths and hashes of old versions of files to merge. 2480 fetch_filter_mergechanges INDEX-OLD INDEX-PRESENT tomerge-old 2481 2482 # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which 2483 # correspond to lines in INDEX-PRESENT with hashes not appearing 2484 # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in 2485 # INDEX-PRESENT has type - and there isn't a corresponding entry in 2486 # INDEX-OLD with type -. 2487 fetch_filter_unmodified_notpresent \ 2488 INDEX-OLD INDEX-PRESENT INDEX-NEW tomerge-old 2489 2490 # For each entry in INDEX-PRESENT of type -, remove any corresponding 2491 # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries 2492 # of type - from INDEX-PRESENT. 2493 fetch_filter_allowadd INDEX-PRESENT INDEX-NEW 2494 2495 # If ${ALLOWDELETE} != "yes", then remove any entries from 2496 # INDEX-PRESENT which don't correspond to entries in INDEX-NEW. 2497 fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW 2498 2499 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in 2500 # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD, 2501 # replace the corresponding line of INDEX-NEW with one having the 2502 # same metadata as the entry in INDEX-PRESENT. 2503 fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW 2504 2505 # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical; 2506 # no need to update a file if it isn't changing. 2507 fetch_filter_uptodate INDEX-PRESENT INDEX-NEW 2508 2509 # Fetch "clean" files from the old release for merging changes. 2510 fetch_files_premerge tomerge-old 2511 2512 # Prepare to fetch files: Generate a list of the files we need, 2513 # copy the unmodified files we have into /files/, and generate 2514 # a list of patches to download. 2515 fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1 2516 2517 # Fetch patches from to-${RELNUM}/${ARCH}/bp/ 2518 PATCHDIR=to-${RELNUM}/${ARCH}/bp 2519 fetch_files || return 1 2520 2521 # Merge configuration file changes. 2522 upgrade_merge tomerge-old INDEX-PRESENT INDEX-NEW || return 1 2523 2524 # Create and populate install manifest directory; and report what 2525 # updates are available. 2526 fetch_create_manifest || return 1 2527 2528 # Leave a note behind to tell the "install" command that the kernel 2529 # needs to be installed before the world. 2530 touch ${BDHASH}-install/kernelfirst 2531} 2532 2533# Make sure that all the file hashes mentioned in $@ have corresponding 2534# gzipped files stored in /files/. 2535install_verify () { 2536 # Generate a list of hashes 2537 cat $@ | 2538 cut -f 2,7 -d '|' | 2539 grep -E '^f' | 2540 cut -f 2 -d '|' | 2541 sort -u > filelist 2542 2543 # Make sure all the hashes exist 2544 while read HASH; do 2545 if ! [ -f files/${HASH}.gz ]; then 2546 echo -n "Update files missing -- " 2547 echo "this should never happen." 2548 echo "Re-run '$0 fetch'." 2549 return 1 2550 fi 2551 done < filelist 2552 2553 # Clean up 2554 rm filelist 2555} 2556 2557# Remove the system immutable flag from files 2558install_unschg () { 2559 # Generate file list 2560 cat $@ | 2561 cut -f 1 -d '|' > filelist 2562 2563 # Remove flags 2564 while read F; do 2565 if ! [ -e ${BASEDIR}/${F} ]; then 2566 continue 2567 fi 2568 2569 chflags noschg ${BASEDIR}/${F} || return 1 2570 done < filelist 2571 2572 # Clean up 2573 rm filelist 2574} 2575 2576# Decide which directory name to use for kernel backups. 2577backup_kernel_finddir () { 2578 CNT=0 2579 while true ; do 2580 # Pathname does not exist, so it is OK use that name 2581 # for backup directory. 2582 if [ ! -e $BACKUPKERNELDIR ]; then 2583 return 0 2584 fi 2585 2586 # If directory do exist, we only use if it has our 2587 # marker file. 2588 if [ -d $BACKUPKERNELDIR -a \ 2589 -e $BACKUPKERNELDIR/.freebsd-update ]; then 2590 return 0 2591 fi 2592 2593 # We could not use current directory name, so add counter to 2594 # the end and try again. 2595 CNT=$((CNT + 1)) 2596 if [ $CNT -gt 9 ]; then 2597 echo "Could not find valid backup dir ($BACKUPKERNELDIR)" 2598 exit 1 2599 fi 2600 BACKUPKERNELDIR="`echo $BACKUPKERNELDIR | sed -Ee 's/[0-9]\$//'`" 2601 BACKUPKERNELDIR="${BACKUPKERNELDIR}${CNT}" 2602 done 2603} 2604 2605# Backup the current kernel using hardlinks, if not disabled by user. 2606# Since we delete all files in the directory used for previous backups 2607# we create a marker file called ".freebsd-update" in the directory so 2608# we can determine on the next run that the directory was created by 2609# freebsd-update and we then do not accidentally remove user files in 2610# the unlikely case that the user has created a directory with a 2611# conflicting name. 2612backup_kernel () { 2613 # Only make kernel backup is so configured. 2614 if [ $BACKUPKERNEL != yes ]; then 2615 return 0 2616 fi 2617 2618 # Decide which directory name to use for kernel backups. 2619 backup_kernel_finddir 2620 2621 # Remove old kernel backup files. If $BACKUPKERNELDIR was 2622 # "not ours", backup_kernel_finddir would have exited, so 2623 # deleting the directory content is as safe as we can make it. 2624 if [ -d $BACKUPKERNELDIR ]; then 2625 rm -f $BACKUPKERNELDIR/* 2626 fi 2627 2628 # Create directory for backup if it doesn't exist. 2629 mkdir -p $BACKUPKERNELDIR 2630 2631 # Mark the directory as having been created by freebsd-update. 2632 touch $BACKUPKERNELDIR/.freebsd-update 2633 if [ $? -ne 0 ]; then 2634 echo "Could not create kernel backup directory" 2635 exit 1 2636 fi 2637 2638 # Disable pathname expansion to be sure *.symbols is not 2639 # expanded. 2640 set -f 2641 2642 # Use find to ignore symbol files, unless disabled by user. 2643 if [ $BACKUPKERNELSYMBOLFILES = yes ]; then 2644 FINDFILTER="" 2645 else 2646 FINDFILTER=-"a ! -name *.symbols" 2647 fi 2648 2649 # Backup all the kernel files using hardlinks. 2650 find $KERNELDIR -type f $FINDFILTER | \ 2651 sed -Ee "s,($KERNELDIR)/?(.*),\1/\2 ${BACKUPKERNELDIR}/\2," | \ 2652 xargs -n 2 cp -pl 2653 2654 # Re-enable patchname expansion. 2655 set +f 2656} 2657 2658# Install new files 2659install_from_index () { 2660 # First pass: Do everything apart from setting file flags. We 2661 # can't set flags yet, because schg inhibits hard linking. 2662 sort -k 1,1 -t '|' $1 | 2663 tr '|' ' ' | 2664 while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do 2665 case ${TYPE} in 2666 d) 2667 # Create a directory 2668 install -d -o ${OWNER} -g ${GROUP} \ 2669 -m ${PERM} ${BASEDIR}/${FPATH} 2670 ;; 2671 f) 2672 if [ -z "${LINK}" ]; then 2673 # Create a file, without setting flags. 2674 gunzip < files/${HASH}.gz > ${HASH} 2675 install -S -o ${OWNER} -g ${GROUP} \ 2676 -m ${PERM} ${HASH} ${BASEDIR}/${FPATH} 2677 rm ${HASH} 2678 else 2679 # Create a hard link. 2680 ln -f ${BASEDIR}/${LINK} ${BASEDIR}/${FPATH} 2681 fi 2682 ;; 2683 L) 2684 # Create a symlink 2685 ln -sfh ${HASH} ${BASEDIR}/${FPATH} 2686 ;; 2687 esac 2688 done 2689 2690 # Perform a second pass, adding file flags. 2691 tr '|' ' ' < $1 | 2692 while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do 2693 if [ ${TYPE} = "f" ] && 2694 ! [ ${FLAGS} = "0" ]; then 2695 chflags ${FLAGS} ${BASEDIR}/${FPATH} 2696 fi 2697 done 2698} 2699 2700# Remove files which we want to delete 2701install_delete () { 2702 # Generate list of new files 2703 cut -f 1 -d '|' < $2 | 2704 sort > newfiles 2705 2706 # Generate subindex of old files we want to nuke 2707 sort -k 1,1 -t '|' $1 | 2708 join -t '|' -v 1 - newfiles | 2709 sort -r -k 1,1 -t '|' | 2710 cut -f 1,2 -d '|' | 2711 tr '|' ' ' > killfiles 2712 2713 # Remove the offending bits 2714 while read FPATH TYPE; do 2715 case ${TYPE} in 2716 d) 2717 rmdir ${BASEDIR}/${FPATH} 2718 ;; 2719 f) 2720 rm ${BASEDIR}/${FPATH} 2721 ;; 2722 L) 2723 rm ${BASEDIR}/${FPATH} 2724 ;; 2725 esac 2726 done < killfiles 2727 2728 # Clean up 2729 rm newfiles killfiles 2730} 2731 2732# Install new files, delete old files, and update linker.hints 2733install_files () { 2734 # If we haven't already dealt with the kernel, deal with it. 2735 if ! [ -f $1/kerneldone ]; then 2736 grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD 2737 grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW 2738 2739 # Backup current kernel before installing a new one 2740 backup_kernel || return 1 2741 2742 # Install new files 2743 install_from_index INDEX-NEW || return 1 2744 2745 # Remove files which need to be deleted 2746 install_delete INDEX-OLD INDEX-NEW || return 1 2747 2748 # Update linker.hints if necessary 2749 if [ -s INDEX-OLD -o -s INDEX-NEW ]; then 2750 kldxref -R /boot/ 2>/dev/null 2751 fi 2752 2753 # We've finished updating the kernel. 2754 touch $1/kerneldone 2755 2756 # Do we need to ask for a reboot now? 2757 if [ -f $1/kernelfirst ] && 2758 [ -s INDEX-OLD -o -s INDEX-NEW ]; then 2759 cat <<-EOF 2760 2761Kernel updates have been installed. Please reboot and run 2762"$0 install" again to finish installing updates. 2763 EOF 2764 exit 0 2765 fi 2766 fi 2767 2768 # If we haven't already dealt with the world, deal with it. 2769 if ! [ -f $1/worlddone ]; then 2770 # Install new shared libraries next 2771 grep -vE '^/boot/' $1/INDEX-NEW | 2772 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2773 install_from_index INDEX-NEW || return 1 2774 2775 # Deal with everything else 2776 grep -vE '^/boot/' $1/INDEX-OLD | 2777 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2778 grep -vE '^/boot/' $1/INDEX-NEW | 2779 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2780 install_from_index INDEX-NEW || return 1 2781 install_delete INDEX-OLD INDEX-NEW || return 1 2782 2783 # Rebuild /etc/spwd.db and /etc/pwd.db if necessary. 2784 if [ /etc/master.passwd -nt /etc/spwd.db ] || 2785 [ /etc/master.passwd -nt /etc/pwd.db ]; then 2786 pwd_mkdb /etc/master.passwd 2787 fi 2788 2789 # Rebuild /etc/login.conf.db if necessary. 2790 if [ /etc/login.conf -nt /etc/login.conf.db ]; then 2791 cap_mkdb /etc/login.conf 2792 fi 2793 2794 # We've finished installing the world and deleting old files 2795 # which are not shared libraries. 2796 touch $1/worlddone 2797 2798 # Do we need to ask the user to portupgrade now? 2799 grep -vE '^/boot/' $1/INDEX-NEW | 2800 grep -E '/lib/.*\.so\.[0-9]+\|' | 2801 cut -f 1 -d '|' | 2802 sort > newfiles 2803 if grep -vE '^/boot/' $1/INDEX-OLD | 2804 grep -E '/lib/.*\.so\.[0-9]+\|' | 2805 cut -f 1 -d '|' | 2806 sort | 2807 join -v 1 - newfiles | 2808 grep -q .; then 2809 cat <<-EOF 2810 2811Completing this upgrade requires removing old shared object files. 2812Please rebuild all installed 3rd party software (e.g., programs 2813installed from the ports tree) and then run "$0 install" 2814again to finish installing updates. 2815 EOF 2816 rm newfiles 2817 exit 0 2818 fi 2819 rm newfiles 2820 fi 2821 2822 # Remove old shared libraries 2823 grep -vE '^/boot/' $1/INDEX-NEW | 2824 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2825 grep -vE '^/boot/' $1/INDEX-OLD | 2826 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2827 install_delete INDEX-OLD INDEX-NEW || return 1 2828 2829 # Remove temporary files 2830 rm INDEX-OLD INDEX-NEW 2831} 2832 2833# Rearrange bits to allow the installed updates to be rolled back 2834install_setup_rollback () { 2835 # Remove the "reboot after installing kernel", "kernel updated", and 2836 # "finished installing the world" flags if present -- they are 2837 # irrelevant when rolling back updates. 2838 if [ -f ${BDHASH}-install/kernelfirst ]; then 2839 rm ${BDHASH}-install/kernelfirst 2840 rm ${BDHASH}-install/kerneldone 2841 fi 2842 if [ -f ${BDHASH}-install/worlddone ]; then 2843 rm ${BDHASH}-install/worlddone 2844 fi 2845 2846 if [ -L ${BDHASH}-rollback ]; then 2847 mv ${BDHASH}-rollback ${BDHASH}-install/rollback 2848 fi 2849 2850 mv ${BDHASH}-install ${BDHASH}-rollback 2851} 2852 2853# Actually install updates 2854install_run () { 2855 echo -n "Installing updates..." 2856 2857 # Make sure we have all the files we should have 2858 install_verify ${BDHASH}-install/INDEX-OLD \ 2859 ${BDHASH}-install/INDEX-NEW || return 1 2860 2861 # Remove system immutable flag from files 2862 install_unschg ${BDHASH}-install/INDEX-OLD \ 2863 ${BDHASH}-install/INDEX-NEW || return 1 2864 2865 # Install new files, delete old files, and update linker.hints 2866 install_files ${BDHASH}-install || return 1 2867 2868 # Rearrange bits to allow the installed updates to be rolled back 2869 install_setup_rollback 2870 2871 echo " done." 2872} 2873 2874# Rearrange bits to allow the previous set of updates to be rolled back next. 2875rollback_setup_rollback () { 2876 if [ -L ${BDHASH}-rollback/rollback ]; then 2877 mv ${BDHASH}-rollback/rollback rollback-tmp 2878 rm -r ${BDHASH}-rollback/ 2879 rm ${BDHASH}-rollback 2880 mv rollback-tmp ${BDHASH}-rollback 2881 else 2882 rm -r ${BDHASH}-rollback/ 2883 rm ${BDHASH}-rollback 2884 fi 2885} 2886 2887# Install old files, delete new files, and update linker.hints 2888rollback_files () { 2889 # Install old shared library files which don't have the same path as 2890 # a new shared library file. 2891 grep -vE '^/boot/' $1/INDEX-NEW | 2892 grep -E '/lib/.*\.so\.[0-9]+\|' | 2893 cut -f 1 -d '|' | 2894 sort > INDEX-NEW.libs.flist 2895 grep -vE '^/boot/' $1/INDEX-OLD | 2896 grep -E '/lib/.*\.so\.[0-9]+\|' | 2897 sort -k 1,1 -t '|' - | 2898 join -t '|' -v 1 - INDEX-NEW.libs.flist > INDEX-OLD 2899 install_from_index INDEX-OLD || return 1 2900 2901 # Deal with files which are neither kernel nor shared library 2902 grep -vE '^/boot/' $1/INDEX-OLD | 2903 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2904 grep -vE '^/boot/' $1/INDEX-NEW | 2905 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2906 install_from_index INDEX-OLD || return 1 2907 install_delete INDEX-NEW INDEX-OLD || return 1 2908 2909 # Install any old shared library files which we didn't install above. 2910 grep -vE '^/boot/' $1/INDEX-OLD | 2911 grep -E '/lib/.*\.so\.[0-9]+\|' | 2912 sort -k 1,1 -t '|' - | 2913 join -t '|' - INDEX-NEW.libs.flist > INDEX-OLD 2914 install_from_index INDEX-OLD || return 1 2915 2916 # Delete unneeded shared library files 2917 grep -vE '^/boot/' $1/INDEX-OLD | 2918 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD 2919 grep -vE '^/boot/' $1/INDEX-NEW | 2920 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW 2921 install_delete INDEX-NEW INDEX-OLD || return 1 2922 2923 # Deal with kernel files 2924 grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD 2925 grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW 2926 install_from_index INDEX-OLD || return 1 2927 install_delete INDEX-NEW INDEX-OLD || return 1 2928 if [ -s INDEX-OLD -o -s INDEX-NEW ]; then 2929 kldxref -R /boot/ 2>/dev/null 2930 fi 2931 2932 # Remove temporary files 2933 rm INDEX-OLD INDEX-NEW INDEX-NEW.libs.flist 2934} 2935 2936# Actually rollback updates 2937rollback_run () { 2938 echo -n "Uninstalling updates..." 2939 2940 # If there are updates waiting to be installed, remove them; we 2941 # want the user to re-run 'fetch' after rolling back updates. 2942 if [ -L ${BDHASH}-install ]; then 2943 rm -r ${BDHASH}-install/ 2944 rm ${BDHASH}-install 2945 fi 2946 2947 # Make sure we have all the files we should have 2948 install_verify ${BDHASH}-rollback/INDEX-NEW \ 2949 ${BDHASH}-rollback/INDEX-OLD || return 1 2950 2951 # Remove system immutable flag from files 2952 install_unschg ${BDHASH}-rollback/INDEX-NEW \ 2953 ${BDHASH}-rollback/INDEX-OLD || return 1 2954 2955 # Install old files, delete new files, and update linker.hints 2956 rollback_files ${BDHASH}-rollback || return 1 2957 2958 # Remove the rollback directory and the symlink pointing to it; and 2959 # rearrange bits to allow the previous set of updates to be rolled 2960 # back next. 2961 rollback_setup_rollback 2962 2963 echo " done." 2964} 2965 2966# Compare INDEX-ALL and INDEX-PRESENT and print warnings about differences. 2967IDS_compare () { 2968 # Get all the lines which mismatch in something other than file 2969 # flags. We ignore file flags because sysinstall doesn't seem to 2970 # set them when it installs FreeBSD; warning about these adds a 2971 # very large amount of noise. 2972 cut -f 1-5,7-8 -d '|' $1 > $1.noflags 2973 sort -k 1,1 -t '|' $1.noflags > $1.sorted 2974 cut -f 1-5,7-8 -d '|' $2 | 2975 comm -13 $1.noflags - | 2976 fgrep -v '|-|||||' | 2977 sort -k 1,1 -t '|' | 2978 join -t '|' $1.sorted - > INDEX-NOTMATCHING 2979 2980 # Ignore files which match IDSIGNOREPATHS. 2981 for X in ${IDSIGNOREPATHS}; do 2982 grep -E "^${X}" INDEX-NOTMATCHING 2983 done | 2984 sort -u | 2985 comm -13 - INDEX-NOTMATCHING > INDEX-NOTMATCHING.tmp 2986 mv INDEX-NOTMATCHING.tmp INDEX-NOTMATCHING 2987 2988 # Go through the lines and print warnings. 2989 while read LINE; do 2990 FPATH=`echo "${LINE}" | cut -f 1 -d '|'` 2991 TYPE=`echo "${LINE}" | cut -f 2 -d '|'` 2992 OWNER=`echo "${LINE}" | cut -f 3 -d '|'` 2993 GROUP=`echo "${LINE}" | cut -f 4 -d '|'` 2994 PERM=`echo "${LINE}" | cut -f 5 -d '|'` 2995 HASH=`echo "${LINE}" | cut -f 6 -d '|'` 2996 LINK=`echo "${LINE}" | cut -f 7 -d '|'` 2997 P_TYPE=`echo "${LINE}" | cut -f 8 -d '|'` 2998 P_OWNER=`echo "${LINE}" | cut -f 9 -d '|'` 2999 P_GROUP=`echo "${LINE}" | cut -f 10 -d '|'` 3000 P_PERM=`echo "${LINE}" | cut -f 11 -d '|'` 3001 P_HASH=`echo "${LINE}" | cut -f 12 -d '|'` 3002 P_LINK=`echo "${LINE}" | cut -f 13 -d '|'` 3003 3004 # Warn about different object types. 3005 if ! [ "${TYPE}" = "${P_TYPE}" ]; then 3006 echo -n "${FPATH} is a " 3007 case "${P_TYPE}" in 3008 f) echo -n "regular file, " 3009 ;; 3010 d) echo -n "directory, " 3011 ;; 3012 L) echo -n "symlink, " 3013 ;; 3014 esac 3015 echo -n "but should be a " 3016 case "${TYPE}" in 3017 f) echo -n "regular file." 3018 ;; 3019 d) echo -n "directory." 3020 ;; 3021 L) echo -n "symlink." 3022 ;; 3023 esac 3024 echo 3025 3026 # Skip other tests, since they don't make sense if 3027 # we're comparing different object types. 3028 continue 3029 fi 3030 3031 # Warn about different owners. 3032 if ! [ "${OWNER}" = "${P_OWNER}" ]; then 3033 echo -n "${FPATH} is owned by user id ${P_OWNER}, " 3034 echo "but should be owned by user id ${OWNER}." 3035 fi 3036 3037 # Warn about different groups. 3038 if ! [ "${GROUP}" = "${P_GROUP}" ]; then 3039 echo -n "${FPATH} is owned by group id ${P_GROUP}, " 3040 echo "but should be owned by group id ${GROUP}." 3041 fi 3042 3043 # Warn about different permissions. We do not warn about 3044 # different permissions on symlinks, since some archivers 3045 # don't extract symlink permissions correctly and they are 3046 # ignored anyway. 3047 if ! [ "${PERM}" = "${P_PERM}" ] && 3048 ! [ "${TYPE}" = "L" ]; then 3049 echo -n "${FPATH} has ${P_PERM} permissions, " 3050 echo "but should have ${PERM} permissions." 3051 fi 3052 3053 # Warn about different file hashes / symlink destinations. 3054 if ! [ "${HASH}" = "${P_HASH}" ]; then 3055 if [ "${TYPE}" = "L" ]; then 3056 echo -n "${FPATH} is a symlink to ${P_HASH}, " 3057 echo "but should be a symlink to ${HASH}." 3058 fi 3059 if [ "${TYPE}" = "f" ]; then 3060 echo -n "${FPATH} has SHA256 hash ${P_HASH}, " 3061 echo "but should have SHA256 hash ${HASH}." 3062 fi 3063 fi 3064 3065 # We don't warn about different hard links, since some 3066 # some archivers break hard links, and as long as the 3067 # underlying data is correct they really don't matter. 3068 done < INDEX-NOTMATCHING 3069 3070 # Clean up 3071 rm $1 $1.noflags $1.sorted $2 INDEX-NOTMATCHING 3072} 3073 3074# Do the work involved in comparing the system to a "known good" index 3075IDS_run () { 3076 workdir_init || return 1 3077 3078 # Prepare the mirror list. 3079 fetch_pick_server_init && fetch_pick_server 3080 3081 # Try to fetch the public key until we run out of servers. 3082 while ! fetch_key; do 3083 fetch_pick_server || return 1 3084 done 3085 3086 # Try to fetch the metadata index signature ("tag") until we run 3087 # out of available servers; and sanity check the downloaded tag. 3088 while ! fetch_tag; do 3089 fetch_pick_server || return 1 3090 done 3091 fetch_tagsanity || return 1 3092 3093 # Fetch INDEX-OLD and INDEX-ALL. 3094 fetch_metadata INDEX-OLD INDEX-ALL || return 1 3095 3096 # Generate filtered INDEX-OLD and INDEX-ALL files containing only 3097 # the components we want and without anything marked as "Ignore". 3098 fetch_filter_metadata INDEX-OLD || return 1 3099 fetch_filter_metadata INDEX-ALL || return 1 3100 3101 # Merge the INDEX-OLD and INDEX-ALL files into INDEX-ALL. 3102 sort INDEX-OLD INDEX-ALL > INDEX-ALL.tmp 3103 mv INDEX-ALL.tmp INDEX-ALL 3104 rm INDEX-OLD 3105 3106 # Translate /boot/${KERNCONF} to ${KERNELDIR} 3107 fetch_filter_kernel_names INDEX-ALL ${KERNCONF} 3108 3109 # Inspect the system and generate an INDEX-PRESENT file. 3110 fetch_inspect_system INDEX-ALL INDEX-PRESENT /dev/null || return 1 3111 3112 # Compare INDEX-ALL and INDEX-PRESENT and print warnings about any 3113 # differences. 3114 IDS_compare INDEX-ALL INDEX-PRESENT 3115} 3116 3117#### Main functions -- call parameter-handling and core functions 3118 3119# Using the command line, configuration file, and defaults, 3120# set all the parameters which are needed later. 3121get_params () { 3122 init_params 3123 parse_cmdline $@ 3124 parse_conffile 3125 default_params 3126} 3127 3128# Fetch command. Make sure that we're being called 3129# interactively, then run fetch_check_params and fetch_run 3130cmd_fetch () { 3131 if [ ! -t 0 ]; then 3132 echo -n "`basename $0` fetch should not " 3133 echo "be run non-interactively." 3134 echo "Run `basename $0` cron instead." 3135 exit 1 3136 fi 3137 fetch_check_params 3138 fetch_run || exit 1 3139} 3140 3141# Cron command. Make sure the parameters are sensible; wait 3142# rand(3600) seconds; then fetch updates. While fetching updates, 3143# send output to a temporary file; only print that file if the 3144# fetching failed. 3145cmd_cron () { 3146 fetch_check_params 3147 sleep `jot -r 1 0 3600` 3148 3149 TMPFILE=`mktemp /tmp/freebsd-update.XXXXXX` || exit 1 3150 if ! fetch_run >> ${TMPFILE} || 3151 ! grep -q "No updates needed" ${TMPFILE} || 3152 [ ${VERBOSELEVEL} = "debug" ]; then 3153 mail -s "`hostname` security updates" ${MAILTO} < ${TMPFILE} 3154 fi 3155 3156 rm ${TMPFILE} 3157} 3158 3159# Fetch files for upgrading to a new release. 3160cmd_upgrade () { 3161 upgrade_check_params 3162 upgrade_run || exit 1 3163} 3164 3165# Install downloaded updates. 3166cmd_install () { 3167 install_check_params 3168 install_run || exit 1 3169} 3170 3171# Rollback most recently installed updates. 3172cmd_rollback () { 3173 rollback_check_params 3174 rollback_run || exit 1 3175} 3176 3177# Compare system against a "known good" index. 3178cmd_IDS () { 3179 IDS_check_params 3180 IDS_run || exit 1 3181} 3182 3183#### Entry point 3184 3185# Make sure we find utilities from the base system 3186export PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH} 3187 3188# Set LC_ALL in order to avoid problems with character ranges like [A-Z]. 3189export LC_ALL=C 3190 3191get_params $@ 3192for COMMAND in ${COMMANDS}; do 3193 cmd_${COMMAND} 3194done 3195